Skip to content

Commit a0db7cb

Browse files
[WASM] Refactoring build script for debuggability
1 parent 93df1b9 commit a0db7cb

File tree

5 files changed

+86
-76
lines changed

5 files changed

+86
-76
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ jobs:
4545
- name: Build Linux installable archive
4646
run: |
4747
./utils/webassembly/ci.sh
48-
echo "Cleanup build directory to free disk space"
49-
rm -rf ../build
5048
- name: Upload Linux installable archive
5149
uses: actions/upload-artifact@v1
5250
with:
@@ -90,7 +88,7 @@ jobs:
9088
name: macos-installable
9189
path: ../swift-wasm-DEVELOPMENT-SNAPSHOT-osx.tar.gz
9290
- name: Pack test results
93-
run: tar cJf swift-test-results.tar.gz ../build/*/swift-macosx-x86_64/swift-test-results
91+
run: tar cJf swift-test-results.tar.gz ../target-build/*/swift-macosx-x86_64/swift-test-results
9492
- name: Upload test results
9593
uses: actions/upload-artifact@v1
9694
with:

utils/webassembly/build-foundation.sh

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ set -ex
33
DESTINATION_TOOLCHAIN=$1
44
SOURCE_PATH="$(cd "$(dirname $0)/../../.." && pwd)"
55

6-
# Remove host CoreFoundation (which can be different from the target) headers
7-
# to avoid shadowing the wasm32 target CoreFoundation
8-
rm -rf $DESTINATION_TOOLCHAIN/usr/lib/swift/CoreFoundation
9-
10-
FOUNDATION_BUILD="$SOURCE_PATH/build/Ninja-ReleaseAssert/foundation-wasi-wasm32"
6+
FOUNDATION_BUILD="$SOURCE_PATH/target-build/Ninja-ReleaseAssert/foundation-wasi-wasm32"
117

128
mkdir -p $FOUNDATION_BUILD
139
cd $FOUNDATION_BUILD
@@ -19,22 +15,12 @@ cmake -G Ninja \
1915
-DWASI_SDK_PATH="$SOURCE_PATH/wasi-sdk" \
2016
-DICU_ROOT="$SOURCE_PATH/icu_out" \
2117
-DBUILD_SHARED_LIBS=OFF \
18+
-DCMAKE_Swift_COMPILER_FORCED=ON \
2219
"${SOURCE_PATH}/swift-corelibs-foundation"
2320

2421
ninja -v
2522
ninja -v install
2623

27-
# On macOS the target CoreFoundation shadows the CoreFoundation suppplied by Xcode.
28-
# On Linux though there's no system CoreFoundation, its headers have to be shipped
29-
# in the installable archive and serve for both host and target.
30-
if [[ "$(uname)" == "Darwin" ]]; then
31-
mv $DESTINATION_TOOLCHAIN/usr/lib/swift_static/CoreFoundation \
32-
$DESTINATION_TOOLCHAIN/usr/lib/swift/wasi/wasm32/CoreFoundation
33-
else
34-
mv $DESTINATION_TOOLCHAIN/usr/lib/swift_static/CoreFoundation \
35-
$DESTINATION_TOOLCHAIN/usr/lib/swift/CoreFoundation
36-
fi
37-
3824
# .swiftdoc and .swiftmodule files should live in `swift`, not in `swift_static`
3925
mv $DESTINATION_TOOLCHAIN/usr/lib/swift_static/wasi/wasm32/Foundation.swift* \
40-
$DESTINATION_TOOLCHAIN/usr/lib/swift/wasi/wasm32
26+
$DESTINATION_TOOLCHAIN/usr/lib/swift/wasi/wasm32

utils/webassembly/build-toolchain.sh

Lines changed: 76 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ case $(uname -s) in
1111
OS_SUFFIX=osx
1212
HOST_PRESET=webassembly-host
1313
TARGET_PRESET=webassembly-macos-target
14+
HOST_SUFFIX=macosx-x86_64
1415
;;
1516
Linux)
1617
OS_SUFFIX=linux
1718
HOST_PRESET=webassembly-linux-host
1819
TARGET_PRESET=webassembly-linux-target
20+
HOST_SUFFIX=linux-x86_64
1921
;;
2022
*)
2123
echo "Unrecognised platform $(uname -s)"
@@ -29,69 +31,92 @@ DAY=$(date +"%d")
2931
TOOLCHAIN_VERSION="${YEAR}${MONTH}${DAY}"
3032
TOOLCHAIN_NAME="swift-wasm-DEVELOPMENT-SNAPSHOT-${YEAR}-${MONTH}-${DAY}-a"
3133
ARCHIVE="${TOOLCHAIN_NAME}-${OS_SUFFIX}.tar.gz"
32-
INSTALLABLE_PACKAGE=$SOURCE_PATH/$ARCHIVE
3334

