|
1 |
| -# Only tested in 3.1, may work in older versions. |
2 |
| -# . |
3 |
| -# Install SFML or set SFML_ROOT and set IMGUI_ROOT prior to running cmake |
4 |
| -# . |
5 |
| -# Provides the follow variables: |
6 |
| -# IMGUI_SFML_INCLUDE_DIRS - imgui and imgui_sfml include paths |
7 |
| -# IMGUI_SOURCES - imgui.cpp source path to link with your binary |
8 |
| -# IMGUI_SFML_SOURCES - imgui_sfml.cpp source path to link with your binary |
9 |
| -# IMGUI_SFML_DEPENDENCIES - found dependencies to link with your library (sfml) |
10 |
| -# . |
11 |
| -# Sample usage: |
12 |
| -# add_subdirectory(repos/imgui-sfml) |
13 |
| -# include_directories("${IMGUI_SFML_INCLUDE_DIRS}") |
14 |
| -# add_executable(MY_PROJECT ${IMGUI_SOURCES} ${IMGUI_SFML_SOURCES} ${SRCS}) |
15 |
| -# ... |
16 |
| -# target_link_libraries(MY_PROJECT ${IMGUI_SFML_DEPENDENCIES}) |
17 |
| -# . |
18 | 1 | cmake_minimum_required(VERSION 3.1)
|
19 | 2 |
|
20 |
| -project(imgui_sfml) |
| 3 | +project(imgui_sfml |
| 4 | + LANGUAGES CXX |
| 5 | + VERSION 2.0 |
| 6 | +) |
21 | 7 |
|
22 |
| -option(IMGUI_SFML_BUILD_EXAMPLES "Build ImGui_SFML examples" ON) |
| 8 | +option(IMGUI_SFML_BUILD_EXAMPLES "Build ImGui_SFML examples" OFF) |
| 9 | +option(IMGUI_SFML_FIND_SFML "Use find_package to find SFML" ON) |
23 | 10 |
|
24 |
| -# Find required libraries |
25 |
| -find_package(SFML 2.2 COMPONENTS graphics window system) |
26 |
| -message(status "** SFML Include: ${SFML_INCLUDE_DIR}") |
27 |
| -message(status "** SFML Libraries: ${SFML_LIBRARIES}") |
28 |
| -if(NOT SFML_FOUND) |
29 |
| - set(SFML_ROOT "" CACHE PATH "SFML top-level directory") |
30 |
| - message("---> SFML 2 directory not found. Set SFML_ROOT to SFML's top-level path (containing \"include\" and \"lib\" directories).\n") |
| 11 | +# If you want to use your own user config when compiling ImGui, please set the following variables |
| 12 | +# For example, if you have your config in /path/to/dir/with/config/myconfig.h, set the variables as follows: |
| 13 | +# |
| 14 | +# IMGUI_SFML_USE_DEFAULT_CONFIG = OFF |
| 15 | +# IMGUI_SFML_USER_CONFIG_DIR = /path/to/dir/with/config |
| 16 | +# IMGUI_SFML_USER_CONFIG_NAME = "myconfig.h" |
| 17 | +# |
| 18 | +# If you set IMGUI_SFML_CONFIG_INSTALL_DIR, ImGui-SFML won't install your custom config, because |
| 19 | +# you might want to do it yourself |
| 20 | +option(IMGUI_SFML_USE_DEFAULT_CONFIG "Use default imconfig-SFML.h" ON) |
| 21 | +set(IMGUI_SFML_CONFIG_DIR "${CMAKE_CURRENT_LIST_DIR}" CACHE PATH "Path to a directory containing user ImGui config") |
| 22 | +set(IMGUI_SFML_CONFIG_NAME "imconfig-SFML.h" CACHE STRING "Name of a custom user ImGui config header") |
| 23 | +set(IMGUI_SFML_CONFIG_INSTALL_DIR "" CACHE PATH "Path where user's config header will be installed") |
| 24 | + |
| 25 | +# For FindImGui.cmake |
| 26 | +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") |
| 27 | + |
| 28 | +if (IMGUI_SFML_FIND_SFML) |
| 29 | + find_package(SFML 2.5 COMPONENTS graphics system window) |
| 30 | + |
| 31 | + if(NOT SFML_FOUND) |
| 32 | + message(FATAL_ERROR "SFML 2 directory not found. Set SFML_DIR to directory where SFML was built (or one which ccontains SFMLConfig.cmake)") |
| 33 | + endif() |
31 | 34 | endif()
|
32 | 35 |
|
33 | 36 | # ImGui does not provide native support for CMakeLists, workaround for now to have
|
34 |
| -# users specify IMGUI_ROOT. See: |
35 |
| -# https://github.com/ocornut/imgui/pull/255 |
36 |
| -if(NOT IMGUI_ROOT) |
37 |
| - set(IMGUI_ROOT "" CACHE PATH "imgui top-level directory") |
38 |
| - message("---> ImGui directory not found. Set IMGUI_ROOT to imgui's top-level path (containing \"imgui.cpp\" and \"imgui.h\" files).\n") |
| 37 | +# users specify IMGUI_DIR. Waiting for this PR to get merged... |
| 38 | +# https://github.com/ocornut/imgui/pull/1713 |
| 39 | +if(NOT IMGUI_DIR) |
| 40 | + set(IMGUI_DIR "" CACHE PATH "imgui top-level directory") |
| 41 | + message(FATAL_ERROR "ImGui directory not found. Set IMGUI_ROOT to imgui's top-level path (containing 'imgui.h' and other files).\n") |
39 | 42 | endif()
|
40 | 43 |
|
41 |
| -# Do a pseudo find files for ImGui once IMGUI_ROOT is set |
42 |
| -if(IMGUI_ROOT) |
43 |
| - set(IMGUI_SERACH_PATH |
44 |
| - ${IMGUI_ROOT} |
45 |
| - $ENV{IMGUI_ROOT} |
46 |
| - ) |
47 |
| - find_path(IMGUI_INCLUDE_DIR imgui.cpp |
48 |
| - PATHS ${IMGUI_SERACH_PATH}) |
49 |
| - if(NOT IMGUI_INCLUDE_DIR) |
50 |
| - message(FATAL_ERROR "---> IMGUI imgui.cpp not found. Set IMGUI_ROOT to imgui's top-level path (containing \"imgui.cpp\" and \"imgui.h\" files).\n") |
51 |
| - else() |
52 |
| - file(GLOB IMGUI_FILES |
53 |
| - "${IMGUI_INCLUDE_DIR}/imgui_draw.cpp" |
54 |
| - "${IMGUI_INCLUDE_DIR}/imgui_demo.cpp" |
55 |
| - "${IMGUI_INCLUDE_DIR}/imgui.cpp" ) |
56 |
| - message("Found imgui.cpp in ${IMGUI_INCLUDE_DIR}") |
57 |
| - # Rename that pesky imconfig.h file for the user. |
58 |
| - install(FILES ${IMGUI_INCLUDE_DIR}/imconfig.h DESTINATION include RENAME imconfig-sample.h) |
59 |
| - endif() |
| 44 | +# This uses FindImGui.cmake provided in ImGui-SFML repo for now |
| 45 | +find_package(ImGui 1.68 REQUIRED) |
| 46 | + |
| 47 | +# these headers will be installed alongside ImGui-SFML |
| 48 | +set(IMGUI_PUBLIC_HEADERS |
| 49 | + ${IMGUI_INCLUDE_DIR}/imconfig.h |
| 50 | + ${IMGUI_INCLUDE_DIR}/imgui.h |
| 51 | + ${IMGUI_INCLUDE_DIR}/imgui_internal.h # not actually public, but users might need it |
| 52 | + ${IMGUI_INCLUDE_DIR}/imstb_rectpack.h |
| 53 | + ${IMGUI_INCLUDE_DIR}/imstb_textedit.h |
| 54 | + ${IMGUI_INCLUDE_DIR}/imstb_truetype.h |
| 55 | + ${IMGUI_INCLUDE_DIR}/misc/cpp/imgui_stdlib.h |
| 56 | +) |
| 57 | + |
| 58 | +# CMake 3.11 and later prefer to choose GLVND, but we choose legacy OpenGL just because it's safer |
| 59 | +# (unless the OpenGL_GL_PREFERENCE was explicitly set) |
| 60 | +# See CMP0072 for more details (cmake --help-policy CMP0072) |
| 61 | +if ((NOT ${CMAKE_VERSION} VERSION_LESS 3.11) AND (NOT OpenGL_GL_PREFERENCE)) |
| 62 | + set(OpenGL_GL_PREFERENCE "LEGACY") |
60 | 63 | endif()
|
61 | 64 |
|
62 |
| -# Glob up both source and headers as sources for VS users. |
63 |
| -file(GLOB IMGUI_SFML_FILES "${PROJECT_SOURCE_DIR}/*.cpp" "${PROJECT_SOURCE_DIR}/*.h") |
| 65 | +find_package(OpenGL REQUIRED) |
64 | 66 |
|
65 |
| -# Set these for users to use |
66 |
| -set(IMGUI_SFML_INCLUDE_DIRS |
67 |
| - ${PROJECT_SOURCE_DIR} |
68 |
| - ${IMGUI_INCLUDE_DIR} |
69 |
| -${SFML_INCLUDE_DIR} CACHE INTERNAL "") |
| 67 | +add_library(ImGui-SFML |
| 68 | + imgui-SFML.cpp |
| 69 | + ${IMGUI_SOURCES} |
| 70 | +) |
70 | 71 |
|
71 |
| -set(IMGUI_SOURCES |
72 |
| -${IMGUI_FILES} CACHE INTERNAL "") |
| 72 | +# Add pretty alias |
| 73 | +add_library(ImGui-SFML::ImGui-SFML ALIAS ImGui-SFML) |
73 | 74 |
|
74 |
| -set(IMGUI_SFML_SOURCES |
75 |
| -${IMGUI_SFML_FILES} CACHE INTERNAL "") |
| 75 | +target_link_libraries(ImGui-SFML |
| 76 | + PUBLIC |
| 77 | + sfml-graphics |
| 78 | + sfml-system |
| 79 | + sfml-window |
| 80 | + ${OPENGL_LIBRARIES} |
| 81 | +) |
76 | 82 |
|
77 |
| -set(IMGUI_SFML_DEPENDENCIES |
78 |
| - ${SFML_DEPENDENCIES} |
79 |
| -${SFML_LIBRARIES} CACHE INTERNAL "") |
| 83 | +include(GNUInstallDirs) |
| 84 | + |
| 85 | +target_include_directories(ImGui-SFML |
| 86 | + PUBLIC |
| 87 | + $<BUILD_INTERFACE:${IMGUI_INCLUDE_DIR}> |
| 88 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}> |
| 89 | + $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> |
| 90 | +) |
| 91 | + |
| 92 | +if(NOT IMGUI_SFML_USE_DEFAULT_CONFIG) |
| 93 | + if (IMGUI_SFML_CONFIG_DIR) |
| 94 | + target_include_directories(ImGui-SFML |
| 95 | + PUBLIC |
| 96 | + $<BUILD_INTERFACE:${IMGUI_SFML_CONFIG_DIR}> |
| 97 | + $<INSTALL_INTERFACE:${IMGUI_SFML_INSTALL_CONFIG_DIR}> |
| 98 | + ) |
| 99 | + else() |
| 100 | + message(FATAL_ERROR "IMGUI_SFML_CONFIG_DIR should be set if IMGUI_SFML_USE_DEFAULT_CONFIG is OFF") |
| 101 | + endif() |
| 102 | +endif() |
| 103 | + |
| 104 | +target_compile_definitions(ImGui-SFML |
| 105 | + PUBLIC |
| 106 | + IMGUI_USER_CONFIG="${IMGUI_SFML_CONFIG_NAME}" |
| 107 | +) |
| 108 | + |
| 109 | +if(BUILD_SHARED_LIBS) |
| 110 | + target_compile_definitions(ImGui-SFML PRIVATE IMGUI_SFML_SHARED_LIB) |
| 111 | + set_target_properties(ImGui-SFML PROPERTIES |
| 112 | + DEFINE_SYMBOL "IMGUI_SFML_EXPORTS" |
| 113 | + ) |
| 114 | +endif() |
| 115 | + |
| 116 | +set(IMGUI_SFML_PUBLIC_HEADERS |
| 117 | + "${CMAKE_CURRENT_LIST_DIR}/imgui-SFML.h" |
| 118 | + "${CMAKE_CURRENT_LIST_DIR}/imgui-SFML_export.h" |
| 119 | +) |
| 120 | + |
| 121 | +if (IMGUI_SFML_USE_DEFAULT_CONFIG OR |
| 122 | + (NOT DEFINED "${IMGUI_SFML_CONFIG_INSTALL_DIR}")) |
| 123 | + list(APPEND IMGUI_SFML_PUBLIC_HEADERS |
| 124 | + "${IMGUI_SFML_CONFIG_DIR}/${IMGUI_SFML_CONFIG_NAME}" |
| 125 | + ) |
| 126 | +# If user set IMGUI_SFML_INSTALL_CONFIG_DIR, it means that they'll install file themselves |
| 127 | +endif() |
| 128 | + |
| 129 | +list(APPEND IMGUI_SFML_PUBLIC_HEADERS "${IMGUI_PUBLIC_HEADERS}") |
| 130 | + |
| 131 | +set_target_properties(ImGui-SFML PROPERTIES |
| 132 | + PUBLIC_HEADER "${IMGUI_SFML_PUBLIC_HEADERS}" |
| 133 | +) |
80 | 134 |
|
81 | 135 | if(IMGUI_SFML_BUILD_EXAMPLES)
|
82 |
| - # Build examples |
83 | 136 | add_subdirectory(examples)
|
84 |
| -endif(IMGUI_SFML_BUILD_EXAMPLES) |
| 137 | +endif() |
| 138 | + |
| 139 | +# installation rules |
| 140 | +install(TARGETS ImGui-SFML |
| 141 | + EXPORT ImGui-SFML |
| 142 | + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} |
| 143 | + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 144 | + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 145 | + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} |
| 146 | +) |
| 147 | + |
| 148 | +install(EXPORT ImGui-SFML |
| 149 | + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ImGui-SFML |
| 150 | + NAMESPACE ImGui-SFML:: |
| 151 | + FILE ImGui-SFMLConfig.cmake |
| 152 | +) |
0 commit comments