-
Notifications
You must be signed in to change notification settings - Fork 6
/
CMakeLists.txt
223 lines (184 loc) · 8.35 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
cmake_minimum_required(VERSION 3.24)
set (CMAKE_C_STANDARD 11)
set (CMAKE_CXX_STANDARD 17)
set(CMAKE_COMPILE_WARNING_AS_ERROR ON)
# Minimum required versions for MSVC
set (MIN_VS_TOOLSET_VERSION 142)
set (MIN_VS_PLATFORM_VERSION 10.0.18362.0)
if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.1)
message(FATAL_ERROR "Require at least gcc-5.1")
endif()
project(51DegreesCommon VERSION 4.0.1 LANGUAGES CXX C)
if (MSVC)
# Check minimum required versions for MSVC
if (CMAKE_VS_PLATFORM_TOOLSET MATCHES "v([0-9]+)")
if (${CMAKE_MATCH_1} LESS MIN_VS_TOOLSET_VERSION)
message(FATAL_ERROR "Require at least Platform Toolset v${MIN_VS_TOOLSET_VERSION}. These can be installed by Visual Studio Installer or as part of C++ Build Tools which can be downloaded from https://visualstudio.microsoft.com/visual-cpp-build-tools/")
endif()
else()
message(FATAL_ERROR "Platform toolset version should start with 'v' and followed by numbers. Got ${CMAKE_VS_PLATFORM_TOOLSET}.")
endif()
if (CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION VERSION_LESS ${MIN_VS_PLATFORM_VERSION})
message(FATAL_ERROR "Require at least windows SDK version ${MIN_VS_PLATFORM_VERSION} which can be downloaded from https://developer.microsoft.com/en-us/windows/downloads/sdk-archive/")
endif()
endif()
include(CTest)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_LIST_DIR}/cmake)
find_package(Threads REQUIRED)
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^arm" OR CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64")
set(IS_ARM TRUE)
endif()
if(NOT CMAKE_BUILD_TYPE AND NOT MSVC)
message("-- Setting default CMAKE_BUILD_TYPE to Release")
set(CMAKE_BUILD_TYPE "Release")
endif()
#Compute is test coverage enabled
string(TOLOWER "${CMAKE_BUILD_TYPE}" lower_CMAKE_BUILD_TYPE)
if (NOT MSVC AND "${lower_CMAKE_BUILD_TYPE}" STREQUAL "debug")
set(TESTCOVERAGE_ENABLED TRUE)
else()
set(TESTCOVERAGE_ENABLED FALSE)
endif()
message("-- TESTCOVERAGE_ENABLED=${TESTCOVERAGE_ENABLED}")
if (NOT MSVC)
if (lower_CMAKE_BUILD_TYPE MATCHES "^debug")
set(COMPILE_OPTION_DEBUG -D_DEBUG)
endif()
endif()
option(32bit "32bit" OFF)
option(MemoryOnly "MemoryOnly" OFF)
option(NoThreading "NoThreading" OFF)
option(ExceptionsDisabled "ExceptionsDisabled" OFF)
if (32bit AND NOT IS_ARM)
message("-- 32 bit compilation")
if (NOT MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
else()
set(CMAKE_VS_PLATFORM_NAME "Win32")
endif()
endif()
if (NOT MSVC AND NOT APPLE)
find_package(GccAtomic)
set(EXE_LINKER_FLAGS "${EXE_LINKER_FLAGS} -latomic")
endif()
if (MemoryOnly)
message("-- Memory only compilation (FIFTYONE_DEGREES_MEMORY_ONLY) is enabled")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFIFTYONE_DEGREES_MEMORY_ONLY")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFIFTYONE_DEGREES_MEMORY_ONLY")
endif()
if (NoThreading)
message("-- No Threading compilation (FIFTYONE_DEGREES_NO_THREADING) is enabled")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFIFTYONE_DEGREES_NO_THREADING")
endif()
if (ExceptionsDisabled)
message("-- Exceptions disable compilation (FIFTYONE_DEGREES_EXCEPTIONS_DISABLED) is enabled")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFIFTYONE_DEGREES_EXCEPTIONS_DISABLED")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFIFTYONE_DEGREES_EXCEPTIONS_DISABLED")
endif()
if (MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _UNICODE /D UNICODE")
endif()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
if (NOT MSVC)
include_directories(/usr/local/include)
link_directories(/usr/local/lib)
endif()
# Common
FILE(GLOB COMC_SRC ${CMAKE_CURRENT_LIST_DIR}/*.c)
FILE(GLOB COMC_H ${CMAKE_CURRENT_LIST_DIR}/*.h)
add_library(fiftyone-common-c ${COMC_SRC} ${COMC_H})
target_link_libraries(fiftyone-common-c ${CMAKE_THREAD_LIBS_INIT} ${GCCLIBATOMIC_LIBRARY})
FILE(GLOB COMCPP_SRC ${CMAKE_CURRENT_LIST_DIR}/*.cpp)
FILE(GLOB COMCPP_H ${CMAKE_CURRENT_LIST_DIR}/*.hpp)
add_library(fiftyone-common-cxx ${COMCPP_SRC} ${COMCPP_H})
target_link_libraries(fiftyone-common-cxx fiftyone-common-c)
set_target_properties(fiftyone-common-c fiftyone-common-cxx PROPERTIES FOLDER "Common")
set(COMMON_C_COMPILE_OPTIONS "${COMPILE_OPTION_DEBUG} -Werror -Wundef -Wno-atomic-alignment -Wno-unused-variable -Wno-unused-result -Wno-unused-but-set-variable")
separate_arguments(COMMON_C_COMPILE_OPTIONS)
if (MSVC)
target_compile_options(fiftyone-common-c PRIVATE "/D_CRT_SECURE_NO_WARNINGS" "/W4" "/WX")
target_compile_options(fiftyone-common-cxx PRIVATE "/D_CRT_SECURE_NO_WARNINGS" "/W4" "/WX")
else ()
target_compile_options(fiftyone-common-c PRIVATE ${COMMON_C_COMPILE_OPTIONS})
target_compile_options(fiftyone-common-cxx PRIVATE ${COMMON_C_COMPILE_OPTIONS})
if (TESTCOVERAGE_ENABLED)
message("-- fiftyone-common-c(xx)-cov adding targets with code coverage")
add_library(fiftyone-common-c-cov ${COMC_SRC} ${COMC_H})
target_link_libraries(fiftyone-common-c-cov ${CMAKE_THREAD_LIBS_INIT} ${GCCLIBATOMIC_LIBRARY})
add_library(fiftyone-common-cxx-cov ${COMCPP_SRC} ${COMCPP_H})
target_link_libraries(fiftyone-common-cxx-cov fiftyone-common-c-cov)
target_compile_options(fiftyone-common-c-cov PRIVATE "--coverage")
target_compile_options(fiftyone-common-cxx-cov PRIVATE "--coverage")
set_target_properties(fiftyone-common-c-cov fiftyone-common-cxx-cov PROPERTIES FOLDER "Common")
target_compile_options(fiftyone-common-c-cov PRIVATE ${COMMON_C_COMPILE_OPTIONS})
target_compile_options(fiftyone-common-cxx-cov PRIVATE ${COMMON_C_COMPILE_OPTIONS})
endif()
endif()
# Examples
add_executable(CachePerf ${CMAKE_CURRENT_LIST_DIR}/performance/CachePerf.c)
target_link_libraries(CachePerf fiftyone-common-c)
if (MSVC)
target_compile_options(CachePerf PRIVATE "/D_CRT_SECURE_NO_WARNINGS" "/W4" "/WX")
target_link_options(CachePerf PRIVATE "/WX")
else ()
target_compile_options(CachePerf PRIVATE ${COMMON_C_COMPILE_OPTIONS})
endif()
set_target_properties(CachePerf PROPERTIES FOLDER "Examples/Common")
# Tests
if(BUILD_TESTING)
# Download and unpack googletest at configure time
configure_file(${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
if(result)
message(FATAL_ERROR "-- CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
if(result)
message(FATAL_ERROR "-- Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()
include(GoogleTest)
set(COM_TEST ${CMAKE_CURRENT_LIST_DIR}/tests)
FILE(GLOB COM_TEST_SRC ${COM_TEST}/*.cpp)
FILE(GLOB COM_TEST_H ${COM_TEST}/*.hpp)
add_executable(CommonTests ${COM_TEST_SRC} ${COM_TEST_H})
if (MSVC)
target_compile_options(CommonTests PRIVATE "/D_CRT_SECURE_NO_WARNINGS" "/W4" "/WX")
target_link_options(CommonTests PRIVATE "/WX")
else ()
target_compile_options(CommonTests PRIVATE ${COMMON_C_COMPILE_OPTIONS})
if (lower_CMAKE_BUILD_TYPE STREQUAL "debug")
message("-- CommonTests Linker options for code coverage")
target_link_options(CommonTests PRIVATE "-fprofile-arcs" "-ftest-coverage")
endif()
endif()
if (TESTCOVERAGE_ENABLED)
message("-- CommonTests linking fiftyone-common-cxx-cov lib with coverage")
target_link_libraries(CommonTests fiftyone-common-cxx-cov gtest_main)
else()
target_link_libraries(CommonTests fiftyone-common-cxx gtest_main)
endif()
gtest_discover_tests(CommonTests PROPERTIES TEST_DISCOVERY_TIMEOUT 600 DISCOVERY_MODE PRE_TEST)
set_target_properties(CommonTests PROPERTIES FOLDER "Tests")
endif()