Skip to content

Commit cbc292c

Browse files
Build with swift.org toolchain if no compiler patch is needed
1 parent 5334f0c commit cbc292c

10 files changed

+430
-167
lines changed

tools/build/build-foundation.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#!/bin/bash
22
set -ex
33
DESTINATION_TOOLCHAIN=$1
4-
WASI_SYSROOT_PATH=$2
4+
LLVM_BIN_DIR=$2
5+
CLANG_BIN_DIR=$3
6+
SWIFT_BIN_DIR=$4
7+
WASI_SYSROOT_PATH=$5
8+
59
SOURCE_PATH="$(cd "$(dirname $0)/../../.." && pwd)"
610
TOOLS_BUILD_PATH="$(cd "$(dirname "$0")" && pwd)"
711
BUILD_SDK_PATH="$SOURCE_PATH/build-sdk"
@@ -15,10 +19,11 @@ cd $FOUNDATION_BUILD
1519
cmake -G Ninja \
1620
-DCMAKE_BUILD_TYPE="Release" \
1721
-DCMAKE_SYSROOT="$WASI_SYSROOT_PATH" \
18-
-DCMAKE_Swift_COMPILER="$DESTINATION_TOOLCHAIN/usr/bin/swiftc" \
22+
-DCMAKE_Swift_COMPILER="$SWIFT_BIN_DIR/swiftc" \
1923
-DCMAKE_STAGING_PREFIX="$DESTINATION_TOOLCHAIN/usr" \
2024
-DCMAKE_TOOLCHAIN_FILE="$TOOLS_BUILD_PATH/toolchain-wasi.cmake" \
21-
-DLLVM_BIN="$DESTINATION_TOOLCHAIN/usr/bin" \
25+
-DLLVM_BIN="$LLVM_BIN_DIR" \
26+
-DCLANG_BIN="$CLANG_BIN_DIR" \
2227
-DICU_ROOT="$BUILD_SDK_PATH/icu" \
2328
-DLIBXML2_INCLUDE_DIR="$LIBXML2_PATH/include/libxml2" \
2429
-DLIBXML2_LIBRARY="$LIBXML2_PATH/lib" \
@@ -27,7 +32,7 @@ cmake -G Ninja \
2732
-DBUILD_TOOLS=OFF \
2833
-DHAS_LIBDISPATCH_API=OFF \
2934
-DCMAKE_Swift_COMPILER_FORCED=ON \
30-
-DCMAKE_Swift_FLAGS="-sdk $WASI_SYSROOT_PATH" \
35+
-DCMAKE_Swift_FLAGS="-sdk $WASI_SYSROOT_PATH -resource-dir $DESTINATION_TOOLCHAIN/usr/lib/swift_static" \
3136
"${SOURCE_PATH}/swift-corelibs-foundation"
3237

3338
ninja

tools/build/build-host-toolchain.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
set -ex
4+
SOURCE_PATH="$(cd "$(dirname "$0")/../../.." && pwd)"
5+
TOOLS_BUILD_PATH="$(cd "$(dirname "$0")" && pwd)"
6+
7+
PACKAGING_DIR="$SOURCE_PATH/build/Packaging"
8+
HOST_TOOLCHAIN_DESTDIR=$PACKAGING_DIR/host-toolchain
9+
10+
build_host_toolchain() {
11+
# Build the host toolchain and SDK first.
12+
"$SOURCE_PATH/swift/utils/build-script" \
13+
--preset-file="$TOOLS_BUILD_PATH/build-presets.ini" \
14+
--preset=webassembly-host-install \
15+
HOST_ARCHITECTURE="$(uname -m)" \
16+
INSTALL_DESTDIR="$HOST_TOOLCHAIN_DESTDIR"
17+
}
18+
19+
show_sccache_stats() {
20+
# If sccache is installed in PATH
21+
if command -v sccache &> /dev/null; then
22+
sccache --show-stats
23+
else
24+
echo "sccache is not installed in PATH"
25+
fi
26+
}
27+
28+
build_host_toolchain
29+
echo "=================================="
30+
echo "Host toolchain built successfully!"
31+
echo "=================================="
32+
echo ""
33+
echo "sccache stats:"
34+
show_sccache_stats

