From 85bed138933463f3b2248a32e2fbdb45418020b3 Mon Sep 17 00:00:00 2001 From: sevenc-nanashi Date: Sun, 23 Jul 2023 06:55:22 +0900 Subject: [PATCH 01/15] Change: this project requires proc-macro2 1.0.60+ --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8f71f5e2d..499d92a09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -192,9 +192,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.58" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa1fb82fc0c281dd9671101b66b771ebbe1eaf967b96ac8740dcba4b70005ca8" +checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" dependencies = [ "unicode-ident", ] diff --git a/Cargo.toml b/Cargo.toml index 7b44bc399..05d95c043 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ serde = { version = "1.0.103", default-features = false, features = ["derive"] } serde_json = "1.0" tempfile = "3" toml = "0.5" -proc-macro2 = "1" +proc-macro2 = "1.0.60" quote = "1" heck = "0.4" From 1b7aab6a87e0ca788dea894aef87dd8b9d2a3c75 Mon Sep 17 00:00:00 2001 From: sevenc-nanashi Date: Sun, 23 Jul 2023 06:55:43 +0900 Subject: [PATCH 02/15] Add: Cython 3 fails test --- contributing.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contributing.md b/contributing.md index 5625d50d0..85822639f 100644 --- a/contributing.md +++ b/contributing.md @@ -16,6 +16,7 @@ There is continuous integration setup for `cbindgen` using [GitHub Actions](http In addition to a C/C++ compiler `cargo test` requires Python and Cython (`python -m pip install Cython`) for checking Cython bindings generated from tests (`.pyx` files). +Note that the tests will be failed with Cython 3.x or later. Please run `cargo test` before filing a pull request to be sure that all tests pass. This will also update the test expectations. From 667de09279c3d5f0524216fefa949e64cbd3bc1a Mon Sep 17 00:00:00 2001 From: sevenc-nanashi Date: Sun, 23 Jul 2023 06:58:20 +0900 Subject: [PATCH 03/15] Add: Add rust-toolchain.toml --- rust-toolchain.toml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 rust-toolchain.toml diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 000000000..5d56faf9a --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly" From b734008c71f31a125797e799f420e1ad32f6b2f9 Mon Sep 17 00:00:00 2001 From: boxdot Date: Mon, 19 Jun 2023 10:51:26 +0200 Subject: [PATCH 04/15] Upgrade clap 3 to clap 4 This also removes the dependency on the unmaintained crate atty. Closes #828 --- Cargo.lock | 253 ++++++++++++++++++++++++++++++++++++++++++---------- Cargo.toml | 2 +- src/main.rs | 107 +++++++++++----------- 3 files changed, 258 insertions(+), 104 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 499d92a09..e21361a51 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,14 +3,52 @@ version = 3 [[package]] -name = "atty" -version = "0.2.14" +name = "anstream" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163" dependencies = [ - "hermit-abi", - "libc", - "winapi", + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is-terminal", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41ed9a86bf92ae6580e0a31281f65a1b1d867c0cc68d5346e2ae128dddfa6a7d" + +[[package]] +name = "anstyle-parse" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e765fd216e48e067936442276d1d57399e37bce53c264d6fefbe298080cb57ee" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188" +dependencies = [ + "anstyle", + "windows-sys", ] [[package]] @@ -43,6 +81,12 @@ dependencies = [ "toml", ] +[[package]] +name = "cc" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" + [[package]] name = "cfg-if" version = "1.0.0" @@ -51,26 +95,57 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "3.2.25" +version = "4.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80672091db20273a15cf9fdd4e47ed43b5091ec9841bf4c6145c9dfbbcae09ed" +dependencies = [ + "clap_builder", +] + +[[package]] +name = "clap_builder" +version = "4.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" +checksum = "c1458a1df40e1e2afebb7ab60ce55c1fa8f431146205aa5f4887e0b111c27636" dependencies = [ - "atty", + "anstream", + "anstyle", "bitflags", "clap_lex", - "indexmap", "strsim", - "termcolor", - "textwrap", ] [[package]] name = "clap_lex" -version = "0.2.4" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + +[[package]] +name = "errno" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" +dependencies = [ + "errno-dragonfly", + "libc", + "windows-sys", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" dependencies = [ - "os_str_bytes", + "cc", + "libc", ] [[package]] @@ -96,12 +171,9 @@ checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] name = "hermit-abi" -version = "0.1.19" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] +checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" [[package]] name = "indexmap" @@ -122,6 +194,29 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "io-lifetimes" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys", +] + +[[package]] +name = "is-terminal" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f" +dependencies = [ + "hermit-abi", + "io-lifetimes", + "rustix", + "windows-sys", +] + [[package]] name = "itoa" version = "1.0.6" @@ -140,6 +235,12 @@ version = "0.2.144" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" +[[package]] +name = "linux-raw-sys" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" + [[package]] name = "lock_api" version = "0.4.9" @@ -159,12 +260,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "os_str_bytes" -version = "6.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ceedf44fb00f2d1984b0bc98102627ce622e083e49a5bacdb3e514fa4238e267" - [[package]] name = "parking_lot" version = "0.11.2" @@ -226,6 +321,20 @@ dependencies = [ "winapi", ] +[[package]] +name = "rustix" +version = "0.37.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b96e891d04aa506a6d1f318d2771bcb1c7dfda84e126660ace067c9b474bb2c0" +dependencies = [ + "bitflags", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "ryu" version = "1.0.13" @@ -339,21 +448,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "termcolor" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" -dependencies = [ - "winapi-util", -] - -[[package]] -name = "textwrap" -version = "0.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" - [[package]] name = "toml" version = "0.5.11" @@ -369,6 +463,12 @@ version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "winapi" version = "0.3.9" @@ -386,16 +486,73 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] -name = "winapi-util" -version = "0.1.5" +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "winapi", + "windows-targets", ] [[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" +name = "windows-targets" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" diff --git a/Cargo.toml b/Cargo.toml index 05d95c043..548562ab7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ exclude = [ ] [dependencies] -clap = { version = "3.1", optional = true } +clap = { version = "4", optional = true } indexmap = "1" log = "0.4" serde = { version = "1.0.103", default-features = false, features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index eb78a080b..8a607fb84 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,6 @@ use std::env; use std::io; use std::path::{Path, PathBuf}; -use std::str::FromStr; extern crate clap; #[macro_use] @@ -20,60 +19,60 @@ extern crate quote; extern crate syn; extern crate toml; +use clap::builder::PossibleValuesParser; +use clap::ArgAction; use clap::{Arg, ArgMatches, Command}; mod bindgen; mod logging; -use crate::bindgen::{Bindings, Builder, Cargo, Config, Error, Profile, Style}; +use crate::bindgen::{Bindings, Builder, Cargo, Config, Error}; fn apply_config_overrides(config: &mut Config, matches: &ArgMatches) { // We allow specifying a language to override the config default. This is // used by compile-tests. - if let Some(lang) = matches.value_of("lang") { - config.language = match lang.parse() { - Ok(lang) => lang, - Err(reason) => { - error!("{}", reason); - return; - } + match matches.try_get_one("lang") { + Ok(Some(lang)) => { + config.language = *lang; + } + Err(reason) => { + error!("{}", reason); + return; } + _ => (), } - if matches.is_present("cpp-compat") { + if matches.contains_id("cpp-compat") { config.cpp_compat = true; } - if matches.is_present("only-target-dependencies") { + if matches.contains_id("only-target-dependencies") { config.only_target_dependencies = true; } - if let Some(style) = matches.value_of("style") { - config.style = match style { - "Both" => Style::Both, - "both" => Style::Both, - "Tag" => Style::Tag, - "tag" => Style::Tag, - "Type" => Style::Type, - "type" => Style::Type, - _ => { - error!("Unknown style specified."); - return; - } + match matches.try_get_one("style") { + Ok(Some(style)) => { + config.style = *style; + } + Err(_) => { + error!("Unknown style specified."); + return; } + _ => (), } - if let Some(profile) = matches.value_of("profile") { - config.parse.expand.profile = match Profile::from_str(profile) { - Ok(p) => p, - Err(e) => { - error!("{}", e); - return; - } + match matches.try_get_one("profile") { + Ok(Some(profile)) => { + config.parse.expand.profile = *profile; + } + Err(e) => { + error!("{}", e); + return; } + _ => (), } - if matches.is_present("d") { + if matches.contains_id("d") { config.parse.parse_deps = true; } } @@ -82,7 +81,7 @@ fn load_bindings(input: &Path, matches: &ArgMatches) -> Result // If a file is specified then we load it as a single source if !input.is_dir() { // Load any config specified or search in the input directory - let mut config = match matches.value_of("config") { + let mut config = match matches.get_one::("config") { Some(c) => Config::from_file(c).unwrap(), None => Config::from_root_or_default( input @@ -102,16 +101,16 @@ fn load_bindings(input: &Path, matches: &ArgMatches) -> Result // We have to load a whole crate, so we use cargo to gather metadata let lib = Cargo::load( input, - matches.value_of("lockfile"), - matches.value_of("crate"), + matches.get_one::("lockfile").map(|s| s.as_str()), + matches.get_one::("crate").map(|s| s.as_str()), true, - matches.is_present("clean"), - matches.is_present("only-target-dependencies"), - matches.value_of("metadata").map(Path::new), + matches.contains_id("clean"), + matches.contains_id("only-target-dependencies"), + matches.get_one::("metadata").map(|p| p.as_path()), )?; // Load any config specified or search in the binding crate directory - let mut config = match matches.value_of("config") { + let mut config = match matches.get_one::("config") { Some(c) => Config::from_file(c).unwrap(), None => { let binding_crate_dir = lib.find_crate_dir(&lib.binding_crate_ref()); @@ -140,7 +139,7 @@ fn main() { .arg( Arg::new("v") .short('v') - .multiple_occurrences(true) + .action(ArgAction::Count) .help("Enable verbose logging"), ) .arg( @@ -161,7 +160,7 @@ fn main() { .long("lang") .value_name("LANGUAGE") .help("Specify the language to output bindings in") - .possible_values(["c++", "C++", "c", "C", "cython", "Cython"]), + .value_parser(PossibleValuesParser::new(["c++", "C++", "c", "C", "cython", "Cython"])), ) .arg( Arg::new("cpp-compat") @@ -180,7 +179,7 @@ fn main() { .long("style") .value_name("STYLE") .help("Specify the declaration style to use for bindings") - .possible_values(["Both", "both", "Tag", "tag", "Type", "type"]), + .value_parser(PossibleValuesParser::new(["Both", "both", "Tag", "tag", "Type", "type"])), ) .arg( Arg::new("d") @@ -257,7 +256,7 @@ fn main() { "Specify the profile to use when expanding macros. \ Has no effect otherwise." ) - .possible_values(["Debug", "debug", "Release", "release"]), + .value_parser(PossibleValuesParser::new(["Debug", "debug", "Release", "release"])), ) .arg( Arg::new("quiet") @@ -270,9 +269,7 @@ fn main() { Arg::new("depfile") .value_name("PATH") .long("depfile") - .takes_value(true) - .min_values(1) - .max_values(1) + .num_args(1) .required(false) .help("Generate a depfile at the given Path listing the source files \ cbindgen traversed when generating the bindings. Useful when \ @@ -282,7 +279,7 @@ fn main() { ) .get_matches(); - if !matches.is_present("out") && matches.is_present("verify") { + if !matches.contains_id("out") && matches.contains_id("verify") { error!( "Cannot verify bindings against `stdout`, please specify a file to compare against." ); @@ -290,10 +287,10 @@ fn main() { } // Initialize logging - if matches.is_present("quiet") { + if matches.contains_id("quiet") { logging::ErrorLogger::init().unwrap(); } else { - match matches.occurrences_of("v") { + match matches.get_count("v") { 0 => logging::WarnLogger::init().unwrap(), 1 => logging::InfoLogger::init().unwrap(), _ => logging::TraceLogger::init().unwrap(), @@ -301,10 +298,10 @@ fn main() { } // Find the input directory - let input = match matches.value_of("INPUT") { - Some(input) => PathBuf::from(input), - None => env::current_dir().unwrap(), - }; + let input: PathBuf = matches + .get_one("INPUT") + .cloned() + .unwrap_or_else(|| env::current_dir().unwrap()); let bindings = match load_bindings(&input, &matches) { Ok(bindings) => bindings, @@ -316,15 +313,15 @@ fn main() { }; // Write the bindings file - match matches.value_of("out") { + match matches.get_one::("out") { Some(file) => { let changed = bindings.write_to_file(file); - if matches.is_present("verify") && changed { + if matches.contains_id("verify") && changed { error!("Bindings changed: {}", file); std::process::exit(2); } - if let Some(depfile) = matches.value_of("depfile") { + if let Some(depfile) = matches.get_one("depfile") { bindings.generate_depfile(file, depfile) } } From b61aa2c330b3d4fe3bbe4fa71d9bc7104b7c94f7 Mon Sep 17 00:00:00 2001 From: boxdot Date: Thu, 22 Jun 2023 09:54:51 +0200 Subject: [PATCH 05/15] msrv 1.64 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 548562ab7..665beff1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ keywords = ["bindings", "ffi", "code-generation"] categories = ["external-ffi-bindings", "development-tools::ffi"] repository = "https://github.com/eqrion/cbindgen" edition = "2018" -rust-version = "1.57" +rust-version = "1.64" exclude = [ "tests/profile.rs", # Test relies in a sub-crate, see https://github.com/rust-lang/cargo/issues/9017 ] From f61946b9798982c4c27da9fd5917824db2093095 Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Mon, 5 Jun 2023 06:15:48 +0200 Subject: [PATCH 06/15] CI: Add semver checks to CI deploy job This will fail creating a new github release, if the semver action fails. This would have caught #841. --- .github/workflows/deploy.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 379e476d4..2fd88f89c 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -8,6 +8,11 @@ on: jobs: linux-binaries: + permissions: + # Grant the GITHUB_TOKEN additional permissions necessary for creating a release. + # We only run this action for tags, so any code has already been reviewed by + # someone with permissions to create a tag. + contents: write runs-on: ubuntu-latest @@ -17,6 +22,11 @@ jobs: - name: Install stable uses: dtolnay/rust-toolchain@stable + - name: semver + uses: obi1kenobi/cargo-semver-checks-action@v2 + with: + rust-toolchain: stable + - name: Build cbindgen run: | cargo build --release From 1e2ffd4414e1124f526a19ccb3b627c0e3694f53 Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Thu, 22 Jun 2023 08:00:50 +0800 Subject: [PATCH 07/15] CI: Replace forbidden actions with cli code the semver checks action and the release action are not in the allow-list so replace them with their respective cli counterparts. These changes can only be tested by someone with permissions, so the release part only releases a draft release for now. --- .github/workflows/deploy.yml | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 2fd88f89c..82b98d99f 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -14,7 +14,7 @@ jobs: # someone with permissions to create a tag. contents: write - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v3 @@ -23,9 +23,9 @@ jobs: uses: dtolnay/rust-toolchain@stable - name: semver - uses: obi1kenobi/cargo-semver-checks-action@v2 - with: - rust-toolchain: stable + run: | + cargo install cargo-semver-checks --locked + cargo semver-checks check-release - name: Build cbindgen run: | @@ -49,11 +49,8 @@ jobs: sed '$ d' | awk '{$1=$1};1' > CHANGES.txt - name: Create a release - uses: softprops/action-gh-release@v1 - with: - name: v${{ steps.tagName.outputs.version }} - body_path: CHANGES.txt - files: | - target/release/cbindgen + run: | + TAG=${{ steps.tagName.outputs.version }} + gh release create ${TAG} --title "${TAG}" --notes-file "CHANGES.txt" --draft 'target/release/cbindgen#cbindgen-ubuntu20.04' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 80526e72f9109d45da10625e790791fc3a5cc18a Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Thu, 22 Jun 2023 11:02:49 +0800 Subject: [PATCH 08/15] Update changelog for v0.25.0 --- CHANGES | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGES b/CHANGES index c19d62a78..c68796a75 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,11 @@ +## 0.25.0 + + * Re-release of yanked 0.24.6 as a major release + * Update MSRV to 1.57 + * Support variadic arguments (`...`) (#805) + * Add --depfile option (#820) + * Breaking changes: The `Config` struct now has a private member. + ## 0.24.6 (YANKED: depfile option was breaking, see #841) * Update MSRV to 1.57 From 67fea1a1a2e77464f96f184b298aed848ac38d53 Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Fri, 25 Aug 2023 16:08:10 +0200 Subject: [PATCH 09/15] Fix CI Commit 667de09279c3d5f0524216fefa949e64cbd3bc1a broke CI by adding rust-toolchain.toml, which changed the default rust version to nightly. Explicitly specify the rust version in CI to use the correct toolchain. --- .github/workflows/cbindgen.yml | 12 ++++++------ .github/workflows/deploy.yml | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/cbindgen.yml b/.github/workflows/cbindgen.yml index 93023aaeb..f5081f732 100644 --- a/.github/workflows/cbindgen.yml +++ b/.github/workflows/cbindgen.yml @@ -23,11 +23,11 @@ jobs: - name: Run rustfmt run: | - cargo fmt --check + cargo +stable fmt --check - name: Run clippy run: | - cargo clippy --workspace -- -D warnings + cargo +stable clippy --workspace -- -D warnings - name: Install minimum supported Rust version id: msrv @@ -59,18 +59,18 @@ jobs: - name: Build run: | - cargo build --verbose + cargo +stable build --verbose - name: Build no-default-features run: | - cargo build --verbose --no-default-features + cargo +stable build --verbose --no-default-features - name: Test package env: CBINDGEN_TEST_VERIFY: 1 run: | - cargo package --verbose - (cd target/package/cbindgen-$(cargo run -- --version | cut -d ' ' -f 2) && cargo test --verbose) + cargo +stable package --verbose + (cd target/package/cbindgen-$(cargo +stable run -- --version | cut -d ' ' -f 2) && cargo +stable test --verbose) - name: Install nightly Rust uses: dtolnay/rust-toolchain@nightly diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 82b98d99f..6aa6168da 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -24,12 +24,12 @@ jobs: - name: semver run: | - cargo install cargo-semver-checks --locked + cargo +stable install cargo-semver-checks --locked cargo semver-checks check-release - name: Build cbindgen run: | - cargo build --release + cargo +stable build --release - name: Strip cbindgen run: | From 289a31ba45c28a6d8d5eb681f7e1c83a23cdb673 Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Fri, 25 Aug 2023 16:14:58 +0200 Subject: [PATCH 10/15] Fix clippy warning Rust 1.72 added a new warning, which should have been ignored by clippy, since clippy.toml is still st to 1.57.0 --- src/bindgen/bindings.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bindgen/bindings.rs b/src/bindgen/bindings.rs index cdacdadca..410b11f30 100644 --- a/src/bindgen/bindings.rs +++ b/src/bindgen/bindings.rs @@ -141,7 +141,7 @@ impl Bindings { let mut canon_source_files: Vec<_> = self .source_files .iter() - .chain(self.config.config_path.as_ref().into_iter()) + .chain(self.config.config_path.as_ref()) .map(|p| p.canonicalize().unwrap()) .collect(); // Sorting makes testing easier by ensuring the output is ordered. From 0529d215e7a1b2ad94ca166c1b26ad96f10e4a1c Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Fri, 25 Aug 2023 16:19:10 +0200 Subject: [PATCH 11/15] Revert "Upgrade clap 3 to clap 4" This reverts commit b734008c71f31a125797e799f420e1ad32f6b2f9. --- Cargo.lock | 253 ++++++++++------------------------------------------ Cargo.toml | 2 +- src/main.rs | 107 +++++++++++----------- 3 files changed, 104 insertions(+), 258 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e21361a51..499d92a09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,52 +3,14 @@ version = 3 [[package]] -name = "anstream" -version = "0.3.2" +name = "atty" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ - "anstyle", - "anstyle-parse", - "anstyle-query", - "anstyle-wincon", - "colorchoice", - "is-terminal", - "utf8parse", -] - -[[package]] -name = "anstyle" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41ed9a86bf92ae6580e0a31281f65a1b1d867c0cc68d5346e2ae128dddfa6a7d" - -[[package]] -name = "anstyle-parse" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e765fd216e48e067936442276d1d57399e37bce53c264d6fefbe298080cb57ee" -dependencies = [ - "utf8parse", -] - -[[package]] -name = "anstyle-query" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" -dependencies = [ - "windows-sys", -] - -[[package]] -name = "anstyle-wincon" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188" -dependencies = [ - "anstyle", - "windows-sys", + "hermit-abi", + "libc", + "winapi", ] [[package]] @@ -81,12 +43,6 @@ dependencies = [ "toml", ] -[[package]] -name = "cc" -version = "1.0.79" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" - [[package]] name = "cfg-if" version = "1.0.0" @@ -95,57 +51,26 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "4.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80672091db20273a15cf9fdd4e47ed43b5091ec9841bf4c6145c9dfbbcae09ed" -dependencies = [ - "clap_builder", -] - -[[package]] -name = "clap_builder" -version = "4.3.4" +version = "3.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1458a1df40e1e2afebb7ab60ce55c1fa8f431146205aa5f4887e0b111c27636" +checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" dependencies = [ - "anstream", - "anstyle", + "atty", "bitflags", "clap_lex", + "indexmap", "strsim", + "termcolor", + "textwrap", ] [[package]] name = "clap_lex" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" - -[[package]] -name = "colorchoice" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" - -[[package]] -name = "errno" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" -dependencies = [ - "errno-dragonfly", - "libc", - "windows-sys", -] - -[[package]] -name = "errno-dragonfly" -version = "0.1.2" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" dependencies = [ - "cc", - "libc", + "os_str_bytes", ] [[package]] @@ -171,9 +96,12 @@ checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] name = "hermit-abi" -version = "0.3.1" +version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] [[package]] name = "indexmap" @@ -194,29 +122,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "io-lifetimes" -version = "1.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" -dependencies = [ - "hermit-abi", - "libc", - "windows-sys", -] - -[[package]] -name = "is-terminal" -version = "0.4.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f" -dependencies = [ - "hermit-abi", - "io-lifetimes", - "rustix", - "windows-sys", -] - [[package]] name = "itoa" version = "1.0.6" @@ -235,12 +140,6 @@ version = "0.2.144" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" -[[package]] -name = "linux-raw-sys" -version = "0.3.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" - [[package]] name = "lock_api" version = "0.4.9" @@ -260,6 +159,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "os_str_bytes" +version = "6.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ceedf44fb00f2d1984b0bc98102627ce622e083e49a5bacdb3e514fa4238e267" + [[package]] name = "parking_lot" version = "0.11.2" @@ -321,20 +226,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "rustix" -version = "0.37.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b96e891d04aa506a6d1f318d2771bcb1c7dfda84e126660ace067c9b474bb2c0" -dependencies = [ - "bitflags", - "errno", - "io-lifetimes", - "libc", - "linux-raw-sys", - "windows-sys", -] - [[package]] name = "ryu" version = "1.0.13" @@ -448,6 +339,21 @@ dependencies = [ "winapi", ] +[[package]] +name = "termcolor" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" + [[package]] name = "toml" version = "0.5.11" @@ -463,12 +369,6 @@ version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" -[[package]] -name = "utf8parse" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" - [[package]] name = "winapi" version = "0.3.9" @@ -486,73 +386,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" +name = "winapi-util" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "windows-sys" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-targets" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "winapi", ] [[package]] -name = "windows_aarch64_gnullvm" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" - -[[package]] -name = "windows_i686_gnu" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" - -[[package]] -name = "windows_i686_msvc" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.48.0" +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 665beff1f..e654e58b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ exclude = [ ] [dependencies] -clap = { version = "4", optional = true } +clap = { version = "3.1", optional = true } indexmap = "1" log = "0.4" serde = { version = "1.0.103", default-features = false, features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index 8a607fb84..eb78a080b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use std::env; use std::io; use std::path::{Path, PathBuf}; +use std::str::FromStr; extern crate clap; #[macro_use] @@ -19,60 +20,60 @@ extern crate quote; extern crate syn; extern crate toml; -use clap::builder::PossibleValuesParser; -use clap::ArgAction; use clap::{Arg, ArgMatches, Command}; mod bindgen; mod logging; -use crate::bindgen::{Bindings, Builder, Cargo, Config, Error}; +use crate::bindgen::{Bindings, Builder, Cargo, Config, Error, Profile, Style}; fn apply_config_overrides(config: &mut Config, matches: &ArgMatches) { // We allow specifying a language to override the config default. This is // used by compile-tests. - match matches.try_get_one("lang") { - Ok(Some(lang)) => { - config.language = *lang; - } - Err(reason) => { - error!("{}", reason); - return; + if let Some(lang) = matches.value_of("lang") { + config.language = match lang.parse() { + Ok(lang) => lang, + Err(reason) => { + error!("{}", reason); + return; + } } - _ => (), } - if matches.contains_id("cpp-compat") { + if matches.is_present("cpp-compat") { config.cpp_compat = true; } - if matches.contains_id("only-target-dependencies") { + if matches.is_present("only-target-dependencies") { config.only_target_dependencies = true; } - match matches.try_get_one("style") { - Ok(Some(style)) => { - config.style = *style; - } - Err(_) => { - error!("Unknown style specified."); - return; + if let Some(style) = matches.value_of("style") { + config.style = match style { + "Both" => Style::Both, + "both" => Style::Both, + "Tag" => Style::Tag, + "tag" => Style::Tag, + "Type" => Style::Type, + "type" => Style::Type, + _ => { + error!("Unknown style specified."); + return; + } } - _ => (), } - match matches.try_get_one("profile") { - Ok(Some(profile)) => { - config.parse.expand.profile = *profile; - } - Err(e) => { - error!("{}", e); - return; + if let Some(profile) = matches.value_of("profile") { + config.parse.expand.profile = match Profile::from_str(profile) { + Ok(p) => p, + Err(e) => { + error!("{}", e); + return; + } } - _ => (), } - if matches.contains_id("d") { + if matches.is_present("d") { config.parse.parse_deps = true; } } @@ -81,7 +82,7 @@ fn load_bindings(input: &Path, matches: &ArgMatches) -> Result // If a file is specified then we load it as a single source if !input.is_dir() { // Load any config specified or search in the input directory - let mut config = match matches.get_one::("config") { + let mut config = match matches.value_of("config") { Some(c) => Config::from_file(c).unwrap(), None => Config::from_root_or_default( input @@ -101,16 +102,16 @@ fn load_bindings(input: &Path, matches: &ArgMatches) -> Result // We have to load a whole crate, so we use cargo to gather metadata let lib = Cargo::load( input, - matches.get_one::("lockfile").map(|s| s.as_str()), - matches.get_one::("crate").map(|s| s.as_str()), + matches.value_of("lockfile"), + matches.value_of("crate"), true, - matches.contains_id("clean"), - matches.contains_id("only-target-dependencies"), - matches.get_one::("metadata").map(|p| p.as_path()), + matches.is_present("clean"), + matches.is_present("only-target-dependencies"), + matches.value_of("metadata").map(Path::new), )?; // Load any config specified or search in the binding crate directory - let mut config = match matches.get_one::("config") { + let mut config = match matches.value_of("config") { Some(c) => Config::from_file(c).unwrap(), None => { let binding_crate_dir = lib.find_crate_dir(&lib.binding_crate_ref()); @@ -139,7 +140,7 @@ fn main() { .arg( Arg::new("v") .short('v') - .action(ArgAction::Count) + .multiple_occurrences(true) .help("Enable verbose logging"), ) .arg( @@ -160,7 +161,7 @@ fn main() { .long("lang") .value_name("LANGUAGE") .help("Specify the language to output bindings in") - .value_parser(PossibleValuesParser::new(["c++", "C++", "c", "C", "cython", "Cython"])), + .possible_values(["c++", "C++", "c", "C", "cython", "Cython"]), ) .arg( Arg::new("cpp-compat") @@ -179,7 +180,7 @@ fn main() { .long("style") .value_name("STYLE") .help("Specify the declaration style to use for bindings") - .value_parser(PossibleValuesParser::new(["Both", "both", "Tag", "tag", "Type", "type"])), + .possible_values(["Both", "both", "Tag", "tag", "Type", "type"]), ) .arg( Arg::new("d") @@ -256,7 +257,7 @@ fn main() { "Specify the profile to use when expanding macros. \ Has no effect otherwise." ) - .value_parser(PossibleValuesParser::new(["Debug", "debug", "Release", "release"])), + .possible_values(["Debug", "debug", "Release", "release"]), ) .arg( Arg::new("quiet") @@ -269,7 +270,9 @@ fn main() { Arg::new("depfile") .value_name("PATH") .long("depfile") - .num_args(1) + .takes_value(true) + .min_values(1) + .max_values(1) .required(false) .help("Generate a depfile at the given Path listing the source files \ cbindgen traversed when generating the bindings. Useful when \ @@ -279,7 +282,7 @@ fn main() { ) .get_matches(); - if !matches.contains_id("out") && matches.contains_id("verify") { + if !matches.is_present("out") && matches.is_present("verify") { error!( "Cannot verify bindings against `stdout`, please specify a file to compare against." ); @@ -287,10 +290,10 @@ fn main() { } // Initialize logging - if matches.contains_id("quiet") { + if matches.is_present("quiet") { logging::ErrorLogger::init().unwrap(); } else { - match matches.get_count("v") { + match matches.occurrences_of("v") { 0 => logging::WarnLogger::init().unwrap(), 1 => logging::InfoLogger::init().unwrap(), _ => logging::TraceLogger::init().unwrap(), @@ -298,10 +301,10 @@ fn main() { } // Find the input directory - let input: PathBuf = matches - .get_one("INPUT") - .cloned() - .unwrap_or_else(|| env::current_dir().unwrap()); + let input = match matches.value_of("INPUT") { + Some(input) => PathBuf::from(input), + None => env::current_dir().unwrap(), + }; let bindings = match load_bindings(&input, &matches) { Ok(bindings) => bindings, @@ -313,15 +316,15 @@ fn main() { }; // Write the bindings file - match matches.get_one::("out") { + match matches.value_of("out") { Some(file) => { let changed = bindings.write_to_file(file); - if matches.contains_id("verify") && changed { + if matches.is_present("verify") && changed { error!("Bindings changed: {}", file); std::process::exit(2); } - if let Some(depfile) = matches.get_one("depfile") { + if let Some(depfile) = matches.value_of("depfile") { bindings.generate_depfile(file, depfile) } } From dd9a550152cd162a3aa01757a55dd22fc56d0d8a Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Fri, 25 Aug 2023 16:26:59 +0200 Subject: [PATCH 12/15] Fix minimal Rust version in CI The minimum rust version was bumped, so we need to install the appropriate version in CI. --- .github/workflows/cbindgen.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cbindgen.yml b/.github/workflows/cbindgen.yml index f5081f732..bc7a22e2d 100644 --- a/.github/workflows/cbindgen.yml +++ b/.github/workflows/cbindgen.yml @@ -31,7 +31,7 @@ jobs: - name: Install minimum supported Rust version id: msrv - uses: dtolnay/rust-toolchain@1.57 + uses: dtolnay/rust-toolchain@1.64 - name: Build with minimum supported Rust version run: | From aeea9c67fff738f3bf2f27cbe099c5928dff0927 Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Thu, 22 Jun 2023 10:23:24 +0800 Subject: [PATCH 13/15] Fix swapping of `>>=` and `<<=` in constants This should't change behavior in practice since using these in constants doesn't seem possible. Closes #850 --- CHANGES | 4 ++++ src/bindgen/ir/constant.rs | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index c68796a75..2cbf62984 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,7 @@ +# Unreleased + + * Fix swapping of `>>=` and `<<=` in constants. + ## 0.25.0 * Re-release of yanked 0.24.6 as a major release diff --git a/src/bindgen/ir/constant.rs b/src/bindgen/ir/constant.rs index 42182ab92..d3b9bd443 100644 --- a/src/bindgen/ir/constant.rs +++ b/src/bindgen/ir/constant.rs @@ -321,8 +321,8 @@ impl Literal { syn::BinOp::BitXorEq(..) => "^=", syn::BinOp::BitAndEq(..) => "&=", syn::BinOp::BitOrEq(..) => "|=", - syn::BinOp::ShlEq(..) => ">>=", - syn::BinOp::ShrEq(..) => "<<=", + syn::BinOp::ShlEq(..) => "<<=", + syn::BinOp::ShrEq(..) => ">>=", }; Ok(Literal::BinOp { left: Box::new(l), From acb1b8d89ee56bebc18dee5de19cab2b0e79b3cf Mon Sep 17 00:00:00 2001 From: boxdot Date: Mon, 19 Jun 2023 11:01:51 +0200 Subject: [PATCH 14/15] fix: verbose logging was always info --- src/logging.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/logging.rs b/src/logging.rs index 3b698c8ee..0b49ce2d8 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -14,7 +14,7 @@ pub struct ErrorLogger; impl TraceLogger { pub fn init() -> Result<(), SetLoggerError> { - log::set_logger(&InfoLogger)?; + log::set_logger(&TraceLogger)?; log::set_max_level(LevelFilter::Trace); Ok(()) } @@ -37,7 +37,7 @@ impl log::Log for TraceLogger { impl WarnLogger { pub fn init() -> Result<(), SetLoggerError> { - log::set_logger(&InfoLogger)?; + log::set_logger(&WarnLogger)?; log::set_max_level(LevelFilter::Warn); Ok(()) } @@ -60,7 +60,7 @@ impl log::Log for WarnLogger { impl ErrorLogger { pub fn init() -> Result<(), SetLoggerError> { - log::set_logger(&InfoLogger)?; + log::set_logger(&ErrorLogger)?; log::set_max_level(LevelFilter::Error); Ok(()) } From 5f235ec199f6a0b934f620fec0d2c481ba1d457a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Ver=C5=A1i=C4=87?= Date: Wed, 19 Jul 2023 10:44:41 +0200 Subject: [PATCH 15/15] generate bindings for non-public extern items MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marin Veršić --- src/bindgen/ir/global.rs | 10 ++- src/bindgen/parser.rs | 96 +++++++--------------- src/bindgen/utilities.rs | 20 ++++- tests/expectations/non_pub_extern.c | 12 +++ tests/expectations/non_pub_extern.compat.c | 20 +++++ tests/expectations/non_pub_extern.cpp | 17 ++++ tests/expectations/non_pub_extern.pyx | 15 ++++ tests/rust/non_pub_extern.rs | 13 +++ 8 files changed, 130 insertions(+), 73 deletions(-) create mode 100644 tests/expectations/non_pub_extern.c create mode 100644 tests/expectations/non_pub_extern.compat.c create mode 100644 tests/expectations/non_pub_extern.cpp create mode 100644 tests/expectations/non_pub_extern.pyx create mode 100644 tests/rust/non_pub_extern.rs diff --git a/src/bindgen/ir/global.rs b/src/bindgen/ir/global.rs index 47c996055..82a1756e1 100644 --- a/src/bindgen/ir/global.rs +++ b/src/bindgen/ir/global.rs @@ -4,8 +4,6 @@ use std::io::Write; -use syn::ext::IdentExt; - use crate::bindgen::cdecl; use crate::bindgen::config::Config; use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver; @@ -26,7 +24,11 @@ pub struct Static { } impl Static { - pub fn load(item: &syn::ItemStatic, mod_cfg: Option<&Cfg>) -> Result { + pub fn load( + path: Path, + item: &syn::ItemStatic, + mod_cfg: Option<&Cfg>, + ) -> Result { let ty = Type::load(&item.ty)?; if ty.is_none() { @@ -34,7 +36,7 @@ impl Static { } Ok(Static::new( - Path::new(item.ident.unraw().to_string()), + path, ty.unwrap(), item.mutability.is_some(), Cfg::append(mod_cfg, Cfg::load(&item.attrs)), diff --git a/src/bindgen/parser.rs b/src/bindgen/parser.rs index e8add192a..9cd6a54a4 100644 --- a/src/bindgen/parser.rs +++ b/src/bindgen/parser.rs @@ -18,7 +18,7 @@ use crate::bindgen::ir::{ AnnotationSet, Cfg, Constant, Documentation, Enum, Function, GenericParam, GenericParams, ItemMap, OpaqueItem, Path, Static, Struct, Type, Typedef, Union, }; -use crate::bindgen::utilities::{SynAbiHelpers, SynAttributeHelpers, SynItemFnHelpers}; +use crate::bindgen::utilities::{SynAbiHelpers, SynAttributeHelpers, SynItemHelpers}; const STD_CRATES: &[&str] = &[ "std", @@ -643,7 +643,6 @@ impl Parse { item, Some(self_type), &item.sig, - &item.vis, &item.attrs, ) } @@ -665,7 +664,6 @@ impl Parse { item, None, &item.sig, - &item.vis, &item.attrs, ); } @@ -677,10 +675,9 @@ impl Parse { binding_crate_name: &str, crate_name: &str, mod_cfg: Option<&Cfg>, - named_symbol: &dyn SynItemFnHelpers, + named_symbol: &dyn SynItemHelpers, self_type: Option<&Path>, sig: &syn::Signature, - vis: &syn::Visibility, attrs: &[syn::Attribute], ) { if !config @@ -707,53 +704,29 @@ impl Parse { let is_extern_c = sig.abi.is_omitted() || sig.abi.is_c(); let exported_name = named_symbol.exported_name(); - if let syn::Visibility::Public(_) = vis { - match (is_extern_c, exported_name) { - (true, Some(exported_name)) => { - let path = Path::new(exported_name); - match Function::load(path, self_type, sig, false, attrs, mod_cfg) { - Ok(func) => { - info!("Take {}.", loggable_item_name()); - self.functions.push(func); - } - Err(msg) => { - error!("Cannot use fn {} ({}).", loggable_item_name(), msg); - } + match (is_extern_c, exported_name) { + (true, Some(exported_name)) => { + let path = Path::new(exported_name); + match Function::load(path, self_type, sig, false, attrs, mod_cfg) { + Ok(func) => { + info!("Take {}.", loggable_item_name()); + self.functions.push(func); + } + Err(msg) => { + error!("Cannot use fn {} ({}).", loggable_item_name(), msg); } } - (true, None) => { - warn!( - "Skipping {} - (not `no_mangle`, and has no `export_name` attribute)", - loggable_item_name() - ); - } - (false, Some(_exported_name)) => { - warn!("Skipping {} - (not `extern \"C\"`", loggable_item_name()); - } - (false, None) => {} } - } else { - match (is_extern_c, exported_name) { - (true, Some(..)) => { - warn!( - "Skipping {} - (not `pub` but is `extern \"C\"` and `no_mangle`)", - loggable_item_name() - ); - } - (true, None) => { - warn!( - "Skipping {} - (not `pub` but is `extern \"C\"`)", - loggable_item_name() - ); - } - (false, Some(..)) => { - warn!( - "Skipping {} - (not `pub` but is `no_mangle`)", - loggable_item_name() - ); - } - (false, None) => {} + (true, None) => { + warn!( + "Skipping {} - (not `no_mangle`, and has no `export_name` attribute)", + loggable_item_name() + ); } + (false, Some(_exported_name)) => { + warn!("Skipping {} - (not `extern \"C\"`", loggable_item_name()); + } + (false, None) => {} } } @@ -892,27 +865,18 @@ impl Parse { return; } - if let syn::Visibility::Public(_) = item.vis { - if item.is_no_mangle() { - match Static::load(item, mod_cfg) { - Ok(constant) => { - info!("Take {}::{}.", crate_name, &item.ident); - - self.globals.try_insert(constant); - } - Err(msg) => { - warn!("Skip {}::{} - ({})", crate_name, &item.ident, msg); - } + if let Some(exported_name) = item.exported_name() { + let path = Path::new(exported_name); + match Static::load(path, item, mod_cfg) { + Ok(constant) => { + info!("Take {}::{}.", crate_name, &item.ident); + self.globals.try_insert(constant); + } + Err(msg) => { + warn!("Skip {}::{} - ({})", crate_name, &item.ident, msg); } } - } - - // TODO - if let syn::Visibility::Public(_) = item.vis { } else { - warn!("Skip {}::{} - (not `pub`).", crate_name, &item.ident); - } - if !item.is_no_mangle() { warn!("Skip {}::{} - (not `no_mangle`).", crate_name, &item.ident); } } diff --git a/src/bindgen/utilities.rs b/src/bindgen/utilities.rs index ae3ee6032..bea1d99a4 100644 --- a/src/bindgen/utilities.rs +++ b/src/bindgen/utilities.rs @@ -30,11 +30,11 @@ where } } -pub trait SynItemFnHelpers: SynAttributeHelpers { +pub trait SynItemHelpers: SynAttributeHelpers { fn exported_name(&self) -> Option; } -impl SynItemFnHelpers for syn::ItemFn { +impl SynItemHelpers for syn::ItemFn { fn exported_name(&self) -> Option { self.attrs .attr_name_value_lookup("export_name") @@ -48,7 +48,7 @@ impl SynItemFnHelpers for syn::ItemFn { } } -impl SynItemFnHelpers for syn::ImplItemMethod { +impl SynItemHelpers for syn::ImplItemMethod { fn exported_name(&self) -> Option { self.attrs .attr_name_value_lookup("export_name") @@ -62,6 +62,20 @@ impl SynItemFnHelpers for syn::ImplItemMethod { } } +impl SynItemHelpers for syn::ItemStatic { + fn exported_name(&self) -> Option { + self.attrs + .attr_name_value_lookup("export_name") + .or_else(|| { + if self.is_no_mangle() { + Some(self.ident.unraw().to_string()) + } else { + None + } + }) + } +} + /// Returns whether this attribute causes us to skip at item. This basically /// checks for `#[cfg(test)]`, `#[test]`, `/// cbindgen::ignore` and /// variations thereof. diff --git a/tests/expectations/non_pub_extern.c b/tests/expectations/non_pub_extern.c new file mode 100644 index 000000000..779000d65 --- /dev/null +++ b/tests/expectations/non_pub_extern.c @@ -0,0 +1,12 @@ +#include +#include +#include +#include + +extern const uint32_t FIRST; + +extern const uint32_t RENAMED; + +void first(void); + +void renamed(void); diff --git a/tests/expectations/non_pub_extern.compat.c b/tests/expectations/non_pub_extern.compat.c new file mode 100644 index 000000000..549345195 --- /dev/null +++ b/tests/expectations/non_pub_extern.compat.c @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +extern const uint32_t FIRST; + +extern const uint32_t RENAMED; + +void first(void); + +void renamed(void); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/non_pub_extern.cpp b/tests/expectations/non_pub_extern.cpp new file mode 100644 index 000000000..c931fbc39 --- /dev/null +++ b/tests/expectations/non_pub_extern.cpp @@ -0,0 +1,17 @@ +#include +#include +#include +#include +#include + +extern "C" { + +extern const uint32_t FIRST; + +extern const uint32_t RENAMED; + +void first(); + +void renamed(); + +} // extern "C" diff --git a/tests/expectations/non_pub_extern.pyx b/tests/expectations/non_pub_extern.pyx new file mode 100644 index 000000000..112841ffd --- /dev/null +++ b/tests/expectations/non_pub_extern.pyx @@ -0,0 +1,15 @@ +from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t +from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t +cdef extern from *: + ctypedef bint bool + ctypedef struct va_list + +cdef extern from *: + + extern const uint32_t FIRST; + + extern const uint32_t RENAMED; + + void first(); + + void renamed(); diff --git a/tests/rust/non_pub_extern.rs b/tests/rust/non_pub_extern.rs new file mode 100644 index 000000000..4174324d7 --- /dev/null +++ b/tests/rust/non_pub_extern.rs @@ -0,0 +1,13 @@ +#[no_mangle] +static FIRST: u32 = 10; + +#[export_name = "RENAMED"] +static SECOND: u32 = 42; + +#[no_mangle] +extern "C" fn first() +{ } + +#[export_name = "renamed"] +extern fn second() +{ }