Skip to content

Commit 903d1c6

Browse files
authored
[libclc] More cross compilation fixes (#97811)
* Move the setup_host_tool calls to the directories of their tool. Although it works to call it in libclc, it can only appear in a single location so it fails the "what if everyone did this?" test and causes problems for downstream code that also wants to use native versions of these tools from other projects. * Correct the TARGET "${${tool}_target}" check. "${${tool}_target}" may be set to the path to the executable, which works in dependencies but cannot be tested using if(TARGET). For lack of a better alternative, just check that "${${tool}_target}" is non-empty and trust that if it is, it is set to a meaningful value. If somehow it turns out to be a valid target, its value will still show up in error messages anyway. * Account for llvm-spirv possibly being provided in-tree. Per https://github.com/KhronosGroup/SPIRV-LLVM-Translator?tab=readme-ov-file#llvm-in-tree-build it is possible to drop llvm-spirv into LLVM and have it built as part of LLVM's build. In this configuration, cross builds of LLVM require a native version of llvm-spirv to be built.
1 parent 93857af commit 903d1c6

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

clang/tools/driver/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ add_clang_tool(clang
3838
GENERATE_DRIVER
3939
)
4040

41+
setup_host_tool(clang CLANG clang_exe clang_target)
42+
4143
clang_target_link_libraries(clang
4244
PRIVATE
4345
clangBasic

libclc/CMakeLists.txt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ else()
7474
endif()
7575

7676
if( NOT EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
77-
setup_host_tool( clang CLANG clang_exe clang_target )
78-
setup_host_tool( llvm-as LLVM_AS llvm-as_exe llvm-as_target )
79-
setup_host_tool( llvm-link LLVM_LINK llvm-link_exe llvm-link_target )
80-
setup_host_tool( opt OPT opt_exe opt_target )
77+
get_host_tool_path( clang CLANG clang_exe clang_target )
78+
get_host_tool_path( llvm-as LLVM_AS llvm-as_exe llvm-as_target )
79+
get_host_tool_path( llvm-link LLVM_LINK llvm-link_exe llvm-link_target )
80+
get_host_tool_path( opt OPT opt_exe opt_target )
8181
endif()
8282
endif()
8383

@@ -98,17 +98,19 @@ if( EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
9898
endif()
9999

100100
foreach( tool IN ITEMS clang opt llvm-as llvm-link )
101-
if( NOT EXISTS "${${tool}_exe}" AND NOT TARGET "${${tool}_target}" )
101+
if( NOT EXISTS "${${tool}_exe}" AND "${tool}_target" STREQUAL "" )
102102
message( FATAL_ERROR "libclc toolchain incomplete - missing tool ${tool}!" )
103103
endif()
104104
endforeach()
105105

106106
# llvm-spirv is an optional dependency, used to build spirv-* targets.
107-
find_program( LLVM_SPIRV llvm-spirv PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
108-
109-
if( LLVM_SPIRV )
110-
add_executable( libclc::llvm-spirv IMPORTED GLOBAL )
111-
set_target_properties( libclc::llvm-spirv PROPERTIES IMPORTED_LOCATION ${LLVM_SPIRV} )
107+
# It may be provided in-tree or externally.
108+
if( TARGET llvm-spirv )
109+
get_host_tool_path( llvm-spirv LLVM_SPIRV llvm-spirv_exe llvm-spirv_target )
110+
else()
111+
find_program( LLVM_SPIRV llvm-spirv PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
112+
set( llvm-spirv_exe "${LLVM_SPIRV}" )
113+
set( llvm-spirv_target )
112114
endif()
113115

114116
# List of all targets. Note that some are added dynamically below.
@@ -131,7 +133,7 @@ endif()
131133

132134
# spirv-mesa3d and spirv64-mesa3d targets can only be built with the (optional)
133135
# llvm-spirv external tool.
134-
if( TARGET libclc::llvm-spirv )
136+
if( llvm-spirv_exe )
135137
list( APPEND LIBCLC_TARGETS_ALL spirv-mesa3d- spirv64-mesa3d- )
136138
endif()
137139

@@ -144,7 +146,7 @@ list( SORT LIBCLC_TARGETS_TO_BUILD )
144146
# Verify that the user hasn't requested mesa3d targets without an available
145147
# llvm-spirv tool.
146148
if( "spirv-mesa3d-" IN_LIST LIBCLC_TARGETS_TO_BUILD OR "spirv64-mesa3d-" IN_LIST LIBCLC_TARGETS_TO_BUILD )
147-
if( NOT TARGET libclc::llvm-spirv )
149+
if( NOT llvm-spirv_exe )
148150
message( FATAL_ERROR "SPIR-V targets requested, but spirv-tools is not installed" )
149151
endif()
150152
endif()
@@ -405,8 +407,8 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
405407
if( ARCH STREQUAL spirv OR ARCH STREQUAL spirv64 )
406408
set( spv_suffix ${arch_suffix}.spv )
407409
add_custom_command( OUTPUT ${spv_suffix}
408-
COMMAND libclc::llvm-spirv ${spvflags} -o ${spv_suffix} ${builtins_link_lib}
409-
DEPENDS ${builtins_link_lib} ${builtins_link_lib_tgt}
410+
COMMAND ${llvm-spirv_exe} ${spvflags} -o ${spv_suffix} ${builtins_link_lib}
411+
DEPENDS ${llvm-spirv_target} ${builtins_link_lib} ${builtins_link_lib_tgt}
410412
)
411413
add_custom_target( "prepare-${spv_suffix}" ALL DEPENDS "${spv_suffix}" )
412414
set_target_properties( "prepare-${spv_suffix}" PROPERTIES FOLDER "libclc/Device IR/Prepare" )

llvm/tools/llvm-as/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ add_llvm_tool(llvm-as
1111
DEPENDS
1212
intrinsics_gen
1313
)
14+
15+
setup_host_tool(llvm-as LLVM_AS llvm_as_exe llvm_as_target)

llvm/tools/llvm-link/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ add_llvm_tool(llvm-link
1717
DEPENDS
1818
intrinsics_gen
1919
)
20+
21+
setup_host_tool(llvm-link LLVM_LINK llvm_link_exe llvm_link_target)

llvm/tools/opt/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ add_llvm_tool(opt
4949
)
5050
target_link_libraries(opt PRIVATE LLVMOptDriver)
5151

52+
setup_host_tool(opt OPT opt_exe opt_target)
53+
5254
export_executable_symbols_for_plugins(opt)

0 commit comments

Comments
 (0)