From a57b127c198fb7c6ab0ba0157a9a968a5ed96ffb Mon Sep 17 00:00:00 2001 From: Pascal Thomet Date: Thu, 19 Dec 2024 12:54:06 +0100 Subject: [PATCH] Separate imgui_bundle_add_demo.cmake --- .../imgui_bundle/demos_cpp/CMakeLists.txt | 80 +------------------ .../demos_cpp/demos_imguizmo/CMakeLists.txt | 4 +- .../demos_cpp/demos_immapp/CMakeLists.txt | 2 +- .../demos_cpp/demos_immvision/CMakeLists.txt | 4 +- .../demos_cpp/demos_nanovg/CMakeLists.txt | 4 +- .../demos_node_editor/CMakeLists.txt | 4 +- .../demos_tex_inspect/CMakeLists.txt | 4 +- .../demos_cpp/sandbox/CMakeLists.txt | 2 +- .../imgui_bundle_add_demo.cmake | 76 ++++++++++++++++++ 9 files changed, 91 insertions(+), 89 deletions(-) create mode 100644 imgui_bundle_cmake/imgui_bundle_add_demo.cmake diff --git a/bindings/imgui_bundle/demos_cpp/CMakeLists.txt b/bindings/imgui_bundle/demos_cpp/CMakeLists.txt index 5fe9c10c..30ec5350 100644 --- a/bindings/imgui_bundle/demos_cpp/CMakeLists.txt +++ b/bindings/imgui_bundle/demos_cpp/CMakeLists.txt @@ -1,85 +1,11 @@ set(CMAKE_CXX_STANDARD 20) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +include(${IMGUI_BUNDLE_PATH}/imgui_bundle_cmake/imgui_bundle_add_demo.cmake) include_directories(.) set(demos_cpp_folder ${CMAKE_CURRENT_LIST_DIR}) -function(add_demo_cpp) - # Creates an app using imgui_bundle_add_app, and link it with demo_utils - # Usage: - # add_demo_cpp(demo_name demo_file_1.cpp demo_file_2.cpp) - set(args ${ARGN}) - list(GET args 0 demo_name) - set(demos_asset_folder ${IMGUI_BUNDLE_PATH}/bindings/imgui_bundle/demos_assets) - imgui_bundle_add_app(${args} ASSETS_LOCATION ${demos_asset_folder}) - if(MSVC) - hello_imgui_msvc_target_set_folder(${demo_name} demos_cpp) - endif() - target_link_libraries(${demo_name} PUBLIC demo_utils) - if (EMSCRIPTEN) - # Bundle demos_assets - hello_imgui_bundle_assets_from_folder(${demo_name} ${demos_cpp_folder}/../demos_assets) - endif() -endfunction() - -function(add_single_file_demo_cpp demo_cpp_file) - # Creates a demo app - # Usage: - # add_single_file_demo_cpp(demo_file.cpp) - get_filename_component(demo_name "${demo_cpp_file}" NAME_WE) - add_demo_cpp(${demo_name} ${demo_cpp_file}) -endfunction() - -function(add_auto_demo demo_cpp_file) - # Creates a demo app, and provides main() automatically - # Usage: - # add_auto_demo(demo_file.cpp) - # (demo_file.cpp should provide a function named according to the containing file, e.g. `void demo_file()`) - get_filename_component(demo_cpp_file "${demo_cpp_file}" NAME) - get_filename_component(demo_name "${demo_cpp_file}" NAME_WE) - add_demo_cpp(${demo_name} ${demo_cpp_file}) - - # Read the contents of the cpp file - file(READ "${demo_cpp_file}" demo_cpp_file_content) - - # If the cpp file does not contain a main function, provide it - if (NOT "${demo_cpp_file_content}" MATCHES "int main") - message(STATUS "${demo_cpp_file} does not contain a main function, adding it!") - set(main_file ${CMAKE_CURRENT_BINARY_DIR}/${demo_cpp_file}.autogenerated.cpp) - configure_file(${demos_cpp_folder}/_auto_main/_auto_main.cpp.in ${main_file}) - target_sources(${demo_name} PRIVATE ${main_file}) - endif() - -endfunction() - -function(add_this_folder_demos) - # Creates demo apps for all cpp files in the current folder - FILE(GLOB demo_cpp_files *.cpp) - FOREACH(demo_cpp_file ${demo_cpp_files}) - add_single_file_demo_cpp(${demo_cpp_file}) - ENDFOREACH() -endfunction() - -function(add_this_folder_auto_demos) - # Creates demo apps for all cpp files in the current folder, and provide a main for them - FILE(GLOB demo_cpp_files *.cpp) - FOREACH(demo_cpp_file ${demo_cpp_files}) - add_auto_demo(${demo_cpp_file}) - ENDFOREACH() -endfunction() - - -function(add_this_folder_as_demos_library) - file(GLOB sources *.cpp) - get_filename_component(folder_name "${CMAKE_CURRENT_LIST_DIR}" NAME_WE) - set(demos_library_name ${folder_name}) - add_library(${demos_library_name} ${sources}) - target_link_libraries(${demos_library_name} PRIVATE imgui_bundle) - target_compile_definitions(${demos_library_name} PRIVATE IMGUI_BUNDLE_BUILD_DEMO_AS_LIBRARY) -endfunction() - - add_subdirectory(demo_utils) add_subdirectory(demos_immapp) add_subdirectory(demos_node_editor) @@ -93,8 +19,8 @@ if (IMGUI_BUNDLE_WITH_IMMVISION) endif() add_subdirectory(sandbox) -add_this_folder_auto_demos() -add_this_folder_as_demos_library() +ibd_add_this_folder_auto_demos() +ibd_add_this_folder_as_demos_library() if (EMSCRIPTEN) set(demos_python_folder ${demos_cpp_folder}/../demos_python) diff --git a/bindings/imgui_bundle/demos_cpp/demos_imguizmo/CMakeLists.txt b/bindings/imgui_bundle/demos_cpp/demos_imguizmo/CMakeLists.txt index ece4a3a6..a2f29418 100644 --- a/bindings/imgui_bundle/demos_cpp/demos_imguizmo/CMakeLists.txt +++ b/bindings/imgui_bundle/demos_cpp/demos_imguizmo/CMakeLists.txt @@ -1,5 +1,5 @@ # Add one demo application per file -add_this_folder_auto_demos() +ibd_add_this_folder_auto_demos() # Also add these demos as a library (where IMGUI_BUNDLE_BUILD_DEMO_AS_LIBRARY will disable existing main() functions) -add_this_folder_as_demos_library() +ibd_add_this_folder_as_demos_library() diff --git a/bindings/imgui_bundle/demos_cpp/demos_immapp/CMakeLists.txt b/bindings/imgui_bundle/demos_cpp/demos_immapp/CMakeLists.txt index 726b8a80..841a8b24 100644 --- a/bindings/imgui_bundle/demos_cpp/demos_immapp/CMakeLists.txt +++ b/bindings/imgui_bundle/demos_cpp/demos_immapp/CMakeLists.txt @@ -1,4 +1,4 @@ -add_this_folder_demos() +ibd_add_this_folder_demos() if(EMSCRIPTEN) target_link_libraries(imgui_example_glfw_opengl3 PRIVATE GL) else() diff --git a/bindings/imgui_bundle/demos_cpp/demos_immvision/CMakeLists.txt b/bindings/imgui_bundle/demos_cpp/demos_immvision/CMakeLists.txt index ece4a3a6..a2f29418 100644 --- a/bindings/imgui_bundle/demos_cpp/demos_immvision/CMakeLists.txt +++ b/bindings/imgui_bundle/demos_cpp/demos_immvision/CMakeLists.txt @@ -1,5 +1,5 @@ # Add one demo application per file -add_this_folder_auto_demos() +ibd_add_this_folder_auto_demos() # Also add these demos as a library (where IMGUI_BUNDLE_BUILD_DEMO_AS_LIBRARY will disable existing main() functions) -add_this_folder_as_demos_library() +ibd_add_this_folder_as_demos_library() diff --git a/bindings/imgui_bundle/demos_cpp/demos_nanovg/CMakeLists.txt b/bindings/imgui_bundle/demos_cpp/demos_nanovg/CMakeLists.txt index 2e9aba88..b1efede2 100644 --- a/bindings/imgui_bundle/demos_cpp/demos_nanovg/CMakeLists.txt +++ b/bindings/imgui_bundle/demos_cpp/demos_nanovg/CMakeLists.txt @@ -1,7 +1,7 @@ # Add one demo application per file -add_this_folder_auto_demos() +ibd_add_this_folder_auto_demos() # Also add these demos as a library (where IMGUI_BUNDLE_BUILD_DEMO_AS_LIBRARY will disable existing main() functions) -add_this_folder_as_demos_library() +ibd_add_this_folder_as_demos_library() target_sources(demo_nanovg_full PRIVATE demo_nanovg_full/demo_nanovg_full_impl.cpp demo_nanovg_full/demo_nanovg_full_impl.h) diff --git a/bindings/imgui_bundle/demos_cpp/demos_node_editor/CMakeLists.txt b/bindings/imgui_bundle/demos_cpp/demos_node_editor/CMakeLists.txt index ece4a3a6..a2f29418 100644 --- a/bindings/imgui_bundle/demos_cpp/demos_node_editor/CMakeLists.txt +++ b/bindings/imgui_bundle/demos_cpp/demos_node_editor/CMakeLists.txt @@ -1,5 +1,5 @@ # Add one demo application per file -add_this_folder_auto_demos() +ibd_add_this_folder_auto_demos() # Also add these demos as a library (where IMGUI_BUNDLE_BUILD_DEMO_AS_LIBRARY will disable existing main() functions) -add_this_folder_as_demos_library() +ibd_add_this_folder_as_demos_library() diff --git a/bindings/imgui_bundle/demos_cpp/demos_tex_inspect/CMakeLists.txt b/bindings/imgui_bundle/demos_cpp/demos_tex_inspect/CMakeLists.txt index ece4a3a6..a2f29418 100644 --- a/bindings/imgui_bundle/demos_cpp/demos_tex_inspect/CMakeLists.txt +++ b/bindings/imgui_bundle/demos_cpp/demos_tex_inspect/CMakeLists.txt @@ -1,5 +1,5 @@ # Add one demo application per file -add_this_folder_auto_demos() +ibd_add_this_folder_auto_demos() # Also add these demos as a library (where IMGUI_BUNDLE_BUILD_DEMO_AS_LIBRARY will disable existing main() functions) -add_this_folder_as_demos_library() +ibd_add_this_folder_as_demos_library() diff --git a/bindings/imgui_bundle/demos_cpp/sandbox/CMakeLists.txt b/bindings/imgui_bundle/demos_cpp/sandbox/CMakeLists.txt index 2d82a68e..34d5e288 100644 --- a/bindings/imgui_bundle/demos_cpp/sandbox/CMakeLists.txt +++ b/bindings/imgui_bundle/demos_cpp/sandbox/CMakeLists.txt @@ -1 +1 @@ -add_this_folder_auto_demos() +ibd_add_this_folder_auto_demos() diff --git a/imgui_bundle_cmake/imgui_bundle_add_demo.cmake b/imgui_bundle_cmake/imgui_bundle_add_demo.cmake new file mode 100644 index 00000000..f477573e --- /dev/null +++ b/imgui_bundle_cmake/imgui_bundle_add_demo.cmake @@ -0,0 +1,76 @@ +# CMake functions to simplify the creation of demo apps using imgui_bundle +# Those are convenience functions, mostly used in the C++ and in the tutorials. + +function(ibd_add_demo_cpp) + # Creates an app using imgui_bundle_add_app, and link it with demo_utils + # Usage: + # ibd_add_demo_cpp(demo_name demo_file_1.cpp demo_file_2.cpp) + set(args ${ARGN}) + list(GET args 0 demo_name) + set(demos_asset_folder ${IMGUI_BUNDLE_PATH}/bindings/imgui_bundle/demos_assets) + imgui_bundle_add_app(${args} ASSETS_LOCATION ${demos_asset_folder}) + if(MSVC) + hello_imgui_msvc_target_set_folder(${demo_name} demos_cpp) + endif() + target_link_libraries(${demo_name} PUBLIC demo_utils) + if (EMSCRIPTEN) + # Bundle demos_assets + hello_imgui_bundle_assets_from_folder(${demo_name} ${demos_cpp_folder}/../demos_assets) + endif() +endfunction() + +function(ibd_add_single_file_demo_cpp demo_cpp_file) + # Creates a demo app + # Usage: + # ibd_add_single_file_demo_cpp(demo_file.cpp) + get_filename_component(demo_name "${demo_cpp_file}" NAME_WE) + ibd_add_demo_cpp(${demo_name} ${demo_cpp_file}) +endfunction() + +function(ibd_add_auto_demo demo_cpp_file) + # Creates a demo app, and provides main() automatically + # Usage: + # ibd_add_auto_demo(demo_file.cpp) + # (demo_file.cpp should provide a function named according to the containing file, e.g. `void demo_file()`) + get_filename_component(demo_cpp_file "${demo_cpp_file}" NAME) + get_filename_component(demo_name "${demo_cpp_file}" NAME_WE) + ibd_add_demo_cpp(${demo_name} ${demo_cpp_file}) + + # Read the contents of the cpp file + file(READ "${demo_cpp_file}" demo_cpp_file_content) + + # If the cpp file does not contain a main function, provide it + if (NOT "${demo_cpp_file_content}" MATCHES "int main") + message(STATUS "${demo_cpp_file} does not contain a main function, adding it!") + set(main_file ${CMAKE_CURRENT_BINARY_DIR}/${demo_cpp_file}.autogenerated.cpp) + configure_file(${demos_cpp_folder}/_auto_main/_auto_main.cpp.in ${main_file}) + target_sources(${demo_name} PRIVATE ${main_file}) + endif() + +endfunction() + +function(ibd_add_this_folder_demos) + # Creates demo apps for all cpp files in the current folder + FILE(GLOB demo_cpp_files *.cpp) + FOREACH(demo_cpp_file ${demo_cpp_files}) + ibd_add_single_file_demo_cpp(${demo_cpp_file}) + ENDFOREACH() +endfunction() + +function(ibd_add_this_folder_auto_demos) + # Creates demo apps for all cpp files in the current folder, and provide a main for them + FILE(GLOB demo_cpp_files *.cpp) + FOREACH(demo_cpp_file ${demo_cpp_files}) + ibd_add_auto_demo(${demo_cpp_file}) + ENDFOREACH() +endfunction() + + +function(ibd_add_this_folder_as_demos_library) + file(GLOB sources *.cpp) + get_filename_component(folder_name "${CMAKE_CURRENT_LIST_DIR}" NAME_WE) + set(demos_library_name ${folder_name}) + add_library(${demos_library_name} ${sources}) + target_link_libraries(${demos_library_name} PRIVATE imgui_bundle) + target_compile_definitions(${demos_library_name} PRIVATE IMGUI_BUNDLE_BUILD_DEMO_AS_LIBRARY) +endfunction()