sdl12-compat: cmake: don't build OpenGL test programs if OpenGL isn't available.

From ffd09b017f3a0d08133c164975bee8997ab890d7 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Thu, 10 Jun 2021 14:15:47 -0400
Subject: [PATCH] cmake: don't build OpenGL test programs if OpenGL isn't
 available.

Fixes #90.
---
 CMakeLists.txt | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index b896e82..c897b7d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -94,15 +94,20 @@ if(SDL12TESTS)
       find_library(MATH_LIBRARY m)
     endif()
 
+    find_package(OpenGL COMPONENTS OpenGL)
+    if(OpenGL_FOUND)
+      set(HAVE_OPENGL_DEFINE "HAVE_OPENGL")
+    endif()
+
     macro(test_program _NAME _SRCS)
         add_executable(${_NAME} ${_SRCS})
         target_include_directories(${_NAME} PRIVATE "include/SDL")
         target_link_libraries(${_NAME} SDL)
         # Turn off MSVC's aggressive C runtime warnings for the old test programs.
         if(MSVC)
-          set_target_properties(${_NAME} PROPERTIES COMPILE_DEFINITIONS "HAVE_OPENGL;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE")
+          set_target_properties(${_NAME} PROPERTIES COMPILE_DEFINITIONS "${HAVE_OPENGL_DEFINE};_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE")
         else()
-          set_target_properties(${_NAME} PROPERTIES COMPILE_DEFINITIONS "HAVE_OPENGL")
+          set_target_properties(${_NAME} PROPERTIES COMPILE_DEFINITIONS "${HAVE_OPENGL_DEFINE}")
         endif()
         if(MATH_LIBRARY)
           target_link_libraries(${_NAME} ${MATH_LIBRARY})
@@ -117,11 +122,9 @@ if(SDL12TESTS)
     test_program(testblitspeed "test/testblitspeed.c")
     test_program(testcdrom "test/testcdrom.c")
     test_program(testcursor "test/testcursor.c")
-    test_program(testdyngl "test/testdyngl.c")
     test_program(testerror "test/testerror.c")
     test_program(testfile "test/testfile.c")
     test_program(testgamma "test/testgamma.c")
-    test_program(testgl "test/testgl.c")
     test_program(testthread "test/testhread.c")
     test_program(testiconv "test/testiconv.c")
     test_program(testjoystick "test/testjoystick.c")
@@ -142,11 +145,11 @@ if(SDL12TESTS)
     test_program(threadwin "test/threadwin.c")
     test_program(torturethread "test/torturethread.c")
 
-    if(WIN32)
-      target_link_libraries(testgl "OpenGL32")
-    else()
-      find_package(OpenGL COMPONENTS OpenGL REQUIRED)
+    if(OpenGL_FOUND)
+      test_program(testdyngl "test/testdyngl.c")
 
+      # testgl links directly to OpenGL (yuck), whereas testdyngl doesn't.
+      test_program(testgl "test/testgl.c")
       if(CMAKE_VERSION VERSION_LESS 3.10)
         target_link_libraries(testgl ${OPENGL_gl_LIBRARY})
       else()