Skip to content

Commit 9d609e2

Browse files
committed
recipes: add new polars recipe
1 parent 87a32be commit 9d609e2

File tree

2 files changed

+82
-10
lines changed

2 files changed

+82
-10
lines changed

pythonforandroid/recipe.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -1204,8 +1204,8 @@ def get_wheel_platform_tag(self, arch):
12041204
"x86": "i686",
12051205
}[arch.arch]
12061206

1207-
def install_wheel(self, arch, built_wheels):
1208-
_wheel = built_wheels[0]
1207+
def install_wheel(self, arch, pattern):
1208+
_wheel = [realpath(whl) for whl in glob.glob(pattern)][0]
12091209
built_wheel_dir = dirname(_wheel)
12101210
# Fix wheel platform tag
12111211
wheel_tag = wheel_tags(
@@ -1247,13 +1247,11 @@ def build_arch(self, arch):
12471247
"builddir={}".format(sub_build_dir),
12481248
] + self.extra_build_args
12491249

1250-
built_wheels = []
12511250
with current_directory(build_dir):
12521251
shprint(
12531252
sh.Command(self.ctx.python_recipe.python_exe), *build_args, _env=env
12541253
)
1255-
built_wheels = [realpath(whl) for whl in glob.glob("dist/*.whl")]
1256-
self.install_wheel(arch, built_wheels)
1254+
self.install_wheel(arch, join(build_dir, "dist", "*.whl"))
12571255

12581256

12591257
class MesonRecipe(PyProjectRecipe):
@@ -1355,6 +1353,8 @@ class RustCompiledComponentsRecipe(PyProjectRecipe):
13551353
"x86_64": "x86_64-linux-android",
13561354
"x86": "i686-linux-android",
13571355
}
1356+
# Rust toolchain to be used for building
1357+
toolchain = "stable"
13581358

13591359
call_hostpython_via_targetpython = False
13601360

@@ -1367,6 +1367,7 @@ def get_recipe_env(self, arch, **kwargs):
13671367
build_target.upper().replace("-", "_")
13681368
)
13691369
env["CARGO_BUILD_TARGET"] = build_target
1370+
env["TARGET"] = build_target
13701371
env[cargo_linker_name] = join(
13711372
self.ctx.ndk.llvm_prebuilt_dir,
13721373
"bin",
@@ -1388,10 +1389,6 @@ def get_recipe_env(self, arch, **kwargs):
13881389
realpython_dir, "android-build", "build",
13891390
"lib.linux-*-{}/".format(self.python_major_minor_version),
13901391
))[0])
1391-
1392-
info_main("Ensuring rust build toolchain")
1393-
shprint(sh.rustup, "target", "add", build_target)
1394-
13951392
# Add host python to PATH
13961393
env["PATH"] = ("{hostpython_dir}:{old_path}").format(
13971394
hostpython_dir=Recipe.get_recipe(
@@ -1401,17 +1398,24 @@ def get_recipe_env(self, arch, **kwargs):
14011398
)
14021399
return env
14031400

1401+
def ensure_rust_toolchain(self, arch):
1402+
info_main("Ensuring rust build toolchain : {}".format(self.toolchain))
1403+
shprint(sh.rustup, "toolchain", "install", self.toolchain)
1404+
shprint(sh.rustup, "target", "add", "--toolchain", self.toolchain, self.RUST_ARCH_CODES[arch.arch])
1405+
shprint(sh.rustup, "default", self.toolchain)
1406+
14041407
def check_host_deps(self):
14051408
if not hasattr(sh, "rustup"):
14061409
error(
1407-
"`rustup` was not found on host system."
1410+
"\n`rustup` was not found on host system."
14081411
"Please install it using :"
14091412
"\n`curl https://sh.rustup.rs -sSf | sh`\n"
14101413
)
14111414
exit(1)
14121415

14131416
def build_arch(self, arch):
14141417
self.check_host_deps()
1418+
self.ensure_rust_toolchain(arch)
14151419
super().build_arch(arch)
14161420

14171421

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import time
2+
from os.path import join
3+
from glob import glob
4+
from pythonforandroid.logger import warning
5+
from pythonforandroid.recipe import RustCompiledComponentsRecipe
6+
7+
WARNING_MSG = """
8+
9+
This build requires at least 6GB of free RAM. If you cannot arrange free RAM, please consider adding some swap memory.
10+
11+
For Linux:
12+
13+
1. Open a terminal and execute the following commands to create an 8GB swap file:
14+
sudo fallocate -l 8G /swapfile.swap
15+
sudo chmod 700 /swapfile.swap
16+
sudo mkswap /swapfile.swap
17+
sudo swapon /swapfile.swap
18+
19+
2. To make the swap memory permanent, add it to your system's configuration by executing:
20+
sudo sh -c 'echo "/swapfile.swap swap swap defaults 0 0" >> /etc/fstab'
21+
22+
Learn more about swap: https://en.wikipedia.org/wiki/Memory_paging
23+
"""
24+
25+
26+
class PolarsRecipe(RustCompiledComponentsRecipe):
27+
version = "0.20.25"
28+
url = "https://github.com/pola-rs/polars/releases/download/py-{version}/polars-{version}.tar.gz"
29+
toolchain = "nightly-2024-04-15" # from rust-toolchain.toml
30+
need_stl_shared = True
31+
32+
def get_recipe_env(self, arch, **kwargs):
33+
env = super().get_recipe_env(arch, **kwargs)
34+
# Required for libz-ng-sys
35+
env["CMAKE_TOOLCHAIN_FILE"] = join(
36+
self.ctx.ndk_dir, "build", "cmake", "android.toolchain.cmake")
37+
38+
# Enable SIMD instructions
39+
simd_include = glob(join(
40+
self.ctx.ndk.llvm_prebuilt_dir,
41+
"lib64",
42+
"clang",
43+
"*",
44+
"include"
45+
))[0]
46+
env["CFLAGS"] += " -D__ARM_FEATURE_SIMD32=1 -I{}".format(simd_include)
47+
48+
# Required for libgit2-sys
49+
env["CFLAGS"] += " -I{}".format(self.ctx.ndk.sysroot_include_dir)
50+
51+
# We don't want rust cc to set flags for us
52+
env["CRATE_CC_NO_DEFAULTS"] = "1"
53+
return env
54+
55+
def build_arch(self, arch):
56+
warning(WARNING_MSG)
57+
time.sleep(5) # let user read the warning
58+
59+
# Polars doesn't officially support 32-bit Python.
60+
# See https://github.com/pola-rs/polars/issues/10460
61+
if arch.arch in ["x86", "armeabi-v7a"]:
62+
warning("Polars does not support architecture: {}".format(arch.arch))
63+
return
64+
else:
65+
super().build_arch(arch)
66+
67+
68+
recipe = PolarsRecipe()

0 commit comments

Comments
 (0)