Echo Writes Code

CrucibleConfigure.cmake

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
include_guard(GLOBAL)

function(crucible_configure_address_sanitizer)
  if(NOT CRUCIBLE_ENABLE_ADDRESS_SANITIZER)
    message(STATUS "NOT enabling AddressSanitizer")
    return()
  endif()

  message(STATUS "Configuring AddressSanitizer...")

  if(CRUCIBLE_ENABLE_THREAD_SANITIZER)
    message(FATAL_ERROR "Cannot enable both AddressSanitizer and ThreadSanitizer")
  endif()

  if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
    # See: https://clang.llvm.org/docs/AddressSanitizer.html
    list(APPEND OPTIONS "-fsanitize=address")
    set(PASS_TO_LINKER TRUE)
  elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
    # See: https://learn.microsoft.com/en-us/cpp/build/reference/fsanitize?view=msvc-170
    list(APPEND OPTIONS "/fsanitize=address")
    set(PASS_TO_LINKER FALSE)
  endif()

  if(OPTIONS)
    message(STATUS "  Adding compiler options: ${OPTIONS}")
    add_compile_options(${OPTIONS})

    if(PASS_TO_LINKER)
      message(STATUS "  Adding linker options: ${OPTIONS}")
      add_link_options(${OPTIONS})
    endif()
  else()
    message(STATUS "  AddressSanitizer is not supported")
  endif()
endfunction()

function(crucible_configure_exception_handling)
  message(STATUS "Configuring exception handling...")

  if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
    # See: https://learn.microsoft.com/en-us/cpp/build/reference/eh-exception-handling-model?view=msvc-170
    list(APPEND OPTIONS "/EHcs")
  endif()

  if(OPTIONS)
    message(STATUS "  Adding compiler options: ${OPTIONS}")
    add_compile_options(${OPTIONS})
  endif()
endfunction()

function(crucible_configure_language_standards)
  message(STATUS "Configuring language standards...")

  # See: https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_EXTENSIONS.html
  set(CMAKE_CXX_EXTENSIONS FALSE CACHE INTERNAL "")

  # See: https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD.html
  set(CMAKE_CXX_STANDARD 20 CACHE INTERNAL "")

  # See: https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD_REQUIRED.html
  set(CMAKE_CXX_STANDARD_REQUIRED TRUE CACHE INTERNAL "")

  message(STATUS "  C++ standard: ${CMAKE_CXX_STANDARD}")
endfunction()

function(crucible_configure_optimizations)
  message(STATUS "Configuring optimizations...")

  if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
    return()
  endif()

  if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
    # See: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
    list(APPEND OPTIONS "-fno-omit-frame-pointer")
  endif()

  if(OPTIONS)
    message(STATUS "  Adding compiler options: ${OPTIONS}")
    add_compile_options(${OPTIONS})
  endif()
endfunction()

function(crucible_configure_output_directory)
  message(STATUS "Configuring output directory...")

  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CRUCIBLE_BUILD_OUTPUT_DIRECTORY}" CACHE INTERNAL "")
  message(STATUS "  Archive output directory is now: ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}")

  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CRUCIBLE_BUILD_OUTPUT_DIRECTORY}" CACHE INTERNAL "")
  message(STATUS "  Library output directory is now: ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")

  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CRUCIBLE_BUILD_OUTPUT_DIRECTORY}" CACHE INTERNAL "")
  message(STATUS "  Runtime output directory is now: ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
endfunction()

function(crucible_configure_preprocessor_definitions)
  message(STATUS "Configuring preprocessor definitions...")

  list(APPEND DEFINITIONS "CRUCIBLE_CMAKE_VERSION=\"${CMAKE_VERSION}\"")
  list(APPEND DEFINITIONS "CRUCIBLE_CXX_COMPILER_ID=\"${CMAKE_CXX_COMPILER_ID}\"")
  list(APPEND DEFINITIONS "CRUCIBLE_CXX_COMPILER_VERSION=\"${CMAKE_CXX_COMPILER_VERSION}\"")
  list(APPEND DEFINITIONS "CRUCIBLE_VERSION=\"${PROJECT_VERSION}\"")

  file(TO_NATIVE_PATH "${PROJECT_SOURCE_DIR}/" SOURCE_ROOT)
  string(REPLACE "\\" "\\\\" SOURCE_ROOT_ESCAPED "${SOURCE_ROOT}")
  list(APPEND DEFINITIONS "CRUCIBLE_SOURCE_ROOT=\"${SOURCE_ROOT_ESCAPED}\"")

  if(DEFINITIONS)
    message(STATUS "  Adding preprocessor definitions: ${DEFINITIONS}")
    add_compile_definitions(${DEFINITIONS})
  endif()
endfunction()

function(crucible_configure_sanitizer_recovery_policy)
  if(NOT CRUCIBLE_ENABLE_ADDRESS_SANITIZER AND NOT CRUCIBLE_ENABLE_THREAD_SANITIZER AND NOT CRUCIBLE_ENABLE_UNDEFINED_BEHAVIOR_SANITIZER)
    message(STATUS "NOT enabling sanitizer recovery")
    return()
  endif()

  if(CRUCIBLE_ENABLE_SANITIZER_RECOVERY)
    message(STATUS "Attempting to enable sanitizer recovery...")

    if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
      list(APPEND OPTIONS "-fsanitize-recover=all")
    endif()
  else()
    message(STATUS "Attempting to disable sanitizer recovery...")

    if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
      list(APPEND OPTIONS "-fno-sanitize-recover=all")
    endif()
  endif()

  if(OPTIONS)
    message(STATUS "  Adding compiler options: ${OPTIONS}")
    add_compile_options(${OPTIONS})

    message(STATUS "  Adding linker options: ${OPTIONS}")
    add_link_options(${OPTIONS})
  else()
    message(STATUS "  Sanitizer recovery is not configurable")
  endif()
endfunction()

function(crucible_configure_symbol_export_behavior)
  message(STATUS "Configuring symbol export behavior...")

  # This makes it unnecessary to use a dllexport/dllimport macro for Windows
  if(WIN32 AND BUILD_SHARED_LIBS)
    message(STATUS "  Automatically generating a .def file for each DLL")
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON CACHE INTERNAL "")
  endif()

  # This makes backtraces work on ELF-based systems (i.e., Linux)
  if(UNIX AND NOT APPLE)
    message(STATUS "  Adding linker options: -rdynamic")
    add_link_options(-rdynamic)
  endif()
