From 6e2397f579407590af0ee7bba0d79ba466f7f02f Mon Sep 17 00:00:00 2001 From: Jeff Waugh Date: Tue, 8 Nov 2016 19:55:49 +1100 Subject: [PATCH 1/2] Add required Builder methods --- src/lib.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 8914d72d42..db87eb228d 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -222,6 +222,30 @@ impl Builder { self } + /// Emit Clang AST. + pub fn emit_clang_ast(mut self) -> Builder { + self.options.emit_ast = true; + self + } + + /// Enable C++ namespaces. + pub fn enable_cxx_namespaces(mut self) -> Builder { + self.options.enable_cxx_namespaces = true; + self + } + + /// Ignore functions. + pub fn ignore_functions(mut self) -> Builder { + self.options.ignore_functions = true; + self + } + + /// Ignore methods. + pub fn ignore_methods(mut self) -> Builder { + self.options.ignore_methods = true; + self + } + /// Avoid generating any unstable Rust in the generated bindings. pub fn no_unstable_rust(mut self) -> Builder { self.options.unstable_rust = false; From dc96c1fe4573f3656ce006d451d57cff378a673a Mon Sep 17 00:00:00 2001 From: Jeff Waugh Date: Tue, 8 Nov 2016 16:50:30 +1100 Subject: [PATCH 2/2] Port to clap command line parsing --- Cargo.toml | 1 + src/bin/bindgen.rs | 266 +++------------------------------------------ src/bin/options.rs | 257 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 276 insertions(+), 248 deletions(-) create mode 100644 src/bin/options.rs diff --git a/Cargo.toml b/Cargo.toml index 97bcb82f93..920902268b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ quasi_codegen = "0.20" [dependencies] cfg-if = "0.1.0" clang-sys = "0.8.0" +clap = "2" lazy_static = "0.1.*" libc = "0.2" log = "0.3" diff --git a/src/bin/bindgen.rs b/src/bin/bindgen.rs index bdb3b2920d..55d3fdde91 100755 --- a/src/bin/bindgen.rs +++ b/src/bin/bindgen.rs @@ -6,248 +6,14 @@ extern crate env_logger; #[macro_use] extern crate log; extern crate clang_sys; +extern crate clap; extern crate rustc_serialize; -use bindgen::{BindgenOptions, Bindings, LinkType, clang_version}; -use std::default::Default; +use bindgen::clang_version; use std::env; -use std::fs; -use std::io; -use std::path; -use std::process; -const USAGE: &'static str = " -Usage: - bindgen [options] \ - [--link=...] \ - [--static-link=...] \ - [--framework-link=...] \ - [--raw-line=...] \ - [--opaque-type=...] \ - [--blacklist-type=...] \ - [--whitelist-type=...] \ - [--whitelist-function=...] \ - [--whitelist-var=...] \ - [--bitfield-enum=...] \ - \ - [-- ...] - - bindgen (-h | --help) - -Options: - -h, --help Display this help message. - - -l=, --link= Link to a dynamic library, can be provided - multiple times. - - --static-link= Link to a static library, can be provided - multiple times. - - --framework-link= Link to a framework. - - -o= Write bindings to - (defaults to stdout) - - --builtins Output bindings for builtin definitions (for - example __builtin_va_list) - - --ignore-functions Don't generate bindings for functions and - methods. This is useful when you only care - about struct layouts. - - --ignore-methods Avoid generating all kind of methods. - - --enable-cxx-namespaces Enable support for C++ namespaces. - - --emit-clang-ast Output the ast (for debugging purposes) - - --use-msvc-mangling Handle MSVC C++ ABI mangling; requires that - target be set to (i686|x86_64)-pc-win32 - - --no-convert-floats Don't convert floats automatically to f32/f64. - - --raw-line= Add a raw line at the beginning of the output. - - --no-unstable-rust Avoid generating unstable rust. - - --use-core Use built-in types from core instead of std. - - --ctypes-prefix= Use the given prefix before the raw types - instead of ::std::os::raw::. - - --opaque-type= Mark a type as opaque. - - --blacklist-type= Mark a type as hidden. - - --whitelist-type= Whitelist the type. If this set or any other - of the whitelisting sets is not empty, then - all the non-whitelisted types (or dependant) - won't be generated. - - --whitelist-function= Whitelist all the free-standing functions - matching . Same behavior on emptyness - than the type whitelisting. - - --whitelist-var= Whitelist all the free-standing variables - matching . Same behavior on emptyness - than the type whitelisting. - - --bitfield-enum= Mark any enum whose name matches as a - set of bitfield flags instead of an - enumeration. - - --dummy-uses= For testing purposes, generate a C/C++ file - containing dummy uses of all types defined in - the input header. - - Options other than stated above are passed - directly through to clang. -"; - -// FIXME(emilio): Replace this with docopt if/when they fix their exponential -// algorithm for argument parsing. -// -// FIXME(fitzgen): Switch from `BindgenOptions` to the non-deprecated `Builder`. -#[allow(deprecated)] -fn parse_args_or_exit(args: Vec) -> (BindgenOptions, Box) { - let mut options = BindgenOptions::default(); - let mut dest_file = None; - let mut source_file = None; - - let mut iter = args.into_iter().skip(1); - loop { - let next = match iter.next() { - Some(arg) => arg, - _ => break, - }; - - match &*next { - "-h" | "--help" => { - println!("{}", USAGE); - process::exit(0); - } - "-l" | "--link" => { - let lib = iter.next().expect("--link needs an argument"); - options.links.push((lib, LinkType::Default)); - } - "--static-link" => { - let lib = iter.next().expect("--static-link needs an argument"); - options.links.push((lib, LinkType::Static)); - } - "--framework-link" => { - let lib = iter.next() - .expect("--framework-link needs an argument"); - options.links.push((lib, LinkType::Framework)); - } - "--raw-line" => { - let line = iter.next().expect("--raw-line needs an argument"); - options.raw_lines.push(line); - } - "--opaque-type" => { - let ty_canonical_name = iter.next() - .expect("--opaque-type expects a type"); - options.opaque_types.insert(ty_canonical_name); - } - "--blacklist-type" => { - let ty_canonical_name = iter.next() - .expect("--blacklist-type expects a type"); - options.hidden_types.insert(ty_canonical_name); - } - "--whitelist-type" => { - let ty_pat = iter.next() - .expect("--whitelist-type expects a type pattern"); - options.whitelisted_types.insert(&ty_pat); - } - "--whitelist-function" => { - let function_pat = iter.next() - .expect("--whitelist-function expects a pattern"); - options.whitelisted_functions.insert(&function_pat); - } - "--whitelist-var" => { - let var_pat = iter.next() - .expect("--whitelist-var expects a pattern"); - options.whitelisted_vars.insert(&var_pat); - } - "--bitfield-enum" => { - let enum_pat = iter.next() - .expect("--bitfield-enum expects a pattern"); - options.bitfield_enums.insert(&enum_pat); - } - "--" => { - while let Some(clang_arg) = iter.next() { - options.clang_args.push(clang_arg); - } - } - "--output" | "-o" => { - let out_name = iter.next().expect("-o expects a file name"); - dest_file = Some(out_name); - } - "--builtins" => { - options.builtins = true; - } - "--ignore-functions" => { - options.ignore_functions = true; - } - "--ignore-methods" => { - options.ignore_methods = true; - } - "--enable-cxx-namespaces" => { - options.enable_cxx_namespaces = true; - } - "--no-unstable-rust" => { - options.unstable_rust = false; - } - "--use-core" => { - options.use_core = true; - } - "--ctypes-prefix" => { - let prefix = iter.next() - .expect("--ctypes-prefix expects a prefix after it"); - options.ctypes_prefix = Some(prefix); - } - "--emit-clang-ast" => { - options.emit_ast = true; - } - "--no-convert-floats" => { - options.convert_floats = false; - } - "--use-msvc-mangling" => { - options.msvc_mangling = true; - } - "--dummy-uses" => { - let dummy_path = iter.next() - .expect("--dummy-uses expects a file path"); - options.dummy_uses = Some(dummy_path); - } - other if source_file.is_none() => { - source_file = Some(other.into()); - } - other => { - panic!("Unknown option: \"{}\"", other); - } - } - } - - if let Some(source_file) = source_file.take() { - options.clang_args.push(source_file); - options.input_header = options.clang_args.last().cloned(); - } else { - options.input_header = options.clang_args - .iter() - .find(|arg| arg.ends_with(".h") || arg.ends_with(".hpp")) - .cloned(); - } - - let out = if let Some(ref path_name) = dest_file { - let path = path::Path::new(path_name); - let file = fs::File::create(path).expect("Opening out file failed"); - Box::new(io::BufWriter::new(file)) as Box - } else { - Box::new(io::BufWriter::new(io::stdout())) as Box - }; - - (options, out) -} +mod options; +use options::builder_from_flags; pub fn main() { log::set_logger(|max_log_level| { @@ -304,14 +70,18 @@ pub fn main() { } } - let (options, out) = parse_args_or_exit(bind_args); - - let mut bindings = Bindings::generate(options, None) - .expect("Unable to generate bindings"); - - bindings.write_dummy_uses() - .expect("Unable to write dummy uses to file."); - - bindings.write(out) - .expect("Unable to write bindings to file."); + match builder_from_flags(env::args()) { + Ok((builder, output)) => { + let mut bindings = builder.generate() + .expect("Unable to generate bindings"); + bindings.write(output) + .expect("Unable to write output"); + bindings.write_dummy_uses() + .expect("Unable to write dummy uses to file."); + } + Err(error) => { + println!("{}", error); + std::process::exit(1); + } + }; } diff --git a/src/bin/options.rs b/src/bin/options.rs new file mode 100644 index 0000000000..9eff423244 --- /dev/null +++ b/src/bin/options.rs @@ -0,0 +1,257 @@ +use std::fs::File; +use std::io::{self, Error, ErrorKind}; + +use clap::{App, Arg}; + +use bindgen::{builder, Builder}; + +/// Construct a new [`Builder`](./struct.Builder.html) from command line flags. +pub fn builder_from_flags(args: I) -> Result<(Builder, Box), io::Error> + where I: Iterator +{ + let matches = App::new("bindgen") + .version(env!("CARGO_PKG_VERSION")) + .about("Generates Rust bindings from C/C++ headers.") + .usage("bindgen [FLAGS] [OPTIONS]
-- ...") + .args(&[ + Arg::with_name("header") + .help("C or C++ header file") + .required(true), + Arg::with_name("bitfield-enum") + .long("bitfield-enum") + .help("Mark any enum whose name matches as a set of bitfield flags instead of an enumeration.") + .value_name("regex") + .takes_value(true) + .multiple(true) + .number_of_values(1), + Arg::with_name("blacklist-type") + .long("blacklist-type") + .help("Mark a type as hidden.") + .value_name("type") + .takes_value(true) + .multiple(true) + .number_of_values(1), + Arg::with_name("builtins") + .long("builtins") + .help("Output bindings for builtin definitions, e.g. __builtin_va_list."), + Arg::with_name("ctypes-prefix") + .long("ctypes-prefix") + .help("Use the given prefix before raw types instead of ::std::os::raw.") + .value_name("prefix") + .takes_value(true), + // All positional arguments after the end of options marker, `--` + Arg::with_name("clang-args") + .multiple(true), + Arg::with_name("dummy-uses") + .long("dummy-uses") + .help("For testing purposes, generate a C/C++ file containing dummy uses of all types defined in the input header.") + .takes_value(true), + Arg::with_name("emit-clang-ast") + .long("emit-clang-ast") + .help("Output the Clang AST for debugging purposes."), + Arg::with_name("enable-cxx-namespaces") + .long("enable-cxx-namespaces") + .help("Enable support for C++ namespaces."), + Arg::with_name("framework") + .long("framework-link") + .help("Link to framework.") + .takes_value(true) + .multiple(true) + .number_of_values(1), + Arg::with_name("ignore-functions") + .long("ignore-functions") + .help("Do not generate bindings for functions or methods. This is useful when you only care about struct layouts."), + Arg::with_name("ignore-methods") + .long("ignore-methods") + .help("Do not generate bindings for methods."), + Arg::with_name("dynamic") + .short("l") + .long("link") + .help("Link to dynamic library.") + .takes_value(true) + .multiple(true) + .number_of_values(1), + Arg::with_name("no-convert-floats") + .long("no-convert-floats") + .help("Don't automatically convert floats to f32/f64."), + Arg::with_name("no-unstable-rust") + .long("no-unstable-rust") + .help("Do not generate unstable Rust code.") + .multiple(true), // FIXME: Pass legacy test suite + Arg::with_name("opaque-type") + .long("opaque-type") + .help("Mark a type as opaque.") + .value_name("type") + .takes_value(true) + .multiple(true) + .number_of_values(1), + Arg::with_name("output") + .short("o") + .long("output") + .help("Write Rust bindings to .") + .takes_value(true), + Arg::with_name("raw-line") + .long("raw-line") + .help("Add a raw line of Rust code at the beginning of output.") + .takes_value(true) + .multiple(true) + .number_of_values(1), + Arg::with_name("static") + .long("static-link") + .help("Link to static library.") + .takes_value(true) + .multiple(true) + .number_of_values(1), + Arg::with_name("use-core") + .long("use-core") + .help("Use types from Rust core instead of std."), + Arg::with_name("use-msvc-mangling") + .long("use-msvc-mangling") + .help("MSVC C++ ABI mangling. DEPRECATED: Has no effect."), + Arg::with_name("whitelist-function") + .long("whitelist-function") + .help("Whitelist all the free-standing functions matching . Other non-whitelisted functions will not be generated.") + .value_name("regex") + .takes_value(true) + .multiple(true) + .number_of_values(1), + Arg::with_name("whitelist-type") + .long("whitelist-type") + .help("Whitelist the type. Other non-whitelisted types will not be generated.") + .value_name("type") + .takes_value(true) + .multiple(true) + .number_of_values(1), + Arg::with_name("whitelist-var") + .long("whitelist-var") + .help("Whitelist all the free-standing variables matching . Other non-whitelisted variables will not be generated.") + .value_name("regex") + .takes_value(true) + .multiple(true) + .number_of_values(1), + ]) // .args() + .get_matches_from(args); + + let mut builder = builder(); + + if let Some(header) = matches.value_of("header") { + builder = builder.header(header); + } else { + return Err(Error::new(ErrorKind::Other, "Header not found")); + } + + if let Some(bitfields) = matches.values_of("bitfield-enum") { + for regex in bitfields { + builder = builder.bitfield_enum(regex); + } + } + + if let Some(hidden_types) = matches.values_of("blacklist-type") { + for ty in hidden_types { + builder = builder.hide_type(ty); + } + } + + if matches.is_present("builtins") { + builder = builder.emit_builtins(); + } + + if let Some(prefix) = matches.value_of("ctypes-prefix") { + builder = builder.ctypes_prefix(prefix); + } + + if let Some(dummy) = matches.value_of("dummy-uses") { + builder = builder.dummy_uses(dummy); + } + + if let Some(links) = matches.values_of("dynamic") { + for library in links { + builder = builder.link(library); + } + } + + if matches.is_present("emit-clang-ast") { + builder = builder.emit_clang_ast(); + } + + if matches.is_present("enable-cxx-namespaces") { + builder = builder.enable_cxx_namespaces(); + } + + if let Some(links) = matches.values_of("framework") { + for framework in links { + builder = builder.link_framework(framework); + } + } + + if matches.is_present("ignore-functions") { + builder = builder.ignore_functions(); + } + + if matches.is_present("ignore-methods") { + builder = builder.ignore_methods(); + } + + if matches.is_present("no-unstable-rust") { + builder = builder.no_unstable_rust(); + } + + if matches.is_present("no-convert-floats") { + builder = builder.no_convert_floats(); + } + + if let Some(opaque_types) = matches.values_of("opaque-type") { + for ty in opaque_types { + builder = builder.opaque_type(ty); + } + } + + if let Some(lines) = matches.values_of("raw-line") { + for line in lines { + builder = builder.raw_line(line); + } + } + + if let Some(links) = matches.values_of("static") { + for library in links { + builder = builder.link_static(library); + } + } + + if matches.is_present("use-core") { + builder = builder.use_core(); + } + + if let Some(whitelist) = matches.values_of("whitelist-function") { + for regex in whitelist { + builder = builder.whitelisted_function(regex); + } + } + + if let Some(whitelist) = matches.values_of("whitelist-type") { + for regex in whitelist { + builder = builder.whitelisted_type(regex); + } + } + + if let Some(whitelist) = matches.values_of("whitelist-var") { + for regex in whitelist { + builder = builder.whitelisted_var(regex); + } + } + + if let Some(args) = matches.values_of("clang-args") { + for arg in args { + builder = builder.clang_arg(arg); + } + } + + let output = if let Some(path) = matches.value_of("output") { + let file = try!(File::create(path)); + Box::new(io::BufWriter::new(file)) as Box + } else { + Box::new(io::BufWriter::new(io::stdout())) as Box + }; + + Ok((builder, output)) +}