Skip to content

Commit 0f9dcdb

Browse files
committed
Move CLI options to bindgen
1 parent 1c834b7 commit 0f9dcdb

File tree

10 files changed

+52
-46
lines changed

10 files changed

+52
-46
lines changed

Cargo.lock

+2-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ default-members = [
1616

1717
# Dependencies shared between crates
1818
[workspace.dependencies]
19-
clap = { version = "4", features = ["derive"] }
20-
clap_complete = "4"
2119
shlex = "1"
2220
syn = "2.0"
2321
proc-macro2 = { version = "1", default-features = false }

bindgen-cli/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ bindgen = { path = "../bindgen", version = "=0.70.1", default-features = false,
2424
env_logger = { version = "0.10.0", optional = true }
2525
log = { version = "0.4", optional = true }
2626

27-
clap.workspace = true
28-
clap_complete.workspace = true
2927
proc-macro2.workspace = true
3028
shlex.workspace = true
3129

bindgen-cli/main.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::env;
22

3-
mod options;
4-
use crate::options::builder_from_flags;
3+
use bindgen::builder_from_flags;
54

65
#[cfg(feature = "logging")]
76
fn clang_version_check() {

bindgen-tests/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ prettyplease = { version = "0.2.7", features = ["verbatim"] }
1111
similar = { version = "2.2.1", features = ["inline"] }
1212
tempfile = "3"
1313

14-
clap.workspace = true
15-
clap_complete.workspace = true
1614
proc-macro2.workspace = true
1715
shlex.workspace = true
1816
syn.workspace = true

bindgen-tests/tests/tests.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ use std::fs;
77
use std::io::{BufRead, BufReader, Error, ErrorKind, Read, Write};
88
use std::path::{Path, PathBuf};
99

10-
use crate::options::builder_from_flags;
11-
12-
#[path = "../../bindgen-cli/options.rs"]
13-
mod options;
10+
use bindgen::builder_from_flags;
1411

1512
mod parse_callbacks;
1613

@@ -709,8 +706,7 @@ fn build_flags_output_helper(builder: &bindgen::Builder) {
709706
println!("{}", flags_str);
710707

711708
let (builder, _output, _verbose) =
712-
crate::options::builder_from_flags(command_line_flags.into_iter())
713-
.unwrap();
709+
builder_from_flags(command_line_flags.into_iter()).unwrap();
714710
builder.generate().expect("failed to generate bindings");
715711
}
716712

bindgen/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ annotate-snippets = { version = "0.11.4", optional = true }
2929
bitflags = "2.2.1"
3030
cexpr = "0.6"
3131
clang-sys = { version = "1", features = ["clang_11_0"] }
32+
clap = { version = "4", features = ["derive"], optional = true }
33+
clap_complete = { version = "4", optional = true}
3234
itertools = { version = ">=0.10,<0.14", default-features = false }
3335
log = { version = "0.4", optional = true }
3436
prettyplease = { version = "0.2.7", optional = true, features = ["verbatim"] }
@@ -52,7 +54,7 @@ experimental = ["dep:annotate-snippets"]
5254
## The following features are for internal use and they shouldn't be used if
5355
## you're not hacking on bindgen
5456
# Features used by `bindgen-cli`
55-
__cli = []
57+
__cli = ["dep:clap", "dep:clap_complete"]
5658
# Features used for CI testing
5759
__testing_only_extra_assertions = []
5860
__testing_only_libclang_9 = []

bindgen/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ mod regex_set;
5050
pub use codegen::{
5151
AliasVariation, EnumVariation, MacroTypeVariation, NonCopyUnionStyle,
5252
};
53-
#[cfg(feature = "__cli")]
54-
pub use features::RUST_TARGET_STRINGS;
5553
pub use features::{RustTarget, LATEST_STABLE_RUST};
5654
pub use ir::annotations::FieldVisibilityKind;
5755
pub use ir::function::Abi;
56+
#[cfg(feature = "__cli")]
57+
pub use options::cli::builder_from_flags;
5858
pub use regex_set::RegexSet;
5959

6060
use codegen::CodegenError;

bindgen-cli/options.rs renamed to bindgen/options/cli.rs

+40-25
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
use bindgen::callbacks::TypeKind;
2-
use bindgen::{
3-
builder, Abi, AliasVariation, Builder, CodegenConfig, EnumVariation,
1+
use crate::{
2+
builder,
3+
callbacks::{
4+
AttributeInfo, DeriveInfo, ItemInfo, ParseCallbacks, TypeKind,
5+
},
6+
features::RUST_TARGET_STRINGS,
7+
Abi, AliasVariation, Builder, CodegenConfig, EnumVariation,
48
FieldVisibilityKind, Formatter, MacroTypeVariation, NonCopyUnionStyle,
5-
RegexSet, RustTarget, DEFAULT_ANON_FIELDS_PREFIX, RUST_TARGET_STRINGS,
9+
RegexSet, RustTarget, DEFAULT_ANON_FIELDS_PREFIX,
10+
};
11+
use clap::{
12+
error::{Error, ErrorKind},
13+
CommandFactory, Parser,
614
};
7-
use clap::error::{Error, ErrorKind};
8-
use clap::{CommandFactory, Parser};
915
use proc_macro2::TokenStream;
1016
use std::fs::File;
1117
use std::io;
@@ -491,6 +497,7 @@ struct BindgenCommand {
491497
#[arg(long, value_name = "VISIBILITY")]
492498
default_visibility: Option<FieldVisibilityKind>,
493499
/// Whether to emit diagnostics or not.
500+
#[cfg(feature = "experimental")]
494501
#[arg(long, requires = "experimental")]
495502
emit_diagnostics: bool,
496503
/// Generates completions for the specified SHELL, sends them to `stdout` and exits.
@@ -633,6 +640,7 @@ where
633640
wrap_static_fns_path,
634641
wrap_static_fns_suffix,
635642
default_visibility,
643+
#[cfg(feature = "experimental")]
636644
emit_diagnostics,
637645
generate_shell_completions,
638646
experimental: _,
@@ -657,7 +665,7 @@ where
657665
option_env!("CARGO_PKG_VERSION").unwrap_or("unknown")
658666
);
659667
if verbose {
660-
println!("Clang: {}", bindgen::clang_version().full);
668+
println!("Clang: {}", crate::clang_version().full);
661669
}
662670
std::process::exit(0);
663671
}
@@ -1046,10 +1054,10 @@ where
10461054
prefix: String,
10471055
}
10481056

1049-
impl bindgen::callbacks::ParseCallbacks for PrefixLinkNameCallback {
1057+
impl ParseCallbacks for PrefixLinkNameCallback {
10501058
fn generated_link_name_override(
10511059
&self,
1052-
item_info: bindgen::callbacks::ItemInfo<'_>,
1060+
item_info: ItemInfo<'_>,
10531061
) -> Option<String> {
10541062
let mut prefix = self.prefix.clone();
10551063
prefix.push_str(item_info.name);
@@ -1114,10 +1122,10 @@ where
11141122
struct CustomDeriveCallback {
11151123
derives: Vec<String>,
11161124
kind: Option<TypeKind>,
1117-
regex_set: bindgen::RegexSet,
1125+
regex_set: RegexSet,
11181126
}
11191127

1120-
impl bindgen::callbacks::ParseCallbacks for CustomDeriveCallback {
1128+
impl ParseCallbacks for CustomDeriveCallback {
11211129
fn cli_args(&self) -> Vec<String> {
11221130
let mut args = vec![];
11231131

@@ -1140,10 +1148,7 @@ where
11401148
args
11411149
}
11421150

1143-
fn add_derives(
1144-
&self,
1145-
info: &bindgen::callbacks::DeriveInfo<'_>,
1146-
) -> Vec<String> {
1151+
fn add_derives(&self, info: &DeriveInfo<'_>) -> Vec<String> {
11471152
if self.kind.map(|kind| kind == info.kind).unwrap_or(true) &&
11481153
self.regex_set.matches(info.name)
11491154
{
@@ -1153,7 +1158,7 @@ where
11531158
}
11541159
}
11551160

1156-
for (custom_derives, kind, name) in [
1161+
for (custom_derives, kind, _name) in [
11571162
(with_derive_custom, None, "--with-derive-custom"),
11581163
(
11591164
with_derive_custom_struct,
@@ -1171,11 +1176,17 @@ where
11711176
"--with-derive-custom-union",
11721177
),
11731178
] {
1174-
let name = emit_diagnostics.then_some(name);
1179+
#[cfg(feature = "experimental")]
1180+
let name = emit_diagnostics.then_some(_name);
1181+
11751182
for (derives, regex) in custom_derives {
11761183
let mut regex_set = RegexSet::new();
11771184
regex_set.insert(regex);
1185+
1186+
#[cfg(feature = "experimental")]
11781187
regex_set.build_with_diagnostics(false, name);
1188+
#[cfg(not(feature = "experimental"))]
1189+
regex_set.build(false);
11791190

11801191
builder = builder.parse_callbacks(Box::new(CustomDeriveCallback {
11811192
derives,
@@ -1189,10 +1200,10 @@ where
11891200
struct CustomAttributeCallback {
11901201
attributes: Vec<String>,
11911202
kind: Option<TypeKind>,
1192-
regex_set: bindgen::RegexSet,
1203+
regex_set: RegexSet,
11931204
}
11941205

1195-
impl bindgen::callbacks::ParseCallbacks for CustomAttributeCallback {
1206+
impl ParseCallbacks for CustomAttributeCallback {
11961207
fn cli_args(&self) -> Vec<String> {
11971208
let mut args = vec![];
11981209

@@ -1215,10 +1226,7 @@ where
12151226
args
12161227
}
12171228

1218-
fn add_attributes(
1219-
&self,
1220-
info: &bindgen::callbacks::AttributeInfo<'_>,
1221-
) -> Vec<String> {
1229+
fn add_attributes(&self, info: &AttributeInfo<'_>) -> Vec<String> {
12221230
if self.kind.map(|kind| kind == info.kind).unwrap_or(true) &&
12231231
self.regex_set.matches(info.name)
12241232
{
@@ -1228,7 +1236,7 @@ where
12281236
}
12291237
}
12301238

1231-
for (custom_attributes, kind, name) in [
1239+
for (custom_attributes, kind, _name) in [
12321240
(with_attribute_custom, None, "--with-attribute-custom"),
12331241
(
12341242
with_attribute_custom_struct,
@@ -1246,11 +1254,17 @@ where
12461254
"--with-attribute-custom-union",
12471255
),
12481256
] {
1249-
let name = emit_diagnostics.then_some(name);
1257+
#[cfg(feature = "experimental")]
1258+
let name = emit_diagnostics.then_some(_name);
1259+
12501260
for (attributes, regex) in custom_attributes {
12511261
let mut regex_set = RegexSet::new();
12521262
regex_set.insert(regex);
1263+
1264+
#[cfg(feature = "experimental")]
12531265
regex_set.build_with_diagnostics(false, name);
1266+
#[cfg(not(feature = "experimental"))]
1267+
regex_set.build(false);
12541268

12551269
builder =
12561270
builder.parse_callbacks(Box::new(CustomAttributeCallback {
@@ -1277,6 +1291,7 @@ where
12771291
builder = builder.default_visibility(visibility);
12781292
}
12791293

1294+
#[cfg(feature = "experimental")]
12801295
if emit_diagnostics {
12811296
builder = builder.emit_diagnostics();
12821297
}

bindgen/options/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#[macro_use]
55
mod helpers;
66
mod as_args;
7+
#[cfg(feature = "__cli")]
8+
pub(crate) mod cli;
79

810
use crate::callbacks::ParseCallbacks;
911
use crate::codegen::{

0 commit comments

Comments
 (0)