Skip to content

Commit

Permalink
freetype: Switch to Meson build system
Browse files Browse the repository at this point in the history
This requires way less patching. Way less.
  • Loading branch information
jwillikers committed Mar 4, 2024
1 parent 1c888e8 commit cccf8ae
Show file tree
Hide file tree
Showing 15 changed files with 538 additions and 6 deletions.
5 changes: 0 additions & 5 deletions recipes/freetype/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
sources:
"2.13.2":
url:
- "https://download.savannah.gnu.org/releases/freetype/freetype-2.13.2.tar.xz"
- "https://sourceforge.net/projects/freetype/files/freetype2/2.13.2/freetype-2.13.2.tar.xz"
sha256: "12991c4e55c506dd7f9b765933e62fd2be2e06d421505d7950a132e4f1bb484d"
"2.13.0":
url:
- "https://download.savannah.gnu.org/releases/freetype/freetype-2.13.0.tar.xz"
Expand Down
2 changes: 1 addition & 1 deletion recipes/freetype/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
versions:
"2.13.2":
folder: all
folder: meson
"2.13.0":
folder: all
"2.12.1":
Expand Down
6 changes: 6 additions & 0 deletions recipes/freetype/meson/conandata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sources:
"2.13.2":
url:
- "https://download.savannah.gnu.org/releases/freetype/freetype-2.13.2.tar.xz"
- "https://sourceforge.net/projects/freetype/files/freetype2/2.13.2/freetype-2.13.2.tar.xz"
sha256: "12991c4e55c506dd7f9b765933e62fd2be2e06d421505d7950a132e4f1bb484d"
240 changes: 240 additions & 0 deletions recipes/freetype/meson/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
from conan import ConanFile
from conan.tools.apple import fix_apple_shared_install_name
from conan.tools.files import (
apply_conandata_patches, collect_libs, copy, export_conandata_patches, load,
get, rename, replace_in_file, rmdir, save
)
from conan.tools.env import VirtualBuildEnv
from conan.tools.gnu import PkgConfigDeps
from conan.tools.layout import basic_layout
from conan.tools.meson import Meson, MesonToolchain
import os
import re
import textwrap

required_conan_version = ">=1.53.0"


class FreetypeConan(ConanFile):
name = "freetype"
description = "FreeType is a freely available software library to render fonts."
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://www.freetype.org"
license = "FTL"
topics = ("freetype", "fonts")
package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
"with_png": [True, False],
"with_zlib": [True, False],
"with_bzip2": [True, False],
"with_brotli": [True, False],
"subpixel": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
"with_png": True,
"with_zlib": True,
"with_bzip2": True,
"with_brotli": True,
"subpixel": False,
}

def export_sources(self):
export_conandata_patches(self)

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def configure(self):
if self.options.shared:
self.options.rm_safe("fPIC")
self.settings.rm_safe("compiler.cppstd")
self.settings.rm_safe("compiler.libcxx")

def layout(self):
basic_layout(self, src_folder="src")

def requirements(self):
if self.options.with_png:
self.requires("libpng/1.6.42")
if self.options.with_zlib:
self.requires("zlib/[>=1.2.10 <2]")
if self.options.with_bzip2:
self.requires("bzip2/1.0.8")
if self.options.get_safe("with_brotli"):
self.requires("brotli/1.1.0")

def build_requirements(self):
self.tool_requires("meson/1.3.2")
if not self.conf.get("tools.gnu:pkg_config", default=False, check_type=str):
self.tool_requires("pkgconf/2.1.0")

def source(self):
get(self, **self.conan_data["sources"][self.version], strip_root=True)

def generate(self):
virtual_build_env = VirtualBuildEnv(self)
virtual_build_env.generate()
deps = PkgConfigDeps(self)
deps.generate()

def feature(option):
return "enabled" if option else "disabled"

tc = MesonToolchain(self)
tc.project_options["brotli"] = feature(self.options.with_brotli)
tc.project_options["bzip2"] = feature(self.options.with_bzip2)
# Harfbuzz support introduces a circular dependency between Harfbuzz and Freetype.
# They both have options to require each other.
tc.project_options["harfbuzz"] = "disabled"
tc.project_options["png"] = feature(self.options.with_png)
tc.project_options["tests"] = "disabled"
tc.project_options["zlib"] = "system" if self.options.with_zlib else "disabled"
tc.generate()

