Skip to content

Commit d2b88f7

Browse files
committed
[libraries] Rework of freetype/harfbuzz recipes
To fix cyclic dependency between freetype and harfbuzz and to produce shared libraries, because we use those libraries in multiple recipes and building those libraries as shared libraries can reduce a little the apk size in some circumstances (whe we build multiple recipes that depends on those libraries) Note: Add `libfreetype6-dev` to docker dependencies or we will be unable to build freetype in travis tests
1 parent 00bd9d5 commit d2b88f7

File tree

4 files changed

+164
-38
lines changed

4 files changed

+164
-38
lines changed

Dockerfile.py2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ RUN dpkg --add-architecture i386 \
112112
# specific recipes dependencies (e.g. libffi requires autoreconf binary)
113113
RUN ${RETRY} apt -y install -qq --no-install-recommends \
114114
libffi-dev autoconf automake cmake gettext libltdl-dev libtool pkg-config \
115+
libfreetype6-dev \
115116
&& apt -y autoremove \
116117
&& apt -y clean
117118

Dockerfile.py3

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ RUN dpkg --add-architecture i386 \
113113
# specific recipes dependencies (e.g. libffi requires autoreconf binary)
114114
RUN ${RETRY} apt -y install -qq --no-install-recommends \
115115
libffi-dev autoconf automake cmake gettext libltdl-dev libtool pkg-config \
116+
libfreetype6-dev \
116117
&& apt -y autoremove \
117118
&& apt -y clean
118119

Lines changed: 96 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,118 @@
11
from pythonforandroid.toolchain import Recipe
2+
from pythonforandroid.logger import shprint, info
23
from pythonforandroid.util import current_directory
3-
from pythonforandroid.logger import shprint
4-
from os.path import exists, join, realpath
4+
from os.path import exists, join
5+
from multiprocessing import cpu_count
56
import sh
67

78

89
class FreetypeRecipe(Recipe):
10+
"""The freetype library it's special, because has cyclic dependencies with
11+
harfbuzz library, so freetype can be build with harfbuzz support, and
12+
harfbuzz can be build with freetype support. This complicates the build of
13+
both recipes because in order to get the full set we need to compile those
14+
recipes several times:
15+
- build freetype without harfbuzz
16+
- build harfbuzz with freetype
17+
- build freetype with harfbuzz support
18+
19+
.. note::
20+
To build freetype with harfbuzz support you must add `harfbuzz` to your
21+
requirements, otherwise freetype will be build without harfbuzz
22+
23+
.. seealso::
24+
https://sourceforge.net/projects/freetype/files/freetype2/2.5.3/
25+
"""
926

1027
version = '2.5.5'
1128
url = 'http://download.savannah.gnu.org/releases/freetype/freetype-{version}.tar.gz' # noqa
1229

13-
depends = ['harfbuzz']
14-
1530
def should_build(self, arch):
16-
if exists(join(self.get_build_dir(arch.arch),
17-
'objs', '.libs', 'libfreetype.a')):
31+
if exists(
32+
join(
33+
self.get_build_dir(arch.arch),
34+
'objs',
35+
'.libs',
36+
'libfreetype.so',
37+
)
38+
):
1839
return False
1940
return True
2041

21-
def build_arch(self, arch):
42+
def build_arch(self, arch, with_harfbuzz=False):
2243
env = self.get_recipe_env(arch)
44+
harfbuzz_in_recipes = 'harfbuzz' in self.ctx.recipe_build_order
45+
prefix_path = self.get_build_dir(arch.arch)
46+
if not harfbuzz_in_recipes:
47+
info('Building freetype (harfbuzz support disabled)')
48+
elif harfbuzz_in_recipes and not with_harfbuzz:
49+
info('First Build of freetype (without harfbuzz, for now...)')
50+
prefix_path = join(prefix_path, 'install')
51+
elif harfbuzz_in_recipes and with_harfbuzz:
52+
# Is the second build, now we link with harfbuzz
53+
info('Second Build of freetype: enabling harfbuzz support ...')
54+
harfbuzz_build = self.get_recipe(
55+
'harfbuzz', self.ctx
56+
).get_build_dir(arch.arch)
57+
freetype_install = join(self.get_build_dir(arch.arch), 'install')
58+
env['CFLAGS'] = ' '.join(
59+
[
60+
env['CFLAGS'],
61+
'-DFT_CONFIG_OPTION_USE_HARFBUZZ',
62+
'-I{harfbuzz}'.format(harfbuzz=harfbuzz_build),
63+
'-I{harfbuzz}/src'.format(harfbuzz=harfbuzz_build),
64+
'-I{freetype}/include/freetype2'.format(
65+
freetype=freetype_install
66+
),
67+
]
68+
)
2369

