-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
462 lines (410 loc) · 19 KB
/
CMakeLists.txt
File metadata and controls
462 lines (410 loc) · 19 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
cmake_minimum_required(VERSION 3.15)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
include(FindPythonModule)
include(MODUtils)
###########################################################################
# Package Properties
###########################################################################
file(READ "VERSION" PROJECT_VERSION)
string(STRIP "${PROJECT_VERSION}" PROJECT_VERSION) # remove the newline
set(mod_VERSION ${PROJECT_VERSION} CACHE INTERNAL "" FORCE)
set(PNAME_FILE mod) # how the project name should be written in file names
set(mod_AUTHOR "Jakob Lykke Andersen")
set(mod_AUTHOR_EMAIL "jlandersen@imada.sdu.dk")
set(CPACK_PACKAGE_CONTACT "${mod_AUTHOR} <${mod_AUTHOR_EMAIL}>")
set(CMAKE_CXX_FLAGS_OPTDEBUG "-g -O3")
set(CMAKE_C_FLAGS_OPTDEBUG "-g -O3")
set(CMAKE_EXE_LINKER_FLAGS_OPTDEBUG "")
set(CMAKE_SHARED_LINKER_FLAGS_OPTDEBUG "")
mark_as_advanced(CMAKE_CXX_FLAGS_OPTDEBUG CMAKE_C_FLAGS_OPTDEBUG CMAKE_EXE_LINKER_FLAGS_OPTDEBUG CMAKE_SHARED_LINKER_FLAGS_OPTDEBUG)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel OptDebug."
FORCE)
endif()
###########################################################################
# Options
###########################################################################
option(BUILD_DOC "Enable documentation building." OFF)
option(BUILD_MOD "Enable central library building." ON)
option(BUILD_PY_MOD "Enable building of the Python bindings." ON)
option(BUILD_PY_MOD_PIP "If BUILD_PY_MOD, then install the bindings also with pip in the default location." ON)
option(BUILD_POST_MOD "Enable building of the post processor." ON)
option(BUILD_POST_MOD_FMT "Enable building of the post processor Latex format files." ON)
option(BUILD_TESTING "Enable test building." OFF)
option(BUILD_WITH_SANITIZERS "Compile libraries and tests with sanitizers." OFF)
option(BUILD_EXAMPLES "Enable example as tests." ON)
option(BUILD_COVERAGE "Enable code coverage." OFF)
option(ENABLE_SYMBOL_HIDING "Hide internal symbols in the library." ON)
option(ENABLE_DEP_SYMBOL_HIDING "Hide symbols provided by library dependencies." ON)
option(ENABLE_IPO "Using link-time optimization." ON)
option(USE_NESTED_GRAPH_CANON "Use the GraphCanon version in external/graph_canon." ON)
option(USE_NESTED_NLOHMANN_JSON "Use the nlohmann/json version in external/nlohmann_json." ON)
option(USE_NESTED_JSON_SCHEMA "Use the pboettch/json-schema-validator version in external/json_schema." ON)
option(WITH_OPENBABEL "Whether features depending on Open Babel are enabled or not." ON)
option(WITH_GUROBI "Whether Gurobi is available or not." ON)
option(WITH_CPLEX "Whether CPLEX is available or not." ON)
option(WITH_CBC "Whether CBC is available or not." ON)
if(BUILD_MOD)
set(languages CXX)
else()
set(languages "")
message(STATUS "Central library disabled (BUILD_MOD=${BUILD_MOD}), disabling PyMØD.")
set(BUILD_PY_MOD OFF)
endif()
project(${PNAME_FILE} VERSION ${PROJECT_VERSION} LANGUAGES ${languages})
get_directory_property(hasParent PARENT_DIRECTORY)
if(hasParent)
set(BUILD_DOC 0)
set(BUILD_EXAMPLES 0)
set(BUILD_TESTING 0)
set(BUILD_COVERAGE 0)
else()
if(BUILD_MOD)
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
include(CheckIPOSupported)
if(ENABLE_IPO)
check_ipo_supported(RESULT result OUTPUT output)
if(result)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
message(FATAL_ERROR "Interprocedural optimization was requested ('-DENABLE_IPO=on'), but it is not supported: ${output}")
endif()
else()
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION FALSE)
endif()
endif()
endif()
if(NOT BUILD_TESTING)
set(BUILD_EXAMPLES OFF)
endif()
enable_testing() # should be included here to add the targets in the top-level folder
###########################################################################
# Individual checks
###########################################################################
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
set(libmod_config_dependencies "")
set(libmod_config_find_files "")
# Boost
# -------------------------------------------------------------------------
if(BUILD_MOD)
if(POLICY CMP0167)
cmake_policy(SET CMP0167 NEW)
endif()
set(v 1.76.0)
string(APPEND libmod_config_dependencies "set(Boost_FOUND 0)\n") # TODO: remove when CMake 3.15 is required https://gitlab.kitware.com/cmake/cmake/issues/18590
string(APPEND libmod_config_dependencies "cmake_policy(PUSH)\nif(POLICY CMP0167)\n\tcmake_policy(SET CMP0167 NEW)\nendif()\n")
if(BUILD_PY_MOD)
foreach(PY 3 37 38 39 310 311 312 313 314 315 316 317 318 319)
set(lib "python${PY}")
find_package(Boost ${v} QUIET COMPONENTS ${lib})
if(Boost_FOUND)
find_package(Boost ${v} COMPONENTS ${lib})
set(PYTHON_TARGET ${lib})
string(APPEND libmod_config_dependencies "find_dependency(Boost ${v} COMPONENTS ${lib})\n")
break()
endif()
endforeach()
if(NOT Boost_FOUND)
find_package(Boost ${v} COMPONENTS python3)
message(FATAL_ERROR "Could not find Boost.Python for Python 3. Tried 'python' wih suffixes 3, 37, 38, 39, and 310 to 319.")
endif()
endif()
if(BUILD_TESTING)
find_package(Boost ${v} REQUIRED COMPONENTS graph iostreams unit_test_framework)
else()
find_package(Boost ${v} REQUIRED COMPONENTS graph iostreams)
endif()
message(STATUS "(Boost include dir is ${Boost_INCLUDE_DIRS})")
string(APPEND libmod_config_dependencies "find_dependency(Boost ${v} COMPONENTS graph iostreams)\n")
string(APPEND libmod_config_dependencies "cmake_policy(POP)\n")
endif()
# GraphCanon
# -------------------------------------------------------------------------
if(BUILD_MOD)
set(v 0.5)
if(USE_NESTED_GRAPH_CANON)
message(STATUS "GraphCanon: using external/graph_canon")
add_subdirectory(external/graph_canon)
if(GraphCanon_VERSION VERSION_LESS v)
message(FATAL_ERROR "Nested GraphCanon version not new enough: is ${GraphCanon_VERSION}, needs ${v}."
" Try again after 'git submodule update --init --recursive; ./bootstrap.sh'."
" Otherwise please report as bug.")
endif()
else()
find_package(GraphCanon ${v} REQUIRED)
string(APPEND libmod_config_dependencies "find_dependency(GraphCanon ${v})\n")
message(STATUS "GraphCanon version: ${GraphCanon_VERSION}")
endif()
endif()
# JSON and JSON Schema
# -------------------------------------------------------------------------
if(BUILD_MOD)
set(v 3.7.3)
if(USE_NESTED_NLOHMANN_JSON)
message(STATUS "nlohmann/json: using external/nlohmann_json")
set(JSON_BuildTests OFF CACHE INTERNAL "")
add_subdirectory(external/nlohmann_json)
else()
find_package(nlohmann_json ${v} REQUIRED)
endif()
# (must be after nlohmann/json to satisfy the dependency)
set(v 2.1.1)
if(USE_NESTED_JSON_SCHEMA)
message(STATUS "pboettch/json-schema-validator: using external/json_schema")
add_subdirectory(external/json_schema)
else()
find_package(nlohmann_json_schema_validator ${v} REQUIRED)
endif()
set_target_properties(nlohmann_json_schema_validator PROPERTIES
POSITION_INDEPENDENT_CODE ON)
# TODO: https://github.com/pboettch/json-schema-validator/issues/108
target_compile_options(nlohmann_json_schema_validator PRIVATE -Wno-error=switch)
endif()
# Graphviz
# -------------------------------------------------------------------------
if(BUILD_POST_MOD)
find_program(DOT dot)
if(NOT DOT)
message(FATAL_ERROR "Could not find the dot executable (from Graphviz) required for PostMØD.\n"
"Disable with -DBUILD_POST_MOD=no, or add the folder of the executable to PATH environment variable.")
# or give with -DCMAKE_PROGRAM_PATH=paths (currently '${CMAKE_PROGRAM_PATH}').")
endif()
message(STATUS "Found dot: ${DOT}")
execute_process(
COMMAND ${DOT} -P
COMMAND grep "cairo_device_svg -> output_svg"
RESULT_VARIABLE res
OUTPUT_QUIET ERROR_QUIET)
if(${res} EQUAL 0)
message(STATUS "Found dot to support cairo svg output.")
else()
message(FATAL_ERROR "The dot command does not seem to support cairo svg output, which is needed for PostMØD."
" Disable with -DBUILD_POST_MOD=no\n")
endif()
execute_process(
COMMAND ${DOT} -P
COMMAND grep "cairo_device_pdf -> output_pdf"
RESULT_VARIABLE res
OUTPUT_QUIET ERROR_QUIET)
if(${res} EQUAL 0)
message(STATUS "Found dot to support cairo pdf output.")
else()
message(FATAL_ERROR "The dot command does not seem to support cairo pdf output, which is needed for PostMØD."
" Disable with -DBUILD_POST_MOD=no\n")
endif()
execute_process(
COMMAND ${DOT} -P
COMMAND grep "rsvg_loadimage_svg -> render_cairo"
RESULT_VARIABLE res
OUTPUT_QUIET ERROR_QUIET)
if(${res} EQUAL 0)
message(STATUS "Found dot to support svg to cairo conversion.")
else()
message(FATAL_ERROR "The dot command does not seem to support svg to cairo conversion, which is needed for PostMØD."
" Disable with -DBUILD_POST_MOD=no\n")
endif()
endif()
# Open Babel
# -------------------------------------------------------------------------
if(BUILD_MOD)
if(WITH_OPENBABEL)
find_package(OpenBabel3 3)
if(OpenBabel3_FOUND)
string(APPEND libmod_config_dependencies "find_dependency(OpenBabel3 3)\n")
list(APPEND libmod_config_find_files "FindOpenBabel3.cmake")
message(STATUS " Include location is ${OPENBABEL3_INCLUDE_DIR}")
else()
find_package(OpenBabel2 2.3.2)
if(OpenBabel2_FOUND)
string(APPEND libmod_config_dependencies "find_dependency(OpenBabel2 2.3.2)\n")
list(APPEND libmod_config_find_files "FindOpenBabel2.cmake")
else()
message(FATAL_ERROR "Could not find Open Babel, neither version 2 nor 3.\n"
"Either disable Open Babel with -DWITH_OPENBABEL=off (not recommended), or add appropriate paths with -DCMAKE_PREFIX_PATH.")
endif()
endif()
set(MOD_HAVE_OPENBABEL TRUE)
endif()
endif()
if(BUILD_POST_MOD OR BUILD_DOC)
# pdf2svg
# ---------------------------------------------------------------------
find_program(PDF2SVG pdf2svg)
if(NOT PDF2SVG)
message(FATAL_ERROR "Could not find pdf2svg program, required for PostMØD and documentation.")
else()
message(STATUS "Found pdf2svg: ${PDF2SVG}")
endif()
# pdflatex
# ---------------------------------------------------------------------
find_package(LATEX REQUIRED COMPONENTS PDFLATEX)
endif()
# Python
# -------------------------------------------------------------------------
if(BUILD_PY_MOD)
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
string(APPEND libmod_config_dependencies "find_dependency(Python3 REQUIRED COMPONENTS Interpreter Development)\n")
endif()
# Sphinx
# -------------------------------------------------------------------------
if(BUILD_DOC)
find_program(SPHINX NAMES sphinx-build sphinx-build3 sphinx-build-3 sphinx-build2)
if(SPHINX)
message(STATUS "Sphinx: ${SPHINX}")
else()
message(FATAL_ERROR "Sphinx not found, required for building the documentation. Use --DBUILD_DOC=off to disable.")
endif()
endif()
# Threads
# -------------------------------------------------------------------------
if(BUILD_MOD)
find_package(Threads REQUIRED)
string(APPEND libmod_config_dependencies "find_dependency(Threads)\n")
endif()
# ILP Solvers
# -------------------------------------------------------------------------
if(BUILD_MOD)
if(WITH_GUROBI)
find_package(GUROBI QUIET)
if(GUROBI_FOUND)
set(MOD_HAVE_GUROBI TRUE)
message(STATUS "Gurobi: ${GUROBI_LIBRARY}, ${GUROBI_CXX_LIBRARY}, ${GUROBI_INCLUDE_DIRS}")
else()
message(FATAL_ERROR "Could NOT find Gurobi.\n"
"Disable Gurobi with -DWITH_GUROBI=no or use either -DGUROBI_DIR=path (currently '${GUROBI_DIR}')"
" or set the environment variable GUROBI_HOME (currently '$ENV{GUROBI_HOME}') to an appropriate path (the path with the 'lib/' and 'include/' folders).")
endif()
endif()
if(WITH_CPLEX)
find_package(CPLEX)
if(CPLEX_FOUND)
set(MOD_HAVE_CPLEX TRUE)
else()
message(FATAL_ERROR "Could NOT find CPLEX.\n"
"Disable CPLEX with -DWITH_CPLEX=no or set -DCPLEX_DIR=path (currently '${CPLEX_DIR}') to an appropriate path.")
endif()
endif()
if(WITH_CBC)
find_package(PkgConfig REQUIRED)
# In Conda builds CMake somehow does not get the required libraries listed in the .pc file,
# so we list the dependencies of cbc explicitly.
pkg_check_modules(CBC cbc osi-clp coinutils IMPORTED_TARGET GLOBAL)
if(CBC_FOUND)
set(MOD_HAVE_CBC TRUE)
else()
message(FATAL_ERROR "Could not find CBC, OsiClp, and CoinUtils from COIN-OR.\n"
"Disable CBC with -DWTIH_CBC=no or add appropriate paths to CMAKE_PREFIX_PATH.")
endif()
# TODO: alias for imported targets only supported from CMake 3.15
#add_library(COIN::CBC ALIAS PkgConfig::CBC)
endif()
if(WITH_GUROBI OR WITH_CPLEX OR WITH_CBC)
set(WITH_ANY_ILP on)
else()
set(WITH_ANY_ILP off)
endif()
endif()
###########################################################################
# Targets and Artefacts
###########################################################################
include(CMakeFiles.txt)
include(Coverage)
include(GNUInstallDirs)
if(NOT hasParent)
set(CMAKE_NO_BUILTIN_CHRPATH TRUE) #
# https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/RPATH-handling
set(CMAKE_SKIP_BUILD_RPATH FALSE) # use RPATH in the build tree
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) # but not the RPATH used in installation
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) # in addition to those deduced automatically
# use
# set_target_properties(theTarget PROPERTIES INSTALL_RPATH "theRPATH")
# on each target that needs it
endif()
if(BUILD_MOD)
add_subdirectory(libs/gml)
add_subdirectory(libs/jla_boost)
add_subdirectory(libs/petri)
add_subdirectory(libs/libmod)
endif()
if(BUILD_PY_MOD)
add_subdirectory(libs/pymodutils)
add_subdirectory(libs/pymod)
endif()
if(BUILD_POST_MOD)
add_subdirectory(libs/post_mod)
endif()
if(BUILD_DOC)
add_subdirectory(doc)
endif()
if(BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
if(BUILD_TESTING)
add_subdirectory(test)
endif()
# Packaging
# -------------------------------------------------------------------------
string(TOUPPER ${PROJECT_NAME} PNAME_UPPER)
set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION}) # because the default doesn't include the tweak number
set(CPACK_GENERATOR "DEB")
#set(CPACK_GENERATOR "DEB" "RPM")
set(CPACK_DEB_COMPONENT_INSTALL ON)
#set(CPACK_RPM_COMPONENT_INSTALL ON)
set(CPACK_DEBIAN_ENABLE_COMPONENT_DEPENDS ON)
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
# we use CACHE INTERNAL to make it work when in a subproject
set(CPACK_DEBIAN_${PNAME_UPPER}_RUN_PACKAGE_SHLIBDEPS ON CACHE INTERNAL "")
set(CPACK_DEBIAN_${PNAME_UPPER}_LIB_PACKAGE_SHLIBDEPS ON CACHE INTERNAL "")
set(CPACK_DEBIAN_${PNAME_UPPER}_DEV_PACKAGE_SHLIBDEPS ON CACHE INTERNAL "")
set(CPACK_DEBIAN_${PNAME_UPPER}_DOC_PACKAGE_SHLIBDEPS ON CACHE INTERNAL "")
set(CPACK_COMPONENT_${PNAME_UPPER}_RUN_DEPENDS ${PNAME_UPPER}_LIB CACHE INTERNAL "")
set(CPACK_COMPONENT_${PNAME_UPPER}_DEV_DEPENDS ${PNAME_UPPER}_LIB CACHE INTERNAL "")
set(CPACK_DEBIAN_${PNAME_UPPER}_RUN_PACKAGE_NAME "${PNAME_FILE}" CACHE INTERNAL "")
set(CPACK_DEBIAN_${PNAME_UPPER}_LIB_PACKAGE_NAME "lib${PNAME_FILE}" CACHE INTERNAL "")
set(CPACK_DEBIAN_${PNAME_UPPER}_DEV_PACKAGE_NAME "lib${PNAME_FILE}-dev" CACHE INTERNAL "")
set(CPACK_DEBIAN_${PNAME_UPPER}_DOC_PACKAGE_NAME "${PNAME_FILE}-doc" CACHE INTERNAL "")
set(CPACK_DEBIAN_${PNAME_UPPER}_RUN_FILE_NAME "${PNAME_FILE}_${PROJECT_VERSION}.deb" CACHE INTERNAL "")
set(CPACK_DEBIAN_${PNAME_UPPER}_LIB_FILE_NAME "lib${PNAME_FILE}_${PROJECT_VERSION}.deb" CACHE INTERNAL "")
set(CPACK_DEBIAN_${PNAME_UPPER}_DEV_FILE_NAME "lib${PNAME_FILE}-dev_${PROJECT_VERSION}.deb" CACHE INTERNAL "")
set(CPACK_DEBIAN_${PNAME_UPPER}_DOC_FILE_NAME "${PNAME_FILE}-doc_${PROJECT_VERSION}.deb" CACHE INTERNAL "")
#set(CPACK_RPM_${PNAME_UPPER}_RUN_PACKAGE_NAME "${PNAME_FILE}" CACHE INTERNAL "")
#set(CPACK_RPM_${PNAME_UPPER}_LIB_PACKAGE_NAME "lib${PNAME_FILE}" CACHE INTERNAL "")
#set(CPACK_RPM_${PNAME_UPPER}_DEV_PACKAGE_NAME "lib${PNAME_FILE}-devel" CACHE INTERNAL "")
#set(CPACK_RPM_${PNAME_UPPER}_DOC_PACKAGE_NAME "${PNAME_FILE}-doc" CACHE INTERNAL "")
#set(CPACK_RPM_${PNAME_UPPER}_RUN_FILE_NAME "${PNAME_FILE}-${PROJECT_VERSION}.rpm" CACHE INTERNAL "")
#set(CPACK_RPM_${PNAME_UPPER}_LIB_FILE_NAME "lib${PNAME_FILE}-${PROJECT_VERSION}.rpm" CACHE INTERNAL "")
#set(CPACK_RPM_${PNAME_UPPER}_DEV_FILE_NAME "lib${PNAME_FILE}-devel-${PROJECT_VERSION}.rpm" CACHE INTERNAL "")
#set(CPACK_RPM_${PNAME_UPPER}_DOC_FILE_NAME "${PNAME_FILE}-doc-${PROJECT_VERSION}.rpm" CACHE INTERNAL "")
if(NOT hasParent)
set(CPACK_SOURCE_PACKAGE_FILE_NAME
"${PNAME_FILE}-${CPACK_PACKAGE_VERSION}")
set(CPACK_SOURCE_GENERATOR "TGZ")
set(CPACK_SOURCE_IGNORE_FILES
"\.swp$"
"~$"
"/__pycache__/"
"\.pyc$"
"/\.git(/|$)"
"/\.gitmodules$"
"/\.idea/"
"/\.mypy_cache/"
"/\.ruff_cache/"
"/build/"
"/stage/"
"/conda/conda-bld/"
"/test/py/out/"
"/test/py/summary/"
"/test/py/.gdb_history$"
"/test/py/.*/out/"
"/test/py/.*/summary/"
"/test/py/.*/.gdb_history$"
"/test/py/graph/myGraph.gml$"
"/test/py/rule/myRule.gml$"
"/bootstrap.sh$")
add_custom_target(dist COMMAND ${CMAKE_MAKE_PROGRAM} package_source)
include(CPack)
endif()