Skip to content

Commit

Permalink
Refactor hello_imgui_assets.cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Dec 30, 2023
1 parent c93e269 commit 82e49cf
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 97 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
108 changes: 12 additions & 96 deletions hello_imgui_cmake/assets/hello_imgui_assets.cmake
Original file line number Diff line number Diff line change
@@ -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 "")
Expand All @@ -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})
Expand Down Expand Up @@ -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()
11 changes: 11 additions & 0 deletions hello_imgui_cmake/assets/him_assets_android.cmake
Original file line number Diff line number Diff line change
@@ -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()
18 changes: 18 additions & 0 deletions hello_imgui_cmake/assets/him_assets_apple_bundle.cmake
Original file line number Diff line number Diff line change
@@ -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()
56 changes: 56 additions & 0 deletions hello_imgui_cmake/assets/him_assets_desktop.cmake
Original file line number Diff line number Diff line change
@@ -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()
12 changes: 12 additions & 0 deletions hello_imgui_cmake/assets/him_assets_emscripten.cmake
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 82e49cf

Please sign in to comment.