Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add basic CMake implementation #7094

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.20)
project(imgui LANGUAGES C CXX)

add_library(imgui imgui.cpp imgui_draw.cpp imgui_tables.cpp imgui_widgets.cpp)
target_include_directories(imgui PUBLIC .)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and everywhere else ${CMAKE_CURRENT_LIST_DIR} seems to be more explicit for me than a "."


add_subdirectory(backends)
add_subdirectory(misc/cpp)

ocornut marked this conversation as resolved.
Show resolved Hide resolved
133 changes: 133 additions & 0 deletions backends/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@

ocornut marked this conversation as resolved.
Show resolved Hide resolved
option(IMGUI_BACKEND_ALLEGRO "Enable the Allegro backend" OFF)
if(IMGUI_BACKEND_ALLEGRO)
add_library(imgui_backend_allegro imgui_impl_allegro5.cpp)
target_include_directories(imgui_backend_allegro PUBLIC .)
target_link_libraries(imgui_backend_allegro PUBLIC imgui)
endif()

option(IMGUI_BACKEND_ANDROID "Enable the Android backend" OFF)
if(IMGUI_BACKEND_ANDROID)
add_library(imgui_backend_android imgui_impl_android.cpp)
target_include_directories(imgui_backend_android PUBLIC .)
target_link_libraries(imgui_backend_android PUBLIC android imgui)
endif()

option(IMGUI_BACKEND_DX10 "Enable the DirectX 10 backend" OFF)
if(IMGUI_BACKEND_DX10)
add_library(imgui_backend_dx10 imgui_impl_dx10.cpp)
target_include_directories(imgui_backend_dx10 PUBLIC .)
target_link_libraries(imgui_backend_dx10 PUBLIC imgui d3d10.lib d3dcompiler.lib)
endif()

option(IMGUI_BACKEND_DX11 "Enable the DirectX 11 backend" OFF)
if(IMGUI_BACKEND_DX11)
add_library(imgui_backend_dx11 imgui_impl_dx11.cpp)
target_include_directories(imgui_backend_dx11 PUBLIC .)
target_link_libraries(imgui_backend_dx11 PUBLIC imgui d3d11.lib d3dcompiler.lib)
endif()

option(IMGUI_BACKEND_DX12 "Enable the DirectX 12 backend" OFF)
if(IMGUI_BACKEND_DX12)
add_library(imgui_backend_dx12 imgui_impl_dx12.cpp)
target_include_directories(imgui_backend_dx12 PUBLIC .)
target_link_libraries(imgui_backend_dx12 PUBLIC imgui d3d12.lib d3dcompiler.lib dxgi.lib)
endif()

option(IMGUI_BACKEND_DX9 "Enable the DirectX 9 backend" OFF)
if(IMGUI_BACKEND_DX9)
add_library(imgui_backend_dx9 imgui_impl_dx9.cpp)
target_include_directories(imgui_backend_dx9 PUBLIC .)
target_link_libraries(imgui_backend_dx9 PUBLIC imgui d3d9.lib)
endif()

option(IMGUI_BACKEND_GLFW "Enable the GLFW backends" OFF)
if(IMGUI_BACKEND_GLFW)
find_package(glfw3 REQUIRED)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and for other find_package calls

This won't work very well for packages that don't want to use glfw added via add_subdirectory - ideally you should check for the presence of glfw target (if (TARGET glfw)) and only do find_package if it wasn't

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't find_package meant to be idempotent? Looking at lib/cmake/glfw3/glfw3Targets.cmake generated for glfw3 in nixpkgs, it does contain a check for if (TARGET glfw). I suspect it's the other way around, users (ab)using add_subdirectory should test for TARGET and verify they don't break the normal dependency injection stuff

add_library(imgui_backend_glfw imgui_impl_glfw.cpp)
target_include_directories(imgui_backend_glfw PUBLIC .)
target_link_libraries(imgui_backend_glfw PUBLIC imgui glfw)
endif()

option(IMGUI_BACKEND_GLUT "Enable the GLUT backend" OFF)
if(IMGUI_BACKEND_GLUT)
find_package(glut REQUIRED)
add_library(imgui_backend_glut imgui_impl_glut.cpp)
target_include_directories(imgui_backend_glut PUBLIC .)
target_link_libraries(imgui_backend_glut PUBLIC imgui glut)
endif()

option(IMGUI_BACKEND_METAL "Enable the Metal backend" OFF)
if(IMGUI_BACKEND_METAL)
add_library(imgui_backend_metal imgui_impl_metal.mm)
target_include_directories(imgui_backend_metal PUBLIC .)
target_link_libraries(imgui_backend_metal PUBLIC imgui "-framework Metal -framework MetalKit -framework QuartzCore")
endif()

option(IMGUI_BACKEND_OPENGL "Enable the OpenGL backends" OFF)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think people would want one or the other, but rarely both, so it's worth separating into "IMGUI_BACKEND_OPENGL2" and "IMGUI_BACKEND_OPENGL3"

