Skip to content

Commit

Permalink
CMake: add options HELLOIMGUI_DOWNLOAD_SDL_IF_NEEDED / HELLOIMGUI_DOW…
Browse files Browse the repository at this point in the history
…NLOAD_GLFW_IF_NEEDED
  • Loading branch information
pthom committed Dec 19, 2023
1 parent 012b1ff commit c749760
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 63 deletions.
71 changes: 30 additions & 41 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,61 +39,50 @@ include(hello_imgui_add_app)
# HelloImGui Build options
###############################################################################

#------------------------------------------------------------------------------
# Available backends combinations: HelloImGui needs to use at least one of them.
# If you specify no option:
# - HELLOIMGUI_USE_GLFW_OPENGL3 will be used for Windows, Linux, and macOS
# - HELLOIMGUI_USE_SDL_OPENGL3 will be used for Android, iOS and emscripten
#------------------------------------------------------------------------------
# Use Glfw3 + OpenGl3
option(HELLOIMGUI_USE_GLFW_OPENGL3 "Build HelloImGui for GLFW+OpenGL3" OFF)
# Use SDL2 + OpenGL3
option(HELLOIMGUI_USE_SDL_OPENGL3 "Build HelloImGui for SDL+OpenGL3" OFF)
# Use SDL2 + Metal (Apple only)
option(HELLOIMGUI_USE_SDL_METAL "Build HelloImGui for SDL+Metal" OFF)
# Note: Metal and OpenGl3 are mutually exclusive!


#------------------------------------------------------------------------------
# Automatic download of Glfw3 and SDL2 (provided as a convenience)
# (disabled by default on Linux, which prefers to use the system libraries,
# enabled by default on other platforms)
#------------------------------------------------------------------------------
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
set(autodownload_default OFF)
else()
set(autodownload_default OFF)
endif()
option(HELLOIMGUI_DOWNLOAD_GLFW_IF_NEEDED "Download and build GLFW if needed" ${autodownload_default})
option(HELLOIMGUI_DOWNLOAD_SDL_IF_NEEDED "Download and build GLFW if needed" ${autodownload_default})


#------------------------------------------------------------------------------
# Options / Windows: provide WinMain automatically
#------------------------------------------------------------------------------
if (WIN32)
option(HELLOIMGUI_WIN32_NO_CONSOLE "Under windows, build apps without Dos Console" ON)
option(HELLOIMGUI_WIN32_AUTO_WINMAIN "Under windows, automatically provide a WinMain (provide `int main(int, char**)`, it will be called by WinMain())" ON)
endif()

#------------------------------------------------------------------------------
# Options / macOS: provide regular terminal executables, not app bundles
#------------------------------------------------------------------------------
if(MACOSX)
option(HELLOIMGUI_MACOS_NO_BUNDLE "Under macOS, build regular terminal executables, not app bundles" OFF)
endif()

#------------------------------------------------------------------------------
# Windowing Backends (SDL, Glfw)
# SDL2 and Glfw3 are supported. If you specify no option, Glfw3 will be downloaded
# automatically (unless HELLOIMGUI_USE_GLFW_SYSTEM_LIB is ON,
# in which case it will be searched via find_package())
#------------------------------------------------------------------------------
# Glfw Backend (Glfw3)
#---------------------
# if HELLOIMGUI_WITH_GLFW is ON, Glfw may be downloaded and built automatically
# (unless HELLOIMGUI_USE_GLFW_SYSTEM_LIB is ON, in which case it will be searched via find_package())
option(HELLOIMGUI_WITH_GLFW "Add GLFW backend (download if needed)" OFF)
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
option(HELLOIMGUI_USE_GLFW_SYSTEM_LIB "Force usage of GLFW system library" ON)
else()
option(HELLOIMGUI_USE_GLFW_SYSTEM_LIB "Force usage of GLFW system library" OFF)
endif()
# SDL Backend (SDL2)
#-------------------
# if HELLOIMGUI_WITH_SDL is ON, SDL may be downloaded and built automatically
# (unless HELLOIMGUI_USE_GLFW_SYSTEM_LIB is ON, in which case it will be searched via find_package())
option(HELLOIMGUI_WITH_SDL "Add SDL backend (download if needed)" OFF)
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
option(HELLOIMGUI_USE_SDL_SYSTEM_LIB "Force usage of SDL system library" ON)
else()
option(HELLOIMGUI_USE_SDL_SYSTEM_LIB "Force usage of SDL system library" OFF)
endif()

#------------------------------------------------------------------------------
# Available backends combinations: HelloImGui needs to use at least one of them
# If you specify no option:
# - HELLOIMGUI_USE_GLFW_OPENGL3 will be ON for Windows, Linux, and macOS
# - HELLOIMGUI_USE_SDL_OPENGL3 will be ON for Android, iOS and emscripten
#------------------------------------------------------------------------------
# HELLOIMGUI_USE_GLFW_OPENGL3:
# Use Glfw + OpenGl3: Glfw will be searched via find_package()
option(HELLOIMGUI_USE_GLFW_OPENGL3 "Build HelloImGui for GLFW+OpenGL3" OFF)
# HELLOIMGUI_USE_SDL_OPENGL3: select Windowing + Rendering backend in one option
# Use SDL + OpenGL3: SDL will be searched via find_package()
option(HELLOIMGUI_USE_SDL_OPENGL3 "Build HelloImGui for SDL+OpenGL3" OFF)
# Metal and OpenGl3 are mutually exclusive!
option(HELLOIMGUI_USE_SDL_METAL "Build HelloImGui for SDL+Metal" OFF)

