-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentSetup.cmake
More file actions
363 lines (289 loc) · 13.3 KB
/
ComponentSetup.cmake
File metadata and controls
363 lines (289 loc) · 13.3 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
include(CMakeTools)
include(GNUInstallDirs)
#====================================================================
#
# Define and setup build process for a target for the current component
#
# Usage:
#
# create_component(COMPONENT)
#
# The following variables must be properly set before any call to this function
# - <COMPONENT>_DIRS : list of directories (path relative to CMAKE_SOURCE_DIR)
# that contain source files.
# - <COMPONENT>_EXCLUDE_SRCS : list of files to exclude from build process
# - <COMPONENT>_RESOURCES : list of qrc files
# - <COMPONENT>_RESOURCES_DIR : of directories (path relative to CMAKE_SOURCE_DIR)
# that contain resource files (icon, image, configuration, ...).
# for this component.
#
# This function:
# creates a target <component> from all sources files in <component>_DIRS
# excluding files from <component>_EXCLUDE_SRCS
#
function(create_component COMPONENT)
# --- Collect source files from given directories ---
# --> Scan source directories and return a list of files to be compiled.
get_sources(${COMPONENT} DIRS ${${COMPONENT}_DIRS} EXCLUDE ${${COMPONENT}_EXCLUDE_SRCS} INCLUDESRCS ${${COMPONENT}_INCLUDE_SRCS})
# --> Scan resource directories and return a list of files to be included in target => ${COMPONENT}_INSTALLRESOURCES
get_resources(${COMPONENT} DIRS ${${COMPONENT}_RESOURCES_DIR})
# Create the library
if(BUILD_EXE)
add_executable(${COMPONENT} ${${COMPONENT}_SRCS} ${${COMPONENT}_RESOURCES})
elseif(BUILD_PYBIND)
if(MSVC)
pybind11_add_module(${COMPONENT} SHARED ${${COMPONENT}_SRCS})
else()
pybind11_add_module(${COMPONENT} ${${COMPONENT}_SRCS})
endif()
elseif(BUILD_SHARED_LIBS)
add_library(${COMPONENT} SHARED ${${COMPONENT}_SRCS} ${${COMPONENT}_RESOURCES})
else()
add_library(${COMPONENT} STATIC ${${COMPONENT}_SRCS})
set_property(TARGET ${COMPONENT} PROPERTY POSITION_INDEPENDENT_CODE ON)
endif()
# Set compiler options
string(TOUPPER ${COMPONENT} UPPERCOMPONENT)
target_compile_definitions(${COMPONENT} PRIVATE -D${UPPERCOMPONENT}_LIBRARY)
if(MSVC)
# problem with msvc, see https://stackoverflow.com/questions/78598141/first-stdmutexlock-crashes-in-application-built-with-latest-visual-studio
target_compile_definitions(${COMPONENT} PRIVATE -D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR)
endif()
# reminder : WARNINGS_LEVEL=0 -> no warnings, =1, developers mininmal set of warnings,
# =2 : strict mode, warnings to errors.
apply_compiler_options(${COMPONENT} DIAGNOSTICS_LEVEL ${WARNINGS_LEVEL})
# Append component source dirs to include directories
# (Private : only to build current component).
foreach(dir IN LISTS ${COMPONENT}_DIRS)
target_include_directories(${COMPONENT} PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${dir}>)
endforeach()
foreach(dir IN LISTS ${COMPONENT}_EXTRA_INCLUDE_DIRECTORIES)
target_include_directories(${COMPONENT} PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${dir}>)
endforeach()
# Add current component include dirs that should be propagated through
# interface, in build tree.
# WARNING : includes for install interface are handled later
# in component install, and may be different.
foreach(dir IN LISTS ${COMPONENT}_INTERFACE_INCLUDE_DIRECTORIES)
target_include_directories(${COMPONENT} INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${dir}>)
message("-- Include dir for ${COMPONENT}, ${CMAKE_CURRENT_SOURCE_DIR}/${dir}")
endforeach()
if (${COMPONENT}_TRANSLATION)
find_package(Qt REQUIRED Core LinguistTools)
FILE(GLOB ${COMPONENT}_TS_FILES "${${COMPONENT}_TRANSLATION}/*.ts")
qt_add_translation(${COMPONENT}_QM_FILES ${${COMPONENT}_TS_FILES})
message("-- Create translations for ${COMPONENT}, ${${COMPONENT}_TS_FILES}")
message("Qt5_LRELEASE_EXECUTABLE: ${Qt5_LRELEASE_EXECUTABLE} ")
set(${COMPONENT}_TRANSLATIONS_FILES ${${COMPONENT}_QM_FILES} PARENT_SCOPE)
endif()
set_target_properties(${COMPONENT} PROPERTIES
OUTPUT_NAME "${COMPONENT}"
#VERSION "${PROJECT_SOVERSION}"
#SOVERSION "${PROJECT_SOVERSION_MAJOR}"
RESOURCE "${${COMPONENT}_INSTALLRESOURCES}"
#POSITION_INDEPENDENT_CODE ON
#INTERFACE_POSITION_INDEPENDENT_CODE ON
#INTERFACE_${PROJECT_NAME}_MAJOR_VERSION ${PROJECT_VERSION_MAJOR}
#COMPATIBLE_INTERFACE_STRING ${PROJECT_NAME}_MAJOR_VERSION
)
endfunction()
#====================================================================
#
# Function to define and setup install process for a target for the current component
#
# Usage:
#
# component_install_setup(<COMPONENT>)
#
#
# This function
# creates a target <component> from all sources files in <component>_DIRS
# excluding files from <component>_EXCLUDE_SRCS
#
function(component_install_setup COMPONENT)
set(oneValueArgs DESTINATION INCLUDE LIBDIR)
cmake_parse_arguments(runtime "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
if("${runtime_DESTINATION}" STREQUAL "")
set(runtime_DESTINATION ${CMAKE_INSTALL_BINDIR})
else()
set(runtime_DESTINATION ${CMAKE_INSTALL_BINDIR}/${runtime_DESTINATION})
endif()
if("${runtime_LIBDIR}" STREQUAL "")
set(runtime_LIBDIR ${CMAKE_INSTALL_LIBDIR})
endif()
if("${runtime_INCLUDE}" STREQUAL "")
set(runtime_INCLUDE ${PROJECT_NAME}/${COMPONENT})
endif()
install(TARGETS ${COMPONENT}
EXPORT ${PROJECT_NAME}Targets
RUNTIME DESTINATION ${runtime_DESTINATION}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${runtime_INCLUDE}
ARCHIVE DESTINATION ${runtime_LIBDIR}
LIBRARY DESTINATION ${runtime_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${runtime_INCLUDE}
RESOURCE DESTINATION resources
)
#target_include_directories(${COMPONENT} INTERFACE
# $<INSTALL_INTERFACE:include>)
# Setup the list of all headers to be installed.
foreach(dir IN LISTS ${COMPONENT}_INSTALL_INTERFACE_INCLUDE_DIRECTORIES)
file(GLOB _headers CONFIGURE_DEPENDS
LIST_DIRECTORIES false ${_FILE} ${dir}/*.h ${dir}/*.hpp)
list(APPEND _all_headers ${_headers})
endforeach()
if(_all_headers)
# Do not install files listed in ${COMPONENT}_HDRS_EXCLUDE
foreach(_file IN LISTS ${COMPONENT}_HDRS_EXCLUDE)
list(REMOVE_ITEM _all_headers "${CMAKE_CURRENT_SOURCE_DIR}/${_file}")
endforeach()
# install files collected in _all_headers
install(
FILES ${_all_headers}
DESTINATION include/${runtime_INCLUDE}
)
# Add include dirs in target interface
target_include_directories(${COMPONENT} INTERFACE
$<INSTALL_INTERFACE:include/${runtime_INCLUDE}>)
endif()
if(${COMPONENT}_EXTRA_FILES)
install(
FILES ${${COMPONENT}_EXTRA_FILES}
DESTINATION ${runtime_DESTINATION}
)
endif()
# TODO: !!!probl�mes dans la g�n�ration des qm
#if(${COMPONENT}_TRANSLATION)
# message("-- Install translations for ${COMPONENT}, ${${COMPONENT}_TRANSLATIONS_FILES}")
# install(
# FILES ${${COMPONENT}_TRANSLATIONS_FILES}
# DESTINATION ${runtime_DESTINATION}/translations
# )
#endif()
#set(${COMPONENT}_DEPS_FILES file(GET_RUNTIME_DEPENDENCIES))
#install(RUNTIME_DEPENDENCY_SET ${COMPONENT}_DEPS_FILES )
# Set installed_targets list (for <project>-config.cmake file)
list(APPEND installed_targets ${COMPONENT})
list(REMOVE_DUPLICATES installed_targets)
set(installed_targets ${installed_targets}
CACHE INTERNAL "List of all exported components (targets).")
#install(CODE [[file(GET_RUNTIME_DEPENDENCIES) ]])
endfunction()
function(component_get_install COMPONENT)
set(oneValueArgs DESTINATION MODULE )
cmake_parse_arguments(install "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
if(NOT ${COMPONENT}_LIBRARIES)
foreach(${COMPONENT}_LIBRARY IN LISTS ${COMPONENT}_LIBS)
find_library(${COMPONENT}_LIB ${${COMPONENT}_LIBRARY}
PATHS ${install_DESTINATION}/lib
REQUIRED)
list(APPEND ${COMPONENT}_LIBRARIES ${${COMPONENT}_LIB})
unset(${COMPONENT}_LIB CACHE)
endforeach()
endif()
find_package_handle_standard_args(${COMPONENT}
REQUIRED_VARS ${COMPONENT}_LIBRARIES ${COMPONENT}_INCLUDE_DIR)
if(${COMPONENT}_FOUND)
option(USE_${COMPONENT} "Use ${COMPONENT}" ON)
foreach(${COMPONENT}_LIBRARY ${COMPONENT}_FULLLIBRARY IN ZIP_LISTS ${COMPONENT}_LIBS ${COMPONENT}_LIBRARIES)
add_library(${${COMPONENT}_LIBRARY} SHARED IMPORTED)
set_property(TARGET ${${COMPONENT}_LIBRARY} PROPERTY INTERFACE_LINK_LIBRARIES ${${COMPONENT}_FULLLIBRARY})
set_property(TARGET ${${COMPONENT}_LIBRARY} PROPERTY IMPORTED_IMPLIB ${${COMPONENT}_FULLLIBRARY})
set_target_properties(${${COMPONENT}_LIBRARY} PROPERTIES
OUTPUT_NAME "${${COMPONENT}_LIBRARY}"
IMPORTED_LOCATION "${install_DESTINATION}/lib"
INTERFACE_INCLUDE_DIRECTORIES "${${COMPONENT}_INCLUDE_DIR}"
)
target_link_libraries(${install_MODULE} PUBLIC ${${COMPONENT}_LIBRARY})
endforeach()
endif()
endfunction()
#====================================================================
#
# Define and setup build & install process for a target for each models in directory MODELS
# One model = one cpp file
#
# Usage:
#
# create_model(MODELS)
#
# The following variables must be properly set before any call to this function
# - MODELS_SFX : string, define a suffixe of the target
#
function(create_model MODELS)
get_sources(${MODELS} DIRS ${MODELS})
foreach(_FILEMODEL IN LISTS ${MODELS}_SRCS)
get_filename_component(_NAMEMODEL ${_FILEMODEL} NAME_WLE)
get_filename_component(COMPONENT_DIR ${_FILEMODEL} DIRECTORY)
set(COMPONENT ${_NAMEMODEL}${MODELS_SFX})
message("\n-- Set up for ${PROJECT_NAME}_${COMPONENT} library ...")
message("\n--dir ${CMAKE_CURRENT_SOURCE_DIR}/${COMPONENT_DIR}")
add_library(${COMPONENT} SHARED ${_FILEMODEL})
target_compile_definitions(${COMPONENT} PRIVATE -DMODELS_LIBRARY)
apply_compiler_options(${COMPONENT} DIAGNOSTICS_LEVEL ${WARNINGS_LEVEL})
target_include_directories(${COMPONENT} PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_include_directories(${COMPONENT} PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${COMPONENT_DIR}>)
target_include_directories(${COMPONENT} INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_include_directories(${COMPONENT} INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${COMPONENT_DIR}>)
message("-- Include dir for ${COMPONENT}, ${CMAKE_CURRENT_SOURCE_DIR}/${COMPONENT_DIR}")
set_target_properties(${COMPONENT} PROPERTIES
OUTPUT_NAME "${COMPONENT}"
VERSION "${PROJECT_SOVERSION}"
SOVERSION "${PROJECT_SOVERSION_MAJOR}"
)
include(EigenSetup)
include(SpdlogSetup)
find_package(Cairn REQUIRED CairnCore CairnModelInterface )
target_compile_definitions(${COMPONENT} PRIVATE -D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR)
target_compile_definitions(${COMPONENT} PRIVATE -DEIGEN_MPL2_ONLY)
if (WITH_PYBIND)
target_link_libraries(${COMPONENT} PRIVATE cairn::CairnCore)
target_link_libraries(${COMPONENT} PRIVATE cairn::CairnModelInterface)
else()
target_link_libraries(${COMPONENT} PRIVATE CAIRN::CAIRN)
endif()
target_link_libraries(${COMPONENT} PRIVATE mipmodeler::MIPModeler)
target_link_libraries(${COMPONENT} PRIVATE mipmodeler::ModelerInterface)
target_link_libraries(${COMPONENT} PRIVATE mipmodeler::MIPSolver)
target_link_libraries(${COMPONENT} PRIVATE Eigen3::Eigen)
target_link_libraries(${COMPONENT} PRIVATE spdlog::spdlog_header_only)
if (${_NAMEMODEL}_LINKEDMODELS)
foreach(_MODEL_LINK IN LISTS ${_NAMEMODEL}_LINKEDMODELS)
set(CAIRNMODEL_LINK ${_MODEL_LINK}${MODELS_SFX})
message("-- link ${CAIRNMODEL_LINK}")
if (WITH_PYBIND)
target_link_libraries(${COMPONENT} PRIVATE cairn::${CAIRNMODEL_LINK})
else()
target_link_libraries(${COMPONENT} PRIVATE CairnModels::${CAIRNMODEL_LINK})
endif()
endforeach()
endif()
# ---- Installation ----
set(${COMPONENT}_INSTALL_INTERFACE_INCLUDE_DIRECTORIES
${MODELS} # All .h are installed
)
component_install_setup(${COMPONENT} INCLUDE ${PROJECT_NAME}/models)
list(APPEND list_models "${_NAMEMODEL}:${MODELS}")
endforeach()
list(REMOVE_DUPLICATES list_models)
set(list_models ${list_models}
CACHE INTERNAL "List of all models")
endfunction()
#====================================================================
#
#
# create_models(MODELS_DIRS)
#
#
#
function(create_models MODELS_DIRS)
foreach(_DIRMODELS IN LISTS ${MODELS_DIRS} )
create_model(${_DIRMODELS})
endforeach()
endfunction()