Fix compiling some rustc crates to wasm

I was dabbling recently seeing what it would take to compile `rustfmt` to the
`wasm32-unknown-unknown` target and it turns out not much effort is needed!
Currently `rustfmt` depends on a few rustc crates published to crates.io, so
this commit touches up those crates to compile for wasm themselves. Notably:

* The `rustc_data_structures` crate's `flock` implementation is stubbed out to
  unconditionally return errors on unsupported platforms.
* The `rustc_errors` crate is extended to not do any locking for all non-windows
  platforms.

In both of these cases if we port the compiler to new platforms the
functionality isn't critical but will be discovered over time as it comes up, so
this hopefully doesn't make it too too hard to compile to new platforms!
This commit is contained in:
Alex Crichton 2018-09-07 00:27:20 -07:00
parent 2ae11a9c22
commit 0c89243fd3
2 changed files with 329 additions and 323 deletions

View file

@ -15,16 +15,15 @@
//! librustdoc, it is not production quality at all.
#![allow(non_camel_case_types)]
#![allow(nonstandard_style)]
use std::io;
use std::path::Path;
pub use self::imp::Lock;
#[cfg(unix)]
mod imp {
cfg_if! {
if #[cfg(unix)] {
use std::ffi::{CString, OsStr};
use std::os::unix::prelude::*;
use std::path::Path;
use std::io;
use libc;
#[cfg(any(target_os = "linux", target_os = "android"))]
@ -236,16 +235,10 @@ mod imp {
}
}
}
}
#[cfg(windows)]
#[allow(nonstandard_style)]
mod imp {
use std::io;
} else if #[cfg(windows)] {
use std::mem;
use std::os::windows::prelude::*;
use std::os::windows::raw::HANDLE;
use std::path::Path;
use std::fs::{File, OpenOptions};
use std::os::raw::{c_ulong, c_int};
@ -351,9 +344,22 @@ mod imp {
// Note that we don't need a Drop impl on the Windows: The file is unlocked
// automatically when it's closed.
} else {
#[derive(Debug)]
pub struct Lock(());
impl Lock {
pub fn new(_p: &Path, _wait: bool, _create: bool, _exclusive: bool)
-> io::Result<Lock>
{
let msg = "file locks not supported on this platform";
Err(io::Error::new(io::ErrorKind::Other, msg))
}
}
}
}
impl imp::Lock {
impl Lock {
pub fn panicking_new(p: &Path,
wait: bool,
create: bool,

View file

@ -109,7 +109,7 @@ pub fn acquire_global_lock(name: &str) -> Box<dyn Any> {
}
}
#[cfg(unix)]
#[cfg(not(windows))]
pub fn acquire_global_lock(_name: &str) -> Box<dyn Any> {
Box::new(())
}