Skip to content

Commit 0dbcba9

Browse files
committed
Improve OpenGL detection checks in CMake
The OpenGL headers are not always implicitly available, so this improves the check by calling `find_package` and using the `OPENGL_INCLUDE_DIRS` or `OPENGL_INCLUDE_DIR` var for the `check_c_source_compiles` test. The minimum CMake version currently set is 3.16, `OPENGL_INCLUDE_DIRS` was only added in 3.29, so the code is set to choose `OPENGL_INCLUDE_DIRS` if it exists. If the minimum CMake version is ever set to >= 3.29 this check can be removed and just the `OPENGL_INCLUDE_DIRS` variable can be chosen.
1 parent f3d3981 commit 0dbcba9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

cmake/sdlchecks.cmake

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,13 +800,29 @@ macro(CheckOpenVR)
800800
endif()
801801
endmacro()
802802

803+
# Requires
804+
# - N/A
805+
macro(FindOpenGLHeaders)
806+
find_package(OpenGL MODULE)
807+
# OPENGL_INCLUDE_DIRS is preferred over OPENGL_INCLUDE_DIR, but was only added in 3.29,
808+
# If the CMake minimum version is changed to be >= 3.29, the second check should be removed.
809+
if(DEFINED OPENGL_INCLUDE_DIRS)
810+
list(APPEND CMAKE_REQUIRED_INCLUDES ${OPENGL_INCLUDE_DIRS})
811+
elseif(DEFINED OPENGL_INCLUDE_DIR)
812+
list(APPEND CMAKE_REQUIRED_INCLUDES ${OPENGL_INCLUDE_DIR})
813+
endif()
814+
endmacro()
815+
803816
# Requires:
804817
# - nada
805818
macro(CheckGLX)
806819
if(SDL_OPENGL)
820+
cmake_push_check_state()
821+
FindOpenGLHeaders()
807822
check_c_source_compiles("
808823
#include <GL/glx.h>
809824
int main(int argc, char** argv) { return 0; }" HAVE_OPENGL_GLX)
825+
cmake_pop_check_state()
810826
if(HAVE_OPENGL_GLX AND NOT HAVE_ROCKCHIP)
811827
set(SDL_VIDEO_OPENGL_GLX 1)
812828
endif()
@@ -840,10 +856,13 @@ endmacro()
840856
# - nada
841857
macro(CheckOpenGL)
842858
if(SDL_OPENGL)
859+
cmake_push_check_state()
860+
FindOpenGLHeaders()
843861
check_c_source_compiles("
844862
#include <GL/gl.h>
845863
#include <GL/glext.h>
846864
int main(int argc, char** argv) { return 0; }" HAVE_OPENGL)
865+
cmake_pop_check_state()
847866
if(HAVE_OPENGL)
848867
set(SDL_VIDEO_OPENGL 1)
849868
set(SDL_VIDEO_RENDER_OGL 1)
@@ -856,6 +875,7 @@ endmacro()
856875
macro(CheckOpenGLES)
857876
if(SDL_OPENGLES)
858877
cmake_push_check_state()
878+
FindOpenGLHeaders()
859879
list(APPEND CMAKE_REQUIRED_INCLUDES "${SDL3_SOURCE_DIR}/src/video/khronos")
860880
check_c_source_compiles("
861881
#include <GLES/gl.h>

0 commit comments

Comments
 (0)