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
base: master
Are you sure you want to change the base?
Conversation
projects or extensions of imgui depending on it.
project(imgui LANGUAGES C CXX) | ||
|
||
add_library(imgui imgui.cpp imgui_draw.cpp imgui_tables.cpp imgui_widgets.cpp) | ||
target_include_directories(imgui PUBLIC .) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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)
@ev1313 Do you need help following up on the feedback? |
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):
It is also possible to depend on a specific backend:
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).