Skip to content

Commit

Permalink
recipes: add new polars recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
T-Dynamos committed May 22, 2024
1 parent 87a32be commit 6292498
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 10 deletions.
24 changes: 14 additions & 10 deletions pythonforandroid/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,8 +1204,8 @@ def get_wheel_platform_tag(self, arch):
"x86": "i686",
}[arch.arch]

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

built_wheels = []
with current_directory(build_dir):
shprint(
sh.Command(self.ctx.python_recipe.python_exe), *build_args, _env=env
)
built_wheels = [realpath(whl) for whl in glob.glob("dist/*.whl")]
self.install_wheel(arch, built_wheels)
self.install_wheel(arch, join(build_dir, "dist", "*.whl"))


class MesonRecipe(PyProjectRecipe):
Expand Down Expand Up @@ -1355,6 +1353,8 @@ class RustCompiledComponentsRecipe(PyProjectRecipe):
"x86_64": "x86_64-linux-android",
"x86": "i686-linux-android",
}
# Rust toolchain to be used for building
toolchain = "stable"

call_hostpython_via_targetpython = False

Expand All @@ -1367,6 +1367,7 @@ def get_recipe_env(self, arch, **kwargs):
build_target.upper().replace("-", "_")
)
env["CARGO_BUILD_TARGET"] = build_target
env["TARGET"] = build_target
env[cargo_linker_name] = join(
self.ctx.ndk.llvm_prebuilt_dir,
"bin",
Expand All @@ -1388,10 +1389,6 @@ def get_recipe_env(self, arch, **kwargs):
realpython_dir, "android-build", "build",
"lib.linux-*-{}/".format(self.python_major_minor_version),
))[0])

info_main("Ensuring rust build toolchain")
shprint(sh.rustup, "target", "add", build_target)

# Add host python to PATH
env["PATH"] = ("{hostpython_dir}:{old_path}").format(
hostpython_dir=Recipe.get_recipe(
Expand All @@ -1401,17 +1398,24 @@ def get_recipe_env(self, arch, **kwargs):
)
return env

def ensure_rust_toolchain(self, arch):
info_main("Ensuring rust build toolchain : {}".format(self.toolchain))
shprint(sh.rustup, "toolchain", "install", self.toolchain)
shprint(sh.rustup, "target", "add", "--toolchain", self.toolchain, self.RUST_ARCH_CODES[arch.arch])
shprint(sh.rustup, "default", self.toolchain)

def check_host_deps(self):
if not hasattr(sh, "rustup"):
error(
"`rustup` was not found on host system."
"\n`rustup` was not found on host system."
"Please install it using :"
"\n`curl https://sh.rustup.rs -sSf | sh`\n"
)
exit(1)

def build_arch(self, arch):
self.check_host_deps()
self.ensure_rust_toolchain(arch)
super().build_arch(arch)


Expand Down
65 changes: 65 additions & 0 deletions pythonforandroid/recipes/polars/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import time
from os.path import join
from glob import glob
from pythonforandroid.logger import warning
from pythonforandroid.recipe import RustCompiledComponentsRecipe

WARNING_MSG = """
This build requires at least 6GB of free RAM. If you cannot arrange free RAM, please consider adding some swap memory.
For Linux:
1. Open a terminal and execute the following commands to create an 8GB swap file:
sudo fallocate -l 8G /swapfile.swap
sudo chmod 700 /swapfile.swap
sudo mkswap /swapfile.swap
sudo swapon /swapfile.swap
2. To make the swap memory permanent, add it to your system's configuration by executing:
sudo sh -c 'echo "/swapfile.swap swap swap defaults 0 0" >> /etc/fstab'
Learn more about swap: https://en.wikipedia.org/wiki/Memory_paging
"""


class PolarsRecipe(RustCompiledComponentsRecipe):
version = "0.20.25"
url = "https://github.com/pola-rs/polars/releases/download/py-{version}/polars-{version}.tar.gz"
toolchain = "nightly-2024-04-15" # from rust-toolchain.toml
need_stl_shared = True

def get_recipe_env(self, arch, **kwargs):
env = super().get_recipe_env(arch, **kwargs)
# Required for libz-ng-sys
env["CMAKE_TOOLCHAIN_FILE"] = join(
self.ctx.ndk_dir, "build", "cmake", "android.toolchain.cmake")

# Enable SIMD instructions
simd_include = glob(join(
self.ctx.ndk.llvm_prebuilt_dir,
"lib64",
"clang",
"*",
"include"
))[0]
env["CFLAGS"] += " -D__ARM_FEATURE_SIMD32=1 -I{}".format(simd_include)

# Required for libgit2-sys
env["CFLAGS"] += " -I{}".format(self.ctx.ndk.sysroot_include_dir)
return env

def build_arch(self, arch):
warning(WARNING_MSG)
time.sleep(5) # let user read the warning

# Polars doesn't officially support 32-bit Python.
# See https://github.com/pola-rs/polars/issues/10460
if arch.arch in ["x86", "armeabi-v7a"]:
warning("Polars does not support architecture: {}".format(arch.arch))
return
else:
super().build_arch(arch)


recipe = PolarsRecipe()

0 comments on commit 6292498

Please sign in to comment.