Bring in a bunch of cmake settings from conflict-set

This commit is contained in:
2025-05-23 12:43:25 -04:00
parent 1217ded8a7
commit 4240894318
4 changed files with 79 additions and 13 deletions

View File

@@ -7,6 +7,11 @@ project(
LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 20)
include(CMakePushCheckState)
include(CheckCXXCompilerFlag)
include(CheckIncludeFileCXX)
include(CheckCXXSourceCompiles)
set(DEFAULT_BUILD_TYPE "Release")
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
@@ -28,7 +33,55 @@ add_compile_options(
-fPIC
-fdata-sections
-ffunction-sections
-fno-omit-frame-pointer)
-fno-omit-frame-pointer
-g)
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_link_options("-Wno-unused-command-line-argument")
find_program(LLVM_OBJCOPY llvm-objcopy)
if(LLVM_OBJCOPY)
set(CMAKE_OBJCOPY
${LLVM_OBJCOPY}
CACHE FILEPATH "path to objcopy binary" FORCE)
endif()
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options("-Wno-maybe-uninitialized")
endif()
set(full_relro_flags "-pie;LINKER:-z,relro,-z,now,-z,noexecstack")
cmake_push_check_state()
list(APPEND CMAKE_REQUIRED_LINK_OPTIONS ${full_relro_flags})
check_cxx_source_compiles("int main(){}" HAS_FULL_RELRO FAIL_REGEX "warning:")
if(HAS_FULL_RELRO)
add_link_options(${full_relro_flags})
endif()
cmake_pop_check_state()
if(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64 OR CMAKE_SYSTEM_PROCESSOR STREQUAL
arm64)
add_compile_options(-mbranch-protection=standard)
else()
add_compile_options(-fcf-protection)
set(rewrite_endbr_flags "-fuse-ld=mold;LINKER:-z,rewrite-endbr")
cmake_push_check_state()
list(APPEND CMAKE_REQUIRED_LINK_OPTIONS ${rewrite_endbr_flags})
check_cxx_source_compiles("int main(){}" HAS_REWRITE_ENDBR FAIL_REGEX
"warning:")
if(HAS_REWRITE_ENDBR)
add_link_options(${rewrite_endbr_flags})
endif()
cmake_pop_check_state()
endif()
set(version_script_flags
LINKER:--version-script=${CMAKE_CURRENT_SOURCE_DIR}/linker.map)
cmake_push_check_state()
list(APPEND CMAKE_REQUIRED_LINK_OPTIONS ${version_script_flags})
check_cxx_source_compiles("int main(){}" HAS_VERSION_SCRIPT FAIL_REGEX
"warning:")
cmake_pop_check_state()
if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64)
add_compile_options(-mavx)
@@ -37,6 +90,16 @@ if(CMAKE_SYSTEM_NAME STREQUAL Linux)
add_link_options(LINKER:--gc-sections)
endif()
# This is encouraged according to
# https://valgrind.org/docs/manual/manual-core-adv.html#manual-core-adv.clientreq
include_directories(SYSTEM ${CMAKE_CURRENT_SOURCE_DIR}/third_party/valgrind)
if(HAS_VERSION_SCRIPT)
target_link_options(
${PROJECT_NAME} PRIVATE
LINKER:--version-script=${CMAKE_CURRENT_SOURCE_DIR}/linker.map)
endif()
add_subdirectory(third_party)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
@@ -93,9 +156,12 @@ if(NOT APPLE)
"Proceeding with all symbols global in static library")
endif()
set(TEST_FLAGS -Wall -Wextra -Wunreachable-code -Wpedantic -UNDEBUG)
add_executable(mytest src/test.cpp)
target_include_directories(mytest PRIVATE include)
target_link_libraries(mytest PRIVATE ${PROJECT_NAME} doctest nanobench simdjson)
target_compile_options(mytest PRIVATE ${TEST_FLAGS})
doctest_discover_tests(mytest)
include(CMakePushCheckState)
@@ -109,7 +175,7 @@ if(HAS_LIB_FUZZER)
add_executable(fuzz src/fuzz.cpp src/lib.cpp)
target_include_directories(fuzz PRIVATE include)
target_link_libraries(fuzz PRIVATE simdjson)
target_compile_options(fuzz PRIVATE -fsanitize=fuzzer)
target_compile_options(fuzz PRIVATE -fsanitize=fuzzer ${TEST_FLAGS})
target_link_options(fuzz PRIVATE -fsanitize=fuzzer)
endif()