build-cpython.sh (5371B)
1 #!/bin/sh 2 # This Source Code Form is subject to the terms of the Mozilla Public 3 # License, v. 2.0. If a copy of the MPL was not distributed with this 4 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 # 6 # This script builds the official interpreter for the python language, 7 # while also packing in a few default extra packages. 8 9 set -e 10 set -x 11 12 # Required fetch artifact 13 clang_bindir=${MOZ_FETCHES_DIR}/clang/bin 14 clang_libdir=${MOZ_FETCHES_DIR}/clang/lib 15 python_src=${MOZ_FETCHES_DIR}/cpython-source 16 xz_prefix=${MOZ_FETCHES_DIR}/xz 17 18 # Make the compiler-rt available to clang. 19 env UPLOAD_DIR= $GECKO_PATH/taskcluster/scripts/misc/repack-clang.sh 20 21 # Extra setup per platform 22 case `uname -s` in 23 Darwin) 24 # Use taskcluster clang instead of host compiler on OSX 25 export PATH=${clang_bindir}:${PATH} 26 export CC=clang 27 export CXX=clang++ 28 export LDFLAGS=-fuse-ld=lld 29 30 case `uname -m` in 31 aarch64) 32 macosx_version_min=11.0 33 ;; 34 *) 35 macosx_version_min=10.15 36 ;; 37 esac 38 # NOTE: both CFLAGS and CPPFLAGS need to be set here, otherwise 39 # configure step fails. 40 sysroot_flags="-isysroot ${MOZ_FETCHES_DIR}/MacOSX26.2.sdk -mmacosx-version-min=${macosx_version_min}" 41 export CPPFLAGS="${sysroot_flags} -I${xz_prefix}/include" 42 export CFLAGS=${sysroot_flags} 43 export LDFLAGS="${LDFLAGS} ${sysroot_flags} -L${xz_prefix}/lib" 44 configure_flags_extra=--with-openssl=/usr/local/opt/openssl 45 46 # see https://bugs.python.org/issue22490 47 unset __PYVENV_LAUNCHER__ 48 49 # see https://bugs.python.org/issue44065 50 sed -i -e 's,$CC --print-multiarch,:,' ${python_src}/configure 51 export LDFLAGS="${LDFLAGS} -Wl,-rpath -Wl,@loader_path/../.." 52 ;; 53 Linux) 54 # Use host gcc on Linux 55 export LDFLAGS="${LDFLAGS} -Wl,-rpath,\\\$ORIGIN/../.." 56 ;; 57 esac 58 59 # Patch Python to honor MOZPYTHONHOME instead of PYTHONHOME. That way we have a 60 # relocatable python for free, while not interfering with the system Python that 61 # already honors PYTHONHOME. 62 find ${python_src} -type f -print0 | xargs -0 perl -i -pe "s,PYTHONHOME,MOZPYTHONHOME,g" 63 64 # Actual build 65 work_dir=`pwd` 66 tardir=python 67 68 cd `mktemp -d` 69 ${python_src}/configure --prefix=/${tardir} --enable-optimizations --with-lto ${configure_flags_extra} || { exit_status=$? && cat config.log && exit $exit_status ; } 70 71 export MAKEFLAGS=-j`nproc` 72 make 73 make DESTDIR=${work_dir} install 74 cd ${work_dir} 75 76 sysconfig_file=$( 77 ls "${work_dir}/${tardir}/lib"/python3.*/*_sysconfigdata*.py 2>/dev/null \ 78 | head -n1 79 ) 80 if [ -n "$sysconfig_file" ]; then 81 cat >> "$sysconfig_file" << 'PYCODE' 82 import sys 83 build_time_vars = { 84 k: v.replace("/python", sys.base_prefix) if isinstance(v, str) and v.startswith("/python") else v 85 for k, v in build_time_vars.items() 86 } 87 PYCODE 88 fi 89 90 ${work_dir}/python/bin/python3 -m pip install --upgrade pip==23.0 91 ${work_dir}/python/bin/python3 -m pip install -r ${GECKO_PATH}/build/psutil_requirements.txt -r ${GECKO_PATH}/build/zstandard_requirements.txt 92 93 case `uname -s` in 94 Darwin) 95 96 cp /usr/local/opt/openssl/lib/libssl*.dylib ${work_dir}/python/lib/ 97 cp /usr/local/opt/openssl/lib/libcrypto*.dylib ${work_dir}/python/lib/ 98 cp ${xz_prefix}/lib/liblzma.dylib ${work_dir}/python/lib/ 99 cp ${xz_prefix}/lib/liblzma.5.dylib ${work_dir}/python/lib/ 100 101 # Instruct the loader to search for the lib in rpath instead of the one used during linking 102 install_name_tool -change /usr/local/opt/openssl@1.1/lib/libssl.1.1.dylib @rpath/libssl.1.1.dylib ${work_dir}/python/lib/python3.*/lib-dynload/_ssl.cpython-3*-darwin.so 103 install_name_tool -change /usr/local/opt/openssl@1.1/lib/libcrypto.1.1.dylib @rpath/libcrypto.1.1.dylib ${work_dir}/python/lib/python3.*/lib-dynload/_ssl.cpython-3*-darwin.so 104 otool -L ${work_dir}/python/lib/python3.*/lib-dynload/_ssl.cpython-3*-darwin.so | grep @rpath/libssl.1.1.dylib 105 106 107 install_name_tool -change /xz/lib/liblzma.5.dylib @rpath/liblzma.5.dylib ${work_dir}/python/lib/python3.*/lib-dynload/_lzma.cpython-3*-darwin.so 108 otool -L ${work_dir}/python/lib/python3.*/lib-dynload/_lzma.cpython-3*-darwin.so | grep @rpath/liblzma.5.dylib 109 110 # Also modify the shipped libssl to use the shipped libcrypto 111 install_name_tool -change /usr/local/Cellar/openssl@1.1/1.1.1h/lib/libcrypto.1.1.dylib @rpath/libcrypto.1.1.dylib ${work_dir}/python/lib/libssl.1.1.dylib 112 otool -L ${work_dir}/python/lib/libssl.1.1.dylib | grep @rpath/libcrypto.1.1.dylib 113 114 # sanity check 115 ${work_dir}/python/bin/python3 -c "import ssl" 116 ${work_dir}/python/bin/python3 -c "import lzma" 117 118 # We may not have access to system certificate on OSX 119 ${work_dir}/python/bin/python3 -m pip install certifi==2024.2.2 120 ;; 121 Linux) 122 cp /usr/lib/$(uname -m)-linux-gnu/libffi.so.* ${work_dir}/python/lib/ 123 cp /usr/lib/$(uname -m)-linux-gnu/libssl.so.* ${work_dir}/python/lib/ 124 cp /usr/lib/$(uname -m)-linux-gnu/libcrypto.so.* ${work_dir}/python/lib/ 125 cp /lib/$(uname -m)-linux-gnu/libncursesw.so.* ${work_dir}/python/lib/ 126 cp /lib/$(uname -m)-linux-gnu/libtinfo.so.* ${work_dir}/python/lib/ 127 ;; 128 esac 129 130 $(dirname $0)/pack.sh ${tardir}