|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +if [[ ! -v WLR_ENV ]] |
| 4 | +then |
| 5 | + echo "WLR build environment is not set" |
| 6 | + exit 1 |
| 7 | +fi |
| 8 | + |
| 9 | +cd "${WLR_SOURCE_PATH}" |
| 10 | + |
| 11 | +if [[ "${WLR_BUILD_FLAVOR}" == *"aio"* ]] |
| 12 | +then |
| 13 | + source ${WLR_REPO_ROOT}/scripts/build-helpers/wlr_wasi_vfs.sh |
| 14 | + wlr_wasi_vfs_setup_dependencies || exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +source ${WLR_REPO_ROOT}/scripts/build-helpers/wlr_pkg_config.sh |
| 18 | + |
| 19 | +export CFLAGS_CONFIG="-O0" |
| 20 | + |
| 21 | + |
| 22 | +# This fails with upgraded clang for wasi-sdk19 and later. Disabled on cpython main. |
| 23 | +# |
| 24 | +# PyModule_AddIntMacro(module, CLOCK_MONOTONIC) and the like cause this. |
| 25 | +# In all POSIX variants CLOCK_MONOTONIC is a numeric constant, so python imports it as int macro |
| 26 | +# However, in wasi-libc clockid_t is defined as a pointer to struct __clockid. |
| 27 | + |
| 28 | +export CFLAGS_CONFIG="${CFLAGS_CONFIG} -Wno-int-conversion" |
| 29 | + |
| 30 | +export CFLAGS="${CFLAGS_CONFIG} ${CFLAGS_DEPENDENCIES} ${CFLAGS}" |
| 31 | +export LDFLAGS="${LDFLAGS_DEPENDENCIES} ${LDFLAGS}" |
| 32 | + |
| 33 | +export PYTHON_WASM_CONFIGURE="--with-build-python=python3" |
| 34 | + |
| 35 | +if [[ "${WLR_BUILD_FLAVOR}" == *"wasmedge"* ]] |
| 36 | +then |
| 37 | + if [[ ! -v WABT_ROOT ]] |
| 38 | + then |
| 39 | + echo "WABT_ROOT is needed to patch imports for wasmedge" |
| 40 | + exit 1 |
| 41 | + fi |
| 42 | +fi |
| 43 | + |
| 44 | +# By exporting WLR_SKIP_WASM_OPT envvar during the build, the |
| 45 | +# wasm-opt wrapper in the wasm-base image will be a dummy wrapper that |
| 46 | +# is effectively a NOP. |
| 47 | +# |
| 48 | +# This is due to https://github.com/llvm/llvm-project/issues/55781, so |
| 49 | +# that we get to choose which optimization passes are executed after |
| 50 | +# the artifacts have been built. |
| 51 | +export WLR_SKIP_WASM_OPT=1 |
| 52 | + |
| 53 | +if [[ -z "$WLR_SKIP_CONFIGURE" ]]; then |
| 54 | + logStatus "Configuring build with '${PYTHON_WASM_CONFIGURE}'... " |
| 55 | + CONFIG_SITE=./Tools/wasm/config.site-wasm32-wasi ./configure -C --host=wasm32-wasi --build=$(./config.guess) ${PYTHON_WASM_CONFIGURE} || exit 1 |
| 56 | +else |
| 57 | + logStatus "Skipping configure..." |
| 58 | +fi |
| 59 | + |
| 60 | +export MAKE_TARGETS='python.wasm wasm_stdlib' |
| 61 | + |
| 62 | +logStatus "Building '${MAKE_TARGETS}'... " |
| 63 | +make -j ${MAKE_TARGETS} || exit 1 |
| 64 | + |
| 65 | +unset WLR_SKIP_WASM_OPT |
| 66 | + |
| 67 | +if [[ "${WLR_BUILD_FLAVOR}" == *"aio"* ]] |
| 68 | +then |
| 69 | + logStatus "Packing with wasi-vfs" |
| 70 | + wlr_wasi_vfs_cli pack python.wasm --mapdir /usr::$PWD/usr -o python.wasm || exit 1 |
| 71 | +fi |
| 72 | + |
| 73 | +logStatus "Optimizing python binary..." |
| 74 | +wasm-opt -O2 -o python-optimized.wasm python.wasm || exit 1 |
| 75 | + |
| 76 | +if [[ "${WLR_BUILD_FLAVOR}" == *"wasmedge"* ]] |
| 77 | +then |
| 78 | + logStatus "Patching python binary for wasmedge..." |
| 79 | + ${WLR_REPO_ROOT}/scripts/build-helpers/patch_wasmedge_wat_sock_accept.sh python-optimized.wasm || exit 1 |
| 80 | +fi |
| 81 | + |
| 82 | +logStatus "Preparing artifacts... " |
| 83 | +TARGET_PYTHON_BINARY=${WLR_OUTPUT}/bin/python.wasm |
| 84 | + |
| 85 | +mkdir -p ${WLR_OUTPUT}/bin 2>/dev/null || exit 1 |
| 86 | + |
| 87 | +if [[ "${WLR_BUILD_FLAVOR}" == *"aio"* ]] |
| 88 | +then |
| 89 | + cp -v python-optimized.wasm ${TARGET_PYTHON_BINARY} || exit 1 |
| 90 | +else |
| 91 | + mkdir -p ${WLR_OUTPUT}/usr 2>/dev/null || exit 1 |
| 92 | + cp -v python-optimized.wasm ${TARGET_PYTHON_BINARY} || exit 1 |
| 93 | + cp -TRv usr ${WLR_OUTPUT}/usr || exit 1 |
| 94 | +fi |
| 95 | + |
| 96 | +if [[ "${WLR_BUILD_FLAVOR}" != *"aio"* && "${WLR_BUILD_FLAVOR}" != *"wasmedge"* ]] |
| 97 | +then |
| 98 | + |
| 99 | + logStatus "Install includes..." |
| 100 | + make inclinstall \ |
| 101 | + prefix=${WLR_OUTPUT} \ |
| 102 | + libdir=${WLR_OUTPUT}/lib/wasm32-wasi \ |
| 103 | + pkgconfigdir=${WLR_OUTPUT}/lib/wasm32-wasi/pkgconfig || exit 1 |
| 104 | + |
| 105 | + logStatus "Create libpython3.11-aio.a" |
| 106 | +(${AR} -M <<EOF |
| 107 | +create libpython3.11-aio.a |
| 108 | +addlib libpython3.11.a |
| 109 | +addlib ${WLR_DEPS_ROOT}/build-output/lib/wasm32-wasi/libz.a |
| 110 | +addlib ${WLR_DEPS_ROOT}/build-output/lib/wasm32-wasi/libbz2.a |
| 111 | +addlib ${WLR_DEPS_ROOT}/build-output/lib/wasm32-wasi/libsqlite3.a |
| 112 | +addlib ${WLR_DEPS_ROOT}/build-output/lib/wasm32-wasi/libuuid.a |
| 113 | +addlib Modules/expat/libexpat.a |
| 114 | +addlib Modules/_decimal/libmpdec/libmpdec.a |
| 115 | +save |
| 116 | +end |
| 117 | +EOF |
| 118 | +) || echo exit 1 |
| 119 | + |
| 120 | + mkdir -p ${WLR_OUTPUT}/lib/wasm32-wasi/ 2>/dev/null || exit 1 |
| 121 | + cp -v libpython3.11-aio.a ${WLR_OUTPUT}/lib/wasm32-wasi/libpython3.11.a || exit 1 |
| 122 | + |
| 123 | + logStatus "Generating pkg-config file for libpython3.11.a" |
| 124 | + DESCRIPTION="libpython3.11 allows embedding the CPython interpreter" |
| 125 | + EXTRA_LINK_FLAGS="-lpython3.11 -Wl,-z,stack-size=524288 -Wl,--stack-first -Wl,--initial-memory=10485760 -lwasi-emulated-getpid -lwasi-emulated-signal -lwasi-emulated-process-clocks" |
| 126 | + |
| 127 | + PC_INCLUDE_SUBDIR=python3.11 wlr_pkg_config_create_pc_file "libpython3.11" "${WLR_PACKAGE_VERSION}" "${DESCRIPTION}" "${EXTRA_LINK_FLAGS}" || exit 1 |
| 128 | + |
| 129 | + WLR_PACKAGE_LIB_EXTRA_DIRS=usr wlr_package_lib || exit 1 |
| 130 | +fi |
| 131 | + |
| 132 | +logStatus "DONE. Artifacts in ${WLR_OUTPUT}" |
0 commit comments