-
Notifications
You must be signed in to change notification settings - Fork 1
210 lines (193 loc) · 10.1 KB
/
build_python.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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