Skip to content

Add an alternative build system written in Rust #26493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions build_rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
target
*racertmp
x86_64*
*~
dl
*#
*.o
*.a
unpack
34 changes: 34 additions & 0 deletions build_rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions build_rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "build-rust"
version = "0.1.0"
authors = ["Chang Liu <cliu712@aucklanduni.ac.nz>"]

build = "build.rs"

[[bin]]
name = "build-rust"
path = "src/main.rs"

[dependencies]
getopts = "*"
regex = "*"
36 changes: 36 additions & 0 deletions build_rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
The Rust Build System
=====================

The Rust Build System is written in Rust and is managed as a Cargo package.
Building the latest Rust compiler is as simple as running:

```sh
$ cargo run
```

under this directory (where the build system is). The Rust Build System will
automatically build all supporting libraries (including LLVM) and bootstrap
a working stage2 compiler.

To speed up the build process by running parallel jobs, use `--nproc`:

```sh
$ cargo run -- --nproc=4
```

This will run 4 parallel jobs when building LLVM.

To show the command output during the build process, use `--verbose`:

```sh
$ cargo run -- --verbose
```

You can use `--no-bootstrap`, `--no-rebuild-llvm`, etc to control the build
process.

This build system supports out-of-tree build. Use `--rustc-root=<DIR>` to
specify the location of the source repo. Use `--build-dir=<DIR>` to specify
the root build directory.

Use `--help` to see a list of supported command line arguments.
84 changes: 84 additions & 0 deletions build_rust/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Collect build environment info and pass on to configure.rs
//!
//! The only two pieces of information we currently collect are
//! the triple spec (arch-vendor-os-abi) of the build machine
//! and the manifest directory of this Cargo package. The latter
//! is used to determine where the Rust source code is.
//!
//! Once finished collecting said variables this script will
//! write them into the file build_env.rs which configure.rs
//! will then include it (at compile time).

struct BuildEnv {
build_triple : String,
manifest_dir : String
}

/// On error, we will convert all errors into an error string,
/// display the error string and then panic.
struct ErrMsg {
msg : String
}

impl<T : std::error::Error> std::convert::From<T> for ErrMsg {
fn from(err : T) -> ErrMsg {
ErrMsg { msg : err.description().to_string() }
}
}

type Result<T> = std::result::Result<T, ErrMsg>;

/// We use the host triple of the compiler used to compile
/// this build script as the triple spec of the build machine.
/// This script must be compiled as a native executable for it
/// runs on the same machine as it is compiled.
fn get_build_env_info() -> Result<BuildEnv> {
let host_triple = try!(std::env::var("HOST"));
let target_triple = try!(std::env::var("TARGET"));
if host_triple != target_triple {
return Err(ErrMsg { msg : "The Rust Build System must be built as a native executable".to_string() });
}
let manifest_dir = try!(std::env::var("CARGO_MANIFEST_DIR"));
Ok(BuildEnv {
build_triple : host_triple.to_string(),
manifest_dir : manifest_dir
})
}

fn write_to_build_env_rs(info : &BuildEnv) -> Result<()> {
use std::io::Write;
let out_dir = env!("OUT_DIR");
let dest_path = std::path::Path::new(&out_dir).join("build_env.rs");
let mut f = try!(std::fs::File::create(&dest_path));
try!(write!(&mut f,
"const BUILD_TRIPLE : &'static str = \"{}\";
const MANIFEST_DIR : &'static str = r\"{}\";",
info.build_triple,
info.manifest_dir));
Ok(())
}

fn run() -> Result<()> {
let info = try!(get_build_env_info());
write_to_build_env_rs(&info)
}

fn main() {
match run() {
Err(e) => {
println!("Failed to collect build environment information: {}", e.msg);
std::process::exit(1);
},
_ => {}
}
}
55 changes: 55 additions & 0 deletions build_rust/src/build_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Indicate the state of the build process.
//!
//! `BuildState<T>` wraps around the `Result<T,E>` type and is used
//! to indicate the state of the build process for functions that may
//! fail at runtime. Functions can continue the build by returning
//! `continue_build()` or fail by returning one of the fail states.
//! Use the `try!` macro to chain functions that return `BuildState`.

pub enum ExitStatus {
SuccessStop, // Build is successful
MsgStop(String), // Build is terminated with a (non-error) message
ErrStop(String) // Build is stopped due to an error
}

pub type BuildState<T> = Result<T, ExitStatus>;

impl<T : ::std::fmt::Display> From<T> for ExitStatus {
fn from(e : T) -> ExitStatus {
ExitStatus::ErrStop(format!("{}", e))
}
}

pub fn continue_build() -> BuildState<()> {
Ok(())
}

pub fn continue_with<T>(v : T) -> BuildState<T> {
Ok(v)
}

pub fn success_stop<V>() -> BuildState<V> {
Err(ExitStatus::SuccessStop)
}

pub fn msg_stop<S : Into<String>, V>(s : S) -> BuildState<V> {
Err(ExitStatus::MsgStop(s.into()))
}

pub fn err_stop<S : Into<String>, V>(s : S) -> BuildState<V> {
Err(ExitStatus::ErrStop(s.into()))
}

macro_rules! err_stop {
( $( $x:expr ),* ) => { return err_stop(format!( $( $x ),* )) }
}
Loading