diff --git a/ortools/init/BUILD.bazel b/ortools/init/BUILD.bazel index efe615cddb..95b55c918c 100644 --- a/ortools/init/BUILD.bazel +++ b/ortools/init/BUILD.bazel @@ -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", diff --git a/ortools/init/CMakeLists.txt b/ortools/init/CMakeLists.txt index f5fbec03c0..25f6fa98dc 100644 --- a/ortools/init/CMakeLists.txt +++ b/ortools/init/CMakeLists.txt @@ -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 diff --git a/ortools/init/init.cc b/ortools/init/init.cc new file mode 100644 index 0000000000..c02a33f709 --- /dev/null +++ b/ortools/init/init.cc @@ -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 diff --git a/ortools/init/init.h b/ortools/init/init.h index 76e182f3bc..142ecd449e 100644 --- a/ortools/init/init.h +++ b/ortools/init/init.h @@ -18,20 +18,9 @@ #include #include -#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 { @@ -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. @@ -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. @@ -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. diff --git a/ortools/sat/cp_model_solver.h b/ortools/sat/cp_model_solver.h index 3bec9b5278..90ef155a70 100644 --- a/ortools/sat/cp_model_solver.h +++ b/ortools/sat/cp_model_solver.h @@ -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 {