|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | +set -o pipefail |
| 5 | +set -x |
| 6 | + |
| 7 | + |
| 8 | +ZLIB_VERSION=1.2.11 |
| 9 | +TERMCAP_VERSION=1.3.1 |
| 10 | +READLINE_VERSION=6.3 |
| 11 | +OPENSSL_VERSION=1.0.2k |
| 12 | +PYTHON_BASE_VERSION=2.7.15 |
| 13 | +PSUTIL_VERSION=5.4.5 |
| 14 | +PYTHON_RC=rc1 |
| 15 | + |
| 16 | +CPU_COUNT=$(nproc --all 2>/dev/null) |
| 17 | +MAKE_J="-j${CPU_COUNT:-1}" |
| 18 | + |
| 19 | +function fetch_psutil() { |
| 20 | + cd /build |
| 21 | + curl -L -o /build/psutil-${PSUTIL_VERSION}.zip https://github.com/giampaolo/psutil/archive/release-${PSUTIL_VERSION}.zip |
| 22 | + unzip /build/psutil-${PSUTIL_VERSION}.zip |
| 23 | + mv psutil-release-${PSUTIL_VERSION} psutil-${PSUTIL_VERSION} |
| 24 | + # Prune out the Python files from the C files and then monkeypatch the |
| 25 | + # Python files |
| 26 | + mkdir -p psutil/{c_files,python_files} |
| 27 | + cp -r /build/psutil-${PSUTIL_VERSION}/psutil psutil/c_files/ |
| 28 | + rm -rf psutil/c_files/psutil/*.py* psutil/c_files/psutil/DEVNOTES psutil/c_files/psutil/tests |
| 29 | + # Python files |
| 30 | + cp -r /build/psutil-${PSUTIL_VERSION}/psutil psutil/python_files/ |
| 31 | + rm -rf psutil/python_files/psutil/*.c psutil/python_files/psutil/*.h psutil/python_files/psutil/arch psutil/python_files/psutil/tests |
| 32 | + # Patch now |
| 33 | + cd psutil |
| 34 | + patch --ignore-whitespace -p0 < /build/psutil_import__init__.patch |
| 35 | + patch --ignore-whitespace -p0 < /build/psutil_import_pslinux.patch |
| 36 | + cd .. |
| 37 | + # Ready to be copied for Lib |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +function build_zlib() { |
| 42 | + cd /build |
| 43 | + |
| 44 | + # Download |
| 45 | + curl -LO http://zlib.net/zlib-${ZLIB_VERSION}.tar.gz |
| 46 | + tar zxvf zlib-${ZLIB_VERSION}.tar.gz |
| 47 | + cd zlib-${ZLIB_VERSION} |
| 48 | + |
| 49 | + # Build |
| 50 | + CC='/opt/cross/x86_64-linux-musl/bin/x86_64-linux-musl-gcc -static -fPIC' \ |
| 51 | + ./configure \ |
| 52 | + --static |
| 53 | + make ${MAKE_J} |
| 54 | +} |
| 55 | + |
| 56 | +function build_termcap() { |
| 57 | + cd /build |
| 58 | + |
| 59 | + # Download |
| 60 | + curl -LO http://ftp.gnu.org/gnu/termcap/termcap-${TERMCAP_VERSION}.tar.gz |
| 61 | + tar zxvf termcap-${TERMCAP_VERSION}.tar.gz |
| 62 | + cd termcap-${TERMCAP_VERSION} |
| 63 | + |
| 64 | + # Build |
| 65 | + CC='/opt/cross/x86_64-linux-musl/bin/x86_64-linux-musl-gcc -static -fPIC' \ |
| 66 | + ./configure \ |
| 67 | + --disable-shared \ |
| 68 | + --enable-static |
| 69 | + make ${MAKE_J} |
| 70 | +} |
| 71 | + |
| 72 | +function build_readline() { |
| 73 | + cd /build |
| 74 | + |
| 75 | + # Download |
| 76 | + curl -LO ftp://ftp.cwru.edu/pub/bash/readline-${READLINE_VERSION}.tar.gz |
| 77 | + tar xzvf readline-${READLINE_VERSION}.tar.gz |
| 78 | + cd readline-${READLINE_VERSION} |
| 79 | + |
| 80 | + # Build |
| 81 | + CC='/opt/cross/x86_64-linux-musl/bin/x86_64-linux-musl-gcc -static -fPIC' \ |
| 82 | + ./configure \ |
| 83 | + --disable-shared \ |
| 84 | + --enable-static |
| 85 | + make ${MAKE_J} |
| 86 | + |
| 87 | + # Note that things look for readline in <readline/readline.h>, so we need |
| 88 | + # that directory to exist. |
| 89 | + ln -s /build/readline-${READLINE_VERSION} /build/readline-${READLINE_VERSION}/readline |
| 90 | +} |
| 91 | + |
| 92 | +function build_openssl() { |
| 93 | + cd /build |
| 94 | + |
| 95 | + # Download |
| 96 | + curl -LO https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz |
| 97 | + tar zxvf openssl-${OPENSSL_VERSION}.tar.gz |
| 98 | + cd openssl-${OPENSSL_VERSION} |
| 99 | + |
| 100 | + # Configure |
| 101 | + CC='/opt/cross/x86_64-linux-musl/bin/x86_64-linux-musl-gcc -static' ./Configure no-shared linux-x86_64 |
| 102 | + |
| 103 | + # Build |
| 104 | + make |
| 105 | + echo "** Finished building OpenSSL" |
| 106 | +} |
| 107 | + |
| 108 | +function build_python() { |
| 109 | + cd /build |
| 110 | + |
| 111 | + # Download |
| 112 | + curl -LO https://www.python.org/ftp/python/${PYTHON_BASE_VERSION}/Python-${PYTHON_BASE_VERSION}${PYTHON_RC}.tar.xz |
| 113 | + unxz Python-${PYTHON_BASE_VERSION}${PYTHON_RC}.tar.xz |
| 114 | + tar -xvf Python-${PYTHON_BASE_VERSION}${PYTHON_RC}.tar |
| 115 | + cd Python-${PYTHON_BASE_VERSION}${PYTHON_RC} |
| 116 | + |
| 117 | + # Copy psutil source code into place |
| 118 | + mkdir Modules/psutil |
| 119 | + cp -r /build/psutil/c_files/psutil/* Modules/psutil/ |
| 120 | + # Convert the version to what psutil expects. This should be stable until |
| 121 | + # their setup.py changes. |
| 122 | + psversion=$(echo $PSUTIL_VERSION | sed 's,\.,,g') |
| 123 | + |
| 124 | + # Set up modules |
| 125 | + cp Modules/Setup.dist Modules/Setup |
| 126 | + MODULES="_bisect _collections _csv _datetime _elementtree _functools _heapq _io _md5 _posixsubprocese _random _sha _sha256 _sha512 _socket _struct _weakref array binascii cmath cStringIO cPickle datetime fcntl future_builtins grp itertools math mmap operator parser readline resource select spwd strop syslog termios time unicodedata zlib" |
| 127 | + for mod in $MODULES; |
| 128 | + do |
| 129 | + sed -i -e "s/^#${mod}/${mod}/" Modules/Setup |
| 130 | + done |
| 131 | + |
| 132 | + echo '_json _json.c' >> Modules/Setup |
| 133 | + echo '_multiprocessing _multiprocessing/multiprocessing.c _multiprocessing/semaphore.c _multiprocessing/socket_connection.c' >> Modules/Setup |
| 134 | + |
| 135 | + # Enable static linking |
| 136 | + sed -i '1i\ |
| 137 | +*static*' Modules/Setup |
| 138 | + |
| 139 | + # Set dependency paths for zlib, readline, etc. |
| 140 | + sed -i \ |
| 141 | + -e "s|^zlib zlibmodule.c|zlib zlibmodule.c -I/build/zlib-${ZLIB_VERSION} -L/build/zlib-${ZLIB_VERSION} -lz|" \ |
| 142 | + -e "s|^readline readline.c|readline readline.c -I/build/readline-${READLINE_VERSION} -L/build/readline-${READLINE_VERSION} -L/build/termcap-${TERMCAP_VERSION} -lreadline -ltermcap|" \ |
| 143 | + Modules/Setup |
| 144 | + # static compile these modules. Any others that `setup.py` might want to |
| 145 | + # build "shared" should follow this pattern to make them included |
| 146 | + echo "" >> Modules/Setup |
| 147 | + cat << 'EOF' >>Modules/Setup |
| 148 | +_lsprof rotatingtree.c _lsprof.c |
| 149 | +pyexpat expat/xmlparse.c expat/xmlrole.c expat/xmltok.c pyexpat.c -I$(srcdir)/Modules/expat -DHAVE_EXPAT_CONFIG_H -DUSE_PYEXPAT_CAPI -DHAVE_SYSCALL_GETRANDOM |
| 150 | +_psutil_linux psutil/_psutil_common.c psutil/_psutil_posix.c psutil/_psutil_linux.c -DPSUTIL_POSIX=1 -DPSUTIL_VERSION=__PSVER__ -DPSUTIL_LINUX=1 |
| 151 | +_psutil_posix psutil/_psutil_common.c psutil/_psutil_posix.c -DPSUTIL_POSIX=1 -DPSUTIL_VERSION=__PSVER__ -DPSUTIL_LINUX=1 |
| 152 | +EOF |
| 153 | + |
| 154 | + # Enable OpenSSL support |
| 155 | + patch --ignore-whitespace -p1 < /build/cpython-enable-openssl.patch |
| 156 | + |
| 157 | + # Fix https://bugs.python.org/issue7938 |
| 158 | + echo "Patching for https://bugs.python.org/issue7938" |
| 159 | + patch --ignore-whitespace -p0 < /build/BPO-7938_pr4338-fix-makesetup-script.patch |
| 160 | + |
| 161 | + sed -i \ |
| 162 | + -e "s|^SSL=/build/openssl-TKTK|SSL=/build/openssl-${OPENSSL_VERSION}|" \ |
| 163 | + -e "s|__PSVER__|${psversion}|g" \ |
| 164 | + Modules/Setup |
| 165 | + |
| 166 | + # Configure |
| 167 | + CC='/opt/cross/x86_64-linux-musl/bin/x86_64-linux-musl-gcc -static -fPIC' \ |
| 168 | + CXX='/opt/cross/x86_64-linux-musl/bin/x86_64-linux-musl-g++ -static -static-libstdc++ -fPIC' \ |
| 169 | + LD=/opt/cross/x86_64-linux-musl/bin/x86_64-linux-musl-ld \ |
| 170 | + ./configure \ |
| 171 | + --disable-shared |
| 172 | + |
| 173 | + # Build |
| 174 | + make --trace ${MAKE_J} LDFLAGS="-static" LINKFORSHARED=" " || true |
| 175 | + |
| 176 | + |
| 177 | + /opt/cross/x86_64-linux-musl/bin/x86_64-linux-musl-strip python |
| 178 | + # There may be a better way to ensure _sysconfigdata.py is included in the |
| 179 | + # .zip file, but I am not sure how best to do it, so this is a bit of a |
| 180 | + # hack, banking on the contents of this container staying around for it |
| 181 | + # to matter. |
| 182 | + cp $(find . -name _sysconfigdata.py -print) Lib |
| 183 | + # Copy the patched psutil Python files into place |
| 184 | + cp -r /build/psutil/python_files/psutil Lib |
| 185 | + cd Lib && zip -r ../python2.7.zip . |
| 186 | + |
| 187 | +} |
| 188 | + |
| 189 | +function doit() { |
| 190 | + fetch_psutil |
| 191 | + build_zlib |
| 192 | + build_termcap |
| 193 | + build_readline |
| 194 | + build_openssl |
| 195 | + build_python |
| 196 | + |
| 197 | + mkdir -p /output |
| 198 | + cp /build/Python-${PYTHON_BASE_VERSION}${PYTHON_RC}/{python,python2.7.zip} /output |
| 199 | +} |
| 200 | + |
| 201 | +doit |
| 202 | + |
| 203 | +echo "Output:" |
| 204 | +ls -l /output |
| 205 | +echo "Done." |
0 commit comments