CMakeLists.txt 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. cmake_minimum_required(VERSION 3.10)
  2. cmake_policy(SET CMP0072 NEW)
  3. project(TestFindOpenGL C)
  4. include(CTest)
  5. find_package(OpenGL REQUIRED)
  6. # import target for GLU
  7. add_executable(test_tgt main.c)
  8. target_link_libraries(test_tgt OpenGL::GLU)
  9. add_test(NAME test_tgt COMMAND test_tgt)
  10. # OPENGL_LIBRARIES should be whatever libraries are needed to link.
  11. add_executable(test_var main.c)
  12. target_include_directories(test_var PRIVATE ${OPENGL_INGLUDE_DIRS})
  13. target_link_libraries(test_var PRIVATE ${OPENGL_LIBRARIES})
  14. add_test(NAME test_var COMMAND test_var)
  15. # VND support adds an ::OpenGL import target. This can be used for OpenGL-only
  16. # code (code that does not manipulate contexts, like our 'main.c'). Without
  17. # VND, ::GL can be used for both context and non-context OpenGL code.
  18. if(OpenGL_TEST_VND)
  19. add_executable(test_comp_none main.c)
  20. target_link_libraries(test_comp_none PRIVATE OpenGL::OpenGL)
  21. add_test(NAME test_comp_none COMMAND test_comp_none)
  22. else()
  23. add_executable(test_comp_none main.c)
  24. target_link_libraries(test_comp_none PRIVATE OpenGL::GL)
  25. add_test(NAME test_comp_none COMMAND test_comp_none)
  26. endif()
  27. # GLX
  28. if(OpenGL_TEST_VND)
  29. find_package(OpenGL REQUIRED COMPONENTS OpenGL GLX)
  30. add_executable(test_comp_glx main.c)
  31. target_link_libraries(test_comp_glx PRIVATE OpenGL::OpenGL OpenGL::GLX)
  32. add_test(NAME test_comp_glx COMMAND test_comp_glx)
  33. else()
  34. # non-VND systems won't have it, but an optional search for GLX should still
  35. # be okay.
  36. find_package(OpenGL COMPONENTS GLX)
  37. add_executable(test_comp_glx_novnd main.c)
  38. target_link_libraries(test_comp_glx_novnd PRIVATE OpenGL::GL)
  39. add_test(NAME test_comp_glx_novnd COMMAND test_comp_glx_novnd)
  40. endif()
  41. # EGL is only available on Linux+GLVND at present.
  42. if(OpenGL_TEST_VND)
  43. find_package(OpenGL COMPONENTS OpenGL EGL)
  44. if(OpenGL_EGL_FOUND)
  45. add_executable(test_comp_egl main.c)
  46. target_link_libraries(test_comp_egl PRIVATE OpenGL::OpenGL OpenGL::EGL)
  47. add_test(NAME test_comp_egl COMMAND test_comp_egl)
  48. # EGL-only code should not link to GLX.
  49. execute_process(COMMAND ldd test_comp_egl
  50. OUTPUT_VARIABLE LDD_OUT
  51. ERROR_VARIABLE LDD_ERR)
  52. if("${LDD_OUT}" MATCHES "GLX")
  53. message(FATAL_ERROR "EGL-only code links to GLX!")
  54. endif()
  55. endif()
  56. # all three COMPONENTS together.
  57. find_package(OpenGL COMPONENTS OpenGL EGL GLX)
  58. if(OpenGL_EGL_FOUND AND OpenGL_GLX_FOUND)
  59. add_executable(test_comp_both main.c)
  60. target_link_libraries(test_comp_both PRIVATE OpenGL::OpenGL OpenGL::EGL
  61. OpenGL::GLX)
  62. add_test(NAME test_comp_both COMMAND test_comp_both)
  63. endif()
  64. endif()