Skip to content

Commit

Permalink
[cmake] add basic packaging support. for testing reasons, tz_io_image…
Browse files Browse the repository at this point in the history
…_test can now be built as a package via ninja tz_io_image_test.package. it works pretty well!
  • Loading branch information
harrand committed Oct 21, 2024
1 parent e770c6d commit 3628965
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ setup_platform()
include(cmake/render_api.cmake)
setup_render_api()
include(cmake/bundle.cmake)
include(cmake/package.cmake)

###################
## TOPAZ - TESTS ##
Expand All @@ -76,4 +77,4 @@ add_subdirectory(test)
## TOPAZ - DEMOS ##
###################

add_subdirectory(demo)
add_subdirectory(demo)
2 changes: 2 additions & 0 deletions cmake/bundle.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
define_property(TARGET PROPERTY TOPAZ_BUNDLE_DEPENDENCIES)
function(topaz_bundle_files)
cmake_parse_arguments(
TOPAZ_BUNDLE_FILES
Expand All @@ -9,6 +10,7 @@ function(topaz_bundle_files)

# source dir: CMAKE_CURRENT_SOURCE_DIR
# Loop over each file and replicate the directory structure in the binary directory
set_target_properties(${TOPAZ_BUNDLE_FILES_TARGET} PROPERTIES TOPAZ_BUNDLE_DEPENDENCIES "${TOPAZ_BUNDLE_FILES_FILES}")
set(counter 0)
foreach(file IN LISTS TOPAZ_BUNDLE_FILES_FILES)
# Ensure the file exists
Expand Down
48 changes: 48 additions & 0 deletions cmake/package.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
set(TOPAZ_PACKAGE_BASE_DIR "${PROJECT_BINARY_DIR}/packages")
function(topaz_define_package)
cmake_parse_arguments(
TOPAZ_DEFINE_PACKAGE
""
"TARGET"
""
${ARGN}
)

set(dependency_list "")
set(package_dir ${TOPAZ_PACKAGE_BASE_DIR}/${TOPAZ_DEFINE_PACKAGE_TARGET}.package)

get_target_property(bundle_deps ${TOPAZ_DEFINE_PACKAGE_TARGET} TOPAZ_BUNDLE_DEPENDENCIES)
# Create commands to copy all the bundled files to the package dir.
if(NOT ${bundle_deps} STREQUAL "bundle_deps-NOTFOUND")
foreach(bundle_file IN LISTS bundle_deps)
set(bundle_file_path $<TARGET_FILE_DIR:${TOPAZ_DEFINE_PACKAGE_TARGET}>/${bundle_file})
add_custom_command(
OUTPUT "${package_dir}/${bundle_file}"
DEPENDS ${bundle_file_path}
COMMAND ${CMAKE_COMMAND} -E copy "${bundle_file_path}" "${package_dir}/${bundle_file}"
COMMENT "Copying bundled file ${bundle_file} into ${TOPAZ_DEFINE_PACKAGE_TARGET}.package"
)
list(APPEND dependency_list "${package_dir}/${bundle_file}")
endforeach()
endif()
# Make tar.package which copies the bundled files
add_custom_target(${TOPAZ_DEFINE_PACKAGE_TARGET}.package
ALL
COMMAND ${CMAKE_COMMAND} -E make_directory ${package_dir}
DEPENDS ${TOPAZ_DEFINE_PACKAGE_TARGET} ${dependency_list}
)
# And also to copy over the executable/library.
add_custom_command(TARGET ${TOPAZ_DEFINE_PACKAGE_TARGET}.package
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${TOPAZ_DEFINE_PACKAGE_TARGET}> "${package_dir}"
COMMAND_EXPAND_LISTS
)
# And finally, all dll dependencies
# why use -t here when its not used anywhere else?
# well TARGET_RUNTIME_DLLS could may well yield an empty list. copy without -t is malformed if you try to copy 0 things, but with -t its much better.
add_custom_command(TARGET ${TOPAZ_DEFINE_PACKAGE_TARGET}.package
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy -t "${package_dir}" $<TARGET_RUNTIME_DLLS:${TOPAZ_DEFINE_PACKAGE_TARGET}>
COMMAND_EXPAND_LISTS
)
endfunction()
1 change: 1 addition & 0 deletions test/tz/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ topaz_bundle_files(
FILES
files/img.png
)
topaz_define_package(TARGET tz_io_image_test)

topaz_add_test(
TARGET tz_vector_test
Expand Down

0 comments on commit 3628965

Please sign in to comment.