Skip to content

Commit 5ee53d4

Browse files
authored
[libclc] Fix cross in-tree builds (#97392)
When performing cross in-tree builds, we need native versions of various tools, we cannot assume the cross builds that are part of the current build are executable. LLVM provides the setup_host_tool function to handle this, either picking up versions of tools from LLVM_NATIVE_TOOL_DIR, or implicitly building native versions as needed. Use it for libclc too. LLVM's setup_host_tool function assumes the project is LLVM, so this also needs libclc's project() to be conditional on it being built standalone. Luckily, the only change this needs is using CMAKE_CURRENT_SOURCE_DIR instead of PROJECT_SOURCE_DIR.
1 parent abb5bd3 commit 5ee53d4

File tree

2 files changed

+38
-33
lines changed

2 files changed

+38
-33
lines changed

libclc/CMakeLists.txt

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
cmake_minimum_required(VERSION 3.20.0)
22

3-
project( libclc VERSION 0.2.0 LANGUAGES CXX C)
3+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
4+
project(libclc VERSION 0.2.0 LANGUAGES CXX C)
5+
endif()
46

57
set(CMAKE_CXX_STANDARD 17)
68

79
# Add path for custom modules
8-
list( INSERT CMAKE_MODULE_PATH 0 "${PROJECT_SOURCE_DIR}/cmake/modules" )
10+
list( INSERT CMAKE_MODULE_PATH 0 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules" )
911

1012
set( LIBCLC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} )
1113
set( LIBCLC_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} )
@@ -49,12 +51,12 @@ if( LIBCLC_STANDALONE_BUILD OR CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DI
4951
message( FATAL_ERROR "libclc needs at least LLVM ${LIBCLC_MIN_LLVM}" )
5052
endif()
5153

52-
# Import required tools as targets
54+
# Import required tools
5355
if( NOT EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
5456
foreach( tool IN ITEMS clang llvm-as llvm-link opt )
5557
find_program( LLVM_TOOL_${tool} ${tool} PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
56-
add_executable( libclc::${tool} IMPORTED GLOBAL )
57-
set_target_properties( libclc::${tool} PROPERTIES IMPORTED_LOCATION ${LLVM_TOOL_${tool}} )
58+
set( ${tool}_exe ${LLVM_TOOL_${tool}} )
59+
set( ${tool}_target )
5860
endforeach()
5961
endif()
6062
else()
@@ -71,9 +73,10 @@ else()
7173
endif()
7274

7375
if( NOT EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
74-
foreach( tool IN ITEMS clang llvm-as llvm-link opt )
75-
add_executable(libclc::${tool} ALIAS ${tool})
76-
endforeach()
76+
setup_host_tool( clang CLANG clang_exe clang_target )
77+
setup_host_tool( llvm-as LLVM_AS llvm-as_exe llvm-as_target )
78+
setup_host_tool( llvm-link LLVM_LINK llvm-link_exe llvm-link_target )
79+
setup_host_tool( opt OPT opt_exe opt_target )
7780
endif()
7881
endif()
7982

@@ -88,14 +91,13 @@ if( EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
8891
foreach( tool IN ITEMS clang llvm-as llvm-link opt )
8992
find_program( LLVM_CUSTOM_TOOL_${tool} ${tool}
9093
PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
91-
add_executable( libclc::${tool} IMPORTED GLOBAL )
92-
set_target_properties( libclc::${tool} PROPERTIES
93-
IMPORTED_LOCATION ${LLVM_CUSTOM_TOOL_${tool}} )
94+
set( ${tool}_exe ${LLVM_CUSTOM_TOOL_${tool}} )
95+
set( ${tool}_target )
9496
endforeach()
9597
endif()
9698

9799
foreach( tool IN ITEMS clang opt llvm-as llvm-link )
98-
if( NOT TARGET libclc::${tool} )
100+
if( NOT EXISTS "${${tool}_exe}" AND NOT TARGET "${${tool}_target}" )
99101
message( FATAL_ERROR "libclc toolchain incomplete - missing tool ${tool}!" )
100102
endif()
101103
endforeach()
@@ -165,8 +167,11 @@ set(LLVM_LINK_COMPONENTS
165167
)
166168
if( LIBCLC_STANDALONE_BUILD )
167169
add_llvm_executable( prepare_builtins utils/prepare-builtins.cpp )
170+
set( prepare_builtins_exe prepare_builtins )
171+
set( prepare_builtins_target prepare_builtins )
168172
else()
169173
add_llvm_utility( prepare_builtins utils/prepare-builtins.cpp )
174+
setup_host_tool( prepare_builtins PREPARE_BUILTINS prepare_builtins_exe prepare_builtins_target )
170175
endif()
171176
target_compile_definitions( prepare_builtins PRIVATE ${LLVM_VERSION_DEFINE} )
172177
# These were not properly reported in early LLVM and we don't need them
@@ -211,15 +216,15 @@ if( ENABLE_RUNTIME_SUBNORMAL )
211216
foreach( file IN ITEMS subnormal_use_default subnormal_disable )
212217
link_bc(
213218
TARGET ${file}
214-
INPUTS ${PROJECT_SOURCE_DIR}/generic/lib/${file}.ll
219+
INPUTS ${CMAKE_CURRENT_SOURCE_DIR}/generic/lib/${file}.ll
215220
)
216221
install( FILES $<TARGET_PROPERTY:${file},TARGET_FILE> ARCHIVE
217222
DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
218223
endforeach()
219224
endif()
220225

221226
find_package( Python3 REQUIRED COMPONENTS Interpreter )
222-
file( TO_CMAKE_PATH ${PROJECT_SOURCE_DIR}/generic/lib/gen_convert.py script_loc )
227+
file( TO_CMAKE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/generic/lib/gen_convert.py script_loc )
223228
add_custom_command(
224229
OUTPUT convert.cl
225230
COMMAND ${Python3_EXECUTABLE} ${script_loc} > convert.cl
@@ -264,7 +269,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
264269
foreach( l ${dirs} ${DARCH} ${DARCH}-${OS} ${DARCH}-${VENDOR}-${OS} )
265270
foreach( s "SOURCES" "SOURCES_${LLVM_MAJOR}.${LLVM_MINOR}" )
266271
file( TO_CMAKE_PATH ${l}/lib/${s} file_loc )
267-
file( TO_CMAKE_PATH ${PROJECT_SOURCE_DIR}/${file_loc} loc )
272+
file( TO_CMAKE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/${file_loc} loc )
268273
# Prepend the location to give higher priority to
269274
# specialized implementation
270275
if( EXISTS ${loc} )
@@ -339,7 +344,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
339344
list( APPEND build_flags
340345
-D__CLC_INTERNAL
341346
-D${CLC_TARGET_DEFINE}
342-
-I${PROJECT_SOURCE_DIR}/generic/include
347+
-I${CMAKE_CURRENT_SOURCE_DIR}/generic/include
343348
# FIXME: Fix libclc to not require disabling this noisy warning
344349
-Wno-bitwise-conditional-parentheses
345350
)
@@ -360,9 +365,9 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
360365
# the path (e.g., ironing out any ".."), then make it relative to the
361366
# root directory again, and use that relative path component for the
362367
# binary path.
363-
get_filename_component( abs_path ${file} ABSOLUTE BASE_DIR ${PROJECT_SOURCE_DIR} )
364-
file( RELATIVE_PATH root_rel_path ${PROJECT_SOURCE_DIR} ${abs_path} )
365-
set( input_file ${PROJECT_SOURCE_DIR}/${file} )
368+
get_filename_component( abs_path ${file} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR} )
369+
file( RELATIVE_PATH root_rel_path ${CMAKE_CURRENT_SOURCE_DIR} ${abs_path} )
370+
set( input_file ${CMAKE_CURRENT_SOURCE_DIR}/${file} )
366371
set( output_file "${LIBCLC_ARCH_OBJFILE_DIR}/${root_rel_path}.bc" )
367372
endif()
368373

@@ -373,7 +378,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
373378
INPUT ${input_file}
374379
OUTPUT ${output_file}
375380
EXTRA_OPTS "${mcpu}" -fno-builtin -nostdlib
376-
"${build_flags}" -I${PROJECT_SOURCE_DIR}/${file_dir}
381+
"${build_flags}" -I${CMAKE_CURRENT_SOURCE_DIR}/${file_dir}
377382
DEPENDENCIES generate_convert.cl clspv-generate_convert.cl
378383
)
379384
list( APPEND bytecode_files ${output_file} )
@@ -407,9 +412,9 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
407412

408413
# Add opt target
409414
add_custom_command( OUTPUT ${builtins_opt_lib_tgt}.bc
410-
COMMAND libclc::opt ${opt_flags} -o ${builtins_opt_lib_tgt}.bc
415+
COMMAND ${opt_exe} ${opt_flags} -o ${builtins_opt_lib_tgt}.bc
411416
${builtins_link_lib}
412-
DEPENDS libclc::opt ${builtins_link_lib} ${builtins_link_lib_tgt}
417+
DEPENDS ${opt_target} ${builtins_link_lib} ${builtins_link_lib_tgt}
413418
)
414419
add_custom_target( ${builtins_opt_lib_tgt}
415420
ALL DEPENDS ${builtins_opt_lib_tgt}.bc
@@ -423,15 +428,15 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
423428
# Add prepare target
424429
set( obj_suffix ${arch_suffix}.bc )
425430
add_custom_command( OUTPUT ${obj_suffix}
426-
COMMAND prepare_builtins -o ${obj_suffix} ${builtins_opt_lib}
427-
DEPENDS ${builtins_opt_lib} ${builtins_opt_lib_tgt} prepare_builtins )
431+
COMMAND ${prepare_builtins_exe} -o ${obj_suffix} ${builtins_opt_lib}
432+
DEPENDS ${builtins_opt_lib} ${builtins_opt_lib_tgt} ${prepare_builtins_target} )
428433
add_custom_target( prepare-${obj_suffix} ALL DEPENDS ${obj_suffix} )
429434

430435
# nvptx-- targets don't include workitem builtins
431436
if( NOT clang_triple MATCHES ".*ptx.*--$" )
432437
add_test( NAME external-calls-${obj_suffix}
433438
COMMAND ./check_external_calls.sh ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} ${LLVM_TOOLS_BINARY_DIR}
434-
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} )
439+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} )
435440
endif()
436441

437442
install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )

libclc/cmake/modules/AddLibclc.cmake

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# * DEPENDENCIES <string> ...
1313
# List of extra dependencies to inject
1414
#
15-
# Depends on the libclc::clang and libclc::llvm-as targets for compiling and
16-
# assembling, respectively.
15+
# Depends on the clang, llvm-as, and llvm-link targets for compiling,
16+
# assembling, and linking, respectively.
1717
function(compile_to_bc)
1818
cmake_parse_arguments(ARG
1919
""
@@ -45,7 +45,7 @@ function(compile_to_bc)
4545

4646
add_custom_command(
4747
OUTPUT ${ARG_OUTPUT}${TMP_SUFFIX}
48-
COMMAND libclc::clang
48+
COMMAND ${clang_exe}
4949
${TARGET_ARG}
5050
${PP_OPTS}
5151
${ARG_EXTRA_OPTS}
@@ -58,7 +58,7 @@ function(compile_to_bc)
5858
-x cl
5959
${ARG_INPUT}
6060
DEPENDS
61-
libclc::clang
61+
${clang_target}
6262
${ARG_INPUT}
6363
${ARG_DEPENDENCIES}
6464
DEPFILE ${ARG_OUTPUT}.d
@@ -67,8 +67,8 @@ function(compile_to_bc)
6767
if( ${FILE_EXT} STREQUAL ".ll" )
6868
add_custom_command(
6969
OUTPUT ${ARG_OUTPUT}
70-
COMMAND libclc::llvm-as -o ${ARG_OUTPUT} ${ARG_OUTPUT}${TMP_SUFFIX}
71-
DEPENDS libclc::llvm-as ${ARG_OUTPUT}${TMP_SUFFIX}
70+
COMMAND ${llvm-as_exe} -o ${ARG_OUTPUT} ${ARG_OUTPUT}${TMP_SUFFIX}
71+
DEPENDS ${llvm-as_target} ${ARG_OUTPUT}${TMP_SUFFIX}
7272
)
7373
endif()
7474
endfunction()
@@ -107,8 +107,8 @@ function(link_bc)
107107

108108
add_custom_command(
109109
OUTPUT ${ARG_TARGET}.bc
110-
COMMAND libclc::llvm-link -o ${ARG_TARGET}.bc ${LINK_INPUT_ARG}
111-
DEPENDS libclc::llvm-link ${ARG_DEPENDENCIES} ${ARG_INPUTS} ${RSP_FILE}
110+
COMMAND ${llvm-link_exe} -o ${ARG_TARGET}.bc ${LINK_INPUT_ARG}
111+
DEPENDS ${llvm-link_target} ${ARG_DEPENDENCIES} ${ARG_INPUTS} ${RSP_FILE}
112112
)
113113

114114
add_custom_target( ${ARG_TARGET} ALL DEPENDS ${ARG_TARGET}.bc )

0 commit comments

Comments
 (0)