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