def _patch_sources(self):
apply_conandata_patches(self)
config_h = os.path.join(self.source_folder, "include", "freetype", "config", "ftoption.h")
if self.options.subpixel:
replace_in_file(self, config_h, "/* #define FT_CONFIG_OPTION_SUBPIXEL_RENDERING */", "#define FT_CONFIG_OPTION_SUBPIXEL_RENDERING")

def build(self):
self._patch_sources()
meson = Meson(self)
meson.configure()
meson.build()

def _make_freetype_config(self, version):
freetype_config_in = os.path.join(self.source_folder, "builds", "unix", "freetype-config.in")
if not os.path.isdir(os.path.join(self.package_folder, "bin")):
os.makedirs(os.path.join(self.package_folder, "bin"))
freetype_config = os.path.join(self.package_folder, "bin", "freetype-config")
rename(self, freetype_config_in, freetype_config)
libs = "-lfreetyped" if self.settings.build_type == "Debug" else "-lfreetype"
staticlibs = f"-lm {libs}" if self.settings.os == "Linux" else libs
replace_in_file(self, freetype_config, r"%PKG_CONFIG%", r"/bin/false") # never use pkg-config
replace_in_file(self, freetype_config, r"%prefix%", r"$conan_prefix")
replace_in_file(self, freetype_config, r"%exec_prefix%", r"$conan_exec_prefix")
replace_in_file(self, freetype_config, r"%includedir%", r"$conan_includedir")
replace_in_file(self, freetype_config, r"%libdir%", r"$conan_libdir")
replace_in_file(self, freetype_config, r"%ft_version%", r"$conan_ftversion")
replace_in_file(self, freetype_config, r"%LIBSSTATIC_CONFIG%", r"$conan_staticlibs")
replace_in_file(self, freetype_config, r"-lfreetype", libs)
replace_in_file(self, freetype_config, r"export LC_ALL", textwrap.dedent("""\
export LC_ALL
BINDIR=$(dirname $0)
conan_prefix=$(dirname $BINDIR)
conan_exec_prefix=${{conan_prefix}}/bin
conan_includedir=${{conan_prefix}}/include
conan_libdir=${{conan_prefix}}/lib
conan_ftversion={version}
conan_staticlibs="{staticlibs}"
""").format(version=version, staticlibs=staticlibs))

def _extract_libtool_version(self):
conf_raw = load(self, os.path.join(self.source_folder, "builds", "unix", "configure.raw"))
return next(re.finditer(r"^version_info='([0-9:]+)'", conf_raw, flags=re.M)).group(1).replace(":", ".")

@property
def _libtool_version_txt(self):
return os.path.join(self.package_folder, "res", "freetype-libtool-version.txt")

def package(self):
meson = Meson(self)
meson.install()

libtool_version = self._extract_libtool_version()
save(self, self._libtool_version_txt, libtool_version)
self._make_freetype_config(libtool_version)

doc_folder = os.path.join(self.source_folder, "docs")
license_folder = os.path.join(self.package_folder, "licenses")
copy(self, "FTL.TXT", doc_folder, license_folder)
copy(self, "GPLv2.TXT", doc_folder, license_folder)
copy(self, "LICENSE.TXT", doc_folder, license_folder)

rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))
rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
self._create_cmake_module_variables(
os.path.join(self.package_folder, self._module_vars_rel_path)
)
self._create_cmake_module_alias_targets(
os.path.join(self.package_folder, self._module_target_rel_path),
{"freetype": "Freetype::Freetype"}
)

fix_apple_shared_install_name(self)

def _create_cmake_module_variables(self, module_file):
content = textwrap.dedent(f"""\
set(FREETYPE_FOUND TRUE)
if(DEFINED Freetype_INCLUDE_DIRS)
set(FREETYPE_INCLUDE_DIRS ${{Freetype_INCLUDE_DIRS}})
endif()
if(DEFINED Freetype_LIBRARIES)
set(FREETYPE_LIBRARIES ${{Freetype_LIBRARIES}})
endif()
set(FREETYPE_VERSION_STRING "{self.version}")
""")
save(self, module_file, content)

def _create_cmake_module_alias_targets(self, module_file, targets):
content = ""
for alias, aliased in targets.items():
content += textwrap.dedent("""\
if(TARGET {aliased} AND NOT TARGET {alias})
add_library({alias} INTERFACE IMPORTED)
set_property(TARGET {alias} PROPERTY INTERFACE_LINK_LIBRARIES {aliased})
endif()
""".format(alias=alias, aliased=aliased))
save(self, module_file, content)

