Various fixes for CMake (#13928)

This commit is contained in:
Ashcon Partovi
2024-09-12 18:08:59 -07:00
committed by GitHub
parent b146449ed5
commit 76191bed44
34 changed files with 598 additions and 423 deletions

View File

@@ -1,57 +1,97 @@
include(Macros)
# clang: https://clang.llvm.org/docs/CommandGuide/clang.html
# clang-cl: https://clang.llvm.org/docs/UsersManual.html#id11
# --- Macros ---
macro(setb variable)
if(${variable})
set(${variable} ON)
else()
set(${variable} OFF)
endif()
endmacro()
set(targets WIN32 APPLE UNIX LINUX)
foreach(target ${targets})
setb(${target})
endforeach()
# --- CPU target ---
if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm|ARM|arm64|ARM64|aarch64|AARCH64")
if(APPLE)
register_compiler_flags(-mcpu=apple-m1)
else()
register_compiler_flags(-march=armv8-a+crc -mtune=ampere1)
endif()
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|X86_64|x64|X64|amd64|AMD64")
if(ENABLE_BASELINE)
register_compiler_flags(-march=nehalem)
else()
register_compiler_flags(-march=haswell)
endif()
else()
unsupported(CMAKE_SYSTEM_PROCESSOR)
endif()
# --- MSVC runtime ---
if(WIN32)
if(DEBUG)
add_compile_options(/MTd) # Use static debug run-time
else()
add_compile_options(/MT) # Use static run-time
endif()
register_compiler_flags(
DESCRIPTION "Use static MSVC runtime"
/MTd ${DEBUG}
/MT ${RELEASE}
/U_DLL
)
endif()
# --- Optimization level ---
if(DEBUG)
if(WIN32)
add_compile_options(/O0)
else()
add_compile_options(-O0)
endif()
register_compiler_flags(
DESCRIPTION "Disable optimization"
/O0 ${WIN32}
-O0 ${UNIX}
)
elseif(ENABLE_SMOL)
if(WIN32)
add_compile_options(/Os)
else()
add_compile_options(-Os)
endif()
register_compiler_flags(
DESCRIPTION "Optimize for size"
/Os ${WIN32}
-Os ${UNIX}
)
else()
if(WIN32)
# TODO: change to /0t (same as -O3) to match macOS and Linux?
add_compile_options(/O2)
else()
add_compile_options(-O3)
endif()
register_compiler_flags(
DESCRIPTION "Optimize for speed"
/O2 ${WIN32} # TODO: change to /0t (same as -O3) to match macOS and Linux?
-O3 ${UNIX}
)
endif()
# --- Debug symbols ---
# --- Debug level ---
if(WIN32)
add_compile_options(
/Z7 # Produce a .pdb file
register_compiler_flags(
DESCRIPTION "Enable debug symbols (.pdb)"
/Z7
)
else()
add_compile_options(
-ggdb # Produce a format that is compatable with GDB
-gdwarf-4 # Produce DWARF v4 debug info
elseif(APPLE)
register_compiler_flags(
DESCRIPTION "Enable debug symbols (.dSYM)"
-gdwarf-4
)
endif()
if(UNIX)
register_compiler_flags(
DESCRIPTION "Enable debug symbols"
-g3 ${DEBUG}
-g1 ${RELEASE}
)
register_compiler_flags(
DESCRIPTION "Optimize debug symbols for LLDB"
-glldb
)
if(DEBUG)
add_compile_options(-g3)
else()
add_compile_options(-g1)
endif()
endif()
# TODO: consider other debug options
@@ -59,44 +99,184 @@ endif()
# -fstandalone-debug # Emit debug info for non-system libraries
# -fno-eliminate-unused-debug-types # Don't eliminate unused debug symbols
# --- RTTI ---
# --- C/C++ flags ---
if(WIN32)
add_compile_options(/GR-)
else()
add_compile_options(-fno-rtti)
register_compiler_flags(
DESCRIPTION "Disable C/C++ exceptions"
-fno-exceptions ${UNIX}
/EHsc ${WIN32} # (s- disables C++, c- disables C)
)
register_compiler_flags(
DESCRIPTION "Disable C++ static destructors"
LANGUAGES CXX
-Xclang ${WIN32}
-fno-c++-static-destructors
)
register_compiler_flags(
DESCRIPTION "Disable runtime type information (RTTI)"
/GR- ${WIN32}
-fno-rtti ${UNIX}
)
register_compiler_flags(
DESCRIPTION "Keep frame pointers"
/Oy- ${WIN32}
-fno-omit-frame-pointer ${UNIX}
-mno-omit-leaf-frame-pointer ${UNIX}
)
if(UNIX)
register_compiler_flags(
DESCRIPTION "Set C/C++ visibility to hidden"
-fvisibility=hidden
-fvisibility-inlines-hidden
)
register_compiler_flags(
DESCRIPTION "Disable unwind tables"
-fno-unwind-tables
-fno-asynchronous-unwind-tables
)
endif()
# --- CPU target (-march, -mtune, -mcpu) ---
register_compiler_flags(
DESCRIPTION "Place each function in its own section"
-ffunction-sections ${UNIX}
/Gy ${WIN32}
)
# Using -march=native can break older systems, instead use a specific CPU
if(CPU STREQUAL "native")
if(ARCH STREQUAL "aarch64")
if(APPLE)
add_compile_options(-mcpu=apple-m1)
else()
add_compile_options(-march=armv8-a+crc -mtune=ampere1)
endif()
register_compiler_flags(
DESCRIPTION "Place each data item in its own section"
-fdata-sections ${UNIX}
/Gw ${WIN32}
)
if(UNIX)
register_compiler_flags(
DESCRIPTION "Emit an address-significance table"
-faddrsig
)
endif()
if(WIN32)
register_compiler_flags(
DESCRIPTION "Enable string pooling"
/GF
)
register_compiler_flags(
DESCRIPTION "Assume thread-local variables are defined in the executable"
/GA
)
endif()
# --- Linker flags ---
if(LINUX)
register_linker_flags(
DESCRIPTION "Disable relocation read-only (RELRO)"
-Wl,-z,norelro
)
endif()
# --- Assertions ---
# Note: This is a helpful guide about assertions:
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++
if(ENABLE_ASSERTIONS)
register_compiler_flags(
DESCRIPTION "Do not eliminate null-pointer checks"
-fno-delete-null-pointer-checks
)
register_compiler_definitions(
DESCRIPTION "Enable libc++ assertions"
_LIBCPP_ENABLE_ASSERTIONS=1
_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE ${RELEASE}
_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG ${DEBUG}
)
register_compiler_definitions(
DESCRIPTION "Enable fortified sources"
_FORTIFY_SOURCE=3
)
if(LINUX)
register_compiler_definitions(
DESCRIPTION "Enable glibc++ assertions"
_GLIBCXX_ASSERTIONS=1
)
endif()
elseif(CPU)
add_compile_options(-march=${CPU} -mtune=${CPU})
else()
message(FATAL_ERROR "No CPU specified, please set -DCPU=<string>")
register_compiler_definitions(
DESCRIPTION "Disable debug assertions"
NDEBUG=1
)
register_compiler_definitions(
DESCRIPTION "Disable libc++ assertions"
_LIBCPP_ENABLE_ASSERTIONS=0
_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_NONE
)
if(LINUX)
register_compiler_definitions(
DESCRIPTION "Disable glibc++ assertions"
_GLIBCXX_ASSERTIONS=0
)
endif()
endif()
# --- Diagnostics ---
if(NOT WIN32)
add_compile_options(-fdiagnostics-color=always)
if(UNIX)
register_compiler_flags(
DESCRIPTION "Enable color diagnostics"
-fdiagnostics-color=always
)
endif()
add_compile_options(-ferror-limit=${ERROR_LIMIT})
register_compiler_flags(
DESCRIPTION "Set C/C++ error limit"
-ferror-limit=${ERROR_LIMIT}
)
# --- LTO ---
if(ENABLE_LTO)
register_compiler_flags(
DESCRIPTION "Enable link-time optimization (LTO)"
-flto=full ${UNIX}
-flto ${WIN32}
)
if(UNIX)
register_compiler_flags(
DESCRIPTION "Enable virtual tables"
LANGUAGES CXX
-fforce-emit-vtables
-fwhole-program-vtables
)
register_linker_flags(
DESCRIPTION "Enable link-time optimization (LTO)"
-flto=full
-fwhole-program-vtables
-fforce-emit-vtables
)
endif()
endif()
# --- Remapping ---
if(NOT WIN32)
add_compile_options(
if(UNIX)
register_compiler_flags(
DESCRIPTION "Remap source files"
-ffile-prefix-map=${CWD}=.
-ffile-prefix-map=${VENDOR_PATH}=vendor
-ffile-prefix-map=${BUILD_PATH}=build
-ffile-prefix-map=${CACHE_PATH}=cache
)
@@ -107,7 +287,7 @@ endif()
# Valgrind cannot handle SSE4.2 instructions
# This is needed for picohttpparser
if(ENABLE_VALGRIND AND ARCH STREQUAL "x64")
add_compile_definitions("__SSE4_2__=0")
register_compiler_definitions(__SSE4_2__=0)
endif()
# --- Other ---
@@ -118,28 +298,9 @@ if(WIN32 AND NOT CMAKE_CL_SHOWINCLUDES_PREFIX)
set(CMAKE_CL_SHOWINCLUDES_PREFIX "Note: including file:")
endif()
if(ENABLE_ASSERTIONS)
if(APPLE)
# add_compile_definitions("_LIBCXX_ENABLE_ASSERTIONS=1")
# add_compile_definitions("_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
add_compile_definitions("_GLIBCXX_ASSERTIONS=1")
endif()
add_compile_definitions("ASSERT_ENABLED=1")
else()
if(APPLE)
# add_compile_definitions("_LIBCXX_ENABLE_ASSERTIONS=0")
# add_compile_definitions("_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_NONE")
endif()
add_compile_definitions("NDEBUG=1")
endif()
# WebKit uses -std=gnu++20 on non-macOS non-Windows.
# If we do not set this, it will crash at startup on the first memory allocation.
if(NOT WIN32 AND NOT APPLE)
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_POSITION_INDEPENDENT_CODE OFF)
endif()