@@ -8,13 +8,14 @@ set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
88set (CMAKE_EXPORT_COMPILE_COMMANDS ON )
99
1010include (GNUInstallDirs )
11+ include (CheckIPOSupported )
1112set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR} /lib" )
1213set (CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR} /lib" )
1314set (CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR} /bin" )
1415
15- option (CLORE_ENABLE_LTO "Enable ThinLTO for all targets" OFF )
16+ option (CLORE_ENABLE_LTO "Enable ThinLTO for all targets" ON )
1617option (CLORE_OFFLINE_BUILD "Disable network downloads during configuration" OFF )
17- option (CLORE_ENABLE_TEST "Build unit tests" OFF )
18+ option (CLORE_ENABLE_TEST "Build unit tests" ON )
1819
1920# Global flags that apply to all targets (including FetchContent dependencies).
2021if (NOT MSVC )
@@ -39,47 +40,31 @@ else()
3940 string (APPEND CMAKE_SHARED_LINKER_FLAGS " -Wl,--gc-sections" )
4041endif ()
4142
43+ set (CLORE_ENABLE_IPO OFF )
44+ set (CLORE_USE_LTO_ARTIFACT OFF )
4245if (CLORE_ENABLE_LTO)
43- string (APPEND CMAKE_C_FLAGS " -flto=thin" )
44- string (APPEND CMAKE_CXX_FLAGS " -flto=thin" )
45- string (APPEND CMAKE_EXE_LINKER_FLAGS " -flto=thin" )
46- string (APPEND CMAKE_SHARED_LINKER_FLAGS " -flto=thin" )
47- string (APPEND CMAKE_MODULE_LINKER_FLAGS " -flto=thin" )
46+ if (CMAKE_BUILD_TYPE STREQUAL "Debug" )
47+ message (STATUS "Interprocedural optimization disabled for Debug builds." )
48+ else ()
49+ check_ipo_supported (RESULT CLORE_IPO_SUPPORTED OUTPUT CLORE_IPO_ERROR LANGUAGES C CXX )
50+ if (CLORE_IPO_SUPPORTED)
51+ set (CLORE_ENABLE_IPO ON )
52+ set (CLORE_USE_LTO_ARTIFACT ON )
53+ message (STATUS "Interprocedural optimization enabled for ${CMAKE_BUILD_TYPE} builds." )
54+ else ()
55+ message (WARNING "LTO requested but IPO is not supported by the active toolchain: ${CLORE_IPO_ERROR} " )
56+ endif ()
57+ endif ()
4858endif ()
4959
5060if (CMAKE_BUILD_TYPE STREQUAL "Debug" )
51- add_compile_options (-fsanitize=address )
52-
53- if (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC" )
54- # clang-cl (MSVC frontend): manually link ASan runtime since clang-cl
55- # doesn't handle -fsanitize=address linking automatically.
56- execute_process (
57- COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir
58- OUTPUT_VARIABLE CLANG_RESOURCE_DIR
59- OUTPUT_STRIP_TRAILING_WHITESPACE
60- )
61- set (ASAN_LIB_PATH "${CLANG_RESOURCE_DIR} /lib/windows" )
62- link_directories (${ASAN_LIB_PATH} )
63-
64- set (ASAN_LINK_FLAGS "" )
65- list (APPEND ASAN_LINK_FLAGS "clang_rt.asan_dynamic-x86_64.lib" )
66- list (APPEND ASAN_LINK_FLAGS "/wholearchive:clang_rt.asan_dynamic_runtime_thunk-x86_64.lib" )
67-
68- foreach (flag ${ASAN_LINK_FLAGS} )
69- string (APPEND CMAKE_EXE_LINKER_FLAGS " ${flag} " )
70- string (APPEND CMAKE_SHARED_LINKER_FLAGS " ${flag} " )
71- string (APPEND CMAKE_MODULE_LINKER_FLAGS " ${flag} " )
72- endforeach ()
61+ if (WIN32 )
62+ message (STATUS "AddressSanitizer disabled on Windows Debug builds: the current MSVC-targeting clang toolchain and prebuilt LLVM static libraries are not ASan-safe in this repo." )
7363 else ()
64+ add_compile_options (-fsanitize=address )
7465 string (APPEND CMAKE_EXE_LINKER_FLAGS " -fsanitize=address" )
7566 string (APPEND CMAKE_SHARED_LINKER_FLAGS " -fsanitize=address" )
7667 endif ()
77-
78- if (WIN32 )
79- # Disable Identical COMDAT Folding in Debug to avoid ASan ODR false positives.
80- string (APPEND CMAKE_EXE_LINKER_FLAGS " -Wl,/OPT:NOICF" )
81- string (APPEND CMAKE_SHARED_LINKER_FLAGS " -Wl,/OPT:NOICF" )
82- endif ()
8368endif ()
8469
8570include ("${PROJECT_SOURCE_DIR} /cmake/package.cmake" )
0 commit comments