swsf-bruteforce/cmake/ProjectFuncs.cmake

78 lines
3.0 KiB
CMake
Raw Permalink Normal View History

2023-08-22 03:44:45 -04:00
function(project_target target)
target_compile_definitions(${target} PRIVATE "$<$<CONFIG:DEBUG>:DEBUG>")
2023-08-21 20:50:31 -04:00
# public headers reside with a component (if they exist)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(${target} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
endif()
target_include_directories(${target} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_features(${target} PUBLIC cxx_std_20)
# some sane compiler flags
2023-08-21 20:58:16 -04:00
if(NOT WIN32)
set(_CORE_COMPILE_ARGS -Wall -Wextra)
set(_CORE_LINKER_ARGS "")
2023-08-21 20:50:31 -04:00
2023-08-21 20:58:16 -04:00
if("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
set(_CORE_COMPILE_ARGS ${_CORE_COMPILE_ARGS} -Werror)
2023-08-21 20:50:31 -04:00
2023-08-21 20:58:16 -04:00
# If on Release use link-time optimizations.
# On clang we use ThinLTO for even better build performance.
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(_CORE_COMPILE_ARGS ${_CORE_COMPILE_ARGS} -flto=thin)
set(_CORE_LINKER_ARGS ${_CORE_LINKER_ARGS} -flto=thin)
2023-08-22 03:44:45 -04:00
target_link_options(${target} PRIVATE -fuse-ld=${PROJECT_LINKER} -flto=thin)
2023-08-21 20:58:16 -04:00
else("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(_CORE_COMPILE_ARGS ${_CORE_COMPILE_ARGS} -flto)
set(_CORE_LINKER_ARGS ${_CORE_LINKER_ARGS} -flto)
2023-08-22 03:44:45 -04:00
target_link_options(${target} PRIVATE -fuse-ld=${PROJECT_LINKER} -flto)
2023-08-21 20:58:16 -04:00
endif()
2023-08-21 20:50:31 -04:00
2023-08-21 20:58:16 -04:00
endif()
2023-08-21 20:50:31 -04:00
2023-08-21 20:58:16 -04:00
if("asan" IN_LIST SSXTOOLS_BUILD_FEATURES)
# Error if someone's trying to mix asan and tsan together,
# they aren't compatible.
if("tsan" IN_LIST SSXTOOLS_BUILD_FEATURES)
message(FATAL_ERROR "ASAN and TSAN cannot be used together.")
endif()
2023-08-21 20:50:31 -04:00
2023-08-21 20:58:16 -04:00
message(STATUS "Enabling ASAN for target ${target}")
set(_CORE_COMPILE_ARGS ${_CORE_COMPILE_ARGS} -fsanitize=address)
set(_CORE_LINKER_ARGS ${_CORE_LINKER_ARGS} -fsanitize=address)
endif()
2023-08-21 20:50:31 -04:00
2023-08-21 20:58:16 -04:00
if("tsan" IN_LIST SSXTOOLS_BUILD_FEATURES)
message(STATUS "Enabling TSAN for target ${target}")
set(_CORE_COMPILE_ARGS ${_CORE_COMPILE_ARGS} -fsanitize=thread)
set(_CORE_LINKER_ARGS ${_CORE_LINKER_ARGS} -fsanitize=thread)
endif()
2023-08-21 20:50:31 -04:00
2023-08-21 20:58:16 -04:00
if("ubsan" IN_LIST SSXTOOLS_BUILD_FEATURES)
message(STATUS "Enabling UBSAN for target ${target}")
set(_CORE_COMPILE_ARGS ${_CORE_COMPILE_ARGS} -fsanitize=undefined)
set(_CORE_LINKER_ARGS ${_CORE_LINKER_ARGS} -fsanitize=undefined)
endif()
2023-08-21 20:50:31 -04:00
2023-08-21 20:58:16 -04:00
target_compile_options(${target} PRIVATE ${_CORE_COMPILE_ARGS})
target_link_options(${target} PRIVATE ${_CORE_LINKER_ARGS})
endif()
2023-08-21 20:50:31 -04:00
endfunction()
2023-08-22 03:44:45 -04:00
function(project_header_only_target target)
2023-08-21 20:50:31 -04:00
target_include_directories(${target} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_compile_features(${target} INTERFACE cxx_std_20)
endfunction()
2023-08-22 03:44:45 -04:00
function(project_set_alternate_linker)
2023-08-21 20:58:16 -04:00
if(NOT WIN32)
2023-08-22 03:44:45 -04:00
find_program(LINKER_EXECUTABLE ld.${PROJECT_LINKER} ${PROJECT_LINKER})
2023-08-21 20:58:16 -04:00
if(LINKER_EXECUTABLE)
2023-08-22 03:44:45 -04:00
message(STATUS "Using ${SSXTOOLS_LINKER} as linker")
2023-08-21 20:58:16 -04:00
else()
message(FATAL_ERROR "Linker ${SSXTOOLS_LINKER} does not exist on your system. Please specify one which does or omit this option from your configure command.")
endif()
endif()
2023-08-21 20:50:31 -04:00
endfunction()