Skip to content

Commit

Permalink
Address some PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Mar 27, 2019
1 parent 87def1f commit 6a600c5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 55 deletions.
51 changes: 37 additions & 14 deletions ci/docker/wasm32-unknown-wasi/Dockerfile
@@ -1,4 +1,7 @@
FROM ubuntu:18.04 as reference-sysroot
# In the first container we want to assemble the `wasi-sysroot` by compiling it
# from source. This requires a clang 8.0+ compiler with enough wasm support and
# then we're just running a standard `make` inside of what we clone.
FROM ubuntu:18.04 as wasi-sysroot

RUN apt-get update && \
apt-get install -y --no-install-recommends \
Expand All @@ -14,15 +17,29 @@ RUN apt-get update && \
ssh \
xz-utils

# Fetch clang 8.0+ which is used to compile the wasi target and link our
# programs together.
RUN curl http://releases.llvm.org/8.0.0/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz | tar xJf -
RUN mv /clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-18.04 /wasmcc

RUN git clone https://github.com/CraneStation/reference-sysroot-wasi && \
cd reference-sysroot-wasi && \
git reset --hard d5a609fe63926533e1054e539ba5f2693d51bdf5
RUN make -C reference-sysroot-wasi install -j $(nproc) WASM_CC=/wasmcc/bin/clang INSTALL_DIR=/wasm-sysroot
COPY docker/wasm32-unknown-wasi/clang.sh /wasm-sysroot/bin/clang
# Note that we're using `git reset --hard` to pin to a specific commit for
# verification for now. The sysroot is currently in somewhat of a state of flux
# and is expected to have breaking changes, so this is an attempt to mitigate
# those breaking changes on `libc`'s own CI
RUN git clone https://github.com/CraneStation/wasi-sysroot && \
cd wasi-sysroot && \
git reset --hard 320054e84f8f2440def3b1c8700cedb8fd697bf8
RUN make -C wasi-sysroot install -j $(nproc) WASM_CC=/wasmcc/bin/clang INSTALL_DIR=/wasi-sysroot

# This is a small wrapper script which executes the actual clang binary in
# `/wasmcc` and then is sure to pass the right `--sysroot` argument which we
# just built above.
COPY docker/wasm32-unknown-wasi/clang.sh /wasi-sysroot/bin/clang

# In the second container we're going to build the `wasmtime` binary which is
# used to execute wasi executables. This is a standard Rust project so we're
# just checking out a known revision (which pairs with the sysroot one we
# downlaoded above) and then we're building it with Cargo
FROM ubuntu:18.04 as wasmtime

RUN apt-get update && \
Expand All @@ -43,13 +60,12 @@ ENV PATH=/root/.cargo/bin:$PATH
RUN apt-get install -y --no-install-recommends python
RUN git clone https://github.com/CraneStation/wasmtime-wasi wasmtime && \
cd wasmtime && \
git reset --hard a7ac05df74759a7536b2b1e30adc6ff4867e36c3

# Install wasmtime in /usr/bin, but make sure to remove rust afterwards because
# we don't want it conflicting with the main Rust we're using to compile
# `libc`.
git reset --hard 4fe2d6084e5b5cc74e69a26860f12750df51d339
RUN cargo build --release --manifest-path wasmtime/Cargo.toml

# And finally in the last image we're going to assemble everything together.
# We'll install things needed at runtime for now and then copy over the
# sysroot/wasmtime artifacts into their final location.
FROM ubuntu:18.04

RUN apt-get update && \
Expand All @@ -58,12 +74,19 @@ RUN apt-get update && \
libc6-dev \
libxml2

# Copy over clang we downloaded to link executables ...
COPY --from=reference-sysroot /wasmcc /wasmcc/
COPY --from=reference-sysroot /wasm-sysroot/ /wasm-sysroot/
# ... and the sysroot we built to link executables against ...
COPY --from=reference-sysroot /wasi-sysroot/ /wasi-sysroot/
# ... and finally wasmtime to actually execute binaries
COPY --from=wasmtime /wasmtime/target/release/wasmtime /usr/bin/

