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

Lightweight, Modular CMake Integration for ImGui #7992

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ examples/example_sdl2_metal/example_sdl2_metal
examples/example_sdl2_opengl2/example_sdl2_opengl2
examples/example_sdl2_opengl3/example_sdl2_opengl3
examples/example_sdl2_sdlrenderer/example_sdl2_sdlrenderer

## User Playground
playground/*
CMakeSettings.json
51 changes: 51 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
cmake_minimum_required(VERSION 3.15)

project(imgui)

option(IMGUI_IMPL_WIN32 "Enable ImGui Win32 implementation" ON)
option(IMGUI_IMPL_OPENGL2 "Enable ImGui OpenGL2 implementation" ON)
option(IMGUI_IMPL_OPENGL3 "Enable ImGui OpenGL3 implementation" ON)
option(IMGUI_IMPL_DX9 "Enable ImGui DirectX9 implementation" ON)
option(IMGUI_IMPL_DX10 "Enable ImGui DirectX10 implementation" ON)
option(IMGUI_IMPL_DX11 "Enable ImGui DirectX11 implementation" ON)
option(IMGUI_IMPL_DX12 "Enable ImGui DirectX12 implementation" ON)
option(IMGUI_ENABLE_PLAYGROUND "Enable playground subdirectory" OFF)

set(IMGUI_INC_DIR
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>)

include(cmake/add_library_ns.cmake)
include(cmake/target_include_dir_iface.cmake)
include(cmake/install_target.cmake)

add_library_ns(imgui core STATIC imgui.cpp imgui_demo.cpp imgui_draw.cpp imgui_tables.cpp imgui_widgets.cpp)
target_include_directories(imgui-core PUBLIC ${IMGUI_INC_DIR})

add_subdirectory(misc)
add_subdirectory(backends)

# Playground is a private space where dev might test/experiment/freestyle stuffs
if(IMGUI_ENABLE_PLAYGROUND)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/playground)
add_subdirectory(playground)
endif()
endif()

install_target_and_headers(imgui core
imgui.h
imgui_internal.h
imconfig.h
imstb_truetype.h
imstb_textedit.h
imstb_rectpack.h
)

configure_file(cmake/imgui-config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/imgui-config.cmake
@ONLY)

install(FILES
${CMAKE_CURRENT_BINARY_DIR}/imgui-config.cmake
DESTINATION lib/cmake/imgui)

60 changes: 60 additions & 0 deletions backends/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
if(IMGUI_IMPL_OPENGL2 OR IMGUI_IMPL_OPENGL3)
find_package(OpenGL)
endif()

macro(add_backend backend)
# Construct source and header file names from the backend name
set(SOURCE_FILE "imgui_impl_${backend}.cpp")
set(HEADER_FILE "imgui_impl_${backend}.h")

# Add a library for the backend
add_library_ns(imgui ${backend} STATIC ${SOURCE_FILE})

# Set up include directories
target_include_dir_iface(imgui-${backend} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} include PRIVATE ${IMGUI_INC_DIR})

# Install the target and the corresponding header file
install_target_and_headers(imgui ${backend}
${HEADER_FILE}
)
endmacro()

if(IMGUI_IMPL_WIN32)
add_backend(win32)
endif()

if(OpenGL_FOUND)

if(IMGUI_IMPL_OPENGL2)
add_backend(opengl2)
target_link_libraries(imgui-opengl2 OpenGL::GL)
endif()

if(IMGUI_IMPL_OPENGL3)
add_backend(opengl3)
target_link_libraries(imgui-opengl3 OpenGL::GL)
endif()

endif()

if(IMGUI_IMPL_DX9)
add_backend(dx9)
target_link_libraries(imgui-dx9 d3d9.lib)
endif()

if(IMGUI_IMPL_DX10)
add_backend(dx10)
target_link_libraries(imgui-dx10 d3d10.lib)
endif()

if(IMGUI_IMPL_DX11)
add_backend(dx11)
target_link_libraries(imgui-dx11 d3d11.lib)
endif()

if(IMGUI_IMPL_DX12)
add_backend(dx12)
target_link_libraries(imgui-dx12 d3d12.lib)
endif()


14 changes: 14 additions & 0 deletions cmake/add_library_ns.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function(add_library_ns target_ns target_name)
# Forward all arguments after the target_name
set(args ${ARGN})
set(target_fullname ${target_ns}-${target_name})

# Call the original add_library command with the forwarded arguments
add_library(${target_fullname} ${args})

# Alias it properly
add_library(${target_ns}::${target_name} ALIAS ${target_fullname})

# Export name unambiguate
set_target_properties(${target_fullname} PROPERTIES EXPORT_NAME ${target_name})
endfunction()
46 changes: 46 additions & 0 deletions cmake/imgui-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@PACKAGE_INIT@ # Init

include(CMakeFindDependencyMacro)

if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/imgui-opengl2-targets.cmake" OR
EXISTS "${CMAKE_CURRENT_LIST_DIR}/imgui-opengl3-targets.cmake")
find_dependency(OpenGL)
endif()

include("${CMAKE_CURRENT_LIST_DIR}/imgui-core-targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/imgui-cpp-targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/imgui-jumbo-targets.cmake")

if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/imgui-freetype-targets.cmake")
find_dependency(Freetype)
include("${CMAKE_CURRENT_LIST_DIR}/imgui-freetype-targets.cmake")
endif()

if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/imgui-win32-targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/imgui-win32-targets.cmake")
endif()

if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/imgui-opengl2-targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/imgui-opengl2-targets.cmake")
endif()

if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/imgui-opengl3-targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/imgui-opengl3-targets.cmake")
endif()

if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/imgui-dx9-targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/imgui-dx9-targets.cmake")
endif()

if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/imgui-dx10-targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/imgui-dx10-targets.cmake")
endif()

if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/imgui-dx11-targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/imgui-dx11-targets.cmake")
endif()

if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/imgui-dx12-targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/imgui-dx12-targets.cmake")
endif()

19 changes: 19 additions & 0 deletions cmake/install_target.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function(install_target_and_headers ns name)

set(target_name ${ns}-${name})

install(FILES
${ARGN}
DESTINATION include)

install(TARGETS ${target_name}
EXPORT ${target_name}-targets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin)

install(EXPORT ${target_name}-targets
FILE ${target_name}-targets.cmake
NAMESPACE ${ns}::
DESTINATION lib/cmake/${ns})
endfunction()
10 changes: 10 additions & 0 deletions cmake/target_include_dir_iface.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function(target_include_dir_iface target visibility build-inc install-inc)
set(args ${ARGN})
target_include_directories(
${target}
${visibility}
$<BUILD_INTERFACE:${build-inc}>
$<INSTALL_INTERFACE:${install-inc}>
${args}
)
endfunction()
5 changes: 5 additions & 0 deletions imconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,8 @@ namespace ImGui
void MyFunction(const char* name, MyMatrix44* mtx);
}
*/
#ifdef IMGUI_TLSCTX
struct ImGuiContext;
extern thread_local ImGuiContext* MyImGuiTLS;
#define GImGui MyImGuiTLS
#endif
4 changes: 4 additions & 0 deletions misc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_subdirectory(cpp)
add_subdirectory(single_file)
add_subdirectory(fonts)
add_subdirectory(freetype)
5 changes: 5 additions & 0 deletions misc/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_library_ns(imgui cpp STATIC imgui_stdlib.cpp)
target_include_dir_iface(imgui-cpp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} include PRIVATE ${IMGUI_INC_DIR})
install_target_and_headers(imgui cpp
imgui_stdlib.h
)
1 change: 1 addition & 0 deletions misc/fonts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_executable(fonts-bin2compressed binary_to_compressed_c.cpp)
9 changes: 9 additions & 0 deletions misc/freetype/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
find_package(Freetype)
if(Freetype_FOUND)
add_library_ns(imgui freetype STATIC imgui_freetype.cpp)
target_include_dir_iface(imgui-freetype PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} include PRIVATE ${IMGUI_INC_DIR})
target_link_libraries(imgui-freetype Freetype::Freetype)
install_target_and_headers(imgui freetype
imgui_freetype.h
)
endif()
5 changes: 5 additions & 0 deletions misc/single_file/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_library_ns(imgui jumbo INTERFACE)
target_include_dir_iface(imgui-jumbo INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} include INTERFACE ${IMGUI_INC_DIR})
install_target_and_headers(imgui jumbo
imgui_single_file.h
)