summaryrefslogtreecommitdiff
path: root/Tests/FindOpenGL/Test/CMakeLists.txt
blob: 3b5ffeecd4cce15336c563312d8118dde27785da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
cmake_minimum_required(VERSION 3.9)
project(TestFindOpenGL C)
include(CTest)

find_package(OpenGL REQUIRED)

# import target for GLU
add_executable(test_tgt main.c)
target_link_libraries(test_tgt OpenGL::GLU)
add_test(NAME test_tgt COMMAND test_tgt)

# OPENGL_LIBRARIES should be whatever libraries are needed to link.
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)

# VND support adds an ::OpenGL import target.  This can be used for OpenGL-only
# code (code that does not manipulate contexts, like our 'main.c').  Without
# VND, ::GL can be used for both context and non-context OpenGL code.
if(OpenGL_TEST_VND)
  add_executable(test_comp_none main.c)
  target_link_libraries(test_comp_none PRIVATE OpenGL::OpenGL)
  add_test(NAME test_comp_none COMMAND test_comp_none)
else()
  add_executable(test_comp_none main.c)
  target_link_libraries(test_comp_none PRIVATE OpenGL::GL)
  add_test(NAME test_comp_none COMMAND test_comp_none)
endif()

# GLX
if(OpenGL_TEST_VND)
  find_package(OpenGL REQUIRED COMPONENTS OpenGL GLX)
  add_executable(test_comp_glx main.c)
  target_link_libraries(test_comp_glx PRIVATE OpenGL::OpenGL OpenGL::GLX)
  add_test(NAME test_comp_glx COMMAND test_comp_glx)
else()
  # non-VND systems won't have it, but an optional search for GLX should still
  # be okay.
  find_package(OpenGL COMPONENTS GLX)
  add_executable(test_comp_glx_novnd main.c)
  target_link_libraries(test_comp_glx_novnd PRIVATE OpenGL::GL)
  add_test(NAME test_comp_glx_novnd COMMAND test_comp_glx_novnd)
endif()

# EGL is only available on Linux+GLVND at present.
if(OpenGL_TEST_VND)
  find_package(OpenGL COMPONENTS OpenGL EGL)
  if(OpenGL_EGL_FOUND)
    add_executable(test_comp_egl main.c)
    target_link_libraries(test_comp_egl PRIVATE OpenGL::OpenGL OpenGL::EGL)
    add_test(NAME test_comp_egl COMMAND test_comp_egl)
    # EGL-only code should not link to GLX.
    execute_process(COMMAND ldd test_comp_egl
                    OUTPUT_VARIABLE LDD_OUT
                    ERROR_VARIABLE LDD_ERR)
    if("${LDD_OUT}" MATCHES "GLX")
      message(FATAL_ERROR "EGL-only code links to GLX!")
    endif()
  endif()

  # all three COMPONENTS together.
  find_package(OpenGL COMPONENTS OpenGL EGL GLX)
  if(OpenGL_EGL_FOUND AND OpenGL_GLX_FOUND)
    add_executable(test_comp_both main.c)
    target_link_libraries(test_comp_both PRIVATE OpenGL::OpenGL OpenGL::EGL
                          OpenGL::GLX)
    add_test(NAME test_comp_both COMMAND test_comp_both)
  endif()
endif()