@property
def _module_vars_rel_path(self):
return os.path.join("lib", "cmake", f"conan-official-{self.name}-variables.cmake")

@property
def _module_target_rel_path(self):
return os.path.join("lib", "cmake", f"conan-official-{self.name}-targets.cmake")

@staticmethod
def _chmod_plus_x(filename):
if os.name == "posix" and (os.stat(filename).st_mode & 0o111) != 0o111:
os.chmod(filename, os.stat(filename).st_mode | 0o111)

def package_info(self):
self.cpp_info.set_property("cmake_find_mode", "both")
self.cpp_info.set_property("cmake_module_file_name", "Freetype")
self.cpp_info.set_property("cmake_file_name", "freetype")
self.cpp_info.set_property("cmake_target_name", "Freetype::Freetype")
self.cpp_info.set_property("cmake_target_aliases", ["freetype"]) # other possible target name in upstream config file
self.cpp_info.set_property("cmake_build_modules", [self._module_vars_rel_path])
self.cpp_info.set_property("pkg_config_name", "freetype2")
self.cpp_info.libs = collect_libs(self)
if self.settings.os in ["Linux", "FreeBSD"]:
self.cpp_info.system_libs.append("m")
self.cpp_info.includedirs.append(os.path.join("include", "freetype2"))

libtool_version = load(self, self._libtool_version_txt).strip()
self.conf_info.define("user.freetype:libtool_version", libtool_version)
# FIXME: need to do override the pkg_config version (pkg_config_custom_content does not work)
# self.cpp_info.version["pkg_config"] = pkg_config_version

# TODO: to remove in conan v2 once cmake_find_package* & pkg_config generators removed
self.cpp_info.filenames["cmake_find_package"] = "Freetype"
self.cpp_info.filenames["cmake_find_package_multi"] = "freetype"
self.cpp_info.names["cmake_find_package"] = "Freetype"
self.cpp_info.names["cmake_find_package_multi"] = "Freetype"
self.cpp_info.build_modules["cmake_find_package"] = [self._module_vars_rel_path]
self.cpp_info.build_modules["cmake_find_package_multi"] = [self._module_target_rel_path]
self.cpp_info.names["pkg_config"] = "freetype2"
freetype_config = os.path.join(self.package_folder, "bin", "freetype-config")
self.env_info.PATH.append(os.path.join(self.package_folder, "bin"))
self.env_info.FT2_CONFIG = freetype_config
self._chmod_plus_x(freetype_config)
self.user_info.LIBTOOL_VERSION = libtool_version
13 changes: 13 additions & 0 deletions recipes/freetype/meson/patches/2.13.0-0001-fix-fallthrough.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--- a/include/freetype/internal/compiler-macros.h
+++ b/include/freetype/internal/compiler-macros.h
@@ -42,8 +42,8 @@ FT_BEGIN_HEADER
( defined( __cplusplus ) && __cplusplus > 201402L )
# define FALL_THROUGH [[__fallthrough__]]
# elif ( defined( __GNUC__ ) && __GNUC__ >= 7 ) || \
- ( defined( __clang__ ) && __clang_major__ >= 10 )
-# define FALL_THROUGH __attribute__(( __fallthrough__ ))
+ ( defined( __clang__ ) && __clang_major__ >= 12 )
+# define FALL_THROUGH __attribute__((fallthrough))
# else
# define FALL_THROUGH ( (void)0 )
# endif
7 changes: 7 additions & 0 deletions recipes/freetype/meson/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.1)
project(test_package LANGUAGES C)

find_package(freetype REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.c)
target_link_libraries(${PROJECT_NAME} PRIVATE freetype)
Binary file not shown.
27 changes: 27 additions & 0 deletions recipes/freetype/meson/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from conan import ConanFile
from conan.tools.cmake import CMake, cmake_layout
from conan.tools.build import can_run
import os


class TestPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
test_type = "explicit"

def layout(self):
cmake_layout(self)

def requirements(self):
self.requires(self.tested_reference_str)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package")
font_path = os.path.join(self.source_folder, "OpenSans-Bold.ttf")
self.run(f"{bin_path} {font_path}", env="conanrun")
Loading

0 comments on commit cccf8ae

Please sign in to comment.