Skip to content

Commit 74d503e

Browse files
committed
Add CMake function to embed source files into a target
1 parent 4dda99a commit 74d503e

File tree

4 files changed

+84
-13
lines changed

4 files changed

+84
-13
lines changed

CMakeLists.txt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,80 @@ target_link_libraries(${PROJECT_NAME} PRIVATE CUDA::cudart_static)
2525
target_link_libraries(${PROJECT_NAME} PRIVATE CUDA::cuda_driver)
2626
target_link_libraries(${PROJECT_NAME} PRIVATE CUDA::nvrtc)
2727

28+
29+
##
30+
# Embed files from a directory into a target.
31+
#
32+
# Parameters:
33+
# target: The target for which the embedded files are created
34+
# directory: The directory where the files are located
35+
# files...: Multiple files can be listed
36+
#
37+
# Usage:
38+
# kernel_launcher_embed(my_target my_directory file1.bin file2.bin)
39+
##
40+
function(kernel_launcher_embed target directory)
41+
string(MAKE_C_IDENTIFIER ${target}_embed_${directory} output_target)
42+
set(deps "")
43+
44+
foreach(input IN LISTS ARGN)
45+
string(MAKE_C_IDENTIFIER ${input} input_identifier)
46+
set(output "${CMAKE_CURRENT_BINARY_DIR}/${input_identifier}.o")
47+
48+
add_custom_command(
49+
OUTPUT ${output}
50+
COMMAND ${CMAKE_LINKER} --relocatable --format binary --output ${output} ${input}
51+
WORKING_DIRECTORY ${directory}
52+
DEPENDS ${input}
53+
)
54+
55+
set(deps ${deps} "${output}")
56+
endforeach()
57+
58+
add_custom_target(${output_target} ALL DEPENDS ${deps})
59+
60+
add_dependencies(${target} ${output_target})
61+
target_link_libraries(${target} ${output})
62+
endfunction()
63+
64+
##
65+
# Embed files from a directory into a target with the ability to specify the embedded files using a glob pattern.
66+
#
67+
# Parameters:
68+
# target: The target for which the embedded files are created
69+
# directory: The directory where the files are located
70+
# patterns...: Multiple patterns can be listed
71+
#
72+
# Usage:
73+
# kernel_launcher_embed_glob(my_target my_directory *.cuh *.h)
74+
##
75+
function(kernel_launcher_embed_glob target directory)
76+
file(GLOB_RECURSE files LIST_DIRECTORIES false RELATIVE "${directory}" ${ARGN})
77+
kernel_launcher_embed("${target}" "${directory}" ${files})
78+
endfunction()
79+
80+
##
81+
# Embed all files from a directory into a target.
82+
#
83+
# Parameters:
84+
# target: The target for which the embedded files are created
85+
# directory: The directory where the files are located
86+
#
87+
# Usage:
88+
# kernel_launcher_embed_directory(my_target my_directory)
89+
##
90+
function(kernel_launcher_embed_directory target directory)
91+
kernel_launcher_embed_glob(target directory "*")
92+
endfunction()
93+
94+
if (NOT DEFINED KERNEL_LAUNCHER_EMBEDDED_DATA)
95+
set(KERNEL_LAUNCHER_EMBEDDED_DATA 1)
96+
endif()
97+
98+
if (KERNEL_LAUNCHER_EMBEDDED_DATA)
99+
target_compile_definitions(${PROJECT_NAME} PRIVATE KERNEL_LAUNCHER_EMBEDDED_DATA=1)
100+
endif()
101+
28102
if (KERNEL_LAUNCHER_BUILD_TEST)
29103
add_subdirectory(tests)
30104
endif()
@@ -39,3 +113,4 @@ if (KERNEL_LAUNCHER_ZLIB)
39113
target_link_libraries(${PROJECT_NAME} PRIVATE ${ZLIB_LIBRARIES})
40114
target_compile_definitions(${PROJECT_NAME} PRIVATE KERNEL_LAUNCHER_USE_ZLIB=1)
41115
endif()
116+

examples/matmul/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ project(${PROJECT_NAME} LANGUAGES CXX CUDA)
66
add_executable(${PROJECT_NAME} "${PROJECT_SOURCE_DIR}/main.cu")
77
target_link_libraries(${PROJECT_NAME} kernel_launcher)
88

9+
kernel_launcher_embed_glob(${PROJECT_NAME} ${PROJECT_SOURCE_DIR} *.cuh *.cu)

examples/matmul/main.cu

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ int main() {
2121
B[i] = TF(i % 13);
2222
}
2323

24-
std::string this_file = __FILE__;
25-
std::string this_directory = this_file.substr(0, this_file.rfind('/'));
26-
27-
kl::KernelBuilder builder("matmul_kernel", this_directory + "/matmul.cu");
24+
kl::KernelBuilder builder("matmul_kernel", "matmul.cu");
2825

2926
auto bx = builder.tune("block_size_x", {16, 32, 48}, 32);
3027
auto by = builder.tune("block_size_y", {1, 2, 4, 8, 16, 32}, 32);

src/fs.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ struct EmbeddedData {
140140
}
141141

142142
~EmbeddedData() {
143-
if (app_) {
143+
if (app_ != nullptr) {
144144
dlclose(app_);
145145
}
146146
}
@@ -154,28 +154,26 @@ struct EmbeddedData {
154154
i = key.find_first_of(illegal_symbols, i + 1);
155155
}
156156

157-
if (app_) {
157+
if (app_ != nullptr) {
158158
const char* data = (const char*)dlsym(app_, key.c_str());
159159

160-
if (data) {
160+
if (data != nullptr) {
161161
return data;
162162
}
163163
}
164164

165165
return nullptr;
166166
}
167167

168-
bool find(const std::string& key, std::vector<char>& result) {
168+
bool find(const std::string& key, std::string& result) const {
169169
const char* begin = resolve_symbol("_binary_" + key + "_start");
170170
const char* end = resolve_symbol("_binary_" + key + "_end");
171171

172-
if (!begin || !end || begin > end) {
172+
if (begin == nullptr || end == nullptr || begin > end) {
173173
return false;
174174
}
175175

176-
size_t length = end - begin;
177-
result.resize(length);
178-
std::copy(begin, end, result.data());
176+
result = std::string(begin, end);
179177
return true;
180178
}
181179

@@ -258,4 +256,4 @@ std::string ForwardLoader::load(const std::string& file_name) const {
258256
return parent_->load(file_name);
259257
}
260258

261-
} // namespace kernel_launcher
259+
} // namespace kernel_launcher

0 commit comments

Comments
 (0)