3435
PACKAGE_ARTIFACT="$SOURCE_PATH/swift-wasm-DEVELOPMENT-SNAPSHOT-${OS_SUFFIX}.tar.gz"
3536

3637
BUNDLE_IDENTIFIER="swiftwasm.${YEAR}${MONTH}${DAY}"
3738
DISPLAY_NAME_SHORT="Swift for WebAssembly Development Snapshot"
3839
DISPLAY_NAME="${DISPLAY_NAME_SHORT} ${YEAR}-${MONTH}-${DAY}"
3940

41+
DIST_TOOLCHAIN_DESTDIR=$SOURCE_PATH/dist-toolchain-sdk
4042
HOST_TOOLCHAIN_DESTDIR=$SOURCE_PATH/host-toolchain-sdk
43+
TARGET_TOOLCHAIN_DESTDIR=$SOURCE_PATH/target-toolchain-sdk
44+
45+
DIST_TOOLCHAIN_SDK=$DIST_TOOLCHAIN_DESTDIR/$TOOLCHAIN_NAME
4146
HOST_TOOLCHAIN_SDK=$HOST_TOOLCHAIN_DESTDIR/$TOOLCHAIN_NAME
47+
TARGET_TOOLCHAIN_SDK=$TARGET_TOOLCHAIN_DESTDIR/$TOOLCHAIN_NAME
48+
4249

43-
BUILD_DIR=$SOURCE_PATH/build/Ninja-ReleaseAssert
50+
HOST_BUILD_ROOT=$SOURCE_PATH/host-build
51+
TARGET_BUILD_ROOT=$SOURCE_PATH/target-build
52+
HOST_BUILD_DIR=$HOST_BUILD_ROOT/Ninja-ReleaseAssert
53+
TARGET_BUILD_DIR=$TARGET_BUILD_ROOT/Ninja-ReleaseAssert
4454

4555
# Avoid clang headers symlink issues
4656
mkdir -p $HOST_TOOLCHAIN_SDK/usr/lib/clang/10.0.0
4757

