11//! Compiles the profiler part of the `compiler-rt` library.
22//!
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>.
46
57use std:: env;
68use std:: path:: PathBuf ;
79
810fn 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}" ) ;
1213 return ;
1314 }
1415
1516 let target_os = env:: var ( "CARGO_CFG_TARGET_OS" ) . expect ( "CARGO_CFG_TARGET_OS was not set" ) ;
1617 let target_env = env:: var ( "CARGO_CFG_TARGET_ENV" ) . expect ( "CARGO_CFG_TARGET_ENV was not set" ) ;
1718 let cfg = & mut cc:: Build :: new ( ) ;
1819
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
2222 "GCDAProfiling.c" ,
2323 "InstrProfiling.c" ,
2424 "InstrProfilingBuffer.c" ,
2525 "InstrProfilingFile.c" ,
26+ "InstrProfilingInternal.c" ,
2627 "InstrProfilingMerge.c" ,
2728 "InstrProfilingMergeFile.c" ,
2829 "InstrProfilingNameVar.c" ,
@@ -37,15 +38,13 @@ fn main() {
3738 "InstrProfilingValue.c" ,
3839 "InstrProfilingVersionVar.c" ,
3940 "InstrProfilingWriter.c" ,
40- // These files were added in LLVM 11.
41- "InstrProfilingInternal.c" ,
42- "InstrProfilingBiasVar.c" ,
41+ "WindowsMMap.c" ,
42+ // tidy-alphabetical-end
4343 ] ;
4444
4545 if target_env == "msvc" {
4646 // Don't pull in extra libraries on MSVC
4747 cfg. flag ( "/Zl" ) ;
48- profile_sources. push ( "WindowsMMap.c" ) ;
4948 cfg. define ( "strdup" , Some ( "_strdup" ) ) ;
5049 cfg. define ( "open" , Some ( "_open" ) ) ;
5150 cfg. define ( "fdopen" , Some ( "_fdopen" ) ) ;
@@ -60,8 +59,6 @@ fn main() {
6059 if target_os != "windows" {
6160 cfg. flag ( "-fvisibility=hidden" ) ;
6261 cfg. define ( "COMPILER_RT_HAS_UNAME" , Some ( "1" ) ) ;
63- } else {
64- profile_sources. push ( "WindowsMMap.c" ) ;
6562 }
6663 }
6764
@@ -80,26 +77,33 @@ fn main() {
8077 }
8178
8279 // 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+ ) ) ;
8984
9085 let src_root = root. join ( "lib" ) . join ( "profile" ) ;
9186 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) ) ;
9990 }
100- assert ! ( n_sources_found > 0 , "couldn't find any profiler runtime source files in {src_root:?}" ) ;
10191
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+
10396 cfg. warnings ( false ) ;
10497 cfg. compile ( "profiler-rt" ) ;
10598}
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