tools/build/build-llvm-tools

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Build missing LLVM tools required to build Swift SDK for WebAssembly.
4+
#
5+
6+
import argparse
7+
import pathlib
8+
import subprocess
9+
from typing import List, Optional
10+
11+
from build_support.actions import REPO_ROOT
12+
13+
14+
WASM_SPECIFIC_TOOLS = [
15+
'wasm-ld',
16+
'llvm-ar',
17+
'llvm-ranlib',
18+
]
19+
20+
21+
def compute_missing_tools(toolchain_path: pathlib.Path):
22+
23+
missing_tools = []
24+
for tool in WASM_SPECIFIC_TOOLS:
25+
if not (toolchain_path / 'usr' / 'bin' / tool).exists():
26+
missing_tools.append(tool)
27+
28+
return missing_tools
29+
30+
31+
def compute_build_products(toolchain_path: pathlib.Path):
32+
build_products = []
33+
missing_tools = compute_missing_tools(toolchain_path)
34+
35+
if 'llvm-ranlib' in missing_tools and \
36+
(toolchain_path / 'usr' / 'bin' / 'llvm-ar').exists():
37+
build_products.append(
38+
SymlinkTool(toolchain_path,
39+
tool_name='llvm-ranlib', target='llvm-ar'))
40+
missing_tools.remove('llvm-ranlib')
41+
42+
if len(missing_tools) > 0:
43+
build_products.append(BuildLLVMTools(toolchain_path, missing_tools))
44+
45+
# Symlink tools for existing tools
46+
for tool in WASM_SPECIFIC_TOOLS:
47+
if tool in missing_tools:
48+
continue
49+
50+
build_products.append(SymlinkTool(toolchain_path, tool))
51+
52+
return build_products
53+
54+
55+
class BuildLLVMTools:
56+
57+
def __init__(self, toolchain_path: pathlib.Path, tools: List[str]):
58+
self.toolchain_path = toolchain_path
59+
self.tools = tools
60+
61+
def configure(
62+
self, build_dir: pathlib.Path, llvm_project_dir: pathlib.Path):
63+
cmake_options = []
64+
if 'wasm-ld' in self.tools:
65+
cmake_options.append('-DLLVM_ENABLE_PROJECTS=lld')
66+
subprocess.check_call([
67+
'cmake',
68+
'-G', 'Ninja',
69+
'-DCMAKE_BUILD_TYPE=Release',
70+
'-DLLVM_TARGETS_TO_BUILD=WebAssembly',
71+
'-DCMAKE_INSTALL_PREFIX=' + str(self.toolchain_path),
72+
'-DLLVM_TOOLCHAIN_TOOLS=' + ';'.join(self.tools),
73+
llvm_project_dir / 'llvm',
74+
], cwd=build_dir)
75+
76+
def build(self, build_dir: pathlib.Path):
77+
print(f'Building LLVM tools in {build_dir}')
78+
subprocess.check_call(['ninja', 'install'], cwd=build_dir)
79+
80+
def __str__(self):
81+
return 'Build LLVM tools (' + ', '.join(self.tools) + ')'
82+
83+
84+
class SymlinkTool:
85+
86+
def __init__(
87+
self, toolchain_path: pathlib.Path,
88+
tool_name: str, target: Optional[str] = None):
89+
self.toolchain_path = toolchain_path
90+
self.tool_name = tool_name
91+
self.target = target
92+
93+
def configure(
94+
self, build_dir: pathlib.Path, llvm_project_dir: pathlib.Path):
95+
pass
96+
97+
def build(self, build_dir: pathlib.Path):
98+
bin_dir = build_dir / 'bin'
99+
bin_dir.mkdir(parents=True, exist_ok=True)
100+
target_name = self.target if self.target else self.tool_name
101+
target = self.toolchain_path / 'usr' / 'bin' / target_name
102+
tool = bin_dir / self.tool_name
103+
if not tool.exists():
104+
tool.symlink_to(target)
105+
106+
def __str__(self):
107+
if self.target:
108+
return f'Symlink {self.tool_name} to {self.target}'
109+
return 'Symlink ' + self.tool_name
110+
111+
112+
def main():
113+
parser = argparse.ArgumentParser()
114+
parser.add_argument('--toolchain', required=True)
115+
args = parser.parse_args()
116+
117+
toolchain_path = pathlib.Path(args.toolchain)
118+
missing_tools = compute_missing_tools(toolchain_path)
119+
120+
if len(missing_tools) == 0:
121+
print('All required tools are available')
122+
return
123+
124+
print('Missing tools:')
125+
for tool in missing_tools:
126+
print(f' * {tool}')
127+
128+
repo_root = pathlib.Path(REPO_ROOT)
129+
build_dir = repo_root / '..' / 'build' / 'llvm-tools'
130+
build_dir.mkdir(parents=True, exist_ok=True)
131+
132+
llvm_project_dir = repo_root / '..' / 'llvm-project'
133+
134+
products = compute_build_products(toolchain_path)
135+
for product in products:
136+
print(product)
137+
product.configure(build_dir, llvm_project_dir)
138+
product.build(build_dir)
139+
140+
141+
if __name__ == '__main__':
142+
main()

tools/build/build-presets.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ compiler-vendor=swiftwasm
1010
enable-experimental-concurrency=1
1111
enable-experimental-differentiable-programming=1
1212
enable-experimental-distributed=1
13+
build-subdir=WebAssemblyCompiler
1314

1415
[preset: webassembly-install]
1516

0 commit comments

Comments
 (0)