From 3628965dd804b1fea983b7a074a62bbc9088b4c3 Mon Sep 17 00:00:00 2001 From: Harrand Date: Tue, 22 Oct 2024 00:18:58 +0100 Subject: [PATCH] [cmake] add basic packaging support. for testing reasons, tz_io_image_test can now be built as a package via ninja tz_io_image_test.package. it works pretty well! --- CMakeLists.txt | 3 ++- cmake/bundle.cmake | 2 ++ cmake/package.cmake | 48 ++++++++++++++++++++++++++++++++++++++++++ test/tz/CMakeLists.txt | 1 + 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 cmake/package.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index c5883688fd..ec52a4913f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,6 +64,7 @@ setup_platform() include(cmake/render_api.cmake) setup_render_api() include(cmake/bundle.cmake) +include(cmake/package.cmake) ################### ## TOPAZ - TESTS ## @@ -76,4 +77,4 @@ add_subdirectory(test) ## TOPAZ - DEMOS ## ################### -add_subdirectory(demo) +add_subdirectory(demo) \ No newline at end of file diff --git a/cmake/bundle.cmake b/cmake/bundle.cmake index 08bf3c1e2e..7efabcacff 100644 --- a/cmake/bundle.cmake +++ b/cmake/bundle.cmake @@ -1,3 +1,4 @@ +define_property(TARGET PROPERTY TOPAZ_BUNDLE_DEPENDENCIES) function(topaz_bundle_files) cmake_parse_arguments( TOPAZ_BUNDLE_FILES @@ -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 diff --git a/cmake/package.cmake b/cmake/package.cmake new file mode 100644 index 0000000000..a5a53d349f --- /dev/null +++ b/cmake/package.cmake @@ -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 $/${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 $ "${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}" $ + COMMAND_EXPAND_LISTS + ) +endfunction() \ No newline at end of file diff --git a/test/tz/CMakeLists.txt b/test/tz/CMakeLists.txt index 6121af9976..d10a2c75eb 100644 --- a/test/tz/CMakeLists.txt +++ b/test/tz/CMakeLists.txt @@ -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