CMakeLists.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # Building for desktop (WebGPU-native) with Dawn:
  2. # 1. git clone https://github.com/google/dawn dawn
  3. # 2. cmake -B build -DIMGUI_DAWN_DIR=dawn
  4. # 3. cmake --build build
  5. # The resulting binary will be found at one of the following locations:
  6. # * build/Debug/example_glfw_wgpu[.exe]
  7. # * build/example_glfw_wgpu[.exe]
  8. # Building for desktop (WGPU-Native) with WGPU-Native:
  9. # 1. download WGPU-Native autogenerated binary modules for your platform/compiler from: https://github.com/gfx-rs/wgpu-native/releases
  10. # 2. unzip the downloaded file in your_preferred_folder
  11. # 3. cmake -B build -DIMGUI_WGPU_DIR=your_preferred_folder ("full path" or "relative" starting from current directory)
  12. # 4. cmake --build build
  13. # The resulting binary will be found at one of the following locations:
  14. # * build/Debug/example_glfw_wgpu[.exe]
  15. # * build/example_glfw_wgpu[.exe]
  16. # Building for Emscripten:
  17. # 1. Install Emscripten SDK following the instructions: https://emscripten.org/docs/getting_started/downloads.html
  18. # 2. Install Ninja build system
  19. # 3. emcmake cmake -G Ninja -B build
  20. # (optional) -DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="--use-port=path/to/emdawnwebgpu_package/emdawnwebgpu.port.py", see ReadMe.md
  21. # 3. cmake --build build
  22. # 4. emrun build/index.html
  23. cmake_minimum_required(VERSION 3.22) # Dawn requires CMake >= 3.22
  24. project(imgui_example_glfw_wgpu C CXX)
  25. set(IMGUI_EXECUTABLE example_glfw_wgpu)
  26. if(NOT CMAKE_BUILD_TYPE)
  27. set(CMAKE_BUILD_TYPE Debug CACHE STRING "" FORCE)
  28. endif()
  29. set(CMAKE_CXX_STANDARD 17) # Dawn requires C++17
  30. # Dear ImGui
  31. set(IMGUI_DIR ../../)
  32. set(IMGUI_EXAMPLE_SOURCE_FILES
  33. # Example code
  34. main.cpp
  35. # Dear ImGui Backend files
  36. ${IMGUI_DIR}/backends/imgui_impl_glfw.cpp
  37. ${IMGUI_DIR}/backends/imgui_impl_wgpu.cpp
  38. # Dear ImGui files
  39. ${IMGUI_DIR}/imgui.cpp
  40. ${IMGUI_DIR}/imgui_draw.cpp
  41. ${IMGUI_DIR}/imgui_demo.cpp
  42. ${IMGUI_DIR}/imgui_tables.cpp
  43. ${IMGUI_DIR}/imgui_widgets.cpp
  44. )
  45. if(EMSCRIPTEN)
  46. if(NOT IMGUI_EMSCRIPTEN_WEBGPU_FLAG) # if IMGUI_EMSCRIPTEN_WEBGPU_FLAG not used, set by current EMSCRIPTEN version
  47. if(EMSCRIPTEN_VERSION VERSION_GREATER_EQUAL "4.0.10")
  48. set(IMGUI_EMSCRIPTEN_WEBGPU_FLAG "--use-port=emdawnwebgpu" CACHE STRING "Choose between --use-port=emdawnwebgpu (Dawn implementation of EMSCRIPTEN) and -sUSE_WEBGPU=1 (WGPU implementation of EMSCRIPTEN, deprecated in 4.0.10): default to --use-port=emdawnwebgpu for EMSCRIPTEN >= 4.0.10")
  49. else()
  50. set(IMGUI_EMSCRIPTEN_WEBGPU_FLAG "-sUSE_WEBGPU=1" CACHE STRING "Use -sUSE_WEBGPU=1 for EMSCRIPTEN WGPU implementation")
  51. endif()
  52. else() # if IMGUI_EMSCRIPTEN_WEBGPU_FLAG used, check correct version
  53. if(EMSCRIPTEN_VERSION VERSION_LESS "4.0.10" AND "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}" MATCHES "emdawnwebgpu")
  54. # it's necessary EMSCRIPTEN >= v4.0.10 (although "--use-port=path/to/emdawnwebgpu.port.py" is supported/tested from v4.0.8)
  55. message(FATAL_ERROR "emdawnwebgpu needs EMSCRIPTEN version >= 4.0.10")
  56. endif()
  57. endif()
  58. if(EMSCRIPTEN_VERSION VERSION_GREATER_EQUAL "3.1.57")
  59. set(IMGUI_EMSCRIPTEN_GLFW3 "--use-port=contrib.glfw3" CACHE STRING "Choose between --use-port=contrib.glfw3 and -sUSE_GLFW=3 for GLFW implementation (default to --use-port=contrib.glfw3)")
  60. else() # cannot use contrib.glfw3 prior to 3.1.57
  61. set(IMGUI_EMSCRIPTEN_GLFW3 "-sUSE_GLFW=3" CACHE STRING "Use -sUSE_GLFW=3 for GLFW implementation" FORCE)
  62. endif()
  63. set(LIBRARIES glfw)
  64. add_compile_options(-sDISABLE_EXCEPTION_CATCHING=1 -DIMGUI_DISABLE_FILE_FUNCTIONS=1)
  65. else() # Native/Desktop build
  66. # Check DAWN/WGPU directory
  67. if(NOT IMGUI_DAWN_DIR AND NOT IMGUI_WGPU_DIR) # if it's Native/Desktop build, IMGUI_DAWN_DIR or IMGUI_WGPU_DIR must be specified
  68. message(FATAL_ERROR "Please specify the Dawn or WGPU base directory")
  69. endif()
  70. if(IMGUI_DAWN_DIR AND IMGUI_WGPU_DIR) # both IMGUI_DAWN_DIR and IMGUI_WGPU_DIR cannot be set
  71. message(FATAL_ERROR "Please specify only one between Dawn / WGPU base directory")
  72. endif()
  73. if(APPLE) # Add SDL2 module to get Surface, with libs and file property for MacOS build
  74. set_source_files_properties(${IMGUI_DIR}/backends/imgui_impl_wgpu.cpp PROPERTIES COMPILE_FLAGS "-x objective-c++")
  75. set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal -framework MetalKit -framework Cocoa")
  76. endif()
  77. find_package(glfw3 REQUIRED)
  78. if(IMGUI_DAWN_DIR) # DAWN-Native build options
  79. list(APPEND CMAKE_PREFIX_PATH ${IMGUI_DAWN_DIR})
  80. find_package(Dawn) # Search for a Dawn installation using IMGUI_DAWN_DIR in CMAKE_PREFIX_PATH
  81. if(Dawn_FOUND)
  82. message("Dawn Installation has been found!")
  83. set(LIBRARIES dawn::webgpu_dawn glfw ${OS_LIBRARIES})
  84. else()
  85. set(IMGUI_DAWN_DIR CACHE PATH "Path to Dawn repository")
  86. option(DAWN_FETCH_DEPENDENCIES "Use fetch_dawn_dependencies.py as an alternative to using depot_tools" ON)
  87. set(DAWN_BUILD_MONOLITHIC_LIBRARY "STATIC" CACHE STRING "Build monolithic library: SHARED, STATIC, or OFF.")
  88. option(DAWN_USE_GLFW OFF) # disable builtin GLFW in DAWN when we use SDL2 / SDL3
  89. # Dawn builds many things by default - disable things we don't need
  90. option(DAWN_BUILD_SAMPLES "Enables building Dawn's samples" OFF)
  91. option(TINT_BUILD_CMD_TOOLS "Build the Tint command line tools" OFF)
  92. option(TINT_BUILD_DOCS "Build documentation" OFF)
  93. option(TINT_BUILD_TESTS "Build tests" OFF)
  94. if(NOT APPLE)
  95. option(TINT_BUILD_MSL_WRITER "Build the MSL output writer" OFF)
  96. endif()
  97. if(WIN32)
  98. option(DAWN_FORCE_SYSTEM_COMPONENT_LOAD "Allow system component fallback" ON)
  99. option(TINT_BUILD_SPV_READER "Build the SPIR-V input reader" OFF)
  100. option(TINT_BUILD_WGSL_READER "Build the WGSL input reader" ON)
  101. option(TINT_BUILD_GLSL_WRITER "Build the GLSL output writer" OFF)
  102. option(TINT_BUILD_GLSL_VALIDATOR "Build the GLSL output validator" OFF)
  103. option(TINT_BUILD_SPV_WRITER "Build the SPIR-V output writer" ON)
  104. option(TINT_BUILD_WGSL_WRITER "Build the WGSL output writer" ON)
  105. endif()
  106. # check if WAYLAND is the current Session Type and enable DAWN_USE_WAYLAND Wayland option @compile time
  107. # You can override this using: cmake -DDAWN_USE_WAYLAND=X (X = ON | OFF)
  108. if(LINUX)
  109. if($ENV{XDG_SESSION_TYPE} MATCHES wayland)
  110. option(DAWN_USE_WAYLAND "Enable support for Wayland surface" ON)
  111. endif()
  112. endif()
  113. add_subdirectory("${IMGUI_DAWN_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/dawn" EXCLUDE_FROM_ALL)
  114. set(LIBRARIES webgpu_dawn glfw)
  115. endif()
  116. else() # WGPU-Native build options
  117. set(WGPU_NATIVE_LIB_DIR ${IMGUI_WGPU_DIR}/lib)
  118. find_library(WGPU_LIBRARY NAMES libwgpu_native.a wgpu_native.lib wgpu_native HINTS ${WGPU_NATIVE_LIB_DIR} REQUIRED)
  119. if(WIN32)
  120. set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32 Propsys RuntimeObject)
  121. elseif(UNIX AND NOT APPLE)
  122. set(OS_LIBRARIES "-lm -ldl")
  123. endif()
  124. set(LIBRARIES glfw ${WGPU_LIBRARY} ${OS_LIBRARIES})
  125. endif()
  126. endif()
  127. add_executable(${IMGUI_EXECUTABLE} ${IMGUI_EXAMPLE_SOURCE_FILES})
  128. target_include_directories(${IMGUI_EXECUTABLE} PUBLIC
  129. ${IMGUI_DIR}
  130. ${IMGUI_DIR}/backends
  131. )
  132. target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_EXAMPLE_GLFW_WGPU")
  133. # compiler option only for IMGUI_EXAMPLE_SOURCE_FILES
  134. if (MSVC)
  135. target_compile_options(${IMGUI_EXECUTABLE} PUBLIC /W4) # warning level 4
  136. else()
  137. target_compile_options(${IMGUI_EXECUTABLE} PUBLIC -Wall) # -Wextra -Wpedantic
  138. endif()
  139. # In this example IMGUI_IMPL_WEBGPU_BACKEND_DAWN / IMGUI_IMPL_WEBGPU_BACKEND_WGPU internal define is set according to:
  140. # EMSCRIPTEN: by used FLAG
  141. # --use-port=emdawnwebgpu --> IMGUI_IMPL_WEBGPU_BACKEND_DAWN defined
  142. # -sUSE_WEBGPU=1 --> IMGUI_IMPL_WEBGPU_BACKEND_WGPU defined
  143. # NATIVE: by used SDK installation directory
  144. # if IMGUI_DAWN_DIR is valid --> IMGUI_IMPL_WEBGPU_BACKEND_DAWN defined
  145. # if IMGUI_WGPU_DIR is valid --> IMGUI_IMPL_WEBGPU_BACKEND_WGPU defined
  146. if(NOT EMSCRIPTEN) # WegGPU-Native settings
  147. # Set IMGUI_IMPL_WEBGPU_BACKEND_XXXX based on the SDK (directory) used
  148. if(IMGUI_DAWN_DIR)
  149. target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_DAWN")
  150. if(NOT Dawn_FOUND)
  151. target_link_libraries(${IMGUI_EXECUTABLE} INTERFACE webgpu_cpp)
  152. endif()
  153. else()
  154. target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_WGPU")
  155. target_include_directories(${IMGUI_EXECUTABLE} PUBLIC ${IMGUI_WGPU_DIR}/include)
  156. endif()
  157. target_link_libraries(${IMGUI_EXECUTABLE} PUBLIC ${LIBRARIES})
  158. else() # Emscripten settings
  159. set(CMAKE_EXECUTABLE_SUFFIX ".html")
  160. if("${IMGUI_EMSCRIPTEN_GLFW3}" STREQUAL "--use-port=contrib.glfw3")
  161. target_compile_options(${IMGUI_EXECUTABLE} PUBLIC "${IMGUI_EMSCRIPTEN_GLFW3}")
  162. endif()
  163. message(STATUS "Using ${IMGUI_EMSCRIPTEN_GLFW3} GLFW implementation")
  164. if("${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}" MATCHES "emdawnwebgpu")
  165. target_compile_options(${IMGUI_EXECUTABLE} PUBLIC "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}")
  166. target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_DAWN")
  167. else()
  168. target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_WGPU")
  169. endif()
  170. message(STATUS "Using ${IMGUI_EMSCRIPTEN_WEBGPU_FLAG} WebGPU implementation")
  171. target_link_options(${IMGUI_EXECUTABLE} PRIVATE
  172. "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}"
  173. "${IMGUI_EMSCRIPTEN_GLFW3}"
  174. "-sWASM=1"
  175. "-sASYNCIFY=1"
  176. "-sALLOW_MEMORY_GROWTH=1"
  177. "-sNO_EXIT_RUNTIME=0"
  178. "-sASSERTIONS=1"
  179. "-sDISABLE_EXCEPTION_CATCHING=1"
  180. "-sNO_FILESYSTEM=1"
  181. "--shell-file=${CMAKE_CURRENT_LIST_DIR}/../libs/emscripten/shell_minimal.html"
  182. )
  183. set_target_properties(${IMGUI_EXECUTABLE} PROPERTIES OUTPUT_NAME "index")
  184. endif()