From 6f8b0b0e2875f5d75e14b4061162bd03d786a7a3 Mon Sep 17 00:00:00 2001
From: rim <rozhuk.im@gmail.com>
Date: Mon, 17 Dec 2018 05:43:36 +0300
Subject: [PATCH] Update CMake build scripts

---
 CMakeLists.txt                    | 437 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 builds/unix/freetype-config.cmake | 211 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 builds/unix/freetype2.cmake       |  14 ++++++++++++++
 3 files changed, 484 insertions(+), 178 deletions(-)
 create mode 100644 builds/unix/freetype-config.cmake
 create mode 100644 builds/unix/freetype2.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c27ae95..3997fc1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,7 +1,7 @@
 # CMakeLists.txt
 #
 # Copyright 2013-2018 by
-# David Turner, Robert Wilhelm, and Werner Lemberg.
+# David Turner, Robert Wilhelm, Werner Lemberg and Rozhuk Ivan.
 #
 # Written originally by John Cary <cary@txcorp.com>
 #
@@ -69,32 +69,26 @@
 #   developer team.
 #
 # . Set the `FT_WITH_ZLIB', `FT_WITH_BZIP2', `FT_WITH_PNG', and
-#   `FT_WITH_HARFBUZZ' CMake variables to `ON' to force using a dependency.
-#   Leave a variable undefined (which is the default) to use the dependency
-#   only if it is available.  Set `CMAKE_DISABLE_FIND_PACKAGE_HarfBuzz=TRUE' to
-#   disable a dependency completely (CMake package name, so `BZip2' instead of
-#   `BZIP2'). Example:
-#
-#     cmake -DFT_WITH_ZLIB=ON -DCMAKE_DISABLE_FIND_PACKAGE_HarfBuzz=TRUE [...]
+#   `FT_WITH_HARFBUZZ' CMake variables to `ON' to use a dependency.
 #
 # . Installation of FreeType can be controlled with the CMake variables
 #   `SKIP_INSTALL_HEADERS', `SKIP_INSTALL_LIBRARIES', and `SKIP_INSTALL_ALL'
 #   (this is compatible with the same CMake variables in zlib's CMake
 #   support).
 
 # FreeType explicitly marks the API to be exported and relies on the compiler
 # to hide all other symbols. CMake supports a C_VISBILITY_PRESET property
 # starting with 2.8.12.
+
+############################# INITIAL SECTION ##########################
 cmake_minimum_required(VERSION 2.8.12)
 
 if (NOT CMAKE_VERSION VERSION_LESS 3.3)
   # Allow symbol visibility settings also on static libraries. CMake < 3.3
   # only sets the propery on a shared library build.
   cmake_policy(SET CMP0063 NEW)
 endif ()
 
-include(CheckIncludeFile)
-
 # CMAKE_TOOLCHAIN_FILE must be set before `project' is called, which
 # configures the base build environment and references the toolchain file
 if (APPLE)
@@ -133,146 +127,192 @@ endif ()
 
 project(freetype C)
 
-set(VERSION_MAJOR "2")
-set(VERSION_MINOR "9")
-set(VERSION_PATCH "1")
+set(PACKAGE_VERSION_MAJOR 2)
+set(PACKAGE_VERSION_MINOR 9)
+set(PACKAGE_VERSION_PATCH 1)
 
 # SOVERSION scheme: CURRENT.AGE.REVISION
 #   If there was an incompatible interface change:
 #     Increment CURRENT. Set AGE and REVISION to 0
 #   If there was a compatible interface change:
 #     Increment AGE. Set REVISION to 0
 #   If the source code was changed, but there were no interface changes:
 #     Increment REVISION.
-set(LIBRARY_VERSION "6.16.0")
-set(LIBRARY_SOVERSION "6")
+set(PACKAGE_LIBRARY_VERSION "6.17.0")
+set(PACKAGE_LIBRARY_SOVERSION "6")
+set(PACKAGE_LIBTOOL_VERSION "22.1.17")
+
+
+set(PACKAGE_NAME "${PROJECT_NAME}")
+set(PACKAGE_VERSION "${PACKAGE_VERSION_MAJOR}.${PACKAGE_VERSION_MINOR}.${PACKAGE_VERSION_PATCH}")
+set(PACKAGE_URL "https://www.freetype.org")
+set(PACKAGE_BUGREPORT "https://savannah.nongnu.org/bugs/?group=freetype")
+set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
+set(PACKAGE_DESCRIPTION "FreeType is a freely available software library to render fonts.")
+
+############################# OPTIONS SECTION ##########################
 
 # These options mean "require x and complain if not found". They'll get
 # optionally found anyway. Use `-DCMAKE_DISABLE_FIND_PACKAGE_x=TRUE` to disable
 # searching for a packge entirely (x is the CMake package name, so "BZip2"
 # instead of "BZIP2").
 option(FT_WITH_ZLIB "Use system zlib instead of internal library." OFF)
 option(FT_WITH_BZIP2 "Support bzip2 compressed fonts." OFF)
 option(FT_WITH_PNG "Support PNG compressed OpenType embedded bitmaps." OFF)
 option(FT_WITH_HARFBUZZ "Improve auto-hinting of OpenType fonts." OFF)
