Skip to content

Commit cf3fa4c

Browse files
committed
Add a wasi target
1 parent 88f755f commit cf3fa4c

39 files changed

+2529
-193
lines changed

Cargo.lock

Lines changed: 83 additions & 83 deletions
Large diffs are not rendered by default.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,8 @@ rustc-workspace-hack = { path = 'src/tools/rustc-workspace-hack' }
6868
# here
6969
rustc-std-workspace-core = { path = 'src/tools/rustc-std-workspace-core' }
7070

71+
libc = { git = 'https://github.com/alexcrichton/libc', branch = 'wasi' }
72+
compiler_builtins = { git = 'https://github.com/alexcrichton/compiler-builtins', branch = 'wasi' }
73+
7174
[patch."https://github.com/rust-lang/rust-clippy"]
7275
clippy_lints = { path = "src/tools/clippy/clippy_lints" }

config.toml.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,9 @@
478478
# linked binaries
479479
#musl-root = "..."
480480

481+
# The root location of the `wasm32-unknown-wasi` sysroot.
482+
#wasi-root = "..."
483+
481484
# Used in testing for configuring where the QEMU images are located, you
482485
# probably don't want to use this.
483486
#qemu-rootfs = "..."

src/bootstrap/bin/rustc.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ fn main() {
116116
cmd.arg("-Cprefer-dynamic");
117117
}
118118

119-
// Help the libc crate compile by assisting it in finding the MUSL
120-
// native libraries.
119+
// Help the libc crate compile by assisting it in finding various
120+
// sysroot native libraries.
121121
if let Some(s) = env::var_os("MUSL_ROOT") {
122122
if target.contains("musl") {
123123
let mut root = OsString::from("native=");
@@ -126,6 +126,12 @@ fn main() {
126126
cmd.arg("-L").arg(&root);
127127
}
128128
}
129+
if let Some(s) = env::var_os("WASI_ROOT") {
130+
let mut root = OsString::from("native=");
131+
root.push(&s);
132+
root.push("/lib/wasm32-wasi");
133+
cmd.arg("-L").arg(&root);
134+
}
129135

130136
// Override linker if necessary.
131137
if let Ok(target_linker) = env::var("RUSTC_TARGET_LINKER") {

src/bootstrap/compile.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ fn copy_third_party_objects(builder: &Builder<'_>, compiler: &Compiler, target:
127127
&libdir.join(obj),
128128
);
129129
}
130+
} else if target.contains("-wasi") {
131+
for &obj in &["crt1.o"] {
132+
builder.copy(
133+
&builder.wasi_root(target).unwrap().join("lib/wasm32-wasi").join(obj),
134+
&libdir.join(obj),
135+
);
136+
}
130137
}
131138

132139
// Copies libunwind.a compiled to be linked wit x86_64-fortanix-unknown-sgx.
@@ -188,6 +195,12 @@ pub fn std_cargo(builder: &Builder<'_>,
188195
cargo.env("MUSL_ROOT", p);
189196
}
190197
}
198+
199+
if target.contains("-wasi") {
200+
if let Some(p) = builder.wasi_root(target) {
201+
cargo.env("WASI_ROOT", p);
202+
}
203+
}
191204
}
192205
}
193206

src/bootstrap/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ pub struct Target {
170170
pub ndk: Option<PathBuf>,
171171
pub crt_static: Option<bool>,
172172
pub musl_root: Option<PathBuf>,
173+
pub wasi_root: Option<PathBuf>,
173174
pub qemu_rootfs: Option<PathBuf>,
174175
pub no_std: bool,
175176
}
@@ -346,6 +347,7 @@ struct TomlTarget {
346347
android_ndk: Option<String>,
347348
crt_static: Option<bool>,
348349
musl_root: Option<String>,
350+
wasi_root: Option<String>,
349351
qemu_rootfs: Option<String>,
350352
}
351353

