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

build: cmake support, examples included #255

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
imgui.ini
/out/
47 changes: 47 additions & 0 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
cmake_minimum_required(VERSION 2.8.12)
project(imgui)

set(inst_src_dir include/imgui_src)

set(root ${CMAKE_CURRENT_SOURCE_DIR}/..)
set(phdrs ${root}/imgui.h)
set(hdrs ${root}/stb_rect_pack.h ${root}/stb_textedit.h ${root}/stb_truetype.h)
set(srcs ${root}/imgui.cpp)
set(inst_srcs ${inst_src_dir}/imgui.cpp)

add_library(imgui INTERFACE)

if(CMAKE_VERSION VERSION_LESS 3.3)
message(WARNING "The config-module created by your CMake version (< 3.3) "
"can't attach the imgui source files to the exported library. Please see the "
"documentation in `_IMGUI_WORKS_WITHOUT_CMAKE.md` for further instructions.")
else()
target_sources(imgui INTERFACE
$<BUILD_INTERFACE:${srcs}>
$<INSTALL_INTERFACE:${inst_srcs}>)
endif()

set(IMGUI_SOURCES ${srcs})

install(FILES ${srcs} ${hdrs} DESTINATION ${inst_src_dir})

target_include_directories(imgui INTERFACE
$<BUILD_INTERFACE:${root}>
$<INSTALL_INTERFACE:include>)

target_compile_definitions(imgui INTERFACE IMGUI_CONDITIONAL_IMCONFIG_H)

install(TARGETS imgui EXPORT imgui-targets DESTINATION lib)

install(EXPORT imgui-targets
DESTINATION lib/cmake/imgui)

install(FILES ${phdrs} DESTINATION include)
install(FILES ${root}/imconfig.h DESTINATION include RENAME imconfig-sample.h)

file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/hide/imgui-config.cmake "
include(\${CMAKE_CURRENT_LIST_DIR}/imgui-targets.cmake)
get_filename_component(IMGUI_SOURCES \"\${CMAKE_CURRENT_LIST_DIR}/../../../include/imgui_src/imgui.cpp\" ABSOLUTE)
")

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/hide/imgui-config.cmake DESTINATION lib/cmake/imgui)
48 changes: 48 additions & 0 deletions cmake/_IMGUI_WORKS_WITHOUT_CMAKE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Please note that `imgui` has been designed to be used without CMake or any other
build tool, by simply adding the `imgui` source files to your project.

But in case it fits your workflow better you can use `imgui` just like
any other CMake-enabled library. To allow for the same customization features
as without CMake, `imgui` will be added to your project as a set of source
files and not as an actual compiled library.

Usage:
------

Build and install the CMakeLists.txt in this directory to create the
`imgui-config.cmake` config-module. Then,

- with CMake version 3.3 or later:

find_package(imgui REQUIRED)
...
target_link_libraries(<my-target> ... imgui ...)

- with CMake versions 2.8.12 - 3.2

find_package(imgui REQUIRED)
...
add_exutable(<my-target> ... ${IMGUI_SOURCES})
target_link_libraries(<my-target> ... imgui ...)

The first method requires the CMake target property `INTERFACE_SOURCES` which
is implemented completely only in V3.3.

For a quick test of the CMake build and also as an example check out
`cmake-testbuild.sh` in this directory.

Customization
-------------

To use a custom imconfig.h,

- copy `include/imconfig-sample.h` from the install directory to your project and
edit it

- define `IMGUI_INCLUDE_IMCONFIG_H` in your project and add its path:

target_compile_definitions(<my-target> PRIVATE IMGUI_INCLUDE_IMCONFIG_H)
target_include_directories(<my-target> PRIVATE <path-to-imgui.h>)

Also, see `examples/imconfig_example`

48 changes: 48 additions & 0 deletions cmake/cmake-testbuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/sh

# This script
#
# - clones and installs glfw3, dependency of the examples
# - installs imgui
# - builds the examples in separate build tree so the imgui config
# module is also tested
#
# Options:
#
# - you can set the variable `generator_option` to a
# non-default generator, example:
#
# generator_option=-GXcode ./cmake-testbuild.sh


root=$(cd $(dirname $0)/..; pwd)

set -ex

make_install() {
srcdir=$1
builddir=$2
extraflags=$3
cmake -DCMAKE_INSTALL_PREFIX=$root/out -DCMAKE_PREFIX_PATH=$root/out \
-H$srcdir -B$root/out/build/$builddir -DCMAKE_BUILD_TYPE=Debug $extraflags \
$generator_option
cmake --build $root/out/build/$builddir --target install --config Debug
cmake $root/out/build/$builddir -DCMAKE_BUILD_TYPE=Release
cmake --build $root/out/build/$builddir --target install --config Release
}

