Compile wasi-common to Emscripten (#688)

* Compile wasi-common to Emscripten

This commit enables cross-compiling of `wasi-common` to Emscripten. To achieve
this, this commit does quite a bit reshuffling in the existing codebase. Namely,
* rename `linux` modules in `wasi-common` and `yanix` to `linux_like` -- this is
  needed so that we can separate out logic specific to Linux and Emscripten out
* tweak `dir` module in `yanix` to support Emscripten -- in particular, the main
  change involves `SeekLoc::from_raw` which has to be now host-specific, and is now
  fallible
* tweak `filetime` so that in Emscripten we never check for existence of `utimensat`
  at runtime since we are guaranteed for it to exist by design
* since `utimes` and `futimes` are not present in Emscripten, move them into a separate
  module, `utimesat`, and tag it cfg-non-emscripten only
* finally, `to_timespec` is now fallible since on Emscripten we have to cast number of
  seconds, `FileTime::seconds` from `i64` to `libc::c_long` which resolves to `i32`
  unlike on other nixes

* Fix macos build

* Verify wasi-common compiles to Emscripten

This commit adds `emscripten` job to Github Actions which installs
`wasm32-unknown-emscripten` target, and builds `wasi-common` crate.

* Use #[path] to cherry-pick mods for Emscripten

This commit effectively reverses the reorg introduced in 145f4a5
in that it ditches `linux_like` mod for separate mods `linux` and
`emscripten` which are now on the same crate level, and instead,
pulls in common bits from `linux` using the `#[path = ..]` proc
macro.
This commit is contained in:
Jakub Konka
2019-12-12 01:25:13 +01:00
committed by Alex Crichton
parent ddd2300010
commit 95c2addf15
38 changed files with 541 additions and 434 deletions

View File

@@ -0,0 +1,36 @@
//! This internal module consists of helper types and functions for dealing
//! with setting the file times specific to Emscripten.
use crate::{sys::unix::filetime::FileTime, Result};
use std::fs::File;
use std::io;
pub(crate) const UTIME_NOW: i32 = 1_073_741_823;
pub(crate) const UTIME_OMIT: i32 = 1_073_741_822;
/// Wrapper for `utimensat` syscall. In Emscripten, there is no point in dynamically resolving
/// if `utimensat` is available as it always was and will be.
pub(crate) fn utimensat(
dirfd: &File,
path: &str,
atime: FileTime,
mtime: FileTime,
symlink_nofollow: bool,
) -> Result<()> {
use crate::sys::unix::filetime::to_timespec;
use std::ffi::CString;
use std::os::unix::prelude::*;
let flags = if symlink_nofollow {
libc::AT_SYMLINK_NOFOLLOW
} else {
0
};
let p = CString::new(path.as_bytes())?;
let times = [to_timespec(&atime)?, to_timespec(&mtime)?];
let rc = unsafe { libc::utimensat(dirfd.as_raw_fd(), p.as_ptr(), times.as_ptr(), flags) };
if rc == 0 {
Ok(())
} else {
Err(io::Error::last_os_error().into())
}
}

View File

@@ -0,0 +1,7 @@
pub(crate) mod filetime;
#[path = "../linux/host_impl.rs"]
pub(crate) mod host_impl;
#[path = "../linux/hostcalls_impl.rs"]
pub(crate) mod hostcalls_impl;
#[path = "../linux/oshandle.rs"]
pub(crate) mod oshandle;