@@ -609,6 +611,7 @@ impl Config {
609611
target.linker = cfg.linker.clone().map(PathBuf::from);
610612
target.crt_static = cfg.crt_static.clone();
611613
target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
614+
target.wasi_root = cfg.wasi_root.clone().map(PathBuf::from);
612615
target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
613616

614617
config.target_config.insert(INTERNER.intern_string(triple.clone()), target);

src/bootstrap/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,13 @@ impl Build {
843843
.map(|p| &**p)
844844
}
845845

846+
/// Returns the sysroot for the wasi target, if defined
847+
fn wasi_root(&self, target: Interned<String>) -> Option<&Path> {
848+
self.config.target_config.get(&target)
849+
.and_then(|t| t.wasi_root.as_ref())
850+
.map(|p| &**p)
851+
}
852+
846853
/// Returns `true` if this is a no-std `target`, if defined
847854
fn no_std(&self, target: Interned<String>) -> Option<bool> {
848855
self.config.target_config.get(&target)

src/ci/docker/dist-various-2/Dockerfile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ COPY dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh /tmp/
3434
# Any update to the commit id here, should cause the container image to be re-built from this point on.
3535
RUN /tmp/build-x86_64-fortanix-unknown-sgx-toolchain.sh "53b586346f2c7870e20b170decdc30729d97c42b"
3636

37+
COPY dist-various-2/build-wasi-toolchain.sh /tmp/
38+
# FIXME: remove this line and uncomment the git clone
39+
COPY dist-various-2/reference-sysroot-wasi/ /tmp/wut/
40+
RUN /tmp/build-wasi-toolchain.sh
41+
3742
COPY scripts/sccache.sh /scripts/
3843
RUN sh /scripts/sccache.sh
3944

@@ -66,6 +71,7 @@ ENV TARGETS=x86_64-fuchsia
6671
ENV TARGETS=$TARGETS,aarch64-fuchsia
6772
ENV TARGETS=$TARGETS,sparcv9-sun-solaris
6873
ENV TARGETS=$TARGETS,wasm32-unknown-unknown
74+
ENV TARGETS=$TARGETS,wasm32-unknown-wasi
6975
ENV TARGETS=$TARGETS,x86_64-sun-solaris
7076
ENV TARGETS=$TARGETS,x86_64-unknown-linux-gnux32
7177
ENV TARGETS=$TARGETS,x86_64-unknown-cloudabi
@@ -74,5 +80,6 @@ ENV TARGETS=$TARGETS,nvptx64-nvidia-cuda
7480

7581
ENV X86_FORTANIX_SGX_LIBS="/x86_64-fortanix-unknown-sgx/lib/"
7682

77-
ENV RUST_CONFIGURE_ARGS --enable-extended --enable-lld --disable-docs
83+
ENV RUST_CONFIGURE_ARGS --enable-extended --enable-lld --disable-docs \
84+
--set target.wasm32-unknown-wasi.wasi-root=/wasm32-unknown-wasi
7885
ENV SCRIPT python2.7 ../x.py dist --target $TARGETS
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sh
2+
3+
set -ex
4+
5+
curl https://prereleases.llvm.org/8.0.0/rc3/clang+llvm-8.0.0-rc3-x86_64-linux-gnu-ubuntu-14.04.tar.xz | \
6+
tar xJf -
7+
export PATH=`pwd`/clang+llvm-8.0.0-rc3-x86_64-linux-gnu-ubuntu-14.04/bin:$PATH
8+
9+
# FIXME: uncomment this line and remove the next
10+
#git clone https://github.com/cranestation/reference-sysroot-wasi
11+
git clone /tmp/wut reference-sysroot-wasi
12+
13+
cd reference-sysroot-wasi
14+
git reset --hard d0d8bc47946948646cb50a37471d9516c90d9786
15+
make -j$(nproc) INSTALL_DIR=/wasm32-unknown-wasi
16+
make install INSTALL_DIR=/wasm32-unknown-wasi
17+
18+
cd ..
19+
rm -rf reference-sysroot-wasi
20+
rm -rf clang+llvm*
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit d0d8bc47946948646cb50a37471d9516c90d9786

0 commit comments

Comments
 (0)