changeset 668:8002da0f5f0b

cmake/FindSDL2: revamp - Enable imported targets, - While here, add official SDL2 addons.
author David Demelier <markand@malikania.fr>
date Sat, 03 Oct 2020 16:15:16 +0200
parents 325be9bbc68f
children 72e143a85525
files cmake/FindSDL2.cmake
diffstat 1 files changed, 81 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/cmake/FindSDL2.cmake	Fri Feb 21 15:09:53 2020 +0100
+++ b/cmake/FindSDL2.cmake	Sat Oct 03 16:15:16 2020 +0200
@@ -1,28 +1,101 @@
 # FindSDL2
 # --------
 #
-# Find SDL2 library, this modules defines:
+# Find SDL2 library and components, this modules defines:
+#
+#  SDL2_LIBRARY, the name of the library to link against.
+#  SDL2_MAIN_LIBRARY, for SDL2main (if present).
+#  SDL2_LIBRARIES, alias to SDL2_LIBRARY.
+#  SDL2_FOUND, true if found.
+#  SDL2_INCLUDE_DIR, where to find SDL.h.
+#
+# The following imported targets will be defined:
+#
+# SDL2::SDL2
+# SDL2::SDL2main (if present)
 #
-#  SDL2_LIBRARY, the name of the library to link against
-#  SDL2_FOUND, if false, do not try to link to SDL
-#  SDL2_INCLUDE_DIR, where to find SDL.h
+# This module also handle the following official SDL addons:
+#
+# - image
+# - mixer
+# - net
+# - ttf
+#
+# And thus, variables SDL2_<C>_LIBRARY, SDL2_<C>_INCLUDE_DIRS and SDL2::<C>
+# imported targets will be defined if they are found.
+#
 
+include(FindPackageHandleStandardArgs)
+
+# The official include convention is <SDL.h> not <SDL/SDL.h>.
 find_path(
 	SDL2_INCLUDE_DIR
 	NAMES SDL.h
 	PATH_SUFFIXES include/SDL2 include
 )
 
-find_library(SDL2_LIBRARY NAMES SDL2 SDL2-2.0 libSDL2 libSDL2-2.0)
+find_library(SDL2_LIBRARY NAMES SDL2 libSDL2)
+find_library(SDL2_MAIN_LIBRARY NAMES SDL2main libSDL2main)
+
+# Standard components.
+foreach (c ${SDL2_FIND_COMPONENTS})
+	find_path(
+		SDL2_${c}_INCLUDE_DIR
+		NAMES SDL.h
+		PATH_SUFFIXES include/SDL2 include
+	)
 
-include(FindPackageHandleStandardArgs)
+	find_library(
+		SDL2_${c}_LIBRARY
+		NAMES SDL2_${c} libSDL2_${c}
+	)
+
+	if (NOT TARGET SDL2::${c} AND SDL2_${c}_LIBRARY)
+		set(SDL2_${c}_FOUND TRUE)
+		add_library(SDL2::${c} UNKNOWN IMPORTED)
+		set_target_properties(
+			SDL2::${c}
+			PROPERTIES
+				IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+				IMPORTED_LOCATION "${SDL2_${c}_LIBRARY}"
+				INTERFACE_INCLUDE_DIRECTORIES "${SDL2_${c}_INCLUDE_DIRS}"
+		)
+	endif ()
+
+	mark_as_advanced(SDL2_${c}_INCLUDE_DIR SDL2_${c}_LIBRARY)
+endforeach ()
 
 find_package_handle_standard_args(
 	SDL2
 	REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR
+	HANDLE_COMPONENTS
 )
 
-set(SDL2_LIBRARIES ${SDL2_LIBRARY})
-set(SDL2_INCLUDE_DIRS ${SDL2_INCLUDE_DIR})
+if (SDL2_FOUND)
+	set(SDL2_LIBRARIES ${SDL2_LIBRARY})
+	set(SDL2_INCLUDE_DIRS ${SDL2_INCLUDE_DIR})
+
+	if (NOT TARGET SDL2::SDL2)
+		add_library(SDL2::SDL2 UNKNOWN IMPORTED)
+		set_target_properties(
+			SDL2::SDL2
+			PROPERTIES
+				IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+				IMPORTED_LOCATION "${SDL2_LIBRARY}"
+				INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIRS}"
+		)
+	endif ()
+
+	if (NOT TARGET SDL2::main AND SDL2_MAIN_LIBRARY)
+		add_library(SDL2::main UNKNOWN IMPORTED)
+		set_target_properties(
+			SDL2::main
+			PROPERTIES
+				IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+				IMPORTED_LOCATION "${SDL2_MAIN_LIBRARY}"
+				INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIRS}"
+		)
+	endif ()
+endif ()
 
 mark_as_advanced(SDL2_INCLUDE_DIR SDL2_LIBRARY)