24-
harfbuzz_recipe = Recipe.get_recipe('harfbuzz', self.ctx)
25-
env['LDFLAGS'] = ' '.join(
26-
[env['LDFLAGS'],
27-
'-L{}'.format(join(harfbuzz_recipe.get_build_dir(arch.arch),
28-
'src', '.libs'))])
70+
env['HARFBUZZ_CFLAGS'] = '-I{harfbuzz} -I{harfbuzz}/src'.format(
71+
harfbuzz=harfbuzz_build
72+
)
73+
env['HARFBUZZ_LIBS'] = (
74+
'-L{freetype}/lib -lfreetype '
75+
'-L{harfbuzz}/src/.libs -lharfbuzz'.format(
76+
freetype=freetype_install, harfbuzz=harfbuzz_build
77+
)
78+
)
2979

80+
# Build freetype library
81+
config_args = {
82+
'--host={}'.format(arch.command_prefix),
83+
'--prefix={}'.format(prefix_path),
84+
'--without-zlib',
85+
'--with-png=no',
86+
}
87+
if not with_harfbuzz and harfbuzz_in_recipes:
88+
info('Build freetype for First time (without harfbuzz)')
89+
config_args = config_args.union({'--disable-shared'})
90+
elif with_harfbuzz or not harfbuzz_in_recipes:
91+
info(
92+
'Build freetype {}'.format(
93+
'for Second time (with harfbuzz)'
94+
if harfbuzz_in_recipes
95+
else '(without harfbuzz)'
96+
)
97+
)
98+
config_args = config_args.union(
99+
{'--disable-static', '--enable-shared'}
100+
)
101+
info('Configure args are: {}'.format('\n\t-'.join(config_args)))
30102
with current_directory(self.get_build_dir(arch.arch)):
31103
configure = sh.Command('./configure')
32-
shprint(configure,
33-
'--host=arm-linux-androideabi',
34-
'--prefix={}'.format(realpath('.')),
35-
'--without-zlib',
36-
'--with-png=no',
37-
'--disable-shared',
38-
_env=env)
39-
shprint(sh.make, '-j5', _env=env)
40-
41-
shprint(sh.cp, 'objs/.libs/libfreetype.a', self.ctx.libs_dir)
104+
shprint(configure, *config_args, _env=env)
105+
shprint(sh.make, '-j', str(cpu_count()), _env=env)
106+
107+
if not with_harfbuzz and harfbuzz_in_recipes:
108+
# First build, install the compiled lib, and clean build env
109+
shprint(sh.make, 'install', _env=env)
110+
shprint(sh.make, 'distclean', _env=env)
111+
else:
112+
# Second build (or the first if harfbuzz not enabled), now we
113+
# copy definitive libs to libs collection. Be sure to link your
114+
# recipes to the definitive library, located at: objs/.libs
115+
self.install_libs(arch, 'objs/.libs/libfreetype.so')
42116

43117

44118
recipe = FreetypeRecipe()
Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,89 @@
11
from pythonforandroid.toolchain import Recipe
22
from pythonforandroid.util import current_directory
33
from pythonforandroid.logger import shprint
4+
from multiprocessing import cpu_count
45
from os.path import exists, join
56
import sh
67

78

