Skip to content

Commit e90ec58

Browse files
authored
[CMake] Support per-target linker flags (#68393)
`CMAKE_{C/CXX}_FLAGS` affects all targets in LLVM. This can be undesirable in situations, like the case of enabling thinLTO, where `-flto` is added to every source file. In reality, we only care about optimizing a select few of binaries, such as clang or lld, that dominate the compilation pipeline. Auxiliary binaries in a distribution and not on the critical path can be kept non-optimized. This PR adds support of per-target linker flags, which can solve the thinLTO problem by negating the effects of LTO via targeted linker flags on the targets. The example of negating thinLTO above can be done by doing the following: ``` set(LLVM_llvm-dwarfdump_LINKER_FLAGS "-Wl,--lto-O0" CACHE STRING "Custom linker flags to llvm-dwarfdump") set(LLVM_lldb_LINKER_FLAGS "-Wl,--lto-O0" CACHE STRING "Custom linker flags to lldb") ``` There's other applications where this could be used (e.g. avoid optimizing host tools for build speed improvement etc.). I've generalized this so that users can apply their desired flags to targets that are generated by `llvm_add_library` or `add_llvm_executable`. Internally, our toolchain builds were on average 1.4x faster when selectively choosing the binaries that we want optimized.
1 parent fc5d815 commit e90ec58

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

llvm/cmake/modules/AddLLVM.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,8 @@ function(llvm_add_library name)
726726
endforeach()
727727
endif()
728728

729+
add_custom_linker_flags(${name})
730+
729731
if(ARG_SHARED OR ARG_MODULE)
730732
llvm_externalize_debuginfo(${name})
731733
llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
@@ -1019,6 +1021,8 @@ macro(add_llvm_executable name)
10191021
endforeach()
10201022
endif( LLVM_COMMON_DEPENDS )
10211023

1024+
add_custom_linker_flags(${name})
1025+
10221026
if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
10231027
llvm_externalize_debuginfo(${name})
10241028
endif()
@@ -1524,6 +1528,13 @@ macro(add_llvm_tool_subdirectory name)
15241528
add_llvm_external_project(${name})
15251529
endmacro(add_llvm_tool_subdirectory)
15261530
1531+
macro(add_custom_linker_flags name)
1532+
if (LLVM_${name}_LINKER_FLAGS)
1533+
message(STATUS "Applying ${LLVM_${name}_LINKER_FLAGS} to ${name}")
1534+
target_link_options(${name} PRIVATE ${LLVM_${name}_LINKER_FLAGS})
1535+
endif()
1536+
endmacro()
1537+
15271538
function(get_project_name_from_src_var var output)
15281539
string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
15291540
MACHED_TOOL "${var}")

llvm/docs/CMake.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ enabled sub-projects. Nearly all of these variable names begin with
428428
$CMAKE_INSTALL_PREFIX/Toolchains containing an xctoolchain directory which can
429429
be used to override the default system tools.
430430

431+
**LLVM_<target>_LINKER_FLAGS**:STRING
432+
Defines the set of linker flags that should be applied to a <target>.
433+
431434
**LLVM_DEFAULT_TARGET_TRIPLE**:STRING
432435
LLVM target to use for code generation when no target is explicitly specified.
433436
It defaults to "host", meaning that it shall pick the architecture

0 commit comments

Comments
 (0)