FindSDL2_image.cmake 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #[[
  2. Custom find module for SDL_image for Emscripten compilation.
  3. Input variables:
  4. SDL2_image_FORMATS
  5. Output variables:
  6. SDL2_image_FOUND
  7. SDL2_image_VERSION
  8. Resulting targets:
  9. SDL2_image::SDL2_image
  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_image::SDL2_image)
  16. # If no version was specified, set default
  17. if(NOT DEFINED SDL2_image_FIND_VERSION)
  18. set(SDL2_image_FIND_VERSION "2")
  19. endif()
  20. if(NOT DEFINED SDL2_image_FORMATS)
  21. set(SDL2_image_FORMATS "[tga]")
  22. endif()
  23. # Check if requested SDL version is valid
  24. if((SDL2_image_FIND_VERSION VERSION_LESS "2") OR (SDL2_image_FIND_VERSION VERSION_GREATER_EQUAL "3"))
  25. message(FATAL_ERROR "The requested SDL2_image version ${SDL2_image_FIND_VERSION} is invalid.")
  26. endif()
  27. message(NOTICE
  28. "SDL_image with Emscripten can only be used if SDL usage is also enabled. "
  29. "The version number of both libraries must match as well."
  30. )
  31. # Emscripten includes SDL support as part of it's SDK, meaning there's no need to find it
  32. set(SDL2_image_FOUND TRUE)
  33. add_library(SDL2_image::SDL2_image INTERFACE IMPORTED)
  34. # Set found SDL_image version based on latest Emscripten SDK at the time of writing this file
  35. # Version set based on latest Emscripten SDK at the time of writing this file
  36. set(SDL2_image_VERSION "2.24.2")
  37. # Version number to pass to compiler
  38. set(SDL2_image_EMSCRIPTEN_COMPILER_SELECTED_VERSION "2")
  39. set(SDL2_image_TARGET_ARGS "-sUSE_SDL_IMAGE=${SDL2_image_EMSCRIPTEN_COMPILER_SELECTED_VERSION}")
  40. if(SDL2_image_FORMATS)
  41. list(APPEND SDL2_image_TARGET_ARGS "-sSDL2_IMAGE_FORMATS=${SDL2_image_FORMATS}")
  42. endif()
  43. # Enable compilation and linking against SDL
  44. target_compile_options(SDL2_image::SDL2_image INTERFACE "${SDL2_image_TARGET_ARGS}")
  45. target_link_libraries(SDL2_image::SDL2_image INTERFACE "${SDL2_image_TARGET_ARGS}")
  46. # Get final compiler and linker flags to print them
  47. get_target_property(SDL2_image_COMPILE_FLAGS SDL2_image::SDL2_image "INTERFACE_COMPILE_OPTIONS")
  48. get_target_property(SDL2_image_LINK_FLAGS SDL2_image::SDL2_image "INTERFACE_LINK_OPTIONS")
  49. find_package_message(
  50. "SDL2_image"
  51. "SDL_image ${SDL2_image_VERSION} has been found as part of the Emscripten SDK."
  52. "[${SDL2_image_COMPILE_FLAGS}][${SDL2_image_LINK_FLAGS}]"
  53. )
  54. # Clean scope
  55. unset(SDL2_image_TARGET_ARGS)
  56. unset(SDL2_image_COMPILE_FLAGS)
  57. unset(SDL2_image_LINK_FLAGS)
  58. unset(SDL2_image_EMSCRIPTEN_COMPILER_SELECTED_VERSION)
  59. else()
  60. # Since the target already exists, we declare it as found
  61. set(SDL2_image_FOUND TRUE)
  62. if(NOT DEFINED SDL2_image_VERSION)
  63. set(SDL2_image_VERSION "UNKNOWN")
  64. endif()
  65. endif()