From: Julian Ospald <hasufell@gentoo.org>
Date: Sun Dec  8 19:28:11 UTC 2013
Subject: prefer pkg-config for freetype2 detection

https://github.com/minetest/minetest/pull/1042

--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -173,7 +173,22 @@
 find_package(OpenGLES2)
 
 if(USE_FREETYPE)
-	find_package(Freetype REQUIRED)
+	if(UNIX)
+		include(FindPkgConfig)
+		if(PKG_CONFIG_FOUND)
+			pkg_check_modules(FREETYPE QUIET freetype2)
+			if(FREETYPE_FOUND)
+				SET(FREETYPE_PKGCONFIG_FOUND TRUE)
+				SET(FREETYPE_LIBRARY ${FREETYPE_LIBRARIES})
+				# because cmake is idiotic
+				string(REPLACE ";" " " FREETYPE_CFLAGS_STR ${FREETYPE_CFLAGS})
+				string(REPLACE ";" " " FREETYPE_LDFLAGS_STR ${FREETYPE_LDFLAGS})
+			endif(FREETYPE_FOUND)
+		endif(PKG_CONFIG_FOUND)
+	endif(UNIX)
+	if(NOT FREETYPE_FOUND)
+		find_package(Freetype REQUIRED)
+	endif(NOT FREETYPE_FOUND)
 	set(CGUITTFONT_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/cguittfont")
 	set(CGUITTFONT_LIBRARY cguittfont)
 endif(USE_FREETYPE)
@@ -446,6 +461,12 @@
 		)
 	endif(USE_CURL)
 	if(USE_FREETYPE)
+		if(FREETYPE_PKGCONFIG_FOUND)
+			set_target_properties(${PROJECT_NAME}
+				PROPERTIES
+				COMPILE_FLAGS "${FREETYPE_CFLAGS_STR}"
+			)
+		endif(FREETYPE_PKGCONFIG_FOUND)
 		target_link_libraries(
 			${PROJECT_NAME}
 			${FREETYPE_LIBRARY}
--- a/src/cguittfont/CMakeLists.txt
+++ b/src/cguittfont/CMakeLists.txt
@@ -1,17 +1,29 @@
-include_directories(
-  ${IRRLICHT_INCLUDE_DIR}
-  ${FREETYPE_INCLUDE_DIRS}
-)
-
 # CGUITTFont authors, y u no include headers you use?
 #   Do not add CGUITTFont.cpp to the line below.
 #   xCGUITTFont.cpp is a wrapper file that includes
 #   additional required headers.
 add_library(cguittfont xCGUITTFont.cpp)
 
+if(FREETYPE_PKGCONFIG_FOUND)
+	set_target_properties(cguittfont
+		PROPERTIES
+		COMPILE_FLAGS "${FREETYPE_CFLAGS_STR}"
+		LINK_FLAGS "${FREETYPE_LDFLAGS_STR}"
+	)
+
+	include_directories(
+	  ${IRRLICHT_INCLUDE_DIR}
+	)
+else(FREETYPE_PKGCONFIG_FOUND)
+	include_directories(
+	  ${IRRLICHT_INCLUDE_DIR}
+	  ${FREETYPE_INCLUDE_DIRS}
+	)
+endif(FREETYPE_PKGCONFIG_FOUND)
+
 target_link_libraries(
-  cguittfont
-  ${IRRLICHT_LIBRARY}
-  ${FREETYPE_LIBRARY}
-  ${ZLIB_LIBRARIES}  # needed by freetype, repeated here for safety
-)
+	cguittfont
+	${IRRLICHT_LIBRARY}
+	${FREETYPE_LIBRARY}
+	${ZLIB_LIBRARIES}  # needed by freetype, repeated here for safety
+	)
