Skip to content

Commit 5a635e4

Browse files
committed
Use Cargo.lock from workspace root
cbindgen currently assumes that the `Cargo.lock` is in directly in the crate directory as a sibling to `Cargo.toml`; however that is usually not the case inside a workspace. Instead, this PR extracts the workspace root from the output of `cargo metadata` [1] (already used to get a list of packages) and uses the `Cargo.lock` from there. 1. This is available since Rust 1.24; see rust-lang/cargo#4940
1 parent 52a187c commit 5a635e4

19 files changed

+150
-6
lines changed

src/bindgen/cargo/cargo.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ impl Cargo {
4646
clean: bool,
4747
) -> Result<Cargo, Error> {
4848
let toml_path = crate_dir.join("Cargo.toml");
49-
let lock_path = match lock_file {
50-
Some(v) => PathBuf::from(v),
51-
None => crate_dir.join("Cargo.lock"),
52-
};
49+
let metadata = cargo_metadata::metadata(&toml_path)
50+
.map_err(|x| Error::CargoMetadata(toml_path.to_str().unwrap().to_owned(), x))?;
51+
let lock_path = lock_file
52+
.map(PathBuf::from)
53+
.unwrap_or_else(|| {
54+
Path::new(&metadata.workspace_root).join("Cargo.lock")
55+
});
5356

5457
let lock = if use_cargo_lock {
5558
match cargo_lock::lock(&lock_path) {
@@ -62,8 +65,6 @@ impl Cargo {
6265
} else {
6366
None
6467
};
65-
let metadata = cargo_metadata::metadata(&toml_path)
66-
.map_err(|x| Error::CargoMetadata(toml_path.to_str().unwrap().to_owned(), x))?;
6768

6869
// Use the specified binding crate name or infer it from the manifest
6970
let manifest = cargo_toml::manifest(&toml_path)

src/bindgen/cargo/cargo_metadata.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ pub struct Metadata {
2424
/// A list of all crates referenced by this crate (and the crate itself)
2525
pub packages: Vec<Package>,
2626
version: usize,
27+
/// path to the workspace containing the `Cargo.lock`
28+
pub workspace_root: String,
2729
}
2830

2931
#[derive(Clone, Deserialize, Debug)]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdint.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
5+
typedef struct ExtType {
6+
uint32_t data;
7+
} ExtType;
8+
9+
void consume_ext(ExtType _ext);

tests/expectations/both/workspace.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdint.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
5+
typedef struct ExtType {
6+
uint32_t data;
7+
} ExtType;
8+
9+
void consume_ext(ExtType _ext);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdint.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
5+
typedef struct {
6+
uint32_t data;
7+
} ExtType;
8+
9+
void consume_ext(ExtType _ext);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <cstdint>
2+
#include <cstdlib>
3+
4+
struct ExtType {
5+
uint32_t data;
6+
};
7+
8+
extern "C" {
9+
10+
void consume_ext(ExtType _ext);
11+
12+
} // extern "C"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdint.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
5+
struct ExtType {
6+
uint32_t data;
7+
};
8+
9+
void consume_ext(struct ExtType _ext);

tests/expectations/tag/workspace.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdint.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
5+
struct ExtType {
6+
uint32_t data;
7+
};
8+
9+
void consume_ext(struct ExtType _ext);

tests/expectations/workspace.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdint.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
5+
typedef struct {
6+
uint32_t data;
7+
} ExtType;
8+
9+
void consume_ext(ExtType _ext);

tests/expectations/workspace.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <cstdint>
2+
#include <cstdlib>
3+
4+
struct ExtType {
5+
uint32_t data;
6+
};
7+
8+
extern "C" {
9+
10+
void consume_ext(ExtType _ext);
11+
12+
} // extern "C"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "child"
3+
version = "0.1.0"
4+
authors = ["cbindgen"]
5+
workspace = "../workspace"
6+
7+
[dependencies.dep]
8+
path = "../workspace/dep"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[parse]
2+
parse_deps = true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern crate dep;
2+
3+
#[no_mangle]
4+
pub extern "C" fn consume_ext(_ext: dep::ExtType) {
5+
}

tests/rust/workspace/Cargo.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/rust/workspace/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "workspace"
3+
version = "0.1.0"
4+
authors = ["cbindgen"]
5+
6+
[dependencies.dep]
7+
path = "dep"
8+
9+
[workspace]
10+
members = [
11+
"../external_workspace_child",
12+
"dep",
13+
]

tests/rust/workspace/cbindgen.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[parse]
2+
parse_deps = true

tests/rust/workspace/dep/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "dep"
3+
version = "0.1.0"
4+
authors = ["cbindgen"]
5+
6+
[dependencies]

tests/rust/workspace/dep/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[repr(C)]
2+
pub struct ExtType {
3+
pub data: u32,
4+
}

tests/rust/workspace/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern crate dep;
2+
3+
#[no_mangle]
4+
pub extern "C" fn consume_ext(_ext: dep::ExtType) {
5+
}

0 commit comments

Comments
 (0)