Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose built cdylib artifacts in the Compilation structure #8418

Merged
merged 1 commit into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub struct Compilation<'cfg> {
/// An array of all binaries created.
pub binaries: Vec<(Unit, PathBuf)>,

/// An array of all cdylibs created.
pub cdylibs: Vec<(Unit, PathBuf)>,

/// All directories for the output of native build commands.
///
/// This is currently used to drive some entries which are added to the
Expand Down Expand Up @@ -123,6 +126,7 @@ impl<'cfg> Compilation<'cfg> {
.collect(),
tests: Vec::new(),
binaries: Vec::new(),
cdylibs: Vec::new(),
extra_env: HashMap::new(),
to_doc_test: Vec::new(),
cfgs: HashMap::new(),
Expand Down
6 changes: 6 additions & 0 deletions src/cargo/core/compiler/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
self.compilation
.binaries
.push((unit.clone(), bindst.clone()));
} else if unit.target.is_cdylib() {
if !self.compilation.cdylibs.iter().any(|(u, _)| u == unit) {
self.compilation
.cdylibs
.push((unit.clone(), bindst.clone()));
}
}
}

Expand Down
54 changes: 53 additions & 1 deletion tests/testsuite/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Tests for the `cargo build` command.

use cargo::util::paths::dylib_path_envvar;
use cargo::{
core::compiler::CompileMode, core::Workspace, ops::CompileOptions,
util::paths::dylib_path_envvar, Config,
};
use cargo_test_support::paths::{root, CargoPathExt};
use cargo_test_support::registry::Package;
use cargo_test_support::{
Expand Down Expand Up @@ -399,6 +402,55 @@ Caused by:
.run();
}

#[cargo_test]
fn cargo_compile_api_exposes_artifact_paths() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
authors = []
version = "0.0.0"

[[bin]]
name = "the_foo_bin"
path = "src/bin.rs"

[lib]
name = "the_foo_lib"
path = "src/foo.rs"
crate-type = ["cdylib", "rlib"]
"#,
)
.file("src/foo.rs", "pub fn bar() {}")
.file("src/bin.rs", "pub fn main() {}")
.build();

let config = Config::default().unwrap();
let ws = Workspace::new(&p.root().join("Cargo.toml"), &config).unwrap();
let compile_options = CompileOptions::new(ws.config(), CompileMode::Build).unwrap();

let result = cargo::ops::compile(&ws, &compile_options).unwrap();

assert_eq!(1, result.binaries.len());
assert!(result.binaries[0].1.exists());
assert!(result.binaries[0]
.1
.to_str()
.unwrap()
.contains("the_foo_bin"));

assert_eq!(1, result.cdylibs.len());
// The exact library path varies by platform, but should certainly exist at least
assert!(result.cdylibs[0].1.exists());
assert!(result.cdylibs[0]
.1
.to_str()
.unwrap()
.contains("the_foo_lib"));
}

#[cargo_test]
fn cargo_compile_with_bin_and_proc() {
let p = project()
Expand Down