src/bootstrap/src/utils/cc_detect.rs RUST 312 lines View on github.com → Search inside
1//! C-compiler probing and detection.2//!3//! This module will fill out the `cc` and `cxx` maps of `Build` by looking for4//! C and C++ compilers for each target configured. A compiler is found through5//! a number of vectors (in order of precedence)6//!7//! 1. Configuration via `target.$target.cc` in `bootstrap.toml`.8//! 2. Configuration via `target.$target.android-ndk` in `bootstrap.toml`, if9//!    applicable10//! 3. Special logic to probe on OpenBSD11//! 4. The `CC_$target` environment variable.12//! 5. The `CC` environment variable.13//! 6. "cc"14//!15//! Some of this logic is implemented here, but much of it is farmed out to the16//! `cc` crate itself, so we end up having the same fallbacks as there.17//! Similar logic is then used to find a C++ compiler, just some s/cc/c++/ is18//! used.19//!20//! It is intended that after this module has run no C/C++ compiler will21//! ever be probed for. Instead the compilers found here will be used for22//! everything.2324use std::collections::HashSet;25use std::iter;26use std::path::{Path, PathBuf};2728use crate::core::config::TargetSelection;29use crate::utils::exec::{BootstrapCommand, command};30use crate::{Build, CLang, GitRepo};3132/// Creates and configures a new [`cc::Build`] instance for the given target.33fn new_cc_build(build: &Build, target: TargetSelection) -> cc::Build {34    let mut cfg = cc::Build::new();35    cfg.cargo_metadata(false)36        .opt_level(2)37        .warnings(false)38        .debug(false)39        // Compress debuginfo40        .flag_if_supported("-gz")41        .target(&target.triple)42        .host(&build.host_target.triple);43    match build.crt_static(target) {44        Some(a) => {45            cfg.static_crt(a);46        }47        None => {48            if target.is_msvc() {49                cfg.static_crt(true);50            }51            if target.contains("musl") {52                cfg.static_flag(true);53            }54        }55    }56    cfg57}5859/// Probes for C and C++ compilers and configures the corresponding entries in the [`Build`]60/// structure.61///62/// This function determines which targets need a C compiler (and, if needed, a C++ compiler)63/// by combining the primary build target, host targets, and any additional targets. For64/// each target, it calls [`fill_target_compiler`] to configure the necessary compiler tools.65pub fn fill_compilers(build: &mut Build) {66    let targets: HashSet<_> = match build.config.cmd {67        // We don't need to check cross targets for these commands.68        crate::Subcommand::Clean { .. }69        | crate::Subcommand::Check { .. }70        | crate::Subcommand::Format { .. }71        | crate::Subcommand::Setup { .. } => {72            build.hosts.iter().cloned().chain(iter::once(build.host_target)).collect()73        }7475        _ => {76            // For all targets we're going to need a C compiler for building some shims77            // and such as well as for being a linker for Rust code.78            build79                .targets80                .iter()81                .chain(&build.hosts)82                .cloned()83                .chain(iter::once(build.host_target))84                .collect()85        }86    };8788    for target in targets.into_iter() {89        fill_target_compiler(build, target);90    }91}9293/// Probes and configures the C and C++ compilers for a single target.94///95/// This function uses both user-specified configuration (from `bootstrap.toml`) and auto-detection96/// logic to determine the correct C/C++ compilers for the target. It also determines the appropriate97/// archiver (`ar`) and sets up additional compilation flags (both handled and unhandled).98pub fn fill_target_compiler(build: &mut Build, target: TargetSelection) {99    let mut cfg = new_cc_build(build, target);100    let config = build.config.target_config.get(&target);101    if let Some(cc) = config102        .and_then(|c| c.cc.clone())103        .or_else(|| default_compiler(&mut cfg, Language::C, target, build))104    {105        cfg.compiler(cc);106    }107108    let compiler = cfg.get_compiler();109    let ar = if let ar @ Some(..) = config.and_then(|c| c.ar.clone()) {110        ar111    } else {112        cfg.try_get_archiver().map(|c| PathBuf::from(c.get_program())).ok()113    };114115    build.cc.insert(target, compiler.clone());116    let mut cflags = build.cc_handled_clags(target, CLang::C);117    cflags.extend(build.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::C));118119    // If we use llvm-libunwind, we will need a C++ compiler as well for all targets120    // We'll need one anyways if the target triple is also a host triple121    let mut cfg = new_cc_build(build, target);122    cfg.cpp(true);123    let cxx_configured = if let Some(cxx) = config124        .and_then(|c| c.cxx.clone())125        .or_else(|| default_compiler(&mut cfg, Language::CPlusPlus, target, build))126    {127        cfg.compiler(cxx);128        true129    } else {130        // Use an auto-detected compiler (or one configured via `CXX_target_triple` env vars).131        cfg.try_get_compiler().is_ok()132    };133134    // for VxWorks, record CXX compiler which will be used in lib.rs:linker()135    if cxx_configured || target.contains("vxworks") {136        let compiler = cfg.get_compiler();137        build.cxx.insert(target, compiler);138    }139140    build.do_if_verbose(|| println!("CC_{} = {:?}", target.triple, build.cc(target)));141    build.do_if_verbose(|| println!("CFLAGS_{} = {cflags:?}", target.triple));142    if let Ok(cxx) = build.cxx(target) {143        let mut cxxflags = build.cc_handled_clags(target, CLang::Cxx);144        cxxflags.extend(build.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::Cxx));145        build.do_if_verbose(|| println!("CXX_{} = {cxx:?}", target.triple));146        build.do_if_verbose(|| println!("CXXFLAGS_{} = {cxxflags:?}", target.triple));147    }148    if let Some(ar) = ar {149        build.do_if_verbose(|| println!("AR_{} = {ar:?}", target.triple));150        build.ar.insert(target, ar);151    }152153    if let Some(ranlib) = config.and_then(|c| c.ranlib.clone()) {154        build.ranlib.insert(target, ranlib);155    }156}157158/// Determines the default compiler for a given target and language when not explicitly159/// configured in `bootstrap.toml`.160fn default_compiler(161    cfg: &mut cc::Build,162    compiler: Language,163    target: TargetSelection,164    build: &Build,165) -> Option<PathBuf> {166    match &*target.triple {167        // When compiling for android we may have the NDK configured in the168        // bootstrap.toml in which case we look there. Otherwise the default169        // compiler already takes into account the triple in question.170        t if t.contains("android") => {171            build.config.android_ndk.as_ref().map(|ndk| ndk_compiler(compiler, &target.triple, ndk))172        }173174        // The default gcc version from OpenBSD may be too old, try using egcc,175        // which is a gcc version from ports, if this is the case.176        t if t.contains("openbsd") => {177            let c = cfg.get_compiler();178            let gnu_compiler = compiler.gcc();179            if !c.path().ends_with(gnu_compiler) {180                return None;181            }182183            let mut cmd = BootstrapCommand::from(c.to_command());184            let output = cmd.arg("--version").run_capture_stdout(build).stdout();185            let i = output.find(" 4.")?;186            match output[i + 3..].chars().next().unwrap() {187                '0'..='6' => {}188                _ => return None,189            }190            let alternative = format!("e{gnu_compiler}");191            if command(&alternative).run_capture(build).is_success() {192                Some(PathBuf::from(alternative))193            } else {194                None195            }196        }197198        "mips-unknown-linux-musl" if compiler == Language::C => {199            if cfg.get_compiler().path().to_str() == Some("gcc") {200                Some(PathBuf::from("mips-linux-musl-gcc"))201            } else {202                None203            }204        }205        "mipsel-unknown-linux-musl" if compiler == Language::C => {206            if cfg.get_compiler().path().to_str() == Some("gcc") {207                Some(PathBuf::from("mipsel-linux-musl-gcc"))208            } else {209                None210            }211        }212213        t if t.contains("musl") && compiler == Language::C => {214            if let Some(root) = build.musl_root(target) {215                let guess = root.join("bin/musl-gcc");216                if guess.exists() { Some(guess) } else { None }217            } else {218                None219            }220        }221222        t if t.contains("-wasi") => {223            let root = if let Some(path) = build.wasi_sdk_path.as_ref() {224                path225            } else {226                if build.config.is_running_on_ci() {227                    panic!("ERROR: WASI_SDK_PATH must be configured for a -wasi target on CI");228                }229                println!("WARNING: WASI_SDK_PATH not set, using default cc/cxx compiler");230                return None;231            };232            let compiler = match compiler {233                Language::C => format!("{t}-clang"),234                Language::CPlusPlus => format!("{t}-clang++"),235            };236            let compiler = root.join("bin").join(compiler);237            Some(compiler)238        }239240        _ => None,241    }242}243244/// Constructs the path to the Android NDK compiler for the given target triple and language.245///246/// This helper function transform the target triple by converting certain architecture names247/// (for example, translating "arm" to "arm7a"), appends the minimum API level (hardcoded as "21"248/// for NDK r26d), and then constructs the full path based on the provided NDK directory and host249/// platform.250pub(crate) fn ndk_compiler(compiler: Language, triple: &str, ndk: &Path) -> PathBuf {251    let mut triple_iter = triple.split('-');252    let triple_translated = if let Some(arch) = triple_iter.next() {253        let arch_new = match arch {254            "arm" | "armv7" | "armv7neon" | "thumbv7" | "thumbv7neon" => "armv7a",255            other => other,256        };257        std::iter::once(arch_new).chain(triple_iter).collect::<Vec<&str>>().join("-")258    } else {259        triple.to_string()260    };261262    // The earliest API supported by NDK r26d is 21.263    let api_level = "21";264    let compiler = format!("{}{}-{}", triple_translated, api_level, compiler.clang());265    let host_tag = if cfg!(target_os = "macos") {266        // The NDK uses universal binaries, so this is correct even on ARM.267        "darwin-x86_64"268    } else if cfg!(target_os = "windows") {269        "windows-x86_64"270    } else {271        // NDK r26d only has official releases for macOS, Windows and Linux.272        // Try the Linux directory everywhere else, on the assumption that the OS has an273        // emulation layer that can cope (e.g. BSDs).274        "linux-x86_64"275    };276    ndk.join("toolchains").join("llvm").join("prebuilt").join(host_tag).join("bin").join(compiler)277}278279/// Representing the target programming language for a native compiler.280///281/// This enum is used to indicate whether a particular compiler is intended for C or C++.282/// It also provides helper methods for obtaining the standard executable names for GCC and283/// clang-based compilers.284#[derive(PartialEq)]285pub(crate) enum Language {286    /// The compiler is targeting C.287    C,288    /// The compiler is targeting C++.289    CPlusPlus,290}291292impl Language {293    /// Returns the executable name for a GCC compiler corresponding to this language.294    fn gcc(self) -> &'static str {295        match self {296            Language::C => "gcc",297            Language::CPlusPlus => "g++",298        }299    }300301    /// Returns the executable name for a clang-based compiler corresponding to this language.302    fn clang(self) -> &'static str {303        match self {304            Language::C => "clang",305            Language::CPlusPlus => "clang++",306        }307    }308}309310#[cfg(test)]311mod tests;

