2023-04-15 21:34:18 +00:00
|
|
|
# Flags shared by Lagom (including Ladybird) and Serenity.
|
2024-04-30 13:19:35 +00:00
|
|
|
set(CMAKE_CXX_STANDARD 23)
|
2022-05-14 13:07:12 +00:00
|
|
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
|
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
|
|
|
|
2024-01-17 20:50:58 +00:00
|
|
|
set(CMAKE_COLOR_DIAGNOSTICS ON)
|
|
|
|
|
2024-07-19 21:54:52 +00:00
|
|
|
macro(add_cxx_compile_options)
|
|
|
|
set(args "")
|
|
|
|
foreach(arg ${ARGN})
|
|
|
|
string(APPEND args ${arg}$<SEMICOLON>)
|
2024-08-04 23:12:34 +00:00
|
|
|
add_compile_options("SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xcc ${arg}>")
|
2024-07-19 21:54:52 +00:00
|
|
|
endforeach()
|
2024-08-04 23:12:34 +00:00
|
|
|
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX,ASM>:${args}>)
|
2024-07-19 21:54:52 +00:00
|
|
|
endmacro()
|
|
|
|
|
|
|
|
macro(add_cxx_link_options)
|
|
|
|
set(args "")
|
|
|
|
foreach(arg ${ARGN})
|
|
|
|
string(APPEND args ${arg}$<SEMICOLON>)
|
|
|
|
endforeach()
|
|
|
|
add_link_options($<$<LINK_LANGUAGE:C,CXX>:${args}>)
|
|
|
|
endmacro()
|
|
|
|
|
2024-07-16 11:33:39 +00:00
|
|
|
macro(add_swift_compile_options)
|
|
|
|
set(args "")
|
|
|
|
foreach(arg ${ARGN})
|
|
|
|
string(APPEND args ${arg}$<SEMICOLON>)
|
|
|
|
endforeach()
|
|
|
|
add_compile_options($<$<COMPILE_LANGUAGE:Swift>:${args}>)
|
|
|
|
endmacro()
|
|
|
|
|
|
|
|
macro(add_swift_link_options)
|
|
|
|
set(args "")
|
|
|
|
foreach(arg ${ARGN})
|
|
|
|
string(APPEND args ${arg}$<SEMICOLON>)
|
|
|
|
endforeach()
|
|
|
|
add_link_options($<$<LINK_LANGUAGE:Swift>:${args}>)
|
|
|
|
endmacro()
|
|
|
|
|
2024-06-06 08:36:16 +00:00
|
|
|
if (MSVC)
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(/W4)
|
2024-06-06 08:36:16 +00:00
|
|
|
# do not warn about unused function
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(/wd4505)
|
2024-06-06 08:36:16 +00:00
|
|
|
# disable exceptions
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(/EHsc)
|
2024-06-06 08:36:16 +00:00
|
|
|
# disable floating-point expression contraction
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(/fp:precise)
|
2024-06-06 08:36:16 +00:00
|
|
|
else()
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(-Wall -Wextra)
|
|
|
|
add_cxx_compile_options(-fno-exceptions)
|
|
|
|
add_cxx_compile_options(-ffp-contract=off)
|
2024-06-06 08:36:16 +00:00
|
|
|
endif()
|
2022-05-14 13:07:12 +00:00
|
|
|
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(-Wcast-qual)
|
|
|
|
add_cxx_compile_options(-Wformat=2)
|
|
|
|
add_cxx_compile_options(-Wimplicit-fallthrough)
|
|
|
|
add_cxx_compile_options(-Wmissing-declarations)
|
|
|
|
add_cxx_compile_options(-Wsuggest-override)
|
2023-11-24 08:24:23 +00:00
|
|
|
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(-Wno-invalid-offsetof)
|
|
|
|
add_cxx_compile_options(-Wno-unknown-warning-option)
|
|
|
|
add_cxx_compile_options(-Wno-unused-command-line-argument)
|
2023-04-15 21:34:18 +00:00
|
|
|
|
Meta: Globally disable floating point contraction
FP contraction is a standard-conforming behavior which allows the
compiler to calculate intermediate results of expressions containing
floating point numbers with a greater precision than the expression type
allows. And in theory, it enables additional optimizations, such as
replacing `a * b + c` with fma(a, b, c).
Unfortunately, it is extremely hard to predict when the contraction will
happen. For example, Clang 17 on x86_64 with the default options will
use FMA only for constant-folded non-constexpr expressions. So, in
practice, FP contraction leads to hard-to-find bugs and inconsistencies
between executables compiled with different toolchains or for different
OSes. And we had two instances of this happening last week.
Since we did not ever used -mfma on x86_64, this patch can only possibly
regress performance on Apple ARM devices, where FMA is enabled by
default. However, this regression will likely be negligible since the
difference would be one additional add instruction, which would be then
likely executed in parallel with something else.
2023-11-16 19:48:32 +00:00
|
|
|
|
2023-11-04 03:59:15 +00:00
|
|
|
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "18")
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(-Wpadded-bitfield)
|
2023-11-04 03:59:15 +00:00
|
|
|
endif()
|
|
|
|
|
2022-05-14 13:07:12 +00:00
|
|
|
if (NOT CMAKE_HOST_SYSTEM_NAME MATCHES SerenityOS)
|
|
|
|
# FIXME: Something makes this go crazy and flag unused variables that aren't flagged as such when building with the toolchain.
|
|
|
|
# Disable -Werror for now.
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(-Werror)
|
2022-05-14 13:07:12 +00:00
|
|
|
endif()
|
2023-04-17 23:50:53 +00:00
|
|
|
|
2024-06-06 08:36:16 +00:00
|
|
|
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT CMAKE_CXX_SIMULATE_ID MATCHES "MSVC")
|
2023-04-17 23:50:53 +00:00
|
|
|
# Clang's default constexpr-steps limit is 1048576(2^20), GCC doesn't have one
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(-fconstexpr-steps=16777216)
|
2023-04-17 23:50:53 +00:00
|
|
|
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(-Wmissing-prototypes)
|
2024-07-14 16:29:33 +00:00
|
|
|
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(-Wno-implicit-const-int-float-conversion)
|
|
|
|
add_cxx_compile_options(-Wno-user-defined-literals)
|
|
|
|
add_cxx_compile_options(-Wno-vla-cxx-extension)
|
2024-08-04 23:10:27 +00:00
|
|
|
add_cxx_compile_options(-Wno-unqualified-std-cast-call)
|
2023-04-17 23:50:53 +00:00
|
|
|
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
|
|
|
|
# Only ignore expansion-to-defined for g++, clang's implementation doesn't complain about function-like macros
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(-Wno-expansion-to-defined)
|
|
|
|
add_cxx_compile_options(-Wno-literal-suffix)
|
2023-05-01 14:59:46 +00:00
|
|
|
|
|
|
|
# FIXME: This warning seems useful but has too many false positives with GCC 13.
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(-Wno-dangling-reference)
|
2024-06-06 08:36:16 +00:00
|
|
|
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang$" AND CMAKE_CXX_SIMULATE_ID MATCHES "MSVC")
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(-Wno-reserved-identifier)
|
|
|
|
add_cxx_compile_options(-Wno-user-defined-literals)
|
2024-08-04 23:10:27 +00:00
|
|
|
add_cxx_compile_options(-Wno-unqualified-std-cast-call)
|
2024-06-06 08:36:16 +00:00
|
|
|
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
|
|
|
|
|
|
|
|
# TODO: this seems wrong, but we use this kind of code too much
|
2024-07-19 21:54:52 +00:00
|
|
|
# add_cxx_compile_options(-Wno-unsafe-buffer-usage)
|
2023-04-17 23:50:53 +00:00
|
|
|
endif()
|
CMake: Set `-fvisibility-inlines-hidden` on ELF platforms
The C++ semantics of `inline` dictate that such functions might be
defined in multiple translation units. As with all other functions, they
must have the same address in all TUs. Because of this, the compiler
emits `inline` functions as weak symbols, which the dynamic linker can
then resolve to the same address everywhere.
This rule is responsible for a significant overhead at startup, as such
lookups happen by name. Namely, 86'000 of the 114'000 by-name symbol
lookups when loading LibWeb can be attributed to this. Most of these
are only ever defined in a single object, making this even more
pointless.
As nothing in our code relies on either ELF symbol interposition rules
or function address equality, we can use the -fvisibility-inlines-hidden
escape hatch, which causes this rule to be disregarded. As the symbols
are now hidden, no load-time symbol lookup is needed. This flag is used
without issues in other large C++ codebases like Chromium and LLVM.
Some relevant light reading, suggested by Nico:
- https://ridiculousfish.com/blog/posts/i-didnt-order-that-so-why-is-it-on-my-bill-episode-1.html
- https://www.cs.dartmouth.edu/~sergey/cs258/ABI/UlrichDrepper-How-To-Write-Shared-Libraries.pdf
- https://blog.llvm.org/2018/11/30-faster-windows-builds-with-clang-cl_14.html
- https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html#index-fvisibility-inlines-hidden
2023-08-11 22:40:34 +00:00
|
|
|
|
|
|
|
if (UNIX AND NOT APPLE AND NOT ENABLE_FUZZERS)
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(-fno-semantic-interposition)
|
|
|
|
add_cxx_compile_options(-fvisibility-inlines-hidden)
|
CMake: Set `-fvisibility-inlines-hidden` on ELF platforms
The C++ semantics of `inline` dictate that such functions might be
defined in multiple translation units. As with all other functions, they
must have the same address in all TUs. Because of this, the compiler
emits `inline` functions as weak symbols, which the dynamic linker can
then resolve to the same address everywhere.
This rule is responsible for a significant overhead at startup, as such
lookups happen by name. Namely, 86'000 of the 114'000 by-name symbol
lookups when loading LibWeb can be attributed to this. Most of these
are only ever defined in a single object, making this even more
pointless.
As nothing in our code relies on either ELF symbol interposition rules
or function address equality, we can use the -fvisibility-inlines-hidden
escape hatch, which causes this rule to be disregarded. As the symbols
are now hidden, no load-time symbol lookup is needed. This flag is used
without issues in other large C++ codebases like Chromium and LLVM.
Some relevant light reading, suggested by Nico:
- https://ridiculousfish.com/blog/posts/i-didnt-order-that-so-why-is-it-on-my-bill-episode-1.html
- https://www.cs.dartmouth.edu/~sergey/cs258/ABI/UlrichDrepper-How-To-Write-Shared-Libraries.pdf
- https://blog.llvm.org/2018/11/30-faster-windows-builds-with-clang-cl_14.html
- https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html#index-fvisibility-inlines-hidden
2023-08-11 22:40:34 +00:00
|
|
|
endif()
|
2024-07-14 17:06:09 +00:00
|
|
|
|
|
|
|
if (NOT WIN32)
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(-fstack-protector-strong)
|
|
|
|
add_cxx_link_options(-fstack-protector-strong)
|
2024-07-14 17:06:09 +00:00
|
|
|
endif()
|
|
|
|
|
2024-07-19 21:54:52 +00:00
|
|
|
add_cxx_compile_options(-fstrict-flex-arrays=2)
|