Skip to content

Commit 67075be

Browse files
committed
Auto merge of #8418 - bdonlan:cdylib-out-path, r=alexcrichton
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.
2 parents 2f4097a + d19ff18 commit 67075be

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

src/cargo/core/compiler/compilation.rs

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ pub struct Compilation<'cfg> {
3434
/// An array of all binaries created.
3535
pub binaries: Vec<(Unit, PathBuf)>,
3636

37+
/// An array of all cdylibs created.
38+
pub cdylibs: Vec<(Unit, PathBuf)>,
39+
3740
/// All directories for the output of native build commands.
3841
///
3942
/// This is currently used to drive some entries which are added to the
@@ -123,6 +126,7 @@ impl<'cfg> Compilation<'cfg> {
123126
.collect(),
124127
tests: Vec::new(),
125128
binaries: Vec::new(),
129+
cdylibs: Vec::new(),
126130
extra_env: HashMap::new(),
127131
to_doc_test: Vec::new(),
128132
cfgs: HashMap::new(),

src/cargo/core/compiler/context/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
179179
self.compilation
180180
.binaries
181181
.push((unit.clone(), bindst.clone()));
182+
} else if unit.target.is_cdylib() {
183+
if !self.compilation.cdylibs.iter().any(|(u, _)| u == unit) {
184+
self.compilation
185+
.cdylibs
186+
.push((unit.clone(), bindst.clone()));
187+
}
182188
}
183189
}
184190

tests/testsuite/build.rs

+53-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Tests for the `cargo build` command.
22
3-
use cargo::util::paths::dylib_path_envvar;
3+
use cargo::{
4+
core::compiler::CompileMode, core::Workspace, ops::CompileOptions,
5+
util::paths::dylib_path_envvar, Config,
6+
};
47
use cargo_test_support::paths::{root, CargoPathExt};
58
use cargo_test_support::registry::Package;
69
use cargo_test_support::{
@@ -399,6 +402,55 @@ Caused by:
399402
.run();
400403
}
401404

405+
#[cargo_test]
406+
fn cargo_compile_api_exposes_artifact_paths() {
407+
let p = project()
408+
.file(
409+
"Cargo.toml",
410+
r#"
411+
[package]
412+
name = "foo"
413+
authors = []
414+
version = "0.0.0"
415+
416+
[[bin]]
417+
name = "the_foo_bin"
418+
path = "src/bin.rs"
419+
420+
[lib]
421+
name = "the_foo_lib"
422+
path = "src/foo.rs"
423+
crate-type = ["cdylib", "rlib"]
424+
"#,
425+
)
426+
.file("src/foo.rs", "pub fn bar() {}")
427+
.file("src/bin.rs", "pub fn main() {}")
428+
.build();
429+
430+
let config = Config::default().unwrap();
431+
let ws = Workspace::new(&p.root().join("Cargo.toml"), &config).unwrap();
432+
let compile_options = CompileOptions::new(ws.config(), CompileMode::Build).unwrap();
433+
434+
let result = cargo::ops::compile(&ws, &compile_options).unwrap();
435+
436+
assert_eq!(1, result.binaries.len());
437+
assert!(result.binaries[0].1.exists());
438+
assert!(result.binaries[0]
439+
.1
440+
.to_str()
441+
.unwrap()
442+
.contains("the_foo_bin"));
443+
444+
assert_eq!(1, result.cdylibs.len());
445+
// The exact library path varies by platform, but should certainly exist at least
446+
assert!(result.cdylibs[0].1.exists());
447+
assert!(result.cdylibs[0]
448+
.1
449+
.to_str()
450+
.unwrap()
451+
.contains("the_foo_lib"));
452+
}
453+
402454
#[cargo_test]
403455
fn cargo_compile_with_bin_and_proc() {
404456
let p = project()

0 commit comments

Comments
 (0)