+option(FT_WITH_CONFIG "Install freetype-config." OFF)
+option(FT_DISABLE_MMAP "do not check mmap() and do not use." OFF)
 
+############################# INCLUDE SECTION ##########################
 
-# Disallow in-source builds
-if ("${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
-  message(FATAL_ERROR
-    "In-source builds are not permitted!  Make a separate folder for"
-    " building, e.g.,\n"
-    "  cmake -E make_directory build\n"
-    "  cmake -E chdir build cmake ..\n"
-    "Before that, remove the files created by this failed run with\n"
-    "  cmake -E remove CMakeCache.txt\n"
-    "  cmake -E remove_directory CMakeFiles")
-endif ()
+include(CheckIncludeFiles)
+include(CheckFunctionExists)
+include(CheckCCompilerFlag)
+include(GNUInstallDirs)
 
 
 # Add local cmake modules
 list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/builds/cmake)
 
-
-if (BUILD_FRAMEWORK)
-  if (NOT "${CMAKE_GENERATOR}" STREQUAL "Xcode")
-    message(FATAL_ERROR
-      "You should use Xcode generator with BUILD_FRAMEWORK enabled")
-  endif ()
-  set(CMAKE_OSX_ARCHITECTURES "$(ARCHS_STANDARD_32_64_BIT)")
-  set(BUILD_SHARED_LIBS ON)
-endif ()
-
-
+find_package(PkgConfig REQUIRED)
 # Find dependencies
 if (FT_WITH_HARFBUZZ)
   find_package(HarfBuzz 1.3.0 REQUIRED)
-else ()
-  find_package(HarfBuzz 1.3.0)
+  list(APPEND CMAKE_REQUIRED_LIBRARIES_PRIV harfbuzz)
 endif ()
 
 if (FT_WITH_PNG)
   find_package(PNG REQUIRED)
-else ()
-  find_package(PNG)
+  list(APPEND CMAKE_REQUIRED_LIBRARIES_PRIV libpng)
 endif ()
 
 if (FT_WITH_ZLIB)
   find_package(ZLIB REQUIRED)
-else ()
-  find_package(ZLIB)
+  list(APPEND CMAKE_REQUIRED_LIBRARIES_PRIV zlib)
 endif ()
 
 if (FT_WITH_BZIP2)
   find_package(BZip2 REQUIRED)
-else ()
-  find_package(BZip2)
+  #list(APPEND CMAKE_REQUIRED_LIBRARIES_PRIV bzip2)
+  set(BZIP2_LDFLAGS "-lbz2") # pkg_check_modules() always fail on BZip2.
 endif ()
 
-# Create the configuration file
-if (UNIX)
-  check_include_file("unistd.h" HAVE_UNISTD_H)
-  check_include_file("fcntl.h" HAVE_FCNTL_H)
-  check_include_file("stdint.h" HAVE_STDINT_H)
 
-  file(READ "${PROJECT_SOURCE_DIR}/builds/unix/ftconfig.in"
-    FTCONFIG_H)
-  if (HAVE_UNISTD_H)
-    string(REGEX REPLACE
-      "#undef +(HAVE_UNISTD_H)" "#define \\1 1"
-      FTCONFIG_H "${FTCONFIG_H}")
+############################# MACRO SECTION ############################
+macro(try_c_flag prop flag)
+  # Try flag once on the C compiler
+  check_c_compiler_flag("-Werror ${flag}" C_FLAG_${prop})
+  if (C_FLAG_${prop})
+    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
+  endif ()
+endmacro()
+
+macro(try_linker_flag prop flag)
+  # Check with the C compiler
+  set(CMAKE_REQUIRED_FLAGS ${flag})
+  check_c_compiler_flag(${flag} LINKER_FLAG_${prop})
+  set(CMAKE_REQUIRED_FLAGS "")
+  if (LINKER_FLAG_${prop})
+    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${flag}")
+    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${flag}")
+    set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${flag}")
   endif ()
-  if (HAVE_FCNTL_H)
+endmacro()
+
+function(check_include_files_buf filename prop incbuf)
+  check_include_files(${filename} ${prop})
+  if (${prop})
     string(REGEX REPLACE
-      "#undef +(HAVE_FCNTL_H)" "#define \\1 1"
-      FTCONFIG_H "${FTCONFIG_H}")
+      "#undef +(${prop})"
+      "#define \\1 1"
+      __tmp "${${incbuf}}")
+      set(${incbuf} "${__tmp}" PARENT_SCOPE)
   endif ()
