Skip to content

Instantly share code, notes, and snippets.

@rokups
Last active March 23, 2020 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rokups/568c4f69f25793d6c5b90fa958d5b5a4 to your computer and use it in GitHub Desktop.
Save rokups/568c4f69f25793d6c5b90fa958d5b5a4 to your computer and use it in GitHub Desktop.
# The MIT License (MIT)
#
# Copyright (c) 2014-2020 Omar Cornut
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Example of using Dear ImGui in your CMake project:
#
# # Set various library options:
# set(IMGUI_SDL_TARGET sdl CACHE STRING "" FORCE)
# # Include Dear ImGui into build.
# add_subdirectory(path/to/imgui)
# <..>
# # Link Dear ImGui to your application:
# # imgui - a main library target
# # imgui-sdl - a platform backend target
# # imgui-dx11 - a graphics backend target
# target_link_libraries(YourApplication PUBLIC imgui imgui-sdl imgui-dx11)
#
# A list of defined targets:
# imgui-sdl: platform backend for Windows/Linux/MacOS/etc using SDL2.
# imgui-glfw: platform backend for Windows/Linux/MacOS/etc using GLFW.
# imgui-osx: platform backend for MacOS.
# imgui-win32: platform backend for Windows.
# imgui-glut: platform backend for Windows/Linux/MacOS using glut/freeglut with OpenGL.
# imgui-metal: graphics backend for MacOS using Metal graphics API.
# imgui-opengl3: graphics backend for Windows/Linux/MacOS/etc using OpenGL3 graphics API.
# imgui-opengl2: graphics backend for Windows/Linux/MacOS/etc using OpenGL2 graphics API.
# imgui-vulkan: graphics backend for Windows/Linux/MacOS/etc using Vulkan graphics API.
# imgui-dx9: graphics backend for Windows using DirectX 9 graphics API.
# imgui-dx10: graphics backend for Windows using DirectX 10 graphics API.
# imgui-dx11: graphics backend for Windows using DirectX 11 graphics API.
# imgui-dx12: graphics backend for Windows using DirectX 12 graphics API.
#
cmake_minimum_required (VERSION 3.2)
project (imgui)
# CMake: 3.13: option() honors normal variables. Sufficiently new CMake and this policy allow user to configure library
# by setting plain variables as opposed to cache variables as demonstrated in example.
cmake_policy(SET CMP0077 NEW)
option(IMGUI_EXAMPLES "Build ImGui examples" ON)
option(IMGUI_DEMO "Include the ImGui demo window implementation in library" ON)
option(IMGUI_DISABLE_OBSOLETE_FUNCTIONS "Disable deprecated functions" ON)
option(IMGUI_DISABLE_FILE_FUNCTIONS "Disable use of file functions" OFF)
option(IMGUI_ENABLE_STDLIB_SUPPORT "Enable extra functions taking stdlib types" OFF)
option(IMGUI_ENABLE_COVERAGE "Enable coverage testing for supported compilers" OFF)
set(IMGUI_SDL_TARGET "" CACHE STRING "A custom SDL target name that will be used for linking to examples.")
set(IMGUI_GLFW_TARGET "" CACHE STRING "A custom GLFW target name that will be used for linking to examples.")
set(IMGUI_GLUT_TARGET "" CACHE STRING "A custom GLFW target name that will be used for linking to examples.")
option(IMGUI_IMPL_SDL "Build the SDL implementation (only if supported)" ON)
option(IMGUI_IMPL_METAL "Build the Metal implementation (only if supported)" ${APPLE})
option(IMGUI_IMPL_OSX "Build the OSX implementation (only if supported)" ${APPLE})
option(IMGUI_IMPL_WIN32 "Build the Win32 (native winapi) implementation (only if supported)" ${WIN32})
option(IMGUI_IMPL_GLFW "Build the GLFW implementation (only if supported)" ON)
option(IMGUI_IMPL_GLUT "Build the GLUT implementation (only if supported)" ON)
option(IMGUI_IMPL_OPENGL3 "Build the OpenGL 3 implementation (only if supported)" ON)
option(IMGUI_IMPL_OPENGL2 "Build the OpenGL 2 (legacy) implementation (only if supported)" ${IMGUI_IMPL_OPENGL3})
option(IMGUI_IMPL_DX9 "Build the DirectX 9 implementation (only if supported)" ${WIN32})
option(IMGUI_IMPL_DX10 "Build the DirectX 10 implementation (only if supported)" ${WIN32})
option(IMGUI_IMPL_DX11 "Build the DirectX 11 implementation (only if supported)" ${WIN32})
option(IMGUI_IMPL_DX12 "Build the DirectX 12 implementation (only if supported)" ${WIN32})
option(IMGUI_IMPL_VULKAN "Build the Vulkan implementation (only if supported)" ON)
if (CMAKE_SYSTEM_NAME STREQUAL Emscripten)
set (IMGUI_IMPL_GLFW OFF)
set (IMGUI_IMPL_GLUT OFF)
endif ()
# Tidy up build dir, but only if user has not configured it yet.
if (NOT DEFINED CMAKE_RUNTIME_OUTPUT_DIRECTORY)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
endif ()
if (NOT DEFINED CMAKE_LIBRARY_OUTPUT_DIRECTORY)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
endif ()
if (NOT DEFINED CMAKE_ARCHIVE_OUTPUT_DIRECTORY)
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
endif ()
# Use fresh C++ standard, but only if user has not configured it yet.
if (NOT DEFINED CMAKE_CXX_STANDARD)
set (CMAKE_CXX_STANDARD 11)
endif ()
# Helper variables. They allow to use this CMakeLists.txt file while it is in examples or in root directory. You may
# copy/symlink this file to imgui root directory in order to help IDEs like CLion to recognize headers as part of the
# project.
if (EXISTS ${CMAKE_CURRENT_LIST_DIR}/imgui.h)
# CMakeLists.txt is in root directory.
set (IMGUI_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR})
set (IMGUI_EXAMPLES_DIR ${IMGUI_ROOT_DIR}/examples)
else ()
# CMakeLists.txt is in examples directory.
set (IMGUI_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
set (IMGUI_EXAMPLES_DIR ${CMAKE_CURRENT_LIST_DIR})
endif ()
# Set up main library
add_library(imgui ${IMGUI_ROOT_DIR}/imgui.h ${IMGUI_ROOT_DIR}/imgui_internal.h ${IMGUI_ROOT_DIR}/imgui.cpp
${IMGUI_ROOT_DIR}/imgui_draw.cpp ${IMGUI_ROOT_DIR}/imgui_widgets.cpp
)
if (IMGUI_DEMO)
target_sources(imgui PRIVATE ${IMGUI_ROOT_DIR}/imgui_demo.cpp)
endif ()
if (IMGUI_DISABLE_OBSOLETE_FUNCTIONS)
target_compile_definitions(imgui PUBLIC -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1)
endif ()
if (IMGUI_DISABLE_FILE_FUNCTIONS)
target_compile_definitions(imgui PUBLIC -DIMGUI_DISABLE_FILE_FUNCTIONS=1)
endif ()
if (IMGUI_USER_CONFIG)
target_compile_definitions(imgui PUBLIC "-DIMGUI_USER_CONFIG=<${IMGUI_USER_CONFIG}>")
endif ()
target_include_directories(imgui PUBLIC ${IMGUI_ROOT_DIR})
if (NOT IMGUI_DEMO)
target_compile_definitions(imgui PUBLIC -DIMGUI_DISABLE_DEMO_WINDOWS=1)
endif ()
if (IMGUI_ENABLE_STDLIB_SUPPORT)
target_include_directories(imgui PUBLIC misc/cpp)
target_sources(imgui PRIVATE misc/cpp/imgui_stdlib.h misc/cpp/imgui_stdlib.cpp)
endif ()
if (IMGUI_ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_compile_options(imgui PUBLIC -fprofile-arcs -ftest-coverage)
target_link_libraries(imgui PUBLIC -fprofile-arcs -ftest-coverage)
target_link_libraries(imgui PUBLIC gcov)
endif ()
# Ignore silently when required headers are missing.
set(CMAKE_REQUIRED_QUIET ON)
include(CheckIncludeFile)
# PkgConfig will be needed for SDL and glfw on some platforms.
find_package(PkgConfig QUIET)
# gl3w OpenGL loader.
if (NOT CMAKE_SYSTEM_NAME STREQUAL Emscripten)
add_library(imgui-gl3w INTERFACE)
target_sources(imgui-gl3w INTERFACE ${IMGUI_EXAMPLES_DIR}/libs/gl3w/GL/gl3w.c)
target_compile_definitions(imgui-gl3w INTERFACE -DIMGUI_IMPL_OPENGL_LOADER_GL3W=1)
target_include_directories(imgui-gl3w INTERFACE ${IMGUI_EXAMPLES_DIR}/libs/gl3w)
if (APPLE)
target_link_libraries(imgui-gl3w INTERFACE "-framework CoreFoundation")
endif ()
endif ()
# Backends
if (IMGUI_IMPL_SDL)
find_package(SDL2 QUIET)
if (CMAKE_SYSTEM_NAME STREQUAL Emscripten)
# Nothing to do for emscripten builds. SDL is bunled with the compiler.
elseif (IMGUI_SDL_TARGET)
# Custom user target was set. No auto-detection required.
elseif (TARGET SDL2::SDL2)
# Some platforms (Linux) have SDL target properly exported as CMake target.
elseif (SDL2_FOUND)
# Platforms that do not export target but use old CMake conventions can be handled this way.
add_library(SDL2::SDL2 INTERFACE IMPORTED)
target_link_libraries(SDL2::SDL2 INTERFACE ${SDL2_LIBRARIES})
target_include_directories(SDL2::SDL2 INTERFACE ${SDL2_INCLUDE_DIRS})
elseif (NOT "$ENV{SDL2_DIR}" STREQUAL "")
# On windows we may set SDL2_DIR environment variable and link to binary SDL distribution.
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(IMGUI_SDL_PLATFORM_ARCH x64)
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(IMGUI_SDL_PLATFORM_ARCH x86)
else ()
message(FATAL_ERROR "Unsupported platform.")
endif()
add_library(SDL2::SDL2 SHARED IMPORTED)
target_include_directories(SDL2::SDL2 INTERFACE "$ENV{SDL2_DIR}/include")
target_link_libraries(SDL2::SDL2 INTERFACE $ENV{SDL2_DIR}/lib/${IMGUI_SDL_PLATFORM_ARCH}/SDL2main.lib)
set_target_properties(SDL2::SDL2 PROPERTIES
IMPORTED_LOCATION "$ENV{SDL2_DIR}/lib/${IMGUI_SDL_PLATFORM_ARCH}/SDL2.dll"
IMPORTED_IMPLIB "$ENV{SDL2_DIR}/lib/${IMGUI_SDL_PLATFORM_ARCH}/SDL2.lib"
)
elseif (PkgConfig_FOUND)
# Rest of the platforms (like MacOS) can consume SDL via pkg-config.
# CMake 3.6 supports IMPORTED_TARGET parameter which creates PkgConfig::sdl2 target that we can easily link to.
# TODO: Switch to using IMPORTED_TARGET when CMake minimal version is increased.
pkg_check_modules(SDL2 QUIET sdl2)
if (SDL2_FOUND)
add_library(SDL2::SDL2 INTERFACE IMPORTED)
target_link_libraries(SDL2::SDL2 INTERFACE ${SDL2_LDFLAGS})
target_compile_options(SDL2::SDL2 INTERFACE ${SDL2_CFLAGS})
endif ()
endif ()
if (NOT IMGUI_SDL_TARGET)
set(IMGUI_SDL_TARGET SDL2::SDL2)
endif ()
if (TARGET ${IMGUI_SDL_TARGET} OR CMAKE_SYSTEM_NAME STREQUAL Emscripten)
add_library (imgui-sdl INTERFACE)
target_sources(imgui-sdl INTERFACE
${IMGUI_EXAMPLES_DIR}/imgui_impl_sdl.h
${IMGUI_EXAMPLES_DIR}/imgui_impl_sdl.cpp
)
target_include_directories(imgui-sdl INTERFACE ${IMGUI_EXAMPLES_DIR})
target_link_libraries(imgui-sdl INTERFACE imgui)
if (TARGET ${IMGUI_SDL_TARGET})
target_link_libraries(imgui-sdl INTERFACE ${IMGUI_SDL_TARGET})
endif ()
else ()
message(STATUS "IMGUI_IMPL_SDL set to ON but SDL2 could not be found.")
endif ()
endif ()
if (IMGUI_IMPL_METAL AND APPLE)
add_library(imgui-metal INTERFACE)
target_sources(imgui-metal INTERFACE
${IMGUI_EXAMPLES_DIR}/imgui_impl_metal.h
${IMGUI_EXAMPLES_DIR}/imgui_impl_metal.mm
)
target_link_libraries(imgui-metal INTERFACE imgui "-framework Cocoa" "-framework Metal" "-framework QuartzCore")
target_compile_options(imgui-metal INTERFACE -fobjc-arc)
target_include_directories(imgui-metal INTERFACE ${IMGUI_EXAMPLES_DIR})
endif ()
if (IMGUI_IMPL_OSX AND APPLE)
add_library(imgui-osx INTERFACE)
target_sources(imgui-osx INTERFACE
${IMGUI_EXAMPLES_DIR}/imgui_impl_osx.h
${IMGUI_EXAMPLES_DIR}/imgui_impl_osx.mm
)
target_link_libraries(imgui-osx INTERFACE imgui "-framework Cocoa" "-framework AppKit")
target_compile_options(imgui-osx INTERFACE -fobjc-arc)
target_include_directories(imgui-osx INTERFACE ${IMGUI_EXAMPLES_DIR})
endif ()
if (IMGUI_IMPL_WIN32 AND WIN32)
add_library(imgui-win32 INTERFACE)
target_sources(imgui-win32 INTERFACE
${IMGUI_EXAMPLES_DIR}/imgui_impl_win32.h
${IMGUI_EXAMPLES_DIR}/imgui_impl_win32.cpp
)
target_include_directories(imgui-win32 INTERFACE ${IMGUI_EXAMPLES_DIR})
endif ()
if (IMGUI_IMPL_GLFW)
if (IMGUI_GLFW_TARGET)
# Custom user target was set. No auto-detection required.
elseif (TARGET glfw)
# GLFW exists already. Nothing to do.
elseif (EXISTS ${IMGUI_ROOT_DIR}/../glfw/CMakeLists.txt)
# When parent directory contains GLFW source code - we can build it directly.
set (GLFW_STANDALONE OFF)
set (GLFW_INSTALL OFF)
set (GLFW_BUILD_DOCS OFF)
add_subdirectory (${IMGUI_ROOT_DIR}/../glfw ${CMAKE_CURRENT_BINARY_DIR}/glfw)
elseif (MSVC)
# We ship compiled libraries in our repository for Visual Studio.
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(IMGUI_GLFW_PLATFORM_BITS 64)
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(IMGUI_GLFW_PLATFORM_BITS 32)
else ()
message(FATAL_ERROR "Unsupported platform.")
endif()
add_library(glfw INTERFACE IMPORTED)
target_link_libraries(glfw INTERFACE libs/glfw/lib-vc2010-${IMGUI_GLFW_PLATFORM_BITS}/glfw3.lib)
target_include_directories(glfw INTERFACE libs/glfw/include)
elseif (PkgConfig_FOUND)
# CMake 3.6 supports IMPORTED_TARGET parameter which creates PkgConfig::glfw target that we can easily link to.
# TODO: Switch to using IMPORTED_TARGET when CMake minimal version is increased.
pkg_check_modules(GLFW QUIET glfw3)
if (GLFW_FOUND)
add_library(glfw INTERFACE IMPORTED)
target_link_libraries(glfw INTERFACE ${GLFW_LDFLAGS})
target_compile_options(glfw INTERFACE ${GLFW_CFLAGS})
endif ()
endif ()
if (NOT IMGUI_GLFW_TARGET)
set(IMGUI_GLFW_TARGET glfw)
endif ()
if (TARGET ${IMGUI_GLFW_TARGET})
add_library(imgui-glfw INTERFACE)
target_sources(imgui-glfw INTERFACE
${IMGUI_EXAMPLES_DIR}/imgui_impl_glfw.h
${IMGUI_EXAMPLES_DIR}/imgui_impl_glfw.cpp
)
target_link_libraries(imgui-glfw INTERFACE ${CMAKE_DL_LIBS} ${IMGUI_GLFW_TARGET})
target_include_directories(imgui-glfw INTERFACE ${IMGUI_EXAMPLES_DIR})
else ()
message(STATUS "IMGUI_IMPL_GLFW set to ON but GLFW could not be found.")
endif ()
endif ()
if (IMGUI_IMPL_OPENGL2 OR IMGUI_IMPL_OPENGL3)
# OpenGL. GLVND is vendor-agnostic OpenGL dispatch library on Linux.
set(OpenGL_GL_PREFERENCE "GLVND")
include(FindOpenGL)
if (NOT OPENGL_FOUND)
message(FATAL_ERROR "IMGUI_IMPL_OPENGL2 or IMGUI_IMPL_OPENGL3 set to ON but OpenGL could not be found.")
endif ()
if (NOT TARGET OpenGL::GL)
add_library(OpenGL::GL INTERFACE IMPORTED)
target_include_directories(OpenGL::GL INTERFACE ${OPENGL_INCLUDE_DIR})
target_link_libraries(OpenGL::GL INTERFACE ${OPENGL_gl_LIBRARY})
endif ()
if (IMGUI_IMPL_OPENGL2)
add_library(imgui-opengl2 INTERFACE)
target_sources(imgui-opengl2 INTERFACE ${IMGUI_EXAMPLES_DIR}/imgui_impl_opengl2.cpp)
target_link_libraries (imgui-opengl2 INTERFACE imgui OpenGL::GL)
target_include_directories(imgui-opengl2 INTERFACE ${IMGUI_EXAMPLES_DIR})
endif ()
if (IMGUI_IMPL_OPENGL3)
add_library(imgui-opengl3 INTERFACE)
target_sources(imgui-opengl3 INTERFACE ${IMGUI_EXAMPLES_DIR}/imgui_impl_opengl3.cpp)
if (TARGET imgui-gl3w)
# OpenGL3 target is built with emscripten, but gl3w is not required.
target_link_libraries (imgui-opengl3 INTERFACE imgui-gl3w)
endif ()
target_link_libraries (imgui-opengl3 INTERFACE imgui OpenGL::GL)
target_include_directories(imgui-opengl3 INTERFACE ${IMGUI_EXAMPLES_DIR})
endif ()
endif ()
if (IMGUI_IMPL_GLUT)
find_package(GLUT QUIET)
if (GLUT_glut_LIBRARY OR IMGUI_GLUT_TARGET)
add_library(imgui-glut INTERFACE)
target_sources(imgui-glut INTERFACE
${IMGUI_EXAMPLES_DIR}/imgui_impl_glut.h
${IMGUI_EXAMPLES_DIR}/imgui_impl_glut.cpp
)
target_link_libraries(imgui-glut INTERFACE imgui)
if (IMGUI_GLUT_TARGET)
target_link_libraries(imgui-glut INTERFACE ${IMGUI_GLUT_TARGET})
else ()
target_include_directories(imgui-glut INTERFACE ${GLUT_INCLUDE_DIR} ${IMGUI_EXAMPLES_DIR})
target_link_libraries(imgui-glut INTERFACE imgui ${GLUT_glut_LIBRARY})
if (APPLE)
target_link_libraries(imgui-glut INTERFACE ${GLUT_cocoa_LIBRARY})
endif ()
endif ()
else ()
message (STATUS "IMGUI_IMPL_GLUT set to ON but GLUT could not be found.")
endif ()
endif ()
if (IMGUI_IMPL_DX9)
check_include_file(d3d9.h HAS_D3D9)
if (HAS_D3D9)
add_library(imgui-dx9 INTERFACE)
target_sources(imgui-dx9 INTERFACE
${IMGUI_EXAMPLES_DIR}/imgui_impl_dx9.h
${IMGUI_EXAMPLES_DIR}/imgui_impl_dx9.cpp
)
target_link_libraries(imgui-dx9 INTERFACE imgui d3d9)
target_include_directories(imgui-dx9 INTERFACE ${IMGUI_EXAMPLES_DIR})
else ()
message (STATUS "IMGUI_IMPL_DX9 set to ON but DirectX 9 could not be found.")
endif ()
endif ()
if (IMGUI_IMPL_DX10)
check_include_file(d3d10.h HAS_D3D10)
if (HAS_D3D10)
add_library(imgui-dx10 INTERFACE)
target_sources(imgui-dx10 INTERFACE
${IMGUI_EXAMPLES_DIR}/imgui_impl_dx10.h
${IMGUI_EXAMPLES_DIR}/imgui_impl_dx10.cpp
)
target_link_libraries(imgui-dx10 INTERFACE imgui d3d10)
target_include_directories(imgui-dx10 INTERFACE ${IMGUI_EXAMPLES_DIR})
else ()
message (STATUS "IMGUI_IMPL_DX10 set to ON but DirectX 10 could not be found.")
endif ()
endif ()
if (IMGUI_IMPL_DX11)
check_include_file(d3d11.h HAS_D3D11)
if (HAS_D3D11)
add_library(imgui-dx11 INTERFACE)
target_sources(imgui-dx11 INTERFACE
${IMGUI_EXAMPLES_DIR}/imgui_impl_dx11.h
${IMGUI_EXAMPLES_DIR}/imgui_impl_dx11.cpp
)
target_link_libraries(imgui-dx11 INTERFACE imgui d3d11)
target_include_directories(imgui-dx11 INTERFACE ${IMGUI_EXAMPLES_DIR})
else ()
message (STATUS "IMGUI_IMPL_DX11 set to ON but DirectX 11 could not be found.")
endif ()
endif ()
if (IMGUI_IMPL_DX12)
check_include_file(d3d12.h HAS_D3D12)
if (HAS_D3D11)
add_library(imgui-dx12 INTERFACE)
target_sources(imgui-dx12 INTERFACE
${IMGUI_EXAMPLES_DIR}/imgui_impl_dx12.h
${IMGUI_EXAMPLES_DIR}/imgui_impl_dx12.cpp
)
target_link_libraries(imgui-dx12 INTERFACE imgui d3d12 dxgi)
target_include_directories(imgui-dx12 INTERFACE ${IMGUI_EXAMPLES_DIR})
else ()
message (STATUS "IMGUI_IMPL_DX12 set to ON but DirectX 12 could not be found.")
endif ()
endif ()
if (IMGUI_IMPL_VULKAN)
find_package(Vulkan QUIET)
if (Vulkan_FOUND)
add_library(imgui-vulkan INTERFACE)
target_sources(imgui-vulkan INTERFACE ${IMGUI_EXAMPLES_DIR}/imgui_impl_vulkan.cpp)
target_link_libraries (imgui-vulkan INTERFACE imgui ${Vulkan_LIBRARIES} OpenGL::GL)
target_include_directories(imgui-vulkan INTERFACE ${Vulkan_INCLUDE_DIRS} ${IMGUI_EXAMPLES_DIR})
else ()
message(STATUS "IMGUI_IMPL_VULKAN set to ON but Vulkan could not be found.")
endif ()
endif ()
if (IMGUI_EXAMPLES)
function (IMGUI_EXAMPLE)
cmake_parse_arguments(IMGUI_EXAMPLE "" "TARGET;SOURCE" "LINKS" ${ARGN})
if (NOT IMGUI_EXAMPLE_SOURCE)
set (IMGUI_EXAMPLE_SOURCE main.cpp)
endif ()
# Check whether dependency libraries are available.
foreach (dep ${IMGUI_EXAMPLE_LINKS})
if (NOT TARGET ${dep})
return ()
endif ()
endforeach ()
# Create a target.
add_executable(${IMGUI_EXAMPLE_TARGET} ${IMGUI_EXAMPLES_DIR}/${IMGUI_EXAMPLE_TARGET}/${IMGUI_EXAMPLE_SOURCE})
target_link_libraries(${IMGUI_EXAMPLE_TARGET} PRIVATE imgui ${IMGUI_EXAMPLE_LINKS})
list(FIND IMGUI_EXAMPLE_LINKS imgui-sdl LINKS_SDL)
if (NOT LINKS_SDL EQUAL -1 AND WIN32)
get_target_property(SDL_IS_IMPORTED ${IMGUI_SDL_TARGET} IMPORTED)
if (SDL_IS_IMPORTED)
# When program links to imgui-sdl and imported target ${IMGUI_SDL_TARGET} exists - copy SDL dll.
add_custom_command(TARGET ${IMGUI_EXAMPLE_TARGET} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:${IMGUI_SDL_TARGET}> $<TARGET_FILE_DIR:${IMGUI_EXAMPLE_TARGET}>)
endif ()
endif ()
endfunction()
if (APPLE)
# MacOS Metal example
# FIXME: This sample is still broken.
add_executable(example_apple_metal MACOSX_BUNDLE
${IMGUI_EXAMPLES_DIR}/example_apple_metal/Shared/main.m
${IMGUI_EXAMPLES_DIR}/example_apple_metal/Shared/AppDelegate.h
${IMGUI_EXAMPLES_DIR}/example_apple_metal/Shared/AppDelegate.m
${IMGUI_EXAMPLES_DIR}/example_apple_metal/Shared/Renderer.h
${IMGUI_EXAMPLES_DIR}/example_apple_metal/Shared/Renderer.mm
${IMGUI_EXAMPLES_DIR}/example_apple_metal/Shared/ViewController.h
${IMGUI_EXAMPLES_DIR}/example_apple_metal/Shared/ViewController.mm
${IMGUI_EXAMPLES_DIR}/example_apple_metal/macOS/Base.lproj/Main.storyboard
)
target_link_libraries(example_apple_metal imgui imgui-osx imgui-metal)
# Generate new plist
file(READ ${IMGUI_EXAMPLES_DIR}/example_apple_metal/macOS/Info-macOS.plist INFO_PLIST)
string(REPLACE "$(EXECUTABLE_NAME)" example_apple_metal INFO_PLIST "${INFO_PLIST}")
string(REPLACE "$(PRODUCT_BUNDLE_IDENTIFIER)" org.imgui.example.apple-metal-macos INFO_PLIST "${INFO_PLIST}")
string(REPLACE "$(MACOSX_DEPLOYMENT_TARGET)" 10.12 INFO_PLIST "${INFO_PLIST}")
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/example_apple_metal-info.plist "${INFO_PLIST}")
# Consume new plist
set_property(TARGET example_apple_metal PROPERTY
MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_BINARY_DIR}/example_apple_metal-info.plist)
# Compile storyboard
if(NOT "${CMAKE_GENERATOR}" MATCHES "^Xcode.*")
# Xcode compiles storyboard automatically. We must do it manually when using other build systems.
find_program(IBTOOL ibtool HINTS /usr/bin ${OSX_DEVELOPER_ROOT}/usr/bin)
add_custom_command(TARGET example_apple_metal POST_BUILD COMMAND ${IBTOOL} --errors --warnings --notices
--output-format human-readable-text --compile
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/example_apple_metal.app/Contents/Resources/Base.lproj/Main.storyboardc
${IMGUI_EXAMPLES_DIR}/example_apple_metal/macOS/Base.lproj/Main.storyboard
COMMENT "Compiling storyboard"
)
endif()
IMGUI_EXAMPLE(TARGET example_apple_opengl2 LINKS imgui-osx imgui-opengl2 SOURCE main.mm)
IMGUI_EXAMPLE(TARGET example_glfw_metal LINKS imgui-glfw imgui-metal SOURCE main.mm)
IMGUI_EXAMPLE(TARGET example_sdl_metal LINKS imgui-sdl imgui-metal SOURCE main.mm)
endif ()
if (WIN32)
IMGUI_EXAMPLE(TARGET example_sdl_directx11 LINKS imgui-sdl imgui-dx11)
IMGUI_EXAMPLE(TARGET example_win32_directx9 LINKS imgui-win32 imgui-dx9)
IMGUI_EXAMPLE(TARGET example_win32_directx10 LINKS imgui-win32 imgui-dx10)
IMGUI_EXAMPLE(TARGET example_win32_directx11 LINKS imgui-win32 imgui-dx11)
IMGUI_EXAMPLE(TARGET example_win32_directx12 LINKS imgui-win32 imgui-dx12)
IMGUI_EXAMPLE(TARGET example_null)
endif ()
if (CMAKE_SYSTEM_NAME STREQUAL Emscripten)
IMGUI_EXAMPLE(TARGET example_emscripten LINKS imgui-sdl imgui-opengl3)
target_compile_options(example_emscripten PUBLIC -s USE_SDL=2)
target_link_libraries(example_emscripten PUBLIC "-s WASM=1")
target_link_libraries(example_emscripten PUBLIC "-s ALLOW_MEMORY_GROWTH=1")
target_link_libraries(example_emscripten PUBLIC "-s DISABLE_EXCEPTION_CATCHING=1" "-s NO_EXIT_RUNTIME=0")
target_link_libraries(example_emscripten PUBLIC "-s ASSERTIONS=1")
target_link_libraries(example_emscripten PUBLIC "--shell-file ${IMGUI_EXAMPLES_DIR}/example_emscripten/shell_minimal.html")
if (IMGUI_DISABLE_FILE_FUNCTIONS)
target_link_libraries(example_emscripten PUBLIC "-s NO_FILESYSTEM=1")
else ()
target_link_libraries(example_emscripten PUBLIC --no-heap-copy "--preload-file ${IMGUI_ROOT_DIR}/misc/fonts@/fonts")
endif ()
# Uncomment next line to fix possible rendering bugs with Emscripten version older then 1.39.0 (https://github.com/ocornut/imgui/issues/2877)
#target_link_libraries(example_emscripten PUBLIC "-s BINARYEN_TRAP_MODE=clamp")
#target_link_libraries(example_emscripten PUBLIC "-s SAFE_HEAP=1") ## Adds overhead
else ()
IMGUI_EXAMPLE(TARGET example_glfw_opengl2 LINKS imgui-glfw imgui-opengl2)
IMGUI_EXAMPLE(TARGET example_glfw_opengl3 LINKS imgui-glfw imgui-opengl3)
IMGUI_EXAMPLE(TARGET example_glfw_vulkan LINKS imgui-glfw imgui-vulkan)
IMGUI_EXAMPLE(TARGET example_glut_opengl2 LINKS imgui-glut imgui-opengl2)
IMGUI_EXAMPLE(TARGET example_sdl_opengl2 LINKS imgui-sdl imgui-opengl2)
IMGUI_EXAMPLE(TARGET example_sdl_opengl3 LINKS imgui-sdl imgui-opengl3)
IMGUI_EXAMPLE(TARGET example_sdl_vulkan LINKS imgui-sdl imgui-vulkan)
endif ()
endif ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment