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

Conversation

ev1313
Copy link

@ev1313 ev1313 commented Dec 5, 2023

Hi,

there are quite a few CMake pull requests, but none of them do what I propose/need.

Description:

This pull requests adds CMake not for examples, but rather for integrating imgui in other CMake projects. Using this it is possible to integrate imgui in another CMake project like this (assuming e.g. imgui is a git submodule):

...
add_subdirectory(deps/imgui)
...
target_link_libraries(mytarget PUBLIC imgui)

It is also possible to depend on a specific backend:

...
set(IMGUI_BACKEND_GLFW ON)
add_subdirectory(deps/imgui)
...
# all necessary headers, glfw, etc. are all linked with it already, as the imgui CMake targets depend on them
target_link_libraries(mytarget PUBLIC imgui_backend_glfw)
...

Incentive:

The benefit of this approach is much easier maintenance compared in case of changes in the names of files, addition of new files, etc. Imgui Module authors can also depend on imgui in their CMake Files without having to provide variables like "imgui location" and such (so in case of new files no changes are necessary usually).
Furthermore the dependecies of backends are handled by the build system and people "just using" one of these backends should not need to configure additional libraries or include paths.

Sideeffects:

It does not break any existing features/buildsystems, because there are none I know of and there are no changes to the source files themselves.

Limitations:

I did not yet test all backends (Mac, Windows, Allegro, WGPU are untested), parts of the code are adapted from
https://github.com/ocornut/imgui/pull/4614/files

There currently only is support for the CPP Module in misc/.

The examples are not covered with this code, as they can be built using a Makefile already (and are only for demonstration).

projects or extensions of imgui depending on it.
backends/CMakeLists.txt Show resolved Hide resolved
CMakeLists.txt Show resolved Hide resolved
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 "."


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

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"

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.


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()

Comment on lines +95 to +97
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)
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.

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"

@@ -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)

@arcanis
Copy link

arcanis commented Apr 30, 2024

@ev1313 Do you need help following up on the feedback?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants