2023-11-19 20:21:56 -05:00
# Copyright 2023 The LightningBolt Authors
# SPDX-License-Identifier: MIT
2023-11-19 06:19:15 -05:00
function ( lb_target target )
target_compile_definitions ( ${ target } PRIVATE "$<$<CONFIG:DEBUG>:LIGHTNINGBOLT_DEBUG>" )
#target_include_directories(${target} PRIVATE ${PROJECT_SOURCE_DIR})
target_compile_features ( ${ target } PRIVATE cxx_std_20 )
target_include_directories ( ${ target } PRIVATE ${ PROJECT_SOURCE_DIR } /lib ${ CMAKE_CURRENT_BINARY_DIR } )
set ( _LIGHTNINGBOLT_CORE_COMPILE_ARGS -Wall -Wextra )
if ( "${CMAKE_BUILD_TYPE}" STREQUAL "Release" OR "${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo" )
set ( _LIGHTNINGBOLT_CORE_COMPILE_ARGS ${ _LIGHTNINGBOLT_CORE_COMPILE_ARGS } )
# 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 ( _LIGHTNINGBOLT_CORE_COMPILE_ARGS ${ _LIGHTNINGBOLT_CORE_COMPILE_ARGS } -flto=thin )
target_link_options ( ${ target } PRIVATE -fuse-ld= ${ LIGHTNINGBOLT_LINKER } -flto=thin )
else ( )
set ( _LIGHTNINGBOLT_CORE_COMPILE_ARGS ${ _LIGHTNINGBOLT_CORE_COMPILE_ARGS } -flto )
target_link_options ( ${ target } PRIVATE -fuse-ld= ${ LIGHTNINGBOLT_LINKER } -flto )
endif ( )
target_compile_options ( ${ target } PRIVATE ${ _LIGHTNINGBOLT_CORE_COMPILE_ARGS } )
endif ( )
if ( "asan" IN_LIST LIGHTNINGBOLT_BUILD_FEATURES )
# Error if someone's trying to mix asan and tsan together,
# they aren't compatible.
if ( "tsan" IN_LIST LIGHTNINGBOLT_BUILD_FEATURES )
message ( FATAL_ERROR "ASAN and TSAN cannot be used together." )
endif ( )
message ( STATUS "Enabling ASAN for target ${target} because it was in LIGHTNINGBOLT_BUILD_FEATURES" )
target_compile_options ( ${ target } PRIVATE ${ _LIGHTNINGBOLT_CORE_COMPILE_ARGS } -fsanitize=address )
target_link_libraries ( ${ target } PRIVATE -fsanitize=address )
endif ( )
if ( "tsan" IN_LIST LIGHTNINGBOLT_BUILD_FEATURES )
message ( STATUS "Enabling TSAN for target ${target} because it was in LIGHTNINGBOLT_BUILD_FEATURES" )
target_compile_options ( ${ target } PRIVATE ${ _LIGHTNINGBOLT_CORE_COMPILE_ARGS } -fsanitize=thread )
target_link_libraries ( ${ target } PRIVATE -fsanitize=thread )
endif ( )
if ( "ubsan" IN_LIST LIGHTNINGBOLT_BUILD_FEATURES )
message ( STATUS "Enabling UBSAN for target ${target} because it was in LIGHTNINGBOLT_BUILD_FEATURES" )
target_compile_options ( ${ target } PRIVATE ${ _LIGHTNINGBOLT_CORE_COMPILE_ARGS } -fsanitize=undefined )
target_link_libraries ( ${ target } PRIVATE -fsanitize=undefined )
endif ( )
endfunction ( )
function ( lb_set_alternate_linker )
find_program ( LINKER_EXECUTABLE ld. ${ LIGHTNINGBOLT_LINKER } ${ COLLABVM_LINKER } )
if ( LINKER_EXECUTABLE )
message ( STATUS "Using ${LIGHTNINGBOLT_LINKER} as linker" )
else ( )
message ( FATAL_ERROR "Linker ${LIGHTNINGBOLT_LINKER} does not exist on your system. Please specify one which does or omit this option from your configure command." )
endif ( )
endfunction ( )