-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
cross-modules.sh
217 lines (149 loc) · 5.5 KB
/
cross-modules.sh
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
#
# BUILD python is the python of venv => mainly used for cmake and pip downloads.
# HOST python is the python matching TARGET conditions but built for BUILD => you can run it
# TARGET python is never used, and you cannot run it normally unless root on target device.
#
# https://bugs.python.org/issue28833 cross compilation of third-party extension modules
# https://github.com/google/python-adb
# https://github.com/appknox/adb-join-wifi
# https://github.com/appknox/AndroidProxySetter
# https://github.com/fujiawei-dev/cerium
# https://pypi.org/project/androidautotest/
# https://pypi.org/project/androidvideo/
# https://github.com/OzymandiasTheGreat/Joy2DroidX
# prevent from using user installed packages
export PYTHONPYCACHEPREFIX=${PYTHONPYCACHEPREFIX}
export HOME=${PYTHONPYCACHEPREFIX}
# install Cython in build python
pip3 install Cython
mkdir -p ${ORIGIN}/pydk-min/prebuilt/${ABI_NAME}
# put a copy into prebuilt for packaging support lib
/bin/cp -f ${APKUSR}/lib/lib*.so ${ORIGIN}/pydk-min/prebuilt/${ABI_NAME}/
# put the sysroot for projects building
# TODO: dedup common + arch folder
mkdir -p ${ORIGIN}/pydk-min/${ENV}/apkroot-${ABI_NAME}/usr
/bin/cp -rf ${APKUSR}/include ${ORIGIN}/pydk-min/${ENV}/apkroot-${ABI_NAME}/usr
if [ -d ${ORIGIN}/wapy-lib ]
then
echo " * using local stdlib"
else
git clone --single-branch --branch wapy-wipdonotuse https://github.com/pmp-p/wapy-lib.git wapy-lib
fi
if [ -d ${ORIGIN}/projects ]
then
echo " * using local projects"
else
git clone https://github.com/pmp-p/projects.git projects
fi
if [ -f "${ORIGIN}/projects/requirements.txt" ]
then
echo " * using local projects pip requirements"
else
# seed some usefull modules to build
cat <<END > "${ORIGIN}/projects/requirements.txt"
wheel
numpy
python-box
pyjnius
plyer
pyglet
git+https://github.com/pmp-p/pygame@pydk
END
#
# prevent eggs https://stackoverflow.com/questions/6301003/stopping-setup-py-from-installing-as-egg
# setup.py install --single-version-externally-managed --root=/
fi
echo " * cross compiling third party modules"
cd ${BUILD_PREFIX}-${ABI_NAME}
mkdir -p pip-${ABI_NAME}
cd pip-${ABI_NAME}
function cross_pip
{
MODE=$1
shift
${ROOT}/bin/python3-${ABI_NAME} -m pip $MODE --use-feature=2020-resolver --no-use-pep517 --no-binary :all: $@
}
for req in $(find "${ORIGIN}/projects" -maxdepth 2 -type f |grep /requirements.txt$)
do
if cross_pip install -r $req
then
echo ok
else
echo "ERROR: pip install $req failed"
fi
done
# TODO: build/patch from sources pip packages here.
# ${ROOT}/bin/pip3 download --dest ${CROSSDIR} --no-binary :all: setuptools pip
mkdir -p ${ORIGIN}/assets/packages
for req in $(find ${APKUSR}/lib/python${PYMAJOR}.${PYMINOR}/site-packages/ -maxdepth 1 | egrep -v 'pth$|info$|egg$|txt$|/$')
do
echo " * adding python pip package : $(basename $req)"
cp -ru $req ${ORIGIN}/assets/packages/
rm $(find ${ORIGIN}/assets/packages/|grep so$)
done
# copy all sysconfig for prebuilt
cp ${APKUSR}/lib/python${PYMAJOR}.${PYMINOR}/_sysconfig*py ${ORIGIN}/assets/python${PYMAJOR}.${PYMINOR}/
LLVM_STRIP=$(find $ANDROID_NDK_HOME/|grep llvm-strip$)
cat > ${BUILD_PREFIX}-${ABI_NAME}/pip_lib.py <<END
import os
import shutil
libdef = []
liblist = []
FOUND="""$(find ${APKUSR}/lib/python3.?|grep so$)""".split("\n")
PREBUILT="${ORIGIN}/pydk-min/prebuilt/${ABI_NAME}/"
PREFIX='/lib/python${PYMAJOR}.${PYMINOR}/'
NEEDED = 'libpython${PYMAJOR}.${PYMINOR}.so'
print(f"PREFIX = {PREFIX}")
# no multiarch
if 0:
for libpath in FOUND:
if libpath.find('lib-dynload/_testcapi.cpython-3'):
SUFFIX = '.'+libpath.rsplit('.',2)[-2] + '.so'
break
else:
SUFFIX= ".so"
print(f"SUFFIX = {SUFFIX}")
for libpath in FOUND:
if "$ABI_NAME" == "wasm":
print("libpath={}".format(libpath))
if not libpath:continue
folder, lib = libpath.rsplit('/',1)
# module can be in "site-packages" "lib-dynload" or none of them
modpath = libpath.rsplit(PREFIX,1)[-1]
modpath = modpath.split('/')
# make all modules relative to same base path
if modpath[0] in ["site-packages","lib-dynload"]:
modpath.pop(0)
# get modname + { .suffix or .so }
modname = modpath.pop()
if modname.find(SUFFIX)>=0:
modname = lib.rsplit(SUFFIX,1)[0]
else:
modname = lib.rsplit('.',1)[0]
cmk_name = '_'.join(modpath) + '_' + modname
lib = "lib." + '.'.join(modpath) + '.' + modname + SUFFIX
if lib.startswith('lib..'):
lib = 'lib' + lib[4:]
print(f" Module : {modpath} | {modname} => {lib}")
libdef.append(f"""
add_library({cmk_name} SHARED IMPORTED )
set_target_properties({cmk_name} PROPERTIES IMPORTED_LOCATION \${{PREBUILT}}+{lib})
""")
liblist.append(f' {cmk_name}\n')
os.makedirs(PREBUILT, exist_ok=True)
shutil.copyfile(libpath,PREBUILT+lib)
os.system(f"patchelf --set-soname {lib} {PREBUILT+lib}")
if not os.popen(f"patchelf --print-needed {libpath}").read().count(NEEDED):
print(f"#FIXME: python module {modpath} {modname} lacked dynamic ref to libpython !")
os.system(f"patchelf --add-needed {NEEDED} {PREBUILT+lib}")
print(PREBUILT)
with open("${ORIGIN}/pydk-min/prebuilt/${ABI_NAME}.include","w") as file:
print(f"""
add_library(jnilink SHARED jnilink.c)
{''.join(libdef)}
target_link_libraries(jnilink
{''.join(liblist)})
""",file=file)
END
$LLVM_STRIP ${ORIGIN}/pydk-min/prebuilt/arm*/lib*.so ${ORIGIN}/pydk-min/prebuilt/x86*/lib*.so
${HOST}/bin/python3 -u -B ${BUILD_PREFIX}-${ABI_NAME}/pip_lib.py