2019-07-23 20:34:17 +03:00
|
|
|
// NO-RUSTC-WRAPPER
|
2019-07-23 22:17:27 +03:00
|
|
|
#![deny(warnings, rust_2018_idioms, unused_lifetimes)]
|
2019-02-04 01:05:45 +09:00
|
|
|
|
2017-03-03 20:11:04 +03:00
|
|
|
use std::fs::File;
|
2015-11-19 15:20:12 -08:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-03-07 15:24:36 -08:00
|
|
|
use std::process::{Command, Stdio};
|
2018-03-31 20:32:40 -06:00
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
2018-03-25 23:20:56 +09:00
|
|
|
use std::{env, fs};
|
2018-07-18 07:34:54 -07:00
|
|
|
use std::thread;
|
2017-02-01 00:27:51 +03:00
|
|
|
|
|
|
|
/// A helper macro to `unwrap` a result except also print out details like:
|
|
|
|
///
|
|
|
|
/// * The file/line of the panic
|
|
|
|
/// * The expression that failed
|
|
|
|
/// * The error itself
|
|
|
|
///
|
|
|
|
/// This is currently used judiciously throughout the build system rather than
|
|
|
|
/// using a `Result` with `try!`, but this may change one day...
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! t {
|
2018-03-25 23:20:56 +09:00
|
|
|
($e:expr) => {
|
|
|
|
match $e {
|
|
|
|
Ok(e) => e,
|
|
|
|
Err(e) => panic!("{} failed with {}", stringify!($e), e),
|
|
|
|
}
|
|
|
|
};
|
2017-02-01 00:27:51 +03:00
|
|
|
}
|
|
|
|
|
2019-01-21 17:47:57 -07:00
|
|
|
// Because Cargo adds the compiler's dylib path to our library search path, llvm-config may
|
|
|
|
// break: the dylib path for the compiler, as of this writing, contains a copy of the LLVM
|
|
|
|
// shared library, which means that when our freshly built llvm-config goes to load it's
|
|
|
|
// associated LLVM, it actually loads the compiler's LLVM. In particular when building the first
|
|
|
|
// compiler (i.e., in stage 0) that's a problem, as the compiler's LLVM is likely different from
|
|
|
|
// the one we want to use. As such, we restore the environment to what bootstrap saw. This isn't
|
|
|
|
// perfect -- we might actually want to see something from Cargo's added library paths -- but
|
|
|
|
// for now it works.
|
|
|
|
pub fn restore_library_path() {
|
|
|
|
println!("cargo:rerun-if-env-changed=REAL_LIBRARY_PATH_VAR");
|
|
|
|
println!("cargo:rerun-if-env-changed=REAL_LIBRARY_PATH");
|
|
|
|
let key = env::var_os("REAL_LIBRARY_PATH_VAR").expect("REAL_LIBRARY_PATH_VAR");
|
|
|
|
if let Some(env) = env::var_os("REAL_LIBRARY_PATH") {
|
|
|
|
env::set_var(&key, &env);
|
|
|
|
} else {
|
|
|
|
env::remove_var(&key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 20:08:49 +02:00
|
|
|
/// Run the command, printing what we are running.
|
|
|
|
pub fn run_verbose(cmd: &mut Command) {
|
2015-11-19 15:20:12 -08:00
|
|
|
println!("running: {:?}", cmd);
|
2019-08-01 20:13:47 +02:00
|
|
|
run(cmd);
|
2015-11-19 15:20:12 -08:00
|
|
|
}
|
|
|
|
|
2019-08-01 20:13:47 +02:00
|
|
|
pub fn run(cmd: &mut Command) {
|
|
|
|
if !try_run(cmd) {
|
2017-06-02 09:27:44 -07:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 20:13:47 +02:00
|
|
|
pub fn try_run(cmd: &mut Command) -> bool {
|
2015-11-19 15:20:12 -08:00
|
|
|
let status = match cmd.status() {
|
|
|
|
Ok(status) => status,
|
2018-03-25 23:20:56 +09:00
|
|
|
Err(e) => fail(&format!(
|
|
|
|
"failed to execute command: {:?}\nerror: {}",
|
|
|
|
cmd, e
|
|
|
|
)),
|
2015-11-19 15:20:12 -08:00
|
|
|
};
|
2017-12-07 05:06:48 +08:00
|
|
|
if !status.success() {
|
2018-03-25 23:20:56 +09:00
|
|
|
println!(
|
|
|
|
"\n\ncommand did not execute successfully: {:?}\n\
|
|
|
|
expected success, got: {}\n\n",
|
|
|
|
cmd, status
|
|
|
|
);
|
2015-11-19 15:20:12 -08:00
|
|
|
}
|
2017-12-07 05:06:48 +08:00
|
|
|
status.success()
|
2015-11-19 15:20:12 -08:00
|
|
|
}
|
|
|
|
|
2017-12-07 05:06:48 +08:00
|
|
|
pub fn run_suppressed(cmd: &mut Command) {
|
|
|
|
if !try_run_suppressed(cmd) {
|
2017-06-02 09:27:44 -07:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 05:06:48 +08:00
|
|
|
pub fn try_run_suppressed(cmd: &mut Command) -> bool {
|
2017-02-15 15:57:06 -08:00
|
|
|
let output = match cmd.output() {
|
|
|
|
Ok(status) => status,
|
2018-03-25 23:20:56 +09:00
|
|
|
Err(e) => fail(&format!(
|
|
|
|
"failed to execute command: {:?}\nerror: {}",
|
|
|
|
cmd, e
|
|
|
|
)),
|
2017-02-15 15:57:06 -08:00
|
|
|
};
|
2017-12-07 05:06:48 +08:00
|
|
|
if !output.status.success() {
|
2018-03-25 23:20:56 +09:00
|
|
|
println!(
|
|
|
|
"\n\ncommand did not execute successfully: {:?}\n\
|
|
|
|
expected success, got: {}\n\n\
|
|
|
|
stdout ----\n{}\n\
|
|
|
|
stderr ----\n{}\n\n",
|
|
|
|
cmd,
|
|
|
|
output.status,
|
|
|
|
String::from_utf8_lossy(&output.stdout),
|
|
|
|
String::from_utf8_lossy(&output.stderr)
|
|
|
|
);
|
2017-12-07 05:06:48 +08:00
|
|
|
}
|
|
|
|
output.status.success()
|
2017-02-15 15:57:06 -08:00
|
|
|
}
|
|
|
|
|
2018-10-09 14:30:14 +02:00
|
|
|
pub fn gnu_target(target: &str) -> &str {
|
2015-11-19 15:20:12 -08:00
|
|
|
match target {
|
2018-10-09 14:30:14 +02:00
|
|
|
"i686-pc-windows-msvc" => "i686-pc-win32",
|
|
|
|
"x86_64-pc-windows-msvc" => "x86_64-pc-win32",
|
|
|
|
"i686-pc-windows-gnu" => "i686-w64-mingw32",
|
|
|
|
"x86_64-pc-windows-gnu" => "x86_64-w64-mingw32",
|
|
|
|
s => s,
|
2015-11-19 15:20:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 20:09:23 +01:00
|
|
|
pub fn make(host: &str) -> PathBuf {
|
2019-05-13 09:13:07 +02:00
|
|
|
if host.contains("dragonfly") || host.contains("freebsd")
|
2018-03-25 23:20:56 +09:00
|
|
|
|| host.contains("netbsd") || host.contains("openbsd")
|
|
|
|
{
|
2016-12-17 20:09:23 +01:00
|
|
|
PathBuf::from("gmake")
|
|
|
|
} else {
|
|
|
|
PathBuf::from("make")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 15:20:12 -08:00
|
|
|
pub fn output(cmd: &mut Command) -> String {
|
|
|
|
let output = match cmd.stderr(Stdio::inherit()).output() {
|
|
|
|
Ok(status) => status,
|
2018-03-25 23:20:56 +09:00
|
|
|
Err(e) => fail(&format!(
|
|
|
|
"failed to execute command: {:?}\nerror: {}",
|
|
|
|
cmd, e
|
|
|
|
)),
|
2015-11-19 15:20:12 -08:00
|
|
|
};
|
|
|
|
if !output.status.success() {
|
2018-03-25 23:20:56 +09:00
|
|
|
panic!(
|
|
|
|
"command did not execute successfully: {:?}\n\
|
|
|
|
expected success, got: {}",
|
|
|
|
cmd, output.status
|
|
|
|
);
|
2015-11-19 15:20:12 -08:00
|
|
|
}
|
|
|
|
String::from_utf8(output.stdout).unwrap()
|
|
|
|
}
|
|
|
|
|
2017-01-27 01:57:30 +03:00
|
|
|
pub fn rerun_if_changed_anything_in_dir(dir: &Path) {
|
2018-03-25 23:20:56 +09:00
|
|
|
let mut stack = dir.read_dir()
|
|
|
|
.unwrap()
|
|
|
|
.map(|e| e.unwrap())
|
|
|
|
.filter(|e| &*e.file_name() != ".git")
|
|
|
|
.collect::<Vec<_>>();
|
2017-01-27 01:57:30 +03:00
|
|
|
while let Some(entry) = stack.pop() {
|
|
|
|
let path = entry.path();
|
|
|
|
if entry.file_type().unwrap().is_dir() {
|
|
|
|
stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
|
|
|
|
} else {
|
|
|
|
println!("cargo:rerun-if-changed={}", path.display());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 00:27:51 +03:00
|
|
|
/// Returns the last-modified time for `path`, or zero if it doesn't exist.
|
2018-03-31 20:32:40 -06:00
|
|
|
pub fn mtime(path: &Path) -> SystemTime {
|
2018-03-25 23:20:56 +09:00
|
|
|
fs::metadata(path)
|
|
|
|
.and_then(|f| f.modified())
|
|
|
|
.unwrap_or(UNIX_EPOCH)
|
2017-02-01 00:27:51 +03:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `true` if `dst` is up to date given that the file or files in `src`
|
2017-02-01 00:27:51 +03:00
|
|
|
/// are used to generate it.
|
|
|
|
///
|
|
|
|
/// Uses last-modified time checks to verify this.
|
|
|
|
pub fn up_to_date(src: &Path, dst: &Path) -> bool {
|
2017-12-01 14:28:14 +05:00
|
|
|
if !dst.exists() {
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-01 00:27:51 +03:00
|
|
|
let threshold = mtime(dst);
|
|
|
|
let meta = match fs::metadata(src) {
|
|
|
|
Ok(meta) => meta,
|
|
|
|
Err(e) => panic!("source {:?} failed to get metadata: {}", src, e),
|
|
|
|
};
|
|
|
|
if meta.is_dir() {
|
2018-03-31 20:32:40 -06:00
|
|
|
dir_up_to_date(src, threshold)
|
2017-02-01 00:27:51 +03:00
|
|
|
} else {
|
2018-03-31 20:32:40 -06:00
|
|
|
meta.modified().unwrap_or(UNIX_EPOCH) <= threshold
|
2017-02-01 00:27:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:11:04 +03:00
|
|
|
#[must_use]
|
2017-03-03 02:15:56 +03:00
|
|
|
pub struct NativeLibBoilerplate {
|
|
|
|
pub src_dir: PathBuf,
|
|
|
|
pub out_dir: PathBuf,
|
|
|
|
}
|
|
|
|
|
2018-09-29 12:37:12 -07:00
|
|
|
impl NativeLibBoilerplate {
|
2019-02-08 14:53:55 +01:00
|
|
|
/// On macOS we don't want to ship the exact filename that compiler-rt builds.
|
2018-09-29 12:37:12 -07:00
|
|
|
/// This conflicts with the system and ours is likely a wildly different
|
|
|
|
/// version, so they can't be substituted.
|
|
|
|
///
|
|
|
|
/// As a result, we rename it here but we need to also use
|
2019-02-08 14:53:55 +01:00
|
|
|
/// `install_name_tool` on macOS to rename the commands listed inside of it to
|
2018-09-29 12:37:12 -07:00
|
|
|
/// ensure it's linked against correctly.
|
|
|
|
pub fn fixup_sanitizer_lib_name(&self, sanitizer_name: &str) {
|
|
|
|
if env::var("TARGET").unwrap() != "x86_64-apple-darwin" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let dir = self.out_dir.join("build/lib/darwin");
|
|
|
|
let name = format!("clang_rt.{}_osx_dynamic", sanitizer_name);
|
|
|
|
let src = dir.join(&format!("lib{}.dylib", name));
|
|
|
|
let new_name = format!("lib__rustc__{}.dylib", name);
|
|
|
|
let dst = dir.join(&new_name);
|
|
|
|
|
|
|
|
println!("{} => {}", src.display(), dst.display());
|
|
|
|
fs::rename(&src, &dst).unwrap();
|
|
|
|
let status = Command::new("install_name_tool")
|
|
|
|
.arg("-id")
|
|
|
|
.arg(format!("@rpath/{}", new_name))
|
|
|
|
.arg(&dst)
|
|
|
|
.status()
|
|
|
|
.expect("failed to execute `install_name_tool`");
|
|
|
|
assert!(status.success());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:11:04 +03:00
|
|
|
impl Drop for NativeLibBoilerplate {
|
|
|
|
fn drop(&mut self) {
|
2018-07-18 07:34:54 -07:00
|
|
|
if !thread::panicking() {
|
|
|
|
t!(File::create(self.out_dir.join("rustbuild.timestamp")));
|
|
|
|
}
|
2017-03-03 20:11:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform standard preparations for native libraries that are build only once for all stages.
|
|
|
|
// Emit rerun-if-changed and linking attributes for Cargo, check if any source files are
|
|
|
|
// updated, calculate paths used later in actual build with CMake/make or C/C++ compiler.
|
|
|
|
// If Err is returned, then everything is up-to-date and further build actions can be skipped.
|
|
|
|
// Timestamps are created automatically when the result of `native_lib_boilerplate` goes out
|
|
|
|
// of scope, so all the build actions should be completed until then.
|
2018-03-25 23:20:56 +09:00
|
|
|
pub fn native_lib_boilerplate(
|
std: Depend directly on crates.io crates
Ever since we added a Cargo-based build system for the compiler the
standard library has always been a little special, it's never been able
to depend on crates.io crates for runtime dependencies. This has been a
result of various limitations, namely that Cargo doesn't understand that
crates from crates.io depend on libcore, so Cargo tries to build crates
before libcore is finished.
I had an idea this afternoon, however, which lifts the strategy
from #52919 to directly depend on crates.io crates from the standard
library. After all is said and done this removes a whopping three
submodules that we need to manage!
The basic idea here is that for any crate `std` depends on it adds an
*optional* dependency on an empty crate on crates.io, in this case named
`rustc-std-workspace-core`. This crate is overridden via `[patch]` in
this repository to point to a local crate we write, and *that* has a
`path` dependency on libcore.
Note that all `no_std` crates also depend on `compiler_builtins`, but if
we're not using submodules we can publish `compiler_builtins` to
crates.io and all crates can depend on it anyway! The basic strategy
then looks like:
* The standard library (or some transitive dep) decides to depend on a
crate `foo`.
* The standard library adds
```toml
[dependencies]
foo = { version = "0.1", features = ['rustc-dep-of-std'] }
```
* The crate `foo` has an optional dependency on `rustc-std-workspace-core`
* The crate `foo` has an optional dependency on `compiler_builtins`
* The crate `foo` has a feature `rustc-dep-of-std` which activates these
crates and any other necessary infrastructure in the crate.
A sample commit for `dlmalloc` [turns out to be quite simple][commit].
After that all `no_std` crates should largely build "as is" and still be
publishable on crates.io! Notably they should be able to continue to use
stable Rust if necessary, since the `rename-dependency` feature of Cargo
is soon stabilizing.
As a proof of concept, this commit removes the `dlmalloc`,
`libcompiler_builtins`, and `libc` submodules from this repository. Long
thorns in our side these are now gone for good and we can directly
depend on crates.io! It's hoped that in the long term we can bring in
other crates as necessary, but for now this is largely intended to
simply make it easier to manage these crates and remove submodules.
This should be a transparent non-breaking change for all users, but one
possible stickler is that this almost for sure breaks out-of-tree
`std`-building tools like `xargo` and `cargo-xbuild`. I think it should
be relatively easy to get them working, however, as all that's needed is
an entry in the `[patch]` section used to build the standard library.
Hopefully we can work with these tools to solve this problem!
[commit]: https://github.com/alexcrichton/dlmalloc-rs/commit/28ee12db813a3b650a7c25d1c36d2c17dcb88ae3
2018-11-19 21:52:50 -08:00
|
|
|
src_dir: &Path,
|
2018-03-25 23:20:56 +09:00
|
|
|
out_name: &str,
|
|
|
|
link_name: &str,
|
|
|
|
search_subdir: &str,
|
|
|
|
) -> Result<NativeLibBoilerplate, ()> {
|
std: Depend directly on crates.io crates
Ever since we added a Cargo-based build system for the compiler the
standard library has always been a little special, it's never been able
to depend on crates.io crates for runtime dependencies. This has been a
result of various limitations, namely that Cargo doesn't understand that
crates from crates.io depend on libcore, so Cargo tries to build crates
before libcore is finished.
I had an idea this afternoon, however, which lifts the strategy
from #52919 to directly depend on crates.io crates from the standard
library. After all is said and done this removes a whopping three
submodules that we need to manage!
The basic idea here is that for any crate `std` depends on it adds an
*optional* dependency on an empty crate on crates.io, in this case named
`rustc-std-workspace-core`. This crate is overridden via `[patch]` in
this repository to point to a local crate we write, and *that* has a
`path` dependency on libcore.
Note that all `no_std` crates also depend on `compiler_builtins`, but if
we're not using submodules we can publish `compiler_builtins` to
crates.io and all crates can depend on it anyway! The basic strategy
then looks like:
* The standard library (or some transitive dep) decides to depend on a
crate `foo`.
* The standard library adds
```toml
[dependencies]
foo = { version = "0.1", features = ['rustc-dep-of-std'] }
```
* The crate `foo` has an optional dependency on `rustc-std-workspace-core`
* The crate `foo` has an optional dependency on `compiler_builtins`
* The crate `foo` has a feature `rustc-dep-of-std` which activates these
crates and any other necessary infrastructure in the crate.
A sample commit for `dlmalloc` [turns out to be quite simple][commit].
After that all `no_std` crates should largely build "as is" and still be
publishable on crates.io! Notably they should be able to continue to use
stable Rust if necessary, since the `rename-dependency` feature of Cargo
is soon stabilizing.
As a proof of concept, this commit removes the `dlmalloc`,
`libcompiler_builtins`, and `libc` submodules from this repository. Long
thorns in our side these are now gone for good and we can directly
depend on crates.io! It's hoped that in the long term we can bring in
other crates as necessary, but for now this is largely intended to
simply make it easier to manage these crates and remove submodules.
This should be a transparent non-breaking change for all users, but one
possible stickler is that this almost for sure breaks out-of-tree
`std`-building tools like `xargo` and `cargo-xbuild`. I think it should
be relatively easy to get them working, however, as all that's needed is
an entry in the `[patch]` section used to build the standard library.
Hopefully we can work with these tools to solve this problem!
[commit]: https://github.com/alexcrichton/dlmalloc-rs/commit/28ee12db813a3b650a7c25d1c36d2c17dcb88ae3
2018-11-19 21:52:50 -08:00
|
|
|
rerun_if_changed_anything_in_dir(src_dir);
|
2017-03-03 02:15:56 +03:00
|
|
|
|
2018-10-12 16:16:00 +02:00
|
|
|
let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or_else(||
|
|
|
|
env::var_os("OUT_DIR").unwrap());
|
2017-03-03 02:15:56 +03:00
|
|
|
let out_dir = PathBuf::from(out_dir).join(out_name);
|
2017-08-07 16:04:46 +01:00
|
|
|
t!(fs::create_dir_all(&out_dir));
|
2017-04-18 04:22:16 +08:00
|
|
|
if link_name.contains('=') {
|
|
|
|
println!("cargo:rustc-link-lib={}", link_name);
|
|
|
|
} else {
|
|
|
|
println!("cargo:rustc-link-lib=static={}", link_name);
|
|
|
|
}
|
2018-03-25 23:20:56 +09:00
|
|
|
println!(
|
|
|
|
"cargo:rustc-link-search=native={}",
|
|
|
|
out_dir.join(search_subdir).display()
|
|
|
|
);
|
2017-03-03 02:15:56 +03:00
|
|
|
|
2017-03-03 20:11:04 +03:00
|
|
|
let timestamp = out_dir.join("rustbuild.timestamp");
|
std: Depend directly on crates.io crates
Ever since we added a Cargo-based build system for the compiler the
standard library has always been a little special, it's never been able
to depend on crates.io crates for runtime dependencies. This has been a
result of various limitations, namely that Cargo doesn't understand that
crates from crates.io depend on libcore, so Cargo tries to build crates
before libcore is finished.
I had an idea this afternoon, however, which lifts the strategy
from #52919 to directly depend on crates.io crates from the standard
library. After all is said and done this removes a whopping three
submodules that we need to manage!
The basic idea here is that for any crate `std` depends on it adds an
*optional* dependency on an empty crate on crates.io, in this case named
`rustc-std-workspace-core`. This crate is overridden via `[patch]` in
this repository to point to a local crate we write, and *that* has a
`path` dependency on libcore.
Note that all `no_std` crates also depend on `compiler_builtins`, but if
we're not using submodules we can publish `compiler_builtins` to
crates.io and all crates can depend on it anyway! The basic strategy
then looks like:
* The standard library (or some transitive dep) decides to depend on a
crate `foo`.
* The standard library adds
```toml
[dependencies]
foo = { version = "0.1", features = ['rustc-dep-of-std'] }
```
* The crate `foo` has an optional dependency on `rustc-std-workspace-core`
* The crate `foo` has an optional dependency on `compiler_builtins`
* The crate `foo` has a feature `rustc-dep-of-std` which activates these
crates and any other necessary infrastructure in the crate.
A sample commit for `dlmalloc` [turns out to be quite simple][commit].
After that all `no_std` crates should largely build "as is" and still be
publishable on crates.io! Notably they should be able to continue to use
stable Rust if necessary, since the `rename-dependency` feature of Cargo
is soon stabilizing.
As a proof of concept, this commit removes the `dlmalloc`,
`libcompiler_builtins`, and `libc` submodules from this repository. Long
thorns in our side these are now gone for good and we can directly
depend on crates.io! It's hoped that in the long term we can bring in
other crates as necessary, but for now this is largely intended to
simply make it easier to manage these crates and remove submodules.
This should be a transparent non-breaking change for all users, but one
possible stickler is that this almost for sure breaks out-of-tree
`std`-building tools like `xargo` and `cargo-xbuild`. I think it should
be relatively easy to get them working, however, as all that's needed is
an entry in the `[patch]` section used to build the standard library.
Hopefully we can work with these tools to solve this problem!
[commit]: https://github.com/alexcrichton/dlmalloc-rs/commit/28ee12db813a3b650a7c25d1c36d2c17dcb88ae3
2018-11-19 21:52:50 -08:00
|
|
|
if !up_to_date(Path::new("build.rs"), ×tamp) || !up_to_date(src_dir, ×tamp) {
|
2018-03-25 23:20:56 +09:00
|
|
|
Ok(NativeLibBoilerplate {
|
std: Depend directly on crates.io crates
Ever since we added a Cargo-based build system for the compiler the
standard library has always been a little special, it's never been able
to depend on crates.io crates for runtime dependencies. This has been a
result of various limitations, namely that Cargo doesn't understand that
crates from crates.io depend on libcore, so Cargo tries to build crates
before libcore is finished.
I had an idea this afternoon, however, which lifts the strategy
from #52919 to directly depend on crates.io crates from the standard
library. After all is said and done this removes a whopping three
submodules that we need to manage!
The basic idea here is that for any crate `std` depends on it adds an
*optional* dependency on an empty crate on crates.io, in this case named
`rustc-std-workspace-core`. This crate is overridden via `[patch]` in
this repository to point to a local crate we write, and *that* has a
`path` dependency on libcore.
Note that all `no_std` crates also depend on `compiler_builtins`, but if
we're not using submodules we can publish `compiler_builtins` to
crates.io and all crates can depend on it anyway! The basic strategy
then looks like:
* The standard library (or some transitive dep) decides to depend on a
crate `foo`.
* The standard library adds
```toml
[dependencies]
foo = { version = "0.1", features = ['rustc-dep-of-std'] }
```
* The crate `foo` has an optional dependency on `rustc-std-workspace-core`
* The crate `foo` has an optional dependency on `compiler_builtins`
* The crate `foo` has a feature `rustc-dep-of-std` which activates these
crates and any other necessary infrastructure in the crate.
A sample commit for `dlmalloc` [turns out to be quite simple][commit].
After that all `no_std` crates should largely build "as is" and still be
publishable on crates.io! Notably they should be able to continue to use
stable Rust if necessary, since the `rename-dependency` feature of Cargo
is soon stabilizing.
As a proof of concept, this commit removes the `dlmalloc`,
`libcompiler_builtins`, and `libc` submodules from this repository. Long
thorns in our side these are now gone for good and we can directly
depend on crates.io! It's hoped that in the long term we can bring in
other crates as necessary, but for now this is largely intended to
simply make it easier to manage these crates and remove submodules.
This should be a transparent non-breaking change for all users, but one
possible stickler is that this almost for sure breaks out-of-tree
`std`-building tools like `xargo` and `cargo-xbuild`. I think it should
be relatively easy to get them working, however, as all that's needed is
an entry in the `[patch]` section used to build the standard library.
Hopefully we can work with these tools to solve this problem!
[commit]: https://github.com/alexcrichton/dlmalloc-rs/commit/28ee12db813a3b650a7c25d1c36d2c17dcb88ae3
2018-11-19 21:52:50 -08:00
|
|
|
src_dir: src_dir.to_path_buf(),
|
2018-03-25 23:20:56 +09:00
|
|
|
out_dir: out_dir,
|
|
|
|
})
|
2017-03-03 20:11:04 +03:00
|
|
|
} else {
|
|
|
|
Err(())
|
2017-03-03 02:15:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 07:34:54 -07:00
|
|
|
pub fn sanitizer_lib_boilerplate(sanitizer_name: &str)
|
|
|
|
-> Result<(NativeLibBoilerplate, String), ()>
|
|
|
|
{
|
2018-09-29 12:37:12 -07:00
|
|
|
let (link_name, search_path, apple) = match &*env::var("TARGET").unwrap() {
|
2017-04-18 04:22:16 +08:00
|
|
|
"x86_64-unknown-linux-gnu" => (
|
|
|
|
format!("clang_rt.{}-x86_64", sanitizer_name),
|
|
|
|
"build/lib/linux",
|
2018-07-18 07:34:54 -07:00
|
|
|
false,
|
2017-04-18 04:22:16 +08:00
|
|
|
),
|
|
|
|
"x86_64-apple-darwin" => (
|
2018-07-18 07:34:54 -07:00
|
|
|
format!("clang_rt.{}_osx_dynamic", sanitizer_name),
|
2017-04-18 04:22:16 +08:00
|
|
|
"build/lib/darwin",
|
2018-07-18 07:34:54 -07:00
|
|
|
true,
|
2017-04-18 04:22:16 +08:00
|
|
|
),
|
|
|
|
_ => return Err(()),
|
|
|
|
};
|
2018-09-29 12:37:12 -07:00
|
|
|
let to_link = if apple {
|
|
|
|
format!("dylib=__rustc__{}", link_name)
|
2018-07-18 07:34:54 -07:00
|
|
|
} else {
|
|
|
|
format!("static={}", link_name)
|
|
|
|
};
|
2019-05-20 11:00:34 -07:00
|
|
|
// This env var is provided by rustbuild to tell us where `compiler-rt`
|
|
|
|
// lives.
|
|
|
|
let dir = env::var_os("RUST_COMPILER_RT_ROOT").unwrap();
|
2018-07-18 07:34:54 -07:00
|
|
|
let lib = native_lib_boilerplate(
|
std: Depend directly on crates.io crates
Ever since we added a Cargo-based build system for the compiler the
standard library has always been a little special, it's never been able
to depend on crates.io crates for runtime dependencies. This has been a
result of various limitations, namely that Cargo doesn't understand that
crates from crates.io depend on libcore, so Cargo tries to build crates
before libcore is finished.
I had an idea this afternoon, however, which lifts the strategy
from #52919 to directly depend on crates.io crates from the standard
library. After all is said and done this removes a whopping three
submodules that we need to manage!
The basic idea here is that for any crate `std` depends on it adds an
*optional* dependency on an empty crate on crates.io, in this case named
`rustc-std-workspace-core`. This crate is overridden via `[patch]` in
this repository to point to a local crate we write, and *that* has a
`path` dependency on libcore.
Note that all `no_std` crates also depend on `compiler_builtins`, but if
we're not using submodules we can publish `compiler_builtins` to
crates.io and all crates can depend on it anyway! The basic strategy
then looks like:
* The standard library (or some transitive dep) decides to depend on a
crate `foo`.
* The standard library adds
```toml
[dependencies]
foo = { version = "0.1", features = ['rustc-dep-of-std'] }
```
* The crate `foo` has an optional dependency on `rustc-std-workspace-core`
* The crate `foo` has an optional dependency on `compiler_builtins`
* The crate `foo` has a feature `rustc-dep-of-std` which activates these
crates and any other necessary infrastructure in the crate.
A sample commit for `dlmalloc` [turns out to be quite simple][commit].
After that all `no_std` crates should largely build "as is" and still be
publishable on crates.io! Notably they should be able to continue to use
stable Rust if necessary, since the `rename-dependency` feature of Cargo
is soon stabilizing.
As a proof of concept, this commit removes the `dlmalloc`,
`libcompiler_builtins`, and `libc` submodules from this repository. Long
thorns in our side these are now gone for good and we can directly
depend on crates.io! It's hoped that in the long term we can bring in
other crates as necessary, but for now this is largely intended to
simply make it easier to manage these crates and remove submodules.
This should be a transparent non-breaking change for all users, but one
possible stickler is that this almost for sure breaks out-of-tree
`std`-building tools like `xargo` and `cargo-xbuild`. I think it should
be relatively easy to get them working, however, as all that's needed is
an entry in the `[patch]` section used to build the standard library.
Hopefully we can work with these tools to solve this problem!
[commit]: https://github.com/alexcrichton/dlmalloc-rs/commit/28ee12db813a3b650a7c25d1c36d2c17dcb88ae3
2018-11-19 21:52:50 -08:00
|
|
|
dir.as_ref(),
|
2018-03-25 23:20:56 +09:00
|
|
|
sanitizer_name,
|
2018-07-18 07:34:54 -07:00
|
|
|
&to_link,
|
2018-03-25 23:20:56 +09:00
|
|
|
search_path,
|
2018-07-18 07:34:54 -07:00
|
|
|
)?;
|
|
|
|
Ok((lib, link_name))
|
2017-04-18 04:22:16 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 20:32:40 -06:00
|
|
|
fn dir_up_to_date(src: &Path, threshold: SystemTime) -> bool {
|
2017-02-01 00:27:51 +03:00
|
|
|
t!(fs::read_dir(src)).map(|e| t!(e)).all(|e| {
|
|
|
|
let meta = t!(e.metadata());
|
|
|
|
if meta.is_dir() {
|
|
|
|
dir_up_to_date(&e.path(), threshold)
|
|
|
|
} else {
|
2018-03-31 20:32:40 -06:00
|
|
|
meta.modified().unwrap_or(UNIX_EPOCH) < threshold
|
2017-02-01 00:27:51 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-19 15:20:12 -08:00
|
|
|
fn fail(s: &str) -> ! {
|
|
|
|
println!("\n\n{}\n\n", s);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|