-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
build-python.sh
executable file
·169 lines (143 loc) · 4.43 KB
/
build-python.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
#!/bin/sh
#
# Copyright (c) 2018 Martin Storsjo
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
set -e
: ${LIBFFI_VERSION:=v3.4.6}
: ${PYTHON_VERSION:=v3.11.9}
: ${PYTHON_VERSION_MINGW:=d6d38acc0d637e6dc2fe6b984664bdd460bd3d04}
unset HOST
BUILDDIR=build
while [ $# -gt 0 ]; do
case "$1" in
--host=*)
HOST="${1#*=}"
BUILDDIR=$BUILDDIR-$HOST
;;
*)
PREFIX="$1"
;;
esac
shift
done
if [ -z "$CHECKOUT_ONLY" ]; then
if [ -z "$PREFIX" ]; then
echo $0 --host=triple dest
exit 1
fi
mkdir -p "$PREFIX"
PREFIX="$(cd "$PREFIX" && pwd)"
fi
MAKE=make
if command -v gmake >/dev/null; then
MAKE=gmake
fi
: ${CORES:=$(nproc 2>/dev/null)}
: ${CORES:=$(sysctl -n hw.ncpu 2>/dev/null)}
: ${CORES:=4}
if [ ! -d libffi ]; then
git clone https://github.com/libffi/libffi.git
CHECKOUT_LIBFFI=1
fi
if [ -n "$SYNC" ] || [ -n "$CHECKOUT_LIBFFI" ]; then
cd libffi
[ -z "$SYNC" ] || git fetch
git reset --hard
git checkout $LIBFFI_VERSION
autoreconf -vfi
cd ..
fi
if [ -z "$HOST" ]; then
# Use a separate checkout for python for the native build;
# mingw builds use a separate fork, maintained by msys2
# which doesn't build on regular Unix
if [ ! -d cpython-native ]; then
git clone https://github.com/python/cpython.git cpython-native
CHECKOUT_PYTHON_NATIVE=1
fi
if [ -n "$SYNC" ] || [ -n "$CHECKOUT_PYTHON_NATIVE" ]; then
cd cpython-native
[ -z "$SYNC" ] || git fetch
git checkout $PYTHON_VERSION
cd ..
fi
[ -z "$CHECKOUT_ONLY" ] || exit 0
cd libffi
[ -z "$CLEAN" ] || rm -rf $BUILDDIR
mkdir -p $BUILDDIR
cd $BUILDDIR
../configure --prefix="$PREFIX" --disable-symvers --disable-docs
$MAKE -j$CORES
$MAKE install
cd ../..
cd cpython-native
[ -z "$CLEAN" ] || rm -rf $BUILDDIR
mkdir -p $BUILDDIR
cd $BUILDDIR
../configure --prefix="$PREFIX" \
CFLAGS="-I$PREFIX/include" CXXFLAGS="-I$PREFIX/include" LDFLAGS="-L$PREFIX/lib -Wl,-s" \
--without-ensurepip \
--disable-test-modules
$MAKE -j$CORES
$MAKE install
exit 0
fi
# Fetching
if [ ! -d cpython-mingw ]; then
git clone https://github.com/msys2-contrib/cpython-mingw.git
CHECKOUT_PYTHON=1
fi
if [ -n "$SYNC" ] || [ -n "$CHECKOUT_PYTHON" ]; then
cd cpython-mingw
[ -z "$SYNC" ] || git fetch
git reset --hard
git checkout $PYTHON_VERSION_MINGW
autoreconf -vfi
cd ..
fi
[ -z "$CHECKOUT_ONLY" ] || exit 0
cd libffi
[ -z "$CLEAN" ] || rm -rf $BUILDDIR
mkdir -p $BUILDDIR
cd $BUILDDIR
../configure --prefix="$PREFIX" --host=$HOST --disable-symvers --disable-docs
$MAKE -j$CORES
$MAKE install
cd ../..
cd cpython-mingw
[ -z "$CLEAN" ] || rm -rf $BUILDDIR
mkdir -p $BUILDDIR
cd $BUILDDIR
BUILD=$(../config.guess) # Python configure requires build triplet for cross compilation
# Locate the native python3 that we've built before, from the path
NATIVE_PYTHON="$(command -v python3)"
export CC=$HOST-gcc
export CXX=$HOST-g++
../configure --prefix="$PREFIX" --build=$BUILD --host=$HOST \
CFLAGS="-I$PREFIX/include" CXXFLAGS="-I$PREFIX/include" LDFLAGS="-L$PREFIX/lib -Wl,-s" \
PKG_CONFIG_LIBDIR="$PREFIX/lib/pkgconfig" \
--with-build-python="$NATIVE_PYTHON" \
--enable-shared \
--with-system-ffi \
--without-ensurepip \
--without-c-locale-coercion \
--disable-test-modules
$MAKE -j$CORES
$MAKE install
find $PREFIX/lib/python* -name __pycache__ | xargs rm -rf
# Provide a versionless executable as well; msys2 does something similar
# (for python3, python3w, python3-config, idle3 and pydoc3) after installing
# a Python version that is supposed to be the primary Python.
cp -a $PREFIX/bin/python3.exe $PREFIX/bin/python.exe
cd ../..