48-
# Build the host toolchain and SDK first.
49-
$SOURCE_PATH/swift/utils/build-script \
50-
--preset=$HOST_PRESET \
51-
INSTALL_DESTDIR="$HOST_TOOLCHAIN_DESTDIR" \
52-
TOOLCHAIN_NAME="$TOOLCHAIN_NAME" \
53-
C_CXX_LAUNCHER="$(which sccache)"
54-
55-
# Clean up the host toolchain build directory so that the next
56-
# `build-script` invocation doesn't pick up wrong CMake config files.
57-
# For some reason passing `--reconfigure` to `build-script` won't do this.
58-
rm -rf $BUILD_DIR/swift-*
59-
# Clean up compiler-rt dir to cross compile it for host and wasm32
60-
(cd $BUILD_DIR/llvm-* && ninja compiler-rt-clear)
61-
62-
# build the cross-compilled toolchain
63-
$SOURCE_PATH/swift/utils/build-script \
64-
--preset=$TARGET_PRESET --reconfigure \
65-
INSTALL_DESTDIR="$SOURCE_PATH/install" \
66-
SOURCE_PATH="$SOURCE_PATH" \
67-
BUNDLE_IDENTIFIER="${BUNDLE_IDENTIFIER}" \
68-
DISPLAY_NAME="${DISPLAY_NAME}" \
69-
DISPLAY_NAME_SHORT="${DISPLAY_NAME_SHORT}" \
70-
TOOLCHAIN_NAME="${TOOLCHAIN_NAME}" \
71-
TOOLCHAIN_VERSION="${TOOLCHAIN_VERSION}" \
72-
TOOLS_BIN_DIR="${HOST_TOOLCHAIN_SDK}/usr/bin" \
73-
C_CXX_LAUNCHER="$(which sccache)"
74-
75-
# Merge wasi-sdk and the toolchain
76-
cp -r $WASI_SDK_PATH/share/wasi-sysroot $HOST_TOOLCHAIN_SDK/usr/share
77-
78-
# Replace absolute sysroot path with relative path
79-
sed -i -e "s@\".*/include@\"../../../../share/wasi-sysroot/include@g" $SOURCE_PATH/install/$TOOLCHAIN_NAME/usr/lib/swift/wasi/wasm32/glibc.modulemap
80-
81-
# Copy the target environment stdlib into the toolchain
82-
83-
# Avoid copying usr/lib/swift/clang because our toolchain's one is a directory
84-
# but nightly's one is symbolic link. A simple copy fails to merge them.
85-
rsync -v -a $SOURCE_PATH/install/$TOOLCHAIN_NAME/usr/lib/ $HOST_TOOLCHAIN_SDK/usr/lib/ --exclude 'swift/clang'
86-
rsync -v -a $SOURCE_PATH/install/$TOOLCHAIN_NAME/usr/bin/ $HOST_TOOLCHAIN_SDK/usr/bin/
87-
88-
$UTILS_PATH/build-foundation.sh $HOST_TOOLCHAIN_SDK
89-
$UTILS_PATH/build-xctest.sh $HOST_TOOLCHAIN_SDK
90-
91-
# Cleanup build directory on Linux CI
92-
if [[ -n "${CI}" && "$(uname)" == "Linux" ]]; then
93-
rm -rf $BUILD_DIR/foundation-* $BUILD_DIR/swiftpm-*
94-
fi
95-
96-
cd $HOST_TOOLCHAIN_DESTDIR
58+
build_host_toolchain() {
59+
# Build the host toolchain and SDK first.
60+
env SWIFT_BUILD_ROOT="$HOST_BUILD_ROOT" \
61+
$SOURCE_PATH/swift/utils/build-script \
62+
--preset=$HOST_PRESET \
63+
--build-dir="$HOST_BUILD_DIR" \
64+
INSTALL_DESTDIR="$HOST_TOOLCHAIN_DESTDIR" \
65+
TOOLCHAIN_NAME="$TOOLCHAIN_NAME" \
66+
C_CXX_LAUNCHER="$(which sccache)"
67+
}
68+
69+
build_target_toolchain() {
70+
mkdir -p $HOST_BUILD_DIR/
71+
# Copy the host build dir to reuse it.
72+
if [[ ! -e "$HOST_BUILD_DIR/llvm-$HOST_SUFFIX" ]]; then
73+
cp -r "$HOST_BUILD_DIR/llvm-$HOST_SUFFIX" "$TARGET_BUILD_DIR/llvm-$HOST_SUFFIX"
74+
# Clean up compiler-rt dir to cross compile it for host and wasm32
75+
(cd "$TARGET_BUILD_DIR/llvm-$HOST_SUFFIX" && ninja compiler-rt-clear)
76+
fi
77+
78+
# build the cross-compilled toolchain
79+
env SWIFT_BUILD_ROOT="$TARGET_BUILD_ROOT" \
80+
$SOURCE_PATH/swift/utils/build-script \
81+
--preset=$TARGET_PRESET --reconfigure \
82+
--build-dir="$TARGET_BUILD_DIR" \
83+
INSTALL_DESTDIR="$TARGET_TOOLCHAIN_DESTDIR" \
84+
SOURCE_PATH="$SOURCE_PATH" \
85+
BUNDLE_IDENTIFIER="${BUNDLE_IDENTIFIER}" \
86+
DISPLAY_NAME="${DISPLAY_NAME}" \
87+
DISPLAY_NAME_SHORT="${DISPLAY_NAME_SHORT}" \
88+
TOOLCHAIN_NAME="${TOOLCHAIN_NAME}" \
89+
TOOLCHAIN_VERSION="${TOOLCHAIN_VERSION}" \
90+
TOOLS_BIN_DIR="${HOST_TOOLCHAIN_SDK}/usr/bin" \
91+
C_CXX_LAUNCHER="$(which sccache)"
92+
93+
$UTILS_PATH/build-foundation.sh $TARGET_TOOLCHAIN_SDK
94+
$UTILS_PATH/build-xctest.sh $TARGET_TOOLCHAIN_SDK
95+
96+
}
97+
98+
merge_toolchains() {
99+
rm -rf "$DIST_TOOLCHAIN_DESTDIR"
100+
# Copy the base host toolchain
101+
cp -r "$HOST_TOOLCHAIN_DESTDIR" "$DIST_TOOLCHAIN_DESTDIR"
102+
103+
# Merge wasi-sdk and the toolchain
104+
cp -r $WASI_SDK_PATH/share/wasi-sysroot $DIST_TOOLCHAIN_SDK/usr/share
105+
106+
# Copy the target environment stdlib into the toolchain
107+
# Avoid copying usr/lib/swift/clang because our toolchain's one is a directory
108+
# but nightly's one is symbolic link. A simple copy fails to merge them.
109+
rsync -v -a $TARGET_TOOLCHAIN_SDK/usr/lib/ $DIST_TOOLCHAIN_SDK/usr/lib/ --exclude 'swift/clang'
110+
rsync -v -a $TARGET_TOOLCHAIN_SDK/usr/bin/ $DIST_TOOLCHAIN_SDK/usr/bin/
111+
112+
# Replace absolute sysroot path with relative path
113+
sed -i -e "s@\".*/include@\"../../../../share/wasi-sysroot/include@g" $DIST_TOOLCHAIN_SDK/usr/lib/swift/wasi/wasm32/glibc.modulemap
114+
}
115+
116+
build_host_toolchain
117+
build_target_toolchain
118+
119+
merge_toolchains
120+
121+
cd $DIST_TOOLCHAIN_DESTDIR
97122
tar cfz $PACKAGE_ARTIFACT $TOOLCHAIN_NAME

