]> git.lizzy.rs Git - rust.git/blob - library/profiler_builtins/build.rs
Rollup merge of #107116 - ozkanonur:consolidate-bootstrap-docs, r=jyn514
[rust.git] / library / profiler_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     // FIXME: `rerun-if-changed` directives are not currently emitted and the build script
13     // will not rerun on changes in these source files or headers included into them.
14     let mut profile_sources = vec![
15         "GCDAProfiling.c",
16         "InstrProfiling.c",
17         "InstrProfilingBuffer.c",
18         "InstrProfilingFile.c",
19         "InstrProfilingMerge.c",
20         "InstrProfilingMergeFile.c",
21         "InstrProfilingNameVar.c",
22         "InstrProfilingPlatformDarwin.c",
23         "InstrProfilingPlatformFuchsia.c",
24         "InstrProfilingPlatformLinux.c",
25         "InstrProfilingPlatformOther.c",
26         "InstrProfilingPlatformWindows.c",
27         "InstrProfilingRuntime.cpp",
28         "InstrProfilingUtil.c",
29         "InstrProfilingValue.c",
30         "InstrProfilingVersionVar.c",
31         "InstrProfilingWriter.c",
32         // These files were added in LLVM 11.
33         "InstrProfilingInternal.c",
34         "InstrProfilingBiasVar.c",
35     ];
36
37     if target.contains("msvc") {
38         // Don't pull in extra libraries on MSVC
39         cfg.flag("/Zl");
40         profile_sources.push("WindowsMMap.c");
41         cfg.define("strdup", Some("_strdup"));
42         cfg.define("open", Some("_open"));
43         cfg.define("fdopen", Some("_fdopen"));
44         cfg.define("getpid", Some("_getpid"));
45         cfg.define("fileno", Some("_fileno"));
46     } else {
47         // Turn off various features of gcc and such, mostly copying
48         // compiler-rt's build system already
49         cfg.flag("-fno-builtin");
50         cfg.flag("-fomit-frame-pointer");
51         cfg.define("VISIBILITY_HIDDEN", None);
52         if !target.contains("windows") {
53             cfg.flag("-fvisibility=hidden");
54             cfg.define("COMPILER_RT_HAS_UNAME", Some("1"));
55         } else {
56             profile_sources.push("WindowsMMap.c");
57         }
58     }
59
60     // Assume that the Unixes we are building this for have fnctl() available
61     if env::var_os("CARGO_CFG_UNIX").is_some() {
62         cfg.define("COMPILER_RT_HAS_FCNTL_LCK", Some("1"));
63     }
64
65     // This should be a pretty good heuristic for when to set
66     // COMPILER_RT_HAS_ATOMICS
67     if env::var_os("CARGO_CFG_TARGET_HAS_ATOMIC")
68         .map(|features| features.to_string_lossy().to_lowercase().contains("ptr"))
69         .unwrap_or(false)
70     {
71         cfg.define("COMPILER_RT_HAS_ATOMICS", Some("1"));
72     }
73
74     // Note that this should exist if we're going to run (otherwise we just
75     // don't build profiler builtins at all).
76     let root = Path::new("../../src/llvm-project/compiler-rt");
77
78     let src_root = root.join("lib").join("profile");
79     for src in profile_sources {
80         let path = src_root.join(src);
81         if path.exists() {
82             cfg.file(path);
83         }
84     }
85
86     cfg.include(root.join("include"));
87     cfg.warnings(false);
88     cfg.compile("profiler-rt");
89 }