diff --git a/src/bindgen/cargo/cargo.rs b/src/bindgen/cargo/cargo.rs index fa9d6c222..942de9b2c 100644 --- a/src/bindgen/cargo/cargo.rs +++ b/src/bindgen/cargo/cargo.rs @@ -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. @@ -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; @@ -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) @@ -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, diff --git a/src/bindgen/cargo/cargo_expand.rs b/src/bindgen/cargo/cargo_expand.rs index 86e45156f..469722322 100644 --- a/src/bindgen/cargo/cargo_expand.rs +++ b/src/bindgen/cargo/cargo_expand.rs @@ -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, @@ -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"); diff --git a/src/bindgen/cargo/cargo_lock.rs b/src/bindgen/cargo/cargo_lock.rs index bc5907117..9d68a3b2f 100644 --- a/src/bindgen/cargo/cargo_lock.rs +++ b/src/bindgen/cargo/cargo_lock.rs @@ -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>, } diff --git a/src/bindgen/cargo/cargo_metadata.rs b/src/bindgen/cargo/cargo_metadata.rs index 681acc8d3..2745029e9 100644 --- a/src/bindgen/cargo/cargo_metadata.rs +++ b/src/bindgen/cargo/cargo_metadata.rs @@ -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, } #[derive(Clone, Deserialize, Debug)] diff --git a/src/bindgen/parser.rs b/src/bindgen/parser.rs index 67b3539cc..9f71e2874 100644 --- a/src/bindgen/parser.rs +++ b/src/bindgen/parser.rs @@ -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)?; diff --git a/tests/expectations/both/expand_dep_v2.c b/tests/expectations/both/expand_dep_v2.c new file mode 100644 index 000000000..401228965 --- /dev/null +++ b/tests/expectations/both/expand_dep_v2.c @@ -0,0 +1,11 @@ +#include +#include +#include +#include + +typedef struct dep_struct { + uint32_t x; + double y; +} dep_struct; + +uint32_t get_x(const dep_struct *dep_struct); diff --git a/tests/expectations/both/expand_dep_v2.compat.c b/tests/expectations/both/expand_dep_v2.compat.c new file mode 100644 index 000000000..056089ef0 --- /dev/null +++ b/tests/expectations/both/expand_dep_v2.compat.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +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 diff --git a/tests/expectations/expand_dep_v2.c b/tests/expectations/expand_dep_v2.c new file mode 100644 index 000000000..3d2cf8e14 --- /dev/null +++ b/tests/expectations/expand_dep_v2.c @@ -0,0 +1,11 @@ +#include +#include +#include +#include + +typedef struct { + uint32_t x; + double y; +} dep_struct; + +uint32_t get_x(const dep_struct *dep_struct); diff --git a/tests/expectations/expand_dep_v2.compat.c b/tests/expectations/expand_dep_v2.compat.c new file mode 100644 index 000000000..4dc7fb377 --- /dev/null +++ b/tests/expectations/expand_dep_v2.compat.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +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 diff --git a/tests/expectations/expand_dep_v2.cpp b/tests/expectations/expand_dep_v2.cpp new file mode 100644 index 000000000..f03f098b1 --- /dev/null +++ b/tests/expectations/expand_dep_v2.cpp @@ -0,0 +1,15 @@ +#include +#include +#include +#include + +struct dep_struct { + uint32_t x; + double y; +}; + +extern "C" { + +uint32_t get_x(const dep_struct *dep_struct); + +} // extern "C" diff --git a/tests/expectations/tag/expand_dep_v2.c b/tests/expectations/tag/expand_dep_v2.c new file mode 100644 index 000000000..45fcba665 --- /dev/null +++ b/tests/expectations/tag/expand_dep_v2.c @@ -0,0 +1,11 @@ +#include +#include +#include +#include + +struct dep_struct { + uint32_t x; + double y; +}; + +uint32_t get_x(const struct dep_struct *dep_struct); diff --git a/tests/expectations/tag/expand_dep_v2.compat.c b/tests/expectations/tag/expand_dep_v2.compat.c new file mode 100644 index 000000000..344af6468 --- /dev/null +++ b/tests/expectations/tag/expand_dep_v2.compat.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +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 diff --git a/tests/rust/expand_dep_v2/Cargo.lock b/tests/rust/expand_dep_v2/Cargo.lock new file mode 100644 index 000000000..5e12ed603 --- /dev/null +++ b/tests/rust/expand_dep_v2/Cargo.lock @@ -0,0 +1,24 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "dep" +version = "0.1.0" + +[[package]] +name = "dep" +version = "0.2.0" + +[[package]] +name = "expand-dep" +version = "0.1.0" +dependencies = [ + "dep 0.1.0", +] + +[[package]] +name = "expand-dep-2" +version = "0.2.0" +dependencies = [ + "dep 0.2.0", + "expand-dep", +] diff --git a/tests/rust/expand_dep_v2/Cargo.toml b/tests/rust/expand_dep_v2/Cargo.toml new file mode 100644 index 000000000..fbfeb8a09 --- /dev/null +++ b/tests/rust/expand_dep_v2/Cargo.toml @@ -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" } diff --git a/tests/rust/expand_dep_v2/cbindgen.toml b/tests/rust/expand_dep_v2/cbindgen.toml new file mode 100644 index 000000000..b6e180a04 --- /dev/null +++ b/tests/rust/expand_dep_v2/cbindgen.toml @@ -0,0 +1,6 @@ +[parse] +parse_deps = true +include = ["dep"] + +[parse.expand] +crates = ["expand-dep"] diff --git a/tests/rust/expand_dep_v2/dep/Cargo.toml b/tests/rust/expand_dep_v2/dep/Cargo.toml new file mode 100644 index 000000000..1cc8d6241 --- /dev/null +++ b/tests/rust/expand_dep_v2/dep/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "dep" +version = "0.1.0" +authors = ["cbindgen"] +edition = "2018" + +[dependencies] diff --git a/tests/rust/expand_dep_v2/dep/src/lib.rs b/tests/rust/expand_dep_v2/dep/src/lib.rs new file mode 100644 index 000000000..7253c5770 --- /dev/null +++ b/tests/rust/expand_dep_v2/dep/src/lib.rs @@ -0,0 +1,5 @@ +#[repr(C)] +pub struct dep_struct { + pub x: u32, + pub y: f64, +} diff --git a/tests/rust/expand_dep_v2/dep_v2/Cargo.toml b/tests/rust/expand_dep_v2/dep_v2/Cargo.toml new file mode 100644 index 000000000..fe52e954c --- /dev/null +++ b/tests/rust/expand_dep_v2/dep_v2/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "dep" +version = "0.2.0" +authors = ["cbindgen"] +edition = "2018" + +[dependencies] diff --git a/tests/rust/expand_dep_v2/dep_v2/src/lib.rs b/tests/rust/expand_dep_v2/dep_v2/src/lib.rs new file mode 100644 index 000000000..7253c5770 --- /dev/null +++ b/tests/rust/expand_dep_v2/dep_v2/src/lib.rs @@ -0,0 +1,5 @@ +#[repr(C)] +pub struct dep_struct { + pub x: u32, + pub y: f64, +} diff --git a/tests/rust/expand_dep_v2/src/lib.rs b/tests/rust/expand_dep_v2/src/lib.rs new file mode 100644 index 000000000..639678d07 --- /dev/null +++ b/tests/rust/expand_dep_v2/src/lib.rs @@ -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 +}