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

Fix for finding dependency version in lockfile v2 #446

Merged
merged 1 commit into from
Jan 9, 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
20 changes: 20 additions & 0 deletions src/bindgen/cargo/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ impl Cargo {
.map(|dep| {
let (dep_name, dep_version) = parse_dep_string(dep);

// If a version was not specified find the only package with the name of the dependency
let dep_version = dep_version.or_else(|| {
let mut versions = self.metadata.packages.iter().filter_map(|package| {
if package.name_and_version.name != dep_name {
return None;
}
package.name_and_version.version.as_ref().map(|v| v.as_str())
});

// If the iterator contains more items, meaning multiple versions of the same
// package are present, warn! amd abort.
let version = versions.next();
if versions.next().is_none() {
version
} else {
warn!("when looking for a version for package {}, multiple versions where found", dep_name);
None
}
});

// Try to find the cfgs in the Cargo.toml
let cfg = self
.metadata
Expand Down
11 changes: 11 additions & 0 deletions tests/expectations/both/dep_v2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct dep_struct {
uint32_t x;
double y;
} dep_struct;

uint32_t get_x(const dep_struct *dep_struct);
19 changes: 19 additions & 0 deletions tests/expectations/both/dep_v2.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct dep_struct {
uint32_t x;
double y;
} dep_struct;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

uint32_t get_x(const dep_struct *dep_struct);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
11 changes: 11 additions & 0 deletions tests/expectations/dep_v2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct {
uint32_t x;
double y;
} dep_struct;

uint32_t get_x(const dep_struct *dep_struct);
19 changes: 19 additions & 0 deletions tests/expectations/dep_v2.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct {
uint32_t x;
double y;
} dep_struct;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

uint32_t get_x(const dep_struct *dep_struct);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
15 changes: 15 additions & 0 deletions tests/expectations/dep_v2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <new>

struct dep_struct {
uint32_t x;
double y;
};

extern "C" {

uint32_t get_x(const dep_struct *dep_struct);

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

struct dep_struct {
uint32_t x;
double y;
};

uint32_t get_x(const struct dep_struct *dep_struct);
19 changes: 19 additions & 0 deletions tests/expectations/tag/dep_v2.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

struct dep_struct {
uint32_t x;
double y;
};

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

uint32_t get_x(const struct dep_struct *dep_struct);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
12 changes: 12 additions & 0 deletions tests/rust/dep_v2/Cargo.lock

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

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

[dependencies]
dep = { path = "dep" }
3 changes: 3 additions & 0 deletions tests/rust/dep_v2/cbindgen.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[parse]
parse_deps = true
include = ["dep"]
7 changes: 7 additions & 0 deletions tests/rust/dep_v2/dep/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "dep"
version = "0.1.0"
authors = ["cbindgen"]
edition = "2018"

[dependencies]
5 changes: 5 additions & 0 deletions tests/rust/dep_v2/dep/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[repr(C)]
pub struct dep_struct {
pub x: u32,
pub y: f64,
}
6 changes: 6 additions & 0 deletions tests/rust/dep_v2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use dep::dep_struct;

#[no_mangle]
pub unsafe extern "C" fn get_x(dep_struct: *const dep_struct) -> u32 {
dep_struct.read().x
}