Skip to content

Commit

Permalink
Use Cargo.lock from workspace root
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Elarnon authored and eqrion committed Jul 31, 2018
1 parent 2f9d3cd commit ba589a8
Show file tree
Hide file tree
Showing 19 changed files with 148 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/bindgen/cargo/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ impl Cargo {
clean: bool,
) -> Result<Cargo, Error> {
let toml_path = crate_dir.join("Cargo.toml");
let lock_path = match lock_file {
Some(v) => PathBuf::from(v),
None => crate_dir.join("Cargo.lock"),
};
let metadata = cargo_metadata::metadata(&toml_path)
.map_err(|x| Error::CargoMetadata(toml_path.to_str().unwrap().to_owned(), x))?;
let lock_path = lock_file
.map(PathBuf::from)
.unwrap_or_else(|| Path::new(&metadata.workspace_root).join("Cargo.lock"));

let lock = if use_cargo_lock {
match cargo_lock::lock(&lock_path) {
Expand All @@ -62,8 +63,6 @@ impl Cargo {
} else {
None
};
let metadata = cargo_metadata::metadata(&toml_path)
.map_err(|x| Error::CargoMetadata(toml_path.to_str().unwrap().to_owned(), x))?;

// Use the specified binding crate name or infer it from the manifest
let manifest = cargo_toml::manifest(&toml_path)
Expand Down
2 changes: 2 additions & 0 deletions src/bindgen/cargo/cargo_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct Metadata {
/// A list of all crates referenced by this crate (and the crate itself)
pub packages: Vec<Package>,
version: usize,
/// path to the workspace containing the `Cargo.lock`
pub workspace_root: String,
}

#[derive(Clone, Deserialize, Debug)]
Expand Down
9 changes: 9 additions & 0 deletions tests/expectations/both/external_workspace_child.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct ExtType {
uint32_t data;
} ExtType;

void consume_ext(ExtType _ext);
9 changes: 9 additions & 0 deletions tests/expectations/both/workspace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct ExtType {
uint32_t data;
} ExtType;

void consume_ext(ExtType _ext);
9 changes: 9 additions & 0 deletions tests/expectations/external_workspace_child.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct {
uint32_t data;
} ExtType;

void consume_ext(ExtType _ext);
12 changes: 12 additions & 0 deletions tests/expectations/external_workspace_child.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <cstdint>
#include <cstdlib>

struct ExtType {
uint32_t data;
};

extern "C" {

void consume_ext(ExtType _ext);

} // extern "C"
9 changes: 9 additions & 0 deletions tests/expectations/tag/external_workspace_child.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>

struct ExtType {
uint32_t data;
};

void consume_ext(struct ExtType _ext);
9 changes: 9 additions & 0 deletions tests/expectations/tag/workspace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>

struct ExtType {
uint32_t data;
};

void consume_ext(struct ExtType _ext);
9 changes: 9 additions & 0 deletions tests/expectations/workspace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct {
uint32_t data;
} ExtType;

void consume_ext(ExtType _ext);
12 changes: 12 additions & 0 deletions tests/expectations/workspace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <cstdint>
#include <cstdlib>

struct ExtType {
uint32_t data;
};

extern "C" {

void consume_ext(ExtType _ext);

} // extern "C"
8 changes: 8 additions & 0 deletions tests/rust/external_workspace_child/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "child"
version = "0.1.0"
authors = ["cbindgen"]
workspace = "../workspace"

[dependencies.dep]
path = "../workspace/dep"
2 changes: 2 additions & 0 deletions tests/rust/external_workspace_child/cbindgen.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[parse]
parse_deps = true
5 changes: 5 additions & 0 deletions tests/rust/external_workspace_child/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extern crate dep;

#[no_mangle]
pub extern "C" fn consume_ext(_ext: dep::ExtType) {
}
18 changes: 18 additions & 0 deletions tests/rust/workspace/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tests/rust/workspace/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "workspace"
version = "0.1.0"
authors = ["cbindgen"]

[dependencies.dep]
path = "dep"

[workspace]
members = [
"../external_workspace_child",
"dep",
]
2 changes: 2 additions & 0 deletions tests/rust/workspace/cbindgen.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[parse]
parse_deps = true
6 changes: 6 additions & 0 deletions tests/rust/workspace/dep/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "dep"
version = "0.1.0"
authors = ["cbindgen"]

[dependencies]
4 changes: 4 additions & 0 deletions tests/rust/workspace/dep/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[repr(C)]
pub struct ExtType {
pub data: u32,
}
5 changes: 5 additions & 0 deletions tests/rust/workspace/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extern crate dep;

#[no_mangle]
pub extern "C" fn consume_ext(_ext: dep::ExtType) {
}

0 comments on commit ba589a8

Please sign in to comment.