From 0fe95af29013103dbdbc560c8a2a54d29f0127a0 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Mon, 29 Apr 2024 22:01:33 +0200 Subject: [PATCH] Link folders and libs mentioned by build scripts --- CHANGELOG.md | 2 ++ src/dependencies.rs | 56 ++++++++++++++++++++++++++++++--------------- 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79867651..d66d928f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +* folders and libraries linked by build scripts were ignored, causing linker failures + ### Changed * removed `Revisioned::no_rustfix` in favor of turning that into a rustc-specific custom flag diff --git a/src/dependencies.rs b/src/dependencies.rs index 49d285cc..e9d580de 100644 --- a/src/dependencies.rs +++ b/src/dependencies.rs @@ -1,6 +1,6 @@ //! Use `cargo` to build dependencies and make them available in your tests -use cargo_metadata::{camino::Utf8PathBuf, DependencyKind}; +use cargo_metadata::{camino::Utf8PathBuf, BuildScript, DependencyKind}; use cargo_platform::Cfg; use color_eyre::eyre::{bail, eyre, Result}; use std::{ @@ -25,6 +25,8 @@ pub struct Dependencies { /// All paths that must be imported with `-L dependency=`. This is for /// finding proc macros run on the host and dependencies for the target. pub import_paths: Vec, + /// Unnamed dependencies that build scripts asked us to link + pub import_libs: Vec, /// The name as chosen in the `Cargo.toml` and its corresponding rmeta file. pub dependencies: Vec<(String, Vec)>, } @@ -95,31 +97,43 @@ fn build_dependencies_inner(config: &Config, info: &DependencyBuilder) -> Result let artifact_output = output.stdout; let artifact_output = String::from_utf8(artifact_output)?; let mut import_paths: HashSet = HashSet::new(); + let mut import_libs: HashSet = HashSet::new(); let mut artifacts = HashMap::new(); 'artifact: for line in artifact_output.lines() { let Ok(message) = serde_json::from_str::(line) else { continue; }; - if let cargo_metadata::Message::CompilerArtifact(artifact) = message { - for ctype in &artifact.target.crate_types { - match ctype.as_str() { - "proc-macro" | "lib" => {} - _ => continue 'artifact, + match message { + cargo_metadata::Message::CompilerArtifact(artifact) => { + for ctype in &artifact.target.crate_types { + match ctype.as_str() { + "proc-macro" | "lib" => {} + _ => continue 'artifact, + } + } + for filename in &artifact.filenames { + import_paths.insert(filename.parent().unwrap().into()); + } + let package_id = artifact.package_id; + if let Some(prev) = artifacts.insert(package_id.clone(), Ok(artifact.filenames)) { + artifacts.insert( + package_id.clone(), + Err(format!( + "{prev:#?} vs {:#?} ({:?})", + artifacts[&package_id], artifact.target.crate_types + )), + ); } } - for filename in &artifact.filenames { - import_paths.insert(filename.parent().unwrap().into()); - } - let package_id = artifact.package_id; - if let Some(prev) = artifacts.insert(package_id.clone(), Ok(artifact.filenames)) { - artifacts.insert( - package_id.clone(), - Err(format!( - "{prev:#?} vs {:#?} ({:?})", - artifacts[&package_id], artifact.target.crate_types - )), - ); + cargo_metadata::Message::BuildScriptExecuted(BuildScript { + linked_libs, + linked_paths, + .. + }) => { + import_paths.extend(linked_paths.into_iter().map(Into::into)); + import_libs.extend(linked_libs.into_iter().map(Into::into)); } + _ => {} } } @@ -204,9 +218,11 @@ fn build_dependencies_inner(config: &Config, info: &DependencyBuilder) -> Result }) .collect::>>()?; let import_paths = import_paths.into_iter().collect(); + let import_libs = import_libs.into_iter().collect(); return Ok(Dependencies { dependencies, import_paths, + import_libs, }); } @@ -288,5 +304,9 @@ pub fn build_dependencies(config: &Config, info: &DependencyBuilder) -> Result