Files
termux-packages/packages/python-llvmlite/build.sh
termux-pacman-bot f38dd0ee93 tree-wide: port debpython to termux
- debpython is the commands `py3compile` and `py3clean` from Debian. I
  am calling them that because a large chunk of their source code is
  found inside a folder inside Debian's source code named "debpython"
  - 5348f70466

- rather than packaging `.pyc` files into packages, `py3compile` and
  `py3clean` can be called from `postinst` and `prerm` scripts to
  generate all `.pyc` for the `.py` files in the package immediately
  after the package is installed, and remove all `.pyc` files immediately
  before uninstalling the package, respectively

- fixes the error `trying to overwrite '/data/data/com.termux/files/usr/lib/python3.12/__pycache__/cProfile.cpython-312.pyc'` when packages were built on-device, but at the same time, also:

- prevents the warnings `dpkg: warning: while removing python, directory '/data/data/com.termux/files/usr/lib/python3.12/site-packages' not empty so not removed` as long as no packages were installed using `pip`

- The `termux_step_create_python_debscripts.sh` can configure work on debpython (i.e. its `py3copile` and `py3clean` commands) from the glibc package `python-glibc`, if some glibc package is being compiled.

- New variables have been implemented:
- `TERMUX_PYTHON_CROSSENV_BUILDHOME` - location of crossenv's python build libraries.
- `TERMUX_PKG_PYTHON_RUNTIME_DEPS` - configures the installation of the python modules via pip3 in the pkg's debscripts. If not configured in the package, it will use the value from `TERMUX_PKG_PYTHON_TARGET_DEPS`. If the variable is set to `false`, then the customization of installing python modules will be disabled, even if the `TERMUX_PKG_PYTHON_TARGET_DEPS` variable is set in the package.
- `TERMUX_SUBPKG_PYTHON_RUNTIME_DEPS` - configures the installation of the python modules via pip3 in the subpkg's debscripts.

- Implemented reconfiguration of prefixes in python module `sysconfig` and setting in `TERMUX_PYTHON_CROSSENV_BUILDHOME`, so that python modules from crossenv building can specify system paths of termux for correct compilation.

- Added automatic addition of `python-glibc{-glibc}` dependency when using the `TERMUX_PKG_PYTHON_RUNTIME_DEPS` (for pkg; will be disabled, i.e. will not be added, if the variable is set to `false`) or `TERMUX_SUBPKG_PYTHON_RUNTIME_DEPS` (for subpkg) value.

> How to add a new Python package after this?

Everything is the same, except, now, this block is no longer necessary in `build.sh`.

```bash
termux_step_create_debscripts() {
	cat <<- EOF > ./postinst
	#!$TERMUX_PREFIX/bin/sh
	echo "Installing dependencies through pip..."
	pip3 install ${TERMUX_PKG_PYTHON_TARGET_DEPS//, / }
	EOF
}
```

- Instead, `scripts/build/termux_step_create_python_debscripts.sh` can now detect the presence of `pip` package lists in `$TERMUX_PKG_PYTHON_TARGET_DEPS`, `$TERMUX_SUBPKG_PYTHON_TARGET_DEPS`, and the `METADATA` file of the Python package if it exists, and automatically insert them as a block into the `postinst` script for all relevant packages.
- `$TERMUX_PKG_PYTHON_TARGET_DEPS` is used for `pip` dependencies that are both on-device build-time and on-device run-time dependencies, and `$TERMUX_PKG_PYTHON_RUNTIME_DEPS` is used for runtime-only `pip` dependencies. `$TERMUX_PKG_PYTHON_RUNTIME_DEPS` overrides `$TERMUX_PKG_PYTHON_TARGET_DEPS` for runtime dependencies,
  - i.e. if `TERMUX_PKG_PYTHON_RUNTIME_DEPS` is not specified, but `TERMUX_PKG_PYTHON_TARGET_DEPS` is, then `TERMUX_PKG_PYTHON_TARGET_DEPS` will be used as both on-device build and on-device runtime dependencies,
  - but if `TERMUX_PKG_PYTHON_RUNTIME_DEPS` is specified, then `TERMUX_PKG_PYTHON_TARGET_DEPS`, if specified, is used only for on-device build-time dependencies.
- If `python-pip` is not already in the dependencies of any package that needs it, the build will fail with an error instructing maintainers to add `python-pip` to the dependencies of the package that needs it.

Co-authored-by: Maxython <mixython@gmail.com>
2026-01-18 09:39:03 +00:00

187 lines
5.6 KiB
Bash

