Skip to content

Commit d3af4d7

Browse files
committed
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
1 parent 998314a commit d3af4d7

38 files changed

+194
-105
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ if(UNIX)
7474
endforeach()
7575
else()
7676
# Currently Only support static build for windows
77-
option(BUILD_SHARED_LIBS "Build shared libraries (.dll)." OFF)
77+
option(BUILD_SHARED_LIBS "Build shared libraries (.dll)." ON)
7878
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
7979
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
8080
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})

cmake/cpp.cmake

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@ set(OR_TOOLS_COMPILE_DEFINITIONS)
4545
set(OR_TOOLS_COMPILE_OPTIONS)
4646
set(OR_TOOLS_LINK_OPTIONS)
4747

48-
if(BUILD_SHARED_LIBS)
49-
list(APPEND OR_TOOLS_COMPILE_DEFINITIONS "OR_TOOLS_AS_DYNAMIC_LIB")
48+
if(MSVC AND BUILD_SHARED_LIBS)
49+
list(APPEND OR_TOOLS_COMPILE_DEFINITIONS "OR_BUILD_DLL")
50+
list(APPEND OR_TOOLS_COMPILE_DEFINITIONS "OR_PROTO_DLL=__declspec(dllimport)")
51+
else()
52+
list(APPEND OR_TOOLS_COMPILE_DEFINITIONS "OR_PROTO_DLL=")
5053
endif()
54+
5155
# Optional built-in components
5256
if(BUILD_LP_PARSER)
5357
list(APPEND OR_TOOLS_COMPILE_DEFINITIONS "USE_LP_PARSER")
@@ -356,7 +360,7 @@ function(generate_proto_library)
356360
COMMAND ${PROTOC_PRG}
357361
"--proto_path=${PROJECT_SOURCE_DIR}"
358362
${PROTO_DIRS}
359-
"--cpp_out=${PROJECT_BINARY_DIR}"
363+
"--cpp_out=dllexport_decl=OR_PROTO_DLL:${PROJECT_BINARY_DIR}"
360364
${PROTO_FILE}
361365
DEPENDS ${PROTO_FILE} ${PROTOC_PRG}
362366
COMMENT "Generate C++ protocol buffer for ${PROTO_FILE}"
@@ -383,6 +387,12 @@ function(generate_proto_library)
383387
#$<TARGET_PROPERTY:protobuf::libprotobuf,INTERFACE_INCLUDE_DIRECTORIES>
384388
)
385389
target_compile_definitions(${PROTO_NAME}_proto PUBLIC ${OR_TOOLS_COMPILE_DEFINITIONS})
390+
if(MSVC AND BUILD_SHARED_LIBS)
391+
target_compile_definitions(${PROTO_NAME}_proto INTERFACE "OR_PROTO_DLL=__declspec(dllimport)")
392+
target_compile_definitions(${PROTO_NAME}_proto PRIVATE "OR_PROTO_DLL=__declspec(dllexport)")
393+
else()
394+
target_compile_definitions(${PROTO_NAME}_proto PUBLIC "OR_PROTO_DLL=")
395+
endif()
386396
target_compile_options(${PROTO_NAME}_proto PUBLIC ${OR_TOOLS_COMPILE_OPTIONS})
387397
target_link_libraries(${PROTO_NAME}_proto PUBLIC protobuf::libprotobuf ${PROTO_LINK_LIBRARIES})
388398
add_library(${PROJECT_NAMESPACE}::${PROTO_NAME}_proto ALIAS ${PROTO_NAME}_proto)
@@ -456,6 +466,9 @@ set_target_properties(${PROJECT_NAME} PROPERTIES
456466
target_compile_features(${PROJECT_NAME} PUBLIC
457467
$<IF:$<CXX_COMPILER_ID:MSVC>,cxx_std_20,cxx_std_17>)
458468
target_compile_definitions(${PROJECT_NAME} PUBLIC ${OR_TOOLS_COMPILE_DEFINITIONS})
469+
if(MSVC AND BUILD_SHARED_LIBS)
470+
target_compile_definitions(${PROJECT_NAME} PRIVATE OR_EXPORT)
471+
endif()
459472
target_compile_options(${PROJECT_NAME} PUBLIC ${OR_TOOLS_COMPILE_OPTIONS})
460473
target_link_options(${PROJECT_NAME} INTERFACE ${OR_TOOLS_LINK_OPTIONS})
461474
# Properties

cmake/docker/web/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ FROM archlinux:latest AS env
44

55
# Install system build dependencies
66
ENV PATH=/usr/local/bin:$PATH
7-
RUN pacman -Syu --noconfirm git base-devel cmake
7+
RUN pacman -Syu --noconfirm git base-devel glibc cmake
88
RUN cmake -version
99
RUN pacman -Syu --noconfirm nodejs emscripten
1010
ENV PATH=/usr/lib/emscripten:$PATH

cmake/dotnet.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ include(UseSWIG)
3131
if(UNIX AND NOT APPLE)
3232
list(APPEND CMAKE_SWIG_FLAGS "-DSWIGWORDSIZE64")
3333
endif()
34+
list(APPEND CMAKE_SWIG_FLAGS "-DOR_DLL=")
3435

3536
# Find dotnet cli
3637
find_program(DOTNET_EXECUTABLE NAMES dotnet)
@@ -397,6 +398,8 @@ set(need_unix_highs_lib "$<AND:${is_not_windows},$<BOOL:${BUILD_HIGHS}>>")
397398
set(need_windows_highs_lib "$<AND:${is_windows},$<BOOL:${BUILD_HIGHS}>>")
398399

399400
set(is_ortools_shared "$<STREQUAL:$<TARGET_PROPERTY:ortools,TYPE>,SHARED_LIBRARY>")
401+
set(need_unix_ortools_lib "$<AND:${is_not_windows},${is_ortools_shared}>")
402+
set(need_windows_ortools_lib "$<AND:${is_windows},${is_ortools_shared}>")
400403

401404
configure_file(
402405
${PROJECT_SOURCE_DIR}/ortools/dotnet/${DOTNET_PACKAGE}.runtime.csproj.in

cmake/java.cmake

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ endif()
3131
if(UNIX AND NOT APPLE)
3232
list(APPEND CMAKE_SWIG_FLAGS "-DSWIGWORDSIZE64")
3333
endif()
34+
list(APPEND CMAKE_SWIG_FLAGS "-DOR_DLL=")
3435

3536
# Find Java and JNI
3637
find_package(Java 1.8 COMPONENTS Development REQUIRED)
@@ -300,14 +301,13 @@ set(need_unix_highs_lib "$<AND:${is_not_windows},$<BOOL:${BUILD_HIGHS}>>")
300301
set(need_windows_highs_lib "$<AND:${is_windows},$<BOOL:${BUILD_HIGHS}>>")
301302

302303
set(is_ortools_shared "$<STREQUAL:$<TARGET_PROPERTY:ortools,TYPE>,SHARED_LIBRARY>")
304+
set(need_unix_ortools_lib "$<AND:${is_not_windows},${is_ortools_shared}>")
305+
set(need_windows_ortools_lib "$<AND:${is_windows},${is_ortools_shared}>")
303306

304307
add_custom_command(
305308
OUTPUT ${JAVA_NATIVE_PROJECT_DIR}/timestamp
306309
COMMAND ${CMAKE_COMMAND} -E remove -f timestamp
307-
COMMAND ${CMAKE_COMMAND} -E copy
308-
$<TARGET_FILE:jni${JAVA_ARTIFACT}>
309-
$<${is_ortools_shared}:$<TARGET_SONAME_FILE:ortools>>
310-
${JAVA_RESSOURCES_PATH}/${JAVA_NATIVE_PROJECT}/
310+
COMMAND ${CMAKE_COMMAND} -E make_directory ${JAVA_RESSOURCES_PATH}/${JAVA_NATIVE_PROJECT}
311311
COMMAND ${CMAKE_COMMAND} -E
312312
$<IF:$<BOOL:${BUILD_ZLIB}>,copy,true>
313313
$<${need_unix_zlib_lib}:$<TARGET_SONAME_FILE:ZLIB::ZLIB>>
@@ -447,6 +447,16 @@ add_custom_command(
447447
$<${need_windows_highs_lib}:$<TARGET_FILE:highs>>
448448
${JAVA_RESSOURCES_PATH}/${JAVA_NATIVE_PROJECT}/
449449

450+
COMMAND ${CMAKE_COMMAND} -E
451+
$<IF:${is_ortools_shared},copy,true>
452+
$<${need_unix_ortools_lib}:$<TARGET_SONAME_FILE:${PROJECT_NAMESPACE}::ortools>>
453+
$<${need_windows_ortools_lib}:$<TARGET_FILE:${PROJECT_NAMESPACE}::ortools>>
454+
${JAVA_RESSOURCES_PATH}/${JAVA_NATIVE_PROJECT}/
455+
456+
COMMAND ${CMAKE_COMMAND} -E copy
457+
$<TARGET_FILE:jni${JAVA_ARTIFACT}>
458+
${JAVA_RESSOURCES_PATH}/${JAVA_NATIVE_PROJECT}/
459+
450460
COMMAND ${MAVEN_EXECUTABLE} compile -B
451461
COMMAND ${MAVEN_EXECUTABLE} package -B $<$<BOOL:${BUILD_FAT_JAR}>:-Dfatjar=true>
452462
COMMAND ${MAVEN_EXECUTABLE} install -B $<$<BOOL:${SKIP_GPG}>:-Dgpg.skip=true>

cmake/python.cmake

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ if(UNIX AND NOT APPLE AND NOT (CMAKE_SYSTEM_NAME STREQUAL "OpenBSD"))
3838
list(APPEND CMAKE_SWIG_FLAGS "-DSWIGWORDSIZE32")
3939
endif()
4040
endif()
41+
list(APPEND CMAKE_SWIG_FLAGS "-DOR_DLL=")
4142

4243
# Find Python 3
4344
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
@@ -456,6 +457,8 @@ set(need_unix_highs_lib "$<AND:${is_not_windows},$<BOOL:${BUILD_HIGHS}>>")
456457
set(need_windows_highs_lib "$<AND:${is_windows},$<BOOL:${BUILD_HIGHS}>>")
457458

458459
set(is_ortools_shared "$<STREQUAL:$<TARGET_PROPERTY:ortools,TYPE>,SHARED_LIBRARY>")
460+
set(need_unix_ortools_lib "$<AND:${is_not_windows},${is_ortools_shared}>")
461+
set(need_windows_ortools_lib "$<AND:${is_windows},${is_ortools_shared}>")
459462

460463
add_custom_command(
461464
OUTPUT python/ortools_timestamp
@@ -556,8 +559,9 @@ add_custom_command(
556559
${PYTHON_PROJECT}/.libs
557560

558561
COMMAND ${CMAKE_COMMAND} -E
559-
$<IF:${need_unix_re2_lib},copy,true>
562+
$<IF:$<BOOL:${BUILD_re2}>,copy,true>
560563
$<${need_unix_re2_lib}:$<TARGET_SONAME_FILE:re2::re2>>
564+
$<${need_windows_re2_lib}:$<TARGET_FILE:re2::re2>>
561565
${PYTHON_PROJECT}/.libs
562566

563567
COMMAND ${CMAKE_COMMAND} -E
@@ -601,7 +605,8 @@ add_custom_command(
601605

602606
COMMAND ${CMAKE_COMMAND} -E
603607
$<IF:${is_ortools_shared},copy,true>
604-
$<${is_ortools_shared}:$<TARGET_SONAME_FILE:ortools>>
608+
$<${need_unix_ortools_lib}:$<TARGET_SONAME_FILE:${PROJECT_NAMESPACE}::ortools>>
609+
$<${need_windows_ortools_lib}:$<TARGET_FILE:${PROJECT_NAMESPACE}::ortools>>
605610
${PYTHON_PROJECT}/.libs
606611
COMMAND ${CMAKE_COMMAND} -E touch ${PROJECT_BINARY_DIR}/python/ortools_timestamp
607612
MAIN_DEPENDENCY

examples/cpp/nqueens.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ ABSL_FLAG(
3939
int, size, 0,
4040
"Size of the problem. If equal to 0, will test several increasing sizes.");
4141
ABSL_FLAG(bool, use_symmetry, false, "Use Symmetry Breaking methods");
42-
ABSL_DECLARE_FLAG(bool, cp_disable_solve);
4342

4443
static const int kNumSolutions[] = {
4544
1, 0, 0, 2, 10, 4, 40, 92, 352, 724, 2680, 14200, 73712, 365596, 2279184};

ortools/base/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,17 @@ cc_library(
379379
],
380380
)
381381

382+
cc_library(
383+
name = "base_export",
384+
hdrs = ["base_export.h"],
385+
)
386+
382387
cc_library(
383388
name = "macros",
384389
hdrs = ["macros.h"],
390+
deps = [
391+
":base_export",
392+
],
385393
)
386394

387395
cc_library(

ortools/base/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ target_compile_definitions(${NAME} PRIVATE
2626
-DOR_TOOLS_MAJOR=${PROJECT_VERSION_MAJOR}
2727
-DOR_TOOLS_MINOR=${PROJECT_VERSION_MINOR}
2828
-DOR_TOOLS_PATCH=${PROJECT_VERSION_PATCH})
29-
if(MSVC)
30-
target_compile_definitions(${NAME} PRIVATE -DOR_TOOLS_EXPORTS)
29+
if(MSVC AND BUILD_SHARED_LIBS)
30+
target_compile_definitions(${NAME} PUBLIC "OR_BUILD_DLL")
31+
target_compile_definitions(${NAME} PRIVATE "OR_EXPORT")
3132
endif()
3233
target_include_directories(${NAME} PRIVATE
3334
${PROJECT_SOURCE_DIR}

ortools/base/base_export.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
#ifndef OR_TOOLS_BASE_BASE_EXPORT_H_
1515
#define OR_TOOLS_BASE_BASE_EXPORT_H_
1616

17-
#if defined(_MSC_VER) && defined(OR_TOOLS_AS_DYNAMIC_LIB)
17+
#if defined(_MSC_VER) && defined(OR_BUILD_DLL)
1818
// Annoying stuff for windows -- makes sure clients can import these functions
19-
#if defined(OR_TOOLS_EXPORTS)
20-
#define BASE_EXPORT __declspec(dllexport)
19+
#if defined(OR_EXPORT)
20+
#define OR_DLL __declspec(dllexport)
2121
#else
22-
#define BASE_EXPORT __declspec(dllimport)
23-
#endif // defined(OR_TOOLS_EXPORT)
24-
#endif // _MSC_VER && OR_TOOLS_AS_DYNAMIC_LIB
22+
#define OR_DLL __declspec(dllimport)
23+
#endif // defined(OR_EXPORT)
24+
#endif // _MSC_VER && OR_BUILD_DLL
2525

26-
#ifndef BASE_EXPORT
27-
#define BASE_EXPORT
26+
#ifndef OR_DLL
27+
#define OR_DLL
2828
#endif
2929

3030
#endif // OR_TOOLS_BASE_BASE_EXPORT_H_

0 commit comments

Comments
 (0)