Skip to content

Commit

Permalink
init: rework it for MSVC
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Jan 27, 2025
1 parent 09d97c1 commit 8f62877
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 31 deletions.
2 changes: 2 additions & 0 deletions ortools/init/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ package(default_visibility = ["//visibility:public"])
cc_library(
name = "init",
hdrs = ["init.h"],
srcs = ["init.cc"],
deps = [
"//ortools/base",
"//ortools/gurobi:environment",
"//ortools/sat:cp_model_solver",
"//ortools/sat:cp_model_solver_helpers",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:globals",
Expand Down
2 changes: 1 addition & 1 deletion ortools/init/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

file(GLOB _SRCS "*.h")
file(GLOB _SRCS "*.h" "*.cc")
set(NAME ${PROJECT_NAME}_init)

# Will be merge in libortools.so
Expand Down
46 changes: 46 additions & 0 deletions ortools/init/init.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2010-2025 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "ortools/init/init.h"

#include "absl/flags/flag.h"
#include "absl/flags/usage.h"
#include "absl/log/globals.h"
#include "absl/log/initialize.h"
#include "ortools/gurobi/environment.h"
#include "ortools/sat/cp_model_solver.h"
#include "ortools/sat/cp_model_solver_helpers.h"

namespace operations_research {
void CppBridge::InitLogging(const std::string& usage) {
absl::SetProgramUsageMessage(usage);
absl::InitializeLog();
}

void CppBridge::SetFlags(const CppFlags& flags) {
absl::SetFlag(&FLAGS_stderrthreshold, flags.stderrthreshold);
absl::EnableLogPrefix(flags.log_prefix);
if (!flags.cp_model_dump_prefix.empty()) {
absl::SetFlag(&FLAGS_cp_model_dump_prefix, flags.cp_model_dump_prefix);
}
absl::SetFlag(&FLAGS_cp_model_dump_models, flags.cp_model_dump_models);
absl::SetFlag(&FLAGS_cp_model_dump_submodels,
flags.cp_model_dump_submodels);
absl::SetFlag(&FLAGS_cp_model_dump_response, flags.cp_model_dump_response);
}

bool CppBridge::LoadGurobiSharedLibrary(const std::string& full_library_path) {
return LoadGurobiDynamicLibrary({full_library_path}).ok();
}

} // namespace operations_research
34 changes: 4 additions & 30 deletions ortools/init/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,9 @@
#include <string>
#include <vector>

#include "absl/flags/flag.h"
#include "absl/flags/usage.h"
#include "absl/log/globals.h"
#include "absl/log/initialize.h"
#include "ortools/base/logging.h"
#include "ortools/base/version.h"
#include "ortools/gurobi/environment.h"
#include "ortools/sat/cp_model_solver.h"

ABSL_DECLARE_FLAG(std::string, cp_model_dump_prefix);
ABSL_DECLARE_FLAG(bool, cp_model_dump_models);
ABSL_DECLARE_FLAG(bool, cp_model_dump_submodels);
ABSL_DECLARE_FLAG(bool, cp_model_dump_response);
ABSL_DECLARE_FLAG(int, stderrthreshold);
#include "ortools/sat/cp_model_solver_helpers.h"

namespace operations_research {

Expand Down Expand Up @@ -97,10 +86,7 @@ class CppBridge {
*
* This must be called once before any other library from OR-Tools are used.
*/
static void InitLogging(const std::string& usage) {
absl::SetProgramUsageMessage(usage);
absl::InitializeLog();
}
static void InitLogging(const std::string& usage);

/**
* Shutdown the C++ logging layer.
Expand All @@ -115,17 +101,7 @@ class CppBridge {
/**
* Sets all the C++ flags contained in the CppFlags structure.
*/
static void SetFlags(const CppFlags& flags) {
absl::SetFlag(&FLAGS_stderrthreshold, flags.stderrthreshold);
absl::EnableLogPrefix(flags.log_prefix);
if (!flags.cp_model_dump_prefix.empty()) {
absl::SetFlag(&FLAGS_cp_model_dump_prefix, flags.cp_model_dump_prefix);
}
absl::SetFlag(&FLAGS_cp_model_dump_models, flags.cp_model_dump_models);
absl::SetFlag(&FLAGS_cp_model_dump_submodels,
flags.cp_model_dump_submodels);
absl::SetFlag(&FLAGS_cp_model_dump_response, flags.cp_model_dump_response);
}
static void SetFlags(const CppFlags& flags);

/**
* Load the gurobi shared library.
Expand All @@ -135,9 +111,7 @@ class CppBridge {
* You need to pass the full path, including the shared library file.
* It returns true if the library was found and correctly loaded.
*/
static bool LoadGurobiSharedLibrary(const std::string& full_library_path) {
return LoadGurobiDynamicLibrary({full_library_path}).ok();
}
static bool LoadGurobiSharedLibrary(const std::string& full_library_path);

/**
* Delete a temporary C++ byte array.
Expand Down
2 changes: 2 additions & 0 deletions ortools/sat/cp_model_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "ortools/sat/model.h"
#include "ortools/sat/sat_parameters.pb.h"

ABSL_DECLARE_FLAG(bool, cp_model_dump_response);

namespace operations_research {
namespace sat {

Expand Down

0 comments on commit 8f62877

Please sign in to comment.