Skip to content

Commit

Permalink
Add OpenGL::GL cmake imported targets (#23705)
Browse files Browse the repository at this point in the history
This allows projects to include OpenGL like so
```cmake
find_package(OpenGL REQUIRED)
target_link_libraries(myproject OpenGL::GL)
```
Link to CMake's documentation:
https://cmake.org/cmake/help/latest/module/FindOpenGL.html#imported-targets

CMake's `FindOpenGL.cmake` also produces GLES-related imported targets.
While I think they would be nice to add, I've omitted them as I don't
know if it would be as simple as duplicating the `OpenGL::GL` target or
more needs to be to communicate that choice of flavor to emscripten.
  • Loading branch information
cajomar authored Feb 27, 2025
1 parent 4147246 commit 2ee1188
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
12 changes: 12 additions & 0 deletions cmake/Modules/FindOpenGL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@ mark_as_advanced(
OPENGL_glu_LIBRARY
OPENGL_gl_LIBRARY
)

add_library(OpenGL::GL INTERFACE IMPORTED)
set_target_properties(OpenGL::GL PROPERTIES
IMPORTED_LIBNAME "${OPENGL_gl_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${OPENGL_INCLUDE_DIR}"
)

add_library(OpenGL::GLU INTERFACE IMPORTED)
set_target_properties(OpenGL::GLU PROPERTIES
IMPORTED_LIBNAME "${OPENGL_glu_LIBRARY}"
INTERFACE_LINK_LIBRARIES OpenGL::GL
)
14 changes: 10 additions & 4 deletions test/cmake/find_modules/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ add_executable(test_prog test.c)
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR})
target_link_libraries(test_prog ${OPENGL_LIBRARIES})
message(" test: OPENGL_LIBRARIES: ${OPENGL_LIBRARIES}")
message(STATUS " test: OPENGL_LIBRARIES: ${OPENGL_LIBRARIES}")

find_package(OpenAL REQUIRED)
include_directories(${OPENAL_INCLUDE_DIR})
target_link_libraries(test_prog ${OPENAL_LIBRARY})
message(" test: OPENGL_LIBRARIES: ${OPENAL_LIBRARIES}")
message(STATUS " test: OPENAL_LIBRARIES: ${OPENAL_LIBRARIES}")

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
target_link_libraries(test_prog SDL2::SDL2)
message(" test: SDL2_LIBRARIES: ${SDL2_LIBRARIES}")
message(" test: SDL2_INCLUDE_DIRS: ${SDL2_INCLUDE_DIRS}")
message(STATUS " test: SDL2_LIBRARIES: ${SDL2_LIBRARIES}")
message(STATUS " test: SDL2_INCLUDE_DIRS: ${SDL2_INCLUDE_DIRS}")

get_target_property(TEST_OPENGL_GL_LIBNAME OpenGL::GL IMPORTED_LIBNAME)
message(STATUS " test: OpenGL::GL IMPORTED_LIBNAME: ${TEST_OPENGL_GL_LIBNAME}")

get_target_property(TEST_OPENGL_GL_INCLUDES OpenGL::GL INTERFACE_INCLUDE_DIRECTORIES)
message(STATUS " test: OpenGL::GL INTERFACE_INCLUDE_DIRECTORIES: ${TEST_OPENGL_GL_INCLUDES}")
2 changes: 1 addition & 1 deletion test/cmake/find_modules/test.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Invlude emscripten/version.h to ensure that the in-tree
// Include emscripten/version.h to ensure that the in-tree
// include directory has not been added to the include path.
#include <emscripten/version.h>
#include <stdio.h>
Expand Down
4 changes: 3 additions & 1 deletion test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,9 @@ def test_cmake_install(self):

@requires_network
def test_cmake_find_modules(self):
self.run_process([EMCMAKE, 'cmake', test_file('cmake/find_modules')])
output = self.run_process([EMCMAKE, 'cmake', test_file('cmake/find_modules')], stdout=PIPE).stdout
self.assertContained(' test: OpenGL::GL IMPORTED_LIBNAME: GL', output)
self.assertContained(' test: OpenGL::GL INTERFACE_INCLUDE_DIRECTORIES: /.+/cache/sysroot/include', output, regex=True)
self.run_process(['cmake', '--build', '.'])
output = self.run_js('test_prog.js')
self.assertContained('AL_VERSION: 1.1', output)
Expand Down

0 comments on commit 2ee1188

Please sign in to comment.