cmake: more proper sdl2-config.cmake work (thanks, Alexander!)

Fixes Bugzilla #4970.

--HG--
extra : amend_source : 58e7991ac25965c177e3f11346396f19e2ca6c5d
diff --git a/Makefile.in b/Makefile.in
index 6b4eecf..65b0fd9 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -46,7 +46,7 @@
 
 INSTALL_SDL2_CONFIG = @INSTALL_SDL2_CONFIG@
 
-SRC_DIST = *.txt acinclude Android.mk autogen.sh android-project build-scripts cmake cmake_uninstall.cmake.in configure configure.ac debian docs include Makefile.* sdl2-config.cmake.in sdl2-config.in sdl2.m4 sdl2.pc.in SDL2.spec.in SDL2Config.cmake src test VisualC.html VisualC VisualC-WinRT Xcode Xcode-iOS wayland-protocols
+SRC_DIST = *.txt acinclude Android.mk autogen.sh android-project build-scripts cmake cmake_uninstall.cmake.in configure configure.ac debian docs include Makefile.* sdl2-config.cmake.in sdl2-config-version.cmake.in sdl2-config.in sdl2.m4 sdl2.pc.in SDL2.spec.in SDL2Config.cmake src test VisualC.html VisualC VisualC-WinRT Xcode Xcode-iOS wayland-protocols
 GEN_DIST = SDL2.spec
 
 ifneq ($V,1)
@@ -188,6 +188,7 @@
 ifeq ($(INSTALL_SDL2_CONFIG),TRUE)
 	$(SHELL) $(auxdir)/mkinstalldirs $(DESTDIR)$(libdir)/cmake/SDL2
 	$(INSTALL) -m 644 sdl2-config.cmake $(DESTDIR)$(libdir)/cmake/SDL2
+	$(INSTALL) -m 644 sdl2-config-version.cmake $(DESTDIR)$(libdir)/cmake/SDL2
 endif
 
 uninstall: uninstall-bin uninstall-hdrs uninstall-lib uninstall-data
@@ -208,6 +209,7 @@
 	rm -f $(DESTDIR)$(datadir)/aclocal/sdl2.m4
 	rm -f $(DESTDIR)$(libdir)/pkgconfig/sdl2.pc
 	rm -f $(DESTDIR)$(libdir)/cmake/SDL2/sdl2-config.cmake
+	rm -f $(DESTDIR)$(libdir)/cmake/SDL2/sdl2-config-version.cmake
 
 clean:
 	rm -rf $(objects)
diff --git a/configure b/configure
index d8da74a..744a916 100755
--- a/configure
+++ b/configure
@@ -25627,7 +25627,7 @@
 $WAYLAND_PROTOCOLS_DEPENDS
 __EOF__
 
-ac_config_files="$ac_config_files Makefile:Makefile.in:Makefile.rules sdl2-config sdl2-config.cmake SDL2.spec sdl2.pc"
+ac_config_files="$ac_config_files Makefile:Makefile.in:Makefile.rules sdl2-config sdl2-config.cmake sdl2-config-version.cmake SDL2.spec sdl2.pc"
 
 ac_config_commands="$ac_config_commands sdl2_config"
 
@@ -26779,6 +26779,7 @@
     "Makefile") CONFIG_FILES="$CONFIG_FILES Makefile:Makefile.in:Makefile.rules" ;;
     "sdl2-config") CONFIG_FILES="$CONFIG_FILES sdl2-config" ;;
     "sdl2-config.cmake") CONFIG_FILES="$CONFIG_FILES sdl2-config.cmake" ;;
+    "sdl2-config-version.cmake") CONFIG_FILES="$CONFIG_FILES sdl2-config-version.cmake" ;;
     "SDL2.spec") CONFIG_FILES="$CONFIG_FILES SDL2.spec" ;;
     "sdl2.pc") CONFIG_FILES="$CONFIG_FILES sdl2.pc" ;;
     "sdl2_config") CONFIG_COMMANDS="$CONFIG_COMMANDS sdl2_config" ;;
diff --git a/configure.ac b/configure.ac
index aa751d1..e16c02e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -4293,7 +4293,7 @@
 __EOF__
 
 AC_CONFIG_FILES([
-    Makefile:Makefile.in:Makefile.rules sdl2-config sdl2-config.cmake SDL2.spec sdl2.pc
+    Makefile:Makefile.in:Makefile.rules sdl2-config sdl2-config.cmake sdl2-config-version.cmake SDL2.spec sdl2.pc
 ])
 AC_CONFIG_COMMANDS([sdl2_config],[chmod a+x sdl2-config])
 
diff --git a/debian/libsdl2-dev.install b/debian/libsdl2-dev.install
index a1f02d8..40a9754 100644
--- a/debian/libsdl2-dev.install
+++ b/debian/libsdl2-dev.install
@@ -5,4 +5,5 @@
 usr/lib/*/*.so
 usr/lib/*/pkgconfig/sdl2.pc
 usr/lib/*/cmake/SDL2/sdl2-config.cmake
+usr/lib/*/cmake/SDL2/sdl2-config-version.cmake
 usr/share/aclocal/sdl2.m4
diff --git a/sdl2-config.cmake.in b/sdl2-config.cmake.in
index 03efbe1..110c605 100644
--- a/sdl2-config.cmake.in
+++ b/sdl2-config.cmake.in
@@ -9,3 +9,31 @@
 set(SDL2_INCLUDE_DIRS "@includedir@/SDL2")
 set(SDL2_LIBRARIES "-L${SDL2_LIBDIR} @SDL_RLD_FLAGS@ @SDL_LIBS@")
 string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES)
+
+if(NOT TARGET SDL2::SDL2)
+  # Remove -lSDL2 as that is handled by CMake, note the space at the end so it does not replace e.g. -lSDL2main
+  # This may require "libdir" beeing set (from above)
+  string(REPLACE "-lSDL2 " "" SDL2_EXTRA_LINK_FLAGS "@SDL_RLD_FLAGS@ @SDL_LIBS@ ")
+  string(STRIP "${SDL2_EXTRA_LINK_FLAGS}" SDL2_EXTRA_LINK_FLAGS)
+  string(REPLACE "-lSDL2 " "" SDL2_EXTRA_LINK_FLAGS_STATIC "@SDL_RLD_FLAGS@ @SDL_STATIC_LIBS@ ")
+  string(STRIP "${SDL2_EXTRA_LINK_FLAGS_STATIC}" SDL2_EXTRA_LINK_FLAGS_STATIC)
+
+  add_library(SDL2::SDL2 SHARED IMPORTED)
+  set_target_properties(SDL2::SDL2 PROPERTIES
+    INTERFACE_INCLUDE_DIRECTORIES "@includedir@/SDL2"
+    IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+    IMPORTED_LOCATION "@libdir@/libSDL2.so"
+    INTERFACE_LINK_LIBRARIES "${SDL2_EXTRA_LINK_FLAGS}")
+
+  add_library(SDL2::SDL2-static STATIC IMPORTED)
+  set_target_properties(SDL2::SDL2-static PROPERTIES
+    INTERFACE_INCLUDE_DIRECTORIES "@includedir@/SDL2"
+    IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+    IMPORTED_LOCATION "@libdir@/libSDL2.a"
+    INTERFACE_LINK_LIBRARIES "${SDL2_EXTRA_LINK_FLAGS_STATIC}")
+
+  add_library(SDL2::SDL2main STATIC IMPORTED)
+  set_target_properties(SDL2::SDL2main PROPERTIES
+    IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+    IMPORTED_LOCATION "@libdir@/libSDL2main.a")
+endif()