Repository navigation
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
190 lines (171 loc) · 8.32 KB
/
Copy pathCMakeLists.txt
File metadata and controls
190 lines (171 loc) · 8.32 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
cmake_minimum_required(VERSION 3.20)
project(inflatelib
VERSION 0.2
DESCRIPTION "A Deflate and Deflate64 decompression library"
HOMEPAGE_URL "https://github.com/microsoft/inflatelib"
LANGUAGES C CXX
)
# "Production" options
option(INFLATELIB_BUILD_SHARED "Build inflatelib as a shared library" OFF)
# "Test" options
option(INFLATELIB_TEST "Build tests for local development and CI validation" ON)
option(INFLATELIB_ASAN "Build with Address Sanitizer" OFF)
option(INFLATELIB_UBSAN "Build with Undefined Behavior Sanitizer" OFF)
option(INFLATELIB_FUZZ "Build the fuzzing target" OFF)
# Fix the CMake defaults. See: https://gitlab.kitware.com/cmake/cmake/-/issues/20812
if (INFLATELIB_TEST)
if (MSVC)
# /Ob1 disables inlining for functions not marked inline. This is not desired except for minimum size
string(REPLACE "/Ob1" "/Ob2" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
string(REPLACE "/Ob1" "/Ob2" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
# Don't use /INCREMENTAL for RelWithDebInfo
string(REPLACE "/INCREMENTAL" "/INCREMENTAL:NO" CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
string(REPLACE "/INCREMENTAL" "/INCREMENTAL:NO" CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO}")
string(REPLACE "/INCREMENTAL" "/INCREMENTAL:NO" CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}")
# We always want debug info
string(APPEND CMAKE_C_FLAGS_RELEASE " /Zi")
string(APPEND CMAKE_CXX_FLAGS_RELEASE " /Zi")
string(APPEND CMAKE_C_FLAGS_MINSIZEREL " /Zi")
string(APPEND CMAKE_CXX_FLAGS_MINSIZEREL " /Zi")
string(APPEND CMAKE_EXE_LINKER_FLAGS_RELEASE " /debug")
string(APPEND CMAKE_MODULE_LINKER_FLAGS_RELEASE " /debug")
string(APPEND CMAKE_SHARED_LINKER_FLAGS_RELEASE " /debug")
string(APPEND CMAKE_EXE_LINKER_FLAGS_MINSIZEREL " /debug")
string(APPEND CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL " /debug")
string(APPEND CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL " /debug")
# TODO: /Gy?
endif()
# Max warning levels/set warnings as errors
if (MSVC)
# MSVC-like command syntax
add_compile_options(/W4 /WX)
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
# MSVC-specific options
add_compile_options(/permissive-)
else()
# Assume clang-cl
add_compile_options(
-fno-delayed-template-parsing
-fno-ms-compatibility
)
endif()
else()
# Assume GNU-like command syntax
add_compile_options(-Wall -Wextra -pedantic -Werror)
endif()
# Handle sanitizer options
set(SANITIZER_FLAGS)
set(SANITIZER_LINKER_FLAGS)
set(SANITIZER_LIBS)
if ((CMAKE_C_COMPILER_ID MATCHES "Clang") OR (CMAKE_C_COMPILER_ID MATCHES "GNU"))
if (INFLATELIB_ASAN OR INFLATELIB_FUZZ)
list(APPEND SANITIZER_FLAGS -fsanitize=address)
# Clang + ASan on Windows curerntly has some limitations. We try and manually handle those limitations here as
# opposed to difficult to track down and understand failures later on.
if (WIN32)
if (CMAKE_BUILD_TYPE MATCHES "Debug")
message(FATAL_ERROR "Clang does not currently support linking with debug runtime libraries with Address Sanitizer enabled on Windows")
else()
if ($ENV{Platform} STREQUAL "x86")
list(APPEND SANITIZER_LIBS
clang_rt.asan_dynamic-i386
clang_rt.asan_dynamic_runtime_thunk-i386.lib
)
elseif ($ENV{Platform} STREQUAL "x64")
list(APPEND SANITIZER_LIBS
clang_rt.asan_dynamic-x86_64.lib
clang_rt.asan_dynamic_runtime_thunk-x86_64.lib
)
else()
message(FATAL_ERROR "Address Sanitizer not supported on $ENV{Platform}")
endif()
endif()
else()
list(APPEND SANITIZER_LINKER_FLAGS -fsanitize=address)
endif()
if (INFLATELIB_FUZZ)
# Fuzzing requires a few extra flags
list(APPEND SANITIZER_FLAGS
-fsanitize-coverage=inline-8bit-counters
-fsanitize-coverage=edge
-fsanitize-coverage=trace-cmp
-fsanitize-coverage=trace-div
)
if (WIN32)
list(APPEND SANITIZER_LIBS libsancov.lib)
list(APPEND SANITIZER_FLAGS /MT)
if ($ENV{Platform} STREQUAL "x86")
list(APPEND SANITIZER_LIBS clang_rt.fuzzer_MT-i386)
elseif ($ENV{Platform} STREQUAL "x64")
list(APPEND SANITIZER_LIBS clang_rt.fuzzer_MT-x86_64.lib)
endif()
# No 'else' clause because all paths handled above for ASan
elseif (CMAKE_C_COMPILER_ID MATCHES "GNU")
message(FATAL_ERROR "GCC does not currently support fuzzing")
else()
list(APPEND SANITIZER_LINKER_FLAGS -fsanitize=fuzzer)
endif()
endif()
endif()
if (INFLATELIB_UBSAN)
list(APPEND SANITIZER_FLAGS -fsanitize=undefined)
# Similar to ASan, UBSan has issues on Windows with Debug since the *ubsan_standalone* lib seems to have been
# built for non-debug
if (WIN32)
if (CMAKE_BUILD_TYPE MATCHES "Debug")
message(FATAL_ERROR "Clang does not currently support linking with debug runtime libraries with Undefined Behavior Sanitizer enabled on Windows")
else()
list(APPEND SANITIZER_FLAGS /MT)
endif()
else()
list(APPEND SANITIZER_LINKER_FLAGS -fsanitize=undefined)
endif()
endif()
elseif (CMAKE_C_COMPILER_ID MATCHES "MSVC")
if (INFLATELIB_ASAN OR INFLATELIB_FUZZ)
list(APPEND SANITIZER_FLAGS /fsanitize=address)
# Fuzzing requires a few extra flags
if (INFLATELIB_FUZZ)
list(APPEND SANITIZER_FLAGS
/fsanitize-coverage=inline-8bit-counters
/fsanitize-coverage=edge
/fsanitize-coverage=trace-cmp
/fsanitize-coverage=trace-div
)
list(APPEND SANITIZER_LIBS sancov.lib)
if ($ENV{Platform} STREQUAL "x86")
if (${CMAKE_BUILD_TYPE} MATCHES "Debug")
list(APPEND SANITIZER_LIBS clang_rt.fuzzer_MDd-i386)
else()
list(APPEND SANITIZER_LIBS clang_rt.fuzzer_MD-i386)
endif()
elseif ($ENV{Platform} STREQUAL "x64")
if (${CMAKE_BUILD_TYPE} MATCHES "Debug")
list(APPEND SANITIZER_LIBS clang_rt.fuzzer_MDd-x86_64.lib)
else()
list(APPEND SANITIZER_LIBS clang_rt.fuzzer_MD-x86_64.lib)
endif()
else()
message(FATAL_ERROR "Fuzzing not supported on $ENV{Platform}")
endif()
endif()
endif()
if (INFLATELIB_UBSAN)
message(FATAL_ERROR "MSVC does not support Undefined Behavior Sanitizer")
endif()
else()
if (INFLATELIB_ASAN)
message(FATAL_ERROR "Address Sanitizer support is not configured for this compiler")
elseif (INFLATELIB_UBSAN)
message(FATAL_ERROR "Undefined Behavior Sanitizer support is not configured for this compiler")
endif()
endif()
endif()
add_subdirectory(src)
if (INFLATELIB_TEST)
add_subdirectory(test)
endif()
# CPack
set(CPACK_PACKAGE_VENDOR "Microsoft Corporation")
set(CPACK_GENERATOR "ZIP;TGZ;TBZ2;TXZ;7Z")
include(CPack)