Skip to content

CMakeLists.txt: Provide both freetype and Freetype::Freetype targets

freetype can be located by consuming projects that use find_package(Freetype) either via the old MODULE path using the CMake supplied FindFreetype.cmake or via the new CONFIG path that uses the freetype-config.cmake supplied by this project. Up to this point the CMake module has supplied the target "Freetype::Freetype" and the config provided by this project the target "freetype". Now we supply both "freetype" and "Freetype::Freetype" so consuming projects can always use the target "Freetype::Freetype" regardless of what path was taken by find_package(Freetype).

Fixes #1165 (closed).

With this change the generated freetype-cmake.config is changed like this:

--- build1/install/lib64/cmake/freetype/freetype-config.cmake	2022-06-23 08:15:34.000000000 +0200
+++ build2/install/lib64/cmake/freetype/freetype-config.cmake	2022-06-23 08:50:51.000000000 +0200
@@ -16,7 +16,7 @@
 set(_targetsDefined)
 set(_targetsNotDefined)
 set(_expectedTargets)
-foreach(_expectedTarget freetype)
+foreach(_expectedTarget freetype Freetype::Freetype)
   list(APPEND _expectedTargets ${_expectedTarget})
   if(NOT TARGET ${_expectedTarget})
     list(APPEND _targetsNotDefined ${_expectedTarget})
@@ -58,8 +58,15 @@
   INTERFACE_LINK_LIBRARIES "/usr/lib64/libz.so;/usr/lib64/libbz2.so;/usr/lib64/libpng.so;/usr/lib64/libz.so;/usr/lib64/libharfbuzz.so;/usr/lib64/libbrotlidec.so"
 )
 
-if(CMAKE_VERSION VERSION_LESS 2.8.12)
-  message(FATAL_ERROR "This file relies on consumers using CMake 2.8.12 or greater.")
+# Create imported target Freetype::Freetype
+add_library(Freetype::Freetype INTERFACE IMPORTED)
+
+set_target_properties(Freetype::Freetype PROPERTIES
+  INTERFACE_LINK_LIBRARIES "freetype"
+)
+
+if(CMAKE_VERSION VERSION_LESS 3.0.0)
+  message(FATAL_ERROR "This file relies on consumers using CMake 3.0.0 or greater.")
 endif()
 
 # Load information for each installed configuration.

I'm not a CMake expert, but this change looks right to me. It bumps the minimum CMake version required up from 2.8.12 to 3.0.0. The 3.0.0 version of CMake was released in June 2014 so being 8 years old, I think that should be acceptable.

Merge request reports