Skip to content

Commit fe5ae5a

Browse files
committed
recipes: add thorvg
1 parent a9b902b commit fe5ae5a

7 files changed

Lines changed: 240 additions & 104 deletions

File tree

pythonforandroid/recipes/jpeg/__init__.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ class JpegRecipe(Recipe):
1414
name = 'jpeg'
1515
version = '2.0.1'
1616
url = 'https://github.com/libjpeg-turbo/libjpeg-turbo/archive/{version}.tar.gz' # noqa
17-
built_libraries = {'libjpeg.a': '.', 'libturbojpeg.a': '.'}
18-
# we will require this below patch to build the shared library
19-
# patches = ['remove-version.patch']
17+
built_libraries = {'libjpeg.so': '.', 'libturbojpeg.so': '.'}
2018

2119
def build_arch(self, arch):
2220
build_dir = self.get_build_dir(arch.arch)
@@ -34,6 +32,8 @@ def build_arch(self, arch):
3432
'-DCMAKE_ANDROID_ARCH_ABI={arch}'.format(arch=arch.arch),
3533
'-DCMAKE_ANDROID_NDK=' + self.ctx.ndk_dir,
3634
'-DCMAKE_C_COMPILER={cc}'.format(cc=arch.get_clang_exe()),
35+
f'-DCMAKE_ASM_COMPILER={arch.get_clang_exe()}',
36+
f'-DCMAKE_ASM_FLAGS=--target={arch.target}',
3737
'-DCMAKE_CXX_COMPILER={cc_plus}'.format(
3838
cc_plus=arch.get_clang_exe(plus_plus=True)),
3939
'-DCMAKE_BUILD_TYPE=Release',
@@ -42,12 +42,11 @@ def build_arch(self, arch):
4242

4343
'-DANDROID_ABI={arch}'.format(arch=arch.arch),
4444
'-DANDROID_ARM_NEON=ON',
45-
'-DENABLE_NEON=ON',
46-
# '-DREQUIRE_SIMD=1',
45+
'-DWITH_SIMD=1',
46+
'-DREQUIRE_SIMD=1',
4747

4848
# Force disable shared, with the static ones is enough
49-
'-DENABLE_SHARED=0',
50-
'-DENABLE_STATIC=1',
49+
'-DENABLE_SHARED=1',
5150

5251
# Fix cmake compatibility issue
5352
'-DCMAKE_POLICY_VERSION_MINIMUM=3.5',

pythonforandroid/recipes/jpeg/build-static.patch

Lines changed: 0 additions & 85 deletions
This file was deleted.

