-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCMakeLists.txt
288 lines (250 loc) · 12.6 KB
/
CMakeLists.txt
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
# --------------------------------------------------------------------------- #
# Main CMake file for MCFClass #
# #
# This file allows one to build the library using CMake. #
# To do so, you can use the following commands: #
# #
# $ cmake -S <source-path> -B <build-path> #
# $ cmake --build <build-path> #
# #
# The following command also installs the library in the system: #
# #
# $ cmake --build <build-path> --target install #
# #
# Note: this file tries to respect the principles of Modern CMake, #
# keep it in mind when editing it. #
# #
# Niccolo' Iardella #
# Donato Meoli #
# Dipartimento di Informatica #
# Universita' di Pisa #
# --------------------------------------------------------------------------- #
cmake_minimum_required(VERSION 3.19)
cmake_policy(VERSION 3.12)
project(MCFClass
VERSION 1.0.0
DESCRIPTION "C++ solvers for MCF problems"
HOMEPAGE_URL http://groups.di.unipi.it/optimize/Software/MCF.html
LANGUAGES C CXX)
# These variables make the code harder to read but easier to change.
set(modName ${PROJECT_NAME})
set(modNamespace "MCFClass")
# This adds the cmake directory to the module search paths,
# allowing us to use our modules.
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
# ----- Settings ------------------------------------------------------------ #
# This creates an ENABLE_TESTING option (default: ON) and enables the testing
# using the ctest executable.
# See: https://cmake.org/cmake/help/latest/manual/ctest.1.html
#include(CTest)
# Sets the default build type (if none was specified).
# See: https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html
include(BuildType)
# An option is an ON/OFF user-settable cache variable
option(MCFClass_USE_CPLEX "Whether ${modName} will use CPLEX or not." ON)
option(MCFClass_READ_PATHS
"Whether ${modName} will read locations for dependencies or not." ON)
# ----- Requirements -------------------------------------------------------- #
if (MCFClass_READ_PATHS)
# Read the dependencies default location from a file written in a
# bash-style syntax since it is shared with the other hand-written Makefiles,
# so we define a function to convert these macros written as `lhs = rhs` into
# their equivalent cmake variants, i.e., `set(lhs rhs)`, avoiding comments.
macro(makefile_macros_to_cmake file)
FILE(STRINGS ${file} paths)
foreach (path IN LISTS paths)
if (path) # ignore empty lines, i.e., \n
# makes empty the comment lines, i.e. those start with #...
string(REGEX REPLACE "^[ \t]*[#].*" "" path ${path})
if (path) # ... and then ignore them
string(REGEX MATCH ".*[=]" lhs "${path}") # retrieve the lhs
string(REGEX REPLACE "=" "" lhs ${lhs}) # remove =
# remove tab or white spaces from head
string(STRIP "${lhs}" lhs)
# if it's not already defined before in custom makefile-paths...
if (NOT DEFINED ${lhs})
# then read its rhs...
string(REGEX MATCH "[=].*" rhs "${path}") # retrieve the rhs
# remove comments starting with # from tails, if any
string(REGEX REPLACE "[#].*" "" rhs ${rhs})
string(REGEX REPLACE "=" "" rhs ${rhs}) # remove =
string(REGEX REPLACE "\"" "" rhs ${rhs}) # remove ", if any
# remove tab or white space from head or tail
string(STRIP "${rhs}" rhs)
# ... and if it is not a default path in Unix...
if (NOT (rhs STREQUAL /usr OR
rhs STREQUAL /usr/local))
# ... then set the one defined in makefile-default-paths
set("${lhs}" ${rhs})
#message(STATUS "Specify ${lhs}: " ${rhs})
endif ()
endif ()
unset(lhs)
unset(rhs)
endif ()
endif ()
endforeach ()
unset(path)
unset(paths)
endmacro ()
# firstly, set the custom makefile-paths, if any...
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/extlib/makefile-paths")
makefile_macros_to_cmake(extlib/makefile-paths)
endif ()
# ... then set the default other ones ignoring those already previously set,
# according to the specific OS
if (UNIX)
if (APPLE)
makefile_macros_to_cmake(extlib/makefile-default-paths-macos)
else ()
makefile_macros_to_cmake(extlib/makefile-default-paths-linux)
endif ()
elseif (WIN32)
makefile_macros_to_cmake(extlib/makefile-default-paths-win)
endif ()
# Retrieve the system architecture
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
if (UNIX)
if (APPLE)
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64") # Apple Silicon MX arch
set(ARCH arm64)
else () # Intel arch
set(ARCH x86-64)
endif ()
else ()
set(ARCH x86-64)
endif ()
elseif (WIN32)
set(ARCH x64)
endif ()
else ()
set(ARCH x86)
endif ()
endif ()
if (MCFClass_USE_CPLEX)
# Needed for MCFCplex
find_package(CPLEX)
if (NOT CPLEX_FOUND)
set(MCFClass_USE_CPLEX OFF CACHE BOOL "Whether ${modName} will use CPLEX or not." FORCE)
endif ()
endif ()
# ----- Configuration header ------------------------------------------------ #
# This will generate a *Config.h header in the build directory.
configure_file(cmake/${modName}Config.h.in ${modName}Config.h)
# ----- Library ------------------------------------------------------------- #
# With the BUILD_SHARED_LIBS variable we can specify if the library will
# be STATIC or SHARED, so no reason to do it now.
add_library(${modName})
target_compile_features(${modName} PUBLIC cxx_std_20)
# When adding source files with target_sources(), PRIVATE means that the files
# should only be added to this library, whereas PUBLIC means they should be
# added to this library and to any target that links to it.
# INTERFACE can be used for sources that should not be added to this library
# but should be added to anything that links to it.
# Note: do not GLOB files here.
target_sources(${modName} PRIVATE
MCFSimplex/MCFSimplex.C
RelaxIV/RelaxIV.C
SPTree/SPTree.C)
# When using target_include_directories(), PUBLIC means that any targets
# that link to this target also need that include directory.
# Other options are PRIVATE (only affect the current target, not dependencies),
# and INTERFACE (only needed for dependencies).
# Different INSTALL_INTERFACE and BUILD_INTERFACE paths are used when
# generating the target import file (***Targets.cmake).
# This means that if a target finds this library in its build directory
# will look into the BUILD_INTERFACE path for its headers, if it finds it
# installed in the system will look into the INSTALL_INTERFACE path.
target_include_directories(
${modName} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/MCFClass>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/MCFClone>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/MCFSimplex>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/OPTUtils>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/RelaxIV>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/SPTree>
$<INSTALL_INTERFACE:include/${modNamespace}>)
# When linking other targets to the library with target_link_libraries(),
# PRIVATE means that the libraries will be linked only to this library,
# PUBLIC means they will be linked also to the targets that depend on this
# library, INTERFACE means they will be linked only to the targets that depend
# on this library.
if (MCFClass_USE_CPLEX)
message(STATUS "${modName}: CPLEX found, configuring MCFCplex")
target_sources(${modName} PRIVATE MCFCplex/MCFCplex.C)
target_include_directories(
${modName} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/MCFCplex>)
target_link_libraries(${modName} PUBLIC CPLEX::Cplex)
endif ()
# This alias is defined so that executables in this same project can use
# the library with this notation.
add_library(${modNamespace}::${modName} ALIAS ${modName})
# ----- Subdirectories ------------------------------------------------------ #
if (BUILD_TESTING)
# Just consuming BUILD_TESTING to avoid warnings
endif ()
# ----- Install instructions ------------------------------------------------ #
# The following commands are used when installing the library
# and its CMake configuration files on the system.
# They are not required for local builds (see below).
include(GNUInstallDirs)
# Install the library
install(TARGETS ${modName}
EXPORT ${modName}Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
# Install the headers
install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/MCFClass/MCFClass.h
${CMAKE_CURRENT_SOURCE_DIR}/MCFClone/MCFClone.h
${CMAKE_CURRENT_SOURCE_DIR}/MCFSimplex/MCFSimplex.h
${CMAKE_CURRENT_SOURCE_DIR}/OPTUtils/OPTUtils.h
${CMAKE_CURRENT_SOURCE_DIR}/RelaxIV/RelaxIV.h
${CMAKE_CURRENT_SOURCE_DIR}/SPTree/SPTree.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${modNamespace})
if (MCFClass_USE_CPLEX)
install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/MCFCplex/MCFCplex.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${modNamespace})
endif ()
# Install the auto-generated configuration header (see above).
install(FILES ${PROJECT_BINARY_DIR}/${modName}Config.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${modNamespace})
# (Generate and) install the target import file, that allows other
# CMake projects to import the target.
install(EXPORT ${modName}Targets
NAMESPACE ${modNamespace}::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${modName})
# Generate the package version file, that allows other
# CMake projects to know the version.
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/${modName}ConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion)
# Generate the package configuration file, that allows other
# CMake projects to find the library with find_package().
configure_package_config_file(
${CMAKE_CURRENT_LIST_DIR}/cmake/${modName}Config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${modName}Config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${modName})
# Install the package version and configuration files.
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/${modName}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${modName}ConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${modName})
# Shipping custom find modules should not be encouraged because third
# party developers should provide for them.
file(GLOB findMods ${CMAKE_CURRENT_LIST_DIR}/cmake/Find*)
install(FILES ${findMods}
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${modName})
# Install the README and LICENSE files.
install(FILES
${CMAKE_CURRENT_LIST_DIR}/README.md
${CMAKE_CURRENT_LIST_DIR}/LICENSE
DESTINATION ${CMAKE_INSTALL_DATADIR}/${modName})
# --------------------------------------------------------------------------- #
# Remove from the search paths the cmake directory we added at the beginning.
list(REMOVE_AT CMAKE_MODULE_PATH -1)
# --------------------------------------------------------------------------- #