Skip to content

Commit 12f324d

Browse files
[SYCL RTC] Use cmake --install for resource.cpp generation (#20466)
Fixes #20275 That way we achieve all of: * Better "filtering" of what we need, as we don't operate at files/directories level anymore. Applies to both what we want to include and what we don't need (i.e., if provided by components we're not interested in) * Avoid race conditions with other targets creating temp files in the directories we take files from
1 parent 40a9999 commit 12f324d

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

sycl-jit/jit-compiler/CMakeLists.txt

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,50 @@ else()
99
set(SYCL_JIT_VIRTUAL_TOOLCHAIN_ROOT "/sycl-jit-toolchain/")
1010
endif()
1111

12-
set(SYCL_JIT_RESOURCE_DEPS
13-
sycl-headers # include/sycl
14-
clang # lib/clang/N/include
15-
${CMAKE_CURRENT_SOURCE_DIR}/utils/generate.py)
12+
set(SYCL_JIT_RESOURCE_INSTALL_COMPONENTS sycl-headers OpenCL-Headers clang-resource-headers)
1613

1714
if ("libclc" IN_LIST LLVM_ENABLE_PROJECTS)
18-
# Somehow just "libclc" doesn't build "remangled-*" (and maybe whatever else).
19-
list(APPEND SYCL_JIT_RESOURCE_DEPS libclc libspirv-builtins) # lib/clc/*.bc
15+
list(APPEND SYCL_JIT_RESOURCE_INSTALL_COMPONENTS libspirv-builtins)
2016
endif()
2117

2218
if ("libdevice" IN_LIST LLVM_ENABLE_PROJECTS)
23-
list(APPEND SYCL_JIT_RESOURCE_DEPS libsycldevice) # lib/*.bc
19+
list(APPEND SYCL_JIT_RESOURCE_INSTALL_COMPONENTS libsycldevice)
2420
endif()
2521

22+
set(SYCL_JIT_RESOURCE_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/rtc-resources-install)
23+
24+
set(SYCL_JIT_PREPARE_RESOURCE_COMMANDS)
25+
foreach(component IN LISTS SYCL_JIT_RESOURCE_INSTALL_COMPONENTS)
26+
list(APPEND SYCL_JIT_PREPARE_RESOURCE_COMMANDS
27+
COMMAND ${CMAKE_COMMAND} --install ${CMAKE_BINARY_DIR} --prefix ${SYCL_JIT_RESOURCE_INSTALL_DIR} --component "${component}"
28+
)
29+
endforeach()
30+
31+
set(SYCL_JIT_RESOURCE_DEPS ${SYCL_JIT_RESOURCE_INSTALL_COMPONENTS})
32+
# OpenCL-Headers doesn't have a corresponding build target:
33+
list(FILTER SYCL_JIT_RESOURCE_DEPS EXCLUDE REGEX "^OpenCL-Headers$")
34+
35+
# This is very hacky and I don't quite know what I'm doing, but it's necessary
36+
# to have `resource.cpp` re-generated/re-built when some SYCL header changes.
37+
#
38+
# Inspired by the way `sycl-headers` target is created.
39+
file(GLOB_RECURSE SYCL_JIT_RESOURCE_FILES CONFIGURE_DEPENDS "${SYCL_JIT_RESOURCE_INSTALL_DIR}/*" )
40+
41+
add_custom_target(rtc-prepare-resources
42+
DEPENDS ${SYCL_JIT_RESOURCE_DEPS}
43+
${SYCL_JIT_PREPARE_RESOURCE_COMMANDS}
44+
BYPRODUCTS
45+
${SYCL_JIT_RESOURCE_FILES}
46+
)
47+
2648
add_custom_command(
2749
OUTPUT ${SYCL_JIT_RESOURCE_CPP}
28-
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/utils/generate.py --toolchain-dir ${CMAKE_BINARY_DIR} --output ${SYCL_JIT_RESOURCE_CPP} --prefix ${SYCL_JIT_VIRTUAL_TOOLCHAIN_ROOT}
50+
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/utils/generate.py --toolchain-dir ${SYCL_JIT_RESOURCE_INSTALL_DIR} --output ${SYCL_JIT_RESOURCE_CPP} --prefix ${SYCL_JIT_VIRTUAL_TOOLCHAIN_ROOT}
2951
DEPENDS
52+
rtc-prepare-resources
3053
${SYCL_JIT_RESOURCE_DEPS}
54+
${SYCL_JIT_RESOURCE_FILES}
55+
${CMAKE_CURRENT_SOURCE_DIR}/utils/generate.py
3156
)
3257

3358
# We use C23/C++26's `#embed` to implement this resource creation, and "current"
@@ -67,6 +92,8 @@ add_custom_command(
6792
${clang_exe} --target=${LLVM_HOST_TRIPLE} ${SYCL_JIT_RESOURCE_CPP} -I ${CMAKE_CURRENT_SOURCE_DIR}/include -c -o ${SYCL_JIT_RESOURCE_OBJ} ${SYCL_JIT_RESOURCE_CXX_FLAGS}
6893
DEPENDS
6994
${SYCL_JIT_RESOURCE_CPP}
95+
${SYCL_JIT_RESOURCE_DEPS}
96+
${SYCL_JIT_RESOURCE_FILES}
7097
${CMAKE_CURRENT_SOURCE_DIR}/include/Resource.h
7198
)
7299

sycl-jit/jit-compiler/utils/generate.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,7 @@ def process_dir(dir):
5252
file_path = os.path.join(root, file)
5353
process_file(file_path)
5454

55-
process_dir(os.path.join(args.toolchain_dir, "include/"))
56-
process_dir(os.path.join(args.toolchain_dir, "lib/clang/"))
57-
process_dir(os.path.join(args.toolchain_dir, "lib/clc/"))
58-
59-
for file in glob.iglob(
60-
"*.bc", root_dir=os.path.join(args.toolchain_dir, "lib")
61-
):
62-
file_path = os.path.join(args.toolchain_dir, "lib", file)
63-
process_file(file_path)
55+
process_dir(args.toolchain_dir)
6456

6557
out.write(
6658
f"""

0 commit comments

Comments
 (0)