Build Python #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build Python | |
on: | |
workflow_dispatch: | |
jobs: | |
build_python: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
strategy: | |
matrix: | |
platform: [arm64, amd64] | |
PYTHON_VERSION: ["3.11.8"] | |
steps: | |
- name: Set Build Architecture | |
run: | | |
if [ "${{ matrix.platform }}" = "arm64" ]; then | |
echo "arch_name=aarch64" >> $GITHUB_ENV | |
elif [ "${{ matrix.platform }}" = "amd64" ]; then | |
echo "arch_name=x86_64" >> $GITHUB_ENV | |
else | |
exit 1 | |
fi | |
- name: Build Python | |
run: | | |
PYTHON_VERSION="${{ matrix.PYTHON_VERSION }}" | |
PYTHON_MAJOR="3" | |
# Support for ARM64 | |
if [[ "${{ matrix.platform }}" == "arm64" ]]; then | |
docker run --rm --privileged aptman/qus -s -- -p aarch64 | |
fi | |
# Patches for supporting multiprocessing python module in Android environment | |
# https://github.com/termux/termux-packages/pull/8990 | |
PYTHON_PATCH_1=' | |
--- Python-'${PYTHON_VERSION}'/Lib/multiprocessing/heap.py 2022-02-07 11:51:46.427116300 +0800 | |
+++ Python-'${PYTHON_VERSION}'.mod/Lib/multiprocessing/heap.py 2022-02-07 11:52:48.432577700 +0800 | |
@@ -70,7 +70,7 @@ | |
""" | |
if sys.platform == '\''linux'\'': | |
- _dir_candidates = ['\''/dev/shm'\''] | |
+ _dir_candidates = [] | |
else: | |
_dir_candidates = [] | |
--- Python-'${PYTHON_VERSION}'/Modules/_multiprocessing/multiprocessing.c 2021-12-07 02:23:39.000000000 +0800 | |
+++ Python-'${PYTHON_VERSION}'.mod/Modules/_multiprocessing/multiprocessing.c 2022-02-10 03:05:11.249248300 +0800 | |
@@ -172,7 +172,7 @@ | |
_MULTIPROCESSING_RECV_METHODDEF | |
_MULTIPROCESSING_SEND_METHODDEF | |
#endif | |
-#if !defined(POSIX_SEMAPHORES_NOT_ENABLED) && !defined(__ANDROID__) | |
+#if !defined(POSIX_SEMAPHORES_NOT_ENABLED) | |
_MULTIPROCESSING_SEM_UNLINK_METHODDEF | |
#endif | |
{NULL} | |
--- Python-'${PYTHON_VERSION}'/Modules/_multiprocessing/posixshmem.c 2021-12-07 02:23:39.000000000 +0800 | |
+++ Python-'${PYTHON_VERSION}'.mod/Modules/_multiprocessing/posixshmem.c 2022-02-10 01:37:03.547649100 +0800 | |
@@ -11,6 +11,9 @@ | |
#include <sys/mman.h> | |
#endif | |
+int shm_open(const char *, int, mode_t); | |
+int shm_unlink(const char *); | |
+ | |
/*[clinic input] | |
module _posixshmem | |
[clinic start generated code]*/ | |
--- Python-'${PYTHON_VERSION}'/Modules/_multiprocessing/posix-shm-extension.c 1970-01-01 08:00:00.000000000 +0800 | |
+++ Python-'${PYTHON_VERSION}'.mod/Modules/_multiprocessing/posix-shm-extension.c 2022-02-12 13:25:30.306949200 +0800 | |
@@ -0,0 +1,77 @@ | |
+/* This file is a port of posix shared memory for Python3 on Termux Android, | |
+ based on musl-libc which is licensed under the following standard MIT | |
+ license. The ported files are listed as following. | |
+ | |
+ File(s): src/mman/shm_open.c | |
+ | |
+ Copyright © 2005-2020 Rich Felker, et al. | |
+ | |
+ Permission is hereby granted, free of charge, to any person obtaining | |
+ a copy of this software and associated documentation files (the | |
+ "Software"), to deal in the Software without restriction, including | |
+ without limitation the rights to use, copy, modify, merge, publish, | |
+ distribute, sublicense, and/or sell copies of the Software, and to | |
+ permit persons to whom the Software is furnished to do so, subject to | |
+ the following conditions: | |
+ | |
+ The above copyright notice and this permission notice shall be | |
+ included in all copies or substantial portions of the Software. | |
+ | |
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
+ */ | |
+ | |
+#define _DEFAULT_SOURCE | |
+#include <fcntl.h> // open() | |
+#include <string.h> // strlen(), memcpy() | |
+#include <errno.h> // errno | |
+#include <limits.h> // NAME_MAX | |
+#include <unistd.h> // unlink() | |
+ | |
+#define SHM_PREFIX "/tmp/shm." | |
+ | |
+static __inline__ char *__strchrnul(const char *s, int c) | |
+{ | |
+ c = (unsigned char)c; | |
+ if (!c) return (char *)s + strlen(s); | |
+ for (; *s && *(unsigned char *)s != c; s++); | |
+ return (char *)s; | |
+} | |
+ | |
+static char *__shm_mapname(const char *name, char *buf) | |
+{ | |
+ char *p; | |
+ while (*name == '\''/'\'') name++; | |
+ if (*(p = __strchrnul(name, '\''/'\'')) || p==name || | |
+ (p-name <= 2 && name[0]=='\''.'\'' && p[-1]=='\''.'\'')) { | |
+ errno = EINVAL; | |
+ return 0; | |
+ } | |
+ if (p-name > NAME_MAX-4) { | |
+ errno = ENAMETOOLONG; | |
+ return 0; | |
+ } | |
+ memcpy(buf, SHM_PREFIX, strlen(SHM_PREFIX)); | |
+ memcpy(buf+strlen(SHM_PREFIX), name, p-name+1); | |
+ return buf; | |
+} | |
+ | |
+int shm_open(const char *name, int flag, mode_t mode) | |
+{ | |
+ char buf[NAME_MAX+strlen(SHM_PREFIX)+1]; | |
+ if (!(name = __shm_mapname(name, buf))) return -1; | |
+ int fd = open(name, flag|O_NOFOLLOW|O_CLOEXEC|O_NONBLOCK, mode); | |
+ return fd; | |
+} | |
+ | |
+int shm_unlink(const char *name) | |
+{ | |
+ char buf[NAME_MAX+strlen(SHM_PREFIX)+1]; | |
+ if (!(name = __shm_mapname(name, buf))) return -1; | |
+ return unlink(name); | |
+} | |
' | |
PYTHON_PATCH_2=' | |
--- Python-'${PYTHON_VERSION}'/setup.py 2022-10-24 23:05:39.000000000 +0530 | |
+++ Python-'${PYTHON_VERSION}'/setup.py 2022-10-25 19:23:59.154046267 +0530 | |
@@ -1328,8 +1329,8 @@ | |
sysconfig.get_config_var('\''POSIX_SEMAPHORES_NOT_ENABLED'\'') | |
): | |
multiprocessing_srcs.append('\''_multiprocessing/semaphore.c'\'') | |
- self.addext(Extension('\''_multiprocessing'\'', multiprocessing_srcs)) | |
- self.addext(Extension('\''_posixshmem'\'', ['\''_multiprocessing/posixshmem.c'\''])) | |
+ self.addext(Extension('\''_multiprocessing'\'', multiprocessing_srcs)) | |
+ self.addext(Extension('\''_posixshmem'\'', ['\''_multiprocessing/posixshmem.c'\'','\''_multiprocessing/posix-shm-extension.c'\''])) | |
def detect_uuid(self): | |
# Build the _uuid module if possible | |
' | |
TMP_DIR="$(mktemp -d)" | |
mkdir -p "${TMP_DIR}/patches/Python-${PYTHON_VERSION}" | |
for i in $(seq -s " " 1 "$(set | cut -d= -f1 | grep -E "PYTHON_PATCH_[0-9]+" | wc -l)"); do | |
eval "echo \"\$PYTHON_PATCH_$i\" > '${TMP_DIR}/patches/Python-${PYTHON_VERSION}/python_patch_$i.patch'" | |
done | |
docker run --name debian-python-builder-${{ matrix.platform }} --platform linux/${{ matrix.platform }} -v "${TMP_DIR}/patches:/tmp/patches" bitnami/minideb:bookworm bash -c ' | |
echo "nameserver 1.1.1.1" > /etc/resolv.conf; \ | |
apt update || exit 1; \ | |
DEBIAN_FRONTEND=noninteractive apt reinstall -y perl-base; DEBIAN_FRONTEND=noninteractive apt install -y --no-install-recommends curl ca-certificates gcc libbz2-dev libev-dev libffi-dev libgdbm-dev liblzma-dev libncurses-dev libreadline-dev libsqlite3-dev libssl-dev make tk-dev wget tar zlib1g-dev || exit 1; \ | |
export PYTHON_VERSION='${PYTHON_VERSION}' PYTHON_MAJOR='${PYTHON_MAJOR}'; \ | |
wget "https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz" -O "Python-${PYTHON_VERSION}.tgz" || exit 1; \ | |
rm -rf "Python-${PYTHON_VERSION}"; \ | |
mkdir -p "Python-${PYTHON_VERSION}"; \ | |
tar -xf "Python-${PYTHON_VERSION}.tgz" -C "Python-${PYTHON_VERSION}" --strip-components=1; \ | |
find "/tmp/patches/Python-${PYTHON_VERSION}" -maxdepth 1 -type f -name "python_patch_*\.patch" -exec patch -p0 -i "{}" \;; \ | |
cd "Python-${PYTHON_VERSION}"; \ | |
./configure --prefix=/usr --enable-shared --enable-optimizations LDFLAGS=-Wl,-rpath=/usr/lib,--disable-new-dtags ac_cv_posix_semaphores_enabled=yes ac_cv_func_sem_open=yes ac_cv_func_sem_timedwait=yes ac_cv_func_sem_getvalue=yes ac_cv_func_sem_unlink=yes; \ | |
make || exit 1; \ | |
rm -rf dist; \ | |
PATH="$(pwd)/dist/usr/bin:${PATH}" DESTDIR=dist make install; \ | |
tar -I "gzip --best" -cf "python-${PYTHON_VERSION}.tar.gz" -C dist ./usr; \ | |
mv "python-${PYTHON_VERSION}.tar.gz" /tmp; \ | |
' | |
docker cp debian-python-builder-${{ matrix.platform }}:/tmp/python-${PYTHON_VERSION}.tar.gz . | |
- name: Upload Release | |
uses: svenstaro/upload-release-action@v2 | |
with: | |
repo_token: ${{ secrets.GITHUB_TOKEN }} | |
file: python-${{ matrix.PYTHON_VERSION }}.tar.gz | |
asset_name: python-${{ matrix.PYTHON_VERSION }}-${{ env.arch_name }}.tar.gz | |
tag: "python-fixed" | |
release_name: "Python Fixed" | |
body: "Python fixed to support multiprocessing module in Android environment - https://github.com/termux/termux-packages/pull/8990" | |
overwrite: true |