From cedab5d9fd24a4a597c2237abf6b87ce16f96e0d Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 7 Jan 2025 14:18:22 +0100 Subject: [PATCH 1/2] Add test for `cargo rustc --print cfg` that relies on an env target --- tests/testsuite/rustc.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/testsuite/rustc.rs b/tests/testsuite/rustc.rs index 599438bc09e..e58f0232d86 100644 --- a/tests/testsuite/rustc.rs +++ b/tests/testsuite/rustc.rs @@ -794,6 +794,38 @@ windows .run(); } +#[cargo_test] +fn rustc_with_print_cfg_config_toml_env() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file( + "targets/best-target.json", + r#"{ + "llvm-target": "x86_64-unknown-none", + "target-pointer-width": "64", + "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128", + "arch": "x86_64" +}"#, + ) + .file( + ".cargo/config.toml", + r#" +[build] +target = "best-target" +[env] +RUST_TARGET_PATH = { value = "./targets", relative = true } +"#, + ) + .file("src/main.rs", r#"fn main() {} "#) + .build(); + + p.cargo("rustc -Z unstable-options --print cfg") + .masquerade_as_nightly_cargo(&["print"]) + .with_status(101) + .with_stdout_data(str!["..."].unordered()) + .run(); +} + #[cargo_test] fn precedence() { // Ensure that the precedence of cargo-rustc is only lower than RUSTFLAGS, From b7a0c9dc56eb220c5c11c8ef28fab8c044da0bf2 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 7 Jan 2025 14:18:22 +0100 Subject: [PATCH 2/2] Setup cargo environment for `cargo rustc --print` --- src/cargo/ops/cargo_compile/mod.rs | 3 ++- tests/testsuite/rustc.rs | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cargo/ops/cargo_compile/mod.rs b/src/cargo/ops/cargo_compile/mod.rs index d03e529be74..1de49db3664 100644 --- a/src/cargo/ops/cargo_compile/mod.rs +++ b/src/cargo/ops/cargo_compile/mod.rs @@ -41,7 +41,7 @@ use std::sync::Arc; use crate::core::compiler::unit_dependencies::build_unit_dependencies; use crate::core::compiler::unit_graph::{self, UnitDep, UnitGraph}; -use crate::core::compiler::{standard_lib, CrateType, TargetInfo}; +use crate::core::compiler::{apply_env_config, standard_lib, CrateType, TargetInfo}; use crate::core::compiler::{BuildConfig, BuildContext, BuildRunner, Compilation}; use crate::core::compiler::{CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit}; use crate::core::compiler::{DefaultExecutor, Executor, UnitInterner}; @@ -188,6 +188,7 @@ pub fn print<'a>( } let target_info = TargetInfo::new(gctx, &build_config.requested_kinds, &rustc, *kind)?; let mut process = rustc.process(); + apply_env_config(gctx, &mut process)?; process.args(&target_info.rustflags); if let Some(args) = target_rustc_args { process.args(args); diff --git a/tests/testsuite/rustc.rs b/tests/testsuite/rustc.rs index e58f0232d86..f0a7cec9b3a 100644 --- a/tests/testsuite/rustc.rs +++ b/tests/testsuite/rustc.rs @@ -821,7 +821,6 @@ RUST_TARGET_PATH = { value = "./targets", relative = true } p.cargo("rustc -Z unstable-options --print cfg") .masquerade_as_nightly_cargo(&["print"]) - .with_status(101) .with_stdout_data(str!["..."].unordered()) .run(); }