-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
428 lines (353 loc) · 15 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
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
# see https://github.com/chromium/chromium/blob/9db0b5162b8648833c3a6b1af044cebd9d7d3855/base/BUILD.gn
cmake_minimum_required(VERSION 3.4)
# project() must be before checks, see https://stackoverflow.com/a/26437667/10904212
set(PROJECT_NAME "flex_reflect_plugin")
set(PROJECT_VERSION 1.0.0.1)
project(${PROJECT_NAME}
VERSION ${PROJECT_VERSION}
LANGUAGES CXX C)
set(FLEX_REFLECT_LIB_NAME ${PROJECT_NAME})
option(ENABLE_TESTS "Enable tests" OFF)
# used by https://docs.conan.io/en/latest/developing_packages/workspaces.html
get_filename_component(LOCAL_BUILD_ABSOLUTE_ROOT_PATH
"${PACKAGE_flex_reflect_plugin_SRC}"
ABSOLUTE)
if(EXISTS "${LOCAL_BUILD_ABSOLUTE_ROOT_PATH}")
# path to Find*.cmake file
list(PREPEND CMAKE_MODULE_PATH "${LOCAL_BUILD_ABSOLUTE_ROOT_PATH}/cmake")
endif()
set(${PROJECT_NAME}_BUILD_SHARED_LIBS
TRUE CACHE BOOL
"Use .so/.dll")
message(STATUS "${PROJECT_NAME}_BUILD_SHARED_LIBS = ${${PROJECT_NAME}_BUILD_SHARED_LIBS}")
if(${PROJECT_NAME}_BUILD_SHARED_LIBS)
set(CORE_LIB_TYPE SHARED)
else()
set(CORE_LIB_TYPE STATIC)
message(FATAL_ERROR "ONLY SHARED PLUGINS SUPPORTED FOR NOW")
endif(${PROJECT_NAME}_BUILD_SHARED_LIBS)
set(ENABLE_CLING TRUE CACHE BOOL "ENABLE_CLING")
message(STATUS "ENABLE_CLING=${ENABLE_CLING}")
set(ENABLE_CLANG_FROM_CONAN FALSE CACHE BOOL "ENABLE_CLANG_FROM_CONAN")
message(STATUS "ENABLE_CLANG_FROM_CONAN=${ENABLE_CLANG_FROM_CONAN}")
if(ENABLE_CLANG_FROM_CONAN AND ENABLE_CLING)
message(FATAL_ERROR
"don't use both ENABLE_CLING and ENABLE_CLANG_FROM_CONAN at the same time. cling already provides clang libtooling")
endif()
set(CUSTOM_PLUGINS
"${CMAKE_CURRENT_SOURCE_DIR}/custom_plugins.cmake"
CACHE STRING
"Path to custom plugins")
message(STATUS "CUSTOM_PLUGINS=${CUSTOM_PLUGINS}")
if(EXISTS ${CUSTOM_PLUGINS})
include(${CUSTOM_PLUGINS})
endif()
# --------------------------- conan configuration ------------------------------
# Note: FetchContent is available since CMake 3.11
# see https://cmake.org/cmake/help/git-master/module/FetchContent.html
# By default, everything is downloaded into your build directory
# Once CMake successfully downloads our external content, it sets two variables that can be used in CMakeLists.txt to locate the new data:
# 1 <resource_name>_SOURCE_DIR
# specifies the location of the downloaded sources,
# 2 <resource_name>_BINARY_DIR
# specifies where is the default build directory for the downloaded sources.
include(FetchContent)
set(FetchContent_cmake_utils "cmake_utils")
FetchContent_Declare(
${FetchContent_cmake_utils}
PREFIX external_dependencies/${FetchContent_cmake_utils}
GIT_REPOSITORY https://github.com/blockspacer/cmake_utils.git
GIT_TAG origin/master
# Disable warning about detached HEAD https://stackoverflow.com/a/36794768
GIT_CONFIG advice.detachedHead=false
SOURCE_DIR "${CMAKE_BINARY_DIR}/${FetchContent_cmake_utils}"
BINARY_DIR "${CMAKE_BINARY_DIR}/${FetchContent_cmake_utils}-build"
CMAKE_ARGS "-Wno-dev"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
FetchContent_GetProperties(${FetchContent_cmake_utils})
if (NOT ${FetchContent_cmake_utils}_POPULATED)
# Populate command can be used only once for every resource
# during cmake configuration, which explains the conditional above.
FetchContent_Populate(${FetchContent_cmake_utils})
endif()
message(STATUS "${FetchContent_cmake_utils}_SOURCE_DIR = ${${FetchContent_cmake_utils}_SOURCE_DIR}")
message(STATUS "${FetchContent_cmake_utils}_BINARY_DIR = ${${FetchContent_cmake_utils}_BINARY_DIR}")
include(${${FetchContent_cmake_utils}_SOURCE_DIR}/Utils.cmake)
set(FetchContent_conan_auto_install "conan_auto_install")
FetchContent_Declare(
${FetchContent_conan_auto_install}
PREFIX external_dependencies/${FetchContent_conan_auto_install}
GIT_REPOSITORY https://github.com/blockspacer/conan_auto_install.git
GIT_TAG origin/master
# Disable warning about detached HEAD https://stackoverflow.com/a/36794768
GIT_CONFIG advice.detachedHead=false
SOURCE_DIR "${CMAKE_BINARY_DIR}/${FetchContent_conan_auto_install}"
BINARY_DIR "${CMAKE_BINARY_DIR}/${FetchContent_conan_auto_install}-build"
CMAKE_ARGS "-Wno-dev"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
FetchContent_GetProperties(${FetchContent_conan_auto_install})
if (NOT ${FetchContent_conan_auto_install}_POPULATED)
# Populate command can be used only once for every resource
# during cmake configuration, which explains the conditional above.
FetchContent_Populate(${FetchContent_conan_auto_install})
endif()
message(STATUS "${FetchContent_conan_auto_install}_SOURCE_DIR = ${${FetchContent_conan_auto_install}_SOURCE_DIR}")
message(STATUS "${FetchContent_conan_auto_install}_BINARY_DIR = ${${FetchContent_conan_auto_install}_BINARY_DIR}")
include(${${FetchContent_conan_auto_install}_SOURCE_DIR}/conan-auto-install.cmake)
option(CONAN_AUTO_INSTALL "Let CMake call conan install automatically"
OFF
)
set(CONAN_FORCE_AUTO_INSTALL
"" # may be changed to "FORCE"
CACHE STRING
"CONAN_FORCE_AUTO_INSTALL")
if (CONAN_AUTO_INSTALL)
set(CONAN_PROFILE
"clang"
CACHE STRING "Conan profile to use during installation")
if (NOT CMAKE_BUILD_TYPE MATCHES "Debug" )
set(conan_build_type "Release")
else()
set(conan_build_type "Debug")
endif()
# No --build=missing cause llvm requires long build
conan_auto_install(
CONAN_OPTIONS "--profile=${CONAN_PROFILE} -s build_type=${conan_build_type} -s cling_conan:build_type=Release -o openssl:shared=True"
${CONAN_FORCE_AUTO_INSTALL}
)
endif()
if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/conanbuildinfo.cmake")
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_BINARY_DIR}/)
include(${CMAKE_CURRENT_BINARY_DIR}/conanbuildinfo.cmake)
include(${CMAKE_CURRENT_BINARY_DIR}/conan_paths.cmake OPTIONAL)
conan_basic_setup(
# prevent conan_basic_setup from resetting cmake variables
TARGETS
KEEP_RPATHS
# see https://github.com/conan-io/conan/issues/6012
NO_OUTPUT_DIRS
)
else()
message (FATAL_ERROR "must use conan")
endif()
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
# Generate clang compilation database
# see https://stackoverflow.com/a/31086619/10904212
#set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(cmake_platform_detection REQUIRED)
run_cmake_platform_detection()
find_package(cmake_build_options REQUIRED)
setup_default_build_type(RELEASE)
setup_cmake_build_options(RELEASE DEBUG)
find_package(cmake_helper_utils REQUIRED)
message(STATUS "Compiler ${CMAKE_CXX_COMPILER}, version: ${CMAKE_CXX_COMPILER_VERSION}")
set_project_version(0 0 1) # from Utils.cmake
check_cmake_build_type_selected() # from Utils.cmake
enable_colored_diagnostics() # from Utils.cmake
print_cmake_system_info() # from Utils.cmake
check_supported_os() # from Utils.cmake
# Keep symbols for JIT resolution
set(LLVM_NO_DEAD_STRIP 1)
if(NOT TARGET CONAN_PKG::chromium_base)
message(FATAL_ERROR "Use chromium_base from conan")
endif()
if(NOT TARGET CONAN_PKG::chromium_build_util)
message(FATAL_ERROR "Use chromium_build_util from conan")
endif()
find_package(chromium_build_util REQUIRED)
#
if(TARGET chromium_build_util::chromium_build_util-static)
set(build_util_LIB "chromium_build_util::chromium_build_util-static")
else()
message(FATAL_ERROR "not supported: using system provided chromium_build_util library")
endif()
find_package(chromium_base REQUIRED)
if(NOT TARGET ${base_LIB})
message(FATAL_ERROR "not supported ${base_LIB}: using system provided chromium_base library")
endif()
# see https://doc.magnum.graphics/corrade/corrade-cmake.html#corrade-cmake-subproject
find_package(Corrade REQUIRED PluginManager)
if(NOT TARGET CONAN_PKG::corrade)
message(FATAL_ERROR "Use corrade from conan")
endif()
find_package( X11 REQUIRED )
message(STATUS "X11_LIBRARIES = ${X11_LIBRARIES}")
find_package( EXPAT REQUIRED )
message(STATUS "EXPAT_LIBRARIES = ${EXPAT_LIBRARIES}")
find_package( ZLIB REQUIRED )
message(STATUS "ZLIB_LIBRARIES = ${ZLIB_LIBRARIES}")
#add_library( boost_outcome INTERFACE )
#target_include_directories( boost_outcome SYSTEM INTERFACE "submodules/boost.outcome/include" )
#get_target_property (BOOST_OUTCOME_IMPORTED_LOCATION boost_outcome INTERFACE_INCLUDE_DIRECTORIES)
#message( STATUS "boost_outcome=${BOOST_OUTCOME_IMPORTED_LOCATION}" )
#
#add_library(microsoft_gsl INTERFACE)
#target_include_directories(microsoft_gsl SYSTEM INTERFACE "submodules/GSL/include")
#get_target_property (microsoft_gsl_IMPORTED_LOCATION microsoft_gsl INTERFACE_INCLUDE_DIRECTORIES)
#message( STATUS "microsoft_gsl=${microsoft_gsl_IMPORTED_LOCATION}" )
message(STATUS "CMAKE_DL_LIBS = ${CMAKE_DL_LIBS}")
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/)
set(flex_reflect_plugin_include_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include/flex_reflect_plugin")
set(flex_reflect_plugin_src_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src")
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/PlatformChecks.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/CommonOptions.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/ProjectFiles.cmake)
add_library(${FLEX_REFLECT_LIB_NAME}-test-includes INTERFACE)
# $<INSTALL_INTERFACE:...> is exported using install(EXPORT)
# $<BUILD_INTERFACE:...> is exported using export(), or when the target is used by another target in the same buildsystem
macro(add_relative_include_dir TARGET VISIBILITY_BUILD VISIBILITY_INSTALL NEW_ELEM)
target_include_directories(${TARGET}
${VISIBILITY_BUILD} "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${NEW_ELEM}>"
${VISIBILITY_INSTALL} "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}/${NEW_ELEM}>"
)
target_include_directories( ${FLEX_REFLECT_LIB_NAME}-test-includes SYSTEM INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/${NEW_ELEM} )
endmacro(add_relative_include_dir)
# NOTE: cling does not support static libs, so we use SHARED
# https://github.com/root-project/cling/issues/280
# https://gitlab.kitware.com/cmake/cmake/issues/16473
message(STATUS "flex_reflect_plugin_SOURCES=${flex_reflect_plugin_SOURCES}")
add_library(${FLEX_REFLECT_LIB_NAME} ${CORE_LIB_TYPE}
${flex_reflect_plugin_SOURCES}
)
#add_relative_include_dir(${FLEX_REFLECT_LIB_NAME} PRIVATE PRIVATE "include/flex_reflect_plugin")
add_relative_include_dir(${FLEX_REFLECT_LIB_NAME} PUBLIC PUBLIC "include")
list(APPEND USED_3DPARTY_LIBS
${base_LIB}
${build_util_LIB}
)
find_package(flexlib REQUIRED)
if(${flexlib_HEADER_DIR} STREQUAL "")
message(FATAL_ERROR "unable to find flexlib_HEADER_DIR")
endif()
find_package(basis REQUIRED)
if(${basis_HEADER_DIR} STREQUAL "")
message(FATAL_ERROR "unable to find basis_HEADER_DIR")
endif()
list(APPEND USED_3DPARTY_LIBS
${basis_LIB}
${flexlib_LIB}
CONAN_PKG::boost
CONAN_PKG::openssl
CONAN_PKG::corrade
Corrade::PluginManager
${USED_BOOST_LIBS}
)
set_property(TARGET ${FLEX_REFLECT_LIB_NAME} PROPERTY CXX_STANDARD 17)
if(TARGET_EMSCRIPTEN)
# use PROPERTY CXX_STANDARD 17
else()
target_compile_features(${FLEX_REFLECT_LIB_NAME}
PUBLIC cxx_auto_type
PRIVATE cxx_variadic_templates)
endif()
set(USED_SYSTEM_LIBS
Threads::Threads # pthread, https://cmake.org/cmake/help/v3.13/module/FindThreads.html
${X11_LIBRARIES} # https://cmake.org/cmake/help/v3.13/module/FindX11.html
${CMAKE_DL_LIBS} # https://cmake.org/cmake/help/v3.13/variable/CMAKE_DL_LIBS.html
stdc++fs # C++17 std::filesystem
CACHE INTERNAL "USED_SYSTEM_LIBS")
target_link_libraries(${FLEX_REFLECT_LIB_NAME} PUBLIC
${USED_3DPARTY_LIBS}
${USED_SYSTEM_LIBS}
)
target_link_libraries(${FLEX_REFLECT_LIB_NAME} PRIVATE
CONAN_PKG::entt
)
set(DEBUG_LIBRARY_SUFFIX "")
set_target_properties(${FLEX_REFLECT_LIB_NAME}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}" # TODO: /lib
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}" # TODO: /lib
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}" # TODO: /bin
OUTPUT_NAME "${FLEX_REFLECT_LIB_NAME}$<$<CONFIG:Debug>:${DEBUG_LIBRARY_SUFFIX}>"
# Plugins don't have any prefix (e.g. 'lib' on Linux)
PREFIX ""
ENABLE_EXPORTS 1
CXX_STANDARD 17
CXX_EXTENSIONS OFF
CMAKE_CXX_STANDARD_REQUIRED ON
)
# POSITION_INDEPENDENT_CODE for -fPIC
set_property(TARGET ${FLEX_REFLECT_LIB_NAME}
PROPERTY POSITION_INDEPENDENT_CODE ON)
if(ENABLE_CLING)
find_package(Cling REQUIRED)
list(APPEND CLING_DEFINITIONS CLING_IS_ON=1)
target_link_libraries(${FLEX_REFLECT_LIB_NAME} PUBLIC#PRIVATE
CONAN_PKG::cling_conan
)
get_target_property (cling_conan_IMPORTED_LOCATION CONAN_PKG::cling_conan INTERFACE_INCLUDE_DIRECTORIES)
message( STATUS "cling_conan=${cling_conan_IMPORTED_LOCATION}" )
target_include_directories( ${FLEX_REFLECT_LIB_NAME} PUBLIC ${cling_conan_IMPORTED_LOCATION} )
if(MSVC)
set_target_properties(${FLEX_REFLECT_LIB_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS 1)
set_property(
TARGET ${FLEX_REFLECT_LIB_NAME}
APPEND_STRING
PROPERTY LINK_FLAGS
"/EXPORT:?setValueNoAlloc@internal@runtime@cling@@YAXPEAX00D_K@Z
/EXPORT:?setValueNoAlloc@internal@runtime@cling@@YAXPEAX00DM@Z
/EXPORT:cling_runtime_internal_throwIfInvalidPointer")
endif()
target_compile_definitions(${FLEX_REFLECT_LIB_NAME} PUBLIC CLING_IS_ON=1)
endif(ENABLE_CLING)
if(ENABLE_CLANG_FROM_CONAN)
target_link_libraries( ${FLEX_REFLECT_LIB_NAME} PUBLIC#PRIVATE
CONAN_PKG::libclang
CONAN_PKG::clang_tooling
CONAN_PKG::clang_tooling_core
CONAN_PKG::llvm_support
)
endif(ENABLE_CLANG_FROM_CONAN)
target_compile_options(${FLEX_REFLECT_LIB_NAME} PRIVATE
$<$<CXX_COMPILER_ID:GNU>:-Wall>)
target_compile_options(${FLEX_REFLECT_LIB_NAME} PRIVATE
-fno-rtti)
target_compile_definitions(${FLEX_REFLECT_LIB_NAME} PRIVATE
${flex_reflect_plugin_PRIVATE_DEFINES}
# https://stackoverflow.com/a/30877725
BOOST_SYSTEM_NO_DEPRECATED BOOST_ERROR_CODE_HEADER_ONLY
# see https://github.com/mosra/corrade/blob/af9d4216f07307a2dff471664eed1e50e180568b/modules/UseCorrade.cmake#L568
CORRADE_DYNAMIC_PLUGIN=1
)
target_compile_definitions(${FLEX_REFLECT_LIB_NAME} PUBLIC
${flex_reflect_plugin_PUBLIC_DEFINES}
${CLING_DEFINITIONS}
)
# TODO: DISABLE_DOCTEST
target_compile_definitions(${FLEX_REFLECT_LIB_NAME} PUBLIC
DISABLE_DOCTEST=1
)
# install and export steps
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Exports.cmake)
# for builds with conan workspace
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/conf/${FLEX_REFLECT_LIB_NAME}.conf")
execute_process(
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/conf/${FLEX_REFLECT_LIB_NAME}.conf
${CMAKE_BINARY_DIR}/${FLEX_REFLECT_LIB_NAME}.conf
)
endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/conf/${FLEX_REFLECT_LIB_NAME}.conf")
# for builds without conan
execute_process(
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/conf/${FLEX_REFLECT_LIB_NAME}.conf
${CMAKE_CURRENT_BINARY_DIR}/${FLEX_REFLECT_LIB_NAME}.conf
)
endif()
if(ENABLE_TESTS)
enable_testing()
message( "${PROJECT_NAME} testing enabled" )
add_subdirectory( ${CMAKE_CURRENT_SOURCE_DIR}/tests )
endif()