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 dependency resolution with lockfile v2. #438

Merged
merged 3 commits into from
Dec 29, 2019
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
23 changes: 17 additions & 6 deletions src/bindgen/cargo/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use bindgen::error::Error;
use bindgen::ir::Cfg;

/// Parse a dependency string used in Cargo.lock
fn parse_dep_string(dep_string: &str) -> (&str, &str) {
fn parse_dep_string(dep_string: &str) -> (&str, Option<&str>) {
let split: Vec<&str> = dep_string.split_whitespace().collect();

(split[0], split[1])
(split[0], split.get(1).cloned())
}

/// A collection of metadata for a library from cargo.
Expand Down Expand Up @@ -99,14 +99,25 @@ impl Cargo {

// Find the dependencies listing in the lockfile
if let Some(ref root) = lock.root {
if root.name == package.name && root.version == package.version {
// If the version is not on the lockfile then it shouldn't be
// ambiguous.
if root.name == package.name
&& package
.version
.as_ref()
.map_or(true, |v| *v == root.version)
{
dependencies = root.dependencies.as_ref();
}
}
if dependencies.is_none() {
if let Some(ref lock_packages) = lock.package {
for lock_package in lock_packages {
if lock_package.name == package.name && lock_package.version == package.version
if lock_package.name == package.name
&& package
.version
.as_ref()
.map_or(true, |v| *v == lock_package.version)
{
dependencies = lock_package.dependencies.as_ref();
break;
Expand Down Expand Up @@ -134,7 +145,7 @@ impl Cargo {

let package_ref = PackageRef {
name: dep_name.to_owned(),
version: dep_version.to_owned(),
version: dep_version.map(|v| v.to_owned()),
};

(package_ref, cfg)
Expand Down Expand Up @@ -202,7 +213,7 @@ impl Cargo {
cargo_expand::expand(
&self.manifest_path,
&package.name,
&package.version,
package.version.as_ref().map(|v| &**v),
self.clean,
expand_all_features,
expand_default_features,
Expand Down
9 changes: 7 additions & 2 deletions src/bindgen/cargo/cargo_expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl error::Error for Error {
pub fn expand(
manifest_path: &Path,
crate_name: &str,
version: &str,
version: Option<&str>,
use_tempdir: bool,
expand_all_features: bool,
expand_default_features: bool,
Expand Down Expand Up @@ -109,7 +109,12 @@ pub fn expand(
cmd.arg("--no-default-features");
}
cmd.arg("-p");
cmd.arg(&format!("{}:{}", crate_name, version));
let mut package = crate_name.to_owned();
if let Some(version) = version {
package.push_str(":");
package.push_str(version);
}
cmd.arg(&package);
cmd.arg("--verbose");
cmd.arg("--");
cmd.arg("-Z");
Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/cargo/cargo_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct Lock {
pub struct Package {
pub name: String,
pub version: String,
/// A list of dependencies formatted like "NAME VERSION REGISTRY-OPT"
/// A list of dependencies formatted like "NAME VERSION-OPT REGISTRY-OPT"
pub dependencies: Option<Vec<String>>,
}

Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/cargo/cargo_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct Metadata {
#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct PackageRef {
pub name: String,
pub version: String,
pub version: Option<String>,
}

#[derive(Clone, Deserialize, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn parse_src(src_file: &FilePath, config: &Config) -> ParseResult {

let pkg_ref = PackageRef {
name: mod_name.to_owned(),
version: "0.0.0".to_owned(),
version: None,
};

context.parse_mod(&pkg_ref, src_file)?;
Expand Down
11 changes: 11 additions & 0 deletions tests/expectations/both/expand_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/expand_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/expand_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/expand_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/expand_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/expand_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/expand_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
24 changes: 24 additions & 0 deletions tests/rust/expand_dep_v2/Cargo.lock

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

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

[dependencies]
expand-dep = { path = "../expand_dep" }
dep = { path = "dep_v2" }
6 changes: 6 additions & 0 deletions tests/rust/expand_dep_v2/cbindgen.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[parse]
parse_deps = true
include = ["dep"]

[parse.expand]
crates = ["expand-dep"]
7 changes: 7 additions & 0 deletions tests/rust/expand_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/expand_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,
}
7 changes: 7 additions & 0 deletions tests/rust/expand_dep_v2/dep_v2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "dep"
version = "0.2.0"
authors = ["cbindgen"]
edition = "2018"

[dependencies]
5 changes: 5 additions & 0 deletions tests/rust/expand_dep_v2/dep_v2/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/expand_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
}