FindSDL2.cmake 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #[[
  2. Custom find module for SDL for Emscripten compilation.
  3. Input variables:
  4. none
  5. Output variables:
  6. SDL2_FOUND
  7. SDL2_VERSION
  8. Resulting targets:
  9. SDL2::SDL2
  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 SDL2::SDL2)
  16. # If no version was specified, set default
  17. if(NOT DEFINED SDL2_FIND_VERSION)
  18. set(SDL2_FIND_VERSION "2")
  19. endif()
  20. # Check if requested SDL version is valid
  21. if((SDL2_FIND_VERSION VERSION_LESS "2") OR (SDL2_FIND_VERSION VERSION_GREATER_EQUAL "3"))
  22. message(FATAL_ERROR "The requested SDL2 version ${SDL2_FIND_VERSION} is invalid.")
  23. endif()
  24. # Emscripten includes SDL support as part of it's SDK, meaning there's no need to find it
  25. set(SDL2_FOUND TRUE)
  26. add_library(SDL2::SDL2 INTERFACE IMPORTED)
  27. # Set found SDL 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(SDL2_VERSION "2.24.2")
  30. # Version number to pass to compiler
  31. set(SDL2_EMSCRIPTEN_COMPILER_SELECTED_VERSION "2")
  32. # Enable compilation and linking against SDL
  33. target_compile_options(SDL2::SDL2 INTERFACE "-sUSE_SDL=${SDL2_EMSCRIPTEN_COMPILER_SELECTED_VERSION}")
  34. target_link_libraries(SDL2::SDL2 INTERFACE "-sUSE_SDL=${SDL2_EMSCRIPTEN_COMPILER_SELECTED_VERSION}")
  35. # Get final compiler and linker flags to print them
  36. get_target_property(SDL2_COMPILE_FLAGS SDL2::SDL2 "INTERFACE_COMPILE_OPTIONS")
  37. get_target_property(SDL2_LINK_FLAGS SDL2::SDL2 "INTERFACE_LINK_OPTIONS")
  38. find_package_message(
  39. "SDL2"
  40. "SDL ${SDL2_VERSION} has been found as part of the Emscripten SDK."
  41. "[${SDL2_COMPILE_FLAGS}][${SDL2_LINK_FLAGS}]"
  42. )
  43. # Clean scope
  44. unset(SDL2_COMPILE_FLAGS)
  45. unset(SDL2_COMPILE_FLAGS)
  46. unset(SDL2_EMSCRIPTEN_COMPILER_SELECTED_VERSION)
  47. else()
  48. # Since the target already exists, we declare it as found
  49. set(SDL2_FOUND TRUE)
  50. if(NOT DEFINED SDL2_VERSION)
  51. set(SDL2_VERSION "UNKNOWN")
  52. endif()
  53. endif()