diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2018-01-24 08:22:34 -0800 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2018-01-28 18:32:45 -0800 |
| commit | c6daea7c9a7d4be88e1ae8d54d992937fcfe24fa (patch) | |
| tree | 29d8d7e69c49ae4e9930c3568ebccb8b840cfde1 /src/bootstrap | |
| parent | 385ef1514c80fb8c0cb061dc69eb1d953a84e2b3 (diff) | |
| download | rust-c6daea7c9a7d4be88e1ae8d54d992937fcfe24fa.tar.gz rust-c6daea7c9a7d4be88e1ae8d54d992937fcfe24fa.zip | |
rustc: Split Emscripten to a separate codegen backend
This commit introduces a separately compiled backend for Emscripten, avoiding compiling the `JSBackend` target in the main LLVM codegen backend. This builds on the foundation provided by #47671 to create a new codegen backend dedicated solely to Emscripten, removing the `JSBackend` of the main codegen backend in the process. A new field was added to each target for this commit which specifies the backend to use for translation, the default being `llvm` which is the main backend that we use. The Emscripten targets specify an `emscripten` backend instead of the main `llvm` one. There's a whole bunch of consequences of this change, but I'll try to enumerate them here: * A *second* LLVM submodule was added in this commit. The main LLVM submodule will soon start to drift from the Emscripten submodule, but currently they're both at the same revision. * Logic was added to rustbuild to *not* build the Emscripten backend by default. This is gated behind a `--enable-emscripten` flag to the configure script. By default users should neither check out the emscripten submodule nor compile it. * The `init_repo.sh` script was updated to fetch the Emscripten submodule from GitHub the same way we do the main LLVM submodule (a tarball fetch). * The Emscripten backend, turned off by default, is still turned on for a number of targets on CI. We'll only be shipping an Emscripten backend with Tier 1 platforms, though. All cross-compiled platforms will not be receiving an Emscripten backend yet. This commit means that when you download the `rustc` package in Rustup for Tier 1 platforms you'll be receiving two trans backends, one for Emscripten and one that's the general LLVM backend. If you never compile for Emscripten you'll never use the Emscripten backend, so we may update this one day to only download the Emscripten backend when you add the Emscripten target. For now though it's just an extra 10MB gzip'd. Closes #46819
Diffstat (limited to 'src/bootstrap')
| -rw-r--r-- | src/bootstrap/bootstrap.py | 23 | ||||
| -rw-r--r-- | src/bootstrap/compile.rs | 194 | ||||
| -rw-r--r-- | src/bootstrap/config.rs | 9 | ||||
| -rwxr-xr-x | src/bootstrap/configure.py | 3 | ||||
| -rw-r--r-- | src/bootstrap/lib.rs | 4 | ||||
| -rw-r--r-- | src/bootstrap/native.rs | 75 |
6 files changed, 207 insertions, 101 deletions
diff --git a/src/bootstrap/bootstrap.py b/src/bootstrap/bootstrap.py index ecf9c0a7590..603a97ddfd4 100644 --- a/src/bootstrap/bootstrap.py +++ b/src/bootstrap/bootstrap.py @@ -640,14 +640,23 @@ class RustBuild(object): os.path.join(self.rust_root, ".gitmodules"), "--get-regexp", "path"] ).decode(default_encoding).splitlines()] - submodules = [module for module in submodules - if not ((module.endswith("llvm") and - self.get_toml('llvm-config')) or - (module.endswith("jemalloc") and - (self.get_toml('use-jemalloc') == "false" or - self.get_toml('jemalloc'))))] + filtered_submodules = [] + for module in submodules: + if module.endswith("llvm"): + if self.get_toml('llvm-config'): + continue + if module.endswith("llvm-emscripten"): + backends = self.get_toml('codegen-backends') + if backends is None or not 'emscripten' in backends: + continue + if module.endswith("jemalloc"): + if self.get_toml('use-jemalloc') == 'false': + continue + if self.get_toml('jemalloc'): + continue + filtered_submodules.append(module) run(["git", "submodule", "update", - "--init", "--recursive"] + submodules, + "--init", "--recursive"] + filtered_submodules, cwd=self.rust_root, verbose=self.verbose) run(["git", "submodule", "-q", "foreach", "git", "reset", "-q", "--hard"], diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index cb236cd7e73..fa289bbd76a 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -581,24 +581,30 @@ impl Step for RustcLink { } #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub struct RustcTrans { +pub struct CodegenBackend { pub compiler: Compiler, pub target: Interned<String>, + pub backend: Interned<String>, } -impl Step for RustcTrans { +impl Step for CodegenBackend { type Output = (); const ONLY_HOSTS: bool = true; const DEFAULT: bool = true; fn should_run(run: ShouldRun) -> ShouldRun { - run.path("src/librustc_trans").krate("rustc_trans") + run.path("src/librustc_trans") } fn make_run(run: RunConfig) { - run.builder.ensure(RustcTrans { + let backend = run.builder.config.rust_codegen_backends.get(0); + let backend = backend.cloned().unwrap_or_else(|| { + INTERNER.intern_str("llvm") + }); + run.builder.ensure(CodegenBackend { compiler: run.builder.compiler(run.builder.top_stage, run.host), target: run.target, + backend }); } @@ -609,58 +615,92 @@ impl Step for RustcTrans { builder.ensure(Rustc { compiler, target }); - // Build LLVM for our target. This will implicitly build the host LLVM - // if necessary. - builder.ensure(native::Llvm { target }); - if build.force_use_stage1(compiler, target) { - builder.ensure(RustcTrans { + builder.ensure(CodegenBackend { compiler: builder.compiler(1, build.build), target, + backend: self.backend, }); return; } - let _folder = build.fold_output(|| format!("stage{}-rustc_trans", compiler.stage)); - println!("Building stage{} trans artifacts ({} -> {})", - compiler.stage, &compiler.host, target); - let mut cargo = builder.cargo(compiler, Mode::Librustc, target, "build"); + let mut features = build.rustc_features().to_string(); cargo.arg("--manifest-path") - .arg(build.src.join("src/librustc_trans/Cargo.toml")) - .arg("--features").arg(build.rustc_features()); + .arg(build.src.join("src/librustc_trans/Cargo.toml")); rustc_cargo_env(build, &mut cargo); - // Pass down configuration from the LLVM build into the build of - // librustc_llvm and librustc_trans. - if build.is_rust_llvm(target) { - cargo.env("LLVM_RUSTLLVM", "1"); - } - cargo.env("LLVM_CONFIG", build.llvm_config(target)); - let target_config = build.config.target_config.get(&target); - if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) { - cargo.env("CFG_LLVM_ROOT", s); - } - // Building with a static libstdc++ is only supported on linux right now, - // not for MSVC or macOS - if build.config.llvm_static_stdcpp && - !target.contains("freebsd") && - !target.contains("windows") && - !target.contains("apple") { - let file = compiler_file(build, - build.cxx(target).unwrap(), - target, - "libstdc++.a"); - cargo.env("LLVM_STATIC_STDCPP", file); - } - if build.config.llvm_link_shared { - cargo.env("LLVM_LINK_SHARED", "1"); + match &*self.backend { + "llvm" | "emscripten" => { + // Build LLVM for our target. This will implicitly build the + // host LLVM if necessary. + let llvm_config = builder.ensure(native::Llvm { + target, + emscripten: self.backend == "emscripten", + }); + + if self.backend == "emscripten" { + features.push_str(" emscripten"); + } + + let _folder = build.fold_output(|| format!("stage{}-rustc_trans", compiler.stage)); + println!("Building stage{} codegen artifacts ({} -> {}, {})", + compiler.stage, &compiler.host, target, self.backend); + + // Pass down configuration from the LLVM build into the build of + // librustc_llvm and librustc_trans. + if build.is_rust_llvm(target) { + cargo.env("LLVM_RUSTLLVM", "1"); + } + cargo.env("LLVM_CONFIG", &llvm_config); + if self.backend != "emscripten" { + let target_config = build.config.target_config.get(&target); + if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) { + cargo.env("CFG_LLVM_ROOT", s); + } + } + // Building with a static libstdc++ is only supported on linux right now, + // not for MSVC or macOS + if build.config.llvm_static_stdcpp && + !target.contains("freebsd") && + !target.contains("windows") && + !target.contains("apple") { + let file = compiler_file(build, + build.cxx(target).unwrap(), + target, + "libstdc++.a"); + cargo.env("LLVM_STATIC_STDCPP", file); + } + if build.config.llvm_link_shared { + cargo.env("LLVM_LINK_SHARED", "1"); + } + } + _ => panic!("unknown backend: {}", self.backend), } - run_cargo(build, - &mut cargo, - &librustc_trans_stamp(build, compiler, target), - false); + let tmp_stamp = build.cargo_out(compiler, Mode::Librustc, target) + .join(".tmp.stamp"); + let files = run_cargo(build, + cargo.arg("--features").arg(features), + &tmp_stamp, + false); + let mut files = files.into_iter() + .filter(|f| { + let filename = f.file_name().unwrap().to_str().unwrap(); + is_dylib(filename) && filename.contains("rustc_trans-") + }); + let codegen_backend = match files.next() { + Some(f) => f, + None => panic!("no dylibs built for codegen backend?"), + }; + if let Some(f) = files.next() { + panic!("codegen backend built two dylibs:\n{}\n{}", + codegen_backend.display(), + f.display()); + } + let stamp = codegen_backend_stamp(build, compiler, target, self.backend); + let codegen_backend = codegen_backend.to_str().unwrap(); + t!(t!(File::create(&stamp)).write_all(codegen_backend.as_bytes())); } } @@ -682,33 +722,29 @@ fn copy_codegen_backends_to_sysroot(builder: &Builder, // not linked into the main compiler by default but is rather dynamically // selected at runtime for inclusion. // - // Here we're looking for the output dylib of the `RustcTrans` step and + // Here we're looking for the output dylib of the `CodegenBackend` step and // we're copying that into the `codegen-backends` folder. let libdir = builder.sysroot_libdir(target_compiler, target); let dst = libdir.join("codegen-backends"); t!(fs::create_dir_all(&dst)); - let stamp = librustc_trans_stamp(build, compiler, target); - let mut copied = None; - for file in read_stamp_file(&stamp) { - let filename = match file.file_name().and_then(|s| s.to_str()) { - Some(s) => s, - None => continue, + for backend in builder.config.rust_codegen_backends.iter() { + let stamp = codegen_backend_stamp(build, compiler, target, *backend); + let mut dylib = String::new(); + t!(t!(File::open(&stamp)).read_to_string(&mut dylib)); + let file = Path::new(&dylib); + let filename = file.file_name().unwrap().to_str().unwrap(); + // change `librustc_trans-xxxxxx.so` to `librustc_trans-llvm.so` + let target_filename = { + let dash = filename.find("-").unwrap(); + let dot = filename.find(".").unwrap(); + format!("{}-{}{}", + &filename[..dash], + backend, + &filename[dot..]) }; - if !is_dylib(filename) || !filename.contains("rustc_trans-") { - continue - } - match copied { - None => copied = Some(file.clone()), - Some(ref s) => { - panic!("copied two codegen backends:\n{}\n{}", - s.display(), - file.display()); - } - } - copy(&file, &dst.join(filename)); + copy(&file, &dst.join(target_filename)); } - assert!(copied.is_some(), "failed to find a codegen backend to copy"); } /// Cargo's output path for the standard library in a given stage, compiled @@ -729,10 +765,12 @@ pub fn librustc_stamp(build: &Build, compiler: Compiler, target: Interned<String build.cargo_out(compiler, Mode::Librustc, target).join(".librustc.stamp") } -pub fn librustc_trans_stamp(build: &Build, - compiler: Compiler, - target: Interned<String>) -> PathBuf { - build.cargo_out(compiler, Mode::Librustc, target).join(".librustc_trans.stamp") +fn codegen_backend_stamp(build: &Build, + compiler: Compiler, + target: Interned<String>, + backend: Interned<String>) -> PathBuf { + build.cargo_out(compiler, Mode::Librustc, target) + .join(format!(".librustc_trans-{}.stamp", backend)) } fn compiler_file(build: &Build, @@ -849,10 +887,13 @@ impl Step for Assemble { compiler: build_compiler, target: target_compiler.host, }); - builder.ensure(RustcTrans { - compiler: build_compiler, - target: target_compiler.host, - }); + for &backend in build.config.rust_codegen_backends.iter() { + builder.ensure(CodegenBackend { + compiler: build_compiler, + target: target_compiler.host, + backend, + }); + } } let stage = target_compiler.stage; @@ -922,7 +963,9 @@ fn stderr_isatty() -> bool { } } -pub fn run_cargo(build: &Build, cargo: &mut Command, stamp: &Path, is_check: bool) { +pub fn run_cargo(build: &Build, cargo: &mut Command, stamp: &Path, is_check: bool) + -> Vec<PathBuf> +{ // Instruct Cargo to give us json messages on stdout, critically leaving // stderr as piped so we can get those pretty colors. cargo.arg("--message-format").arg("json") @@ -1066,8 +1109,8 @@ pub fn run_cargo(build: &Build, cargo: &mut Command, stamp: &Path, is_check: boo let mut new_contents = Vec::new(); let mut max = None; let mut max_path = None; - for dep in deps { - let mtime = mtime(&dep); + for dep in deps.iter() { + let mtime = mtime(dep); if Some(mtime) > max { max = Some(mtime); max_path = Some(dep.clone()); @@ -1080,7 +1123,7 @@ pub fn run_cargo(build: &Build, cargo: &mut Command, stamp: &Path, is_check: boo if stamp_contents == new_contents && max <= stamp_mtime { build.verbose(&format!("not updating {:?}; contents equal and {} <= {}", stamp, max, stamp_mtime)); - return + return deps } if max > stamp_mtime { build.verbose(&format!("updating {:?} as {:?} changed", stamp, max_path)); @@ -1088,4 +1131,5 @@ pub fn run_cargo(build: &Build, cargo: &mut Command, stamp: &Path, is_check: boo build.verbose(&format!("updating {:?} as deps changed", stamp)); } t!(t!(File::create(stamp)).write_all(&new_contents)); + deps } diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs index 72e75fddc19..dbeb27cbfb7 100644 --- a/src/bootstrap/config.rs +++ b/src/bootstrap/config.rs @@ -91,6 +91,7 @@ pub struct Config { pub rust_optimize_tests: bool, pub rust_debuginfo_tests: bool, pub rust_dist_src: bool, + pub rust_codegen_backends: Vec<Interned<String>>, pub build: Interned<String>, pub hosts: Vec<Interned<String>>, @@ -280,6 +281,7 @@ struct Rust { quiet_tests: Option<bool>, test_miri: Option<bool>, save_toolstates: Option<String>, + codegen_backends: Option<Vec<String>>, } /// TOML representation of how each build target is configured. @@ -318,6 +320,7 @@ impl Config { config.ignore_git = false; config.rust_dist_src = true; config.test_miri = false; + config.rust_codegen_backends = vec![INTERNER.intern_str("llvm")]; config.on_fail = flags.on_fail; config.stage = flags.stage; @@ -465,6 +468,12 @@ impl Config { config.musl_root = rust.musl_root.clone().map(PathBuf::from); config.save_toolstates = rust.save_toolstates.clone().map(PathBuf::from); + if let Some(ref backends) = rust.codegen_backends { + config.rust_codegen_backends = backends.iter() + .map(|s| INTERNER.intern_str(s)) + .collect(); + } + match rust.codegen_units { Some(0) => config.rust_codegen_units = Some(num_cpus::get() as u32), Some(n) => config.rust_codegen_units = Some(n), diff --git a/src/bootstrap/configure.py b/src/bootstrap/configure.py index 9a4b5e786e1..bc6f666d001 100755 --- a/src/bootstrap/configure.py +++ b/src/bootstrap/configure.py @@ -65,6 +65,7 @@ o("sanitizers", "build.sanitizers", "build the sanitizer runtimes (asan, lsan, m o("dist-src", "rust.dist-src", "when building tarballs enables building a source tarball") o("cargo-openssl-static", "build.openssl-static", "static openssl in cargo") o("profiler", "build.profiler", "build the profiler runtime") +o("emscripten", None, "compile the emscripten backend as well as LLVM") # Optimization and debugging options. These may be overridden by the release # channel, etc. @@ -321,6 +322,8 @@ for key in known_args: set('build.host', value.split(',')) elif option.name == 'target': set('build.target', value.split(',')) + elif option.name == 'emscripten': + set('rust.codegen-backends', ['llvm', 'emscripten']) elif option.name == 'option-checking': # this was handled above pass diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index 8928bef9faa..aae0a4f056f 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -480,6 +480,10 @@ impl Build { self.out.join(&*target).join("llvm") } + fn emscripten_llvm_out(&self, target: Interned<String>) -> PathBuf { + self.out.join(&*target).join("llvm-emscripten") + } + /// Output directory for all documentation for a target fn doc_out(&self, target: Interned<String>) -> PathBuf { self.out.join(&*target).join("doc") diff --git a/src/bootstrap/native.rs b/src/bootstrap/native.rs index 442098a7afa..3f30756a568 100644 --- a/src/bootstrap/native.rs +++ b/src/bootstrap/native.rs @@ -22,7 +22,7 @@ use std::env; use std::ffi::OsString; use std::fs::{self, File}; use std::io::{Read, Write}; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::process::Command; use build_helper::output; @@ -30,7 +30,7 @@ use cmake; use cc; use Build; -use util; +use util::{self, exe}; use build_helper::up_to_date; use builder::{Builder, RunConfig, ShouldRun, Step}; use cache::Interned; @@ -38,30 +38,42 @@ use cache::Interned; #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub struct Llvm { pub target: Interned<String>, + pub emscripten: bool, } impl Step for Llvm { - type Output = (); + type Output = PathBuf; // path to llvm-config + const ONLY_HOSTS: bool = true; fn should_run(run: ShouldRun) -> ShouldRun { - run.path("src/llvm") + run.path("src/llvm").path("src/llvm-emscripten") } fn make_run(run: RunConfig) { - run.builder.ensure(Llvm { target: run.target }) + let emscripten = run.path.map(|p| { + p.ends_with("llvm-emscripten") + }).unwrap_or(false); + run.builder.ensure(Llvm { + target: run.target, + emscripten, + }); } /// Compile LLVM for `target`. - fn run(self, builder: &Builder) { + fn run(self, builder: &Builder) -> PathBuf { let build = builder.build; let target = self.target; + let emscripten = self.emscripten; // If we're using a custom LLVM bail out here, but we can only use a // custom LLVM for the build triple. - if let Some(config) = build.config.target_config.get(&target) { - if let Some(ref s) = config.llvm_config { - return check_llvm_version(build, s); + if !self.emscripten { + if let Some(config) = build.config.target_config.get(&target) { + if let Some(ref s) = config.llvm_config { + check_llvm_version(build, s); + return s.to_path_buf() + } } } @@ -69,8 +81,17 @@ impl Step for Llvm { let mut rebuild_trigger_contents = String::new(); t!(t!(File::open(&rebuild_trigger)).read_to_string(&mut rebuild_trigger_contents)); - let out_dir = build.llvm_out(target); + let (out_dir, llvm_config_ret_dir) = if emscripten { + let dir = build.emscripten_llvm_out(target); + let config_dir = dir.join("bin"); + (dir, config_dir) + } else { + (build.llvm_out(target), + build.llvm_out(build.config.build).join("bin")) + }; let done_stamp = out_dir.join("llvm-finished-building"); + let build_llvm_config = llvm_config_ret_dir + .join(exe("llvm-config", &*build.config.build)); if done_stamp.exists() { let mut done_contents = String::new(); t!(t!(File::open(&done_stamp)).read_to_string(&mut done_contents)); @@ -78,17 +99,19 @@ impl Step for Llvm { // If LLVM was already built previously and contents of the rebuild-trigger file // didn't change from the previous build, then no action is required. if done_contents == rebuild_trigger_contents { - return + return build_llvm_config } } let _folder = build.fold_output(|| "llvm"); - println!("Building LLVM for {}", target); + let descriptor = if emscripten { "Emscripten " } else { "" }; + println!("Building {}LLVM for {}", descriptor, target); let _time = util::timeit(); t!(fs::create_dir_all(&out_dir)); // http://llvm.org/docs/CMake.html - let mut cfg = cmake::Config::new(build.src.join("src/llvm")); + let root = if self.emscripten { "src/llvm-emscripten" } else { "src/llvm" }; + let mut cfg = cmake::Config::new(build.src.join(root)); if build.config.ninja { cfg.generator("Ninja"); } @@ -99,13 +122,22 @@ impl Step for Llvm { (true, true) => "RelWithDebInfo", }; - // NOTE: remember to also update `config.toml.example` when changing the defaults! - let llvm_targets = match build.config.llvm_targets { - Some(ref s) => s, - None => "X86;ARM;AArch64;Mips;PowerPC;SystemZ;JSBackend;MSP430;Sparc;NVPTX;Hexagon", + // NOTE: remember to also update `config.toml.example` when changing the + // defaults! + let llvm_targets = if self.emscripten { + "JSBackend" + } else { + match build.config.llvm_targets { + Some(ref s) => s, + None => "X86;ARM;AArch64;Mips;PowerPC;SystemZ;MSP430;Sparc;NVPTX;Hexagon", + } }; - let llvm_exp_targets = &build.config.llvm_experimental_targets; + let llvm_exp_targets = if self.emscripten { + "" + } else { + &build.config.llvm_experimental_targets[..] + }; let assertions = if build.config.llvm_assertions {"ON"} else {"OFF"}; @@ -155,7 +187,10 @@ impl Step for Llvm { // http://llvm.org/docs/HowToCrossCompileLLVM.html if target != build.build { - builder.ensure(Llvm { target: build.build }); + builder.ensure(Llvm { + target: build.build, + emscripten: false, + }); // FIXME: if the llvm root for the build triple is overridden then we // should use llvm-tblgen from there, also should verify that it // actually exists most of the time in normal installs of LLVM. @@ -241,6 +276,8 @@ impl Step for Llvm { cfg.build(); t!(t!(File::create(&done_stamp)).write_all(rebuild_trigger_contents.as_bytes())); + + build_llvm_config } } |