89
class HarfbuzzRecipe(Recipe):
10+
"""The harfbuzz library it's special, because has cyclic dependencies with
11+
freetype library, so freetype can be build with harfbuzz support, and
12+
harfbuzz can be build with freetype support. This complicates the build of
13+
both recipes because in order to get the full set we need to compile those
14+
recipes several times:
15+
- build freetype without harfbuzz
16+
- build harfbuzz with freetype
17+
- build freetype with harfbuzz support
18+
19+
.. seealso::
20+
https://sourceforge.net/projects/freetype/files/freetype2/2.5.3/
21+
"""
922
version = '0.9.40'
1023
url = 'http://www.freedesktop.org/software/harfbuzz/release/harfbuzz-{version}.tar.bz2' # noqa
24+
depends = ['freetype']
1125

1226
def should_build(self, arch):
13-
if exists(join(self.get_build_dir(arch.arch),
14-
'src', '.libs', 'libharfbuzz.a')):
27+
if exists(
28+
join(
29+
self.get_build_dir(arch.arch), 'src', '.libs', 'libharfbuzz.so'
30+
)
31+
):
1532
return False
1633
return True
1734

1835
def build_arch(self, arch):
1936

2037
env = self.get_recipe_env(arch)
21-
env['LDFLAGS'] = env['LDFLAGS'] + ' -L{}'.format(
22-
self.ctx.get_libs_dir(arch.arch) +
23-
'-L{}'.format(self.ctx.libs_dir))
38+
env['LDFLAGS'] += ' -L{} -L{}'.format(
39+
self.ctx.get_libs_dir(arch.arch), self.ctx.libs_dir
40+
)
41+
42+
with_freetype = 'no'
43+
# freetype flags
44+
if 'freetype' in self.ctx.recipe_build_order:
45+
with_freetype = 'yes'
46+
freetype = self.get_recipe('freetype', self.ctx)
47+
freetype_install = join(
48+
freetype.get_build_dir(arch.arch), 'install'
49+
)
50+
env['CFLAGS'] = ' '.join(
51+
[
52+
env['CFLAGS'],
53+
'-I{}/include/freetype2'.format(freetype_install),
54+
'-L{}/lib'.format(freetype_install),
55+
'-lfreetype',
56+
]
57+
)
58+
# harfbuzz flags
59+
env['CFLAGS'] = ' '.join(
60+
[
61+
env['CFLAGS'],
62+
'-I{}'.format(self.get_build_dir(arch.arch)),
63+
'-I{}/src'.format(self.get_build_dir(arch.arch)),
64+
]
65+
)
66+
env['LDFLAGS'] += ' -L{}/src/.libs'.format(
67+
self.get_build_dir(arch.arch)
68+
)
69+
2470
with current_directory(self.get_build_dir(arch.arch)):
2571
configure = sh.Command('./configure')
26-
shprint(configure, '--without-icu', '--host=arm-linux=androideabi',
27-
'--prefix={}'.format(
28-
join(self.ctx.build_dir, 'python-install')),
29-
'--without-freetype',
30-
'--without-glib',
31-
'--disable-shared',
32-
_env=env)
33-
shprint(sh.make, '-j5', _env=env)
34-
35-
shprint(sh.cp, '-L', join('src', '.libs', 'libharfbuzz.a'),
36-
self.ctx.libs_dir)
72+
shprint(
73+
configure,
74+
'--without-icu',
75+
'--host={}'.format(arch.command_prefix),
76+
'--prefix={}'.format(self.get_build_dir(arch.arch)),
77+
'--with-freetype={}'.format(with_freetype),
78+
'--without-glib',
79+
_env=env,
80+
)
81+
shprint(sh.make, '-j', str(cpu_count()), _env=env)
82+
self.install_libs(arch, join('src', '.libs', 'libharfbuzz.so'))
83+
84+
if 'freetype' in self.ctx.recipe_build_order:
85+
# Rebuild freetype with harfbuzz support
86+
freetype.build_arch(arch, with_harfbuzz=True)
3787

3888

3989
recipe = HarfbuzzRecipe()

0 commit comments

Comments
 (0)