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