TERMUX_PKG_HOMEPAGE=https://llvmlite.pydata.org/
TERMUX_PKG_DESCRIPTION="A lightweight LLVM python binding for writing JIT compilers"
# LICENSES: BSD 2-Clause, Apache-2.0 with LLVM Exceptions
TERMUX_PKG_LICENSE="BSD 2-Clause, Apache-2.0"
TERMUX_PKG_LICENSE_FILE="LICENSE, LICENSE.thirdparty"
TERMUX_PKG_MAINTAINER="@termux"
TERMUX_PKG_VERSION=(
"0.46.0"
"20.1.8"
)
TERMUX_PKG_REVISION=1
TERMUX_PKG_SRCURL=(
"https://github.com/numba/llvmlite/archive/refs/tags/v${TERMUX_PKG_VERSION[0]}.tar.gz"
"https://github.com/llvm/llvm-project/releases/download/llvmorg-${TERMUX_PKG_VERSION[1]}/llvm-project-${TERMUX_PKG_VERSION[1]}.src.tar.xz"
)
TERMUX_PKG_SHA256=(
34733887fd325a7392eef69b15879d37b02694e1e6b01ba11ed67c3b251290c5
6898f963c8e938981e6c4a302e83ec5beb4630147c7311183cf61069af16333d
)
TERMUX_PKG_DEPENDS="libc++, libffi, python, python-pip"
TERMUX_PKG_PYTHON_COMMON_DEPS="wheel"
TERMUX_PKG_BUILD_IN_SRC=true
TERMUX_PKG_AUTO_UPDATE=true
TERMUX_PKG_HOSTBUILD=true
# See http://llvm.org/docs/CMake.html:
TERMUX_PKG_EXTRA_CONFIGURE_ARGS="
-DANDROID_PLATFORM_LEVEL=$TERMUX_PKG_API_LEVEL
-DPYTHON_EXECUTABLE=$(command -v python3)
-DLLVM_ENABLE_PIC=ON
-DLLVM_INCLUDE_TESTS=OFF
-DDEFAULT_SYSROOT=$(dirname $TERMUX_PREFIX)
-DLLVM_NATIVE_TOOL_DIR=$TERMUX_PKG_HOSTBUILD_DIR/bin
-DCROSS_TOOLCHAIN_FLAGS_LLVM_NATIVE=-DLLVM_NATIVE_TOOL_DIR=$TERMUX_PKG_HOSTBUILD_DIR/bin
-DLIBOMP_ENABLE_SHARED=FALSE
-DLLVM_ENABLE_SPHINX=ON
-DSPHINX_OUTPUT_MAN=ON
-DSPHINX_WARNINGS_AS_ERRORS=OFF
-DLLVM_TARGETS_TO_BUILD=all
-DPERL_EXECUTABLE=$(command -v perl)
-DLLVM_ENABLE_ZSTD=OFF
-DLLVM_ENABLE_LIBEDIT=OFF
-DLLVM_ENABLE_LIBXML2=OFF
-DLLVM_ENABLE_RTTI=OFF
-DLLVM_ENABLE_TERMINFO=OFF
-DLLVM_INCLUDE_BENCHMARKS=OFF
-DLLVM_INCLUDE_DOCS=OFF
-DLLVM_INCLUDE_EXAMPLES=OFF
-DLLVM_INCLUDE_GO_TESTS=OFF
-DLLVM_INCLUDE_UTILS=ON
-DLLVM_INSTALL_UTILS=ON
-DLLVM_BUILD_LLVM_DYLIB=OFF
-DLLVM_LINK_LLVM_DYLIB=OFF
-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly
-DLLVM_ENABLE_FFI=ON
-DLLVM_ENABLE_Z3_SOLVER=OFF
-DLLVM_OPTIMIZED_TABLEGEN=ON
"
if [ $TERMUX_ARCH_BITS = 32 ]; then
# Do not set _FILE_OFFSET_BITS=64
TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" -DLLVM_FORCE_SMALLFILE_FOR_ANDROID=on"
fi
termux_step_post_get_source() {
mv llvm-project-"${TERMUX_PKG_VERSION[1]}".src llvm-project
}
termux_step_host_build() {
termux_setup_cmake
termux_setup_ninja
cmake -G Ninja "-DCMAKE_BUILD_TYPE=Release" \
"-DLLVM_ENABLE_PROJECTS=clang" \
$TERMUX_PKG_SRCDIR/llvm-project/llvm
ninja -j $TERMUX_PKG_MAKE_PROCESSES llvm-tblgen clang-tblgen
}
__llvmlite_build_llvm() {
export _LLVMLITE_LLVM_INSTALL_DIR="$TERMUX_PKG_BUILDDIR"/llvm-install
if [ -f "$_LLVMLITE_LLVM_INSTALL_DIR"/.llvmlite-llvm-built ]; then
return
fi
termux_setup_cmake
termux_setup_ninja
# Add unknown vendor, otherwise it screws with the default LLVM triple
# detection.
export LLVM_DEFAULT_TARGET_TRIPLE=${CCTERMUX_HOST_PLATFORM/-/-unknown-}
export LLVM_TARGET_ARCH
if [ $TERMUX_ARCH = "arm" ]; then
LLVM_TARGET_ARCH=ARM
elif [ $TERMUX_ARCH = "aarch64" ]; then
LLVM_TARGET_ARCH=AArch64
elif [ $TERMUX_ARCH = "i686" ] || [ $TERMUX_ARCH = "x86_64" ]; then
LLVM_TARGET_ARCH=X86
else
termux_error_exit "Invalid arch: $TERMUX_ARCH"
fi
TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" -DLLVM_TARGET_ARCH=$LLVM_TARGET_ARCH"
TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" -DLLVM_HOST_TRIPLE=$LLVM_DEFAULT_TARGET_TRIPLE"
TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" -DCMAKE_INSTALL_PREFIX=$_LLVMLITE_LLVM_INSTALL_DIR"
TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" -DCMAKE_INSTALL_INCLUDEDIR=$_LLVMLITE_LLVM_INSTALL_DIR/include"
TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" -DCMAKE_INSTALL_LIBDIR=$_LLVMLITE_LLVM_INSTALL_DIR/lib"
# Backup dirs and envs
local __old_ldflags="$LDFLAGS"
local __old_srcdir="$TERMUX_PKG_SRCDIR"
local __old_builddir="$TERMUX_PKG_BUILDDIR"
LDFLAGS="-Wl,--undefined-version $LDFLAGS"
TERMUX_PKG_SRCDIR="$TERMUX_PKG_SRCDIR"/llvm-project/llvm
TERMUX_PKG_BUILDDIR="$TERMUX_PKG_BUILDDIR"/llvm-build
# Configure
mkdir -p "$TERMUX_PKG_BUILDDIR"
cd "$TERMUX_PKG_BUILDDIR"
termux_step_configure_cmake
# Cross-compile & install LLVM
cd "$TERMUX_PKG_BUILDDIR"
ninja -j $TERMUX_PKG_MAKE_PROCESSES install
# Recover dirs and envs
LDFLAGS="$__old_ldflags"
TERMUX_PKG_SRCDIR="$__old_srcdir"
TERMUX_PKG_BUILDDIR="$__old_builddir"
# Mark as built
mkdir -p "$_LLVMLITE_LLVM_INSTALL_DIR"
touch -f "$_LLVMLITE_LLVM_INSTALL_DIR"/.llvmlite-llvm-built
}
__llvmlite_build_lib() {
termux_setup_cmake
termux_setup_ninja
TERMUX_PKG_EXTRA_CONFIGURE_ARGS="-DLLVM_DIR=$_LLVMLITE_LLVM_INSTALL_DIR/lib/cmake/llvm"
TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" -DCMAKE_POLICY_VERSION_MINIMUM=3.5"
# Backup dirs and envs
local __old_srcdir="$TERMUX_PKG_SRCDIR"
local __old_builddir="$TERMUX_PKG_BUILDDIR"
TERMUX_PKG_SRCDIR="$TERMUX_PKG_SRCDIR"/ffi
TERMUX_PKG_BUILDDIR="$TERMUX_PKG_SRCDIR"/build
# Configure
mkdir -p "$TERMUX_PKG_BUILDDIR"
cd "$TERMUX_PKG_BUILDDIR"
termux_step_configure_cmake
# Cross-compile llvmlite
cd "$TERMUX_PKG_BUILDDIR"
ninja -j $TERMUX_PKG_MAKE_PROCESSES
# Recover dirs and envs
TERMUX_PKG_SRCDIR="$__old_srcdir"
TERMUX_PKG_BUILDDIR="$__old_builddir"
}
termux_step_configure() {
:
}
termux_step_make() {
__llvmlite_build_llvm
__llvmlite_build_lib
# Copy libs
cp -f "$TERMUX_PKG_SRCDIR"/ffi/build/libllvmlite.so "$TERMUX_PKG_SRCDIR"/llvmlite/binding/
}
termux_step_make_install() {
LDFLAGS+=" -Wl,--no-as-needed -lpython${TERMUX_PYTHON_VERSION}"
export LLVMLITE_SKIP_BUILD_LIBRARY=1
pip install . --prefix="$TERMUX_PREFIX" -vv --no-build-isolation --no-deps
}
termux_step_post_massage() {
local dir="include"
if [[ -d "${TERMUX_PKG_MASSAGEDIR}${TERMUX_PREFIX}/$dir" ]]; then
termux_error_exit "$dir should not exist in $TERMUX_PKG_NAME!"
fi
}