1
1
//! Compiles the profiler part of the `compiler-rt` library.
2
2
//!
3
- //! See the build.rs for libcompiler_builtins crate for details.
3
+ //! Loosely based on:
4
+ //! - LLVM's `compiler-rt/lib/profile/CMakeLists.txt`
5
+ //! - <https://github.com/rust-lang/compiler-builtins/blob/master/build.rs>.
4
6
5
7
use std:: env;
6
8
use std:: path:: PathBuf ;
7
9
8
10
fn main ( ) {
9
- println ! ( "cargo:rerun-if-env-changed=LLVM_PROFILER_RT_LIB" ) ;
10
- if let Ok ( rt) = env:: var ( "LLVM_PROFILER_RT_LIB" ) {
11
- println ! ( "cargo:rustc-link-lib=static:+verbatim={rt}" ) ;
11
+ if let Ok ( rt) = tracked_env_var ( "LLVM_PROFILER_RT_LIB" ) {
12
+ println ! ( "cargo::rustc-link-lib=static:+verbatim={rt}" ) ;
12
13
return ;
13
14
}
14
15
15
16
let target_os = env:: var ( "CARGO_CFG_TARGET_OS" ) . expect ( "CARGO_CFG_TARGET_OS was not set" ) ;
16
17
let target_env = env:: var ( "CARGO_CFG_TARGET_ENV" ) . expect ( "CARGO_CFG_TARGET_ENV was not set" ) ;
17
18
let cfg = & mut cc:: Build :: new ( ) ;
18
19
19
- // FIXME: `rerun-if-changed` directives are not currently emitted and the build script
20
- // will not rerun on changes in these source files or headers included into them.
21
- let mut profile_sources = vec ! [
20
+ let profile_sources = vec ! [
21
+ // tidy-alphabetical-start
22
22
"GCDAProfiling.c" ,
23
23
"InstrProfiling.c" ,
24
24
"InstrProfilingBuffer.c" ,
25
25
"InstrProfilingFile.c" ,
26
+ "InstrProfilingInternal.c" ,
26
27
"InstrProfilingMerge.c" ,
27
28
"InstrProfilingMergeFile.c" ,
28
29
"InstrProfilingNameVar.c" ,
@@ -37,15 +38,13 @@ fn main() {
37
38
"InstrProfilingValue.c" ,
38
39
"InstrProfilingVersionVar.c" ,
39
40
"InstrProfilingWriter.c" ,
40
- // These files were added in LLVM 11.
41
- "InstrProfilingInternal.c" ,
42
- "InstrProfilingBiasVar.c" ,
41
+ "WindowsMMap.c" ,
42
+ // tidy-alphabetical-end
43
43
] ;
44
44
45
45
if target_env == "msvc" {
46
46
// Don't pull in extra libraries on MSVC
47
47
cfg. flag ( "/Zl" ) ;
48
- profile_sources. push ( "WindowsMMap.c" ) ;
49
48
cfg. define ( "strdup" , Some ( "_strdup" ) ) ;
50
49
cfg. define ( "open" , Some ( "_open" ) ) ;
51
50
cfg. define ( "fdopen" , Some ( "_fdopen" ) ) ;
@@ -60,8 +59,6 @@ fn main() {
60
59
if target_os != "windows" {
61
60
cfg. flag ( "-fvisibility=hidden" ) ;
62
61
cfg. define ( "COMPILER_RT_HAS_UNAME" , Some ( "1" ) ) ;
63
- } else {
64
- profile_sources. push ( "WindowsMMap.c" ) ;
65
62
}
66
63
}
67
64
@@ -80,26 +77,33 @@ fn main() {
80
77
}
81
78
82
79
// Get the LLVM `compiler-rt` directory from bootstrap.
83
- println ! ( "cargo:rerun-if-env-changed=RUST_COMPILER_RT_FOR_PROFILER" ) ;
84
- let root = PathBuf :: from ( env:: var ( "RUST_COMPILER_RT_FOR_PROFILER" ) . unwrap_or_else ( |_| {
85
- let path = "../../src/llvm-project/compiler-rt" ;
86
- println ! ( "RUST_COMPILER_RT_FOR_PROFILER was not set; falling back to {path:?}" ) ;
87
- path. to_owned ( )
88
- } ) ) ;
80
+ let root = PathBuf :: from ( tracked_env_var_or_fallback (
81
+ "RUST_COMPILER_RT_FOR_PROFILER" ,
82
+ "../../src/llvm-project/compiler-rt" ,
83
+ ) ) ;
89
84
90
85
let src_root = root. join ( "lib" ) . join ( "profile" ) ;
91
86
assert ! ( src_root. exists( ) , "profiler runtime source directory not found: {src_root:?}" ) ;
92
- let mut n_sources_found = 0u32 ;
93
- for src in profile_sources {
94
- let path = src_root. join ( src) ;
95
- if path. exists ( ) {
96
- cfg. file ( path) ;
97
- n_sources_found += 1 ;
98
- }
87
+ println ! ( "cargo::rerun-if-changed={}" , src_root. display( ) ) ;
88
+ for file in profile_sources {
89
+ cfg. file ( src_root. join ( file) ) ;
99
90
}
100
- assert ! ( n_sources_found > 0 , "couldn't find any profiler runtime source files in {src_root:?}" ) ;
101
91
102
- cfg. include ( root. join ( "include" ) ) ;
92
+ let include = root. join ( "include" ) ;
93
+ println ! ( "cargo::rerun-if-changed={}" , include. display( ) ) ;
94
+ cfg. include ( include) ;
95
+
103
96
cfg. warnings ( false ) ;
104
97
cfg. compile ( "profiler-rt" ) ;
105
98
}
99
+
100
+ fn tracked_env_var ( key : & str ) -> Result < String , env:: VarError > {
101
+ println ! ( "cargo::rerun-if-env-changed={key}" ) ;
102
+ env:: var ( key)
103
+ }
104
+ fn tracked_env_var_or_fallback ( key : & str , fallback : & str ) -> String {
105
+ tracked_env_var ( key) . unwrap_or_else ( |_| {
106
+ println ! ( "cargo::warning={key} was not set; falling back to {fallback:?}" ) ;
107
+ fallback. to_owned ( )
108
+ } )
109
+ }
0 commit comments