-
Notifications
You must be signed in to change notification settings - Fork 6
/
CMakeLists.txt
442 lines (348 loc) · 16.3 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# Copyright 2020 Proyectos y Sistemas de Mantenimiento SL (eProsima).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################
# Project and Version information #
###############################################################################
cmake_minimum_required(VERSION 3.22)
# Set CMAKE_BUILD_TYPE to Release by default if generator is single config.
if(NOT GENERATOR_IS_MULTI_CONFIG AND NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Release' as none was specified.")
set(CMAKE_BUILD_TYPE Release CACHE STRING
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()
# PROJECT_SOURCE_DIR is the source directory of the most recent project() command.
# If it exists already then this file has been added and IS_TOP_LEVEL should be FALSE.
set(IS_TOP_LEVEL TRUE)
if(PROJECT_SOURCE_DIR)
set(IS_TOP_LEVEL FALSE)
string(REPLACE "\\" "/" PROJECT_SOURCE_DIR ${PROJECT_SOURCE_DIR} )
endif()
# Let's see what's the latest version of the current branch
execute_process(COMMAND git describe --tags WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
OUTPUT_VARIABLE GIT_TAG_VERSION ERROR_QUIET )
if (GIT_TAG_VERSION)
string(REGEX MATCH "[0-9]+(\\.[0-9]+)*" PROJECT_VERSION ${GIT_TAG_VERSION} )
else()
set(PROJECT_VERSION "2.0.0")
endif()
if (NOT PROJECT_VERSION)
message(FATAL_ERROR
"Project must have a version number. If not found in git, please, define PROJECT_VERSION variable.")
endif()
project("discovery-server" VERSION ${PROJECT_VERSION} LANGUAGES C CXX)
set(PROJECT_NAME_STYLED "DiscoveryServer")
set(PROJECT_NAME_LARGE "Discovery Server")
set(PROJECT_NAME_UPPER "DS")
include(CheckCXXCompilerFlag)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG OR
CMAKE_CXX_COMPILER_ID MATCHES "Clang")
check_cxx_compiler_flag(--std=c++11 SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W -Wall -Wextra -pedantic")
if(SUPPORTS_CXX11)
add_compile_options(--std=c++11)
else()
message(FATAL_ERROR "Compiler doesn't support C++11")
endif()
endif()
###############################################################################
# Test system configuration
###############################################################################
include(${PROJECT_SOURCE_DIR}/cmake/common/check_configuration.cmake)
check_stdcxx()
check_compile_feature()
check_endianness()
###############################################################################
# Check MSVC architecture
###############################################################################
if(MSVC OR MSVC_IDE)
check_msvc_arch()
endif()
###############################################################################
# Load external dependencies
###############################################################################
# tinyxml2
# first we search for the new config files
find_package(tinyxml2 CONFIG)
# according to docs and sources the above call searching for tinyxml2 would focus on:
# tinyxml2Config.cmake
# tinyxml2-config.cmake
# but this is only true for unix like OS. CMake windows APIs rely on non-case-sensitive
# APIs like GetFileAttributes thus the above call will on windows find:
# TinyXML2Config.cmake
# as a match for tinyxml2Config.cmake. That forces us to use the tinyxml2 target as a
# decision variable (old TinyXML2Config.cmake doesn't create any target).
# There are two versions of the tinyxml2Config.cmake:
# - old one: generated with the cmake export() command that defines a tinyxml2 target
# with all config info.
# - new one: generated with the cmake install(TARGET ... EXPORT ...) and install(EXPORT ..)
# cmake commands. This one deprecates the above one in per-target installation. Tinyxml2
# CMakelists uses the install(EXPORT) namespace option, thus, the target is actually call
# tinyxml2::tinyxml2.
# The tinyxml2 CMakelists.txt uses export() together with install(...) generating two independent
# tinyxml2Config.cmake files. Fortunately only installs one. We must be able to handle the existence
# of tinyxml2 and tinyxml2::tinyxml2 targets because we don't know which one is gonna find.
if(TARGET tinyxml2::tinyxml2)
# retrieve required properties
set(TINYXML2_LIBRARY tinyxml2::tinyxml2)
get_target_property(TINYXML2_INCLUDE_DIR tinyxml2::tinyxml2 INTERFACE_INCLUDE_DIRECTORIES)
else()
if(TARGET tinyxml2)
# retrieve required properties
set(TINYXML2_LIBRARY tinyxml2)
get_target_property(TINYXML2_INCLUDE_DIR tinyxml2 INTERFACE_INCLUDE_DIRECTORIES)
else()
# redundant on Windows because matches find_package(tinyxml2 CONFIG) behaviour
find_package(TinyXML2 CONFIG)
if(NOT TinyXML2_FOUND)
# we try find the binaries directly
include(FindPackageHandleStandardArgs)
find_path(TINYXML2_INCLUDE_DIR NAMES tinyxml2.h)
find_library(TINYXML2_LIBRARY tinyxml2)
find_package_handle_standard_args(tinyxml2 DEFAULT_MSG TINYXML2_LIBRARY TINYXML2_INCLUDE_DIR)
if(NOT tinyxml2_FOUND)
message(FATAL_ERROR "Unable to load tinyxml2 dependency. ${TINYXML2_INCLUDE_DIR}")
endif()
endif()
endif()
endif()
find_package(fastcdr REQUIRED )
# new DDS API is required
find_package(fastdds 3 REQUIRED)
find_package(OpenSSL QUIET) # according to cmake docs on FindOpenSS is hinted define OPENSSL_ROOT_DIR
#some tests require python
find_package(PythonInterp 3 REQUIRED)
#install lxml module required for some tests
execute_process(COMMAND ${Python3_EXECUTABLE} "-m pip install lxml xmltodict jsondiff")
# OpenSSL isn't mandatory
if(TARGET OpenSSL::SSL)
set(OPENSSL_TARGET OpenSSL::SSL)
else()
unset(OPENSSL_TARGET)
unset(OPENSSL_INCLUDE_DIR CACHE)
endif()
###############################################################################
# Logging
###############################################################################
option(LOG_LEVEL_INFO OFF)
option(LOG_LEVEL_WARN OFF)
option(LOG_LEVEL_ERROR OFF)
add_definitions(-DLOG_LEVEL_INFO=$<STREQUAL:$<UPPER_CASE:${LOG_LEVEL_INFO}>,ON>)
add_definitions(-DLOG_LEVEL_WARN=$<STREQUAL:$<UPPER_CASE:${LOG_LEVEL_WARN}>,ON>)
add_definitions(-DLOG_LEVEL_ERROR=$<STREQUAL:$<UPPER_CASE:${LOG_LEVEL_ERROR}>,ON>)
configure_file(${PROJECT_SOURCE_DIR}/src/DSLog.h.in ${PROJECT_SOURCE_DIR}/include/log/DSLog.h @ONLY)
###############################################################################
# Versioning
###############################################################################
# Generate a config.h with version global variables from a template and copy to the current build dir.
# project() doesn't create 0 valued version variables. We must workaround it.
foreach( version_var PROJECT_VERSION_MAJOR PROJECT_VERSION_MINOR PROJECT_VERSION_PATCH PROJECT_VERSION_TWEAK)
if( NOT ${version_var} )
set( ${version_var} "0 ")
list(APPEND version_unset ${version_var})
endif()
endforeach()
configure_file(${PROJECT_SOURCE_DIR}/src/config.h.in ${PROJECT_SOURCE_DIR}/include/version/config.h @ONLY)
# we leave the project() generated variables as they were before
foreach( version_var IN LISTS version_unset)
unset( ${version_var} )
endforeach()
unset(version_unset)
###############################################################################
# Activate Sanitizers
###############################################################################
option(SANITIZER "Enable Thread or Address sanitizers" OFF)
string(TOUPPER ${SANITIZER} SANITIZER)
if (SANITIZER)
if(${SANITIZER} STREQUAL "ADDRESS")
message(STATUS "Enabling address sanitizer...")
# Warning/Error messages
if(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug"))
message(WARNING "Address sanitizer results with an optimized (non-Debug) build may be misleading")
endif()
if("${CMAKE_C_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
message(STATUS "Building with llvm Address sanitizer Tools")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
elseif(CMAKE_COMPILER_IS_GNUCXX)
message(STATUS "Building with Address sanitizer Tools")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
else()
message(FATAL_ERROR "Address sanitizer requires Clang or GCC. Aborting.")
endif()
elseif(${SANITIZER} STREQUAL "THREAD")
message(STATUS "Enabling thread sanitizer...")
# Warning/Error messages
if(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug"))
message(WARNING "Thread sanitizer results with an optimized (non-Debug) build may be misleading")
endif()
if("${CMAKE_C_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
message(STATUS "Building with llvm Thread sanitizer Tools")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
elseif(CMAKE_COMPILER_IS_GNUCXX)
message(STATUS "Building with Thread sanitizer Tools")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer")
else()
message(FATAL_ERROR "Thread sanitizer requires Clang or GCC. Aborting.")
endif()
else()
message(WARNING "Sanitizer option not supported. Continuing without any code instrumentation... ")
endif()
endif()
###############################################################################
# Compilation
###############################################################################
# Create the target and its dependencies, note that variable PROJECT_NAME is created by the closest project()
# called in the current directory scope or above.
# We must populate this variables as project sources grow
# Set Fast DDS source files
# add fast headers to enable class view on visual studio IDE
if (WIN32)
get_target_property(FAST_INCLUDE_DIR fastdds INTERFACE_INCLUDE_DIRECTORIES)
list(GET FAST_INCLUDE_DIR 0 FAST_INCLUDE_DIR)
#get_filename_component(FAST_INCLUDE_DIR "${FAST_INCLUDE_DIR}" DIRECTORY )
file(GLOB_RECURSE ALL_FAST_HEADERS
"${FAST_INCLUDE_DIR}/**/*.h"
"${FAST_INCLUDE_DIR}/**/*.hpp"
"${FAST_INCLUDE_DIR}/**/*.hxx"
)
endif(WIN32)
# xml schema file
set(${PROJECT_NAME}_schema_files
resources/xsd/discovery-server.xsd
resources/xsd/ds-snapshot.xsd
)
# testing static types
file(GLOB ${PROJECT_NAME}_xtypes
resources/xtypes/*.*
)
#filter out the idl within the static types
set( ${PROJECT_NAME}_idl ${${PROJECT_NAME}_xtypes} )
list(FILTER ${PROJECT_NAME}_idl INCLUDE REGEX "\\.idl$" )
set(${PROJECT_NAME}_header_files
#TODO: remove when development process is finished
${ALL_FAST_HEADERS}
# application sources
include/version/config.h
include/log/DSLog.h
include/DiscoveryServerManager.h
# library sources
include/DiscoveryItem.h
include/LateJoiner.h
include/IDs.h
)
set(${PROJECT_NAME}_source_files
#application sources
src/main.cpp
src/DiscoveryServerManager.cpp
#library sources
src/DiscoveryItem.cpp
src/LateJoiner.cpp
)
# Executable
add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_source_files} ${${PROJECT_NAME}_header_files}
${${PROJECT_NAME}_python_tests} ${${PROJECT_NAME}_xtypes})
# schema, types and auxiliary xmls
source_group(resources\\xsd FILES ${${PROJECT_NAME}_schema_files} )
source_group(resources\\xtypes FILES ${${PROJECT_NAME}_xtypes} )
# avoid visual studio from trying to compile .idl files with midl
set_source_files_properties(${${PROJECT_NAME}_idl} PROPERTIES HEADER_FILE_ONLY TRUE)
# BUILD_INTERFACE and INSTALL_INTERFACE are not variables but generator expressions
# (see under type Output-Related Expressions):
# $<BUILD_INTERFACE:...>. Content of ... when the property is exported using export(),
# or when the target is used by another target in the same buildsystem. Expands to the empty string otherwise.
# $<INSTALL_INTERFACE:...>. Content of ... when the property is exported using install(EXPORT), and empty otherwise.
# Relative paths are allowed within the INSTALL_INTERFACE expression and are interpreted relative to the installation
# prefix.
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
${TINYXML2_INCLUDE_DIR}
${ASIO_INCLUDE_DIR}
${OPENSSL_INCLUDE_DIR}
)
target_compile_definitions(${PROJECT_NAME} PRIVATE
ASIO_STANDALONE
$<$<AND:$<BOOL:${WIN32}>,$<STREQUAL:"${CMAKE_SYSTEM_NAME}","WindowsStore">>:_WIN32_WINNT=0x0603>
$<$<AND:$<BOOL:${WIN32}>,$<NOT:$<STREQUAL:"${CMAKE_SYSTEM_NAME}","WindowsStore">>>:_WIN32_WINNT=0x0601>
)
#if (WIN32)
# target_compile_options(${PROJECT_NAME} PRIVATE -wd4251)
#endif()
# we link dynamically to tinyxml2
target_link_libraries(${PROJECT_NAME} PUBLIC fastdds fastcdr
${TINYXML2_LIBRARY}
${OPENSSL_TARGET}
)
# Properties that change bin names depending on current config.
# This is convenient because on installation all bins share folder
set_target_properties(${PROJECT_NAME} PROPERTIES RELEASE_POSTFIX -${PROJECT_VERSION})
set_target_properties(${PROJECT_NAME} PROPERTIES DEBUG_POSTFIX -${PROJECT_VERSION}d)
set_target_properties(${PROJECT_NAME} PROPERTIES MINSIZEREL_POSTFIX -${PROJECT_VERSION}m)
set_target_properties(${PROJECT_NAME} PROPERTIES RELWITHDEBINFO_POSTFIX -${PROJECT_VERSION}rd)
###############################################################################
# Testing
###############################################################################
enable_testing()
include(CTest)
add_subdirectory(test)
###############################################################################
# Installation
###############################################################################
if(WIN32)
set(INSTALL_PER_USER 0 CACHE BOOL "Windows per user installation mode")
if(INSTALL_PER_USER)
set(CMAKE_INSTALL_PREFIX "$ENV{USERPROFILE}/AppData/Local/eProsima/${PROJECT_NAME_LARGE}/" )
endif()
endif()
set(BIN_INSTALL_DIR bin/ CACHE PATH "Installation directory for binaries")
set(INCLUDE_INSTALL_DIR include/ CACHE PATH "Installation directory for C++ headers")
set(INCLUDE_SOURCE_DIR src/ CACHE PATH "Installation directory for C++ sources")
set(LIB_INSTALL_DIR lib/ CACHE PATH "Installation directory for libraries")
set(LICENSE_INSTALL_DIR license/ CACHE PATH "Installation directory for licenses")
set(EXPORT_INSTALL_DIR export/ CACHE PATH "Installation directory for cmake exports")
# install binaries
install(TARGETS ${PROJECT_NAME}
COMPONENT bin
EXPORT ${PROJECT_NAME}_Targets
RUNTIME DESTINATION ${BIN_INSTALL_DIR}
LIBRARY DESTINATION ${LIB_INSTALL_DIR}
ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
)
# Install includes
#install(DIRECTORY include/
# COMPONENT headers
# DESTINATION ${INCLUDE_INSTALL_DIR}
# FILES_MATCHING PATTERN "*.h"
# )
# export library
install(EXPORT ${PROJECT_NAME}_Targets
COMPONENT bin
DESTINATION ${EXPORT_INSTALL_DIR}
)
# Sources
#install(DIRECTORY ${PROJECT_SOURCE_DIR}/src/
# COMPONENT sources
# DESTINATION ${INCLUDE_SOURCE_DIR}
# FILES_MATCHING PATTERN "*.cpp"
# )
# Install licenses
install(FILES ${PROJECT_SOURCE_DIR}/license/LICENSE
COMPONENT licenses
DESTINATION ${LICENSE_INSTALL_DIR}
)