Repository: rust-lang/rust
File: compiler/rustc_llvm/build.rs (Rust)

Code before:

fn main() {
    if cfg!(feature = "check_only") {
        return;
    }

    for component in REQUIRED_COMPONENTS.iter().chain(OPTIONAL_COMPONENTS.iter()) {
        println!("cargo:rustc-check-cfg=cfg(llvm_component,values(\"{component}\"))");
    }

    if tracked_env_var_os("RUST_CHECK").is_some() {
        // If we're just running `check`, there's no need for LLVM to be built.
        return;
    }

    restore_library_path();

    let llvm_config =
        PathBuf::from(tracked_env_var_os("LLVM_CONFIG").expect("LLVM_CONFIG was not set"));

    println!("cargo:rerun-if-changed={}", llvm_config.display());

    let llvm_config_supports_quote_paths =
        stderr(Command::new(&llvm_config).arg("--help")).contains("quote-paths");

    let quoted_split = |mut cmd: Command| {
        if llvm_config_supports_quote_paths {
            LlvmConfigOutput::QuotedPaths(output(cmd.arg("--quote-paths")))
        } else {
            LlvmConfigOutput::UnquotedPaths(output(&mut cmd))
        }
    };

    // Test whether we're cross-compiling LLVM. This is a pretty rare case
    // currently where we're producing an LLVM for a different platform than
    // what this build script is currently running on.
    //
    // In that case, there's no guarantee that we can actually run the target,
    // so the build system works around this by giving us the LLVM_CONFIG for
    // the host platform. This only really works if the host LLVM and target
    // LLVM are compiled the same way, but for us that's typically the case.
    //
    // We *want* detect this cross compiling situation by asking llvm-config
    // what its host-target is. If that's not the TARGET, then we're cross
    // compiling. Unfortunately `llvm-config` seems either be buggy, or we're
    // misconfiguring it, because the `i686-pc-windows-gnu` build of LLVM will
    // report itself with a `--host-target` of `x86_64-pc-windows-gnu`. This
    // tricks us into thinking we're doing a cross build when we aren't, so
    // havoc ensues.
    //
    // In any case, if we're cross compiling, this generally just means that we

Review comment (refactor):
`--quote-paths` was added in LLVM 22, so we should add a FIXME to remove this condition when the minimum LLVM version is bumped to 22.

Also if the FIXME comment includes `llvm_version >= (22, 0, 0)`, that will make this easier to find when searching for other LLVM version checks to remove.

Code after:

fn main() {
    if cfg!(feature = "check_only") {
        return;
    }

    for component in REQUIRED_COMPONENTS.iter().chain(OPTIONAL_COMPONENTS.iter()) {
        println!("cargo:rustc-check-cfg=cfg(llvm_component,values(\"{component}\"))");
    }

    if tracked_env_var_os("RUST_CHECK").is_some() {
        // If we're just running `check`, there's no need for LLVM to be built.
        return;
    }

    restore_library_path();

    let llvm_config =
        PathBuf::from(tracked_env_var_os("LLVM_CONFIG").expect("LLVM_CONFIG was not set"));

    println!("cargo:rerun-if-changed={}", llvm_config.display());

    // FIXME: `--quote-paths` was added to llvm-config in LLVM 22, so this test (and all its ensuing
    //        fallback paths) can be removed once we bump the minimum llvm_version >= (22, 0, 0).
    let llvm_config_supports_quote_paths =
        stderr(Command::new(&llvm_config).arg("--help")).contains("quote-paths");

    let quoted_split = |mut cmd: Command| {
        if llvm_config_supports_quote_paths {
            LlvmConfigOutput::QuotedPaths(output(cmd.arg("--quote-paths")))
        } else {
            LlvmConfigOutput::UnquotedPaths(output(&mut cmd))
        }
    };

    // Test whether we're cross-compiling LLVM. This is a pretty rare case
    // currently where we're producing an LLVM for a different platform than
    // what this build script is currently running on.
    //
    // In that case, there's no guarantee that we can actually run the target,
    // so the build system works around this by giving us the LLVM_CONFIG for
    // the host platform. This only really works if the host LLVM and target
    // LLVM are compiled the same way, but for us that's typically the case.
    //
    // We *want* detect this cross compiling situation by asking llvm-config
    // what its host-target is. If that's not the TARGET, then we're cross
    // compiling. Unfortunately `llvm-config` seems either be buggy, or we're
    // misconfiguring it, because the `i686-pc-windows-gnu` build of LLVM will
    // report itself with a `--host-target` of `x86_64-pc-windows-gnu`. This
    // tricks us into thinking we're doing a cross build when we aren't, so
    // havoc ensues.