Skip to content

Commit e97ba83

Browse files
committedNov 25, 2017
Auto merge of #46115 - alexcrichton:add-wasm-target, r=kennytm
rustbuild: Enable WebAssembly backend by default This commit alters how we compile LLVM by default enabling the WebAssembly backend. This then also adds the wasm32-unknown-unknown target to get compiled on the `cross` builder and distributed through rustup. Tests are not yet enabled for this target but that should hopefully be coming soon!
·
1.89.01.24.0
2 parents 2f47a9e + 48996f9 commit e97ba83

File tree

12 files changed

+148
-107
lines changed

12 files changed

+148
-107
lines changed
 

‎config.toml.example

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,9 @@
6060
# LLVM experimental targets to build support for. These targets are specified in
6161
# the same format as above, but since these targets are experimental, they are
6262
# not built by default and the experimental Rust compilation targets that depend
63-
# on them will not work unless the user opts in to building them. Possible
64-
# experimental LLVM targets include WebAssembly for the
65-
# wasm32-experimental-emscripten Rust target.
66-
#experimental-targets = ""
63+
# on them will not work unless the user opts in to building them. By default the
64+
# `WebAssembly` target is enabled when compiling LLVM from scratch.
65+
#experimental-targets = "WebAssembly"
6766

6867
# Cap the number of parallel linker invocations when compiling LLVM.
6968
# This can be useful when building LLVM with debug info, which significantly

‎src/bootstrap/config.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub struct Config {
7676
pub llvm_static_stdcpp: bool,
7777
pub llvm_link_shared: bool,
7878
pub llvm_targets: Option<String>,
79-
pub llvm_experimental_targets: Option<String>,
79+
pub llvm_experimental_targets: String,
8080
pub llvm_link_jobs: Option<u32>,
8181

8282
// rust codegen options
@@ -447,7 +447,8 @@ impl Config {
447447
set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
448448
set(&mut config.llvm_link_shared, llvm.link_shared);
449449
config.llvm_targets = llvm.targets.clone();
450-
config.llvm_experimental_targets = llvm.experimental_targets.clone();
450+
config.llvm_experimental_targets = llvm.experimental_targets.clone()
451+
.unwrap_or("WebAssembly".to_string());
451452
config.llvm_link_jobs = llvm.link_jobs;
452453
}
453454

‎src/bootstrap/native.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,7 @@ impl Step for Llvm {
110110
None => "X86;ARM;AArch64;Mips;PowerPC;SystemZ;JSBackend;MSP430;Sparc;NVPTX;Hexagon",
111111
};
112112

113-
let llvm_exp_targets = match build.config.llvm_experimental_targets {
114-
Some(ref s) => s,
115-
None => "",
116-
};
113+
let llvm_exp_targets = &build.config.llvm_experimental_targets;
117114

118115
let assertions = if build.config.llvm_assertions {"ON"} else {"OFF"};
119116

‎src/ci/docker/cross2/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ ENV \
4747
ENV TARGETS=x86_64-unknown-fuchsia
4848
ENV TARGETS=$TARGETS,aarch64-unknown-fuchsia
4949
ENV TARGETS=$TARGETS,sparcv9-sun-solaris
50+
ENV TARGETS=$TARGETS,wasm32-unknown-unknown
5051
ENV TARGETS=$TARGETS,x86_64-sun-solaris
5152
ENV TARGETS=$TARGETS,x86_64-unknown-linux-gnux32
5253

‎src/librustc_driver/driver.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ use std::sync::mpsc;
5656
use syntax::{ast, diagnostics, visit};
5757
use syntax::attr;
5858
use syntax::ext::base::ExtCtxt;
59+
use syntax::fold::Folder;
5960
use syntax::parse::{self, PResult};
6061
use syntax::util::node_count::NodeCounter;
6162
use syntax;
6263
use syntax_ext;
6364
use arena::DroplessArena;
6465

6566
use derive_registrar;
67+
use pretty::ReplaceBodyWithLoop;
6668

6769
use profile;
6870

@@ -809,6 +811,12 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
809811
sess.diagnostic())
810812
});
811813

