Skip to content

Commit 4cee0e3

Browse files
authored
[LLD] Fix llvm-driver cmake integration for LLD (#76305)
Previously, even though LLD was linked as part of llvm-driver when using `cmake ... -DLLVM_TOOL_LLVM_DRIVER_BUILD=ON`, there were build issues when compiling incrementally. Sometimes link errors when linking LLD, other times, the `llvm.exe` would be impropely be replaced by `lld.exe`.
1 parent 93b4705 commit 4cee0e3

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

lld/cmake/modules/AddLLD.cmake

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,48 @@ macro(add_lld_executable name)
3737
endmacro(add_lld_executable)
3838

3939
macro(add_lld_tool name)
40+
cmake_parse_arguments(ARG "DEPENDS;GENERATE_DRIVER" "" "" ${ARGN})
4041
if (NOT LLD_BUILD_TOOLS)
4142
set(EXCLUDE_FROM_ALL ON)
4243
endif()
44+
if(ARG_GENERATE_DRIVER
45+
AND LLVM_TOOL_LLVM_DRIVER_BUILD
46+
AND (NOT LLVM_DISTRIBUTION_COMPONENTS OR ${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS)
47+
)
48+
set(get_obj_args ${ARGN})
49+
list(FILTER get_obj_args EXCLUDE REGEX "^SUPPORT_PLUGINS$")
50+
generate_llvm_objects(${name} ${get_obj_args})
51+
add_custom_target(${name} DEPENDS llvm-driver)
52+
else()
53+
add_lld_executable(${name} ${ARGN})
4354

44-
add_lld_executable(${name} ${ARGN})
45-
46-
if (LLD_BUILD_TOOLS)
47-
get_target_export_arg(${name} LLD export_to_lldtargets)
48-
install(TARGETS ${name}
49-
${export_to_lldtargets}
50-
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
51-
COMPONENT ${name})
52-
53-
if(NOT CMAKE_CONFIGURATION_TYPES)
54-
add_llvm_install_targets(install-${name}
55-
DEPENDS ${name}
55+
if (LLD_BUILD_TOOLS)
56+
get_target_export_arg(${name} LLD export_to_lldtargets)
57+
install(TARGETS ${name}
58+
${export_to_lldtargets}
59+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
5660
COMPONENT ${name})
61+
62+
if(NOT CMAKE_CONFIGURATION_TYPES)
63+
add_llvm_install_targets(install-${name}
64+
DEPENDS ${name}
65+
COMPONENT ${name})
66+
endif()
67+
set_property(GLOBAL APPEND PROPERTY LLD_EXPORTS ${name})
5768
endif()
58-
set_property(GLOBAL APPEND PROPERTY LLD_EXPORTS ${name})
5969
endif()
6070
endmacro()
6171

6272
macro(add_lld_symlink name dest)
63-
llvm_add_tool_symlink(LLD ${name} ${dest} ALWAYS_GENERATE)
64-
# Always generate install targets
65-
llvm_install_symlink(LLD ${name} ${dest} ALWAYS_GENERATE)
73+
get_property(LLVM_DRIVER_TOOLS GLOBAL PROPERTY LLVM_DRIVER_TOOLS)
74+
if(LLVM_TOOL_LLVM_DRIVER_BUILD
75+
AND ${dest} IN_LIST LLVM_DRIVER_TOOLS
76+
AND (NOT LLVM_DISTRIBUTION_COMPONENTS OR ${dest} IN_LIST LLVM_DISTRIBUTION_COMPONENTS)
77+
)
78+
set_property(GLOBAL APPEND PROPERTY LLVM_DRIVER_TOOL_ALIASES_${dest} ${name})
79+
else()
80+
llvm_add_tool_symlink(LLD ${name} ${dest} ALWAYS_GENERATE)
81+
# Always generate install targets
82+
llvm_install_symlink(LLD ${name} ${dest} ALWAYS_GENERATE)
83+
endif()
6684
endmacro()

lld/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ llvm_canonicalize_cmake_booleans(
77
LLVM_BUILD_EXAMPLES
88
LLVM_ENABLE_PLUGINS
99
LLVM_BYE_LINK_INTO_TOOLS
10+
LLVM_TOOL_LLVM_DRIVER_BUILD
1011
)
1112

1213
configure_lit_site_cfg(

lld/tools/lld/CMakeLists.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ add_lld_tool(lld
1212
export_executable_symbols_for_plugins(lld)
1313

1414
function(lld_target_link_libraries target type)
15-
target_link_libraries(${target} ${type} ${ARGN})
1615
if (TARGET obj.${target})
1716
target_link_libraries(obj.${target} ${ARGN})
1817
endif()
18+
19+
get_property(LLVM_DRIVER_TOOLS GLOBAL PROPERTY LLVM_DRIVER_TOOLS)
20+
if(LLVM_TOOL_LLVM_DRIVER_BUILD AND ${target} IN_LIST LLVM_DRIVER_TOOLS)
21+
set(target llvm-driver)
22+
endif()
23+
24+
target_link_libraries(${target} ${type} ${ARGN})
1925
endfunction()
2026

2127
lld_target_link_libraries(lld
@@ -28,9 +34,6 @@ lld_target_link_libraries(lld
2834
lldWasm
2935
)
3036

31-
install(TARGETS lld
32-
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
33-
3437
if(NOT LLD_SYMLINKS_TO_CREATE)
3538
set(LLD_SYMLINKS_TO_CREATE
3639
lld-link ld.lld ld64.lld wasm-ld)

0 commit comments

Comments
 (0)