-  if (HAVE_STDINT_H)
+endfunction()
+
+function(set_options_buf prop propb incbuf)
+  if (${prop})
     string(REGEX REPLACE
-      "#undef +(HAVE_STDINT_H)" "#define \\1 1"
-      FTCONFIG_H "${FTCONFIG_H}")
+      "/\\* +(#define +${propb}) +\\*/"
+      "\\1"
+      __tmp "${${incbuf}}")
+      set(${incbuf} "${__tmp}" PARENT_SCOPE)
   endif ()
-  string(REPLACE "/undef " "#undef "
-    FTCONFIG_H "${FTCONFIG_H}")
-else()
-  file(READ "${PROJECT_SOURCE_DIR}/include/freetype/config/ftconfig.h"
-    FTCONFIG_H)
-endif ()
-file(WRITE "${PROJECT_BINARY_DIR}/include/freetype/config/ftconfig.h"
-  "${FTCONFIG_H}")
+endfunction()
 
+############################# CONFIG SECTION ###########################
 
-# Create the options file
-file(READ "${PROJECT_SOURCE_DIR}/include/freetype/config/ftoption.h"
-  FTOPTION_H)
-if (ZLIB_FOUND)
-  string(REGEX REPLACE
-    "/\\* +(#define +FT_CONFIG_OPTION_SYSTEM_ZLIB) +\\*/" "\\1"
-    FTOPTION_H "${FTOPTION_H}")
-endif ()
-if (BZIP2_FOUND)
-  string(REGEX REPLACE
-    "/\\* +(#define +FT_CONFIG_OPTION_USE_BZIP2) +\\*/" "\\1"
-    FTOPTION_H "${FTOPTION_H}")
+# Disallow in-source builds
+if ("${PROJECT_BINARY_DIR}" STREQUAL "${PROJECT_SOURCE_DIR}")
+  message(FATAL_ERROR
+      "In-source builds are not permitted!  Make a separate folder for"
+      " building, e.g.,\n"
+      "  cmake -E make_directory build\n"
+      "  cmake -E chdir build cmake ..\n"
+      "Before that, remove the files created by this failed run with\n"
+      "  cmake -E remove CMakeCache.txt\n"
+      "  cmake -E remove_directory CMakeFiles")
 endif ()
-if (PNG_FOUND)
-  string(REGEX REPLACE
-    "/\\* +(#define +FT_CONFIG_OPTION_USE_PNG) +\\*/" "\\1"
-    FTOPTION_H "${FTOPTION_H}")
+
+if (BUILD_FRAMEWORK)
+  if (NOT "${CMAKE_GENERATOR}" STREQUAL "Xcode")
+    message(FATAL_ERROR
+      "You should use Xcode generator with BUILD_FRAMEWORK enabled")
+  endif ()
+  set(CMAKE_OSX_ARCHITECTURES "$(ARCHS_STANDARD_32_64_BIT)")
+  set(BUILD_SHARED_LIBS ON)
 endif ()
-if (HARFBUZZ_FOUND)
-  string(REGEX REPLACE
-    "/\\* +(#define +FT_CONFIG_OPTION_USE_HARFBUZZ) +\\*/" "\\1"
-    FTOPTION_H "${FTOPTION_H}")
+
+
+# Hardening flags
+try_c_flag(FPIE "-fPIE")
+try_linker_flag(PIE "-pie")
+try_linker_flag(ZRELRO "-Wl,-z,relro")
+try_linker_flag(ZNOW "-Wl,-z,now")
+try_linker_flag(ZNOEXECSTACK "-Wl,-z,noexecstack")
+
+
+# Silently strip whitespace
+string(STRIP "${CMAKE_C_FLAGS}" CMAKE_C_FLAGS)
+string(STRIP "${CMAKE_EXE_LINKER_FLAGS}" CMAKE_EXE_LINKER_FLAGS)
+string(STRIP "${CMAKE_MODULE_LINKER_FLAGS}" CMAKE_MODULE_LINKER_FLAGS)
+string(STRIP "${CMAKE_SHARED_LINKER_FLAGS}" CMAKE_SHARED_LINKER_FLAGS)
+
+
+# Check platform specific includes.
+# Create the configuration file
+if (UNIX)
+  file(READ "${PROJECT_SOURCE_DIR}/builds/unix/ftconfig.in" FTCONFIG_H)
+  string(REPLACE "/undef " "#undef " FTCONFIG_H "${FTCONFIG_H}")
+  check_include_files_buf(unistd.h HAVE_UNISTD_H FTCONFIG_H)
+  check_include_files_buf(fcntl.h HAVE_FCNTL_H FTCONFIG_H)
+  check_include_files_buf(stdint.h HAVE_STDINT_H FTCONFIG_H)
+  file(WRITE
+    "${PROJECT_BINARY_DIR}/include/freetype/config/ftconfig.h"
+    "${FTCONFIG_H}")
 endif ()
