]> git.lizzy.rs Git - rust.git/blob - src/libprofiler_builtins/build.rs
Rollup merge of #73774 - ecstatic-morse:liveness-of-projections, r=oli-obk
[rust.git] / src / libprofiler_builtins / build.rs
1 //! Compiles the profiler part of the `compiler-rt` library.
2 //!
3 //! See the build.rs for libcompiler_builtins crate for details.
4
5 use std::env;
6 use std::path::Path;
7
8 fn main() {
9     let target = env::var("TARGET").expect("TARGET was not set");
10     let cfg = &mut cc::Build::new();
11
12     let mut profile_sources = vec![
13         "GCDAProfiling.c",
14         "InstrProfiling.c",
15         "InstrProfilingBuffer.c",
16         "InstrProfilingFile.c",
17         "InstrProfilingMerge.c",
18         "InstrProfilingMergeFile.c",
19         "InstrProfilingNameVar.c",
20         "InstrProfilingPlatformDarwin.c",
21         "InstrProfilingPlatformLinux.c",
22         "InstrProfilingPlatformOther.c",
23         "InstrProfilingPlatformWindows.c",
24         "InstrProfilingUtil.c",
25         "InstrProfilingValue.c",
26         "InstrProfilingWriter.c",
27         // This file was renamed in LLVM 10.
28         "InstrProfilingRuntime.cc",
29         "InstrProfilingRuntime.cpp",
30         // These files were added in LLVM 11.
31         "InstrProfilingInternal.c",
32         "InstrProfilingBiasVar.c",
33     ];
34
35     if target.contains("msvc") {
36         // Don't pull in extra libraries on MSVC
37         cfg.flag("/Zl");
38         profile_sources.push("WindowsMMap.c");
39         cfg.define("strdup", Some("_strdup"));
40         cfg.define("open", Some("_open"));
41         cfg.define("fdopen", Some("_fdopen"));
42         cfg.define("getpid", Some("_getpid"));
43         cfg.define("fileno", Some("_fileno"));
44     } else {
45         // Turn off various features of gcc and such, mostly copying
46         // compiler-rt's build system already
47         cfg.flag("-fno-builtin");
48         cfg.flag("-fvisibility=hidden");
49         cfg.flag("-fomit-frame-pointer");
50         cfg.define("VISIBILITY_HIDDEN", None);
51         if !target.contains("windows") {
52             cfg.define("COMPILER_RT_HAS_UNAME", Some("1"));
53         } else {
54             profile_sources.push("WindowsMMap.c");
55         }
56     }
57
58     // Assume that the Unixes we are building this for have fnctl() available
59     if env::var_os("CARGO_CFG_UNIX").is_some() {
60         cfg.define("COMPILER_RT_HAS_FCNTL_LCK", Some("1"));
61     }
62
63     // This should be a pretty good heuristic for when to set
64     // COMPILER_RT_HAS_ATOMICS
65     if env::var_os("CARGO_CFG_TARGET_HAS_ATOMIC")
66         .map(|features| features.to_string_lossy().to_lowercase().contains("cas"))
67         .unwrap_or(false)
68     {
69         cfg.define("COMPILER_RT_HAS_ATOMICS", Some("1"));
70     }
71
72     // Note that this should exist if we're going to run (otherwise we just
73     // don't build profiler builtins at all).
74     let root = Path::new("../llvm-project/compiler-rt");
75
76     let src_root = root.join("lib").join("profile");
77     for src in profile_sources {
78         let path = src_root.join(src);
79         if path.exists() {
80             cfg.file(path);
81         }
82     }
83
84     cfg.include(root.join("include"));
85     cfg.warnings(false);
86     cfg.compile("profiler-rt");
87 }