-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
280 lines (240 loc) · 9.89 KB
/
Copy pathCMakeLists.txt
File metadata and controls
280 lines (240 loc) · 9.89 KB
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
# SPDX-FileCopyrightText: © 2009 Team CharLS
# SPDX-License-Identifier: BSD-3-Clause
cmake_minimum_required(VERSION 3.22...4.3)
# Extract the version info from version.h
file(READ "include/charls/version.h" version)
string(REGEX MATCH "CHARLS_VERSION_MAJOR ([0-9]*)" _ ${version})
set(version_major ${CMAKE_MATCH_1})
string(REGEX MATCH "CHARLS_VERSION_MINOR ([0-9]*)" _ ${version})
set(version_minor ${CMAKE_MATCH_1})
string(REGEX MATCH "CHARLS_VERSION_PATCH ([0-9]*)" _ ${version})
set(version_patch ${CMAKE_MATCH_1})
message(STATUS "CharLS version: ${version_major}.${version_minor}.${version_patch}")
project(charls VERSION ${version_major}.${version_minor}.${version_patch} LANGUAGES C CXX)
# Determine if project is built as a sub-project (using add_subdirectory) or if it is the main project.
set(MAIN_PROJECT OFF)
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(MAIN_PROJECT ON)
message(STATUS "Building as main project, CMake version: ${CMAKE_VERSION}")
endif()
if(EMSCRIPTEN)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
message(STATUS "Building for 64-bit WASM target")
else()
message(STATUS "Building for 32-bit WASM target")
endif()
endif()
# The basic options to control what will be built besides the library.
option(CHARLS_BUILD_TESTS "Build test application" OFF)
option(CHARLS_BUILD_AFL_FUZZ_TEST "Build AFL test fuzzer application" OFF)
option(CHARLS_BUILD_LIB_FUZZER_FUZZ_TEST "Build LibFuzzer test fuzzer application" OFF)
option(CHARLS_BUILD_CLI "Build command line interface application" OFF)
option(CHARLS_BUILD_SAMPLES "Build sample applications" OFF)
option(CHARLS_INSTALL "Generate the install target." ${MAIN_PROJECT})
option(CHARLS_BUILD_ALL "Build all features" OFF)
if(CHARLS_BUILD_ALL)
foreach(_opt CHARLS_BUILD_TESTS CHARLS_BUILD_AFL_FUZZ_TEST
CHARLS_BUILD_LIB_FUZZER_FUZZ_TEST CHARLS_BUILD_CLI CHARLS_BUILD_SAMPLES)
set(${_opt} ON CACHE BOOL "" FORCE)
endforeach()
endif()
# Provide BUILD_SHARED_LIBS as an option for GUI tools
option(BUILD_SHARED_LIBS "Will control if charls lib is build as shared lib/DLL or static library")
# Provide option to build CharLS with address sanitizer
option(CHARLS_ENABLE_ASAN "Build with address sanitizer enabled." OFF)
# Provide option to build CharLS with clang-tidy
option(CHARLS_ENABLE_CLANG_TIDY "Enable clang-tidy static analysis." OFF)
# These options are used by the CI pipeline to ensure new warnings are detected quickly.
# Not enabled by default to ensure the CharLS package is end-user friendly.
option(CHARLS_PEDANTIC_WARNINGS "Enable extra warnings and static analysis." OFF)
option(CHARLS_TREAT_WARNING_AS_ERROR "Treat a warning as an error." OFF)
# CharLS is written in portable c++:
set(CMAKE_CXX_EXTENSIONS OFF)
# Configure the supported C++ compilers: gcc, clang and MSVC
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(PEDANTIC_CXX_COMPILE_FLAGS
-Wall
-Wextra
-pedantic
-pedantic-errors
-Wold-style-cast
-Wfloat-equal
-Wlogical-op
-Wundef
-Wredundant-decls
-Wshadow
-Wwrite-strings
-Wpointer-arith
-Wcast-qual
-Wformat=2
-Wmissing-include-dirs
-Wcast-align
-Wctor-dtor-privacy
-Wdisabled-optimization
-Winvalid-pch
-Woverloaded-virtual
-Wnon-virtual-dtor
-Wnoexcept
-Wdouble-promotion
-Wtrampolines
-Wzero-as-null-pointer-constant
-Wuseless-cast
-Wvector-operation-performance
-Wsized-deallocation
-Wattributes
-Wuseless-cast
-Wconversion
)
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0)
set(PEDANTIC_CXX_COMPILE_FLAGS ${PEDANTIC_CXX_COMPILE_FLAGS}
-Wshift-overflow=2
-Wnull-dereference
-Wduplicated-cond
)
endif()
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0)
set(PEDANTIC_CXX_COMPILE_FLAGS ${PEDANTIC_CXX_COMPILE_FLAGS}
-Wcast-align=strict
)
endif()
set(WARNINGS_AS_ERRORS_FLAG_COMPILER -Werror)
set(WARNINGS_AS_ERRORS_FLAG_LINKER LINKER:--fatal-warnings)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(PEDANTIC_CXX_COMPILE_FLAGS
-Wall
-Wextra # (-W is synonym)
-Wnon-gcc
-Wpedantic
-Wcast-qual
-Wformat=2
-Wvla
-Warray-bounds-pointer-arithmetic
-Wassign-enum
-Wbad-function-cast
-Wconditional-uninitialized
-Widiomatic-parentheses
-Wimplicit-fallthrough
-Wloop-analysis
-Wpointer-arith
-Wshift-sign-overflow
-Wtautological-constant-in-range-compare
-Wunreachable-code-aggressive
-Wthread-safety
-Wthread-safety-beta
-Wcomma
-Wconversion
# -Weverything provides the option to discover useful Clang warnings.
# The list below ignores not useful Weverything warnings.
-Wno-weak-vtables # Ignore, linker will remove the couple of extra vtables.
-Wno-padded # Ignore, padding optimization is not needed.
-Wno-c++98-compat # Ignore, CharLS targets C++17, ignore C++98 compatibility.
-Wno-c++98-compat-pedantic # Ignore, CharLS targets C++17, ignore C++98 compatibility.
-Wno-global-constructors # Ignore, by design CharLS uses types created at startup.
-Wno-switch-enum # Ignore, cases are handled by default.
-Wno-sign-conversion # Ignore, would just introduce ugly static_asserts.
-Wno-exit-time-destructors # Ignore, by design exit-time destructors are used.
-Wno-missing-braces # Ignore, False warning in clang 5.0, fixed in 6.0.
)
if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0.2)
set(PEDANTIC_CXX_COMPILE_FLAGS ${PEDANTIC_CXX_COMPILE_FLAGS}
-Wno-undefined-func-template # Ignore, linker will complain if final template code is not available.
)
endif()
if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10.0)
set(PEDANTIC_CXX_COMPILE_FLAGS ${PEDANTIC_CXX_COMPILE_FLAGS}
-Walloca
)
endif()
set(WARNINGS_AS_ERRORS_FLAG_COMPILER -Werror)
if (NOT CMAKE_CXX_COMPILER_ID MATCHES "AppleClang")
set(WARNINGS_AS_ERRORS_FLAG_LINKER LINKER:--fatal-warnings)
endif()
if(NOT APPLE)
set(LIB_FUZZER_SUPPORTED ON)
endif()
endif()
if(MSVC)
message(STATUS "MSVC architecture: ${MSVC_CXX_ARCHITECTURE_ID}")
set(PEDANTIC_CXX_COMPILE_FLAGS /W4)
set(WARNINGS_AS_ERRORS_FLAG_COMPILER /WX)
set(WARNINGS_AS_ERRORS_FLAG_LINKER /WX)
# Exception settings are not set for ARM(64) when building for Ninja
string(TOLOWER ${CMAKE_CXX_COMPILER} CXX_COMPILER_LOWER)
string(FIND ${CXX_COMPILER_LOWER} "arm" ARM_DETECTED)
if(${ARM_DETECTED} GREATER 0)
add_compile_options("/EHsc")
endif()
# Remove option /GR (/GR is added by default by CMake). /GR is already the default
# and this makes it possible to use /GR- without warnings.
string(REGEX REPLACE " /GR" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
# All C and C++ source files are encoded in UTF-8 without signature (BOM), MSVC requires the utf-8 switch to read files correctly.
add_compile_options("/utf-8")
# Zc:__cplusplus: Will configure the MSVC compiler to use the correct value for the __cplusplus macro
# This option is introduced with Visual Studio 2017 version 15.7
if(MSVC_VERSION GREATER_EQUAL 1914)
add_compile_options("/Zc:__cplusplus")
endif()
# /Zc:throwingNew: Will configure the MSVC compiler that only the standard throwing operator new is used.
add_compile_options("/Zc:throwingNew")
# /permissive-: Will configure the MSVC compiler to accept only standards conformance C++
# This option is introduced with Visual Studio 2017.
if(MSVC_VERSION GREATER_EQUAL 1910)
add_compile_options("/permissive-")
endif()
# /ZH:SHA_256: Will use SHA 256 for checksums between source code and debug symbols (more secure)
add_compile_options("/ZH:SHA_256")
# /guard:cf: Will enable Control Flow Guard, which provides addition compile time and runtime checks.
add_compile_options("/guard:cf")
add_link_options("/guard:cf")
# /CETCOMPAT: Mark executable image compatible with the Control-flow Enforcement Technology (CET) Shadow Stack.
# Marking an image CET compatible is only supported for x64 and x86 CPUs.
# Depending on the hardware, 11th generation Intel and AMD Zen3 and newer CPUs, shadow stacks are enabled at runtime.
if(${ARM_DETECTED} EQUAL -1)
add_link_options("/CETCOMPAT")
endif()
# Enable LibFuzzer support for MSVC
if(${ARM_DETECTED} EQUAL -1 AND MSVC_VERSION GREATER_EQUAL 1930)
set(LIB_FUZZER_SUPPORTED ON)
endif()
endif()
if(CHARLS_ENABLE_CLANG_TIDY)
find_program(CLANG_TIDY_EXE NAMES clang-tidy)
if(CLANG_TIDY_EXE)
message(STATUS "Clang-tidy enabled and found at: ${CLANG_TIDY_EXE}")
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
else()
message(WARNING "Clang-tidy enabled, but not found!")
endif()
endif()
# When enabled apply the pedantic warnings options and warnings as errors to globally.
if(CHARLS_PEDANTIC_WARNINGS)
if(MSVC)
# Remove existing warning level (W3 is added by default by CMake), duplicate level will generate cmd-line warnings.
string(REGEX REPLACE " /W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
endif()
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:${PEDANTIC_CXX_COMPILE_FLAGS}>")
endif()
if(CHARLS_TREAT_WARNING_AS_ERROR)
add_compile_options(${WARNINGS_AS_ERRORS_FLAG_COMPILER})
add_link_options(${WARNINGS_AS_ERRORS_FLAG_LINKER})
endif()
include(src/CMakeLists.txt)
if(CHARLS_BUILD_TESTS)
enable_testing()
add_subdirectory(test)
endif()
if(CHARLS_BUILD_AFL_FUZZ_TEST)
add_subdirectory(fuzzing/afl)
endif()
if(CHARLS_BUILD_LIB_FUZZER_FUZZ_TEST AND LIB_FUZZER_SUPPORTED)
message(STATUS "LibFuzzer test applications are requested and supported and will be built")
add_subdirectory(fuzzing/libfuzzer-decoder)
add_subdirectory(fuzzing/libfuzzer-encoder)
endif()
if(CHARLS_BUILD_CLI)
add_subdirectory(cli)
endif()
if(CHARLS_BUILD_SAMPLES)
add_subdirectory(samples)
endif()