bootstrap/core/build_steps/
llvm.rs

1//! Compilation of native dependencies like LLVM.
2//!
3//! Native projects like LLVM unfortunately aren't suited just yet for
4//! compilation in build scripts that Cargo has. This is because the
5//! compilation takes a *very* long time but also because we don't want to
6//! compile LLVM 3 times as part of a normal bootstrap (we want it cached).
7//!
8//! LLVM and compiler-rt are essentially just wired up to everything else to
9//! ensure that they're always in place if needed.
10
11use std::env::consts::EXE_EXTENSION;
12use std::ffi::{OsStr, OsString};
13use std::path::{Path, PathBuf};
14use std::sync::OnceLock;
15use std::{env, fs};
16
17use build_helper::git::get_closest_merge_commit;
18#[cfg(feature = "tracing")]
19use tracing::instrument;
20
21use crate::core::builder::{Builder, RunConfig, ShouldRun, Step};
22use crate::core::config::{Config, TargetSelection};
23use crate::utils::build_stamp::{BuildStamp, generate_smart_stamp_hash};
24use crate::utils::exec::command;
25use crate::utils::helpers::{
26    self, exe, get_clang_cl_resource_dir, t, unhashed_basename, up_to_date,
27};
28use crate::{CLang, GitRepo, Kind, trace};
29
30#[derive(Clone)]
31pub struct LlvmResult {
32    /// Path to llvm-config binary.
33    /// NB: This is always the host llvm-config!
34    pub llvm_config: PathBuf,
35    /// Path to LLVM cmake directory for the target.
36    pub llvm_cmake_dir: PathBuf,
37}
38
39pub struct Meta {
40    stamp: BuildStamp,
41    res: LlvmResult,
42    out_dir: PathBuf,
43    root: String,
44}
45
46pub enum LlvmBuildStatus {
47    AlreadyBuilt(LlvmResult),
48    ShouldBuild(Meta),
49}
50
51impl LlvmBuildStatus {
52    pub fn should_build(&self) -> bool {
53        match self {
54            LlvmBuildStatus::AlreadyBuilt(_) => false,
55            LlvmBuildStatus::ShouldBuild(_) => true,
56        }
57    }
58
59    #[cfg(test)]
60    pub fn llvm_result(&self) -> &LlvmResult {
61        match self {
62            LlvmBuildStatus::AlreadyBuilt(res) => res,
63            LlvmBuildStatus::ShouldBuild(meta) => &meta.res,
64        }
65    }
66}
67
68/// Linker flags to pass to LLVM's CMake invocation.
69#[derive(Debug, Clone, Default)]
70struct LdFlags {
71    /// CMAKE_EXE_LINKER_FLAGS
72    exe: OsString,
73    /// CMAKE_SHARED_LINKER_FLAGS
74    shared: OsString,
75    /// CMAKE_MODULE_LINKER_FLAGS
76    module: OsString,
77}
78
79impl LdFlags {
80    fn push_all(&mut self, s: impl AsRef<OsStr>) {
81        let s = s.as_ref();
82        self.exe.push(" ");
83        self.exe.push(s);
84        self.shared.push(" ");
85        self.shared.push(s);
86        self.module.push(" ");
87        self.module.push(s);
88    }
89}
90
91/// This returns whether we've already previously built LLVM.
92///
93/// It's used to avoid busting caches during x.py check -- if we've already built
94/// LLVM, it's fine for us to not try to avoid doing so.
95///
96/// This will return the llvm-config if it can get it (but it will not build it
97/// if not).
98pub fn prebuilt_llvm_config(
99    builder: &Builder<'_>,
100    target: TargetSelection,
101    // Certain commands (like `x test mir-opt --bless`) may call this function with different targets,
102    // which could bypass the CI LLVM early-return even if `builder.config.llvm_from_ci` is true.
103    // This flag should be `true` only if the caller needs the LLVM sources (e.g., if it will build LLVM).
104    handle_submodule_when_needed: bool,
105) -> LlvmBuildStatus {
106    builder.config.maybe_download_ci_llvm();
107
108    // If we're using a custom LLVM bail out here, but we can only use a
109    // custom LLVM for the build triple.
110    if let Some(config) = builder.config.target_config.get(&target) {
111        if let Some(ref s) = config.llvm_config {
112            check_llvm_version(builder, s);
113            let llvm_config = s.to_path_buf();
114            let mut llvm_cmake_dir = llvm_config.clone();
115            llvm_cmake_dir.pop();
116            llvm_cmake_dir.pop();
117            llvm_cmake_dir.push("lib");
118            llvm_cmake_dir.push("cmake");
119            llvm_cmake_dir.push("llvm");
120            return LlvmBuildStatus::AlreadyBuilt(LlvmResult { llvm_config, llvm_cmake_dir });
121        }
122    }
123
124    if handle_submodule_when_needed {
125        // If submodules are disabled, this does nothing.
126        builder.config.update_submodule("src/llvm-project");
127    }
128
129    let root = "src/llvm-project/llvm";
130    let out_dir = builder.llvm_out(target);
131
132    let build_llvm_config = if let Some(build_llvm_config) = builder
133        .config
134        .target_config
135        .get(&builder.config.build)
136        .and_then(|config| config.llvm_config.clone())
137    {
138        build_llvm_config
139    } else {
140        let mut llvm_config_ret_dir = builder.llvm_out(builder.config.build);
141        llvm_config_ret_dir.push("bin");
142        llvm_config_ret_dir.join(exe("llvm-config", builder.config.build))
143    };
144
145    let llvm_cmake_dir = out_dir.join("lib/cmake/llvm");
146    let res = LlvmResult { llvm_config: build_llvm_config, llvm_cmake_dir };
147
148    static STAMP_HASH_MEMO: OnceLock<String> = OnceLock::new();
149    let smart_stamp_hash = STAMP_HASH_MEMO.get_or_init(|| {
150        generate_smart_stamp_hash(
151            builder,
152            &builder.config.src.join("src/llvm-project"),
153            builder.in_tree_llvm_info.sha().unwrap_or_default(),
154        )
155    });
156
157    let stamp = BuildStamp::new(&out_dir).with_prefix("llvm").add_stamp(smart_stamp_hash);
158
159    if stamp.is_up_to_date() {
160        if stamp.stamp().is_empty() {
161            builder.info(
162                "Could not determine the LLVM submodule commit hash. \
163                     Assuming that an LLVM rebuild is not necessary.",
164            );
165            builder.info(&format!(
166                "To force LLVM to rebuild, remove the file `{}`",
167                stamp.path().display()
168            ));
169        }
170        return LlvmBuildStatus::AlreadyBuilt(res);
171    }
172
173    LlvmBuildStatus::ShouldBuild(Meta { stamp, res, out_dir, root: root.into() })
174}
175
176/// Paths whose changes invalidate LLVM downloads.
177pub const LLVM_INVALIDATION_PATHS: &[&str] = &[
178    "src/llvm-project",
179    "src/bootstrap/download-ci-llvm-stamp",
180    // the LLVM shared object file is named `LLVM-<LLVM-version>-rust-{version}-nightly`
181    "src/version",
182];
183
184/// This retrieves the LLVM sha we *want* to use, according to git history.
185pub(crate) fn detect_llvm_sha(config: &Config, is_git: bool) -> String {
186    let llvm_sha = if is_git {
187        get_closest_merge_commit(Some(&config.src), &config.git_config(), LLVM_INVALIDATION_PATHS)
188            .unwrap()
189    } else if let Some(info) = crate::utils::channel::read_commit_info_file(&config.src) {
190        info.sha.trim().to_owned()
191    } else {
192        "".to_owned()
193    };
194
195    if llvm_sha.is_empty() {
196        eprintln!("error: could not find commit hash for downloading LLVM");
197        eprintln!("HELP: maybe your repository history is too shallow?");
198        eprintln!("HELP: consider disabling `download-ci-llvm`");
199        eprintln!("HELP: or fetch enough history to include one upstream commit");
200        panic!();
201    }
202
203    llvm_sha
204}
205
206/// Returns whether the CI-found LLVM is currently usable.
207///
208/// This checks the build triple platform to confirm we're usable at all, and if LLVM
209/// with/without assertions is available.
210pub(crate) fn is_ci_llvm_available_for_target(config: &Config, asserts: bool) -> bool {
211    // This is currently all tier 1 targets and tier 2 targets with host tools
212    // (since others may not have CI artifacts)
213    // https://doc.rust-lang.org/rustc/platform-support.html#tier-1
214    let supported_platforms = [
215        // tier 1
216        ("aarch64-unknown-linux-gnu", false),
217        ("aarch64-apple-darwin", false),
218        ("i686-pc-windows-gnu", false),
219        ("i686-pc-windows-msvc", false),
220        ("i686-unknown-linux-gnu", false),
221        ("x86_64-unknown-linux-gnu", true),
222        ("x86_64-apple-darwin", true),
223        ("x86_64-pc-windows-gnu", true),
224        ("x86_64-pc-windows-msvc", true),
225        // tier 2 with host tools
226        ("aarch64-pc-windows-msvc", false),
227        ("aarch64-unknown-linux-musl", false),
228        ("arm-unknown-linux-gnueabi", false),
229        ("arm-unknown-linux-gnueabihf", false),
230        ("armv7-unknown-linux-gnueabihf", false),
231        ("loongarch64-unknown-linux-gnu", false),
232        ("loongarch64-unknown-linux-musl", false),
233        ("mips-unknown-linux-gnu", false),
234        ("mips64-unknown-linux-gnuabi64", false),
235        ("mips64el-unknown-linux-gnuabi64", false),
236        ("mipsel-unknown-linux-gnu", false),
237        ("powerpc-unknown-linux-gnu", false),
238        ("powerpc64-unknown-linux-gnu", false),
239        ("powerpc64le-unknown-linux-gnu", false),
240        ("powerpc64le-unknown-linux-musl", false),
241        ("riscv64gc-unknown-linux-gnu", false),
242        ("s390x-unknown-linux-gnu", false),
243        ("x86_64-unknown-freebsd", false),
244        ("x86_64-unknown-illumos", false),
245        ("x86_64-unknown-linux-musl", false),
246        ("x86_64-unknown-netbsd", false),
247    ];
248
249    if !supported_platforms.contains(&(&*config.build.triple, asserts))
250        && (asserts || !supported_platforms.contains(&(&*config.build.triple, true)))
251    {
252        return false;
253    }
254
255    true
256}
257
258#[derive(Debug, Clone, Hash, PartialEq, Eq)]
259pub struct Llvm {
260    pub target: TargetSelection,
261}
262
263impl Step for Llvm {
264    type Output = LlvmResult;
265
266    const ONLY_HOSTS: bool = true;
267
268    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
269        run.path("src/llvm-project").path("src/llvm-project/llvm")
270    }
271
272    fn make_run(run: RunConfig<'_>) {
273        run.builder.ensure(Llvm { target: run.target });
274    }
275
276    /// Compile LLVM for `target`.
277    fn run(self, builder: &Builder<'_>) -> LlvmResult {
278        let target = self.target;
279        let target_native = if self.target.starts_with("riscv") {
280            // RISC-V target triples in Rust is not named the same as C compiler target triples.
281            // This converts Rust RISC-V target triples to C compiler triples.
282            let idx = target.triple.find('-').unwrap();
283
284            format!("riscv{}{}", &target.triple[5..7], &target.triple[idx..])
285        } else if self.target.starts_with("powerpc") && self.target.ends_with("freebsd") {
286            // FreeBSD 13 had incompatible ABI changes on all PowerPC platforms.
287            // Set the version suffix to 13.0 so the correct target details are used.
288            format!("{}{}", self.target, "13.0")
289        } else {
290            target.to_string()
291        };
292
293        // If LLVM has already been built or been downloaded through download-ci-llvm, we avoid building it again.
294        let Meta { stamp, res, out_dir, root } = match prebuilt_llvm_config(builder, target, true) {
295            LlvmBuildStatus::AlreadyBuilt(p) => return p,
296            LlvmBuildStatus::ShouldBuild(m) => m,
297        };
298
299        if builder.llvm_link_shared() && target.is_windows() {
300            panic!("shared linking to LLVM is not currently supported on {}", target.triple);
301        }
302
303        let _guard = builder.msg_unstaged(Kind::Build, "LLVM", target);
304        t!(stamp.remove());
305        let _time = helpers::timeit(builder);
306        t!(fs::create_dir_all(&out_dir));
307
308        // https://llvm.org/docs/CMake.html
309        let mut cfg = cmake::Config::new(builder.src.join(root));
310        let mut ldflags = LdFlags::default();
311
312        let profile = match (builder.config.llvm_optimize, builder.config.llvm_release_debuginfo) {
313            (false, _) => "Debug",
314            (true, false) => "Release",
315            (true, true) => "RelWithDebInfo",
316        };
317
318        // NOTE: remember to also update `bootstrap.example.toml` when changing the
319        // defaults!
320        let llvm_targets = match &builder.config.llvm_targets {
321            Some(s) => s,
322            None => {
323                "AArch64;AMDGPU;ARM;BPF;Hexagon;LoongArch;MSP430;Mips;NVPTX;PowerPC;RISCV;\
324                     Sparc;SystemZ;WebAssembly;X86"
325            }
326        };
327
328        let llvm_exp_targets = match builder.config.llvm_experimental_targets {
329            Some(ref s) => s,
330            None => "AVR;M68k;CSKY;Xtensa",
331        };
332
333        let assertions = if builder.config.llvm_assertions { "ON" } else { "OFF" };
334        let plugins = if builder.config.llvm_plugins { "ON" } else { "OFF" };
335        let enable_tests = if builder.config.llvm_tests { "ON" } else { "OFF" };
336        let enable_warnings = if builder.config.llvm_enable_warnings { "ON" } else { "OFF" };
337
338        cfg.out_dir(&out_dir)
339            .profile(profile)
340            .define("LLVM_ENABLE_ASSERTIONS", assertions)
341            .define("LLVM_UNREACHABLE_OPTIMIZE", "OFF")
342            .define("LLVM_ENABLE_PLUGINS", plugins)
343            .define("LLVM_TARGETS_TO_BUILD", llvm_targets)
344            .define("LLVM_EXPERIMENTAL_TARGETS_TO_BUILD", llvm_exp_targets)
345            .define("LLVM_INCLUDE_EXAMPLES", "OFF")
346            .define("LLVM_INCLUDE_DOCS", "OFF")
347            .define("LLVM_INCLUDE_BENCHMARKS", "OFF")
348            .define("LLVM_INCLUDE_TESTS", enable_tests)
349            // FIXME: remove this when minimal llvm is 19
350            .define("LLVM_ENABLE_TERMINFO", "OFF")
351            .define("LLVM_ENABLE_LIBEDIT", "OFF")
352            .define("LLVM_ENABLE_BINDINGS", "OFF")
353            .define("LLVM_ENABLE_Z3_SOLVER", "OFF")
354            .define("LLVM_PARALLEL_COMPILE_JOBS", builder.jobs().to_string())
355            .define("LLVM_TARGET_ARCH", target_native.split('-').next().unwrap())
356            .define("LLVM_DEFAULT_TARGET_TRIPLE", target_native)
357            .define("LLVM_ENABLE_WARNINGS", enable_warnings);
358
359        // Parts of our test suite rely on the `FileCheck` tool, which is built by default in
360        // `build/$TARGET/llvm/build/bin` is but *not* then installed to `build/$TARGET/llvm/bin`.
361        // This flag makes sure `FileCheck` is copied in the final binaries directory.
362        cfg.define("LLVM_INSTALL_UTILS", "ON");
363
364        if builder.config.llvm_profile_generate {
365            cfg.define("LLVM_BUILD_INSTRUMENTED", "IR");
366            if let Ok(llvm_profile_dir) = std::env::var("LLVM_PROFILE_DIR") {
367                cfg.define("LLVM_PROFILE_DATA_DIR", llvm_profile_dir);
368            }
369            cfg.define("LLVM_BUILD_RUNTIME", "No");
370        }
371        if let Some(path) = builder.config.llvm_profile_use.as_ref() {
372            cfg.define("LLVM_PROFDATA_FILE", path);
373        }
374
375        // Libraries for ELF section compression.
376        if !target.is_windows() {
377            cfg.define("LLVM_ENABLE_ZLIB", "ON");
378        } else {
379            cfg.define("LLVM_ENABLE_ZLIB", "OFF");
380        }
381
382        // Are we compiling for iOS/tvOS/watchOS/visionOS?
383        if target.contains("apple-ios")
384            || target.contains("apple-tvos")
385            || target.contains("apple-watchos")
386            || target.contains("apple-visionos")
387        {
388            // Prevent cmake from adding -bundle to CFLAGS automatically, which leads to a compiler error because "-bitcode_bundle" also gets added.
389            cfg.define("LLVM_ENABLE_PLUGINS", "OFF");
390            // Zlib fails to link properly, leading to a compiler error.
391            cfg.define("LLVM_ENABLE_ZLIB", "OFF");
392        }
393
394        // This setting makes the LLVM tools link to the dynamic LLVM library,
395        // which saves both memory during parallel links and overall disk space
396        // for the tools. We don't do this on every platform as it doesn't work
397        // equally well everywhere.
398        if builder.llvm_link_shared() {
399            cfg.define("LLVM_LINK_LLVM_DYLIB", "ON");
400        }
401
402        if (target.starts_with("csky")
403            || target.starts_with("riscv")
404            || target.starts_with("sparc-"))
405            && !target.contains("freebsd")
406            && !target.contains("openbsd")
407            && !target.contains("netbsd")
408        {
409            // CSKY and RISC-V GCC erroneously requires linking against
410            // `libatomic` when using 1-byte and 2-byte C++
411            // atomics but the LLVM build system check cannot
412            // detect this. Therefore it is set manually here.
413            // Some BSD uses Clang as its system compiler and
414            // provides no libatomic in its base system so does
415            // not want this. 32-bit SPARC requires linking against
416            // libatomic as well.
417            ldflags.exe.push(" -latomic");
418            ldflags.shared.push(" -latomic");
419        }
420
421        if target.starts_with("mips") && target.contains("netbsd") {
422            // LLVM wants 64-bit atomics, while mipsel is 32-bit only, so needs -latomic
423            ldflags.exe.push(" -latomic");
424            ldflags.shared.push(" -latomic");
425        }
426
427        if target.is_msvc() {
428            cfg.define("CMAKE_MSVC_RUNTIME_LIBRARY", "MultiThreaded");
429            cfg.static_crt(true);
430        }
431
432        if target.starts_with("i686") {
433            cfg.define("LLVM_BUILD_32_BITS", "ON");
434        }
435
436        if target.starts_with("x86_64") && target.contains("ohos") {
437            cfg.define("LLVM_TOOL_LLVM_RTDYLD_BUILD", "OFF");
438        }
439
440        let mut enabled_llvm_projects = Vec::new();
441
442        if helpers::forcing_clang_based_tests() {
443            enabled_llvm_projects.push("clang");
444        }
445
446        if builder.config.llvm_polly {
447            enabled_llvm_projects.push("polly");
448        }
449
450        if builder.config.llvm_clang {
451            enabled_llvm_projects.push("clang");
452        }
453
454        // We want libxml to be disabled.
455        // See https://github.com/rust-lang/rust/pull/50104
456        cfg.define("LLVM_ENABLE_LIBXML2", "OFF");
457
458        if !enabled_llvm_projects.is_empty() {
459            enabled_llvm_projects.sort();
460            enabled_llvm_projects.dedup();
461            cfg.define("LLVM_ENABLE_PROJECTS", enabled_llvm_projects.join(";"));
462        }
463
464        let mut enabled_llvm_runtimes = Vec::new();
465
466        if helpers::forcing_clang_based_tests() {
467            enabled_llvm_runtimes.push("compiler-rt");
468        }
469
470        if builder.config.llvm_offload {
471            enabled_llvm_runtimes.push("offload");
472            //FIXME(ZuseZ4): LLVM intends to drop the offload dependency on openmp.
473            //Remove this line once they achieved it.
474            enabled_llvm_runtimes.push("openmp");
475        }
476
477        if !enabled_llvm_runtimes.is_empty() {
478            enabled_llvm_runtimes.sort();
479            enabled_llvm_runtimes.dedup();
480            cfg.define("LLVM_ENABLE_RUNTIMES", enabled_llvm_runtimes.join(";"));
481        }
482
483        if let Some(num_linkers) = builder.config.llvm_link_jobs {
484            if num_linkers > 0 {
485                cfg.define("LLVM_PARALLEL_LINK_JOBS", num_linkers.to_string());
486            }
487        }
488
489        // https://llvm.org/docs/HowToCrossCompileLLVM.html
490        if !builder.is_builder_target(target) {
491            let LlvmResult { llvm_config, .. } =
492                builder.ensure(Llvm { target: builder.config.build });
493            if !builder.config.dry_run() {
494                let llvm_bindir =
495                    command(&llvm_config).arg("--bindir").run_capture_stdout(builder).stdout();
496                let host_bin = Path::new(llvm_bindir.trim());
497                cfg.define(
498                    "LLVM_TABLEGEN",
499                    host_bin.join("llvm-tblgen").with_extension(EXE_EXTENSION),
500                );
501                // LLVM_NM is required for cross compiling using MSVC
502                cfg.define("LLVM_NM", host_bin.join("llvm-nm").with_extension(EXE_EXTENSION));
503            }
504            cfg.define("LLVM_CONFIG_PATH", llvm_config);
505            if builder.config.llvm_clang {
506                let build_bin = builder.llvm_out(builder.config.build).join("build").join("bin");
507                let clang_tblgen = build_bin.join("clang-tblgen").with_extension(EXE_EXTENSION);
508                if !builder.config.dry_run() && !clang_tblgen.exists() {
509                    panic!("unable to find {}", clang_tblgen.display());
510                }
511                cfg.define("CLANG_TABLEGEN", clang_tblgen);
512            }
513        }
514
515        let llvm_version_suffix = if let Some(ref suffix) = builder.config.llvm_version_suffix {
516            // Allow version-suffix="" to not define a version suffix at all.
517            if !suffix.is_empty() { Some(suffix.to_string()) } else { None }
518        } else if builder.config.channel == "dev" {
519            // Changes to a version suffix require a complete rebuild of the LLVM.
520            // To avoid rebuilds during a time of version bump, don't include rustc
521            // release number on the dev channel.
522            Some("-rust-dev".to_string())
523        } else {
524            Some(format!("-rust-{}-{}", builder.version, builder.config.channel))
525        };
526        if let Some(ref suffix) = llvm_version_suffix {
527            cfg.define("LLVM_VERSION_SUFFIX", suffix);
528        }
529
530        configure_cmake(builder, target, &mut cfg, true, ldflags, &[]);
531        configure_llvm(builder, target, &mut cfg);
532
533        for (key, val) in &builder.config.llvm_build_config {
534            cfg.define(key, val);
535        }
536
537        if builder.config.dry_run() {
538            return res;
539        }
540
541        cfg.build();
542
543        // Helper to find the name of LLVM's shared library on darwin and linux.
544        let find_llvm_lib_name = |extension| {
545            let major = get_llvm_version_major(builder, &res.llvm_config);
546            match &llvm_version_suffix {
547                Some(version_suffix) => format!("libLLVM-{major}{version_suffix}.{extension}"),
548                None => format!("libLLVM-{major}.{extension}"),
549            }
550        };
551
552        // FIXME(ZuseZ4): Do we need that for Enzyme too?
553        // When building LLVM with LLVM_LINK_LLVM_DYLIB for macOS, an unversioned
554        // libLLVM.dylib will be built. However, llvm-config will still look
555        // for a versioned path like libLLVM-14.dylib. Manually create a symbolic
556        // link to make llvm-config happy.
557        if builder.llvm_link_shared() && target.contains("apple-darwin") {
558            let lib_name = find_llvm_lib_name("dylib");
559            let lib_llvm = out_dir.join("build").join("lib").join(lib_name);
560            if !lib_llvm.exists() {
561                t!(builder.symlink_file("libLLVM.dylib", &lib_llvm));
562            }
563        }
564
565        // When building LLVM as a shared library on linux, it can contain unexpected debuginfo:
566        // some can come from the C++ standard library. Unless we're explicitly requesting LLVM to
567        // be built with debuginfo, strip it away after the fact, to make dist artifacts smaller.
568        if builder.llvm_link_shared()
569            && builder.config.llvm_optimize
570            && !builder.config.llvm_release_debuginfo
571        {
572            // Find the name of the LLVM shared library that we just built.
573            let lib_name = find_llvm_lib_name("so");
574
575            // If the shared library exists in LLVM's `/build/lib/` or `/lib/` folders, strip its
576            // debuginfo.
577            crate::core::build_steps::compile::strip_debug(
578                builder,
579                target,
580                &out_dir.join("lib").join(&lib_name),
581            );
582            crate::core::build_steps::compile::strip_debug(
583                builder,
584                target,
585                &out_dir.join("build").join("lib").join(&lib_name),
586            );
587        }
588
589        t!(stamp.write());
590
591        res
592    }
593}
594
595pub fn get_llvm_version(builder: &Builder<'_>, llvm_config: &Path) -> String {
596    command(llvm_config).arg("--version").run_capture_stdout(builder).stdout().trim().to_owned()
597}
598
599pub fn get_llvm_version_major(builder: &Builder<'_>, llvm_config: &Path) -> u8 {
600    let version = get_llvm_version(builder, llvm_config);
601    let major_str = version.split_once('.').expect("Failed to parse LLVM version").0;
602    major_str.parse().unwrap()
603}
604
605fn check_llvm_version(builder: &Builder<'_>, llvm_config: &Path) {
606    if builder.config.dry_run() {
607        return;
608    }
609
610    let version = get_llvm_version(builder, llvm_config);
611    let mut parts = version.split('.').take(2).filter_map(|s| s.parse::<u32>().ok());
612    if let (Some(major), Some(_minor)) = (parts.next(), parts.next()) {
613        if major >= 18 {
614            return;
615        }
616    }
617    panic!("\n\nbad LLVM version: {version}, need >=18\n\n")
618}
619
620fn configure_cmake(
621    builder: &Builder<'_>,
622    target: TargetSelection,
623    cfg: &mut cmake::Config,
624    use_compiler_launcher: bool,
625    mut ldflags: LdFlags,
626    suppressed_compiler_flag_prefixes: &[&str],
627) {
628    // Do not print installation messages for up-to-date files.
629    // LLVM and LLD builds can produce a lot of those and hit CI limits on log size.
630    cfg.define("CMAKE_INSTALL_MESSAGE", "LAZY");
631
632    // Do not allow the user's value of DESTDIR to influence where
633    // LLVM will install itself. LLVM must always be installed in our
634    // own build directories.
635    cfg.env("DESTDIR", "");
636
637    if builder.ninja() {
638        cfg.generator("Ninja");
639    }
640    cfg.target(&target.triple).host(&builder.config.build.triple);
641
642    if !builder.is_builder_target(target) {
643        cfg.define("CMAKE_CROSSCOMPILING", "True");
644
645        // NOTE: Ideally, we wouldn't have to do this, and `cmake-rs` would just handle it for us.
646        // But it currently determines this based on the `CARGO_CFG_TARGET_OS` environment variable,
647        // which isn't set when compiling outside `build.rs` (like bootstrap is).
648        //
649        // So for now, we define `CMAKE_SYSTEM_NAME` ourselves, to panicking in `cmake-rs`.
650        if target.contains("netbsd") {
651            cfg.define("CMAKE_SYSTEM_NAME", "NetBSD");
652        } else if target.contains("dragonfly") {
653            cfg.define("CMAKE_SYSTEM_NAME", "DragonFly");
654        } else if target.contains("openbsd") {
655            cfg.define("CMAKE_SYSTEM_NAME", "OpenBSD");
656        } else if target.contains("freebsd") {
657            cfg.define("CMAKE_SYSTEM_NAME", "FreeBSD");
658        } else if target.is_windows() {
659            cfg.define("CMAKE_SYSTEM_NAME", "Windows");
660        } else if target.contains("haiku") {
661            cfg.define("CMAKE_SYSTEM_NAME", "Haiku");
662        } else if target.contains("solaris") || target.contains("illumos") {
663            cfg.define("CMAKE_SYSTEM_NAME", "SunOS");
664        } else if target.contains("linux") {
665            cfg.define("CMAKE_SYSTEM_NAME", "Linux");
666        } else if target.contains("darwin") {
667            // macOS
668            cfg.define("CMAKE_SYSTEM_NAME", "Darwin");
669        } else if target.contains("ios") {
670            cfg.define("CMAKE_SYSTEM_NAME", "iOS");
671        } else if target.contains("tvos") {
672            cfg.define("CMAKE_SYSTEM_NAME", "tvOS");
673        } else if target.contains("visionos") {
674            cfg.define("CMAKE_SYSTEM_NAME", "visionOS");
675        } else if target.contains("watchos") {
676            cfg.define("CMAKE_SYSTEM_NAME", "watchOS");
677        } else if target.contains("none") {
678            // "none" should be the last branch
679            cfg.define("CMAKE_SYSTEM_NAME", "Generic");
680        } else {
681            builder.info(&format!(
682                "could not determine CMAKE_SYSTEM_NAME from the target `{target}`, build may fail",
683            ));
684            // Fallback, set `CMAKE_SYSTEM_NAME` anyhow to avoid the logic `cmake-rs` tries, and
685            // to avoid CMAKE_SYSTEM_NAME being inferred from the host.
686            cfg.define("CMAKE_SYSTEM_NAME", "Generic");
687        }
688
689        // When cross-compiling we should also set CMAKE_SYSTEM_VERSION, but in
690        // that case like CMake we cannot easily determine system version either.
691        //
692        // Since, the LLVM itself makes rather limited use of version checks in
693        // CMakeFiles (and then only in tests), and so far no issues have been
694        // reported, the system version is currently left unset.
695
696        if target.contains("apple") {
697            if !target.contains("darwin") {
698                // FIXME(madsmtm): compiler-rt's CMake setup is kinda weird, it seems like they do
699                // version testing etc. for macOS (i.e. Darwin), even while building for iOS?
700                //
701                // So for now we set it to "Darwin" on all Apple platforms.
702                cfg.define("CMAKE_SYSTEM_NAME", "Darwin");
703
704                // These two defines prevent CMake from automatically trying to add a MacOSX sysroot, which leads to a compiler error.
705                cfg.define("CMAKE_OSX_SYSROOT", "/");
706                cfg.define("CMAKE_OSX_DEPLOYMENT_TARGET", "");
707            }
708
709            // Make sure that CMake does not build universal binaries on macOS.
710            // Explicitly specify the one single target architecture.
711            if target.starts_with("aarch64") {
712                // macOS uses a different name for building arm64
713                cfg.define("CMAKE_OSX_ARCHITECTURES", "arm64");
714            } else if target.starts_with("i686") {
715                // macOS uses a different name for building i386
716                cfg.define("CMAKE_OSX_ARCHITECTURES", "i386");
717            } else {
718                cfg.define("CMAKE_OSX_ARCHITECTURES", target.triple.split('-').next().unwrap());
719            }
720        }
721    }
722
723    let sanitize_cc = |cc: &Path| {
724        if target.is_msvc() {
725            OsString::from(cc.to_str().unwrap().replace('\\', "/"))
726        } else {
727            cc.as_os_str().to_owned()
728        }
729    };
730
731    // MSVC with CMake uses msbuild by default which doesn't respect these
732    // vars that we'd otherwise configure. In that case we just skip this
733    // entirely.
734    if target.is_msvc() && !builder.ninja() {
735        return;
736    }
737
738    let (cc, cxx) = match builder.config.llvm_clang_cl {
739        Some(ref cl) => (cl.into(), cl.into()),
740        None => (builder.cc(target), builder.cxx(target).unwrap()),
741    };
742
743    // If ccache is configured we inform the build a little differently how
744    // to invoke ccache while also invoking our compilers.
745    if use_compiler_launcher {
746        if let Some(ref ccache) = builder.config.ccache {
747            cfg.define("CMAKE_C_COMPILER_LAUNCHER", ccache)
748                .define("CMAKE_CXX_COMPILER_LAUNCHER", ccache);
749        }
750    }
751    cfg.define("CMAKE_C_COMPILER", sanitize_cc(&cc))
752        .define("CMAKE_CXX_COMPILER", sanitize_cc(&cxx))
753        .define("CMAKE_ASM_COMPILER", sanitize_cc(&cc));
754
755    cfg.build_arg("-j").build_arg(builder.jobs().to_string());
756    // FIXME(madsmtm): Allow `cmake-rs` to select flags by itself by passing
757    // our flags via `.cflag`/`.cxxflag` instead.
758    //
759    // Needs `suppressed_compiler_flag_prefixes` to be gone, and hence
760    // https://github.com/llvm/llvm-project/issues/88780 to be fixed.
761    let mut cflags: OsString = builder
762        .cc_handled_clags(target, CLang::C)
763        .into_iter()
764        .chain(builder.cc_unhandled_cflags(target, GitRepo::Llvm, CLang::C))
765        .filter(|flag| {
766            !suppressed_compiler_flag_prefixes
767                .iter()
768                .any(|suppressed_prefix| flag.starts_with(suppressed_prefix))
769        })
770        .collect::<Vec<String>>()
771        .join(" ")
772        .into();
773    if let Some(ref s) = builder.config.llvm_cflags {
774        cflags.push(" ");
775        cflags.push(s);
776    }
777    if target.contains("ohos") {
778        cflags.push(" -D_LINUX_SYSINFO_H");
779    }
780    if builder.config.llvm_clang_cl.is_some() {
781        cflags.push(format!(" --target={target}"));
782    }
783    cfg.define("CMAKE_C_FLAGS", cflags);
784    let mut cxxflags: OsString = builder
785        .cc_handled_clags(target, CLang::Cxx)
786        .into_iter()
787        .chain(builder.cc_unhandled_cflags(target, GitRepo::Llvm, CLang::Cxx))
788        .filter(|flag| {
789            !suppressed_compiler_flag_prefixes
790                .iter()
791                .any(|suppressed_prefix| flag.starts_with(suppressed_prefix))
792        })
793        .collect::<Vec<String>>()
794        .join(" ")
795        .into();
796    if let Some(ref s) = builder.config.llvm_cxxflags {
797        cxxflags.push(" ");
798        cxxflags.push(s);
799    }
800    if target.contains("ohos") {
801        cxxflags.push(" -D_LINUX_SYSINFO_H");
802    }
803    if builder.config.llvm_clang_cl.is_some() {
804        cxxflags.push(format!(" --target={target}"));
805    }
806    cfg.define("CMAKE_CXX_FLAGS", cxxflags);
807    if let Some(ar) = builder.ar(target) {
808        if ar.is_absolute() {
809            // LLVM build breaks if `CMAKE_AR` is a relative path, for some reason it
810            // tries to resolve this path in the LLVM build directory.
811            cfg.define("CMAKE_AR", sanitize_cc(&ar));
812        }
813    }
814
815    if let Some(ranlib) = builder.ranlib(target) {
816        if ranlib.is_absolute() {
817            // LLVM build breaks if `CMAKE_RANLIB` is a relative path, for some reason it
818            // tries to resolve this path in the LLVM build directory.
819            cfg.define("CMAKE_RANLIB", sanitize_cc(&ranlib));
820        }
821    }
822
823    if let Some(ref flags) = builder.config.llvm_ldflags {
824        ldflags.push_all(flags);
825    }
826
827    if let Some(flags) = get_var("LDFLAGS", &builder.config.build.triple, &target.triple) {
828        ldflags.push_all(&flags);
829    }
830
831    // For distribution we want the LLVM tools to be *statically* linked to libstdc++.
832    // We also do this if the user explicitly requested static libstdc++.
833    if builder.config.llvm_static_stdcpp
834        && !target.is_msvc()
835        && !target.contains("netbsd")
836        && !target.contains("solaris")
837    {
838        if target.contains("apple") || target.is_windows() {
839            ldflags.push_all("-static-libstdc++");
840        } else {
841            ldflags.push_all("-Wl,-Bsymbolic -static-libstdc++");
842        }
843    }
844
845    cfg.define("CMAKE_SHARED_LINKER_FLAGS", &ldflags.shared);
846    cfg.define("CMAKE_MODULE_LINKER_FLAGS", &ldflags.module);
847    cfg.define("CMAKE_EXE_LINKER_FLAGS", &ldflags.exe);
848
849    if env::var_os("SCCACHE_ERROR_LOG").is_some() {
850        cfg.env("RUSTC_LOG", "sccache=warn");
851    }
852}
853
854fn configure_llvm(builder: &Builder<'_>, target: TargetSelection, cfg: &mut cmake::Config) {
855    // ThinLTO is only available when building with LLVM, enabling LLD is required.
856    // Apple's linker ld64 supports ThinLTO out of the box though, so don't use LLD on Darwin.
857    if builder.config.llvm_thin_lto {
858        cfg.define("LLVM_ENABLE_LTO", "Thin");
859        if !target.contains("apple") {
860            cfg.define("LLVM_ENABLE_LLD", "ON");
861        }
862    }
863
864    // Libraries for ELF section compression.
865    if builder.config.llvm_libzstd {
866        cfg.define("LLVM_ENABLE_ZSTD", "FORCE_ON");
867        cfg.define("LLVM_USE_STATIC_ZSTD", "TRUE");
868    } else {
869        cfg.define("LLVM_ENABLE_ZSTD", "OFF");
870    }
871
872    if let Some(ref linker) = builder.config.llvm_use_linker {
873        cfg.define("LLVM_USE_LINKER", linker);
874    }
875
876    if builder.config.llvm_allow_old_toolchain {
877        cfg.define("LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN", "YES");
878    }
879}
880
881// Adapted from https://github.com/alexcrichton/cc-rs/blob/fba7feded71ee4f63cfe885673ead6d7b4f2f454/src/lib.rs#L2347-L2365
882fn get_var(var_base: &str, host: &str, target: &str) -> Option<OsString> {
883    let kind = if host == target { "HOST" } else { "TARGET" };
884    let target_u = target.replace('-', "_");
885    env::var_os(format!("{var_base}_{target}"))
886        .or_else(|| env::var_os(format!("{}_{}", var_base, target_u)))
887        .or_else(|| env::var_os(format!("{}_{}", kind, var_base)))
888        .or_else(|| env::var_os(var_base))
889}
890
891#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
892pub struct Enzyme {
893    pub target: TargetSelection,
894}
895
896impl Step for Enzyme {
897    type Output = PathBuf;
898    const ONLY_HOSTS: bool = true;
899
900    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
901        run.path("src/tools/enzyme/enzyme")
902    }
903
904    fn make_run(run: RunConfig<'_>) {
905        run.builder.ensure(Enzyme { target: run.target });
906    }
907
908    /// Compile Enzyme for `target`.
909    #[cfg_attr(
910        feature = "tracing",
911        instrument(
912            level = "debug",
913            name = "Enzyme::run",
914            skip_all,
915            fields(target = ?self.target),
916        ),
917    )]
918    fn run(self, builder: &Builder<'_>) -> PathBuf {
919        builder.require_submodule(
920            "src/tools/enzyme",
921            Some("The Enzyme sources are required for autodiff."),
922        );
923        if builder.config.dry_run() {
924            let out_dir = builder.enzyme_out(self.target);
925            return out_dir;
926        }
927        let target = self.target;
928
929        let LlvmResult { llvm_config, .. } = builder.ensure(Llvm { target: self.target });
930
931        static STAMP_HASH_MEMO: OnceLock<String> = OnceLock::new();
932        let smart_stamp_hash = STAMP_HASH_MEMO.get_or_init(|| {
933            generate_smart_stamp_hash(
934                builder,
935                &builder.config.src.join("src/tools/enzyme"),
936                builder.enzyme_info.sha().unwrap_or_default(),
937            )
938        });
939
940        let out_dir = builder.enzyme_out(target);
941        let stamp = BuildStamp::new(&out_dir).with_prefix("enzyme").add_stamp(smart_stamp_hash);
942
943        trace!("checking build stamp to see if we need to rebuild enzyme artifacts");
944        if stamp.is_up_to_date() {
945            trace!(?out_dir, "enzyme build artifacts are up to date");
946            if stamp.stamp().is_empty() {
947                builder.info(
948                    "Could not determine the Enzyme submodule commit hash. \
949                     Assuming that an Enzyme rebuild is not necessary.",
950                );
951                builder.info(&format!(
952                    "To force Enzyme to rebuild, remove the file `{}`",
953                    stamp.path().display()
954                ));
955            }
956            return out_dir;
957        }
958
959        trace!(?target, "(re)building enzyme artifacts");
960        builder.info(&format!("Building Enzyme for {}", target));
961        t!(stamp.remove());
962        let _time = helpers::timeit(builder);
963        t!(fs::create_dir_all(&out_dir));
964
965        builder
966            .config
967            .update_submodule(Path::new("src").join("tools").join("enzyme").to_str().unwrap());
968        let mut cfg = cmake::Config::new(builder.src.join("src/tools/enzyme/enzyme/"));
969        configure_cmake(builder, target, &mut cfg, true, LdFlags::default(), &[]);
970
971        // Re-use the same flags as llvm to control the level of debug information
972        // generated by Enzyme.
973        // FIXME(ZuseZ4): Find a nicer way to use Enzyme Debug builds.
974        let profile = match (builder.config.llvm_optimize, builder.config.llvm_release_debuginfo) {
975            (false, _) => "Debug",
976            (true, false) => "Release",
977            (true, true) => "RelWithDebInfo",
978        };
979        trace!(?profile);
980
981        cfg.out_dir(&out_dir)
982            .profile(profile)
983            .env("LLVM_CONFIG_REAL", &llvm_config)
984            .define("LLVM_ENABLE_ASSERTIONS", "ON")
985            .define("ENZYME_EXTERNAL_SHARED_LIB", "ON")
986            .define("LLVM_DIR", builder.llvm_out(target));
987
988        cfg.build();
989
990        t!(stamp.write());
991        out_dir
992    }
993}
994
995#[derive(Debug, Clone, Hash, PartialEq, Eq)]
996pub struct Lld {
997    pub target: TargetSelection,
998}
999
1000impl Step for Lld {
1001    type Output = PathBuf;
1002    const ONLY_HOSTS: bool = true;
1003
1004    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1005        run.path("src/llvm-project/lld")
1006    }
1007
1008    fn make_run(run: RunConfig<'_>) {
1009        run.builder.ensure(Lld { target: run.target });
1010    }
1011
1012    /// Compile LLD for `target`.
1013    fn run(self, builder: &Builder<'_>) -> PathBuf {
1014        if builder.config.dry_run() {
1015            return PathBuf::from("lld-out-dir-test-gen");
1016        }
1017        let target = self.target;
1018
1019        let LlvmResult { llvm_config, llvm_cmake_dir } = builder.ensure(Llvm { target });
1020
1021        // The `dist` step packages LLD next to LLVM's binaries for download-ci-llvm. The root path
1022        // we usually expect here is `./build/$triple/ci-llvm/`, with the binaries in its `bin`
1023        // subfolder. We check if that's the case, and if LLD's binary already exists there next to
1024        // `llvm-config`: if so, we can use it instead of building LLVM/LLD from source.
1025        let ci_llvm_bin = llvm_config.parent().unwrap();
1026        if ci_llvm_bin.is_dir() && ci_llvm_bin.file_name().unwrap() == "bin" {
1027            let lld_path = ci_llvm_bin.join(exe("lld", target));
1028            if lld_path.exists() {
1029                // The following steps copying `lld` as `rust-lld` to the sysroot, expect it in the
1030                // `bin` subfolder of this step's out dir.
1031                return ci_llvm_bin.parent().unwrap().to_path_buf();
1032            }
1033        }
1034
1035        let out_dir = builder.lld_out(target);
1036
1037        let lld_stamp = BuildStamp::new(&out_dir).with_prefix("lld");
1038        if lld_stamp.path().exists() {
1039            return out_dir;
1040        }
1041
1042        let _guard = builder.msg_unstaged(Kind::Build, "LLD", target);
1043        let _time = helpers::timeit(builder);
1044        t!(fs::create_dir_all(&out_dir));
1045
1046        let mut cfg = cmake::Config::new(builder.src.join("src/llvm-project/lld"));
1047        let mut ldflags = LdFlags::default();
1048
1049        // When building LLD as part of a build with instrumentation on windows, for example
1050        // when doing PGO on CI, cmake or clang-cl don't automatically link clang's
1051        // profiler runtime in. In that case, we need to manually ask cmake to do it, to avoid
1052        // linking errors, much like LLVM's cmake setup does in that situation.
1053        if builder.config.llvm_profile_generate && target.is_msvc() {
1054            if let Some(clang_cl_path) = builder.config.llvm_clang_cl.as_ref() {
1055                // Find clang's runtime library directory and push that as a search path to the
1056                // cmake linker flags.
1057                let clang_rt_dir = get_clang_cl_resource_dir(builder, clang_cl_path);
1058                ldflags.push_all(format!("/libpath:{}", clang_rt_dir.display()));
1059            }
1060        }
1061
1062        // LLD is built as an LLVM tool, but is distributed outside of the `llvm-tools` component,
1063        // which impacts where it expects to find LLVM's shared library. This causes #80703.
1064        //
1065        // LLD is distributed at "$root/lib/rustlib/$host/bin/rust-lld", but the `libLLVM-*.so` it
1066        // needs is distributed at "$root/lib". The default rpath of "$ORIGIN/../lib" points at the
1067        // lib path for LLVM tools, not the one for rust binaries.
1068        //
1069        // (The `llvm-tools` component copies the .so there for the other tools, and with that
1070        // component installed, one can successfully invoke `rust-lld` directly without rustup's
1071        // `LD_LIBRARY_PATH` overrides)
1072        //
1073        if builder.config.rpath_enabled(target)
1074            && helpers::use_host_linker(target)
1075            && builder.config.llvm_link_shared()
1076            && target.contains("linux")
1077        {
1078            // So we inform LLD where it can find LLVM's libraries by adding an rpath entry to the
1079            // expected parent `lib` directory.
1080            //
1081            // Be careful when changing this path, we need to ensure it's quoted or escaped:
1082            // `$ORIGIN` would otherwise be expanded when the `LdFlags` are passed verbatim to
1083            // cmake.
1084            ldflags.push_all("-Wl,-rpath,'$ORIGIN/../../../'");
1085        }
1086
1087        configure_cmake(builder, target, &mut cfg, true, ldflags, &[]);
1088        configure_llvm(builder, target, &mut cfg);
1089
1090        // Re-use the same flags as llvm to control the level of debug information
1091        // generated for lld.
1092        let profile = match (builder.config.llvm_optimize, builder.config.llvm_release_debuginfo) {
1093            (false, _) => "Debug",
1094            (true, false) => "Release",
1095            (true, true) => "RelWithDebInfo",
1096        };
1097
1098        cfg.out_dir(&out_dir)
1099            .profile(profile)
1100            .define("LLVM_CMAKE_DIR", llvm_cmake_dir)
1101            .define("LLVM_INCLUDE_TESTS", "OFF");
1102
1103        if !builder.is_builder_target(target) {
1104            // Use the host llvm-tblgen binary.
1105            cfg.define(
1106                "LLVM_TABLEGEN_EXE",
1107                llvm_config.with_file_name("llvm-tblgen").with_extension(EXE_EXTENSION),
1108            );
1109        }
1110
1111        cfg.build();
1112
1113        t!(lld_stamp.write());
1114        out_dir
1115    }
1116}
1117
1118#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1119pub struct Sanitizers {
1120    pub target: TargetSelection,
1121}
1122
1123impl Step for Sanitizers {
1124    type Output = Vec<SanitizerRuntime>;
1125
1126    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1127        run.alias("sanitizers")
1128    }
1129
1130    fn make_run(run: RunConfig<'_>) {
1131        run.builder.ensure(Sanitizers { target: run.target });
1132    }
1133
1134    /// Builds sanitizer runtime libraries.
1135    fn run(self, builder: &Builder<'_>) -> Self::Output {
1136        let compiler_rt_dir = builder.src.join("src/llvm-project/compiler-rt");
1137        if !compiler_rt_dir.exists() {
1138            return Vec::new();
1139        }
1140
1141        let out_dir = builder.native_dir(self.target).join("sanitizers");
1142        let runtimes = supported_sanitizers(&out_dir, self.target, &builder.config.channel);
1143
1144        if builder.config.dry_run() || runtimes.is_empty() {
1145            return runtimes;
1146        }
1147
1148        let LlvmResult { llvm_config, .. } = builder.ensure(Llvm { target: builder.config.build });
1149
1150        static STAMP_HASH_MEMO: OnceLock<String> = OnceLock::new();
1151        let smart_stamp_hash = STAMP_HASH_MEMO.get_or_init(|| {
1152            generate_smart_stamp_hash(
1153                builder,
1154                &builder.config.src.join("src/llvm-project/compiler-rt"),
1155                builder.in_tree_llvm_info.sha().unwrap_or_default(),
1156            )
1157        });
1158
1159        let stamp = BuildStamp::new(&out_dir).with_prefix("sanitizers").add_stamp(smart_stamp_hash);
1160
1161        if stamp.is_up_to_date() {
1162            if stamp.stamp().is_empty() {
1163                builder.info(&format!(
1164                    "Rebuild sanitizers by removing the file `{}`",
1165                    stamp.path().display()
1166                ));
1167            }
1168
1169            return runtimes;
1170        }
1171
1172        let _guard = builder.msg_unstaged(Kind::Build, "sanitizers", self.target);
1173        t!(stamp.remove());
1174        let _time = helpers::timeit(builder);
1175
1176        let mut cfg = cmake::Config::new(&compiler_rt_dir);
1177        cfg.profile("Release");
1178        cfg.define("CMAKE_C_COMPILER_TARGET", self.target.triple);
1179        cfg.define("COMPILER_RT_BUILD_BUILTINS", "OFF");
1180        cfg.define("COMPILER_RT_BUILD_CRT", "OFF");
1181        cfg.define("COMPILER_RT_BUILD_LIBFUZZER", "OFF");
1182        cfg.define("COMPILER_RT_BUILD_PROFILE", "OFF");
1183        cfg.define("COMPILER_RT_BUILD_SANITIZERS", "ON");
1184        cfg.define("COMPILER_RT_BUILD_XRAY", "OFF");
1185        cfg.define("COMPILER_RT_DEFAULT_TARGET_ONLY", "ON");
1186        cfg.define("COMPILER_RT_USE_LIBCXX", "OFF");
1187        cfg.define("LLVM_CONFIG_PATH", &llvm_config);
1188
1189        if self.target.contains("ohos") {
1190            cfg.define("COMPILER_RT_USE_BUILTINS_LIBRARY", "ON");
1191        }
1192
1193        // On Darwin targets the sanitizer runtimes are build as universal binaries.
1194        // Unfortunately sccache currently lacks support to build them successfully.
1195        // Disable compiler launcher on Darwin targets to avoid potential issues.
1196        let use_compiler_launcher = !self.target.contains("apple-darwin");
1197        // Since v1.0.86, the cc crate adds -mmacosx-version-min to the default
1198        // flags on MacOS. A long-standing bug in the CMake rules for compiler-rt
1199        // causes architecture detection to be skipped when this flag is present,
1200        // and compilation fails. https://github.com/llvm/llvm-project/issues/88780
1201        let suppressed_compiler_flag_prefixes: &[&str] =
1202            if self.target.contains("apple-darwin") { &["-mmacosx-version-min="] } else { &[] };
1203        configure_cmake(
1204            builder,
1205            self.target,
1206            &mut cfg,
1207            use_compiler_launcher,
1208            LdFlags::default(),
1209            suppressed_compiler_flag_prefixes,
1210        );
1211
1212        t!(fs::create_dir_all(&out_dir));
1213        cfg.out_dir(out_dir);
1214
1215        for runtime in &runtimes {
1216            cfg.build_target(&runtime.cmake_target);
1217            cfg.build();
1218        }
1219        t!(stamp.write());
1220
1221        runtimes
1222    }
1223}
1224
1225#[derive(Clone, Debug)]
1226pub struct SanitizerRuntime {
1227    /// CMake target used to build the runtime.
1228    pub cmake_target: String,
1229    /// Path to the built runtime library.
1230    pub path: PathBuf,
1231    /// Library filename that will be used rustc.
1232    pub name: String,
1233}
1234
1235/// Returns sanitizers available on a given target.
1236fn supported_sanitizers(
1237    out_dir: &Path,
1238    target: TargetSelection,
1239    channel: &str,
1240) -> Vec<SanitizerRuntime> {
1241    let darwin_libs = |os: &str, components: &[&str]| -> Vec<SanitizerRuntime> {
1242        components
1243            .iter()
1244            .map(move |c| SanitizerRuntime {
1245                cmake_target: format!("clang_rt.{}_{}_dynamic", c, os),
1246                path: out_dir
1247                    .join(format!("build/lib/darwin/libclang_rt.{}_{}_dynamic.dylib", c, os)),
1248                name: format!("librustc-{}_rt.{}.dylib", channel, c),
1249            })
1250            .collect()
1251    };
1252
1253    let common_libs = |os: &str, arch: &str, components: &[&str]| -> Vec<SanitizerRuntime> {
1254        components
1255            .iter()
1256            .map(move |c| SanitizerRuntime {
1257                cmake_target: format!("clang_rt.{}-{}", c, arch),
1258                path: out_dir.join(format!("build/lib/{}/libclang_rt.{}-{}.a", os, c, arch)),
1259                name: format!("librustc-{}_rt.{}.a", channel, c),
1260            })
1261            .collect()
1262    };
1263
1264    match &*target.triple {
1265        "aarch64-apple-darwin" => darwin_libs("osx", &["asan", "lsan", "tsan"]),
1266        "aarch64-apple-ios" => darwin_libs("ios", &["asan", "tsan"]),
1267        "aarch64-apple-ios-sim" => darwin_libs("iossim", &["asan", "tsan"]),
1268        "aarch64-apple-ios-macabi" => darwin_libs("osx", &["asan", "lsan", "tsan"]),
1269        "aarch64-unknown-fuchsia" => common_libs("fuchsia", "aarch64", &["asan"]),
1270        "aarch64-unknown-linux-gnu" => {
1271            common_libs("linux", "aarch64", &["asan", "lsan", "msan", "tsan", "hwasan"])
1272        }
1273        "aarch64-unknown-linux-ohos" => {
1274            common_libs("linux", "aarch64", &["asan", "lsan", "msan", "tsan", "hwasan"])
1275        }
1276        "loongarch64-unknown-linux-gnu" | "loongarch64-unknown-linux-musl" => {
1277            common_libs("linux", "loongarch64", &["asan", "lsan", "msan", "tsan"])
1278        }
1279        "x86_64-apple-darwin" => darwin_libs("osx", &["asan", "lsan", "tsan"]),
1280        "x86_64-unknown-fuchsia" => common_libs("fuchsia", "x86_64", &["asan"]),
1281        "x86_64-apple-ios" => darwin_libs("iossim", &["asan", "tsan"]),
1282        "x86_64-apple-ios-macabi" => darwin_libs("osx", &["asan", "lsan", "tsan"]),
1283        "x86_64-unknown-freebsd" => common_libs("freebsd", "x86_64", &["asan", "msan", "tsan"]),
1284        "x86_64-unknown-netbsd" => {
1285            common_libs("netbsd", "x86_64", &["asan", "lsan", "msan", "tsan"])
1286        }
1287        "x86_64-unknown-illumos" => common_libs("illumos", "x86_64", &["asan"]),
1288        "x86_64-pc-solaris" => common_libs("solaris", "x86_64", &["asan"]),
1289        "x86_64-unknown-linux-gnu" => {
1290            common_libs("linux", "x86_64", &["asan", "dfsan", "lsan", "msan", "safestack", "tsan"])
1291        }
1292        "x86_64-unknown-linux-musl" => {
1293            common_libs("linux", "x86_64", &["asan", "lsan", "msan", "tsan"])
1294        }
1295        "s390x-unknown-linux-gnu" => {
1296            common_libs("linux", "s390x", &["asan", "lsan", "msan", "tsan"])
1297        }
1298        "s390x-unknown-linux-musl" => {
1299            common_libs("linux", "s390x", &["asan", "lsan", "msan", "tsan"])
1300        }
1301        "x86_64-unknown-linux-ohos" => {
1302            common_libs("linux", "x86_64", &["asan", "lsan", "msan", "tsan"])
1303        }
1304        _ => Vec::new(),
1305    }
1306}
1307
1308#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1309pub struct CrtBeginEnd {
1310    pub target: TargetSelection,
1311}
1312
1313impl Step for CrtBeginEnd {
1314    type Output = PathBuf;
1315
1316    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1317        run.path("src/llvm-project/compiler-rt/lib/crt")
1318    }
1319
1320    fn make_run(run: RunConfig<'_>) {
1321        if run.target.needs_crt_begin_end() {
1322            run.builder.ensure(CrtBeginEnd { target: run.target });
1323        }
1324    }
1325
1326    /// Build crtbegin.o/crtend.o for musl target.
1327    fn run(self, builder: &Builder<'_>) -> Self::Output {
1328        builder.require_submodule(
1329            "src/llvm-project",
1330            Some("The LLVM sources are required for the CRT from `compiler-rt`."),
1331        );
1332
1333        let out_dir = builder.native_dir(self.target).join("crt");
1334
1335        if builder.config.dry_run() {
1336            return out_dir;
1337        }
1338
1339        let crtbegin_src = builder.src.join("src/llvm-project/compiler-rt/lib/builtins/crtbegin.c");
1340        let crtend_src = builder.src.join("src/llvm-project/compiler-rt/lib/builtins/crtend.c");
1341        if up_to_date(&crtbegin_src, &out_dir.join("crtbeginS.o"))
1342            && up_to_date(&crtend_src, &out_dir.join("crtendS.o"))
1343        {
1344            return out_dir;
1345        }
1346
1347        let _guard = builder.msg_unstaged(Kind::Build, "crtbegin.o and crtend.o", self.target);
1348        t!(fs::create_dir_all(&out_dir));
1349
1350        let mut cfg = cc::Build::new();
1351
1352        if let Some(ar) = builder.ar(self.target) {
1353            cfg.archiver(ar);
1354        }
1355        cfg.compiler(builder.cc(self.target));
1356        cfg.cargo_metadata(false)
1357            .out_dir(&out_dir)
1358            .target(&self.target.triple)
1359            .host(&builder.config.build.triple)
1360            .warnings(false)
1361            .debug(false)
1362            .opt_level(3)
1363            .file(crtbegin_src)
1364            .file(crtend_src);
1365
1366        // Those flags are defined in src/llvm-project/compiler-rt/lib/builtins/CMakeLists.txt
1367        // Currently only consumer of those objects is musl, which use .init_array/.fini_array
1368        // instead of .ctors/.dtors
1369        cfg.flag("-std=c11")
1370            .define("CRT_HAS_INITFINI_ARRAY", None)
1371            .define("EH_USE_FRAME_REGISTRY", None);
1372
1373        let objs = cfg.compile_intermediates();
1374        assert_eq!(objs.len(), 2);
1375        for obj in objs {
1376            let base_name = unhashed_basename(&obj);
1377            assert!(base_name == "crtbegin" || base_name == "crtend");
1378            t!(fs::copy(&obj, out_dir.join(format!("{}S.o", base_name))));
1379            t!(fs::rename(&obj, out_dir.join(format!("{}.o", base_name))));
1380        }
1381
1382        out_dir
1383    }
1384}
1385
1386#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1387pub struct Libunwind {
1388    pub target: TargetSelection,
1389}
1390
1391impl Step for Libunwind {
1392    type Output = PathBuf;
1393
1394    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1395        run.path("src/llvm-project/libunwind")
1396    }
1397
1398    fn make_run(run: RunConfig<'_>) {
1399        run.builder.ensure(Libunwind { target: run.target });
1400    }
1401
1402    /// Build libunwind.a
1403    fn run(self, builder: &Builder<'_>) -> Self::Output {
1404        builder.require_submodule(
1405            "src/llvm-project",
1406            Some("The LLVM sources are required for libunwind."),
1407        );
1408
1409        if builder.config.dry_run() {
1410            return PathBuf::new();
1411        }
1412
1413        let out_dir = builder.native_dir(self.target).join("libunwind");
1414        let root = builder.src.join("src/llvm-project/libunwind");
1415
1416        if up_to_date(&root, &out_dir.join("libunwind.a")) {
1417            return out_dir;
1418        }
1419
1420        let _guard = builder.msg_unstaged(Kind::Build, "libunwind.a", self.target);
1421        t!(fs::create_dir_all(&out_dir));
1422
1423        let mut cc_cfg = cc::Build::new();
1424        let mut cpp_cfg = cc::Build::new();
1425
1426        cpp_cfg.cpp(true);
1427        cpp_cfg.cpp_set_stdlib(None);
1428        cpp_cfg.flag("-nostdinc++");
1429        cpp_cfg.flag("-fno-exceptions");
1430        cpp_cfg.flag("-fno-rtti");
1431        cpp_cfg.flag_if_supported("-fvisibility-global-new-delete-hidden");
1432
1433        for cfg in [&mut cc_cfg, &mut cpp_cfg].iter_mut() {
1434            if let Some(ar) = builder.ar(self.target) {
1435                cfg.archiver(ar);
1436            }
1437            cfg.target(&self.target.triple);
1438            cfg.host(&builder.config.build.triple);
1439            cfg.warnings(false);
1440            cfg.debug(false);
1441            // get_compiler() need set opt_level first.
1442            cfg.opt_level(3);
1443            cfg.flag("-fstrict-aliasing");
1444            cfg.flag("-funwind-tables");
1445            cfg.flag("-fvisibility=hidden");
1446            cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
1447            cfg.include(root.join("include"));
1448            cfg.cargo_metadata(false);
1449            cfg.out_dir(&out_dir);
1450
1451            if self.target.contains("x86_64-fortanix-unknown-sgx") {
1452                cfg.static_flag(true);
1453                cfg.flag("-fno-stack-protector");
1454                cfg.flag("-ffreestanding");
1455                cfg.flag("-fexceptions");
1456
1457                // easiest way to undefine since no API available in cc::Build to undefine
1458                cfg.flag("-U_FORTIFY_SOURCE");
1459                cfg.define("_FORTIFY_SOURCE", "0");
1460                cfg.define("RUST_SGX", "1");
1461                cfg.define("__NO_STRING_INLINES", None);
1462                cfg.define("__NO_MATH_INLINES", None);
1463                cfg.define("_LIBUNWIND_IS_BAREMETAL", None);
1464                cfg.define("__LIBUNWIND_IS_NATIVE_ONLY", None);
1465                cfg.define("NDEBUG", None);
1466            }
1467            if self.target.is_windows() {
1468                cfg.define("_LIBUNWIND_HIDE_SYMBOLS", "1");
1469                cfg.define("_LIBUNWIND_IS_NATIVE_ONLY", "1");
1470            }
1471        }
1472
1473        cc_cfg.compiler(builder.cc(self.target));
1474        if let Ok(cxx) = builder.cxx(self.target) {
1475            cpp_cfg.compiler(cxx);
1476        } else {
1477            cc_cfg.compiler(builder.cc(self.target));
1478        }
1479
1480        // Don't set this for clang
1481        // By default, Clang builds C code in GNU C17 mode.
1482        // By default, Clang builds C++ code according to the C++98 standard,
1483        // with many C++11 features accepted as extensions.
1484        if cc_cfg.get_compiler().is_like_gnu() {
1485            cc_cfg.flag("-std=c99");
1486        }
1487        if cpp_cfg.get_compiler().is_like_gnu() {
1488            cpp_cfg.flag("-std=c++11");
1489        }
1490
1491        if self.target.contains("x86_64-fortanix-unknown-sgx") || self.target.contains("musl") {
1492            // use the same GCC C compiler command to compile C++ code so we do not need to setup the
1493            // C++ compiler env variables on the builders.
1494            // Don't set this for clang++, as clang++ is able to compile this without libc++.
1495            if cpp_cfg.get_compiler().is_like_gnu() {
1496                cpp_cfg.cpp(false);
1497                cpp_cfg.compiler(builder.cc(self.target));
1498            }
1499        }
1500
1501        let mut c_sources = vec![
1502            "Unwind-sjlj.c",
1503            "UnwindLevel1-gcc-ext.c",
1504            "UnwindLevel1.c",
1505            "UnwindRegistersRestore.S",
1506            "UnwindRegistersSave.S",
1507        ];
1508
1509        let cpp_sources = vec!["Unwind-EHABI.cpp", "Unwind-seh.cpp", "libunwind.cpp"];
1510        let cpp_len = cpp_sources.len();
1511
1512        if self.target.contains("x86_64-fortanix-unknown-sgx") {
1513            c_sources.push("UnwindRustSgx.c");
1514        }
1515
1516        for src in c_sources {
1517            cc_cfg.file(root.join("src").join(src).canonicalize().unwrap());
1518        }
1519
1520        for src in &cpp_sources {
1521            cpp_cfg.file(root.join("src").join(src).canonicalize().unwrap());
1522        }
1523
1524        cpp_cfg.compile("unwind-cpp");
1525
1526        // FIXME: https://github.com/alexcrichton/cc-rs/issues/545#issuecomment-679242845
1527        let mut count = 0;
1528        for entry in fs::read_dir(&out_dir).unwrap() {
1529            let file = entry.unwrap().path().canonicalize().unwrap();
1530            if file.is_file() && file.extension() == Some(OsStr::new("o")) {
1531                // Object file name without the hash prefix is "Unwind-EHABI", "Unwind-seh" or "libunwind".
1532                let base_name = unhashed_basename(&file);
1533                if cpp_sources.iter().any(|f| *base_name == f[..f.len() - 4]) {
1534                    cc_cfg.object(&file);
1535                    count += 1;
1536                }
1537            }
1538        }
1539        assert_eq!(cpp_len, count, "Can't get object files from {out_dir:?}");
1540
1541        cc_cfg.compile("unwind");
1542        out_dir
1543    }
1544}