2017-02-13 03:57:50 -06:00
|
|
|
//! Compiles the profiler part of the `compiler-rt` library.
|
|
|
|
//!
|
|
|
|
//! See the build.rs for libcompiler_builtins crate for details.
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
fn main() {
|
2023-07-25 15:11:50 -05:00
|
|
|
println!("cargo:rerun-if-env-changed=LLVM_PROFILER_RT_LIB");
|
|
|
|
if let Ok(rt) = env::var("LLVM_PROFILER_RT_LIB") {
|
|
|
|
println!("cargo:rustc-link-lib=static:+verbatim={rt}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-22 05:33:02 -06:00
|
|
|
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS was not set");
|
|
|
|
let target_env = env::var("CARGO_CFG_TARGET_ENV").expect("CARGO_CFG_TARGET_ENV was not set");
|
2017-09-22 23:34:27 -05:00
|
|
|
let cfg = &mut cc::Build::new();
|
2017-02-13 03:57:50 -06:00
|
|
|
|
2020-07-22 12:06:37 -05:00
|
|
|
// FIXME: `rerun-if-changed` directives are not currently emitted and the build script
|
|
|
|
// will not rerun on changes in these source files or headers included into them.
|
2019-12-22 16:42:04 -06:00
|
|
|
let mut profile_sources = vec![
|
|
|
|
"GCDAProfiling.c",
|
|
|
|
"InstrProfiling.c",
|
|
|
|
"InstrProfilingBuffer.c",
|
|
|
|
"InstrProfilingFile.c",
|
|
|
|
"InstrProfilingMerge.c",
|
|
|
|
"InstrProfilingMergeFile.c",
|
|
|
|
"InstrProfilingNameVar.c",
|
|
|
|
"InstrProfilingPlatformDarwin.c",
|
2020-08-27 13:52:36 -05:00
|
|
|
"InstrProfilingPlatformFuchsia.c",
|
2019-12-22 16:42:04 -06:00
|
|
|
"InstrProfilingPlatformLinux.c",
|
|
|
|
"InstrProfilingPlatformOther.c",
|
|
|
|
"InstrProfilingPlatformWindows.c",
|
2021-03-22 13:33:43 -05:00
|
|
|
"InstrProfilingRuntime.cpp",
|
2019-12-22 16:42:04 -06:00
|
|
|
"InstrProfilingUtil.c",
|
|
|
|
"InstrProfilingValue.c",
|
2021-02-20 09:51:18 -06:00
|
|
|
"InstrProfilingVersionVar.c",
|
2019-12-22 16:42:04 -06:00
|
|
|
"InstrProfilingWriter.c",
|
2020-06-25 20:52:41 -05:00
|
|
|
// These files were added in LLVM 11.
|
|
|
|
"InstrProfilingInternal.c",
|
|
|
|
"InstrProfilingBiasVar.c",
|
2019-12-22 16:42:04 -06:00
|
|
|
];
|
2017-06-12 17:08:08 -05:00
|
|
|
|
2024-01-22 05:33:02 -06:00
|
|
|
if target_env == "msvc" {
|
2017-02-13 03:57:50 -06:00
|
|
|
// Don't pull in extra libraries on MSVC
|
|
|
|
cfg.flag("/Zl");
|
2017-06-12 17:08:08 -05:00
|
|
|
profile_sources.push("WindowsMMap.c");
|
2017-06-12 17:08:57 -05:00
|
|
|
cfg.define("strdup", Some("_strdup"));
|
|
|
|
cfg.define("open", Some("_open"));
|
|
|
|
cfg.define("fdopen", Some("_fdopen"));
|
2018-03-25 16:17:47 -05:00
|
|
|
cfg.define("getpid", Some("_getpid"));
|
|
|
|
cfg.define("fileno", Some("_fileno"));
|
2017-02-13 03:57:50 -06:00
|
|
|
} else {
|
|
|
|
// Turn off various features of gcc and such, mostly copying
|
|
|
|
// compiler-rt's build system already
|
|
|
|
cfg.flag("-fno-builtin");
|
|
|
|
cfg.flag("-fomit-frame-pointer");
|
|
|
|
cfg.define("VISIBILITY_HIDDEN", None);
|
2024-01-22 05:33:02 -06:00
|
|
|
if target_os != "windows" {
|
2020-08-24 04:57:32 -05:00
|
|
|
cfg.flag("-fvisibility=hidden");
|
2019-05-23 06:32:30 -05:00
|
|
|
cfg.define("COMPILER_RT_HAS_UNAME", Some("1"));
|
|
|
|
} else {
|
|
|
|
profile_sources.push("WindowsMMap.c");
|
|
|
|
}
|
2017-02-13 03:57:50 -06:00
|
|
|
}
|
|
|
|
|
2019-04-30 03:46:21 -05:00
|
|
|
// Assume that the Unixes we are building this for have fnctl() available
|
|
|
|
if env::var_os("CARGO_CFG_UNIX").is_some() {
|
|
|
|
cfg.define("COMPILER_RT_HAS_FCNTL_LCK", Some("1"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should be a pretty good heuristic for when to set
|
|
|
|
// COMPILER_RT_HAS_ATOMICS
|
2019-12-22 16:42:04 -06:00
|
|
|
if env::var_os("CARGO_CFG_TARGET_HAS_ATOMIC")
|
2020-08-23 08:55:18 -05:00
|
|
|
.map(|features| features.to_string_lossy().to_lowercase().contains("ptr"))
|
2019-12-22 16:42:04 -06:00
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2019-04-30 03:46:21 -05:00
|
|
|
cfg.define("COMPILER_RT_HAS_ATOMICS", Some("1"));
|
|
|
|
}
|
|
|
|
|
2020-04-10 15:42:19 -05:00
|
|
|
// Note that this should exist if we're going to run (otherwise we just
|
|
|
|
// don't build profiler builtins at all).
|
2020-06-11 21:31:49 -05:00
|
|
|
let root = Path::new("../../src/llvm-project/compiler-rt");
|
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 23:52:50 -06:00
|
|
|
|
2020-01-03 17:19:37 -06:00
|
|
|
let src_root = root.join("lib").join("profile");
|
2017-02-13 03:57:50 -06:00
|
|
|
for src in profile_sources {
|
2020-06-25 20:52:41 -05:00
|
|
|
let path = src_root.join(src);
|
|
|
|
if path.exists() {
|
|
|
|
cfg.file(path);
|
|
|
|
}
|
2017-02-13 03:57:50 -06:00
|
|
|
}
|
|
|
|
|
2020-01-03 17:13:25 -06:00
|
|
|
cfg.include(root.join("include"));
|
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 23:52:50 -06:00
|
|
|
cfg.warnings(false);
|
2017-11-26 15:43:24 -06:00
|
|
|
cfg.compile("profiler-rt");
|
2017-02-13 03:57:50 -06:00
|
|
|
}
|