-file(WRITE "${PROJECT_BINARY_DIR}/include/freetype/config/ftoption.h"
+
+# Create the options file
+file(READ
+  "${PROJECT_SOURCE_DIR}/include/freetype/config/ftoption.h"
+  FTOPTION_H)
+set_options_buf(ZLIB_FOUND FT_CONFIG_OPTION_SYSTEM_ZLIB FTOPTION_H)
+set_options_buf(BZIP2_FOUND FT_CONFIG_OPTION_USE_BZIP2 FTOPTION_H)
+set_options_buf(PNG_FOUND FT_CONFIG_OPTION_USE_PNG FTOPTION_H)
+set_options_buf(HARFBUZZ_FOUND FT_CONFIG_OPTION_USE_HARFBUZZ FTOPTION_H)
+file(WRITE
+  "${PROJECT_BINARY_DIR}/include/freetype/config/ftoption.h"
   "${FTOPTION_H}")
 
 
-file(GLOB PUBLIC_HEADERS "include/ft2build.h" "include/freetype/*.h")
-file(GLOB PUBLIC_CONFIG_HEADERS "include/freetype/config/*.h")
-file(GLOB PRIVATE_HEADERS "include/freetype/internal/*.h")
+# Check platform API.
+if (NOT FT_DISABLE_MMAP)
+  check_function_exists(mmap HAVE_MMAP)
+endif ()
+
+################################ SUBDIRS SECTION #######################
 
+############################ TARGETS SECTION ###########################
 
 set(BASE_SRCS
   src/autofit/autofit.c
@@ -316,6 +356,13 @@ set(BASE_SRCS
   src/winfonts/winfnt.c
 )
 
+if (HAVE_MMAP)
+  list(APPEND BASE_SRCS "src/base/ftsystem.c")
+else ()
+  # xxx: unix, amiga, vms
+  list(APPEND BASE_SRCS "builds/unix/ftsystem.c")
+endif ()
+
 if (WIN32)
   enable_language(RC)
   list(APPEND BASE_SRCS builds/windows/ftdebug.c
@@ -330,25 +377,23 @@ if (BUILD_FRAMEWORK)
   list(APPEND BASE_SRCS builds/mac/freetype-Info.plist)
 endif ()
 
-
 if (NOT DISABLE_FORCE_DEBUG_POSTFIX)
   set(CMAKE_DEBUG_POSTFIX d)
-endif()
+endif ()
 
+file(GLOB PUBLIC_HEADERS "include/ft2build.h" "include/freetype/*.h")
+file(GLOB PUBLIC_CONFIG_HEADERS "include/freetype/config/*.h")
+file(GLOB PRIVATE_HEADERS "include/freetype/internal/*.h")
 
-add_library(freetype
+add_library(freetype OBJECT
   ${PUBLIC_HEADERS}
   ${PUBLIC_CONFIG_HEADERS}
   ${PRIVATE_HEADERS}
   ${BASE_SRCS}
 )
 
-set_target_properties(
-  freetype PROPERTIES
-    C_VISIBILITY_PRESET hidden)
-
-target_compile_definitions(
-  freetype PRIVATE FT2_BUILD_LIBRARY)
+set_target_properties(freetype PROPERTIES C_VISIBILITY_PRESET hidden)
+target_compile_definitions(freetype PRIVATE FT2_BUILD_LIBRARY)
 
 if (WIN32)
   target_compile_definitions(
@@ -359,64 +404,56 @@ if (WIN32)
   endif ()
 endif ()
 
-if (BUILD_SHARED_LIBS)
-  set_target_properties(freetype PROPERTIES
-    VERSION ${LIBRARY_VERSION}
-    SOVERSION ${LIBRARY_SOVERSION})
-endif ()
-
 # Pick up ftconfig.h and ftoption.h generated above, first.
 target_include_directories(
   freetype
     PUBLIC
       $<INSTALL_INTERFACE:include/freetype2>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
     PRIVATE
       ${CMAKE_CURRENT_BINARY_DIR}/include
-      ${CMAKE_CURRENT_SOURCE_DIR}/include)
+      ${CMAKE_CURRENT_SOURCE_DIR}/include
+      ${BZIP2_INCLUDE_DIR} # not BZIP2_INCLUDE_DIRS
+      ${ZLIB_INCLUDE_DIRS}
+      ${PNG_INCLUDE_DIRS}
+      ${HARFBUZZ_INCLUDE_DIRS})
 
+target_compile_definitions(freetype PRIVATE ${PNG_DEFINITIONS})
 
 if (BUILD_FRAMEWORK)
   set_property(SOURCE ${PUBLIC_CONFIG_HEADERS}
-    PROPERTY MACOSX_PACKAGE_LOCATION Headers/config
-  )
+    PROPERTY MACOSX_PACKAGE_LOCATION Headers/config)
   set_target_properties(freetype PROPERTIES
     FRAMEWORK TRUE
     MACOSX_FRAMEWORK_INFO_PLIST builds/mac/freetype-Info.plist
     PUBLIC_HEADER "${PUBLIC_HEADERS}"
-    XCODE_ATTRIBUTE_INSTALL_PATH "@rpath"
-  )
+    XCODE_ATTRIBUTE_INSTALL_PATH "@rpath")
 endif ()
 
 
-set(PKG_CONFIG_REQUIRED_PRIVATE "")
+add_library(freetype_static STATIC $<TARGET_OBJECTS:freetype>)
+target_link_libraries(freetype_static PRIVATE
+  ${BZIP2_LIBRARIES}
+  ${ZLIB_LIBRARIES}
+  ${PNG_LIBRARIES}
+  ${HARFBUZZ_LIBRARIES})
+set_target_properties(freetype_static PROPERTIES OUTPUT_NAME "freetype")
 
-if (ZLIB_FOUND)
-  target_link_libraries(freetype PRIVATE ${ZLIB_LIBRARIES})
-  target_include_directories(freetype PRIVATE ${ZLIB_INCLUDE_DIRS})
-  list(APPEND PKG_CONFIG_REQUIRED_PRIVATE zlib)
-endif ()
-if (BZIP2_FOUND)
-  target_link_libraries(freetype PRIVATE ${BZIP2_LIBRARIES})
-  target_include_directories(freetype PRIVATE ${BZIP2_INCLUDE_DIR}) # not BZIP2_INCLUDE_DIRS
-  list(APPEND PKG_CONFIG_REQUIRED_PRIVATE bzip2)
-endif ()
-if (PNG_FOUND)
-  target_link_libraries(freetype PRIVATE ${PNG_LIBRARIES})
-  target_compile_definitions(freetype PRIVATE ${PNG_DEFINITIONS})
-  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})
-  list(APPEND PKG_CONFIG_REQUIRED_PRIVATE harfbuzz)
+if (BUILD_SHARED_LIBS)
+  add_library(freetype_shared SHARED $<TARGET_OBJECTS:freetype>)
+  target_link_libraries(freetype_shared PRIVATE
+    ${BZIP2_LIBRARIES}
+    ${ZLIB_LIBRARIES}
+    ${PNG_LIBRARIES}
+    ${HARFBUZZ_LIBRARIES})
+  set_target_properties(freetype_shared PROPERTIES
+    VERSION ${PACKAGE_LIBRARY_VERSION}
+    SOVERSION ${PACKAGE_LIBRARY_SOVERSION}
+    OUTPUT_NAME "freetype")
 endif ()
 
