cmake: Fix and enabled shared lib support with MSVC

* Add decldll to proto
* rework init to make it working for MSVC
* fix test_xprs_interface build
dotnet: Fix MSVC shared libs support
  * Fix csproj to include libortools.dll
java: Fix MSVC shared_libs support
  * Fix runtime jar to include libortools.dll
python: Fix MSVC shared_libs support
  * fix __init__.py.in loading for MSVC
This commit is contained in:
Corentin Le Molgat
2025-01-27 02:01:03 -08:00
committed by Mizux Seiha
parent befa5f7a4f
commit da058cd01b
36 changed files with 189 additions and 100 deletions

View File

@@ -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",

View File

@@ -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
@@ -21,6 +21,10 @@ set_target_properties(${NAME} PROPERTIES
LINKER_LANGUAGE CXX
POSITION_INDEPENDENT_CODE ON
)
if(MSVC AND BUILD_SHARED_LIBS)
target_compile_definitions(${NAME} PUBLIC "OR_BUILD_DLL")
target_compile_definitions(${NAME} PRIVATE "OR_EXPORT")
endif()
target_include_directories(${NAME} PRIVATE
${PROJECT_SOURCE_DIR}
${PROJECT_BINARY_DIR})

46
ortools/init/init.cc Normal file
View File

@@ -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

View File

@@ -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 {
@@ -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.