From 027ce359ff7454ac0aca4d9a9208759ef4b9c8b7 Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 2 Dec 2016 14:34:41 -0500 Subject: FindOpenGL: Provide imported targets for GL and GLU MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Create OpenGL::GL and OpenGL::GLU imported targets using the locations found. This feature was originally added by commit v3.1.0-rc1~420^2~2 (FindOpenGL: Provide imported targets for GL and GLU, 2014-05-31) but had to be reverted by commit v3.1.0-rc3~10^2 (FindOpenGL: Revert support for imported targets, 2014-12-01) due to issue #15267. Since then we added support for `IMPORTED_LIBNAME` to interface libraries, so use it to handle the case where we have only the library name without an absolute path. Inspired-by: Philipp Möller Closes: #15267 --- Help/release/dev/FindOpenGL-imported-targets.rst | 5 ++ Modules/FindOpenGL.cmake | 59 ++++++++++++++++++++++++ Tests/CMakeLists.txt | 4 ++ Tests/FindOpenGL/CMakeLists.txt | 10 ++++ Tests/FindOpenGL/Test/CMakeLists.txt | 14 ++++++ Tests/FindOpenGL/Test/main.c | 17 +++++++ 6 files changed, 109 insertions(+) create mode 100644 Help/release/dev/FindOpenGL-imported-targets.rst create mode 100644 Tests/FindOpenGL/CMakeLists.txt create mode 100644 Tests/FindOpenGL/Test/CMakeLists.txt create mode 100644 Tests/FindOpenGL/Test/main.c diff --git a/Help/release/dev/FindOpenGL-imported-targets.rst b/Help/release/dev/FindOpenGL-imported-targets.rst new file mode 100644 index 0000000..79e3cb0 --- /dev/null +++ b/Help/release/dev/FindOpenGL-imported-targets.rst @@ -0,0 +1,5 @@ +FindOpenGL-imported-targets +--------------------------- + +* The :module:`FindOpenGL` module now provides imported targets + ``OpenGL::GL`` and ``OpenGL::GLU`` when the libraries are found. diff --git a/Modules/FindOpenGL.cmake b/Modules/FindOpenGL.cmake index 295de65..503d955 100644 --- a/Modules/FindOpenGL.cmake +++ b/Modules/FindOpenGL.cmake @@ -7,6 +7,16 @@ # # FindModule for OpenGL and GLU. # +# IMPORTED Targets +# ^^^^^^^^^^^^^^^^ +# +# This module defines the :prop_tgt:`IMPORTED` targets: +# +# ``OpenGL::GL`` +# Defined if the system has OpenGL. +# ``OpenGL::GLU`` +# Defined if the system has GLU. +# # Result Variables # ^^^^^^^^^^^^^^^^ # @@ -160,6 +170,55 @@ include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) FIND_PACKAGE_HANDLE_STANDARD_ARGS(OpenGL REQUIRED_VARS ${_OpenGL_REQUIRED_VARS}) unset(_OpenGL_REQUIRED_VARS) +# OpenGL:: targets +if(OPENGL_FOUND) + if(NOT TARGET OpenGL::GL) + if(IS_ABSOLUTE "${OPENGL_gl_LIBRARY}") + add_library(OpenGL::GL UNKNOWN IMPORTED) + if(OPENGL_gl_LIBRARY MATCHES "/([^/]+)\\.framework$") + set(_gl_fw "${OPENGL_gl_LIBRARY}/${CMAKE_MATCH_1}") + if(EXISTS "${_gl_fw}.tbd") + set(_gl_fw "${_gl_fw}.tbd") + endif() + set_target_properties(OpenGL::GL PROPERTIES + IMPORTED_LOCATION "${_gl_fw}") + else() + set_target_properties(OpenGL::GL PROPERTIES + IMPORTED_LOCATION "${OPENGL_gl_LIBRARY}") + endif() + else() + add_library(OpenGL::GL INTERFACE IMPORTED) + set_target_properties(OpenGL::GL PROPERTIES + IMPORTED_LIBNAME "${OPENGL_gl_LIBRARY}") + endif() + set_target_properties(OpenGL::GL PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${OPENGL_INCLUDE_DIR}") + endif() + + if(OPENGL_GLU_FOUND AND NOT TARGET OpenGL::GLU) + if(IS_ABSOLUTE "${OPENGL_glu_LIBRARY}") + add_library(OpenGL::GLU UNKNOWN IMPORTED) + if(OPENGL_glu_LIBRARY MATCHES "/([^/]+)\\.framework$") + set(_glu_fw "${OPENGL_glu_LIBRARY}/${CMAKE_MATCH_1}") + if(EXISTS "${_glu_fw}.tbd") + set(_glu_fw "${_glu_fw}.tbd") + endif() + set_target_properties(OpenGL::GLU PROPERTIES + IMPORTED_LOCATION "${_glu_fw}") + else() + set_target_properties(OpenGL::GLU PROPERTIES + IMPORTED_LOCATION "${OPENGL_glu_LIBRARY}") + endif() + else() + add_library(OpenGL::GLU INTERFACE IMPORTED) + set_target_properties(OpenGL::GLU PROPERTIES + IMPORTED_LIBNAME "${OPENGL_glu_LIBRARY}") + endif() + set_target_properties(OpenGL::GLU PROPERTIES + INTERFACE_LINK_LIBRARIES OpenGL::GL) + endif() +endif() + mark_as_advanced( OPENGL_INCLUDE_DIR OPENGL_xmesa_INCLUDE_DIR diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index fa5f3e8..73eef9a 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -1400,6 +1400,10 @@ ${CMake_BINARY_DIR}/bin/cmake -DDIR=dev -P ${CMake_SOURCE_DIR}/Utilities/Release add_subdirectory(FindOpenCL) endif() + if(CMake_TEST_FindOpenGL) + add_subdirectory(FindOpenGL) + endif() + if(CMake_TEST_FindOpenSSL) add_subdirectory(FindOpenSSL) endif() diff --git a/Tests/FindOpenGL/CMakeLists.txt b/Tests/FindOpenGL/CMakeLists.txt new file mode 100644 index 0000000..9aa3aba --- /dev/null +++ b/Tests/FindOpenGL/CMakeLists.txt @@ -0,0 +1,10 @@ +add_test(NAME FindOpenGL.Test COMMAND + ${CMAKE_CTEST_COMMAND} -C $ + --build-and-test + "${CMake_SOURCE_DIR}/Tests/FindOpenGL/Test" + "${CMake_BINARY_DIR}/Tests/FindOpenGL/Test" + ${build_generator_args} + --build-project TestFindOpenGL + --build-options ${build_options} + --test-command ${CMAKE_CTEST_COMMAND} -V -C $ + ) diff --git a/Tests/FindOpenGL/Test/CMakeLists.txt b/Tests/FindOpenGL/Test/CMakeLists.txt new file mode 100644 index 0000000..cac3424 --- /dev/null +++ b/Tests/FindOpenGL/Test/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.7) +project(TestFindOpenGL C) +include(CTest) + +find_package(OpenGL REQUIRED) + +add_executable(test_tgt main.c) +target_link_libraries(test_tgt OpenGL::GLU) +add_test(NAME test_tgt COMMAND test_tgt) + +add_executable(test_var main.c) +target_include_directories(test_var PRIVATE ${OPENGL_INGLUDE_DIRS}) +target_link_libraries(test_var PRIVATE ${OPENGL_LIBRARIES}) +add_test(NAME test_var COMMAND test_var) diff --git a/Tests/FindOpenGL/Test/main.c b/Tests/FindOpenGL/Test/main.c new file mode 100644 index 0000000..bca3d7e --- /dev/null +++ b/Tests/FindOpenGL/Test/main.c @@ -0,0 +1,17 @@ +#ifdef _WIN32 +#include +#endif +#ifdef __APPLE__ +#include +#else +#include +#endif + +#include + +int main() +{ + /* Reference a GL symbol without requiring a context at runtime. */ + printf("&glGetString = %p\n", &glGetString); + return 0; +} -- cgit v0.12