Code quality findings 13

Warning: Direct indexing (e.g., `vec[i]`, `slice[i]`) panics on out-of-bounds access. Prefer using `.get(index)` or `.get_mut(index)` which return Option<&T>/Option<&mut T>.
warning correctness unchecked-indexing
/// Creates and configures a new [`cc::Build`] instance for the given target.
Warning: Direct indexing (e.g., `vec[i]`, `slice[i]`) panics on out-of-bounds access. Prefer using `.get(index)` or `.get_mut(index)` which return Option<&T>/Option<&mut T>.
warning correctness unchecked-indexing
/// Probes for C and C++ compilers and configures the corresponding entries in the [`Build`]
Warning: Direct indexing (e.g., `vec[i]`, `slice[i]`) panics on out-of-bounds access. Prefer using `.get(index)` or `.get_mut(index)` which return Option<&T>/Option<&mut T>.
warning correctness unchecked-indexing
/// each target, it calls [`fill_target_compiler`] to configure the necessary compiler tools.
Warning: '.unwrap()' will panic on None/Err variants. Prefer using pattern matching (match, if let), combinators (map, and_then), or the '?' operator for robust error handling.
warning correctness unwrap-usage
match output[i + 3..].chars().next().unwrap() {
Warning: Direct indexing (e.g., `vec[i]`, `slice[i]`) panics on out-of-bounds access. Prefer using `.get(index)` or `.get_mut(index)` which return Option<&T>/Option<&mut T>.
warning correctness unchecked-indexing
match output[i + 3..].chars().next().unwrap() {
Info: Ensure 'match' statements are exhaustive. If matching on enums, consider adding a wildcard arm `_ => {}` only if necessary and intentional, as it suppresses warnings about unhandled variants.
info correctness match-wildcard
let targets: HashSet<_> = match build.config.cmd {
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
build.do_if_verbose(|| println!("CC_{} = {:?}", target.triple, build.cc(target)));
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
build.do_if_verbose(|| println!("CFLAGS_{} = {cflags:?}", target.triple));
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
build.do_if_verbose(|| println!("CXX_{} = {cxx:?}", target.triple));
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
build.do_if_verbose(|| println!("CXXFLAGS_{} = {cxxflags:?}", target.triple));
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
build.do_if_verbose(|| println!("AR_{} = {ar:?}", target.triple));
Info: Ensure 'match' statements are exhaustive. If matching on enums, consider adding a wildcard arm `_ => {}` only if necessary and intentional, as it suppresses warnings about unhandled variants.
info correctness match-wildcard
match output[i + 3..].chars().next().unwrap() {
Info: Direct printing to stdout/stderr. For application logging, prefer using a logging facade like `log` or `tracing` for better control over levels, formatting, and output destinations.
info maintainability println-macro
println!("WARNING: WASI_SDK_PATH not set, using default cc/cxx compiler");

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.