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