SDL2Config.cmake 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. include("${CMAKE_CURRENT_LIST_DIR}/SDL2Targets.cmake")
  2. # provide ${SDL2_LIBRARIES}, ${SDL2_INCLUDE_DIRS} etc, like sdl2-config.cmake does,
  3. # for compatibility between SDL2 built with autotools and SDL2 built with CMake
  4. # the following seems to work on Windows for both MSVC and MINGW+MSYS and with both SDL2Config/Target.cmake
  5. # from vcpkg and from building myself with cmake from latest git
  6. # AND on Linux when building SDL2 (tested current git) with CMake
  7. # the headers are easy - but note that this adds both .../include/ and .../include/SDL2/
  8. # while the SDL2_INCLUDE_DIRS of sdl2-config.cmake only add ...include/SDL2/
  9. # But at least if building worked with sdl2-config.cmake it will also work with this.
  10. get_target_property(SDL2_INCLUDE_DIRS SDL2::SDL2 INTERFACE_INCLUDE_DIRECTORIES)
  11. # get the paths to the .lib files for both SDL2 and SDL2main
  12. # for the "normal"/release build they could be in lots of different properties..
  13. set(relprops IMPORTED_IMPLIB_RELEASE IMPORTED_IMPLIB_NOCONFIG IMPORTED_IMPLIB IMPORTED_IMPLIB_MINSIZEREL IMPORTED_IMPLIB_RELWITHDEBINFO
  14. IMPORTED_LOCATION_RELEASE IMPORTED_LOCATION_NOCONFIG IMPORTED_LOCATION IMPORTED_LOCATION_MINSIZEREL IMPORTED_LOCATION_RELWITHDEBINFO)
  15. # fewer possibilities for debug builds
  16. set(dbgprops IMPORTED_IMPLIB_DEBUG IMPORTED_LOCATION_DEBUG)
  17. foreach(prop ${relprops})
  18. get_target_property(sdl2implib SDL2::SDL2 ${prop})
  19. if(sdl2implib)
  20. #message("set sdl2implib from ${prop}")
  21. break()
  22. else()
  23. #message("no luck for sdl2implib with ${prop}")
  24. endif()
  25. endforeach()
  26. foreach(prop ${relprops})
  27. get_target_property(sdl2mainimplib SDL2::SDL2main ${prop})
  28. if(sdl2mainimplib)
  29. #message("set sdl2mainimplib from ${prop}")
  30. break()
  31. else()
  32. #message("no luck for sdl2mainimplib with ${prop}")
  33. endif()
  34. endforeach()
  35. foreach(prop ${dbgprops})
  36. get_target_property(sdl2implibdbg SDL2::SDL2 ${prop})
  37. if(sdl2implibdbg)
  38. #message("set sdl2implibdbg from ${prop}")
  39. break()
  40. else()
  41. #message("no luck for sdl2implibdbg with ${prop}")
  42. endif()
  43. endforeach()
  44. foreach(prop ${dbgprops})
  45. get_target_property(sdl2mainimplibdbg SDL2::SDL2main ${prop})
  46. if(sdl2mainimplibdbg)
  47. #message("set sdl2mainimplibdbg from ${prop}")
  48. break()
  49. else()
  50. #message("no luck for sdl2mainimplibdbg with ${prop}")
  51. endif()
  52. endforeach()
  53. if( sdl2implib AND sdl2mainimplib AND sdl2implibdbg AND sdl2mainimplibdbg )
  54. # we have both release and debug builds of SDL2 and SDL2main, so use this ugly
  55. # generator expression in SDL2_LIBRARIES to support both in MSVC, depending on build type configured there
  56. set(SDL2_LIBRARIES $<IF:$<CONFIG:Debug>,${sdl2mainimplibdbg},${sdl2mainimplib}> $<IF:$<CONFIG:Debug>,${sdl2implibdbg},${sdl2implib}>)
  57. else()
  58. if( (NOT sdl2implib) AND sdl2implibdbg ) # if we only have a debug version of the lib
  59. set(sdl2implib sdl2implibdbg)
  60. endif()
  61. if( (NOT sdl2mainimplib) AND sdl2mainimplibdbg ) # if we only have a debug version of the lib
  62. set(sdl2mainimplib sdl2mainimplibdbg)
  63. endif()
  64. if( sdl2implib AND sdl2mainimplib )
  65. set(SDL2_LIBRARIES ${sdl2mainimplib} ${sdl2implib})
  66. elseif(WIN32 OR APPLE) # I think these platforms have a non-dummy SDLmain?
  67. message(FATAL_ERROR, "SDL2::SDL2 and/or SDL2::SDL2main don't seem to contain any kind of IMPORTED_IMPLIB* or IMPORTED_LOCATION*")
  68. elseif(sdl2implib) # on other platforms just libSDL2 will hopefully do?
  69. set(SDL2_LIBRARIES ${sdl2implib})
  70. message(STATUS, "No SDL2main lib not found, I hope you don't need it..")
  71. else()
  72. message(FATAL_ERROR, "SDL2::SDL2 doesn't seem to contain any kind of lib to link against in IMPORTED_IMPLIB* or IMPORTED_LOCATION*")
  73. endif()
  74. endif()
  75. get_filename_component(SDL2_LIBDIR ${sdl2implib} PATH)
  76. # NOTE: SDL2_LIBRARIES now looks like "c:/path/to/SDL2main.lib;c:/path/to/SDL2.lib"
  77. # which is different to what it looks like when coming from sdl2-config.cmake
  78. # (there it's more like "-L${SDL2_LIBDIR} -lSDL2main -lSDL2" - and also -lmingw32 and -mwindows)
  79. # This seems to work with both MSVC and MinGW though, while the other only worked with MinGW
  80. # We *could* use if(MSVC) here and make the MinGW case oldschool, BUT keep in mind that
  81. # for some reason vcpkg has SDL2.lib and SDL2main.lib in different directories!
  82. # On Linux it looks like "/tmp/sdl2inst/lib/libSDL2main.a;/tmp/sdl2inst/lib/libSDL2-2.0.so.0.14.1" which also seems to work
  83. if(FALSE) # this (not tested much) could be used to make SDL2_LIBRARIES look/behave more like the original from sdl2-config.cmake
  84. #if(NOT MSVC) # this is GCC/ld syntax (should also work for mingw and clang), not suitable for MSVC
  85. if(sdl2mainimplib) # we have libSDL2main
  86. # first set libSDL2main and its directory
  87. get_filename_component(sdl2main_libdir ${sdl2mainimplib} PATH)
  88. set(SDL2_LIBRARIES "-L${sdl2main_libdir} -lSDL2main")
  89. # SDL2main can be in a different directory than libSDL2 itself, at least when using vcpkg
  90. # if that's the case, add an additional "-L" part for libSDL2's libdir
  91. if( NOT (sdl2main_libdir STREQUAL SDL2_LIBDIR) )
  92. set(SDL2_LIBRARIES "${SDL2_LIBRARIES} -L${SDL2_LIBDIR}")
  93. endif()
  94. # lastly, add -lSDL2 itself
  95. set(SDL2_LIBRARIES "${SDL2_LIBRARIES} -lSDL2")
  96. unset(sdl2main_libdir)
  97. else() # no SDL2main, just libSDL2 itself
  98. set(SDL2_LIBRARIES "-L${SDL2_LIBDIR} -lSDL2")
  99. endif()
  100. endif()
  101. # 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 ..
  102. get_filename_component(SDL2_EXEC_PREFIX ${SDL2_LIBDIR} PATH)
  103. set(SDL2_PREFIX ${SDL2_EXEC_PREFIX}) # TODO: could this be somewhere else? parent dir of include or sth?
  104. unset(sdl2implib)
  105. unset(sdl2mainimplib)
  106. unset(sdl2implibdbg)
  107. unset(sdl2mainimplibdbg)
  108. unset(relprops)
  109. unset(dbgprops)