pythonforandroid/recipes/jpeg/remove-version.patch

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from pythonforandroid.recipe import Recipe, MesonRecipe
2+
from os.path import join
3+
from pythonforandroid.util import ensure_dir, current_directory
4+
from pythonforandroid.logger import shprint
5+
from multiprocessing import cpu_count
6+
from glob import glob
7+
import sh
8+
9+
10+
class LibThorVGRecipe(MesonRecipe):
11+
name = "libthorvg"
12+
version = "1.0.1"
13+
url = "https://github.com/thorvg/thorvg/releases/download/v{version}/thorvg.tar.gz"
14+
config_otps = [
15+
"-Dsimd=true",
16+
"-Dbindings=capi",
17+
"-Dtools=all",
18+
"-Dengines=sw,gl",
19+
"-Dloaders=svg,png,jpg,ttf,webp",
20+
"-Dextra=opengl_es,lottie_exp,openmp",
21+
"--reconfigure",
22+
]
23+
need_stl_shared = True
24+
skip_python = True
25+
depends = ["png", "libwebp", "jpeg"]
26+
patches = ["meson.patch"]
27+
bins = ["tvg-lottie2gif", "tvg-svg2png"]
28+
built_libraries = {
29+
"libthorvg-1.so": "install/lib",
30+
"libomp.so": "install/lib"
31+
}
32+
for bin in bins:
33+
built_libraries[f"lib{bin}bin.so"] = "install/bin"
34+
35+
def should_build(self, arch):
36+
return Recipe.should_build(self, arch)
37+
38+
def get_include_dir(self, arch):
39+
return join(self.get_build_dir(arch.arch), "install", "include")
40+
41+
def build_arch(self, arch):
42+
super().build_arch(arch)
43+
build_dir = self.get_build_dir(arch.arch)
44+
install_dir = join(build_dir, "install")
45+
ensure_dir(install_dir)
46+
env = self.get_recipe_env(arch)
47+
48+
lib_dir = self.ctx.get_libs_dir(arch.arch)
49+
png_include = self.get_recipe("png", self.ctx).get_build_dir(arch.arch)
50+
webp_include = join(
51+
self.get_recipe("libwebp", self.ctx).get_build_dir(arch.arch), "src"
52+
)
53+
jpg_dir = self.get_recipe("jpeg", self.ctx).get_build_dir(arch.arch)
54+
55+
with current_directory(build_dir):
56+
57+
shprint(
58+
sh.meson,
59+
"setup",
60+
"builddir",
61+
"--cross-file",
62+
join("/tmp", "android.meson.cross"),
63+
f"--prefix={install_dir}",
64+
# config opts
65+
*self.config_otps,
66+
# deps
67+
f"-Dpng_include_dir={png_include}",
68+
f"-Dpng_lib_dir={lib_dir}",
69+
f"-Dwebp_include_dir={webp_include}",
70+
f"-Dwebp_lib_dir={lib_dir}",
71+
f"-Djpg_include_dir={jpg_dir}",
72+
f"-Djpg_lib_dir={jpg_dir}",
73+
_env=env,
74+
)
75+
76+
shprint(sh.ninja, "-C", "builddir", "-j", str(cpu_count()), _env=env)
77+
shprint(sh.rm, "-rf", install_dir)
78+
shprint(sh.mkdir, install_dir)
79+
shprint(sh.ninja, "-C", "builddir", "install", _env=env)
80+
81+
# copy libomp.so
82+
arch_map = {
83+
"arm64-v8a": "aarch64",
84+
"armeabi-v7a": "arm",
85+
"x86": "i386",
86+
"x86_64": "x86_64",
87+
}
88+
lib_arch = arch_map[arch.arch]
89+
# clang version directory is variable, so glob it
90+
clang_lib_dir = glob(
91+
join(self.ctx.ndk.llvm_prebuilt_dir, "lib64/clang/*/lib/linux", lib_arch)
92+
)[0]
93+
libomp = join(clang_lib_dir, "libomp.so")
94+
shprint(sh.cp, libomp, join("install", "lib"))
95+
96+
# setup bins
97+
bin_dir = join("install", "bin")
98+
for bin in self.bins:
99+
shprint(sh.cp, join(bin_dir, bin), join(bin_dir, f"lib{bin}bin.so"))
100+
101+
102+
recipe = LibThorVGRecipe()
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
diff '--color=auto' -uNr thorvg/meson_options.txt thorvg.mod/meson_options.txt
2+
--- thorvg/meson_options.txt 2026-02-12 21:02:13.000000000 +0530
3+
+++ thorvg.mod/meson_options.txt 2026-03-06 11:11:43.111722092 +0530
4+
@@ -68,3 +68,11 @@
5+
choices: ['', 'opengl_es', 'lottie_exp', 'openmp'],
6+
value: ['lottie_exp', 'openmp'],
7+
description: 'Enable support for extra options')
8+
+
9+
+
10+
+option('png_include_dir', type: 'string', value: '', description: 'Path to PNG headers')
11+
+option('png_lib_dir', type: 'string', value: '', description: 'Path to PNG library')
12+
+option('webp_include_dir', type: 'string', value: '', description: 'Path to WEBP headers')
13+
+option('webp_lib_dir', type: 'string', value: '', description: 'Path to WEBP library')
14+
+option('jpg_include_dir', type: 'string', value: '', description: 'Path to JPG headers')
15+
+option('jpg_lib_dir', type: 'string', value: '', description: 'Path to JPG library')
16+
diff '--color=auto' -uNr thorvg/src/loaders/external_jpg/meson.build thorvg.mod/src/loaders/external_jpg/meson.build
17+
--- thorvg/src/loaders/external_jpg/meson.build 2026-02-12 21:02:13.000000000 +0530
18+
+++ thorvg.mod/src/loaders/external_jpg/meson.build 2026-03-06 11:16:26.360504907 +0530
19+
@@ -3,11 +3,12 @@
20+
'tvgJpgLoader.cpp',
21+
]
22+
23+
-jpg_dep = dependency('libturbojpeg', required: false)
24+
-
25+
-if not jpg_dep.found()
26+
- jpg_dep = cc.find_library('turbojpeg', required: false)
27+
-endif
28+
+jpg_inc = include_directories(get_option('jpg_include_dir'))
29+
+jpg_lib = cc.find_library('turbojpeg', dirs: [get_option('jpg_lib_dir')], required: true)
30+
+jpg_dep = declare_dependency(
31+
+ include_directories: jpg_inc,
32+
+ dependencies: [jpg_lib]
33+
+)
34+
35+
if jpg_dep.found()
36+
subloader_dep += [declare_dependency(
37+
diff '--color=auto' -uNr thorvg/src/loaders/external_png/meson.build thorvg.mod/src/loaders/external_png/meson.build
38+
--- thorvg/src/loaders/external_png/meson.build 2026-02-12 21:02:13.000000000 +0530
39+
+++ thorvg.mod/src/loaders/external_png/meson.build 2026-03-06 10:59:38.966203768 +0530
40+
@@ -3,7 +3,12 @@
41+
'tvgPngLoader.cpp',
42+
]
43+
44+
-png_dep = dependency('libpng', required: false)
45+
+png_inc = include_directories(get_option('png_include_dir'))
46+
+png_lib = cc.find_library('png16', dirs: [get_option('png_lib_dir')], required: true)
47+
+png_dep = declare_dependency(
48+
+ include_directories: png_inc,
49+
+ dependencies: [png_lib]
50+
+)
51+
52+
if png_dep.found()
53+
subloader_dep += [declare_dependency(
54+
diff '--color=auto' -uNr thorvg/src/loaders/external_webp/meson.build thorvg.mod/src/loaders/external_webp/meson.build
55+
--- thorvg/src/loaders/external_webp/meson.build 2026-02-12 21:02:13.000000000 +0530
56+
+++ thorvg.mod/src/loaders/external_webp/meson.build 2026-03-06 11:03:55.626148453 +0530
57+
@@ -3,7 +3,12 @@
58+
'tvgWebpLoader.cpp',
59+
]
60+
61+
-webp_dep = dependency('libwebp', required: false)
62+
+webp_inc = include_directories(get_option('webp_include_dir'))
63+
+webp_lib = cc.find_library('webp', dirs: [get_option('webp_lib_dir')], required: true)
64+
+webp_dep = declare_dependency(
65+
+ include_directories: webp_inc,
66+
+ dependencies: [webp_lib]
67+
+)
68+
69+
if webp_dep.found()
70+
subloader_dep += [declare_dependency(
71+
@@ -11,4 +16,4 @@
72+
dependencies : webp_dep,
73+
sources : source_file
74+
)]
75+
-endif
76+
\ No newline at end of file
77+
+endif
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from pythonforandroid.recipe import PyProjectRecipe
2+
3+
4+
class ThorVGPythonRecipe(PyProjectRecipe):
5+
site_packages_name = "thorvg_python"
6+
version = "1.1.1"
7+
url = "https://github.com/laggykiller/thorvg-python/archive/refs/tags/v{version}.tar.gz"
8+
depends = ["libthorvg"]
9+
patches = ["sharedlib.patch"]
10+
11+
12+
recipe = ThorVGPythonRecipe()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
diff '--color=auto' -uNr thorvg-python-1.1.1/pyproject.toml thorvg-python-1.1.1.mod/pyproject.toml
2+
--- thorvg-python-1.1.1/pyproject.toml 2026-02-28 01:45:23.000000000 +0530
3+
+++ thorvg-python-1.1.1.mod/pyproject.toml 2026-03-06 13:36:02.822762758 +0530
4+
@@ -34,7 +34,6 @@
5+
6+
[build-system]
7+
requires = [
8+
- "conan>=2.0",
9+
"setuptools>=45"
10+
]
11+
build-backend = "setuptools.build_meta"
12+
@@ -45,4 +44,4 @@
13+
14+
[tool.mypy]
15+
python_version = "3.9"
16+
-files = ["src", "tests"]
17+
\ No newline at end of file
18+
+files = ["src", "tests"]
19+
diff '--color=auto' -uNr thorvg-python-1.1.1/setup.py thorvg-python-1.1.1.mod/setup.py
20+
--- thorvg-python-1.1.1/setup.py 2026-02-28 01:45:23.000000000 +0530
21+
+++ thorvg-python-1.1.1.mod/setup.py 2026-03-06 13:32:56.878441668 +0530
22+
@@ -213,10 +213,4 @@
23+
packages=find_packages(where="src"),
24+
package_dir={"": "src"},
25+
include_package_data=True,
26+
- package_data={
27+
- "thorvg_python": ["*.dll", "*.dylib", "*.so"],
28+
- },
29+
- cmdclass={
30+
- "build_py": build_py_custom,
31+
- },
32+
)
33+
diff '--color=auto' -uNr thorvg-python-1.1.1/src/thorvg_python/engine.py thorvg-python-1.1.1.mod/src/thorvg_python/engine.py
34+
--- thorvg-python-1.1.1/src/thorvg_python/engine.py 2026-02-28 01:45:23.000000000 +0530
35+
+++ thorvg-python-1.1.1.mod/src/thorvg_python/engine.py 2026-03-06 16:42:11.625316343 +0530
36+
@@ -14,6 +14,7 @@
37+
) -> Optional[ctypes.CDLL]:
38+
package_dir = os.path.dirname(__file__)
39+
thorvg_lib_name = lib_prefix + "thorvg" + lib_suffix
40+
+ return ctypes.cdll.LoadLibrary(thorvg_lib_name)
41+
thorvg_lib_path_local = os.path.join(package_dir, thorvg_lib_name)
42+
43+
if os.path.isfile(thorvg_lib_path_local):

0 commit comments

Comments
 (0)