From 82e49cf9e74b8dd9c06fb9f42818532c76a981a1 Mon Sep 17 00:00:00 2001 From: Pascal Thomet Date: Sat, 30 Dec 2023 12:33:26 +0100 Subject: [PATCH] Refactor hello_imgui_assets.cmake --- CMakeLists.txt | 7 +- .../assets/hello_imgui_assets.cmake | 108 ++---------------- .../assets/him_assets_android.cmake | 11 ++ .../assets/him_assets_apple_bundle.cmake | 18 +++ .../assets/him_assets_desktop.cmake | 56 +++++++++ .../assets/him_assets_emscripten.cmake | 12 ++ 6 files changed, 115 insertions(+), 97 deletions(-) create mode 100644 hello_imgui_cmake/assets/him_assets_android.cmake create mode 100644 hello_imgui_cmake/assets/him_assets_apple_bundle.cmake create mode 100644 hello_imgui_cmake/assets/him_assets_desktop.cmake create mode 100644 hello_imgui_cmake/assets/him_assets_emscripten.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index af617b57..2eee3fbb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,11 @@ include(${CMAKE_CURRENT_LIST_DIR}/hello_imgui_cmake/utils/cache_hello_imgui_path # Make cmake function `hello_imgui_add_app` available include(hello_imgui_add_app) +# HELLOIMGUI_ADD_APP_WITH_INSTALL: if ON, hello_imgui_add_app will add install() instructions for the app. +# On desktop platforms, it will install the "app exe" and the "assets" folder in the install folder. +# (this works under Windows, Linux; and macOS if HELLOIMGUI_MACOS_NO_BUNDLE is ON) +option(HELLOIMGUI_ADD_APP_WITH_INSTALL "Add cmake install() instructions with hello_imgui_add_app" ON) + ############################################################################### # HelloImGui Build options @@ -127,7 +132,7 @@ option(HELLOIMGUI_WITH_TEST_ENGINE "Provide ImGui Test engine" OFF) message(STATUS "HELLOIMGUI_WITH_TEST_ENGINE=${HELLOIMGUI_WITH_TEST_ENGINE}") #------------------------------------------------------------------------------ -# Instructions: how to enable Esmcripten multihtread support +# Instructions: how to enable Emscripten multithreading support #------------------------------------------------------------------------------ if (EMSCRIPTEN) # 1. set HELLOIMGUI_EMSCRIPTEN_PTHREAD to ON to enable support for multithreading diff --git a/hello_imgui_cmake/assets/hello_imgui_assets.cmake b/hello_imgui_cmake/assets/hello_imgui_assets.cmake index ba952032..11f53acc 100644 --- a/hello_imgui_cmake/assets/hello_imgui_assets.cmake +++ b/hello_imgui_cmake/assets/hello_imgui_assets.cmake @@ -1,3 +1,15 @@ +# Platform dependent definition of hello_imgui_bundle_assets_from_folder +if (EMSCRIPTEN) + include(${CMAKE_CURRENT_LIST_DIR}/him_assets_emscripten.cmake) +elseif(IOS OR (MACOSX AND (NOT HELLOIMGUI_MACOS_NO_BUNDLE))) + include(${CMAKE_CURRENT_LIST_DIR}/him_assets_apple_bundle.cmake) +elseif(ANDROID) + include(${CMAKE_CURRENT_LIST_DIR}/him_assets_android.cmake) +else() + include(${CMAKE_CURRENT_LIST_DIR}/him_assets_desktop.cmake) +endif() + + function(hello_imgui_file_glob_recurse_relative out_file_list folder) FILE(GLOB_RECURSE files_fullpath ${folder}/*) set(files_relativepath "") @@ -9,95 +21,6 @@ function(hello_imgui_file_glob_recurse_relative out_file_list folder) endfunction() -if (EMSCRIPTEN) - - # Bundle assets / emscripten version - function(hello_imgui_bundle_assets_from_folder app_name assets_folder) - if (IS_DIRECTORY ${assets_folder}) - target_link_options( - ${app_name} - PRIVATE - "SHELL:--preload-file ${assets_folder}@/" - ) - else() - message(WARNING "hello_imgui_bundle_assets_from_folder: ignoring missing folder ${assets_folder}") - endif() - endfunction() - -elseif(IOS OR (MACOSX AND (NOT HELLOIMGUI_MACOS_NO_BUNDLE))) - - # Bundle assets / macOS and iOS app version - function(hello_imgui_apple_bundle_add_files_from_folder app_name assets_folder location_in_bundle) - file(GLOB_RECURSE assets ${assets_folder}/*.*) - target_sources(${app_name} PRIVATE ${assets}) - foreach(asset ${assets}) - file(RELATIVE_PATH asset_relative ${assets_folder} ${asset}) - get_filename_component(asset_dir ${asset_relative} DIRECTORY) - set_source_files_properties ( - ${asset} - PROPERTIES - MACOSX_PACKAGE_LOCATION ${location_in_bundle}/${asset_dir} - ) - endforeach() - endfunction() - - function(hello_imgui_bundle_assets_from_folder app_name assets_folder) - hello_imgui_apple_bundle_add_files_from_folder(${app_name} ${assets_folder} "Resources/assets") - endfunction() - -elseif(ANDROID) - - function(hello_imgui_bundle_assets_from_folder app_name assets_folder) - message(VERBOSE "hello_imgui_bundle_assets_from_folder ${app_name} ${assets_folder}") - FILE(GLOB children ${assets_folder}/*) - if (DEFINED HELLO_IMGUI_ANDROID_ASSETS_FOLDER) - foreach(child ${children}) - message(VERBOSE " Copying ${child}") - FILE(COPY ${child} DESTINATION ${HELLO_IMGUI_ANDROID_ASSETS_FOLDER}) - endforeach() - endif() - endfunction() - - -else() - function(hello_imgui_get_real_output_directory app_name r) - # Warning: RUNTIME_OUTPUT_DIRECTORY is stable, but RUNTIME_OUTPUT_DIRECTORY_CONFIG can vary between Debug/Release configs - # cf https://cmake.org/cmake/help/latest/prop_tgt/RUNTIME_OUTPUT_DIRECTORY_CONFIG.html - get_property(runtime_output_directory TARGET ${app_name} PROPERTY RUNTIME_OUTPUT_DIRECTORY) - if ("${runtime_output_directory}" STREQUAL "") - set(${r} ${CMAKE_CURRENT_BINARY_DIR} PARENT_SCOPE) - else() - set(${r} ${runtime_output_directory} PARENT_SCOPE) - endif() - endfunction() - - function(_do_copy_asset app_name src dst) - hello_imgui_get_real_output_directory(${app_name} real_output_directory) - - FILE(COPY "${src}" DESTINATION "${real_output_directory}/${dst}") - message(VERBOSE "_do_copy_asset=> FILE(COPY ${src} DESTINATION ${real_output_directory}/${dst})") - - if (IS_DIRECTORY ${src}) - install(DIRECTORY "${src}" DESTINATION "${CMAKE_INSTALL_PREFIX}/${dst}") - message(VERBOSE "_do_copy_asset=> install(DIRECTORY ${src} DESTINATION ${CMAKE_INSTALL_PREFIX}/${dst} )") - else() - install(FILES "${src}" DESTINATION "${CMAKE_INSTALL_PREFIX}/${dst}") - message(VERBOSE "_do_copy_asset=> install(FILES ${src} DESTINATION ${CMAKE_INSTALL_PREFIX}/${dst} )") - endif() - endfunction() - - # Bundle assets - function(hello_imgui_bundle_assets_from_folder app_name assets_folder) - message(VERBOSE "hello_imgui_bundle_assets_from_folder ${app_name} ${assets_folder}") - FILE(GLOB children ${assets_folder}/*) - foreach(child ${children}) - _do_copy_asset(${app_name} ${child} assets/) - endforeach() - endfunction() - -endif() - - function(hello_imgui_copy_folder1_files_missing_from_folder2 folder_src_1 folder_src_2 folder_dst) if (NOT EXISTS ${folder_dst}) file(MAKE_DIRECTORY ${folder_dst}) @@ -129,11 +52,4 @@ function(hello_imgui_bundle_assets app_name assets_location) message(VERBOSE "hello_imgui_bundle_assets: ${app_name} found local assets") hello_imgui_bundle_assets_from_folder(${app_name} ${local_assets_folder}) endif() - - if (WIN32) - # Fix msvc quirk: set the debugger working dir to the exe dir! - hello_imgui_get_real_output_directory(${app_name} app_output_dir) - set_target_properties(${app_name} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${app_output_dir}) - endif() - endfunction() diff --git a/hello_imgui_cmake/assets/him_assets_android.cmake b/hello_imgui_cmake/assets/him_assets_android.cmake new file mode 100644 index 00000000..768e2ee2 --- /dev/null +++ b/hello_imgui_cmake/assets/him_assets_android.cmake @@ -0,0 +1,11 @@ +# hello_imgui_bundle_assets_from_folder for Android +function(hello_imgui_bundle_assets_from_folder app_name assets_folder) + message(VERBOSE "hello_imgui_bundle_assets_from_folder ${app_name} ${assets_folder}") + FILE(GLOB children ${assets_folder}/*) + if (DEFINED HELLO_IMGUI_ANDROID_ASSETS_FOLDER) + foreach(child ${children}) + message(VERBOSE " Copying ${child}") + FILE(COPY ${child} DESTINATION ${HELLO_IMGUI_ANDROID_ASSETS_FOLDER}) + endforeach() + endif() +endfunction() diff --git a/hello_imgui_cmake/assets/him_assets_apple_bundle.cmake b/hello_imgui_cmake/assets/him_assets_apple_bundle.cmake new file mode 100644 index 00000000..96144bd6 --- /dev/null +++ b/hello_imgui_cmake/assets/him_assets_apple_bundle.cmake @@ -0,0 +1,18 @@ +# Bundle assets / macOS and iOS app version +function(hello_imgui_apple_bundle_add_files_from_folder app_name assets_folder location_in_bundle) + file(GLOB_RECURSE assets ${assets_folder}/*.*) + target_sources(${app_name} PRIVATE ${assets}) + foreach(asset ${assets}) + file(RELATIVE_PATH asset_relative ${assets_folder} ${asset}) + get_filename_component(asset_dir ${asset_relative} DIRECTORY) + set_source_files_properties ( + ${asset} + PROPERTIES + MACOSX_PACKAGE_LOCATION ${location_in_bundle}/${asset_dir} + ) + endforeach() +endfunction() + +function(hello_imgui_bundle_assets_from_folder app_name assets_folder) + hello_imgui_apple_bundle_add_files_from_folder(${app_name} ${assets_folder} "Resources/assets") +endfunction() diff --git a/hello_imgui_cmake/assets/him_assets_desktop.cmake b/hello_imgui_cmake/assets/him_assets_desktop.cmake new file mode 100644 index 00000000..fc729d0c --- /dev/null +++ b/hello_imgui_cmake/assets/him_assets_desktop.cmake @@ -0,0 +1,56 @@ +# hello_imgui_bundle_assets_from_folder for Desktop platforms (Windows, Linux, macOS when not using app bundles) +# +# In this case, we will: +# - copy the assets folder to the output directory (so that we can run the app without install) +# - install the assets folder and the app exe to the install directory +# - set the debugger working directory to the output directory (for Win32) + +function(hello_imgui_get_real_output_directory app_name r) + # Warning: RUNTIME_OUTPUT_DIRECTORY is stable, but RUNTIME_OUTPUT_DIRECTORY_CONFIG can vary between Debug/Release configs + # cf https://cmake.org/cmake/help/latest/prop_tgt/RUNTIME_OUTPUT_DIRECTORY_CONFIG.html + get_property(runtime_output_directory TARGET ${app_name} PROPERTY RUNTIME_OUTPUT_DIRECTORY) + if ("${runtime_output_directory}" STREQUAL "") + set(${r} ${CMAKE_CURRENT_BINARY_DIR} PARENT_SCOPE) + else() + set(${r} ${runtime_output_directory} PARENT_SCOPE) + endif() +endfunction() + + +function(_do_copy_asset app_name src dst) + hello_imgui_get_real_output_directory(${app_name} real_output_directory) + + FILE(COPY "${src}" DESTINATION "${real_output_directory}/${dst}") + message(VERBOSE "_do_copy_asset=> FILE(COPY ${src} DESTINATION ${real_output_directory}/${dst})") + + if (HELLOIMGUI_ADD_APP_WITH_INSTALL) + if (IS_DIRECTORY ${src}) + install(DIRECTORY "${src}" DESTINATION "${CMAKE_INSTALL_PREFIX}/${dst}") + message(VERBOSE "_do_copy_asset=> install(DIRECTORY ${src} DESTINATION ${CMAKE_INSTALL_PREFIX}/${dst} )") + else() + install(FILES "${src}" DESTINATION "${CMAKE_INSTALL_PREFIX}/${dst}") + message(VERBOSE "_do_copy_asset=> install(FILES ${src} DESTINATION ${CMAKE_INSTALL_PREFIX}/${dst} )") + endif() + endif() +endfunction() + + +# Bundle assets +function(hello_imgui_bundle_assets_from_folder app_name assets_folder) + message(VERBOSE "hello_imgui_bundle_assets_from_folder ${app_name} ${assets_folder}") + FILE(GLOB children ${assets_folder}/*) + foreach(child ${children}) + _do_copy_asset(${app_name} ${child} assets/) + endforeach() + + if (WIN32) + # Fix msvc quirk: set the debugger working dir to the exe dir! + hello_imgui_get_real_output_directory(${app_name} app_output_dir) + set_target_properties(${app_name} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${app_output_dir}) + endif() + + # Install app exe to install directory + if (HELLOIMGUI_ADD_APP_WITH_INSTALL) + install(TARGETS ${app_name} DESTINATION ${CMAKE_INSTALL_PREFIX}) + endif() +endfunction() diff --git a/hello_imgui_cmake/assets/him_assets_emscripten.cmake b/hello_imgui_cmake/assets/him_assets_emscripten.cmake new file mode 100644 index 00000000..8b30510b --- /dev/null +++ b/hello_imgui_cmake/assets/him_assets_emscripten.cmake @@ -0,0 +1,12 @@ +# Bundle assets / emscripten version +function(hello_imgui_bundle_assets_from_folder app_name assets_folder) + if (IS_DIRECTORY ${assets_folder}) + target_link_options( + ${app_name} + PRIVATE + "SHELL:--preload-file ${assets_folder}@/" + ) + else() + message(WARNING "hello_imgui_bundle_assets_from_folder: ignoring missing folder ${assets_folder}") + endif() +endfunction()