[CMake] Explicitly disable LTO if no LTO option is provided (#85621)

When compiling Swift as an LLVM external project (not the non-unified
workflow from the build-script), if one tries to enable LTO for LLVM
projects it will end up affecting Swift and the Swift standard library,
even if the options `SWIFT_TOOLS_ENABLE_LTO` and
`SWIFT_STDLIB_ENABLE_LTO` are disabled or using their default values,
because [HandleLLVMOptions] modifies the `CMAKE_C/CXX_FLAGS` globally.

By setting `-fno-lto` explicitly when those options are disabled or
using their default values, the options from `CMAKE_C/CXX_FLAGS` are
overriden, and the `SWIFT_*_ENABLE_LTO` controls the usage or not of LTO
optimizations and the presence of bitcode in the object files.

[HandleLLVMOptions]:
b58b2a34d5/llvm/cmake/modules/HandleLLVMOptions.cmake (L1278-L1310)
This commit is contained in:
Daniel Rodríguez Troitiño
2025-12-01 09:40:59 -08:00
committed by GitHub
parent 24c69c674d
commit 51d2f2b61d

View File

@@ -58,6 +58,8 @@ function(_compute_lto_flag option out_var)
set(${out_var} "-flto=full" PARENT_SCOPE)
elseif (lowercase_option STREQUAL "thin")
set(${out_var} "-flto=thin" PARENT_SCOPE)
else()
set(${out_var} "-fno-lto" PARENT_SCOPE)
endif()
endfunction()