# Of note here is our clang wrapper which just executes a normal clang
# executable with the right sysroot, and then we're sure to turn off the
# crt-static feature to ensure that the CRT that we're specifying with `clang`
# is used.
ENV CARGO_TARGET_WASM32_UNKNOWN_WASI_RUNNER=wasmtime \
CARGO_TARGET_WASM32_UNKNOWN_WASI_LINKER=/wasm-sysroot/bin/clang \
CC_wasm32_unknown_wasi=/wasm-sysroot/bin/clang \
CARGO_TARGET_WASM32_UNKNOWN_WASI_LINKER=/wasi-sysroot/bin/clang \
CC_wasm32_unknown_wasi=/wasi-sysroot/bin/clang \
PATH=$PATH:/rust/bin \
RUSTFLAGS=-Ctarget-feature=-crt-static
4 changes: 2 additions & 2 deletions ci/docker/wasm32-unknown-wasi/clang.sh
@@ -1,2 +1,2 @@
#!/bin/sh
exec /wasmcc/bin/clang --target=wasm32-unknown-wasi --sysroot /wasm-sysroot "$@"
#!/usr/bin/env sh
exec /wasmcc/bin/clang --target=wasm32-unknown-wasi --sysroot /wasi-sysroot "$@"
20 changes: 5 additions & 15 deletions libc-test/build.rs
Expand Up @@ -1896,37 +1896,27 @@ fn test_wasi(target: &str) {
cfg.type_name(move |ty, is_struct, is_union| match ty {
"FILE" => ty.to_string(),
t if is_union => format!("union {}", t),
t if t.starts_with("__wasi") && t.ends_with("_u") => format!("union {}", t),
t if t.starts_with("__wasi") && t.ends_with("_u") => {
format!("union {}", t)
}
t if t.starts_with("__wasi") && is_struct => format!("struct {}", t),
t if t.ends_with("_t") => t.to_string(),
t if is_struct => format!("struct {}", t),
t => t.to_string(),
});

// This is an opaque struct but we go through shenanigans to define values
// for it
cfg.skip_struct(move |ty| ty == "__clockid");

cfg.field_name(move |_struct, field| {
match field {
// deal with fields as rust keywords
// deal with fields as rust keywords
"type_" => "type".to_string(),
s => s.to_string(),
}
});

cfg.skip_static(move |name| {
match name {
// wasi shenanigans for defining CLOCK_REALTIME and such
s if s.starts_with("__CLOCK") => true,
_ => false,
}
});

// Looks like LLD doesn't merge duplicate imports, so if the Rust
// code imports from a module and the C code also imports from a
// module we end up with two imports of function pointers which
// improt the same thing bug have different function pointers
// import the same thing but have different function pointers
cfg.skip_fn_ptrcheck(|f| f.starts_with("__wasi"));

cfg.generate("../src/lib.rs", "main.rs");
Expand Down
34 changes: 10 additions & 24 deletions src/wasi.rs
Expand Up @@ -26,7 +26,6 @@ pub type clock_t = c_longlong;
pub type time_t = c_longlong;
pub type c_double = f64;
pub type c_float = f32;
pub type clockid_t = &'static __clockid;

pub type __wasi_advice_t = u8;
pub type __wasi_clockid_t = u32;
Expand Down Expand Up @@ -289,8 +288,6 @@ s! {
pub it_value: timespec,
}

pub struct __clockid {}

pub struct __wasi_dirent_t {
pub d_next: __wasi_dircookie_t,
pub d_ino: __wasi_inode_t,
Expand Down Expand Up @@ -367,15 +364,15 @@ s_no_extra_traits! {
#[allow(missing_debug_implementations)]
pub union __wasi_event_u {
pub fd_readwrite: __wasi_event_u_fd_readwrite_t,
_bindgen_union_align: [u64; 2usize],
_bindgen_union_align: [u64; 2],
}

#[allow(missing_debug_implementations)]
pub union __wasi_subscription_u {
pub clock: __wasi_subscription_u_clock_t,
pub fd_readwrite:
__wasi_subscription_u_fd_readwrite_t,
_bindgen_union_align: [u64; 5usize],
_bindgen_union_align: [u64; 5],
}

#[allow(missing_debug_implementations)]
Expand All @@ -391,11 +388,6 @@ s_no_extra_traits! {

}

pub static CLOCK_REALTIME: clockid_t = unsafe { &__CLOCK_REALTIME_ADDR };
pub static CLOCK_MONOTONIC: clockid_t = unsafe { &__CLOCK_MONOTONIC_ADDR };
pub static CLOCK_MONOTONIC_RAW: clockid_t =
unsafe { &__CLOCK_MONOTONIC_RAW_ADDR };

#[cfg_attr(feature = "rustc-dep-of-std",
link(name = "c", kind = "static",
cfg(target_feature = "crt-static")))]
Expand Down Expand Up @@ -484,21 +476,15 @@ extern "C" {
pub fn asctime_r(a: *const tm, b: *mut c_char) -> *mut c_char;
pub fn ctime_r(a: *const time_t, b: *mut c_char) -> *mut c_char;

#[link_name = "CLOCK_REALTIME"]
pub static __CLOCK_REALTIME_ADDR: __clockid;
#[link_name = "CLOCK_MONOTONIC"]
pub static __CLOCK_MONOTONIC_ADDR: __clockid;
#[link_name = "CLOCK_MONOTONIC_RAW"]
pub static __CLOCK_MONOTONIC_RAW_ADDR: __clockid;
pub fn nanosleep(a: *const timespec, b: *mut timespec) -> c_int;
pub fn clock_getres(a: clockid_t, b: *mut timespec) -> c_int;
pub fn clock_gettime(a: clockid_t, b: *mut timespec) -> c_int;
pub fn clock_nanosleep(
a: clockid_t,
a2: c_int,
b: *const timespec,
c: *mut timespec,
) -> c_int;
// pub fn clock_getres(a: clockid_t, b: *mut timespec) -> c_int;
// pub fn clock_gettime(a: clockid_t, b: *mut timespec) -> c_int;
// pub fn clock_nanosleep(
// a: clockid_t,
// a2: c_int,
// b: *const timespec,
// c: *mut timespec,
// ) -> c_int;

pub fn __wasilibc_register_preopened_fd(
fd: c_int,
Expand Down

0 comments on commit 6a600c5

Please sign in to comment.