Skip to content

Commit

Permalink
Add a wasi target
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Mar 13, 2019
1 parent 88f755f commit cf3fa4c
Show file tree
Hide file tree
Showing 39 changed files with 2,529 additions and 193 deletions.
166 changes: 83 additions & 83 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,8 @@ rustc-workspace-hack = { path = 'src/tools/rustc-workspace-hack' }
# here
rustc-std-workspace-core = { path = 'src/tools/rustc-std-workspace-core' }

libc = { git = 'https://github.com/alexcrichton/libc', branch = 'wasi' }
compiler_builtins = { git = 'https://github.com/alexcrichton/compiler-builtins', branch = 'wasi' }

[patch."https://github.com/rust-lang/rust-clippy"]
clippy_lints = { path = "src/tools/clippy/clippy_lints" }
3 changes: 3 additions & 0 deletions config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,9 @@
# linked binaries
#musl-root = "..."

# The root location of the `wasm32-unknown-wasi` sysroot.
#wasi-root = "..."

# Used in testing for configuring where the QEMU images are located, you
# probably don't want to use this.
#qemu-rootfs = "..."
Expand Down
10 changes: 8 additions & 2 deletions src/bootstrap/bin/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ fn main() {
cmd.arg("-Cprefer-dynamic");
}

// Help the libc crate compile by assisting it in finding the MUSL
// native libraries.
// Help the libc crate compile by assisting it in finding various
// sysroot native libraries.
if let Some(s) = env::var_os("MUSL_ROOT") {
if target.contains("musl") {
let mut root = OsString::from("native=");
Expand All @@ -126,6 +126,12 @@ fn main() {
cmd.arg("-L").arg(&root);
}
}
if let Some(s) = env::var_os("WASI_ROOT") {
let mut root = OsString::from("native=");
root.push(&s);
root.push("/lib/wasm32-wasi");
cmd.arg("-L").arg(&root);
}

// Override linker if necessary.
if let Ok(target_linker) = env::var("RUSTC_TARGET_LINKER") {
Expand Down
13 changes: 13 additions & 0 deletions src/bootstrap/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ fn copy_third_party_objects(builder: &Builder<'_>, compiler: &Compiler, target:
&libdir.join(obj),
);
}
} else if target.contains("-wasi") {
for &obj in &["crt1.o"] {
builder.copy(
&builder.wasi_root(target).unwrap().join("lib/wasm32-wasi").join(obj),
&libdir.join(obj),
);
}
}

// Copies libunwind.a compiled to be linked wit x86_64-fortanix-unknown-sgx.
Expand Down Expand Up @@ -188,6 +195,12 @@ pub fn std_cargo(builder: &Builder<'_>,
cargo.env("MUSL_ROOT", p);
}
}