utils/webassembly/build-xctest.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set -ex
33
DESTINATION_TOOLCHAIN=$1
44
SOURCE_PATH="$(cd "$(dirname $0)/../../.." && pwd)"
55

6-
BUILD_DIR="$SOURCE_PATH/build/Ninja-ReleaseAssert/xctest-wasi-wasm32"
6+
BUILD_DIR="$SOURCE_PATH/target-build/Ninja-ReleaseAssert/xctest-wasi-wasm32"
77

88
mkdir -p $BUILD_DIR
99
cd $BUILD_DIR
@@ -14,6 +14,7 @@ cmake -G Ninja \
1414
-DCMAKE_TOOLCHAIN_FILE="$SOURCE_PATH/swift/utils/webassembly/toolchain-wasi.cmake" \
1515
-DWASI_SDK_PATH="$SOURCE_PATH/wasi-sdk" \
1616
-DBUILD_SHARED_LIBS=OFF \
17+
-DCMAKE_Swift_COMPILER_FORCED=ON \
1718
-DSWIFT_FOUNDATION_PATH=$DESTINATION_TOOLCHAIN/usr/lib/swift/wasi/wasm32 \
1819
"${SOURCE_PATH}/swift-corelibs-xctest"
1920

utils/webassembly/ci.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fi
1313

1414
BUILD_SCRIPT=$UTILS_PATH/build-toolchain.sh
1515
RUN_TEST_BIN=$SWIFT_PATH/utils/run-test
16-
BUILD_DIR=$SOURCE_PATH/build/Ninja-ReleaseAssert
16+
TARGET_BUILD_DIR=$SOURCE_PATH/target-build/Ninja-ReleaseAssert
1717

1818
$DEPENDENCIES_SCRIPT
1919

@@ -26,15 +26,15 @@ $BUILD_SCRIPT
2626

2727
if [[ "$(uname)" == "Darwin" ]]; then
2828
# workaround: host target test directory is necessary to use run-test
29-
mkdir -p $BUILD_DIR/swift-macosx-x86_64/test-macosx-x86_64
29+
mkdir -p $TARGET_BUILD_DIR/swift-macosx-x86_64/test-macosx-x86_64
3030
fi
3131

3232
if [[ "$(uname)" == "Linux" ]]; then
33-
$RUN_TEST_BIN --build-dir $BUILD_DIR --target wasi-wasm32 test/stdlib/ || true
33+
$RUN_TEST_BIN --build-dir $TARGET_BUILD_DIR --target wasi-wasm32 test/stdlib/ || true
3434
echo "Skip running test suites for Linux"
3535
else
3636

37-
$RUN_TEST_BIN --build-dir $BUILD_DIR --target wasi-wasm32 test/stdlib/
37+
$RUN_TEST_BIN --build-dir $TARGET_BUILD_DIR --target wasi-wasm32 test/stdlib/
3838

3939
# Run test but ignore failure temporarily
4040
$BUILD_SCRIPT -t || true

0 commit comments

Comments
 (0)