#------------------------------------------------------------------------------
# Build options / ImGui
Expand Down
32 changes: 10 additions & 22 deletions src/hello_imgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ function(him_sanity_checks)
# use SDL for emscripten
if (EMSCRIPTEN AND NOT HELLOIMGUI_USE_SDL_OPENGL3 AND NOT HELLOIMGUI_USE_GLFW_OPENGL3)
set(HELLOIMGUI_USE_SDL_OPENGL3 ON CACHE BOOL "" FORCE)
set(HELLOIMGUI_USE_GLFW_OPENGL3 OFF CACHE BOOL "" FORCE)
endif()
if (IOS AND NOT HELLOIMGUI_USE_SDL_OPENGL3 AND NOT HELLOIMGUI_USE_GLFW_OPENGL3 AND NOT HELLOIMGUI_USE_SDL_METAL)
set(HELLOIMGUI_USE_SDL_OPENGL3 ON CACHE BOOL "" FORCE)
endif()

_him_check_if_no_backend_selected() # will set HELLOIMGUI_NO_BACKEND_SELECTED to ON if no backend selected
Expand All @@ -126,22 +128,12 @@ function(him_sanity_checks)
if (HELLOIMGUI_NO_BACKEND_SELECTED)
message(FATAL_ERROR "HelloImGui: no backend selected, and could not auto-select one!")
endif()

if (HELLOIMGUI_WITH_GLFW)
set(HELLOIMGUI_USE_GLFW_OPENGL3 ON CACHE BOOL "" FORCE)
endif()
if (HELLOIMGUI_WITH_SDL AND NOT HELLOIMGUI_USE_SDL_METAL)
set(HELLOIMGUI_USE_SDL_OPENGL3 ON CACHE BOOL "" FORCE)
endif()
endfunction()

function(_him_check_if_no_backend_selected)
if (NOT HELLOIMGUI_WITH_GLFW
AND NOT HELLOIMGUI_WITH_SDL
AND NOT HELLOIMGUI_USE_SDL_OPENGL3
if (NOT HELLOIMGUI_USE_SDL_OPENGL3
AND NOT HELLOIMGUI_USE_GLFW_OPENGL3
AND NOT HELLOIMGUI_USE_SDL_METAL
AND NOT EMSCRIPTEN
)
set(HELLOIMGUI_NO_BACKEND_SELECTED ON CACHE INTERNAL "")
endif()
Expand All @@ -153,10 +145,11 @@ function(_him_try_select_glfw_if_no_backend_selected)
# either select Glfw automatically if possible, or fail
#------------------------------------------------------------------------------
#
if (NOT HELLOIMGUI_USE_GLFW_SYSTEM_LIB)
set(HELLOIMGUI_WITH_GLFW ON CACHE BOOL "" FORCE)
if (HELLOIMGUI_DOWNLOAD_GLFW_IF_NEEDED)
set(HELLOIMGUI_USE_GLFW_OPENGL3 ON CACHE BOOL "" FORCE)
message(STATUS "
HelloImGui: using Glfw as default default backend (it will be downloaded and built inside {CMAKE_CURRENT_BINARY_DIR}/_deps/glfw-*)
HelloImGui: using HELLOIMGUI_USE_GLFW_OPENGL3 as default default backend
(Glfw3 will be downloaded and built inside {CMAKE_CURRENT_BINARY_DIR}/_deps/glfw-*)
${backend_message}
")
set(HELLOIMGUI_NO_BACKEND_SELECTED OFF CACHE INTERNAL "")
Expand Down Expand Up @@ -373,8 +366,7 @@ function(_him_fetch_sdl_if_needed)
# option HELLOIMGUI_WITH_SDL was passed
# and SDL not available by find_package
# and HELLOIMGUI_USE_SDL_SYSTEM_LIB is OFF
if (HELLOIMGUI_WITH_SDL AND NOT HELLOIMGUI_USE_SDL_SYSTEM_LIB
AND NOT TARGET sdl AND NOT EMSCRIPTEN)
if (HELLOIMGUI_DOWNLOAD_SDL_IF_NEEDED AND NOT TARGET sdl AND NOT EMSCRIPTEN)
find_package(SDL2 QUIET)
if (NOT SDL2_FOUND)
set(shall_fetch_sdl ON)
Expand Down Expand Up @@ -476,11 +468,7 @@ endfunction()

function(_him_fetch_glfw_if_needed)
set(shall_fetch_glfw OFF)
if (HELLOIMGUI_WITH_GLFW
AND NOT HELLOIMGUI_USE_GLFW_SYSTEM_LIB
AND NOT TARGET glfw
AND NOT EMSCRIPTEN
)
if (HELLOIMGUI_DOWNLOAD_GLFW_IF_NEEDED AND NOT TARGET glfw)
find_package(glfw3 QUIET)
if (NOT glfw3_FOUND)
set(shall_fetch_glfw ON)
Expand Down

0 comments on commit c749760

Please sign in to comment.