This repository has been archived by the owner on Oct 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
282 lines (242 loc) · 10.9 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
# This cmake configuration is based on an existing file the author (Jiahui Xie)
# wrote for another project with some minor changes added to make it work
# properly with a modern c++ 14 codebase.
cmake_minimum_required(VERSION "3.4.3" FATAL_ERROR)
# Both languages have to be listed here due to issues in the cmake build system
# e.g. cmake will not find thread library for some unknown reason if c is not
# listed below.
project("CPlusPlusHowToProgram" C CXX)
# =============================== Build Info ==================================
if(APPLE)
# The short system name, e.g. "FreeBSD", "Darwin", "Linux", or "Windows".
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
message(STATUS "Build Environment: Mac OS X")
else()
message(STATUS "Build Environment: Mac OS")
endif()
elseif(WIN32)
if(BORLAND)
message(STATUS "Build Environment: Windows/Borland")
elseif(CYGWIN)
message(STATUS "Build Environment: Windows/Cygwin")
elseif(MINGW)
message(STATUS "Build Environment: Windows/MinGW")
elseif(MSVC)
message(STATUS "Build Environment: Windows/MSVC")
elseif(WATCOM)
message(STATUS "Build Environment: Windows/Watcom")
endif()
elseif(UNIX)
if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
message(STATUS "Build Environment: FreeBSD")
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
message(STATUS "Build Environment: Linux")
endif()
endif()
# =============================== Build Info ==================================
# ============================= CMake Settings ================================
# Let the compiler export compile commands to a 'json' file; this will add
# interactive compiler diagnostics for 'YouCompleteMe' plugin of 'Vim'.
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# --------------------------- Language Standards ------------------------------
# All the targets generated by this master file requires c++14.
set(CMAKE_CXX_STANDARD 14)
# Prevent the previous one from "decaying" to previous standards;
# i.e. make the c++14 standard a requirement.
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# --------------------------- Language Standards ------------------------------
# Make sure that our source directory is on the current cmake module path so
# that we can include cmake files from this directory.
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
if (CMAKE_VERSION VERSION_LESS 3.2)
set(UPDATE_DISCONNECTED_IF_AVAILABLE "")
else()
set(UPDATE_DISCONNECTED_IF_AVAILABLE "UPDATE_DISCONNECTED 1")
endif()
# ------------------------------ CMake Modules --------------------------------
# Expose the function definitions in the custom CMake modules.
include("AddExecutableList")
include("AddGraphvizTarget")
include("CTest")
include("DownloadProject")
# ------------------------------ CMake Modules --------------------------------
# --------------------------- Dependant Libraries -----------------------------
# Only 64-bit version Visual C++ on is supported on Windows.
# Reference:
# stackoverflow.com/questions/39258250/how-to-detect-if-64-bit-msvc-with-cmake
if(WIN32)
if(NOT MSVC)
message(
FATAL_ERROR
"Only Microsoft Visual C++ is supported on Windows!"
)
endif()
if(NOT CMAKE_CL_64)
message(
FATAL_ERROR
"Only 64-bit version generator is supported on Windows!"
)
endif()
endif()
# The GraphViz package is only used to build diagrams located in the 'doc'
# sub-directory; this step is skipped if it is not found.
find_package(GraphViz)
# Test the existence of thread library on the system.
find_package(Threads REQUIRED)
if(UNIX)
if(NOT CMAKE_THREAD_LIBS_INIT)
message(FATAL_ERROR "Pthread must be supported on the system!")
else()
# For systems that have multiple thread libraries support,
# select pthread as the preferred implementation to link to.
if(NOT CMAKE_USE_PTHREADS_INIT)
set(CMAKE_THREAD_PREFER_PTHREAD ON)
endif()
endif()
endif()
if(WIN32)
if(NOT CMAKE_USE_WIN32_THREADS_INIT)
message(FATAL_ERROR "Thread library must be supported on the system!")
endif()
endif()
find_package(Boost "1.62" REQUIRED)
if(NOT Boost_FOUND)
message( FATAL_ERROR "Boost library must be installed on the system!")
endif()
# --------------------------- Dependant Libraries -----------------------------
# ============================= CMake Settings ================================
# =============================== Google Test =================================
#download_project(
# PROJ googletest
# GIT_REPOSITORY https://github.com/google/googletest.git
# GIT_TAG release-1.8.0
# ${UPDATE_DISCONNECTED_IF_AVAILABLE}
#)
# Prevent GoogleTest from overriding compiler/linker options when building with
# Visual Studio
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
#add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR})
# =============================== Google Test =================================
# ============================ Visual C++ Flags ===============================
# ------------------------- Global Warning Options ----------------------------
# The default warning level ("/W3") used in Visual Studio 2017 should suffice.
set(VCPP_WARN4 "/W4")
set(VCPP_WARN_ADDITIONAL_SECURITY "/sdl")
# ------------------------- Global Warning Options ----------------------------
# ----------------------------- Output Options --------------------------------
# Reference:
# docs.microsoft.com/en-us/cpp/build/reference/
# utf-8-set-source-and-executable-character-sets-to-utf-8
# "
# By default, Visual Studio detects a byte-order mark to determine if the
# source file is in an encoded Unicode format, for example, UTF-16 or UTF-8.
# If no byte-order mark is found, it assumes the source file is encoded using
# the current user code page, unless you have specified a code page by using
# /utf-8 or the /source-charset option.
# "
# Since certain programs on UNIX have trouble parsing text files with byte
# order markers, the text files need to be explicitly treated as UTF-8 encoded
# on Windows.
set(VCPP_UTF8 "/utf-8")
# ----------------------------- Output Options --------------------------------
if(WIN32)
# 2 flags enabled in total.
add_compile_options(
#${VCPP_WARN4}
${VCPP_WARN_ADDITIONAL_SECURITY}
${VCPP_UTF8}
)
endif()
# ============================ Visual C++ Flags ===============================
# ================================ GCC Flags ==================================
# Note that these compiler options should work for all compilers that support
# the same flag as gcc: so clang, mingw, and cygwin are all valid candidates.
# ------------------------- Global Warning Options ----------------------------
set(GCC_WARN_ALL "-Wall")
set(GCC_WARN_EXTRA "-Wextra")
set(GCC_WARN_PEDANTIC "-Wpedantic")
# ------------------------- Global Warning Options ----------------------------
# ----------------------- Individual Warning Options --------------------------
# Warn whenever a pointer is cast so as to remove a type qualifier from the
# target type.
set(GCC_WARN_POINTER_CAST_QUALIFIER "-Wcast-qual")
# Warn for implicit conversions that may alter a value.
set(GCC_WARN_IMPLICIT_CONVERSION "-Wconversion")
# Warn when a value of type "float" is implicitly promoted to "double".
set(GCC_WARN_DOUBLE_PROMOTION "-Wdouble-promotion")
# Warn if comparison operators are directly used for floats.
set(GCC_WARN_FLOAT_EQUAL_FLAG "-Wfloat-equal")
# Warn if a user-supplied include directory does not exist.
set(GCC_WARN_MISSING_USER_INCLUDE "-Wmissing-include-dirs")
# Warn if pointer arithmetic is performed on pointers to either function or
# void.
set(GCC_WARN_POINTER_ARITH "-Wpointer-arith")
# Warn if local identifers shadow other identifiers outside the scope.
set(GCC_WARN_SHADOW_FLAG "-Wshadow")
# Warn if an automatic variable is used without first being initialized or if a
# variable may be clobbered by a "setjmp" call.
# In C++, warn if a non-static reference or non-static "const" member appears
# in a class without constructors.
set(GCC_WARN_UNINITIALIZED "-Wuninitialized")
# ----------------------- Individual Warning Options --------------------------
# ----------------------------- Output Options --------------------------------
# Use pipes to communicate among different stages of compilation;
# speed up the compilation when GNU Assembler is available.
set(GCC_OUTPUT_PIPE "-pipe")
# ----------------------------- Output Options --------------------------------
# ------------------------- Code Generation Options ---------------------------
# Enable exception handling.
set(GCC_GENERATION_EXCEPTION "-fexceptions")
# Generate assembly trap instructions if signed integer types overflow;
# this would abort the program in most platforms.
set(GCC_GENERATION_SIGNED_OVERFLOW_TRAP "-ftrapv")
# ------------------------- Code Generation Options ---------------------------
# ------------------------- Instrumentation Options ---------------------------
# GCC has built-ins that can help identify common memory-related bugs
set(GCC_SANITIZE_ADDRESS_FLAG "-fsanitize=address")
set(GCC_SANITIZE_LEAK_FLAG "-fsanitize=leak")
set(GCC_SANITIZE_UNDEFINED_FLAG "-fsanitize=undefined")
# ------------------------- Instrumentation Options ---------------------------
# From the cmake documentation: "Adds options to the compiler command line for
# sources in the current directory and below", so it is used rather than
# add_definitions(), which seems to be reserved for macros.
if(UNIX)
# 15 flags enabled in total.
add_compile_options(
${GCC_WARN_ALL}
${GCC_WARN_EXTRA}
${GCC_WARN_PEDANTIC}
${GCC_WARN_POINTER_CAST_QUALIFIER}
${GCC_WARN_IMPLICIT_CONVERSION}
${GCC_WARN_DOUBLE_PROMOTION}
${GCC_WARN_FLOAT_EQUAL_FLAG}
${GCC_WARN_MISSING_USER_INCLUDE}
${GCC_WARN_POINTER_ARITH}
${GCC_WARN_SHADOW_FLAG}
${GCC_WARN_UNINITIALIZED}
${GCC_OUTPUT_PIPE}
${GCC_GENERATION_EXCEPTION}
${GCC_GENERATION_SIGNED_OVERFLOW_TRAP}
)
# ${GCC_SANITIZE_ADDRESS_FLAG}
# ${GCC_SANITIZE_LEAK_FLAG}
# ${GCC_SANITIZE_UNDEFINED_FLAG}
endif()
# ================================ GCC Flags ==================================
# ============================= Source Probing ================================
# NOTE
# This has to be done after the flags for the compiler are set;
# the 'Google Test' section needs to be moved above the compiler flags section
# to make those flags affect only this project.
# Header(s) to be exported reside(s) in the include directory in the
# relative root directory of this project.
include_directories(
SYSTEM
${Boost_INCLUDE_DIRS}
#"${PROJECT_SOURCE_DIR}/include"
)
if(GraphViz_FOUND)
add_subdirectory("doc")
endif()
add_subdirectory("src")
# ============================= Source Probing ================================