if(IMGUI_BACKEND_OPENGL)
find_package(OpenGL REQUIRED)

add_library(imgui_backend_opengl2 imgui_impl_opengl2.cpp)
target_include_directories(imgui_backend_opengl2 PUBLIC .)
target_link_libraries(imgui_backend_opengl2 PUBLIC imgui OpenGL::OpenGL)

add_library(imgui_backend_opengl3 imgui_impl_opengl3.cpp)
target_include_directories(imgui_backend_opengl3 PUBLIC .)
target_link_libraries(imgui_backend_opengl3 PUBLIC imgui OpenGL::OpenGL)
endif()

option(IMGUI_BACKEND_OSX "Enable the OSX backend" OFF)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "IMGUI_BACKEND_COCOA"? Because "OSX backend" is a bit confusing.

if(IMGUI_BACKEND_OSX)
add_library(imgui_backend_osx imgui_impl_osx.mm)
target_include_directories(imgui_backend_osx PUBLIC .)
target_link_libraries(imgui_backend_osx PUBLIC imgui "-framework Cocoa")
endif()

option(IMGUI_BACKEND_SDL2 "Enable the SDL2 backends" OFF)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and everywhere else: "backends" should be "backend"

if(IMGUI_BACKEND_SDL2)
find_package(SDL2 REQUIRED)

add_library(imgui_backend_sdl2 imgui_impl_sdl2.cpp)
target_include_directories(imgui_backend_sdl2 PUBLIC .)
target_link_libraries(imgui_backend_sdl2 PUBLIC imgui SDL2::SDL2main SDL2::SDL2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SDL2::SDL2main is not necessary here.

Also SDL2::SDL2 is a shared lib and for static linking, you need to use SDL2::SDL2-static, so probably needs to do something like this:

if(BUILD_SHARED_LIBS)
  target_link_libraries(imgui_packend_sdl2 PUBLIC
    SDL2::SDL2
  )
else()
  target_link_libraries(imgui_packend_sdl2 PUBLIC
    SDL2::SDL2-static
  )
endif()


add_library(imgui_backend_sdlrenderer2 imgui_impl_sdlrenderer2.cpp)
target_include_directories(imgui_backend_sdlrenderer2 PUBLIC .)
target_link_libraries(imgui_backend_sdlrenderer2 PUBLIC imgui SDL2::SDL2main SDL2::SDL2)
Comment on lines +95 to +97
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely needs to be optional in the same way that OpenGL backends are optional, for example.

If I use OpenGL, I have no need for sdlrenderer impl.

endif()

option(IMGUI_BACKEND_SDL3 "Enable the SDL3 backends" OFF)
if(IMGUI_BACKEND_SDL3)
find_package(SDL3 REQUIRED)
add_library(imgui_backend_sdl3 imgui_impl_sdl3.cpp)
target_include_directories(imgui_backend_sdl3 PUBLIC .)
target_link_libraries(imgui_backend_sdl3 PUBLIC imgui SDL3::SDL3)

add_library(imgui_backend_sdlrenderer3 imgui_impl_sdlrenderer3.cpp)
target_include_directories(imgui_backend_sdlrenderer3 PUBLIC .)
target_link_libraries(imgui_backend_sdlrenderer3 PUBLIC imgui SDL3::SDL3)
endif()

option(IMGUI_BACKEND_VULKAN "Enable the Vulkan backends" OFF)
if(IMGUI_BACKEND_VULKAN)
find_package(Vulkan REQUIRED)
add_library(imgui_backend_vulkan imgui_impl_vulkan.cpp)
target_include_directories(imgui_backend_vulkan PUBLIC .)
target_link_libraries(imgui_backend_vulkan PUBLIC imgui Vulkan::Vulkan)
endif()

option(IMGUI_BACKEND_WGPU "Enable the WGPU backends" OFF)
if(IMGUI_BACKEND_WGPU)
find_package(wgpu REQUIRED)
add_library(imgui_backend_wgpu imgui_impl_wgpu.cpp)
target_include_directories(imgui_backend_wgpu PUBLIC .)
target_link_libraries(imgui_backend_wgpu PUBLIC imgui wgpu)
endif()

option(IMGUI_BACKEND_WIN32 "Enable the Win32 backend" OFF)
if(IMGUI_BACKEND_WIN32)
add_library(imgui_backend_win32 imgui_impl_win32.cpp)
target_include_directories(imgui_backend_win32 PUBLIC .)
target_link_libraries(imgui_backend_win32 PUBLIC imgui)
endif()
3 changes: 3 additions & 0 deletions misc/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_library(imgui_stdlib imgui_stdlib.cpp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't see why imgui_stdlib should be a separate lib here - probably should be included with the main imgui target and be optional under some IMGUI_CPP_STDLIB option (and this should be done in the root CMakeLists file)

target_include_directories(imgui_stdlib PUBLIC .)
target_link_libraries(imgui_stdlib PUBLIC imgui)