Commit ebf9e5ac719a5a1a374dbf2dd6b12e84de9466ed

Nikolaus Waxweiler 2021-05-15T22:36:00

[CMake] Update dependency finders. 1. Fixes CMake using any found HarfBuzz version lower than the minimum required. This is based on HALX99's merge request at https://gitlab.freedesktop.org/freetype/freetype/-/merge_requests/31 2. Update FindHarfBuzz.cmake from https://github.com/WebKit/WebKit/blob/1ce32454/Source/cmake/FindHarfBuzz.cmake and guard post-CMake-3.1 features to keep the minimum version unchanged 3. Update FindBrotliDec.cmake to stop the warnings, based on what https://github.com/google/woff2/blob/a0d0ed7d/cmake/FindBrotliDec.cmake is doing * CMakeLists.txt, builds/cmake/FindHarfBuzz.cmake: Implement 1 and 2. * builds/cmake/FindBrotliDec.cmake: Implement 3.

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ffb7c48..e9b6fb6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -103,6 +103,9 @@ if (NOT CMAKE_VERSION VERSION_LESS 3.3)
   # Allow symbol visibility settings also on static libraries. CMake < 3.3
   # only sets the property on a shared library build.
   cmake_policy(SET CMP0063 NEW)
+
+  # Support new IN_LIST if() operator.
+  cmake_policy(SET CMP0057 NEW)
 endif ()
 
 include(CheckIncludeFile)
@@ -461,9 +464,9 @@ if (PNG_FOUND)
   target_include_directories(freetype PRIVATE ${PNG_INCLUDE_DIRS})
   list(APPEND PKG_CONFIG_REQUIRED_PRIVATE "libpng")
 endif ()
-if (HARFBUZZ_FOUND)
-  target_link_libraries(freetype PRIVATE ${HARFBUZZ_LIBRARIES})
-  target_include_directories(freetype PRIVATE ${HARFBUZZ_INCLUDE_DIRS})
+if (HarfBuzz_FOUND)
+  target_link_libraries(freetype PRIVATE ${HarfBuzz_LIBRARY})
+  target_include_directories(freetype PRIVATE ${HarfBuzz_INCLUDE_DIRS})
   list(APPEND PKG_CONFIG_REQUIRED_PRIVATE "harfbuzz >= ${HARFBUZZ_MIN_VERSION}")
 endif ()
 if (BROTLIDEC_FOUND)
diff --git a/ChangeLog b/ChangeLog
index d3583a4..0a63e60 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2021-05-19  Nikolaus Waxweiler  <madigens@gmail.com>
+
+	[CMake] Update dependency finders.
+
+	1. Fixes CMake using any found HarfBuzz version lower than the minimum
+	required. This is based on HALX99's merge request at
+	https://gitlab.freedesktop.org/freetype/freetype/-/merge_requests/31
+	2. Update FindHarfBuzz.cmake from
+	https://github.com/WebKit/WebKit/blob/1ce32454/Source/cmake/FindHarfBuzz.cmake
+	and guard post-CMake-3.1 features to keep the minimum version unchanged
+	3. Update FindBrotliDec.cmake to stop the warnings, based on what
+	https://github.com/google/woff2/blob/a0d0ed7d/cmake/FindBrotliDec.cmake
+	is doing
+
+	* CMakeLists.txt, builds/cmake/FindHarfBuzz.cmake: Implement 1 and 2.
+	* builds/cmake/FindBrotliDec.cmake: Implement 3.
+
 2021-05-19  Ben Wagner  <bungeman@chromium.org>
 
 	[gzip] Use exact type for `ft_gzip_alloc` and `ft_gzip_free`.
diff --git a/builds/cmake/FindBrotliDec.cmake b/builds/cmake/FindBrotliDec.cmake
index a4402d2..46356b1 100644
--- a/builds/cmake/FindBrotliDec.cmake
+++ b/builds/cmake/FindBrotliDec.cmake
@@ -19,7 +19,8 @@
 #   BROTLIDEC_INCLUDE_DIRS
 #   BROTLIDEC_LIBRARIES
 
-include(FindPkgConfig)
+find_package(PkgConfig QUIET)
+
 pkg_check_modules(PC_BROTLIDEC QUIET libbrotlidec)
 
 if (PC_BROTLIDEC_VERSION)
