forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp.cmake
385 lines (350 loc) · 11.8 KB
/
cpp.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
if(NOT BUILD_CXX)
return()
endif()
# Check dependencies
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREAD_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
# Tell find_package() to try “Config” mode before “Module” mode if no mode was specified.
# This should avoid find_package() to first find our FindXXX.cmake modules if
# distro package already provide a CMake config file...
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE)
# libprotobuf force us to depends on ZLIB::ZLIB target
if(BUILD_ZLIB)
find_package(ZLIB REQUIRED CONFIG)
else()
find_package(ZLIB REQUIRED)
endif()
if(BUILD_absl)
find_package(absl REQUIRED CONFIG)
else()
find_package(absl REQUIRED)
endif()
set(ABSL_DEPS
absl::base
absl::cord
absl::random_random
absl::raw_hash_set
absl::hash
absl::memory
absl::meta
absl::status
absl::str_format
absl::strings
absl::synchronization
absl::any
)
set(GFLAGS_USE_TARGET_NAMESPACE TRUE)
if(BUILD_gflags)
find_package(gflags REQUIRED CONFIG)
set(GFLAGS_DEP gflags::gflags_static)
else()
find_package(gflags REQUIRED)
set(GFLAGS_DEP gflags::gflags)
endif()
if(BUILD_glog)
find_package(glog REQUIRED CONFIG)
else()
find_package(glog REQUIRED)
endif()
if(BUILD_Protobuf)
find_package(Protobuf REQUIRED CONFIG)
else()
find_package(Protobuf REQUIRED)
endif()
if(USE_COINOR)
if(BUILD_CoinUtils)
find_package(CoinUtils REQUIRED CONFIG)
else()
find_package(CoinUtils REQUIRED)
endif()
if(BUILD_Osi)
find_package(Osi REQUIRED CONFIG)
else()
find_package(Osi REQUIRED)
endif()
if(BUILD_Clp)
find_package(Clp REQUIRED CONFIG)
else()
find_package(Clp REQUIRED)
endif()
if(BUILD_Cgl)
find_package(Cgl REQUIRED CONFIG)
else()
find_package(Cgl REQUIRED)
endif()
if(BUILD_Cbc)
find_package(Cbc REQUIRED CONFIG)
else()
find_package(Cbc REQUIRED)
endif()
set(COINOR_DEPS Coin::CbcSolver Coin::OsiCbc Coin::ClpSolver Coin::OsiClp)
endif()
# Check optional Dependencies
if(USE_CPLEX)
find_package(CPLEX REQUIRED)
set(CPLEX_DEP CPLEX::CPLEX)
endif()
if(USE_SCIP)
find_package(SCIP REQUIRED)
set(SCIP_DEP SCIP::SCIP)
endif()
if(USE_XPRESS)
find_package(XPRESS REQUIRED)
set(XPRESS_DEP XPRESS::XPRESS)
endif()
# Main Target
add_library(${PROJECT_NAME} "")
# Xcode fails to build if library doesn't contains at least one source file.
if(XCODE)
file(GENERATE
OUTPUT ${PROJECT_BINARY_DIR}/${PROJECT_NAME}/version.cpp
CONTENT "namespace {char* version = \"${PROJECT_VERSION}\";}")
target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}/version.cpp)
endif()
list(APPEND OR_TOOLS_COMPILE_DEFINITIONS
"USE_BOP" # enable BOP support
"USE_GLOP" # enable GLOP support
)
if(USE_COINOR)
list(APPEND OR_TOOLS_COMPILE_DEFINITIONS
"USE_CBC" # enable COIN-OR CBC support
"USE_CLP" # enable COIN-OR CLP support
)
endif()
if(USE_CPLEX)
list(APPEND OR_TOOLS_COMPILE_DEFINITIONS "USE_CPLEX")
endif()
if(USE_SCIP)
list(APPEND OR_TOOLS_COMPILE_DEFINITIONS "USE_SCIP")
endif()
if(USE_XPRESS)
list(APPEND OR_TOOLS_COMPILE_DEFINITIONS "USE_XPRESS")
endif()
if(WIN32)
list(APPEND OR_TOOLS_COMPILE_DEFINITIONS "__WIN32__")
endif()
if(MSVC)
list(APPEND OR_TOOLS_COMPILE_OPTIONS
"/bigobj" # Allow big object
"/DNOMINMAX"
"/DWIN32_LEAN_AND_MEAN=1"
"/D_CRT_SECURE_NO_WARNINGS"
"/D_CRT_SECURE_NO_DEPRECATE"
"/MP" # Build with multiple processes
)
# Prefer /MD over /MT and add NDEBUG in Release
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
list(APPEND OR_TOOLS_COMPILE_OPTIONS "/MDd")
else()
list(APPEND OR_TOOLS_COMPILE_OPTIONS "/MD" "/DNDEBUG")
endif()
# MSVC warning suppressions
list(APPEND OR_TOOLS_COMPILE_OPTIONS
"/wd4005" # 'macro-redefinition'
"/wd4018" # 'expression' : signed/unsigned mismatch
"/wd4065" # switch statement contains 'default' but no 'case' labels
"/wd4068" # 'unknown pragma'
"/wd4101" # 'identifier' : unreferenced local variable
"/wd4146" # unary minus operator applied to unsigned type, result still unsigned
"/wd4200" # nonstandard extension used : zero-sized array in struct/union
"/wd4244" # 'conversion' conversion from 'type1' to 'type2', possible loss of data
"/wd4251" # 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
"/wd4267" # 'var' : conversion from 'size_t' to 'type', possible loss of data
"/wd4305" # 'identifier' : truncation from 'type1' to 'type2'
"/wd4307" # 'operator' : integral constant overflow
"/wd4309" # 'conversion' : truncation of constant value
"/wd4334" # 'operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
"/wd4355" # 'this' : used in base member initializer list
"/wd4477" # 'fwprintf' : format string '%s' requires an argument of type 'wchar_t *'
"/wd4506" # no definition for inline function 'function'
"/wd4715" # function' : not all control paths return a value
"/wd4800" # 'type' : forcing value to bool 'true' or 'false' (performance warning)
"/wd4996" # The compiler encountered a deprecated declaration.
)
else()
list(APPEND OR_TOOLS_COMPILE_OPTIONS "-fwrapv")
endif()
# Includes
target_include_directories(${PROJECT_NAME} INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
$<INSTALL_INTERFACE:include>
)
# Compile options
set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
)
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_11)
target_compile_definitions(${PROJECT_NAME} PUBLIC ${OR_TOOLS_COMPILE_DEFINITIONS})
target_compile_options(${PROJECT_NAME} PUBLIC ${OR_TOOLS_COMPILE_OPTIONS})
# Properties
if(NOT APPLE)
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION})
else()
# Clang don't support version x.y.z with z > 255
set_target_properties(${PROJECT_NAME} PROPERTIES
INSTALL_RPATH "@loader_path"
VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR})
endif()
set_target_properties(${PROJECT_NAME} PROPERTIES
SOVERSION ${PROJECT_VERSION_MAJOR}
POSITION_INDEPENDENT_CODE ON
INTERFACE_POSITION_INDEPENDENT_CODE ON
)
set_target_properties(${PROJECT_NAME} PROPERTIES INTERFACE_${PROJECT_NAME}_MAJOR_VERSION ${PROJECT_VERSION_MAJOR})
set_target_properties(${PROJECT_NAME} PROPERTIES COMPATIBLE_INTERFACE_STRING ${PROJECT_NAME}_MAJOR_VERSION)
# Dependencies
target_link_libraries(${PROJECT_NAME} PUBLIC
ZLIB::ZLIB
${ABSL_DEPS}
${GFLAGS_DEP}
glog::glog
protobuf::libprotobuf
${COINOR_DEPS}
${CPLEX_DEP}
${SCIP_DEP}
${XPRESS_DEP}
Threads::Threads)
if(WIN32)
target_link_libraries(${PROJECT_NAME} PUBLIC psapi.lib ws2_32.lib)
endif()
# ALIAS
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
# Generate Protobuf cpp sources
set(PROTO_HDRS)
set(PROTO_SRCS)
file(GLOB_RECURSE proto_files RELATIVE ${PROJECT_SOURCE_DIR}
"ortools/bop/*.proto"
"ortools/constraint_solver/*.proto"
"ortools/data/*.proto"
"ortools/glop/*.proto"
"ortools/graph/*.proto"
"ortools/linear_solver/*.proto"
"ortools/sat/*.proto"
"ortools/util/*.proto"
"ortools/linear_solver/*.proto"
)
# Get Protobuf include dir
get_target_property(protobuf_dirs protobuf::libprotobuf INTERFACE_INCLUDE_DIRECTORIES)
foreach(dir IN LISTS protobuf_dirs)
if ("${dir}" MATCHES "BUILD_INTERFACE")
list(APPEND PROTO_DIRS "\"--proto_path=${dir}\"")
endif()
endforeach()
foreach(PROTO_FILE IN LISTS proto_files)
#message(STATUS "protoc proto(cc): ${PROTO_FILE}")
get_filename_component(PROTO_DIR ${PROTO_FILE} DIRECTORY)
get_filename_component(PROTO_NAME ${PROTO_FILE} NAME_WE)
set(PROTO_HDR ${PROJECT_BINARY_DIR}/${PROTO_DIR}/${PROTO_NAME}.pb.h)
set(PROTO_SRC ${PROJECT_BINARY_DIR}/${PROTO_DIR}/${PROTO_NAME}.pb.cc)
#message(STATUS "protoc hdr: ${PROTO_HDR}")
#message(STATUS "protoc src: ${PROTO_SRC}")
add_custom_command(
OUTPUT ${PROTO_SRC} ${PROTO_HDR}
COMMAND protobuf::protoc
"--proto_path=${PROJECT_SOURCE_DIR}"
${PROTO_DIRS}
"--cpp_out=${PROJECT_BINARY_DIR}"
${PROTO_FILE}
DEPENDS ${PROTO_FILE} protobuf::protoc
COMMENT "Generate C++ protocol buffer for ${PROTO_FILE}"
VERBATIM)
list(APPEND PROTO_HDRS ${PROTO_HDR})
list(APPEND PROTO_SRCS ${PROTO_SRC})
endforeach()
#add_library(${PROJECT_NAME}_proto STATIC ${PROTO_SRCS} ${PROTO_HDRS})
add_library(${PROJECT_NAME}_proto OBJECT ${PROTO_SRCS} ${PROTO_HDRS})
set_target_properties(${PROJECT_NAME}_proto PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(${PROJECT_NAME}_proto PROPERTIES CXX_STANDARD 11)
set_target_properties(${PROJECT_NAME}_proto PROPERTIES CXX_STANDARD_REQUIRED ON)
set_target_properties(${PROJECT_NAME}_proto PROPERTIES CXX_EXTENSIONS OFF)
target_include_directories(${PROJECT_NAME}_proto PRIVATE
${PROJECT_SOURCE_DIR}
${PROJECT_BINARY_DIR}
$<TARGET_PROPERTY:protobuf::libprotobuf,INTERFACE_INCLUDE_DIRECTORIES>
)
target_compile_definitions(${PROJECT_NAME}_proto PUBLIC ${OR_TOOLS_COMPILE_DEFINITIONS})
target_compile_options(${PROJECT_NAME}_proto PUBLIC ${OR_TOOLS_COMPILE_OPTIONS})
#target_link_libraries(${PROJECT_NAME}_proto PRIVATE protobuf::libprotobuf)
add_dependencies(${PROJECT_NAME}_proto protobuf::libprotobuf)
add_library(${PROJECT_NAME}::proto ALIAS ${PROJECT_NAME}_proto)
# Add ortools::proto to libortools
#target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_NAME}::proto)
target_sources(${PROJECT_NAME} PRIVATE $<TARGET_OBJECTS:${PROJECT_NAME}::proto>)
add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}::proto)
foreach(SUBPROJECT IN ITEMS
algorithms base bop constraint_solver data glop graph linear_solver lp_data
port sat util)
add_subdirectory(ortools/${SUBPROJECT})
#target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_NAME}::${SUBPROJECT})
target_sources(${PROJECT_NAME} PRIVATE $<TARGET_OBJECTS:${PROJECT_NAME}::${SUBPROJECT}>)
add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}::${SUBPROJECT})
endforeach()
if(BUILD_TESTING)
add_subdirectory(examples/cpp)
endif()
# Install rules
include(GNUInstallDirs)
# Install builded dependencies
if(INSTALL_BUILD_DEPS)
if( BUILD_ZLIB OR
BUILD_absl OR
BUILD_gflags OR
BUILD_glog OR
BUILD_Protobuf OR
BUILD_CoinUtils OR
BUILD_Osi OR
BUILD_Clp OR
BUILD_Cgl OR
BUILD_Cbc
)
install(
DIRECTORY ${CMAKE_BINARY_DIR}/dependencies/install/
DESTINATION ${CMAKE_INSTALL_PREFIX}
)
endif()
endif()
include(GenerateExportHeader)
GENERATE_EXPORT_HEADER(${PROJECT_NAME})
install(FILES ${PROJECT_BINARY_DIR}/${PROJECT_NAME}_export.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(EXPORT ${PROJECT_NAME}Targets
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
install(DIRECTORY ortools
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
COMPONENT Devel
FILES_MATCHING
PATTERN "*.h")
install(DIRECTORY ${PROJECT_BINARY_DIR}/ortools
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
COMPONENT Devel
FILES_MATCHING
PATTERN "*.pb.h"
PATTERN CMakeFiles EXCLUDE)
include(CMakePackageConfigHelpers)
string (TOUPPER "${PROJECT_NAME}" PACKAGE_PREFIX)
configure_package_config_file(cmake/${PROJECT_NAME}Config.cmake.in
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
NO_CHECK_REQUIRED_COMPONENTS_MACRO)
write_basic_package_version_file(
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
COMPATIBILITY SameMajorVersion
)
install(
FILES
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
COMPONENT Devel)