SDL2Config.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. include("${CMAKE_CURRENT_LIST_DIR}/SDL2Targets.cmake")
  2. # cuts off the path at the last (back)slash
  3. function(my_dirname FILEPATH RETVAL)
  4. string(FIND ${FILEPATH} "/" slashPos REVERSE)
  5. string(FIND ${FILEPATH} "\\" bsPos REVERSE) # TODO: untested, cmake always gave me forward slashes
  6. if(DEFINED slashPos)
  7. if( (DEFINED bsPos) AND (${bsPos} GREATER ${slashPos}) )
  8. set(slashPos, ${bsPos})
  9. endif()
  10. elseif(DEFINED bsPos)
  11. set(slashPos, ${bsPos})
  12. endif()
  13. if(DEFINED slashPos)
  14. string(SUBSTRING ${FILEPATH} 0 ${slashPos} dirname)
  15. set(${RETVAL} ${dirname} PARENT_SCOPE)
  16. endif()
  17. endfunction()
  18. if(WIN32)
  19. # this seems to work for both MSVC and MINGW+MSYS and with both SDL2Config/Target.cmake from vcpkg
  20. # and from building myself with cmake from latest git
  21. # ok, the headers are easy - but note that this adds both .../include and .../include/SDL2/
  22. # while the SDL2_INCLUDE_DIRS of sdl2-config.cmake only add ...include/SDL2/
  23. # But at least if building worked with sdl2-config.cmake it will also work with this.
  24. get_target_property(SDL2_INCLUDE_DIRS SDL2::SDL2 INTERFACE_INCLUDE_DIRECTORIES)
  25. # get the paths to the .lib files for both SDL2 and SDL2main
  26. # they could be in lots of different properties..
  27. get_target_property(sdl2implib SDL2::SDL2 IMPORTED_IMPLIB_RELEASE)
  28. get_target_property(sdl2implibdbg SDL2::SDL2 IMPORTED_IMPLIB_DEBUG)
  29. # TODO: for all this bla_RELEASE bla_DEBUG etc stuff, maybe only try the ones
  30. # from get_target_property(available_configs SDL2::SDL2 IMPORTED_CONFIGURATIONS) ?
  31. # OR otherwise at least try all of NOCONFIG, RELEASE, MINSIZEREL, RELWITHDEBINFO, DEBUG (possibly in that order)?
  32. get_target_property(sdl2mainimplib SDL2::SDL2main IMPORTED_IMPLIB_RELEASE)
  33. if(NOT sdl2mainimplib)
  34. get_target_property(sdl2mainimplib SDL2::SDL2main IMPORTED_LOCATION_RELEASE)
  35. endif()
  36. get_target_property(sdl2mainimplibdbg SDL2::SDL2main IMPORTED_IMPLIB_DEBUG)
  37. if(NOT sdl2mainimplibdbg)
  38. get_target_property(sdl2mainimplibdbg SDL2::SDL2main IMPORTED_LOCATION_DEBUG)
  39. endif()
  40. if( sdl2implib AND sdl2mainimplib AND sdl2implibdbg AND sdl2mainimplibdbg )
  41. # we have both release and debug builds of SDL2 and SDL2main, so use this ugly
  42. # generator expression in SDL2_LIBRARIES to support both in MSVC, depending on build type configured there
  43. set(SDL2_LIBRARIES $<IF:$<CONFIG:Debug>,${sdl2mainimplibdbg},${sdl2mainimplib}> $<IF:$<CONFIG:Debug>,${sdl2implibdbg},${sdl2implib}>)
  44. else()
  45. if(NOT sdl2implib) # try to get it from other properties
  46. foreach(prop IMPORTED_IMPLIB IMPORTED_IMPLIB_NOCONFIG IMPORTED_IMPLIB_DEBUG IMPORTED_LOCATION_RELEASE IMPORTED_LOCATION_DEBUG)
  47. get_target_property(sdl2implib SDL2::SDL2 ${prop})
  48. if(sdl2implib)
  49. message(STATUS "succeeded with ${prop} => ${sdl2implib}")
  50. break()
  51. endif()
  52. message(STATUS "no luck with ${prop}")
  53. endforeach()
  54. endif()
  55. if(NOT sdl2mainimplib)
  56. foreach(prop IMPORTED_IMPLIB IMPORTED_IMPLIB_NOCONFIG IMPORTED_IMPLIB_DEBUG IMPORTED_LOCATION_RELEASE IMPORTED_LOCATION_DEBUG)
  57. get_target_property(sdl2mainimplib SDL2::SDL2main ${prop})
  58. if(sdl2mainimplib)
  59. message(STATUS "succeeded with ${prop} => ${sdl2mainimplib}")
  60. break()
  61. endif()
  62. message(STATUS "no luck with ${prop}")
  63. endforeach()
  64. endif()
  65. if( sdl2implib AND sdl2mainimplib )
  66. set(SDL2_LIBRARIES ${sdl2mainimplib} ${sdl2implib})
  67. else()
  68. message(FATAL_ERROR, "SDL2::SDL2 and/or SDL2::SDL2main don't seem to contain any kind of IMPORTED_IMPLIB")
  69. endif()
  70. endif()
  71. # NOTE: SDL2_LIBRARIES now looks like "c:/path/to/SDL2main.lib;c:/path/to/SDL2.lib"
  72. # which is different to what it looks like when coming from sdl2-config.cmake
  73. # (there it's more like "-L${SDL2_LIBDIR} -lSDL2main -lSDL2" - and also -lmingw32 and -mwindows)
  74. # This seems to work with both MSVC and MinGW though, while the other only worked with MinGW
  75. my_dirname(${sdl2implib} SDL2_LIBDIR)
  76. my_dirname(${SDL2_LIBDIR} SDL2_EXEC_PREFIX) # the exec prefix is one level up from lib/ - TODO: really, always? at least on Linux there's /usr/lib/x86_64-bla-blub/libSDL2-asdf.so.0 ..
  77. set(SDL2_PREFIX ${SDL2_PREFIX}) # TODO: could this be somewhere else? parent dir of include or sth?
  78. elseif() # not windows
  79. # TODO: about the same but don't have to look at *_IMPLIB* as that's windows specific, it should be in IMPORTED_LOCATION*
  80. # TODO: make SDL2_LIBRARIES look more like the original (instead of path to libSDL2.so)?
  81. # TODO: anything special about macOS? I can't test that, I have no Mac
  82. endif()