-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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 simple CMake support for imgui #5394
Conversation
Cool that you chose to implement build in a single file, not littering entire project with CMake-isms! There already were other attempts, adding them for reference: #4614, #3027, #2820, #2033, #1778, #1713, #1573, #255, CMakeLists.txt. Wow, this was a lot. Seems like there is a persistent demand, even though building Dear ImGui does not require any special build steps and is easy to include as part of your project, whatever build system is used. This PR is by no means complete though. I love simplicity of this build script, but unfortunately it will not be enough to build all examples on most supported operating systems. If you look at my linked
This is incorrect use of SDL. By convention users must import SDL with it's special include paths: ~ % pkg-config --cflags sdl2
-I/usr/include/SDL2 -D_REENTRANT You on the other hand changed includes in a way that depends on
Dear ImGui requires C++11, there is no point to force a higher standard version. Also care must be taken to allow users to set higher version should they desire. This would be best done through
This is a big no-no. With modern CMake we use target-based approach, assigning include paths to targets. You should use All in all this build script only covers a tiny fraction of usecases - building of examples for demonstration purposes on Linux. A lot more work is still required. But maybe its a good start! 👍 |
CMakeLists.txt
Outdated
# You may have to manually set CMAKE_C_COMPILER and CMAKE_CXX_COMPILER respectively to clang | ||
# set(CMAKE_C_COMPILER "/usr/local/bin/clang") | ||
# set(CMAKE_CXX_COMPILER "/usr/local/bin/clang++") |
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 has nothing to do with this repo. It is recommended to put this on a toolchain file, the user can do this on it's side
CMakeLists.txt
Outdated
@@ -0,0 +1,275 @@ | |||
cmake_minimum_required(VERSION 3.13) # CMake version check | |||
project(imgui) # Create project "imgui" | |||
set(CMAKE_CXX_STANDARD 14) # Enable c++14 standard |
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.
Two things with this
- This is expected to be 11
- This must be set at the target level. You are polluting the global scope!
CMakeLists.txt
Outdated
option(BUILD_SHARED_LIBS "Build using shared libraries" ON) | ||
|
||
# Add library target with source files listed in SOURCE_FILES variable | ||
add_library(imgui ${SOURCE_FILES}) |
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.
List the sources directly here
CMakeLists.txt
Outdated
|
||
|
||
# Add all .cpp files of project root directory as source file | ||
set(SOURCE_FILES imgui.cpp imgui_draw.cpp imgui_demo.cpp imgui_tables.cpp imgui_widgets.cpp) # Add all .cpp files of project root directory as source files |
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.
set(SOURCE_FILES imgui.cpp imgui_draw.cpp imgui_demo.cpp imgui_tables.cpp imgui_widgets.cpp) # Add all .cpp files of project root directory as source files |
CMakeLists.txt
Outdated
# Add all .cpp files of project root directory as source file | ||
set(SOURCE_FILES imgui.cpp imgui_draw.cpp imgui_demo.cpp imgui_tables.cpp imgui_widgets.cpp) # Add all .cpp files of project root directory as source files | ||
|
||
option(BUILD_SHARED_LIBS "Build using shared libraries" ON) |
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.
Put all options together and as high as possible on the file
CMakeLists.txt
Outdated
set(BACKEND_FILE ./backends/imgui_impl_allegro5.cpp) | ||
add_library(imgui_backend_allegro5 ${BACKEND_FILE}) |
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.
set(BACKEND_FILE ./backends/imgui_impl_allegro5.cpp) | |
add_library(imgui_backend_allegro5 ${BACKEND_FILE}) | |
add_library(imgui_backend_allegro5 backends/imgui_impl_allegro5.cpp) |
CMakeLists.txt
Outdated
find_package(PkgConfig REQUIRED) # For .pc packages | ||
|
||
|
||
include_directories(./backends) |
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.
Use target version
CMakeLists.txt
Outdated
set(EXAMPLE_MAIN_FILE ./examples/example_apple_opengl2/main.mm) | ||
add_executable(example_apple_opengl2 ${EXAMPLE_MAIN_FILE}) |
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.
set(EXAMPLE_MAIN_FILE ./examples/example_apple_opengl2/main.mm) | |
add_executable(example_apple_opengl2 ${EXAMPLE_MAIN_FILE}) | |
add_executable(example_apple_opengl2 examples/example_apple_opengl2/main.mm) |
CMakeLists.txt
Outdated
|
||
|
||
include_directories(./backends) | ||
if(${BUILD_EXAMPLE_ALLEGRO5}) |
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.
if(${BUILD_EXAMPLE_ALLEGRO5}) | |
if(BUILD_EXAMPLE_ALLEGRO5) |
backends/imgui_impl_sdl.cpp
Outdated
#include <SDL2/SDL.h> | ||
#include <SDL2/SDL_syswm.h> |
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 is not needed with properly set targets
@rokups are there other reasons besides building examples on Apple why you haven't already created a PR out of your CMakeLists? It looks quite comprehensive and addresses at least some of the points you mentioned. |
Build systems are a complicated can of worms. Adopting a build script implies maintenance. Current build infrastructure (vcxproj + Makefile) are not used by people so they only need to be maintained as much as we need to use them for testing or on CI. A comprehensive CMakeLists will be used by people so it will definitely be more work maintaining that as well. In the end it is pretty trivial for everyone to come up with a minimal CMakeLists for imgui, that fits their project needs. Even in my own sideprojects i just throw couple lines simple CMakeLists and use that instead of using my script as it is just simpler. tl;dr; there is no real hard need for that so its on hold. maybe one day, maybe not, who knows \o/ |
CMakeLists.txt
Outdated
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.
even though building Dear ImGui does not require any special build steps and is easy to include as part of your project, whatever build system is used
Offtopic but this very much sounds like https://youtu.be/sBP17HQAQjk?t=318 🤣
I'm wondering if maybe at this point it's more sensible to just advertise vcpkg's (or equivalent) cmake rules for imgui. From bootstrapping and/or distributions' points of view, people vendoring imgui is an issue, because that is usually done in a way that prevents substitution/dependency injection. Just having standardized pkg-config/cmake target names (e.g. ones declared by vcpkg) that people could test for before falling back to their vendored stuff would already be quite helpful
CMakeLists.txt
to build imgui into a shared library, and add the options to build a couple of more backend modules, as well as examples.