Findglfw3.cmake 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #[[
  2. Custom find module for GLFW for Emscripten compilation.
  3. Input variables:
  4. none
  5. Output variables:
  6. glfw3_FOUND
  7. glfw3_VERSION
  8. Resulting targets:
  9. glfw
  10. More info:
  11. https://emscripten.org/docs/porting/multimedia_and_graphics/OpenGL-support.html
  12. ]]
  13. include(FindPackageHandleStandardArgs)
  14. include(FindPackageMessage)
  15. if(NOT TARGET glfw)
  16. # If no version was specified, set default
  17. if(NOT DEFINED glfw3_FIND_VERSION)
  18. set(glfw3_FIND_VERSION "1")
  19. endif()
  20. # Check if requested GLFW version is valid
  21. if((glfw3_FIND_VERSION VERSION_LESS "3") OR (glfw3_FIND_VERSION VERSION_GREATER_EQUAL "4"))
  22. message(FATAL_ERROR "The requested GLFW version ${glfw3_FIND_VERSION} is invalid.")
  23. endif()
  24. # Emscripten includes GLFW support as part of it's SDK, meaning there's no need to find it
  25. set(glfw3_FOUND TRUE)
  26. add_library(glfw INTERFACE IMPORTED)
  27. # Set found GLFW version based on latest Emscripten SDK at the time of writing this file
  28. # Version set based on latest Emscripten SDK at the time of writing this file
  29. set(glfw3_VERSION "3")
  30. # Version number to pass to compiler
  31. set(glfw3_EMSCRIPTEN_COMPILER_SELECTED_VERSION ${glfw3_VERSION})
  32. # Enable compilation and linking against GLFW
  33. target_compile_options(glfw INTERFACE "-sUSE_GLFW=${glfw3_EMSCRIPTEN_COMPILER_SELECTED_VERSION}")
  34. target_link_libraries(glfw INTERFACE "-sUSE_GLFW=${glfw3_EMSCRIPTEN_COMPILER_SELECTED_VERSION}")
  35. # Get final compiler and linker flags to print them
  36. get_target_property(glfw3_COMPILE_FLAGS glfw "INTERFACE_COMPILE_OPTIONS")
  37. get_target_property(glfw3_LINK_FLAGS glfw "INTERFACE_LINK_OPTIONS")
  38. find_package_message(
  39. "glfw3"
  40. "GLFW ${glfw3_VERSION} has been found as part of the Emscripten SDK."
  41. "[${glfw3_COMPILE_FLAGS}][${glfw3_LINK_FLAGS}]"
  42. )
  43. # Clean scope
  44. unset(glfw3_COMPILE_FLAGS)
  45. unset(glfw3_COMPILE_FLAGS)
  46. unset(glfw3_EMSCRIPTEN_COMPILER_SELECTED_VERSION)
  47. else()
  48. # Since the target already exists, we declare it as found
  49. set(glfw3_FOUND TRUE)
  50. if(NOT DEFINED glfw3_VERSION)
  51. set(glfw3_VERSION "UNKNOWN")
  52. endif()
  53. endif()