|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Build the Swift standard library. |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | +set -x |
| 7 | + |
| 8 | +print_help() { |
| 9 | + echo "Usage: $0 [options]" |
| 10 | + echo "" |
| 11 | + echo "Options:" |
| 12 | + echo " --help Display this help message." |
| 13 | + echo " --llvm-bin Path to LLVM bin directory." |
| 14 | + echo " --swift-bin Path to Swift bin directory." |
| 15 | +} |
| 16 | + |
| 17 | +SCHEMES_BUILD_PATH="$(cd "$(dirname "$0")" && pwd)" |
| 18 | +SOURCE_PATH="$(cd "$(dirname "$0")/../../../.." && pwd)" |
| 19 | +TARGET_BUILD_ROOT=$SOURCE_PATH/build/WebAssembly |
| 20 | +WASI_SYSROOT_PATH="$TARGET_BUILD_ROOT/wasi-sysroot" |
| 21 | +PACKAGING_DIR="$SOURCE_PATH/build/Packaging" |
| 22 | +TARGET_TOOLCHAIN_DESTDIR=$PACKAGING_DIR/target-toolchain |
| 23 | + |
| 24 | +build_target_toolchain() { |
| 25 | + |
| 26 | + local LLVM_BIN_DIR="$1" |
| 27 | + local CLANG_BIN_DIR="$2" |
| 28 | + local SWIFT_BIN_DIR="$3" |
| 29 | + local TRIPLE="$4" |
| 30 | + local SHORT_TRIPLE="$5" |
| 31 | + local CLANG_MULTIARCH_TRIPLE="$6" |
| 32 | + local STDLIB_PRODUCT="$7" |
| 33 | + local COMPILER_RT_OS_DIR="$8" |
| 34 | + |
| 35 | + local HOST_SUFFIX |
| 36 | + HOST_SUFFIX=$(find "$TARGET_BUILD_ROOT" -name "wasmstdlib-*" -exec basename {} \; | sed 's/wasmstdlib-//') |
| 37 | + |
| 38 | + local TRIPLE_DESTDIR="$TARGET_TOOLCHAIN_DESTDIR/$TRIPLE" |
| 39 | + |
| 40 | + env DESTDIR="$TRIPLE_DESTDIR" \ |
| 41 | + cmake --install "$TARGET_BUILD_ROOT/$STDLIB_PRODUCT-$HOST_SUFFIX" --prefix /usr |
| 42 | + |
| 43 | + local swift_testing_build_dir="$TARGET_BUILD_ROOT/swift-testing-$SHORT_TRIPLE" |
| 44 | + # TODO: Remove this check once we build swift-testing for +threads target |
| 45 | + if [[ -d "$swift_testing_build_dir" ]]; then |
| 46 | + env DESTDIR="$TRIPLE_DESTDIR" \ |
| 47 | + cmake --install "$swift_testing_build_dir" --prefix /usr |
| 48 | + fi |
| 49 | + |
| 50 | + rm -rf "$TRIPLE_DESTDIR/usr/lib/swift_static/clang/lib/$COMPILER_RT_OS_DIR" |
| 51 | + # XXX: Is this the right way to install compiler-rt? |
| 52 | + cp -R "$TARGET_BUILD_ROOT/wasi-sysroot/$CLANG_MULTIARCH_TRIPLE/lib/$COMPILER_RT_OS_DIR" "$TRIPLE_DESTDIR/usr/lib/swift_static/clang/lib/$COMPILER_RT_OS_DIR" |
| 53 | + |
| 54 | + # FIXME: Clang resource directory installation is not the best way currently. |
| 55 | + # We currently have two copies of compiler headers copied from the base toolchain in |
| 56 | + # lib/swift/clang and lib/swift_static/clang. This is because the Swift CMake build |
| 57 | + # system installs the compiler headers from the native tools path when not building |
| 58 | + # tools including clang compiler. This is not ideal but then where should we bring |
| 59 | + # the compiler headers from? If we use the headers beside the base toolchain, clang |
| 60 | + # driver will not be able to find libclang_rt.builtins-wasm32.a because it is not |
| 61 | + # a part of the base toolchain. We need to find a better way to handle this. |
| 62 | + local CLANG_VERSION |
| 63 | + CLANG_VERSION="$(basename "$($CLANG_BIN_DIR/clang -print-resource-dir)")" |
| 64 | + mkdir -p "$TRIPLE_DESTDIR/usr/lib/clang/$CLANG_VERSION/lib" |
| 65 | + ln -sf "../../../swift_static/clang/lib/$COMPILER_RT_OS_DIR" "$TRIPLE_DESTDIR/usr/lib/clang/$CLANG_VERSION/lib/$COMPILER_RT_OS_DIR" |
| 66 | +} |
| 67 | + |
| 68 | +build_target_corelibs() { |
| 69 | + local LLVM_BIN_DIR="$1" |
| 70 | + local CLANG_BIN_DIR="$2" |
| 71 | + local SWIFT_BIN_DIR="$3" |
| 72 | + local TRIPLE="$4" |
| 73 | + local SHORT_TRIPLE="$5" |
| 74 | + |
| 75 | + local TRIPLE_DESTDIR="$TARGET_TOOLCHAIN_DESTDIR/$TRIPLE" |
| 76 | + local CORELIBS_ARGS=( |
| 77 | + "$TRIPLE_DESTDIR" |
| 78 | + "$LLVM_BIN_DIR" |
| 79 | + "$CLANG_BIN_DIR" |
| 80 | + "$SWIFT_BIN_DIR" |
| 81 | + "$WASI_SYSROOT_PATH/$SHORT_TRIPLE" |
| 82 | + ) |
| 83 | + "$SCHEMES_BUILD_PATH/build-foundation.sh" "${CORELIBS_ARGS[@]}" "$TRIPLE" |
| 84 | + "$SCHEMES_BUILD_PATH/build-xctest.sh" "${CORELIBS_ARGS[@]}" "$TRIPLE" |
| 85 | +} |
| 86 | + |
| 87 | +main() { |
| 88 | + local OPTIONS_LLVM_BIN="" |
| 89 | + local OPTIONS_CLANG_BIN="" |
| 90 | + local OPTIONS_SWIFT_BIN="" |
| 91 | + |
| 92 | + while [[ $# -gt 0 ]]; do |
| 93 | + case "$1" in |
| 94 | + --llvm-bin) |
| 95 | + OPTIONS_LLVM_BIN="$2" |
| 96 | + shift 2 |
| 97 | + ;; |
| 98 | + --clang-bin) |
| 99 | + OPTIONS_CLANG_BIN="$2" |
| 100 | + shift 2 |
| 101 | + ;; |
| 102 | + --swift-bin) |
| 103 | + OPTIONS_SWIFT_BIN="$2" |
| 104 | + shift 2 |
| 105 | + ;; |
| 106 | + --scheme) |
| 107 | + OPTIONS_SCHEME="$2" |
| 108 | + shift 2 |
| 109 | + ;; |
| 110 | + --help) |
| 111 | + print_help |
| 112 | + exit 0 |
| 113 | + ;; |
| 114 | + *) |
| 115 | + echo "Unknown option: $1" |
| 116 | + print_help |
| 117 | + exit 1 |
| 118 | + ;; |
| 119 | + esac |
| 120 | + done |
| 121 | + |
| 122 | + if [[ -z "$OPTIONS_LLVM_BIN" ]]; then |
| 123 | + echo "Missing --llvm-bin option" |
| 124 | + print_help |
| 125 | + exit 1 |
| 126 | + fi |
| 127 | + |
| 128 | + if [[ -z "$OPTIONS_SWIFT_BIN" ]]; then |
| 129 | + echo "Missing --swift-bin option" |
| 130 | + print_help |
| 131 | + exit 1 |
| 132 | + fi |
| 133 | + |
| 134 | + if [[ -z "$OPTIONS_CLANG_BIN" ]]; then |
| 135 | + OPTIONS_CLANG_BIN="$OPTIONS_LLVM_BIN" |
| 136 | + fi |
| 137 | + |
| 138 | + # NOTE: The llvm-cmake-options is a workaround for the issue on amazonlinux2 |
| 139 | + # See https://github.com/apple/swift/commit/40c7268e8f7d402b27e3ad16a84180e07c37f92c |
| 140 | + "$SOURCE_PATH/swift/utils/build-script" \ |
| 141 | + --build-subdir=WebAssembly \ |
| 142 | + --release \ |
| 143 | + --skip-build-llvm \ |
| 144 | + --skip-build-swift \ |
| 145 | + --skip-build-cmark \ |
| 146 | + --skip-build-benchmarks \ |
| 147 | + --skip-early-swift-driver \ |
| 148 | + --build-wasm-stdlib \ |
| 149 | + --skip-test-wasm-stdlib \ |
| 150 | + --native-swift-tools-path="$OPTIONS_SWIFT_BIN" \ |
| 151 | + --native-clang-tools-path="$OPTIONS_CLANG_BIN" \ |
| 152 | + --native-llvm-tools-path="$OPTIONS_LLVM_BIN" \ |
| 153 | + --extra-cmake-options="\ |
| 154 | + -DSWIFT_STDLIB_TRACING=NO \ |
| 155 | + -DSWIFT_STDLIB_HAS_ASL=NO \ |
| 156 | + -DSWIFT_STDLIB_CONCURRENCY_TRACING=NO \ |
| 157 | + -DSWIFT_RUNTIME_CRASH_REPORTER_CLIENT=NO \ |
| 158 | + -DSWIFT_STDLIB_INSTALL_PARENT_MODULE_FOR_SHIMS=NO \ |
| 159 | + " \ |
| 160 | + --llvm-cmake-options="\ |
| 161 | + -DCROSS_TOOLCHAIN_FLAGS_LLVM_NATIVE='-DCMAKE_C_COMPILER=clang;-DCMAKE_CXX_COMPILER=clang++' \ |
| 162 | + " \ |
| 163 | + --sccache |
| 164 | + |
| 165 | + local BUILD_TOOLS_ARGS=( |
| 166 | + "$OPTIONS_LLVM_BIN" |
| 167 | + "$OPTIONS_CLANG_BIN" |
| 168 | + "$OPTIONS_SWIFT_BIN" |
| 169 | + ) |
| 170 | + |
| 171 | + build_target_toolchain "${BUILD_TOOLS_ARGS[@]}" "wasm32-unknown-wasi" "wasi-wasm32" "wasm32-wasi" "wasmstdlib" "wasi" |
| 172 | + build_target_toolchain "${BUILD_TOOLS_ARGS[@]}" "wasm32-unknown-wasip1-threads" "wasip1-threads-wasm32" "wasm32-wasip1-threads" "wasmthreadsstdlib" "wasip1" |
| 173 | + |
| 174 | + rsync -av "$WASI_SYSROOT_PATH/" "$PACKAGING_DIR/wasi-sysroot/" |
| 175 | + |
| 176 | + build_target_corelibs "${BUILD_TOOLS_ARGS[@]}" "wasm32-unknown-wasi" "wasm32-wasi" |
| 177 | + build_target_corelibs "${BUILD_TOOLS_ARGS[@]}" "wasm32-unknown-wasip1-threads" "wasm32-wasip1-threads" |
| 178 | +} |
| 179 | + |
| 180 | +main "$@" |
0 commit comments