Skip to content

Commit

Permalink
add the experimental feature
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdrz committed Jan 24, 2023
1 parent ad0f161 commit 101cde0
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion bindgen-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ path = "main.rs"
name = "bindgen"

[dependencies]
bindgen = { path = "../bindgen", version = "=0.63.0", features = ["cli"] }
bindgen = { path = "../bindgen", version = "=0.63.0", features = ["cli", "experimental"] }
shlex = "1"
clap = { version = "4", features = ["derive"] }
env_logger = { version = "0.9.0", optional = true }
Expand Down
12 changes: 8 additions & 4 deletions bindgen-cli/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,20 @@ struct BindgenCommand {
#[arg(long, value_name = "CUSTOM")]
with_derive_custom_union: Vec<String>,
/// Generate extern wrappers for inlined functions
#[arg(long)]
#[arg(long, requires = "experimental")]
generate_extern_functions: bool,
/// Sets the name of the header and source code files that would be created if any extern wrapper functions must be generated due to the presence of inlined functions.
#[arg(long, value_name = "FILENAME")]
#[arg(long, requires = "experimental", value_name = "FILENAME")]
extern_functions_file_name: Option<String>,
#[arg(long, value_name = "DIRECTORY")]
#[arg(long, requires = "experimental", value_name = "DIRECTORY")]
/// Sets the directory path where any extra files must be created due to the presence of inlined functions.
extern_functions_directory: Option<String>,
/// Sets the suffix added to the extern wrapper functions generated for inlined functions.
#[arg(long, value_name = "SUFFIX")]
#[arg(long, requires = "experimental", value_name = "SUFFIX")]
extern_function_suffix: Option<String>,
/// Enables experimental features.
#[arg(long)]
experimental: bool,
/// Prints the version, and exits
#[arg(short = 'V', long)]
version: bool,
Expand Down Expand Up @@ -489,6 +492,7 @@ where
extern_functions_file_name,
extern_functions_directory,
extern_function_suffix,
experimental: _,
version,
clang_args,
} = command;
Expand Down
20 changes: 13 additions & 7 deletions bindgen-integration/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,13 @@ fn setup_extern_test() {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let out_rust_file = out_path.join("extern.rs");

let input_header_dir = PathBuf::from("../bindgen-tests/tests/headers/").canonicalize()
let input_header_dir = PathBuf::from("../bindgen-tests/tests/headers/")
.canonicalize()
.expect("Cannot canonicalize libdir path");
let input_header_file_path = input_header_dir.join("generate-extern-functions.h");
let input_header_file_path_str = input_header_file_path.to_str()
let input_header_file_path =
input_header_dir.join("generate-extern-functions.h");
let input_header_file_path_str = input_header_file_path
.to_str()
.expect("Path could not be converted to a str");

// generate external bindings with the external .c and .h files
Expand All @@ -226,7 +229,7 @@ fn setup_extern_test() {
.generate()
.expect("Unable to generate bindings");

println!("cargo:rustc-link-lib=extern"); // tell cargo to link libextern
println!("cargo:rustc-link-lib=extern"); // tell cargo to link libextern
println!("bindings generated: {}", bindings);

let obj_path = out_path.join("extern.o");
Expand All @@ -243,7 +246,8 @@ fn setup_extern_test() {
.output()
.expect("`clang` command error")
.status
.success() {
.success()
{
panic!("Could not compile object file");
}

Expand All @@ -254,11 +258,13 @@ fn setup_extern_test() {
.output()
.expect("`ar` command error")
.status
.success() {
.success()
{
panic!("Could not emit library file");
}

bindings.write_to_file(out_rust_file)
bindings
.write_to_file(out_rust_file)
.expect("Cound not write bindings to the Rust file");
}

Expand Down
2 changes: 1 addition & 1 deletion bindgen-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version = "0.1.0"
publish = false

[dev-dependencies]
bindgen = { path = "../bindgen", features = ["cli"] }
bindgen = { path = "../bindgen", features = ["cli", "experimental"] }
diff = "0.1"
shlex = "1"
clap = { version = "4", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion bindgen-tests/tests/headers/generate-extern-functions.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// bindgen-flags: --generate-extern-functions
// bindgen-flags: --experimental --generate-extern-functions

static inline int foo() {
return 11;
Expand Down
7 changes: 3 additions & 4 deletions bindgen-tests/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ fn test_extern_generated_headers() {
None,
Path::new(expect_path.join("extern.c").to_str().unwrap()),
)
.unwrap();
.unwrap();
}

if expected_h != actual_h {
Expand All @@ -759,7 +759,6 @@ fn test_extern_generated_headers() {
None,
Path::new(expect_path.join("extern.h").to_str().unwrap()),
)
.unwrap();
.unwrap();
}

}
}
1 change: 1 addition & 0 deletions bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ runtime = ["clang-sys/runtime"]
# Dynamically discover a `rustfmt` binary using the `which` crate
which-rustfmt = ["which"]
cli = []
experimental = []

# These features only exist for CI testing -- don't use them if you're not hacking
# on bindgen!
Expand Down
8 changes: 8 additions & 0 deletions bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,10 @@ impl Builder {
output_vector.push(suffix.clone());
}

if cfg!(feature = "experimental") {
output_vector.push("--experimental".into());
}

// Add clang arguments

output_vector.push("--".into());
Expand Down Expand Up @@ -1815,6 +1819,7 @@ impl Builder {
self
}

#[cfg(feature = "experimental")]
/// Whether to generate extern wrappers for inline functions. Defaults to false.
pub fn generate_extern_functions(mut self, doit: bool) -> Self {
self.options.generate_extern_functions = if doit {
Expand All @@ -1825,6 +1830,7 @@ impl Builder {
self
}

#[cfg(feature = "experimental")]
/// Set the name of the header and source code files that would be created if any extern
/// wrapper functions must be generated due to the presence of inlined functions.
pub fn extern_functions_file_name<T: AsRef<str>>(
Expand All @@ -1836,6 +1842,7 @@ impl Builder {
self
}

#[cfg(feature = "experimental")]
/// Set the directory path where any extra files must be created due to the presence of inlined
/// functions.
pub fn extern_functions_directory<T: AsRef<str>>(
Expand All @@ -1847,6 +1854,7 @@ impl Builder {
self
}

#[cfg(feature = "experimental")]
/// Set the suffix added to the extern wrapper functions generated for inlined functions.
pub fn extern_function_suffix<T: AsRef<str>>(mut self, suffix: T) -> Self {
self.options.extern_function_suffix = Some(suffix.as_ref().to_owned());
Expand Down

0 comments on commit 101cde0

Please sign in to comment.