-
-# Installation
-include(GNUInstallDirs)
+##################### INSTALLATION #####################################
 
 if (NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL)
   install(
@@ -435,67 +472,111 @@ if (NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL)
 endif ()
 
 if (NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL)
-  # Generate the pkg-config file
   if (UNIX)
-    file(READ ${PROJECT_SOURCE_DIR}/builds/unix/freetype2.in FREETYPE2_PC_IN)
-
-    string(REPLACE ";" ", " PKG_CONFIG_REQUIRED_PRIVATE "${PKG_CONFIG_REQUIRED_PRIVATE}")
-
-    string(REPLACE "%prefix%" ${CMAKE_INSTALL_PREFIX}
-           FREETYPE2_PC_IN ${FREETYPE2_PC_IN})
-    string(REPLACE "%exec_prefix%" "\${prefix}"
-           FREETYPE2_PC_IN ${FREETYPE2_PC_IN})
-    string(REPLACE "%libdir%" "\${prefix}/${CMAKE_INSTALL_LIBDIR}"
-           FREETYPE2_PC_IN ${FREETYPE2_PC_IN})
-    string(REPLACE "%includedir%" "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}"
-           FREETYPE2_PC_IN ${FREETYPE2_PC_IN})
-    string(REPLACE "%ft_version%" "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}"
-           FREETYPE2_PC_IN ${FREETYPE2_PC_IN})
-    string(REPLACE "%REQUIRES_PRIVATE%" "${PKG_CONFIG_REQUIRED_PRIVATE}"
-           FREETYPE2_PC_IN ${FREETYPE2_PC_IN})
-    string(REPLACE "%LIBS_PRIVATE%" ""  # All libs support pkg-config
-           FREETYPE2_PC_IN ${FREETYPE2_PC_IN})
-
-    file(WRITE ${PROJECT_BINARY_DIR}/freetype2.pc ${FREETYPE2_PC_IN})
-
-    install(
-      FILES ${PROJECT_BINARY_DIR}/freetype2.pc
+    # Create libs LD flags list
+    set(LIBS_LDFLAGS_LIST ${BZIP2_LDFLAGS})
+    foreach (__m ${CMAKE_REQUIRED_LIBRARIES_PRIV})
+      string(TOUPPER ${__m} __prefix)
+      pkg_check_modules(${__prefix} QUIET ${__m})
+      list(APPEND LIBS_LDFLAGS_LIST ${${__prefix}_LDFLAGS})
+      list(APPEND LIBS_LDFLAGS_LIST ${${__prefix}_LDFLAGS_OTHER})
+    endforeach ()
+    list(REMOVE_DUPLICATES LIBS_LDFLAGS_LIST)
+    string(REPLACE ";" " " LIBS_LDFLAGS_LIST "${LIBS_LDFLAGS_LIST}")
+  
+    # Generate the pkg-config file
+    # Prepare: PKGCONF_REQ_PRIV
+    string(REPLACE ";" ", "
+      PKGCONF_REQ_PRIV "${CMAKE_REQUIRED_LIBRARIES_PRIV}")
+    # Prepare: PKGCONF_LIBS_PRIV
+    set(PKGCONF_LIBS_PRIV ${LIBS_LDFLAGS_LIST})
+    string(REGEX REPLACE "-L[^ ]+" ""
+      PKGCONF_LIBS_PRIV "${PKGCONF_LIBS_PRIV}")
+    string(STRIP "${PKGCONF_LIBS_PRIV}" PKGCONF_LIBS_PRIV)
+    # Generate...
+    configure_file(
+      ${PROJECT_SOURCE_DIR}/builds/unix/freetype2.cmake
+      ${PROJECT_BINARY_DIR}/freetype2.pc
+      @ONLY)
+    install(FILES ${PROJECT_BINARY_DIR}/freetype2.pc
       DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
+      PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ
       COMPONENT pkgconfig)
+
+    # Generate the freetype-config file
+    if (FT_WITH_CONFIG)
+      # Prepare: PKG_CONFIG
+      if (DEFINED ENV{PKG_CONFIG})
+        set(PKG_CONFIG "$ENV{PKG_CONFIG}")
+      else ()
+        set(PKG_CONFIG ${PKG_CONFIG_EXECUTABLE})
+      endif ()
+      # Prepare: PKGCONF_LIBSSTATIC
+      set(PKGCONF_LIBSSTATIC "-lfreetype ${LIBS_LDFLAGS_LIST}")
+      string(STRIP "${PKGCONF_LIBSSTATIC}" PKGCONF_LIBSSTATIC)
+      # Generate...
+      configure_file(
+        ${PROJECT_SOURCE_DIR}/builds/unix/freetype-config.cmake
+        ${PROJECT_BINARY_DIR}/freetype-config
+        @ONLY)
+      install(FILES ${PROJECT_BINARY_DIR}/freetype-config
+        DESTINATION ${CMAKE_INSTALL_PREFIX}/bin/
+        PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
+          GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ
+        COMPONENT bin)
+      install(FILES ${PROJECT_SOURCE_DIR}/docs/freetype-config.1
+        DESTINATION ${CMAKE_INSTALL_MANDIR}/man1/)
+    endif ()
   endif ()
 
-  install(
-    TARGETS freetype
+  install(TARGETS freetype
+    EXPORT freetype-targets
+    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+    FRAMEWORK DESTINATION Library/Frameworks
+    COMPONENT libraries)
+  install(EXPORT freetype-targets
+    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/freetype
+    FILE freetype-config.cmake
+    COMPONENT headers)
+  install(FILES ${PROJECT_SOURCE_DIR}/builds/unix/freetype2.m4
+    DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/aclocal/
+    PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)
+
+  install(TARGETS freetype_static
+    EXPORT freetype-targets
+    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+    FRAMEWORK DESTINATION Library/Frameworks
+    COMPONENT libraries)
+  if (BUILD_SHARED_LIBS)
+    install(TARGETS freetype_shared
       EXPORT freetype-targets
       LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
       ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
       RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
       FRAMEWORK DESTINATION Library/Frameworks
       COMPONENT libraries)
-  install(
-    EXPORT freetype-targets
-      DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/freetype
-      FILE freetype-config.cmake
-      COMPONENT headers)
+  endif ()
 endif ()
 
+##################### Packaging #####################################
 
-# Packaging
-set(CPACK_PACKAGE_NAME ${CMAKE_PROJECT_NAME})
-set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "The FreeType font rendering library.")
+set(CPACK_PACKAGE_NAME ${PACKAGE_NAME})
+set(CPACK_PACKAGE_DESCRIPTION_SUMMARY ${PACKAGE_DESCRIPTION})
 set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README")
 set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/docs/LICENSE.TXT")
 
-set(CPACK_PACKAGE_VERSION_MAJOR ${VERSION_MAJOR})
-set(CPACK_PACKAGE_VERSION_MINOR ${VERSION_MINOR})
-set(CPACK_PACKAGE_VERSION_PATCH ${VERSION_PATCH})
-set(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
+set(CPACK_PACKAGE_VERSION_MAJOR ${PACKAGE_VERSION_MAJOR})
+set(CPACK_PACKAGE_VERSION_MINOR ${PACKAGE_VERSION_MINOR})
+set(CPACK_PACKAGE_VERSION_PATCH ${PACKAGE_VERSION_PATCH})
+set(CPACK_PACKAGE_VERSION ${PACKAGE_VERSION})
 
 if (WIN32)
   set(CPACK_GENERATOR ZIP)
-else()
+else ()
   set(CPACK_GENERATOR TGZ)
-endif()
+endif ()
 
 set(CPACK_COMPONENT_LIBRARIES_DISPLAY_NAME "Libraries")
 set(CPACK_COMPONENT_HEADERS_DISPLAY_NAME "C/C++ Headers")
diff --git a/builds/unix/freetype-config.cmake b/builds/unix/freetype-config.cmake
new file mode 100644
index 0000000..7749f45
--- /dev/null
+++ b/builds/unix/freetype-config.cmake
@@ -0,0 +1,211 @@
+#! /bin/sh
+#
+# Copyright 2000-2018 by
+# David Turner, Robert Wilhelm, and Werner Lemberg.
+#
+# This file is part of the FreeType project, and may only be used, modified,
+# and distributed under the terms of the FreeType project license,
+# LICENSE.TXT.  By continuing to use, modify, or distribute this file you
+# indicate that you have read the license and understand and accept it
+# fully.
+
+LC_ALL=C
+export LC_ALL
+
+
+# if `pkg-config' is available, use values from `freetype2.pc'
+@PKG_CONFIG@ --atleast-pkgconfig-version 0.24 >/dev/null 2>&1
+if test $? -eq 0 ; then
+  # note that option `--variable' is not affected by the
+  # PKG_CONFIG_SYSROOT_DIR environment variable
+  if test "x$SYSROOT" != "x" ; then
+    PKG_CONFIG_SYSROOT_DIR="$SYSROOT"
+    export PKG_CONFIG_SYSROOT_DIR
+  fi
+
+  prefix=`@PKG_CONFIG@ --variable prefix freetype2`
+  exec_prefix=`@PKG_CONFIG@ --variable exec_prefix freetype2`
+
+  includedir=`@PKG_CONFIG@ --variable includedir freetype2`
+  libdir=`@PKG_CONFIG@ --variable libdir freetype2`
+
+  version=`@PKG_CONFIG@ --modversion freetype2`
+
+  cflags=`@PKG_CONFIG@ --cflags freetype2`
+  dynamic_libs=`@PKG_CONFIG@ --libs freetype2`
+  static_libs=`@PKG_CONFIG@ --static --libs freetype2`
+else
+  prefix="@CMAKE_INSTALL_PREFIX@"
+  exec_prefix=${prefix}
+
+  includedir="${prefix}/@CMAKE_INSTALL_INCLUDEDIR@"
+  libdir="${prefix}/@CMAKE_INSTALL_LIBDIR@"
+
+  version=@PACKAGE_LIBTOOL_VERSION@
+
+  cflags="-I${SYSROOT}$includedir/freetype2"
+  dynamic_libs="-lfreetype"
+  static_libs="@PKGCONF_LIBSSTATIC@"
+  if test "${SYSROOT}$libdir" != "/usr/lib"   &&
+     test "${SYSROOT}$libdir" != "/usr/lib64" ; then
+    libs_L="-L${SYSROOT}$libdir"
+  fi
+fi
+
+orig_prefix=$prefix
+orig_exec_prefix=$exec_prefix
+
+orig_includedir=$includedir
+orig_libdir=$libdir
+
+include_suffix=`echo $includedir | sed "s|$prefix||"`
+lib_suffix=`echo $libdir | sed "s|$exec_prefix||"`
+
+
+usage()
+{
+  cat <<EOF
+Usage: freetype-config [OPTION]...
+Get FreeType compilation and linking information.
+
+Options:
+  --prefix               display \`--prefix' value used for building the
+                         FreeType library
+  --prefix=PREFIX        override \`--prefix' value with PREFIX
+  --exec-prefix          display \`--exec-prefix' value used for building
+                         the FreeType library
+  --exec-prefix=EPREFIX  override \`--exec-prefix' value with EPREFIX
+  --version              display libtool version of the FreeType library
+  --ftversion            display FreeType version number
+  --libs                 display flags for linking with the FreeType library
+  --libtool              display library name for linking with libtool
+  --cflags               display flags for compiling with the FreeType
+                         library
+  --static               make command line options display flags
+                         for static linking
+  --help                 display this help and exit
+EOF
+  exit $1
+}
+
+
+if test $# -eq 0 ; then
+  usage 1 1>&2
+fi
+
+
+while test $# -gt 0 ; do
+  case "$1" in
+  -*=*)
+    optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
+    ;;
+  *)
+    optarg=
+    ;;
+  esac
+
+  case $1 in
+  --prefix=*)
+    prefix=$optarg
+    local_prefix=yes
+    ;;
+  --prefix)
+    echo_prefix=yes
+    ;;
+  --exec-prefix=*)
+    exec_prefix=$optarg
+    exec_prefix_set=yes
+    local_prefix=yes
+    ;;
+  --exec-prefix)
+    echo_exec_prefix=yes
+    ;;
+  --version)
+    echo_version=yes
+    break
+    ;;
+  --ftversion)
+    echo_ft_version=yes
+    ;;
+  --cflags)
+    echo_cflags=yes
+    ;;
+  --libs)
+    echo_libs=yes
+    ;;
+  --libtool)
+    echo_libtool=yes
+    ;;
+  --static)
+    show_static=yes
+    ;;
+  --help)
+    usage 0
+    ;;
+  *)
+    usage 1 1>&2
+    ;;
+  esac
+  shift
+done
+
+
+if test "$local_prefix" = "yes" ; then
+  if test "$exec_prefix_set" != "yes" ; then
+    exec_prefix=$prefix
+  fi
+fi
+
+if test "$local_prefix" = "yes" ; then
+  includedir=${prefix}${include_suffix}
+  if test "$exec_prefix_set" = "yes" ; then
+    libdir=${exec_prefix}${lib_suffix}
+  else
+    libdir=${prefix}${lib_suffix}
+  fi
+fi
+
+
+if test "$echo_version" = "yes" ; then
+  echo $version
+fi
+
+if test "$echo_prefix" = "yes" ; then
+  echo ${SYSROOT}$prefix
+fi
+
+if test "$echo_exec_prefix" = "yes" ; then
+  echo ${SYSROOT}$exec_prefix
+fi
+
+if test "$echo_ft_version" = "yes" ; then
+  major=`grep define ${SYSROOT}$includedir/freetype2/freetype/freetype.h \
+         | grep FREETYPE_MAJOR \
+         | sed 's/.*[ 	]\([0-9][0-9]*\).*/\1/'`
+  minor=`grep define ${SYSROOT}$includedir/freetype2/freetype/freetype.h \
+         | grep FREETYPE_MINOR \
+         | sed 's/.*[ 	]\([0-9][0-9]*\).*/\1/'`
+  patch=`grep define ${SYSROOT}$includedir/freetype2/freetype/freetype.h \
+         | grep FREETYPE_PATCH \
+         | sed 's/.*[ 	]\([0-9][0-9]*\).*/\1/'`
+  echo $major.$minor.$patch
+fi
+
+if test "$echo_cflags" = "yes" ; then
+  echo $cflags | sed "s|$orig_includedir/freetype2|$includedir/freetype2|"
+fi
+
+if test "$echo_libs" = "yes" ; then
+  if test "$show_static" = "yes" ; then
+    libs="$libs_L $static_libs"
+  else
+    libs="$libs_L $dynamic_libs"
+  fi
+  echo $libs | sed "s|$orig_libdir|$libdir|"
+fi
+
+if test "$echo_libtool" = "yes" ; then
+  echo ${SYSROOT}$libdir/libfreetype.la
+fi
+
+# EOF
diff --git a/builds/unix/freetype2.cmake b/builds/unix/freetype2.cmake
new file mode 100644
index 0000000..b06a55a
--- /dev/null
+++ b/builds/unix/freetype2.cmake
@@ -0,0 +1,14 @@
+prefix=@CMAKE_INSTALL_PREFIX@
+exec_prefix=${prefix}
+libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
+includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
+
+Name: FreeType 2
+URL: https://freetype.org
+Description: A free, high-quality, and portable font engine.
+Version: @PACKAGE_LIBTOOL_VERSION@
+Requires:
+Requires.private: @PKGCONF_REQ_PRIV@
+Libs: -L${libdir} -lfreetype
+Libs.private: @PKGCONF_LIBS_PRIV@
+Cflags: -I${includedir}/freetype2
--
libgit2 0.28.1

