Skip to content

Commit 4b38c09

Browse files
authored
cmake: merge loops for handing Python and data extensions (#266)
* cmake: merge loops for handing Python and data extensions * Fixup move
1 parent b209d62 commit 4b38c09

File tree

1 file changed

+33
-50
lines changed

1 file changed

+33
-50
lines changed

build2cmake/src/templates/build-variants.cmake

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ function(add_kernels_install_target TARGET_NAME PACKAGE_NAME BUILD_VARIANT_NAME)
127127
set(ARG_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
128128
endif()
129129

130+
# Always include 'py' extension for Python files
131+
set(ALL_EXTENSIONS ${ARG_DATA_EXTENSIONS})
132+
list(APPEND ALL_EXTENSIONS "py")
133+
130134
if (${GPU_LANG} STREQUAL "CPU")
131135
set(_BACKEND "cpu")
132136
elseif (${GPU_LANG} STREQUAL "CUDA")
@@ -153,13 +157,15 @@ function(add_kernels_install_target TARGET_NAME PACKAGE_NAME BUILD_VARIANT_NAME)
153157
RUNTIME DESTINATION "${KERNEL_INSTALL_DIR}"
154158
COMPONENT ${TARGET_NAME})
155159

156-
# Glob Python files to install recursively.
157-
file(GLOB_RECURSE PYTHON_FILES RELATIVE "${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}" "${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}/*.py")
158-
foreach(python_file IN LISTS PYTHON_FILES)
159-
get_filename_component(python_file_dir "${python_file}" DIRECTORY)
160-
install(FILES "${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}/${python_file}"
161-
DESTINATION "${KERNEL_INSTALL_DIR}/${python_file_dir}"
162-
COMPONENT ${TARGET_NAME})
160+
# Install data files with specified extensions
161+
foreach(ext IN LISTS ALL_EXTENSIONS)
162+
file(GLOB_RECURSE DATA_FILES RELATIVE "${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}" "${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}/*.${ext}")
163+
foreach(data_file IN LISTS DATA_FILES)
164+
get_filename_component(data_file_dir "${data_file}" DIRECTORY)
165+
install(FILES "${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}/${data_file}"
166+
DESTINATION "${KERNEL_INSTALL_DIR}/${data_file_dir}"
167+
COMPONENT ${TARGET_NAME})
168+
endforeach()
163169
endforeach()
164170

165171
install(FILES ${CMAKE_SOURCE_DIR}/metadata-${_BACKEND}.json
@@ -173,17 +179,6 @@ function(add_kernels_install_target TARGET_NAME PACKAGE_NAME BUILD_VARIANT_NAME)
173179
RENAME "__init__.py"
174180
COMPONENT ${TARGET_NAME})
175181

176-
# Install data files with specified extensions
177-
foreach(ext IN LISTS ARG_DATA_EXTENSIONS)
178-
file(GLOB_RECURSE DATA_FILES RELATIVE "${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}" "${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}/*.${ext}")
179-
foreach(data_file IN LISTS DATA_FILES)
180-
get_filename_component(data_file_dir "${data_file}" DIRECTORY)
181-
install(FILES "${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}/${data_file}"
182-
DESTINATION "${KERNEL_INSTALL_DIR}/${data_file_dir}"
183-
COMPONENT ${TARGET_NAME})
184-
endforeach()
185-
endforeach()
186-
187182
message(STATUS "Added install rules for ${TARGET_NAME} -> ${BUILD_VARIANT_NAME}")
188183
endfunction()
189184

@@ -207,14 +202,15 @@ function(add_local_install_target TARGET_NAME PACKAGE_NAME BUILD_VARIANT_NAME)
207202
set(multiValueArgs DATA_EXTENSIONS)
208203
cmake_parse_arguments(ARG "" "" "${multiValueArgs}" ${ARGN})
209204

205+
# Always include 'py' extension for Python files
206+
set(ALL_EXTENSIONS ${ARG_DATA_EXTENSIONS})
207+
list(APPEND ALL_EXTENSIONS "py")
208+
210209
# Define your local, folder based, installation directory
211210
set(LOCAL_INSTALL_DIR "${CMAKE_SOURCE_DIR}/build/${BUILD_VARIANT_NAME}")
212211
# Variant directory is where metadata.json should go (for kernels upload discovery)
213212
set(VARIANT_DIR "${CMAKE_SOURCE_DIR}/build/${BUILD_VARIANT_NAME}")
214213

215-
# Glob Python files to install recursively.
216-
file(GLOB_RECURSE PYTHON_FILES RELATIVE "${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}" "${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}/*.py")
217-
218214
# Create a custom target for local installation
219215
add_custom_target(local_install
220216
COMMENT "Installing files to local directory..."
@@ -234,6 +230,22 @@ function(add_local_install_target TARGET_NAME PACKAGE_NAME BUILD_VARIANT_NAME)
234230
message(FATAL_ERROR "Unsupported GPU_LANG: ${GPU_LANG}")
235231
endif()
236232

233+
# Copy data files with specified extensions
234+
foreach(ext IN LISTS ALL_EXTENSIONS)
235+
file(GLOB_RECURSE DATA_FILES RELATIVE "${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}" "${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}/*.${ext}")
236+
foreach(data_file IN LISTS DATA_FILES)
237+
get_filename_component(data_file_dir "${data_file}" DIRECTORY)
238+
add_custom_command(TARGET local_install POST_BUILD
239+
COMMAND ${CMAKE_COMMAND} -E make_directory
240+
${LOCAL_INSTALL_DIR}/${data_file_dir}
241+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
242+
${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}/${data_file}
243+
${LOCAL_INSTALL_DIR}/${data_file_dir}/
244+
COMMENT "Copying ${data_file} to ${LOCAL_INSTALL_DIR}/${data_file_dir}"
245+
)
246+
endforeach()
247+
endforeach()
248+
237249
# Add custom commands to copy files
238250
add_custom_command(TARGET local_install POST_BUILD
239251
# Copy the shared library
@@ -255,35 +267,6 @@ function(add_local_install_target TARGET_NAME PACKAGE_NAME BUILD_VARIANT_NAME)
255267
COMMAND_EXPAND_LISTS
256268
)
257269

258-
# Copy each Python file preserving directory structure
259-
foreach(python_file IN LISTS PYTHON_FILES)
260-
get_filename_component(python_file_dir "${python_file}" DIRECTORY)
261-
add_custom_command(TARGET local_install POST_BUILD
262-
COMMAND ${CMAKE_COMMAND} -E make_directory
263-
${LOCAL_INSTALL_DIR}/${python_file_dir}
264-
COMMAND ${CMAKE_COMMAND} -E copy_if_different
265-
${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}/${python_file}
266-
${LOCAL_INSTALL_DIR}/${python_file_dir}/
267-
COMMENT "Copying ${python_file} to ${LOCAL_INSTALL_DIR}/${python_file_dir}"
268-
)
269-
endforeach()
270-
271-
# Copy data files with specified extensions
272-
foreach(ext IN LISTS ARG_DATA_EXTENSIONS)
273-
file(GLOB_RECURSE DATA_FILES RELATIVE "${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}" "${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}/*.${ext}")
274-
foreach(data_file IN LISTS DATA_FILES)
275-
get_filename_component(data_file_dir "${data_file}" DIRECTORY)
276-
add_custom_command(TARGET local_install POST_BUILD
277-
COMMAND ${CMAKE_COMMAND} -E make_directory
278-
${LOCAL_INSTALL_DIR}/${data_file_dir}
279-
COMMAND ${CMAKE_COMMAND} -E copy_if_different
280-
${CMAKE_SOURCE_DIR}/torch-ext/${PACKAGE_NAME}/${data_file}
281-
${LOCAL_INSTALL_DIR}/${data_file_dir}/
282-
COMMENT "Copying ${data_file} to ${LOCAL_INSTALL_DIR}/${data_file_dir}"
283-
)
284-
endforeach()
285-
endforeach()
286-
287270
# Create both directories: variant dir for metadata.json, package dir for binaries
288271
file(MAKE_DIRECTORY ${VARIANT_DIR})
289272
file(MAKE_DIRECTORY ${LOCAL_INSTALL_DIR})

0 commit comments

Comments
 (0)