@@ -41,7 +42,7 @@ find_library(BROTLIDEC_LIBRARIES
 
 include(FindPackageHandleStandardArgs)
 find_package_handle_standard_args(
-  brotlidec
+  BrotliDec
   REQUIRED_VARS BROTLIDEC_INCLUDE_DIRS BROTLIDEC_LIBRARIES
   FOUND_VAR BROTLIDEC_FOUND
   VERSION_VAR BROTLIDEC_VERSION)
diff --git a/builds/cmake/FindHarfBuzz.cmake b/builds/cmake/FindHarfBuzz.cmake
index d489613..b481fa4 100644
--- a/builds/cmake/FindHarfBuzz.cmake
+++ b/builds/cmake/FindHarfBuzz.cmake
@@ -1,4 +1,5 @@
 # Copyright (c) 2012, Intel Corporation
+# Copyright (c) 2019 Sony Interactive Entertainment Inc.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions are met:
@@ -23,65 +24,180 @@
 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 # POSSIBILITY OF SUCH DAMAGE.
-
-
-# Try to find HarfBuzz include and library directories.
 #
-# After successful discovery, this will set for inclusion where needed:
+# Try to find Harfbuzz include and library directories.
 #
-#   HARFBUZZ_INCLUDE_DIRS - containg the HarfBuzz headers
-#   HARFBUZZ_LIBRARIES    - containg the HarfBuzz library
+# After successful discovery, this will set for inclusion where needed:
+# HarfBuzz_INCLUDE_DIRS - containg the HarfBuzz headers
+# HarfBuzz_LIBRARIES - containg the HarfBuzz library
+
+#[=======================================================================[.rst:
+FindHarfBuzz
+--------------
+
+Find HarfBuzz headers and libraries.
+
+Imported Targets
+^^^^^^^^^^^^^^^^
+
+``HarfBuzz::HarfBuzz``
+  The HarfBuzz library, if found.
+
+``HarfBuzz::ICU``
+  The HarfBuzz ICU library, if found.
 
-include(FindPkgConfig)
+Result Variables
+^^^^^^^^^^^^^^^^
+
+This will define the following variables in your project:
+
+``HarfBuzz_FOUND``
+  true if (the requested version of) HarfBuzz is available.
+``HarfBuzz_VERSION``
+  the version of HarfBuzz.
+``HarfBuzz_LIBRARIES``
+  the libraries to link against to use HarfBuzz.
+``HarfBuzz_INCLUDE_DIRS``
+  where to find the HarfBuzz headers.
+``HarfBuzz_COMPILE_OPTIONS``
+  this should be passed to target_compile_options(), if the
+  target is not used for linking
+
+#]=======================================================================]
+
+find_package(PkgConfig QUIET)
 pkg_check_modules(PC_HARFBUZZ QUIET harfbuzz)
+set(HarfBuzz_COMPILE_OPTIONS ${PC_HARFBUZZ_CFLAGS_OTHER})
+set(HarfBuzz_VERSION ${PC_HARFBUZZ_CFLAGS_VERSION})
+
+find_path(HarfBuzz_INCLUDE_DIR
+    NAMES hb.h
+    HINTS ${PC_HARFBUZZ_INCLUDEDIR} ${PC_HARFBUZZ_INCLUDE_DIRS}
+    PATH_SUFFIXES harfbuzz
+)
+
+find_library(HarfBuzz_LIBRARY
+    NAMES ${HarfBuzz_NAMES} harfbuzz
+    HINTS ${PC_HARFBUZZ_LIBDIR} ${PC_HARFBUZZ_LIBRARY_DIRS}
+)
+
+if (HarfBuzz_INCLUDE_DIR AND NOT HarfBuzz_VERSION)
+    if (EXISTS "${HarfBuzz_INCLUDE_DIR}/hb-version.h")
+        file(READ "${HarfBuzz_INCLUDE_DIR}/hb-version.h" _harfbuzz_version_content)
+
+        string(REGEX MATCH "#define +HB_VERSION_STRING +\"([0-9]+\\.[0-9]+\\.[0-9]+)\"" _dummy "${_harfbuzz_version_content}")
+        set(HarfBuzz_VERSION "${CMAKE_MATCH_1}")
+    endif ()
+endif ()
 
-find_path(HARFBUZZ_INCLUDE_DIRS
-  NAMES hb.h
-  HINTS ${PC_HARFBUZZ_INCLUDEDIR}
-        ${PC_HARFBUZZ_INCLUDE_DIRS}
-  PATH_SUFFIXES harfbuzz)
-
-find_library(HARFBUZZ_LIBRARIES
-  NAMES harfbuzz
-  HINTS ${PC_HARFBUZZ_LIBDIR}
-        ${PC_HARFBUZZ_LIBRARY_DIRS})
-
-if (HARFBUZZ_INCLUDE_DIRS)
-  if (EXISTS "${HARFBUZZ_INCLUDE_DIRS}/hb-version.h")
-    file(READ "${HARFBUZZ_INCLUDE_DIRS}/hb-version.h" _harfbuzz_version_content)
-
-    string(REGEX MATCH
-           "#define +HB_VERSION_STRING +\"([0-9]+\\.[0-9]+\\.[0-9]+)\""
-           _dummy "${_harfbuzz_version_content}")
-    set(HARFBUZZ_VERSION "${CMAKE_MATCH_1}")
+if ("${HarfBuzz_FIND_VERSION}" VERSION_GREATER "${HarfBuzz_VERSION}")
+  if (HarfBuzz_FIND_REQUIRED)
+    message(FATAL_ERROR
+      "Required version (" ${HarfBuzz_FIND_VERSION} ")"
+      " is higher than found version (" ${HarfBuzz_VERSION} ")")
+  else ()
+    message(WARNING
+      "Required version (" ${HarfBuzz_FIND_VERSION} ")"
+      " is higher than found version (" ${HarfBuzz_VERSION} ")")
+    unset(HarfBuzz_VERSION)
+    unset(HarfBuzz_INCLUDE_DIRS)
+    unset(HarfBuzz_LIBRARIES)
+    return ()
   endif ()
 endif ()
 
-if ("${harfbuzz_FIND_VERSION}" VERSION_GREATER "${HARFBUZZ_VERSION}")
-  message(FATAL_ERROR
-    "Required version (" ${harfbuzz_FIND_VERSION} ")"
-    " is higher than found version (" ${HARFBUZZ_VERSION} ")")
+# Find components
+if (HarfBuzz_INCLUDE_DIR AND HarfBuzz_LIBRARY)
+    set(_HarfBuzz_REQUIRED_LIBS_FOUND ON)
+    set(HarfBuzz_LIBS_FOUND "HarfBuzz (required): ${HarfBuzz_LIBRARY}")
+else ()
+    set(_HarfBuzz_REQUIRED_LIBS_FOUND OFF)
+    set(HarfBuzz_LIBS_NOT_FOUND "HarfBuzz (required)")
 endif ()
 
-include(FindPackageHandleStandardArgs)
-find_package_handle_standard_args(
-  harfbuzz
-  REQUIRED_VARS HARFBUZZ_INCLUDE_DIRS HARFBUZZ_LIBRARIES
-  VERSION_VAR HARFBUZZ_VERSION)
+if (NOT CMAKE_VERSION VERSION_LESS 3.3)
+  if ("ICU" IN_LIST HarfBuzz_FIND_COMPONENTS)
+      pkg_check_modules(PC_HARFBUZZ_ICU QUIET harfbuzz-icu)
+      set(HarfBuzz_ICU_COMPILE_OPTIONS ${PC_HARFBUZZ_ICU_CFLAGS_OTHER})
 
-mark_as_advanced(
-  HARFBUZZ_INCLUDE_DIRS
-  HARFBUZZ_LIBRARIES)
+      find_path(HarfBuzz_ICU_INCLUDE_DIR
+          NAMES hb-icu.h
+          HINTS ${PC_HARFBUZZ_ICU_INCLUDEDIR} ${PC_HARFBUZZ_ICU_INCLUDE_DIRS}
+          PATH_SUFFIXES harfbuzz
+      )
+
+      find_library(HarfBuzz_ICU_LIBRARY
+          NAMES ${HarfBuzz_ICU_NAMES} harfbuzz-icu
+          HINTS ${PC_HARFBUZZ_ICU_LIBDIR} ${PC_HARFBUZZ_ICU_LIBRARY_DIRS}
+      )
+
+      if (HarfBuzz_ICU_LIBRARY)
+          if (HarfBuzz_FIND_REQUIRED_ICU)
+              list(APPEND HarfBuzz_LIBS_FOUND "ICU (required): ${HarfBuzz_ICU_LIBRARY}")
+          else ()
+            list(APPEND HarfBuzz_LIBS_FOUND "ICU (optional): ${HarfBuzz_ICU_LIBRARY}")
+          endif ()
+      else ()
+          if (HarfBuzz_FIND_REQUIRED_ICU)
+            set(_HarfBuzz_REQUIRED_LIBS_FOUND OFF)
+            list(APPEND HarfBuzz_LIBS_NOT_FOUND "ICU (required)")
+          else ()
+            list(APPEND HarfBuzz_LIBS_NOT_FOUND "ICU (optional)")
+          endif ()
+      endif ()
+  endif ()
+endif ()
+
+if (NOT HarfBuzz_FIND_QUIETLY)
+    if (HarfBuzz_LIBS_FOUND)
+        message(STATUS "Found the following HarfBuzz libraries:")
+        foreach (found ${HarfBuzz_LIBS_FOUND})
+            message(STATUS " ${found}")
+        endforeach ()
+    endif ()
+    if (HarfBuzz_LIBS_NOT_FOUND)
+        message(STATUS "The following HarfBuzz libraries were not found:")
+        foreach (found ${HarfBuzz_LIBS_NOT_FOUND})
+            message(STATUS " ${found}")
+        endforeach ()
+    endif ()
+endif ()
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(HarfBuzz
+    FOUND_VAR HarfBuzz_FOUND
+    REQUIRED_VARS HarfBuzz_INCLUDE_DIR HarfBuzz_LIBRARY _HarfBuzz_REQUIRED_LIBS_FOUND
+    VERSION_VAR HarfBuzz_VERSION
+)
 
-# Allow easy linking as in
-#
-#   target_link_libraries(freetype PRIVATE Harfbuzz::Harfbuzz)
-#
 if (NOT CMAKE_VERSION VERSION_LESS 3.1)
-  if (HARFBUZZ_FOUND AND NOT TARGET Harfbuzz::Harfbuzz)
-    add_library(Harfbuzz::Harfbuzz INTERFACE IMPORTED)
-    set_target_properties(
-        Harfbuzz::Harfbuzz PROPERTIES
-          INTERFACE_INCLUDE_DIRECTORIES "${HARFBUZZ_INCLUDE_DIRS}")
+  if (HarfBuzz_LIBRARY AND NOT TARGET HarfBuzz::HarfBuzz)
+      add_library(HarfBuzz::HarfBuzz UNKNOWN IMPORTED GLOBAL)
+      set_target_properties(HarfBuzz::HarfBuzz PROPERTIES
+          IMPORTED_LOCATION "${HarfBuzz_LIBRARY}"
+          INTERFACE_COMPILE_OPTIONS "${HarfBuzz_COMPILE_OPTIONS}"
+          INTERFACE_INCLUDE_DIRECTORIES "${HarfBuzz_INCLUDE_DIR}"
+      )
+  endif ()
+
+  if (HarfBuzz_ICU_LIBRARY AND NOT TARGET HarfBuzz::ICU)
+      add_library(HarfBuzz::ICU UNKNOWN IMPORTED GLOBAL)
+      set_target_properties(HarfBuzz::ICU PROPERTIES
+          IMPORTED_LOCATION "${HarfBuzz_ICU_LIBRARY}"
+          INTERFACE_COMPILE_OPTIONS "${HarfBuzz_ICU_COMPILE_OPTIONS}"
+          INTERFACE_INCLUDE_DIRECTORIES "${HarfBuzz_ICU_INCLUDE_DIR}"
+      )
   endif ()
 endif ()
+
+mark_as_advanced(
+    HarfBuzz_INCLUDE_DIR
+    HarfBuzz_ICU_INCLUDE_DIR
+    HarfBuzz_LIBRARY
+    HarfBuzz_ICU_LIBRARY
+)
+
+if (HarfBuzz_FOUND)
+   set(HarfBuzz_LIBRARIES ${HarfBuzz_LIBRARY} ${HarfBuzz_ICU_LIBRARY})
+   set(HarfBuzz_INCLUDE_DIRS ${HarfBuzz_INCLUDE_DIR} ${HarfBuzz_ICU_INCLUDE_DIR})
+endif ()