Skip to content

Commit 961e084

Browse files
committed
cmake: Fix shared lib support with MSVC
1 parent 74f7c15 commit 961e084

File tree

10 files changed

+47
-18
lines changed

10 files changed

+47
-18
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}"
@@ -373,6 +377,12 @@ function(generate_proto_library)
373377
else()
374378
set_target_properties(${PROTO_NAME}_proto PROPERTIES CXX_STANDARD 17)
375379
endif()
380+
if(MSVC AND BUILD_SHARED_LIBS)
381+
target_compile_definitions(${PROTO_NAME}_proto INTERFACE "OR_PROTO_DLL=__declspec(dllimport)")
382+
target_compile_definitions(${PROTO_NAME}_proto PRIVATE "OR_PROTO_DLL=__declspec(dllexport)")
383+
else()
384+
target_compile_definitions(${PROTO_NAME}_proto PUBLIC "OR_PROTO_DLL=")
385+
endif()
376386
set_target_properties(${PROTO_NAME}_proto PROPERTIES
377387
CXX_STANDARD_REQUIRED ON
378388
CXX_EXTENSIONS OFF
@@ -447,6 +457,9 @@ set_target_properties(${PROJECT_NAME} PROPERTIES
447457
target_compile_features(${PROJECT_NAME} PUBLIC
448458
$<IF:$<CXX_COMPILER_ID:MSVC>,cxx_std_20,cxx_std_17>)
449459
target_compile_definitions(${PROJECT_NAME} PUBLIC ${OR_TOOLS_COMPILE_DEFINITIONS})
460+
if(MSVC AND BUILD_SHARED_LIBS)
461+
target_compile_definitions(${PROJECT_NAME} PRIVATE OR_EXPORT)
462+
endif()
450463
target_compile_options(${PROJECT_NAME} PUBLIC ${OR_TOOLS_COMPILE_OPTIONS})
451464
target_link_options(${PROJECT_NAME} INTERFACE ${OR_TOOLS_LINK_OPTIONS})
452465
# Properties

cmake/python.cmake

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,13 @@ set(need_unix_highs_lib "$<AND:${is_not_windows},$<BOOL:${BUILD_HIGHS}>>")
450450
set(need_windows_highs_lib "$<AND:${is_windows},$<BOOL:${BUILD_HIGHS}>>")
451451

452452
set(is_ortools_shared "$<STREQUAL:$<TARGET_PROPERTY:ortools,TYPE>,SHARED_LIBRARY>")
453+
set(need_unix_ortools_lib "$<AND:${is_not_windows},${is_ortools_shared}>")
454+
set(need_windows_ortools_lib "$<AND:${is_windows},${is_ortools_shared}>")
453455

454456
add_custom_command(
455457
OUTPUT python/ortools_timestamp
456458
COMMAND ${CMAKE_COMMAND} -E remove -f ortools_timestamp
457-
COMMAND ${CMAKE_COMMAND} -E make_directory ${PYTHON_PROJECT}/.libs
459+
COMMAND ${CMAKE_COMMAND} -E make_directory ${PYTHON_PROJECT}/.libs
458460
COMMAND ${CMAKE_COMMAND} -E
459461
$<IF:$<BOOL:${BUILD_ZLIB}>,copy,true>
460462
$<${need_unix_zlib_lib}:$<TARGET_SONAME_FILE:ZLIB::ZLIB>>
@@ -596,7 +598,8 @@ add_custom_command(
596598

597599
COMMAND ${CMAKE_COMMAND} -E
598600
$<IF:${is_ortools_shared},copy,true>
599-
$<${is_ortools_shared}:$<TARGET_SONAME_FILE:ortools>>
601+
$<${need_unix_ortools_lib}:$<TARGET_SONAME_FILE:${PROJECT_NAMESPACE}::ortools>>
602+
$<${need_windows_ortools_lib}:$<TARGET_FILE:${PROJECT_NAMESPACE}::ortools>>
600603
${PYTHON_PROJECT}/.libs
601604
COMMAND ${CMAKE_COMMAND} -E touch ${PROJECT_BINARY_DIR}/python/ortools_timestamp
602605
MAIN_DEPENDENCY

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_

ortools/base/macros.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include <cstdlib> // for size_t.
1818

19+
#include "ortools/base/base_export.h" // for OR_DLL
20+
1921
#define COMPILE_ASSERT(x, msg)
2022

2123
#ifdef NDEBUG

ortools/math_opt/core/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ list(FILTER _SRCS EXCLUDE REGEX "/[^/]*_test\\.cc$")
2020
set(NAME ${PROJECT_NAME}_math_opt_core)
2121
add_library(${NAME} OBJECT ${_SRCS})
2222
set_target_properties(${NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
23+
if(MSVC AND BUILD_SHARED_LIBS)
24+
target_compile_definitions(${NAME} PUBLIC "OR_BUILD_DLL")
25+
target_compile_definitions(${NAME} PRIVATE "OR_EXPORT")
26+
endif()
2327
target_include_directories(${NAME} PUBLIC
2428
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
2529
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>)

ortools/math_opt/core/solver_debug.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <atomic>
1818
#include <cstdint>
1919

20+
#include <ortools/base/base_export.h>
21+
2022
namespace operations_research {
2123
namespace math_opt {
2224
namespace internal {
@@ -26,7 +28,7 @@ namespace internal {
2628
// This variable is intended to be used by MathOpt unit tests in other languages
2729
// to test the proper garbage collection. It should never be used in any other
2830
// context.
29-
extern std::atomic<int64_t> debug_num_solver;
31+
OR_DLL extern std::atomic<int64_t> debug_num_solver;
3032

3133
} // namespace internal
3234
} // namespace math_opt

ortools/util/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ add_library(${NAME} OBJECT ${_SRCS})
2222
set_target_properties(${NAME} PROPERTIES
2323
POSITION_INDEPENDENT_CODE ON
2424
)
25+
if(MSVC AND BUILD_SHARED_LIBS)
26+
target_compile_definitions(${NAME} PUBLIC "OR_BUILD_DLL")
27+
target_compile_definitions(${NAME} PRIVATE "OR_EXPORT")
28+
endif()
2529
target_include_directories(${NAME} PRIVATE
2630
${PROJECT_SOURCE_DIR}
2731
${PROJECT_BINARY_DIR})

ortools/util/time_limit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ namespace operations_research {
9191
*/
9292
// TODO(user): The expression "deterministic time" should be replaced with
9393
// "number of operations" to avoid confusion with "real" time.
94-
class TimeLimit {
94+
class OR_DLL TimeLimit {
9595
public:
9696
static const double kSafetyBufferSeconds; // See the .cc for the value.
9797
static const int kHistorySize;

0 commit comments

Comments
 (0)