if test ! -f $root/out/glfw_src/CMakeLists.txt; then
git clone --depth 1 https://github.com/shaxbee/glfw.git $root/out/glfw_src
fi

make_install $root/out/glfw_src glfw "-DGLFW_BUILD_DOCS=0 -DGLFW_BUILD_EXAMPLES=0 -DGLFW_BUILD_TESTS=0 -DCMAKE_DEBUG_POSTFIX=d"
make_install $root/cmake imgui
make_install $root/examples/opengl_example opengl_example
make_install $root/examples/opengl3_example opengl3_example

make_install $root/examples/imconfig_example imconfig_example

set +x
echo "---- You can run these examples from $root/out/bin: ----"

ls -l $root/out/bin
8 changes: 6 additions & 2 deletions examples/README.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Those are standalone ready-to-build applications to demonstrate ImGui.
Unfortunately in 2015 it is still a massive pain to create and maintain portable build files.
I choose to provide Visual Studio 10 .sln files and Makefile for Linux/OSX.
I choose to provide Visual Studio 10 .sln files and Makefile for Linux/OSX.
Also, CMake project files are provided, too.
Please let me know if they don't work with your setup!
You can probably just import the .cpp files into your own system and figure out the linkage from there.

Expand All @@ -22,3 +22,7 @@ directx11_example/
DirectX11 example, Windows only.
This is quite long and tedious, because: DirectX11.

imconfig_example/
This example demonstrates how to use imconfig.h. The code itself
is the same as the opengl_example but a custom imconfig.h file
is included.
31 changes: 31 additions & 0 deletions examples/imconfig_example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 2.8.12)

project(imconfig_example)

find_package(OpenGL REQUIRED)
find_package(imgui REQUIRED)

find_package(glfw3 REQUIRED CONFIG)

set(srcs imgui_impl_glfw.cpp imgui_impl_glfw.h main.cpp imconfig.h)

if(CMAKE_VERSION VERSION_LESS 3.3)
list(APPEND srcs ${IMGUI_SOURCES})
endif()

add_executable(imconfig_example ${srcs})

target_compile_definitions(imconfig_example PRIVATE IMGUI_INCLUDE_IMCONFIG_H)

target_include_directories(imconfig_example PRIVATE
${OPENGL_INCLUDE_DIR}
${CMAKE_CURRENT_LIST_DIR} # needed for imgui.h to find our imconfig.h
)

target_link_libraries(imconfig_example
${OPENGL_LIBRARIES}
glfw
imgui)

install(TARGETS imconfig_example DESTINATION bin)
set_target_properties(imconfig_example PROPERTIES DEBUG_POSTFIX _d)
33 changes: 33 additions & 0 deletions examples/imconfig_example/imconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//-----------------------------------------------------------------------------
// USER IMPLEMENTATION
// This file contains compile-time options for ImGui.
// Other options (memory allocation overrides, callbacks, etc.) can be set at runtime via the ImGuiIO structure - ImGui::GetIO().
// Usage: to make imgui.h include imconfig.h:
// - either don't define IMGUI_CONDITIONAL_IMCONFIG_H
// - or define both IMGUI_CONDITIONAL_IMCONFIG_H and IMGUI_INCLUDE_IMCONFIG_H
// (the cmake-installed `imgui` target defines
// IMGUI_CONDITIONAL_IMCONFIG_H automatically)
//-----------------------------------------------------------------------------

#pragma once

#include "myvec.h"

//---- Don't implement help and test window functionality (ShowUserGuide()/ShowStyleEditor()/ShowTestWindow() methods will be empty)
#define IMGUI_DISABLE_TEST_WINDOWS

//---- Don't define obsolete functions names
#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS

//---- Implement STB libraries in a namespace to avoid conflicts
#define IMGUI_STB_NAMESPACE ImStb

//---- Define constructor and implicit cast operators to convert back<>forth from your math types and ImVec2/ImVec4.
#define IM_VEC2_CLASS_EXTRA \
ImVec2(const MyVec2& f) { x = f.x; y = f.y; } \
operator MyVec2() const { return MyVec2(x,y); }

#define IM_VEC4_CLASS_EXTRA \
ImVec4(const MyVec4& f) { x = f.x; y = f.y; z = f.z; w = f.w; } \
operator MyVec4() const { return MyVec4(x,y,z,w); }

Loading