-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
205 lines (173 loc) · 5.1 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
cmake_minimum_required(VERSION 3.20.0 FATAL_ERROR)
#
# OPTIONS
#
option(SUPPRESS_WARNINGS "Suppress compiler warnings" OFF)
option(CLANG_TIDY "Perform Clang-Tidy" OFF)
option(CLANG_TIDY_FIX "Perform fixes for Clang-Tidy" OFF)
if(UNIX)
option(STATIC_LINK_LIBGCC "Linking libgcc statically" OFF)
option(STATIC_LINK_LIBSTDCPP "Linking libstdc++ statically" OFF)
endif()
#
# VARIABLES
#
# Local variables
if(UNIX AND NOT APPLE)
set(LINUX TRUE)
endif()
# Default CMake variables
set(CMAKE_BUILD_TYPE
Release
CACHE
STRING
"Specifies the build type on single-configuration generators. Defaults to Release."
)
if(UNIX)
# Prefer clang
if(NOT CMAKE_C_COMPILER)
find_program(CLANG clang)
if(CLANG)
set(CMAKE_C_COMPILER
"clang"
CACHE STRING "Specifies the C compiler. Defaults to clang")
endif()
endif()
if(NOT CMAKE_CXX_COMPILER)
find_program(CLANGPP clang++)
if(CLANGPP)
set(CMAKE_CXX_COMPILER
"clang++"
CACHE STRING "Specifies the C++ compiler. Defaults to clang++")
endif()
endif()
endif()
# Default CMake environment variables
set(ENV{CMAKE_EXPORT_COMPILE_COMMANDS} ON)
#
# OUTPUT PATH
#
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/lib)
#
# PROJECT
#
project(cmake_sample_all CXX)
#
# COMPILER/LINKER FLAGS
#
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(NOT MSVC)
# Prefer -g3 to -g
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -g3")
# Show warnings
if(NOT SUPPRESS_WARNINGS)
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -Wall -Wextra -Wcast-align -Wcast-qual -Wdelete-non-virtual-dtor -Wold-style-cast -Woverloaded-virtual -Wpointer-arith -Wfloat-equal -Winit-self -Wredundant-decls -Wconversion -Wsign-promo -Wswitch-default -Wswitch-enum -Wvariadic-macros -Wwrite-strings -Wno-unknown-pragmas"
)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-pragmas")
endif()
endif()
# Static linking of libgcc/libstdc++
if(LINUX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
if(STATIC_LINK_LIBGCC)
add_link_options(-static-libgcc)
endif()
if(STATIC_LINK_LIBSTDCPP)
add_link_options(-static-libstdc++)
endif()
endif()
if(APPLE AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") # except AppleClang
# Use libc++ instead of libstdc++
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()
else() # MSVC
# Show warnings
if(SUPPRESS_WARNINGS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /w")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
endif()
if(WIN32)
if(VCPKG_TARGET_TRIPLET MATCHES "static")
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
else()
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()
endif()
# fPIC/fPIE
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# Hide symbols
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
if(LINUX)
add_link_options(-Wl,--exclude-libs,ALL)
endif()
# Strip for release build
add_link_options($<$<CONFIG:Release,MinSizeRel>:-s>)
# Set shared library search path to ORIGIN
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
if(LINUX)
set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib")
elseif(APPLE)
set(CMAKE_INSTALL_RPATH "@loader_path/../lib")
endif()
#
# EXPORT COMPILE COMMANDS
#
if(CMAKE_EXPORT_COMPILE_COMMANDS)
add_custom_target(
gen_compile_commands ALL
COMMAND ${CMAKE_COMMAND} -E touch
${CMAKE_BINARY_DIR}/compile_commands.json # trick to always run
DEPENDS ${CMAKE_SOURCE_DIR}/compile_commands.json)
# Generate a compilation database with header files
if(NOT COMPDB)
find_program(COMPDB compdb)
endif()
if(COMPDB)
# Run compdb
add_custom_command(
OUTPUT ${CMAKE_SOURCE_DIR}/compile_commands.json
DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json
COMMAND ${CMAKE_COMMAND} -E remove
${CMAKE_SOURCE_DIR}/compile_commands.json
COMMAND ${COMPDB} list > ${CMAKE_SOURCE_DIR}/compile_commands.json)
else()
message(AUTHOR_WARNING "'compdb' not found")
# Copy file instead of compdb
add_custom_command(
OUTPUT ${CMAKE_SOURCE_DIR}/compile_commands.json
DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json
COMMAND
${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/compile_commands.json
${CMAKE_SOURCE_DIR}/compile_commands.json)
endif()
endif()
#
# SUBDIRECTORIES
#
# header only library project
add_subdirectory(my_header_lib)
# static library project
add_subdirectory(my_static_lib)
# shared library project
add_subdirectory(my_shared_lib)
# CUDA project
add_subdirectory(cuda)
# main project
add_subdirectory(main)
#
# CTest
#
enable_testing()
add_subdirectory(test)