|
| 1 | +if (IMGUI_BACKEND_OPENGL2 OR IMGUI_BACKEND_OPENGL3) |
| 2 | + if(POLICY CMP0072) |
| 3 | + cmake_policy(SET CMP0072 NEW) |
| 4 | + endif() |
| 5 | + find_package(OpenGL REQUIRED) |
| 6 | +endif() |
| 7 | + |
| 8 | +# OpenGL 2 |
| 9 | +if (IMGUI_BACKEND_OPENGL2) |
| 10 | + add_library(imgui-opengl2 INTERFACE) |
| 11 | + target_sources(imgui-opengl2 INTERFACE |
| 12 | + "${CMAKE_CURRENT_LIST_DIR}/imgui_impl_opengl2.h" |
| 13 | + "${CMAKE_CURRENT_LIST_DIR}/imgui_impl_opengl2.cpp" |
| 14 | + ) |
| 15 | + target_link_libraries(imgui-opengl2 INTERFACE |
| 16 | + imgui |
| 17 | + OpenGL::GL |
| 18 | + ) |
| 19 | +endif() |
| 20 | + |
| 21 | +# OpenGL 3 |
| 22 | +if (IMGUI_BACKEND_OPENGL3) |
| 23 | + add_library(imgui-opengl3 INTERFACE) |
| 24 | + target_sources(imgui-opengl3 INTERFACE |
| 25 | + "${CMAKE_CURRENT_LIST_DIR}/imgui_impl_opengl3.h" |
| 26 | + "${CMAKE_CURRENT_LIST_DIR}/imgui_impl_opengl3.cpp" |
| 27 | + ) |
| 28 | + target_link_libraries(imgui-opengl3 INTERFACE |
| 29 | + imgui |
| 30 | + OpenGL::GL |
| 31 | + imgui-gl3w # TODO: allow to choose OpenGL3 loader |
| 32 | + ) |
| 33 | +endif() |
| 34 | + |
| 35 | +# SDL |
| 36 | +if(IMGUI_BACKEND_SDL) |
| 37 | + # find SDL2 |
| 38 | + if(CMAKE_SYSTEM_NAME STREQUAL Emscripten) |
| 39 | + # Nothing to do for emscripten builds. SDL is bunled with the compiler. |
| 40 | + elseif(TARGET SDL2::SDL2) |
| 41 | + # user has already added SDL2::SDL2 target by any means |
| 42 | + elseif(MSVC AND IMGUI_SDL2_PREBUILT_DIR) |
| 43 | + # On Windows you may set IMGUI_SDL2_PREBUILT_DIR variable and link to binary SDL distribution. |
| 44 | + if(CMAKE_SIZEOF_VOID_P EQUAL 8) |
| 45 | + set(IMGUI_SDL2_PLATFORM_ARCH x64) |
| 46 | + elseif(CMAKE_SIZEOF_VOID_P EQUAL 4) |
| 47 | + set(IMGUI_SDL2_PLATFORM_ARCH x86) |
| 48 | + else () |
| 49 | + message(FATAL_ERROR "Unsupported platform.") |
| 50 | + endif() |
| 51 | + |
| 52 | + add_library(SDL2 SHARED IMPORTED GLOBAL) |
| 53 | + target_include_directories(SDL2 INTERFACE "${IMGUI_SDL2_PREBUILT_DIR}/include") |
| 54 | + set_target_properties(SDL2 PROPERTIES |
| 55 | + IMPORTED_LOCATION "${IMGUI_SDL2_PREBUILT_DIR}/lib/${IMGUI_SDL2_PLATFORM_ARCH}/SDL2.dll" |
| 56 | + IMPORTED_IMPLIB "${IMGUI_SDL2_PREBUILT_DIR}/lib/${IMGUI_SDL2_PLATFORM_ARCH}/SDL2.lib" |
| 57 | + ) |
| 58 | + add_library(SDL2::SDL2 ALIAS SDL2) |
| 59 | + |
| 60 | + add_library(SDL2main STATIC IMPORTED GLOBAL) |
| 61 | + set_target_properties(SDL2main PROPERTIES |
| 62 | + IMPORTED_LOCATION "${IMGUI_SDL2_PREBUILT_DIR}/lib/${IMGUI_SDL2_PLATFORM_ARCH}/SDL2main.lib" |
| 63 | + ) |
| 64 | + elseif(SDL2_FOUND) |
| 65 | + # Platforms that do not export target but use old CMake conventions can be handled this way. |
| 66 | + add_library(SDL2::SDL2 INTERFACE IMPORTED) |
| 67 | + target_link_libraries(SDL2::SDL2 INTERFACE ${SDL2_LIBRARIES}) |
| 68 | + target_include_directories(SDL2::SDL2 INTERFACE ${SDL2_INCLUDE_DIRS}) |
| 69 | + elseif(PkgConfig_FOUND) |
| 70 | + # Rest of the platforms (like MacOS) can consume SDL via pkg-config. |
| 71 | + # CMake 3.6 supports IMPORTED_TARGET parameter which creates PkgConfig::sdl2 target that we can easily link to. |
| 72 | + # TODO: Switch to using IMPORTED_TARGET when CMake minimal version is increased. |
| 73 | + pkg_check_modules(SDL2 QUIET sdl2) |
| 74 | + if(SDL2_FOUND) |
| 75 | + add_library(SDL2::SDL2 INTERFACE IMPORTED) |
| 76 | + target_link_libraries(SDL2::SDL2 INTERFACE ${SDL2_LDFLAGS}) |
| 77 | + target_compile_options(SDL2::SDL2 INTERFACE ${SDL2_CFLAGS}) |
| 78 | + endif() |
| 79 | + else() |
| 80 | + find_package(SDL2 REQUIRED) |
| 81 | + endif() |
| 82 | + |
| 83 | + add_library(imgui-sdl INTERFACE) |
| 84 | + target_sources(imgui-sdl INTERFACE |
| 85 | + "${CMAKE_CURRENT_LIST_DIR}/imgui_impl_sdl2.h" |
| 86 | + "${CMAKE_CURRENT_LIST_DIR}/imgui_impl_sdl2.cpp" |
| 87 | + ) |
| 88 | + target_include_directories(imgui-sdl INTERFACE "${CMAKE_CURRENT_LIST_DIR}") |
| 89 | + target_link_libraries(imgui-sdl INTERFACE imgui SDL2::SDL2) |
| 90 | +endif() |
| 91 | + |
| 92 | +# GLFW |
| 93 | +if(IMGUI_BACKEND_GLFW) |
| 94 | + # find glfw |
| 95 | + if(TARGET glfw) |
| 96 | + # GLFW exists already. Nothing to do. |
| 97 | + elseif(MSVC) |
| 98 | + # On Windows you may set IMGUI_GLFW_PREBUILT_DIR variable and link to binary GLFW distribution. |
| 99 | + if(IMGUI_GLFW_PREBUILT_DIR) |
| 100 | + set(IMGUI_GLFW_INCLUDE_DIR "${IMGUI_GLFW_PREBUILT_DIR}/include") |
| 101 | + set(IMGUI_GLFW_LIB_DIR "${IMGUI_GLFW_PREBUILT_DIR}/lib-vc2019") # TODO: don't hardcode VC version |
| 102 | + |
| 103 | + add_library(glfw SHARED IMPORTED GLOBAL) |
| 104 | + target_include_directories(glfw INTERFACE "${IMGUI_GLFW_INCLUDE_DIR}") |
| 105 | + set_target_properties(glfw PROPERTIES |
| 106 | + IMPORTED_IMPLIB "${IMGUI_GLFW_LIB_DIR}/glfw3.lib" |
| 107 | + IMPORTED_LOCATION "${IMGUI_GLFW_LIB_DIR}/glfw3.dll" |
| 108 | + ) |
| 109 | + else() # Use prebuilt GLFW from Dear ImGui's repo (only static build is included) |
| 110 | + if(CMAKE_SIZEOF_VOID_P EQUAL 8) |
| 111 | + set(IMGUI_GLFW_PLATFORM_BITS 64) |
| 112 | + elseif(CMAKE_SIZEOF_VOID_P EQUAL 4) |
| 113 | + set(IMGUI_GLFW_PLATFORM_BITS 32) |
| 114 | + else() |
| 115 | + message(FATAL_ERROR "Unsupported platform.") |
| 116 | + endif() |
| 117 | + set(IMGUI_GLFW_DIR "${PROJECT_SOURCE_DIR}/examples/libs/glfw") |
| 118 | + set(IMGUI_GLFW_INCLUDE_DIR "${IMGUI_GLFW_DIR}/include") |
| 119 | + set(IMGUI_GLFW_LIB_DIR "${IMGUI_GLFW_DIR}/lib-vc2010-${IMGUI_GLFW_PLATFORM_BITS}") |
| 120 | + |
| 121 | + add_library(glfw STATIC IMPORTED GLOBAL) |
| 122 | + target_include_directories(glfw INTERFACE "${IMGUI_GLFW_INCLUDE_DIR}") |
| 123 | + set_target_properties(glfw PROPERTIES |
| 124 | + IMPORTED_LOCATION "${IMGUI_GLFW_LIB_DIR}/glfw3.lib" |
| 125 | + ) |
| 126 | + endif() |
| 127 | + elseif(PkgConfig_FOUND) |
| 128 | + # CMake 3.6 supports IMPORTED_TARGET parameter which creates PkgConfig::glfw target that we can easily link to. |
| 129 | + # TODO: Switch to using IMPORTED_TARGET when CMake minimal version is increased. |
| 130 | + pkg_check_modules(GLFW QUIET glfw3) |
| 131 | + if(GLFW_FOUND) |
| 132 | + add_library(glfw INTERFACE IMPORTED) |
| 133 | + target_link_libraries(glfw INTERFACE ${GLFW_LDFLAGS}) |
| 134 | + target_compile_options(glfw INTERFACE ${GLFW_CFLAGS}) |
| 135 | + endif() |
| 136 | + else() |
| 137 | + find_package(glfw3 REQUIRED) |
| 138 | + endif() |
| 139 | + |
| 140 | + add_library(imgui-glfw INTERFACE) |
| 141 | + target_sources(imgui-glfw INTERFACE |
| 142 | + "${CMAKE_CURRENT_LIST_DIR}/imgui_impl_glfw.h" |
| 143 | + "${CMAKE_CURRENT_LIST_DIR}/imgui_impl_glfw.cpp" |
| 144 | + ) |
| 145 | + target_link_libraries(imgui-glfw INTERFACE ${CMAKE_DL_LIBS} glfw) |
| 146 | + target_include_directories(imgui-glfw INTERFACE "${CMAKE_CURRENT_LIST_DIR}") |
| 147 | +endif() |
0 commit comments