From 810c37ec1e0fa218979ca8b0cc72db68b477cfb9 Mon Sep 17 00:00:00 2001 From: Ellen Arteca Date: Tue, 23 Apr 2024 00:17:47 +0000 Subject: [PATCH] Adds support to bindgen to handle static inline fcts Adds support for bindgen to be able to handle `static inline` functions. This is done by adding a new boolean field to the `BindgenProperties` struct, `Handle_static_inline` (default to false). If this field is true, then the flags to trigger bindgen support of static inline functions are passed in. The rust-bindgen documentation list two ways of handling `static inline` functions, both specified with command line args. 1) --generate-inline-functions 2) --experimental --wrap-static-fns Option 1 requires some extra effort on the part of the C library developer, in that they have to expose the function symbols: the docs (linked below) explain that this is often done by compiling the library with inlining disabled, which can be detrimental to performance. Option 2 requires no effort on the part of the C library developer, but it does require the `--experimental` flag, since this feature is still under development. This CL goes with option 2. Relevant docs: https://github.com/rust-lang/rust-bindgen/discussions/2405 This CL also adds a new test: TestBindgenHandleStaticInlining in bindgen_test.go Test: m blueprint_tests Change-Id: If28000e3f3ccecc65c4cae1c62d7bf455454239a --- rust/bindgen.go | 6 ++++++ rust/bindgen_test.go | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/rust/bindgen.go b/rust/bindgen.go index 11ba74d45..eaed1b9d4 100644 --- a/rust/bindgen.go +++ b/rust/bindgen.go @@ -101,6 +101,9 @@ type BindgenProperties struct { // // "my_bindgen [flags] wrapper_header.h -o [output_path] -- [clang flags]" Custom_bindgen string + + // flag to indicate if bindgen should handle `static inline` functions (default is false) + Handle_static_inline bool } type bindgenDecorator struct { @@ -232,6 +235,9 @@ func (b *bindgenDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) andr bindgenFlags := defaultBindgenFlags bindgenFlags = append(bindgenFlags, esc(b.Properties.Bindgen_flags)...) + if b.Properties.Handle_static_inline { + bindgenFlags = append(bindgenFlags, "--experimental --wrap-static-fns") + } // cat reads from stdin if its command line is empty, // so we pass in /dev/null if there are no other flag files diff --git a/rust/bindgen_test.go b/rust/bindgen_test.go index 0c0a6dad0..11cfe4e88 100644 --- a/rust/bindgen_test.go +++ b/rust/bindgen_test.go @@ -227,3 +227,22 @@ func TestBindgenFlagFile(t *testing.T) { // TODO: The best we can do right now is check $flagfiles. Once bindgen.go switches to RuleBuilder, // we may be able to check libbinder.RuleParams.Command to see if it contains $(cat /dev/null flag_file.txt) } + + +func TestBindgenHandleStaticInlining(t *testing.T) { + ctx := testRust(t, ` + rust_bindgen { + name: "libbindgen", + wrapper_src: "src/any.h", + crate_name: "bindgen", + stem: "libbindgen", + source_stem: "bindings", + handle_static_inline: true + } + `) + libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a_source").Output("bindings.rs") + // Make sure the flag to support `static inline` functions is present + if !strings.Contains(libbindgen.Args["flags"], "--wrap-static-fns") { + t.Errorf("missing flag to handle static inlining in rust_bindgen rule: flags %#v", libbindgen.Args["flags"]) + } +}