Skip to content

Commit

Permalink
Refactor hello_imgui_cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Dec 19, 2023
1 parent 193cff8 commit 0391540
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 324 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ option(HELLOIMGUI_USE_SDL_OPENGL3 "Build HelloImGui for SDL+OpenGL3" OFF)
# HELLOIMGUI_IMGUI_SOURCE_DIR: folder containing the sources for imgui (by default in the submodule external/imgui)
if (NOT DEFINED HELLOIMGUI_IMGUI_SOURCE_DIR)
set(HELLOIMGUI_IMGUI_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/external/imgui" CACHE STRING "Source dir of ImGui")
message(WARNING "Defined HELLOIMGUI_IMGUI_SOURCE_DIR=${HELLOIMGUI_IMGUI_SOURCE_DIR}")
endif()
# if HELLOIMGUI_BUILD_IMGUI, imgui will be built as part of the build of HelloImGui
if (NOT DEFINED HELLOIMGUI_BUILD_IMGUI)
Expand Down
4 changes: 0 additions & 4 deletions hello_imgui_cmake/android/hello_imgui_android.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
if (ANDROID)

function (hello_imgui_platform_customization app_name assets_location)
endfunction()

endif(ANDROID)
319 changes: 78 additions & 241 deletions hello_imgui_cmake/apple/hello_imgui_apple.cmake

Large diffs are not rendered by default.

89 changes: 89 additions & 0 deletions hello_imgui_cmake/apple/hello_imgui_ios.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
string(REGEX MATCH "SIMULATOR.*" IOS_IS_SIMULATOR ${PLATFORM})

function(hello_imgui_ios_set_dev_team app_name)
if (IOS_IS_SIMULATOR)
return()
endif()
set_target_properties( ${app_name} PROPERTIES
XCODE_ATTRIBUTE_DEVELOPMENT_TEAM ${CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM}
)
endfunction()


# Check CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM
function(hello_imgui_ios_check_development_team)
if (IOS_IS_SIMULATOR)
return()
endif()
if(NOT DEFINED CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM OR ${CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM} STREQUAL "123456789A")
if(DEFINED ENV{CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM})
set(CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM $ENV{CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM} CACHE STRING "iOS Devel Team id" FORCE)
else()
message(FATAL_ERROR "Please set CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM")
endif()
endif()
message(VERBOSE "CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM=${CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM}")
endfunction()

# Handle icons (and conversion if necessary)
function(hello_imgui_ios_add_icons app_name assets_location)
# Default HelloImGui icons
set(icons_assets_folder ${HELLOIMGUI_BASEPATH}/hello_imgui_cmake/apple/ios_icons)
set(found_custom_icon OFF)

# First possible custom icons folder, in ios/icons
set(custom_icons_assets_folder ${CMAKE_CURRENT_SOURCE_DIR}/ios/icons)
if ((NOT found_custom_icon) AND (IS_DIRECTORY ${custom_icons_assets_folder}))
message(STATUS "hello_imgui_ios_add_icons: found ios/icons specific icons folder for app ${app_name} ")
set(icons_assets_folder ${custom_icons_assets_folder})
set(found_custom_icon ON)
endif()

# Second possible custom icon, in assets/app_settings/icon.png
set(custom_app_png_icon ${assets_location}/app_settings/icon.png)
if ((NOT found_custom_icon) AND (EXISTS ${custom_app_png_icon}))
# find python program
find_program(PYTHON_EXECUTABLE NAMES python3 python)
if (NOT PYTHON_EXECUTABLE)
message(WARNING "
${app_name}: can't create iOS icon from ${custom_app_png_icon}
(did not find python)
This is not a fatal error, but the iOS app will not have the correct icon.
")
return()
endif()

# We need to convert icon.png to an icon folder
set(script_png_icon_to_icon_folder "${HELLOIMGUI_BASEPATH}/hello_imgui_cmake/apple/ios_png_icon_to_icon_folder.py")
message(STATUS "hello_imgui_ios_add_icons: converting ${custom_app_png_icon} to icon folder for app ${app_name} ")
set(custom_icons_assets_folder ${CMAKE_CURRENT_BINARY_DIR}/custom_icons)
execute_process(
COMMAND ${PYTHON_EXECUTABLE} ${script_png_icon_to_icon_folder} ${custom_app_png_icon} ${custom_icons_assets_folder}
RESULT_VARIABLE script_png_icon_to_icon_folder_result
)
if (NOT ${script_png_icon_to_icon_folder_result} EQUAL 0)
message(WARNING "
${app_name}: failed to create iOS icon from ${custom_app_icon}
Tried to run:
${PYTHON_EXECUTABLE} ${script_png_icon_to_icon_folder} ${custom_app_png_icon} ${custom_icons_assets_folder}
This is not a fatal error, but the app will not have a custom icon.
")
return()
endif()

set(icons_assets_folder ${custom_icons_assets_folder})
set(found_custom_icon ON)
endif()

hello_imgui_apple_bundle_add_files_from_folder(${app_name} ${icons_assets_folder} "")
endfunction()


function(hello_imgui_ios_copy_resources app_name assets_location)
# if assets_location/app_settings/apple/Resources/ios exists, copy it to the bundle Resources folder
set(custom_resources_folder ${assets_location}/app_settings/apple/Resources/ios)
if (EXISTS ${custom_resources_folder})
message(STATUS "hello_imgui_ios_copy_resources: found custom iOS resources folder ${custom_resources_folder} for app ${app_name} ")
hello_imgui_apple_bundle_add_files_from_folder(${app_name} ${custom_resources_folder} "Resources")
endif()
endfunction()
61 changes: 61 additions & 0 deletions hello_imgui_cmake/apple/hello_imgui_macos.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
########################################################
# Specific functions for MACOSX
########################################################
# Handle icons (and conversion if necessary)
function(hello_imgui_macos_add_icons app_name assets_location)
# Default HelloImGui icon
set(app_icon ${HELLOIMGUI_BASEPATH}/hello_imgui_cmake/apple/mac_icons/icon.icns)
set(found_custom_icon OFF)

# First possible custom icon in macos/icon.icns
set(custom_app_icon ${CMAKE_CURRENT_SOURCE_DIR}/macos/${HELLO_IMGUI_BUNDLE_ICON_FILE})
if ((NOT found_custom_icon) AND (EXISTS ${custom_app_icon}))
set(app_icon ${custom_app_icon})
set(found_custom_icon ON)
message(STATUS "hello_imgui_macos_add_icons: found app specific icon at ${custom_app_icon} for app ${app_name} ")
endif()

# Second possible custom icon, in assets/app_settings/icon.png (which we need to convert to icon.icns)
set(custom_app_png_icon ${assets_location}/app_settings/icon.png)
if ((NOT found_custom_icon) AND (EXISTS ${custom_app_png_icon}))
# find python program
find_program(PYTHON_EXECUTABLE NAMES python3 python)
if (NOT PYTHON_EXECUTABLE)
message(WARNING "
${app_name}: can't create a macOS icons from ${custom_app_png_icon}
(did not find python)
This is not a fatal error, but the app will not have a custom icon.
")
return()
endif()

# We need to convert icon.png to icon.icns
set(script_png_to_icns "${HELLOIMGUI_BASEPATH}/hello_imgui_cmake/apple/macos_png_icon_to_icns.py")
message(VERBOSE "hello_imgui_macos_add_icons: converting ${custom_app_png_icon} to icns for app ${app_name}")
set(custom_app_icon ${CMAKE_CURRENT_BINARY_DIR}/icon.icns)
execute_process(
COMMAND ${PYTHON_EXECUTABLE} ${script_png_to_icns} ${custom_app_png_icon} ${custom_app_icon}
RESULT_VARIABLE script_png_to_icns_result
)
if (NOT ${script_png_to_icns_result} EQUAL 0)
message(WARNING "
${app_name}: failed to create macOS icon from ${custom_app_icon}
Tried to run:
${PYTHON_EXECUTABLE} ${script_png_to_icns} ${custom_app_png_icon} ${custom_app_icon}
This is not a fatal error, but the app will not have a custom icon.
")
return()
endif()

set(app_icon ${custom_app_icon})
set(found_custom_icon ON)
message(VERBOSE "found custom icon at ${custom_app_icon} for app ${app_name} ")
endif()

# Add icon to target
target_sources(${app_name} PRIVATE ${app_icon})
set_source_files_properties(${app_icon}
PROPERTIES
MACOSX_PACKAGE_LOCATION "Resources"
)
endfunction()
4 changes: 0 additions & 4 deletions hello_imgui_cmake/emscripten/hello_imgui_emscripten.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
if (EMSCRIPTEN)

include(${CMAKE_CURRENT_LIST_DIR}/hello_imgui_emscripten_global_options.cmake)


Expand Down Expand Up @@ -127,5 +125,3 @@ function(hello_imgui_platform_customization app_name assets_location)
_hello_imgui_emscripten_target_compile_options(${app_name})
_hello_imgui_emscripten_add_js_css_resources(${app_name} ${assets_location})
endfunction()

endif (EMSCRIPTEN)
19 changes: 12 additions & 7 deletions hello_imgui_cmake/hello_imgui_add_app.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,18 @@ endfunction()
function(hello_imgui_platform_customization app_name assets_location)
endfunction()
# But it can be overriden by platform specific implementations, in the following files:
include(${HELLOIMGUI_CMAKE_PATH}/apple/hello_imgui_apple.cmake)
include(${HELLOIMGUI_CMAKE_PATH}/android/hello_imgui_android.cmake)
include(${HELLOIMGUI_CMAKE_PATH}/emscripten/hello_imgui_emscripten.cmake)
include(${HELLOIMGUI_CMAKE_PATH}/windows/hello_imgui_windows.cmake)
include(${HELLOIMGUI_CMAKE_PATH}/assets/hello_imgui_assets.cmake)

include(${HELLOIMGUI_CMAKE_PATH}/android/apkCMake/apkCMake.cmake)
if(APPLE)
include(${CMAKE_CURRENT_LIST_DIR}/apple/hello_imgui_apple.cmake)
elseif(ANDROID)
include(${CMAKE_CURRENT_LIST_DIR}/android/hello_imgui_android) # almost empty
include(${CMAKE_CURRENT_LIST_DIR}/android/apkCMake/apkCMake.cmake) # most of the logic is here and called by hello_imgui_prepare_app()
elseif (EMSCRIPTEN)
include(${CMAKE_CURRENT_LIST_DIR}/emscripten/hello_imgui_emscripten.cmake)
elseif (WIN32)
include(${CMAKE_CURRENT_LIST_DIR}/windows/hello_imgui_windows.cmake)
endif()

include(${CMAKE_CURRENT_LIST_DIR}/assets/hello_imgui_assets.cmake)


function(set_bundle_variables_defaults app_name)
Expand Down
127 changes: 60 additions & 67 deletions hello_imgui_cmake/windows/hello_imgui_windows.cmake
Original file line number Diff line number Diff line change
@@ -1,67 +1,60 @@
# if we are under windows
if (WIN32)

function(_hello_imgui_create_windows_ico assets_location)
set(custom_app_png_icon ${assets_location}/app_settings/icon.png)
if (NOT EXISTS ${custom_app_png_icon})
return()
endif()

# find python program
find_program(PYTHON_EXECUTABLE NAMES python3 python)
# if python is not found, we can't create the ico file
if (NOT PYTHON_EXECUTABLE)
message(WARNING "
${app_name}: can't create a windows ico file from ${custom_app_png_icon}
(did not find python)
This is not a fatal error, but the web app will not have a favicon.
")
return()
endif()

# We need to convert icon.png to a windows ico file
set(script_png_to_ico "${HELLOIMGUI_BASEPATH}/hello_imgui_cmake/windows/windows_png_icon_to_ico.py")
message(VERBOSE "_hello_imgui_create_ico: converting ${custom_app_png_icon} to windows ico for app ${app_name}")
set(custom_app_icon ${CMAKE_CURRENT_BINARY_DIR}/icon.ico)
execute_process(
COMMAND ${PYTHON_EXECUTABLE} ${script_png_to_ico} ${custom_app_png_icon} ${custom_app_icon}
RESULT_VARIABLE script_png_to_ico_result
)
if (NOT ${script_png_to_ico_result} EQUAL 0)
message(WARNING "
${app_name}: failed to create a Windows ico file from ${custom_app_icon}
Tried to run:
${PYTHON_EXECUTABLE} ${script_png_to_ico} ${custom_app_png_icon} ${custom_app_icon}
This is not a fatal error, but the app will not have a custom icon.
In order to have a custom icon, you need to have python with the Pillow package:
pip install Pillow
")
return()
endif()
endfunction()

function(_hello_imgui_add_windows_icon app_name assets_location)
_hello_imgui_create_windows_ico(${assets_location})
set(custom_app_icon ${CMAKE_CURRENT_BINARY_DIR}/icon.ico)
if (NOT EXISTS ${custom_app_icon})
return()
endif()

# Create rc file
set(icon_rc_file "${CMAKE_CURRENT_BINARY_DIR}/app_${app_name}.rc")
set(icon_rc_file_contents "IDI_ICON1 ICON DISCARDABLE \"${custom_app_icon}\"")
file(WRITE ${icon_rc_file} ${icon_rc_file_contents})

# Add the rc file to the executable
target_sources(${app_name} PRIVATE ${icon_rc_file})
endfunction()


function(hello_imgui_platform_customization app_name assets_location)
_hello_imgui_add_windows_icon(${app_name} ${assets_location})
endfunction()
endif(WIN32)



function(_hello_imgui_create_windows_ico assets_location)
set(custom_app_png_icon ${assets_location}/app_settings/icon.png)
if (NOT EXISTS ${custom_app_png_icon})
return()
endif()

# find python program
find_program(PYTHON_EXECUTABLE NAMES python3 python)
# if python is not found, we can't create the ico file
if (NOT PYTHON_EXECUTABLE)
message(WARNING "
${app_name}: can't create a windows ico file from ${custom_app_png_icon}
(did not find python)
This is not a fatal error, but the web app will not have a favicon.
")
return()
endif()

# We need to convert icon.png to a windows ico file
set(script_png_to_ico "${HELLOIMGUI_BASEPATH}/hello_imgui_cmake/windows/windows_png_icon_to_ico.py")
message(VERBOSE "_hello_imgui_create_ico: converting ${custom_app_png_icon} to windows ico for app ${app_name}")
set(custom_app_icon ${CMAKE_CURRENT_BINARY_DIR}/icon.ico)
execute_process(
COMMAND ${PYTHON_EXECUTABLE} ${script_png_to_ico} ${custom_app_png_icon} ${custom_app_icon}
RESULT_VARIABLE script_png_to_ico_result
)
if (NOT ${script_png_to_ico_result} EQUAL 0)
message(WARNING "
${app_name}: failed to create a Windows ico file from ${custom_app_icon}
Tried to run:
${PYTHON_EXECUTABLE} ${script_png_to_ico} ${custom_app_png_icon} ${custom_app_icon}
This is not a fatal error, but the app will not have a custom icon.
In order to have a custom icon, you need to have python with the Pillow package:
pip install Pillow
")
return()
endif()
endfunction()

function(_hello_imgui_add_windows_icon app_name assets_location)
_hello_imgui_create_windows_ico(${assets_location})
set(custom_app_icon ${CMAKE_CURRENT_BINARY_DIR}/icon.ico)
if (NOT EXISTS ${custom_app_icon})
return()
endif()

# Create rc file
set(icon_rc_file "${CMAKE_CURRENT_BINARY_DIR}/app_${app_name}.rc")
set(icon_rc_file_contents "IDI_ICON1 ICON DISCARDABLE \"${custom_app_icon}\"")
file(WRITE ${icon_rc_file} ${icon_rc_file_contents})

# Add the rc file to the executable
target_sources(${app_name} PRIVATE ${icon_rc_file})
endfunction()


function(hello_imgui_platform_customization app_name assets_location)
_hello_imgui_add_windows_icon(${app_name} ${assets_location})
endfunction()

0 comments on commit 0391540

Please sign in to comment.