814+
// If we're actually rustdoc then there's no need to actually compile
815+
// anything, so switch everything to just looping
816+
if sess.opts.actually_rustdoc {
817+
krate = ReplaceBodyWithLoop::new(sess).fold_crate(krate);
818+
}
819+
812820
// If we're in rustdoc we're always compiling as an rlib, but that'll trip a
813821
// bunch of checks in the `modify` function below. For now just skip this
814822
// step entirely if we're rustdoc as it's not too useful anyway.

‎src/librustc_driver/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,9 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
565565
control.after_hir_lowering.stop = Compilation::Stop;
566566

567567
control.after_parse.callback = box move |state| {
568-
state.krate = Some(pretty::fold_crate(state.krate.take().unwrap(), ppm));
568+
state.krate = Some(pretty::fold_crate(state.session,
569+
state.krate.take().unwrap(),
570+
ppm));
569571
};
570572
control.after_hir_lowering.callback = box move |state| {
571573
pretty::print_after_hir_lowering(state.session,
@@ -587,7 +589,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
587589
control.after_parse.stop = Compilation::Stop;
588590

589591
control.after_parse.callback = box move |state| {
590-
let krate = pretty::fold_crate(state.krate.take().unwrap(), ppm);
592+
let krate = pretty::fold_crate(state.session, state.krate.take().unwrap(), ppm);
591593
pretty::print_after_parsing(state.session,
592594
state.input,
593595
&krate,

‎src/librustc_driver/pretty.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -638,13 +638,14 @@ impl UserIdentifiedItem {
638638
// ambitious form of the closed RFC #1637. See also [#34511].
639639
//
640640
// [#34511]: https://github.com/rust-lang/rust/issues/34511#issuecomment-322340401
641-
pub struct ReplaceBodyWithLoop {
641+
pub struct ReplaceBodyWithLoop<'a> {
642642
within_static_or_const: bool,
643+
sess: &'a Session,
643644
}
644645

645-
impl ReplaceBodyWithLoop {
646-
pub fn new() -> ReplaceBodyWithLoop {
647-
ReplaceBodyWithLoop { within_static_or_const: false }
646+
impl<'a> ReplaceBodyWithLoop<'a> {
647+
pub fn new(sess: &'a Session) -> ReplaceBodyWithLoop<'a> {
648+
ReplaceBodyWithLoop { within_static_or_const: false, sess }
648649
}
649650

650651
fn run<R, F: FnOnce(&mut Self) -> R>(&mut self, is_const: bool, action: F) -> R {
@@ -691,7 +692,7 @@ impl ReplaceBodyWithLoop {
691692
}
692693
}
693694

694-
impl fold::Folder for ReplaceBodyWithLoop {
695+
impl<'a> fold::Folder for ReplaceBodyWithLoop<'a> {
695696
fn fold_item_kind(&mut self, i: ast::ItemKind) -> ast::ItemKind {
696697
let is_const = match i {
697698
ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => true,
@@ -723,34 +724,36 @@ impl fold::Folder for ReplaceBodyWithLoop {
723724
}
724725

725726
fn fold_block(&mut self, b: P<ast::Block>) -> P<ast::Block> {
726-
fn expr_to_block(rules: ast::BlockCheckMode, e: Option<P<ast::Expr>>) -> P<ast::Block> {
727+
fn expr_to_block(rules: ast::BlockCheckMode,
728+
e: Option<P<ast::Expr>>,
729+
sess: &Session) -> P<ast::Block> {
727730
P(ast::Block {
728731
stmts: e.map(|e| {
729732
ast::Stmt {
730-
id: ast::DUMMY_NODE_ID,
733+
id: sess.next_node_id(),
731734
span: e.span,
732735
node: ast::StmtKind::Expr(e),
733736
}
734737
})
735738
.into_iter()
736739
.collect(),
737740
rules,
738-
id: ast::DUMMY_NODE_ID,
741+
id: sess.next_node_id(),
739742
span: syntax_pos::DUMMY_SP,
740743
})
741744
}
742745

743746
if !self.within_static_or_const {
744747

745-
let empty_block = expr_to_block(BlockCheckMode::Default, None);
748+
let empty_block = expr_to_block(BlockCheckMode::Default, None, self.sess);
746749
let loop_expr = P(ast::Expr {
747750
node: ast::ExprKind::Loop(empty_block, None),
748-
id: ast::DUMMY_NODE_ID,
751+
id: self.sess.next_node_id(),
749752
span: syntax_pos::DUMMY_SP,
750753
attrs: ast::ThinVec::new(),
751754
});
752755

753-
expr_to_block(b.rules, Some(loop_expr))
756+
expr_to_block(b.rules, Some(loop_expr), self.sess)
754757

755758
} else {
756759
fold::noop_fold_block(b, self)
@@ -829,9 +832,9 @@ fn print_flowgraph<'a, 'tcx, W: Write>(variants: Vec<borrowck_dot::Variant>,
829832
}
830833
}
831834

832-
pub fn fold_crate(krate: ast::Crate, ppm: PpMode) -> ast::Crate {
835+
pub fn fold_crate(sess: &Session, krate: ast::Crate, ppm: PpMode) -> ast::Crate {
833836
if let PpmSource(PpmEveryBodyLoops) = ppm {
834-
let mut fold = ReplaceBodyWithLoop::new();
837+
let mut fold = ReplaceBodyWithLoop::new(sess);
835838
fold.fold_crate(krate)
836839
} else {
837840
krate

‎src/librustdoc/core.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use rustc_lint;
1212
use rustc_driver::{driver, target_features, abort_on_err};
13-
use rustc_driver::pretty::ReplaceBodyWithLoop;
1413
use rustc::session::{self, config};
1514
use rustc::hir::def_id::DefId;
1615
use rustc::hir::def::Def;
@@ -26,7 +25,6 @@ use rustc_metadata::cstore::CStore;
2625

2726
use syntax::codemap;
2827
use syntax::feature_gate::UnstableFeatures;
29-
use syntax::fold::Folder;
3028
use errors;
3129
use errors::emitter::ColorConfig;
3230

@@ -157,7 +155,6 @@ pub fn run_core(search_paths: SearchPaths,
157155
let control = &driver::CompileController::basic();
158156

159157
let krate = panictry!(driver::phase_1_parse_input(control, &sess, &input));
160-
let krate = ReplaceBodyWithLoop::new().fold_crate(krate);
161158

162159
let name = link::find_crate_name(Some(&sess), &krate.attrs, &input);
163160

‎src/librustdoc/test.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@ use rustc_back::dynamic_lib::DynamicLibrary;
3131
use rustc_back::tempdir::TempDir;
3232
use rustc_driver::{self, driver, Compilation};
3333
use rustc_driver::driver::phase_2_configure_and_expand;
34-
use rustc_driver::pretty::ReplaceBodyWithLoop;
3534
use rustc_metadata::cstore::CStore;
3635
use rustc_resolve::MakeGlobMap;
3736
use rustc_trans;
3837
use rustc_trans::back::link;
3938
use syntax::ast;
4039
use syntax::codemap::CodeMap;
4140
use syntax::feature_gate::UnstableFeatures;
42-
use syntax::fold::Folder;
4341
use syntax_pos::{BytePos, DUMMY_SP, Pos, Span};
4442
use errors;
4543
use errors::emitter::ColorConfig;
@@ -97,7 +95,6 @@ pub fn run(input: &str,
9795
let krate = panictry!(driver::phase_1_parse_input(&driver::CompileController::basic(),
9896
&sess,
9997
&input));
100-
let krate = ReplaceBodyWithLoop::new().fold_crate(krate);
10198
let driver::ExpansionResult { defs, mut hir_forest, .. } = {
10299
phase_2_configure_and_expand(
103100
&sess,

‎src/libstd/os/mod.rs

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,53 @@
1313
#![stable(feature = "os", since = "1.0.0")]
1414
#![allow(missing_docs, bad_style, missing_debug_implementations)]
1515

16-
#[cfg(all(not(dox), any(target_os = "redox", unix)))]
17-
#[stable(feature = "rust1", since = "1.0.0")]
18-
pub use sys::ext as unix;
19-
#[cfg(all(not(dox), windows))]
20-
#[stable(feature = "rust1", since = "1.0.0")]
21-
pub use sys::ext as windows;
22-
23-
#[cfg(dox)]
24-
#[stable(feature = "rust1", since = "1.0.0")]
25-
pub use sys::unix_ext as unix;
26-
#[cfg(dox)]
27-
#[stable(feature = "rust1", since = "1.0.0")]
28-
pub use sys::windows_ext as windows;
29-
30-
#[cfg(any(dox, target_os = "linux", target_os = "l4re"))]
31-
#[doc(cfg(target_os = "linux"))]
32-
pub mod linux;
33-
34-
#[cfg(all(not(dox), target_os = "android"))] pub mod android;
35-
#[cfg(all(not(dox), target_os = "bitrig"))] pub mod bitrig;
36-
#[cfg(all(not(dox), target_os = "dragonfly"))] pub mod dragonfly;
37-
#[cfg(all(not(dox), target_os = "freebsd"))] pub mod freebsd;
38-
#[cfg(all(not(dox), target_os = "haiku"))] pub mod haiku;
39-
#[cfg(all(not(dox), target_os = "ios"))] pub mod ios;
40-
#[cfg(all(not(dox), target_os = "macos"))] pub mod macos;
41-
#[cfg(all(not(dox), target_os = "netbsd"))] pub mod netbsd;
42-
#[cfg(all(not(dox), target_os = "openbsd"))] pub mod openbsd;
43-
#[cfg(all(not(dox), target_os = "solaris"))] pub mod solaris;
44-
#[cfg(all(not(dox), target_os = "emscripten"))] pub mod emscripten;
45-
#[cfg(all(not(dox), target_os = "fuchsia"))] pub mod fuchsia;
16+
cfg_if! {
17+
if #[cfg(dox)] {
18+
19+
// When documenting libstd we want to show unix/windows/linux modules as
20+
// these are the "main modules" that are used across platforms. This
21+
// should help show platform-specific functionality in a hopefully
22+
// cross-platform way in the documentation
23+
24+
#[stable(feature = "rust1", since = "1.0.0")]
25+
pub use sys::unix_ext as unix;
26+
27+
#[stable(feature = "rust1", since = "1.0.0")]
28+
pub use sys::windows_ext as windows;
29+
30+
#[doc(cfg(target_os = "linux"))]
31+
pub mod linux;
32+
33+
} else {
34+
35+
// If we're not documenting libstd then we just expose everything as we
36+
// otherwise would.
37+
38+
#[cfg(target_os = "android")] pub mod android;
39+
#[cfg(target_os = "bitrig")] pub mod bitrig;
40+
#[cfg(target_os = "dragonfly")] pub mod dragonfly;
41+
#[cfg(target_os = "freebsd")] pub mod freebsd;
42+
#[cfg(target_os = "haiku")] pub mod haiku;
43+
#[cfg(target_os = "ios")] pub mod ios;
44+
#[cfg(target_os = "macos")] pub mod macos;
45+
#[cfg(target_os = "netbsd")] pub mod netbsd;
46+
#[cfg(target_os = "openbsd")] pub mod openbsd;
47+
#[cfg(target_os = "solaris")] pub mod solaris;
48+
#[cfg(target_os = "emscripten")] pub mod emscripten;
49+
#[cfg(target_os = "fuchsia")] pub mod fuchsia;
50+
51+
#[cfg(any(target_os = "redox", unix))]
52+
#[stable(feature = "rust1", since = "1.0.0")]
53+
pub use sys::ext as unix;
54+
55+
#[cfg(windows)]
56+
#[stable(feature = "rust1", since = "1.0.0")]
57+
pub use sys::ext as windows;
58+
59+
#[cfg(any(target_os = "linux", target_os = "l4re"))]
60+
pub mod linux;
61+
62+
}
63+
}
4664

4765
pub mod raw;

‎src/libstd/sys/mod.rs

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,49 +32,66 @@
3232
3333
#![allow(missing_debug_implementations)]
3434

35-
pub use self::imp::*;
36-
37-
#[cfg(unix)]
38-
#[path = "unix/mod.rs"]
39-
mod imp;
40-
41-
#[cfg(windows)]
42-
#[path = "windows/mod.rs"]
43-
mod imp;
44-
45-
#[cfg(target_os = "redox")]
46-
#[path = "redox/mod.rs"]
47-
mod imp;
48-
49-
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
50-
#[path = "wasm/mod.rs"]
51-
mod imp;
52-
53-
// Import essential modules from both platforms when documenting.
54-
55-
#[cfg(all(dox, not(unix)))]
56-
use os::linux as platform;
57-
58-
#[cfg(all(dox, not(any(unix, target_os = "redox"))))]
59-
#[path = "unix/ext/mod.rs"]
60-
pub mod unix_ext;
61-
62-
#[cfg(all(dox, any(unix, target_os = "redox")))]
63-
pub use self::ext as unix_ext;
64-
65-
66-
#[cfg(all(dox, not(windows)))]
67-
#[macro_use]
68-
#[path = "windows/compat.rs"]
69-
mod compat;
70-
71-
#[cfg(all(dox, not(windows)))]
72-
#[path = "windows/c.rs"]
73-
mod c;
74-
75-
#[cfg(all(dox, not(windows)))]
76-
#[path = "windows/ext/mod.rs"]
77-
pub mod windows_ext;
78-
79-
#[cfg(all(dox, windows))]
80-
pub use self::ext as windows_ext;
35+
cfg_if! {
36+
if #[cfg(unix)] {
37+
mod unix;
38+
pub use self::unix::*;
39+
} else if #[cfg(windows)] {
40+
mod windows;
41+
pub use self::windows::*;
42+
} else if #[cfg(target_os = "redox")] {
43+
mod redox;
44+
pub use self::redox::*;
45+
} else if #[cfg(target_arch = "wasm32")] {
46+
mod wasm;
47+
pub use self::wasm::*;
48+
} else {
49+
compile_error!("libstd doesn't compile for this platform yet");
50+
}
51+
}
52+
53+
// Import essential modules from both platforms when documenting. These are
54+
// then later used in the `std::os` module when documenting, for example,
55+
// Windows when we're compiling for Linux.
56+
57+
#[cfg(dox)]
58+
cfg_if! {
59+
if #[cfg(any(unix, target_os = "redox"))] {
60+
// On unix we'll document what's already available
61+
pub use self::ext as unix_ext;
62+
} else if #[cfg(target_arch = "wasm32")] {
63+
// On wasm right now the module below doesn't compile (missing things
64+
// in `libc` which is empty) so just omit everything with an empty module
65+
#[unstable(issue = "0", feature = "std_internals")]
66+
pub mod unix_ext {}
67+
} else {
68+
// On other platforms like Windows document the bare bones of unix
69+
use os::linux as platform;
70+
#[path = "unix/ext/mod.rs"]
71+
pub mod unix_ext;
72+
}
73+
}
74+
75+
#[cfg(dox)]
76+
cfg_if! {
77+
if #[cfg(windows)] {
78+
// On windows we'll just be documenting what's already available
79+
pub use self::ext as windows_ext;
80+
} else if #[cfg(target_arch = "wasm32")] {
81+
// On wasm right now the shim below doesn't compile, so just omit it
82+
#[unstable(issue = "0", feature = "std_internals")]
83+
pub mod windows_ext {}
84+
} else {
85+
// On all other platforms (aka linux/osx/etc) then pull in a "minimal"
86+
// amount of windows goop which ends up compiling
87+
#[macro_use]
88+
#[path = "windows/compat.rs"]
89+
mod compat;
90+
91+
#[path = "windows/c.rs"]
92+
mod c;
93+
94+
#[path = "windows/ext/mod.rs"]
95+
pub mod windows_ext;
96+
}
97+
}

‎src/tools/build-manifest/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ static TARGETS: &'static [&'static str] = &[
8484
"sparc64-unknown-linux-gnu",
8585
"sparcv9-sun-solaris",
8686
"wasm32-unknown-emscripten",
87+
"wasm32-unknown-unknown",
8788
"x86_64-linux-android",
8889
"x86_64-apple-darwin",
8990
"x86_64-apple-ios",

0 commit comments

Comments
 (0)
Please sign in to comment.