Skip to content

Commit

Permalink
Separate imgui_bundle_add_demo.cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Dec 19, 2024
1 parent 35c4105 commit a57b127
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 89 deletions.
80 changes: 3 additions & 77 deletions bindings/imgui_bundle/demos_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions bindings/imgui_bundle/demos_cpp/demos_imguizmo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 2 additions & 2 deletions bindings/imgui_bundle/demos_cpp/demos_nanovg/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion bindings/imgui_bundle/demos_cpp/sandbox/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
add_this_folder_auto_demos()
ibd_add_this_folder_auto_demos()
76 changes: 76 additions & 0 deletions imgui_bundle_cmake/imgui_bundle_add_demo.cmake
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit a57b127

Please sign in to comment.