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

function(crucible_configure_address_sanitizer)
  if(NOT CRUCIBLE_ENABLE_ADDRESS_SANITIZER)
    message(STATUS "Skipping 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_directories)
  message(STATUS "Configuring output directories...")

  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_TARGET_SYSTEM_ARCHITECTURE=\"${CMAKE_SYSTEM_PROCESSOR}\"")
  list(APPEND DEFINITIONS "CRUCIBLE_TARGET_SYSTEM_ID=\"${CMAKE_SYSTEM_NAME}\"")
  list(APPEND DEFINITIONS "CRUCIBLE_TARGET_SYSTEM_VERSION=\"${CMAKE_SYSTEM_VERSION}\"")
  list(APPEND DEFINITIONS "CRUCIBLE_VERSION=\"${PROJECT_VERSION}\"")

  file(TO_NATIVE_PATH "${PROJECT_SOURCE_DIR}" NATIVE_ROOT_PATH)
  string(REPLACE "\\" "\\\\" NATIVE_ROOT_PATH_ESCAPED "${NATIVE_ROOT_PATH}")
  list(APPEND DEFINITIONS "CRUCIBLE_ROOT=\"${NATIVE_ROOT_PATH_ESCAPED}\"")

  if(DEFINITIONS)
    message(STATUS "  Adding preprocessor definitions: ${DEFINITIONS}")
    add_compile_definitions(${DEFINITIONS})
  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 "Skipping 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 "Skipping 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 "Skipping warning level")
    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")

    # GCC freaks out whenever we have a format() method that happens to never get called
    list(APPEND OPTIONS "-Wno-unused-function")
  elseif(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()