Skip to content

Commit

Permalink
Auto merge of #8418 - bdonlan:cdylib-out-path, r=alexcrichton
Browse files Browse the repository at this point in the history
Expose built cdylib artifacts in the Compilation structure

This change makes it much easier to find these artifacts in a
platform-independent way when writing automation around the cargo API.
  • Loading branch information
bors committed Jun 26, 2020
2 parents 2f4097a + d19ff18 commit 67075be
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
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

0 comments on commit 67075be

Please sign in to comment.