diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index 8a703fc1b70..7f0786652e6 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -8,9 +8,7 @@ //! * [`TargetInfo::rustc_outputs`] to get a list of supported file types. use crate::core::compiler::apply_env_config; -use crate::core::compiler::{ - BuildOutput, BuildRunner, CompileKind, CompileMode, CompileTarget, CrateType, -}; +use crate::core::compiler::{BuildRunner, CompileKind, CompileMode, CompileTarget, CrateType}; use crate::core::{Dependency, Package, Target, TargetKind, Workspace}; use crate::util::context::{GlobalContext, StringList, TargetConfig}; use crate::util::interning::InternedString; @@ -1038,14 +1036,6 @@ impl<'gctx> RustcTargetData<'gctx> { CompileKind::Target(s) => &self.target_config[&s], } } - - /// If a build script is overridden, this returns the `BuildOutput` to use. - /// - /// `lib_name` is the `links` library name and `kind` is whether it is for - /// Host or Target. - pub fn script_override(&self, lib_name: &str, kind: CompileKind) -> Option<&BuildOutput> { - self.target_config(kind).links_overrides.get(lib_name) - } } /// Structure used to deal with Rustdoc fingerprinting diff --git a/src/cargo/core/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index ec4ac68adb2..98ceeb5ad73 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -61,7 +61,7 @@ const OLD_CARGO_WARNING_SYNTAX: &str = "cargo:warning="; /// [the doc]: https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#cargo-warning const NEW_CARGO_WARNING_SYNTAX: &str = "cargo::warning="; /// Contains the parsed output of a custom build script. -#[derive(Clone, Debug, Hash, Default)] +#[derive(Clone, Debug, Hash, Default, PartialEq, Eq, PartialOrd, Ord)] pub struct BuildOutput { /// Paths to pass to rustc with the `-L` flag. pub library_paths: Vec, @@ -160,7 +160,7 @@ pub struct BuildDeps { /// See the [build script documentation][1] for more. /// /// [1]: https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#cargorustc-link-argflag -#[derive(Clone, Hash, Debug, PartialEq, Eq)] +#[derive(Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord)] pub enum LinkArgTarget { /// Represents `cargo::rustc-link-arg=FLAG`. All, @@ -1168,11 +1168,7 @@ pub fn build_map(build_runner: &mut BuildRunner<'_, '_>) -> CargoResult<()> { // If there is a build script override, pre-fill the build output. if unit.mode.is_run_custom_build() { if let Some(links) = unit.pkg.manifest().links() { - if let Some(output) = build_runner - .bcx - .target_data - .script_override(links, unit.kind) - { + if let Some(output) = unit.links_overrides.get(links) { let metadata = build_runner.get_run_build_script_metadata(unit); build_runner.build_script_outputs.lock().unwrap().insert( unit.pkg.package_id(), diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs index 8f095b73ef6..8b24cf7cdcb 100644 --- a/src/cargo/core/compiler/standard_lib.rs +++ b/src/cargo/core/compiler/standard_lib.rs @@ -219,6 +219,7 @@ pub fn generate_std_roots( features.clone(), target_data.info(*kind).rustflags.clone(), target_data.info(*kind).rustdocflags.clone(), + target_data.target_config(*kind).links_overrides.clone(), /*is_std*/ true, /*dep_hash*/ 0, IsArtifact::No, diff --git a/src/cargo/core/compiler/unit.rs b/src/cargo/core/compiler/unit.rs index 319916ac13b..a26b92574e7 100644 --- a/src/cargo/core/compiler/unit.rs +++ b/src/cargo/core/compiler/unit.rs @@ -9,13 +9,15 @@ use crate::util::hex::short_hash; use crate::util::interning::InternedString; use crate::util::GlobalContext; use std::cell::RefCell; -use std::collections::HashSet; +use std::collections::{BTreeMap, HashSet}; use std::fmt; use std::hash::{Hash, Hasher}; use std::ops::Deref; use std::rc::Rc; use std::sync::Arc; +use super::BuildOutput; + /// All information needed to define a unit. /// /// A unit is an object that has enough information so that cargo knows how to build it. @@ -82,6 +84,12 @@ pub struct UnitInner { /// [`BuildContext::extra_args_for`]: crate::core::compiler::build_context::BuildContext::extra_args_for /// [`TargetInfo.rustdocflags`]: crate::core::compiler::build_context::TargetInfo::rustdocflags pub rustdocflags: Arc<[String]>, + /// Build script override for the given library name. + /// + /// Any package with a `links` value for the given library name will skip + /// running its build script and instead use the given output from the + /// config file. + pub links_overrides: Rc>, // if `true`, the dependency is an artifact dependency, requiring special handling when // calculating output directories, linkage and environment variables provided to builds. pub artifact: IsArtifact, @@ -176,6 +184,7 @@ impl fmt::Debug for Unit { .field("features", &self.features) .field("rustflags", &self.rustflags) .field("rustdocflags", &self.rustdocflags) + .field("links_overrides", &self.links_overrides) .field("artifact", &self.artifact.is_true()) .field( "artifact_target_for_features", @@ -225,6 +234,7 @@ impl UnitInterner { features: Vec, rustflags: Arc<[String]>, rustdocflags: Arc<[String]>, + links_overrides: Rc>, is_std: bool, dep_hash: u64, artifact: IsArtifact, @@ -260,6 +270,7 @@ impl UnitInterner { features, rustflags, rustdocflags, + links_overrides, is_std, dep_hash, artifact, diff --git a/src/cargo/core/compiler/unit_dependencies.rs b/src/cargo/core/compiler/unit_dependencies.rs index 5ecd5890940..59e356fc682 100644 --- a/src/cargo/core/compiler/unit_dependencies.rs +++ b/src/cargo/core/compiler/unit_dependencies.rs @@ -457,11 +457,7 @@ fn compute_deps_custom_build( state: &State<'_, '_>, ) -> CargoResult> { if let Some(links) = unit.pkg.manifest().links() { - if state - .target_data - .script_override(links, unit.kind) - .is_some() - { + if unit.links_overrides.get(links).is_some() { // Overridden build scripts don't have any dependencies. return Ok(Vec::new()); } @@ -861,6 +857,11 @@ fn new_unit_dep_with_profile( features, state.target_data.info(kind).rustflags.clone(), state.target_data.info(kind).rustdocflags.clone(), + state + .target_data + .target_config(kind) + .links_overrides + .clone(), state.is_std, /*dep_hash*/ 0, artifact.map_or(IsArtifact::No, |_| IsArtifact::Yes), diff --git a/src/cargo/ops/cargo_compile/mod.rs b/src/cargo/ops/cargo_compile/mod.rs index ca17eefe021..77f6266355f 100644 --- a/src/cargo/ops/cargo_compile/mod.rs +++ b/src/cargo/ops/cargo_compile/mod.rs @@ -698,6 +698,7 @@ fn traverse_and_share( unit.features.clone(), unit.rustflags.clone(), unit.rustdocflags.clone(), + unit.links_overrides.clone(), unit.is_std, unit.dep_hash, unit.artifact, @@ -725,6 +726,7 @@ fn traverse_and_share( unit.features.clone(), unit.rustflags.clone(), unit.rustdocflags.clone(), + unit.links_overrides.clone(), unit.is_std, new_dep_hash, unit.artifact, @@ -888,6 +890,7 @@ fn override_rustc_crate_types( unit.features.clone(), unit.rustflags.clone(), unit.rustdocflags.clone(), + unit.links_overrides.clone(), unit.is_std, unit.dep_hash, unit.artifact, diff --git a/src/cargo/ops/cargo_compile/unit_generator.rs b/src/cargo/ops/cargo_compile/unit_generator.rs index 09b70e4fe2d..ce10e173c6c 100644 --- a/src/cargo/ops/cargo_compile/unit_generator.rs +++ b/src/cargo/ops/cargo_compile/unit_generator.rs @@ -173,6 +173,7 @@ impl<'a> UnitGenerator<'a, '_> { features.clone(), self.target_data.info(kind).rustflags.clone(), self.target_data.info(kind).rustdocflags.clone(), + self.target_data.target_config(kind).links_overrides.clone(), /*is_std*/ false, /*dep_hash*/ 0, IsArtifact::No, diff --git a/src/cargo/util/context/target.rs b/src/cargo/util/context/target.rs index 6d7e7fde9b9..f306ecc1731 100644 --- a/src/cargo/util/context/target.rs +++ b/src/cargo/util/context/target.rs @@ -4,6 +4,7 @@ use crate::util::CargoResult; use serde::Deserialize; use std::collections::{BTreeMap, HashMap}; use std::path::PathBuf; +use std::rc::Rc; /// Config definition of a `[target.'cfg(…)']` table. /// @@ -36,7 +37,7 @@ pub struct TargetConfig { /// Any package with a `links` value for the given library name will skip /// running its build script and instead use the given output from the /// config file. - pub links_overrides: BTreeMap, + pub links_overrides: Rc>, } /// Loads all of the `target.'cfg()'` tables. @@ -128,7 +129,7 @@ fn load_config_table(gctx: &GlobalContext, prefix: &str) -> CargoResult