endfunction()

function(crucible_configure_text_encoding)
  message(STATUS "Configuring text encoding...")

  if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
    # See: https://learn.microsoft.com/en-us/cpp/build/reference/utf-8-set-source-and-executable-character-sets-to-utf-8?view=msvc-170
    list(APPEND OPTIONS "/utf-8")
  endif()

  if(OPTIONS)
    message(STATUS "  Adding compiler options: ${OPTIONS}")
    add_compile_options(${OPTIONS})
  endif()
endfunction()

function(crucible_configure_thread_sanitizer)
  if(NOT CRUCIBLE_ENABLE_THREAD_SANITIZER)
    message(STATUS "NOT enabling ThreadSanitizer")
    return()
  endif()

  message(STATUS "Configuring ThreadSanitizer...")

  if(CRUCIBLE_ENABLE_ADDRESS_SANITIZER)
    message(FATAL_ERROR "Cannot enable both AddressSanitizer and ThreadSanitizer")
  endif()

  if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
    # See: https://clang.llvm.org/docs/ThreadSanitizer.html
    list(APPEND OPTIONS "-fsanitize=thread")
  endif()

  if(OPTIONS)
    message(STATUS "  Adding compiler options: ${OPTIONS}")
    add_compile_options(${OPTIONS})

    message(STATUS "  Adding linker options: ${OPTIONS}")
    add_link_options(${OPTIONS})
  else()
    message(STATUS "  ThreadSanitizer is not supported")
  endif()
endfunction()

function(crucible_configure_undefined_behavior_sanitizer)
  if(NOT CRUCIBLE_ENABLE_UNDEFINED_BEHAVIOR_SANITIZER)
    message(STATUS "NOT enabling UndefinedBehaviorSanitizer")
    return()
  endif()

  message(STATUS "Configuring UndefinedBehaviorSanitizer...")

  if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
    # See: https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
    list(APPEND OPTIONS "-fsanitize=undefined")
  endif()

  if(OPTIONS)
    message(STATUS "  Adding compiler options: ${OPTIONS}")
    add_compile_options(${OPTIONS})

    message(STATUS "  Adding linker options: ${OPTIONS}")
    add_link_options(${OPTIONS})
  else()
    message(STATUS "  UndefinedBehaviorSanitizer is not supported")
  endif()
endfunction()

function(crucible_configure_warning_level)
  if(NOT CRUCIBLE_ENABLE_COMPILER_WARNINGS)
    message(STATUS "NOT enabling compiler warnings")
    return()
  endif()

  message(STATUS "Configuring warning level...")

  if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
    # See: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#Warning-Options
    list(APPEND OPTIONS "-Wall")
    list(APPEND OPTIONS "-Wextra")
    list(APPEND OPTIONS "-Werror")
		list(APPEND OPTIONS "-Wundef")
  endif()

  if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    # GCC freaks out whenever we have a format() method that happens to never get called
    list(APPEND OPTIONS "-Wno-unused-function")
  endif()

  if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
    # See: https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=msvc-170
    list(APPEND OPTIONS "/W4")
    list(APPEND OPTIONS "/WX")

    # See: https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-1-c4180?view=msvc-170)
    list(APPEND OPTIONS "/wd4180")

    # See: https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4702?view=msvc-170
    list(APPEND OPTIONS "/wd4702")
  endif()

  if(OPTIONS)
    message(STATUS "  Adding compiler options: ${OPTIONS}")
    add_compile_options(${OPTIONS})
  else()
    message(WARNING "  Additional warnings are not supported")
  endif()
endfunction()