if target.contains("-wasi") {
if let Some(p) = builder.wasi_root(target) {
cargo.env("WASI_ROOT", p);
}
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ pub struct Target {
pub ndk: Option<PathBuf>,
pub crt_static: Option<bool>,
pub musl_root: Option<PathBuf>,
pub wasi_root: Option<PathBuf>,
pub qemu_rootfs: Option<PathBuf>,
pub no_std: bool,
}
Expand Down Expand Up @@ -346,6 +347,7 @@ struct TomlTarget {
android_ndk: Option<String>,
crt_static: Option<bool>,
musl_root: Option<String>,
wasi_root: Option<String>,
qemu_rootfs: Option<String>,
}

Expand Down Expand Up @@ -609,6 +611,7 @@ impl Config {
target.linker = cfg.linker.clone().map(PathBuf::from);
target.crt_static = cfg.crt_static.clone();
target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
target.wasi_root = cfg.wasi_root.clone().map(PathBuf::from);
target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);

config.target_config.insert(INTERNER.intern_string(triple.clone()), target);
Expand Down
7 changes: 7 additions & 0 deletions src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,13 @@ impl Build {
.map(|p| &**p)
}

/// Returns the sysroot for the wasi target, if defined
fn wasi_root(&self, target: Interned<String>) -> Option<&Path> {
self.config.target_config.get(&target)
.and_then(|t| t.wasi_root.as_ref())
.map(|p| &**p)
}

/// Returns `true` if this is a no-std `target`, if defined
fn no_std(&self, target: Interned<String>) -> Option<bool> {
self.config.target_config.get(&target)
Expand Down
9 changes: 8 additions & 1 deletion src/ci/docker/dist-various-2/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ COPY dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh /tmp/
# Any update to the commit id here, should cause the container image to be re-built from this point on.
RUN /tmp/build-x86_64-fortanix-unknown-sgx-toolchain.sh "53b586346f2c7870e20b170decdc30729d97c42b"

COPY dist-various-2/build-wasi-toolchain.sh /tmp/
# FIXME: remove this line and uncomment the git clone
COPY dist-various-2/reference-sysroot-wasi/ /tmp/wut/
RUN /tmp/build-wasi-toolchain.sh

COPY scripts/sccache.sh /scripts/
RUN sh /scripts/sccache.sh

Expand Down Expand Up @@ -66,6 +71,7 @@ ENV TARGETS=x86_64-fuchsia
ENV TARGETS=$TARGETS,aarch64-fuchsia
ENV TARGETS=$TARGETS,sparcv9-sun-solaris
ENV TARGETS=$TARGETS,wasm32-unknown-unknown
ENV TARGETS=$TARGETS,wasm32-unknown-wasi
ENV TARGETS=$TARGETS,x86_64-sun-solaris
ENV TARGETS=$TARGETS,x86_64-unknown-linux-gnux32
ENV TARGETS=$TARGETS,x86_64-unknown-cloudabi
Expand All @@ -74,5 +80,6 @@ ENV TARGETS=$TARGETS,nvptx64-nvidia-cuda

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

ENV RUST_CONFIGURE_ARGS --enable-extended --enable-lld --disable-docs
ENV RUST_CONFIGURE_ARGS --enable-extended --enable-lld --disable-docs \
--set target.wasm32-unknown-wasi.wasi-root=/wasm32-unknown-wasi
ENV SCRIPT python2.7 ../x.py dist --target $TARGETS
20 changes: 20 additions & 0 deletions src/ci/docker/dist-various-2/build-wasi-toolchain.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

set -ex

curl https://prereleases.llvm.org/8.0.0/rc3/clang+llvm-8.0.0-rc3-x86_64-linux-gnu-ubuntu-14.04.tar.xz | \
tar xJf -
export PATH=`pwd`/clang+llvm-8.0.0-rc3-x86_64-linux-gnu-ubuntu-14.04/bin:$PATH

# FIXME: uncomment this line and remove the next
#git clone https://github.com/cranestation/reference-sysroot-wasi
git clone /tmp/wut reference-sysroot-wasi

cd reference-sysroot-wasi
git reset --hard d0d8bc47946948646cb50a37471d9516c90d9786
make -j$(nproc) INSTALL_DIR=/wasm32-unknown-wasi
make install INSTALL_DIR=/wasm32-unknown-wasi

cd ..
rm -rf reference-sysroot-wasi
rm -rf clang+llvm*
1 change: 1 addition & 0 deletions src/ci/docker/dist-various-2/reference-sysroot-wasi
Submodule reference-sysroot-wasi added at d0d8bc
19 changes: 11 additions & 8 deletions src/librustc_codegen_ssa/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,20 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
LinkerFlavor::PtxLinker => "rust-ptx-linker",
}), flavor)),
(Some(linker), None) => {
let stem = if linker.extension().and_then(|ext| ext.to_str()) == Some("exe") {
linker.file_stem().and_then(|stem| stem.to_str())
} else {
linker.to_str()
}.unwrap_or_else(|| {
sess.fatal("couldn't extract file stem from specified linker");
}).to_owned();
let stem = linker
.file_stem()
.and_then(|stem| stem.to_str())
.unwrap_or_else(|| {
sess.fatal("couldn't extract file stem from specified linker");
}).to_owned();

let flavor = if stem == "emcc" {
LinkerFlavor::Em
} else if stem == "gcc" || stem.ends_with("-gcc") {
} else if stem == "gcc"
|| stem.ends_with("-gcc")
|| stem == "clang"
|| stem.ends_with("-clang")
{
LinkerFlavor::Gcc
} else if stem == "ld" || stem == "ld.lld" || stem.ends_with("-ld") {
LinkerFlavor::Ld
Expand Down
68 changes: 12 additions & 56 deletions src/librustc_codegen_ssa/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ impl<'a> GccLinker<'a> {
}

fn takes_hints(&self) -> bool {
!self.sess.target.target.options.is_like_osx
!self.sess.target.target.options.is_like_osx &&
self.sess.target.target.arch != "wasm32"
}

// Some platforms take hints about whether a library is static or dynamic.
Expand Down Expand Up @@ -375,6 +376,12 @@ impl<'a> Linker for GccLinker<'a> {
return
}

// Symbol visibility takes care of this. Additionally LLD doesn't
// support the script arguments just yet
if self.sess.target.target.arch == "wasm32" {
return;
}

let mut arg = OsString::new();
let path = tmpdir.join("list");

Expand Down Expand Up @@ -443,13 +450,13 @@ impl<'a> Linker for GccLinker<'a> {
}

fn group_start(&mut self) {
if !self.sess.target.target.options.is_like_osx {
if self.takes_hints() {
self.linker_arg("--start-group");
}
}

fn group_end(&mut self) {
if !self.sess.target.target.options.is_like_osx {
if self.takes_hints() {
self.linker_arg("--end-group");
}
}
Expand Down Expand Up @@ -877,59 +884,7 @@ pub struct WasmLd<'a> {
}

impl<'a> WasmLd<'a> {
fn new(mut cmd: Command, sess: &'a Session, info: &'a LinkerInfo) -> WasmLd<'a> {
// There have been reports in the wild (rustwasm/wasm-bindgen#119) of
// using threads causing weird hangs and bugs. Disable it entirely as
// this isn't yet the bottleneck of compilation at all anyway.
cmd.arg("--no-threads");

// By default LLD only gives us one page of stack (64k) which is a
// little small. Default to a larger stack closer to other PC platforms
// (1MB) and users can always inject their own link-args to override this.
cmd.arg("-z").arg("stack-size=1048576");

// By default LLD's memory layout is:
//
// 1. First, a blank page
// 2. Next, all static data
// 3. Finally, the main stack (which grows down)
//
// This has the unfortunate consequence that on stack overflows you
// corrupt static data and can cause some exceedingly weird bugs. To
// help detect this a little sooner we instead request that the stack is
// placed before static data.
//
// This means that we'll generate slightly larger binaries as references
// to static data will take more bytes in the ULEB128 encoding, but
// stack overflow will be guaranteed to trap as it underflows instead of
// corrupting static data.
cmd.arg("--stack-first");

// FIXME we probably shouldn't pass this but instead pass an explicit
// whitelist of symbols we'll allow to be undefined. Unfortunately
// though we can't handle symbols like `log10` that LLVM injects at a
// super late date without actually parsing object files. For now let's
// stick to this and hopefully fix it before stabilization happens.
cmd.arg("--allow-undefined");

// For now we just never have an entry symbol
cmd.arg("--no-entry");

// Rust code should never have warnings, and warnings are often
// indicative of bugs, let's prevent them.
cmd.arg("--fatal-warnings");

// The symbol visibility story is a bit in flux right now with LLD.
// It's... not entirely clear to me what's going on, but this looks to
// make everything work when `export_symbols` isn't otherwise called for
// things like executables.
cmd.arg("--export-dynamic");

// LLD only implements C++-like demangling, which doesn't match our own
// mangling scheme. Tell LLD to not demangle anything and leave it up to
// us to demangle these symbols later.
cmd.arg("--no-demangle");

fn new(cmd: Command, sess: &'a Session, info: &'a LinkerInfo) -> WasmLd<'a> {
WasmLd { cmd, sess, info }
}
}
Expand Down Expand Up @@ -1025,6 +980,7 @@ impl<'a> Linker for WasmLd<'a> {
}

fn build_dylib(&mut self, _out_filename: &Path) {
self.cmd.arg("--no-entry");
}

fn export_symbols(&mut self, _tmpdir: &Path, crate_type: CrateType) {
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_target/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ mod l4re_base;
mod fuchsia_base;
mod redox_base;
mod riscv_base;
mod wasm32_base;

#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash,
RustcEncodable, RustcDecodable)]
Expand Down Expand Up @@ -437,6 +438,7 @@ supported_targets! {
("asmjs-unknown-emscripten", asmjs_unknown_emscripten),
("wasm32-unknown-emscripten", wasm32_unknown_emscripten),
("wasm32-unknown-unknown", wasm32_unknown_unknown),
("wasm32-unknown-wasi", wasm32_unknown_wasi),
("wasm32-experimental-emscripten", wasm32_experimental_emscripten),

("thumbv6m-none-eabi", thumbv6m_none_eabi),
Expand Down
Loading

0 comments on commit cf3fa4c

Please sign in to comment.