rust: Support for generated c files from bindgen
To handle static inline functions, bindgen generates a C file which is
expected to be compiled and linked into dependents on the generated
bindgen source.
This CL adds support for cc modules ingesting this output and for
bindgen to produce it (and link it against the bindgen rust_library
variant as well).
Bug: 290347127
Test: m blueprint_tests
Test: Example module with static inline functions builds locally
Change-Id: I167a8356eb6e0059fc21169fd3bfc6bf4d9c812f
diff --git a/rust/bindgen.go b/rust/bindgen.go
index eaed1b9..4277753 100644
--- a/rust/bindgen.go
+++ b/rust/bindgen.go
@@ -102,8 +102,17 @@
// "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
+ // flag to indicate if bindgen should handle `static inline` functions (default is false).
+ // If true, Static_inline_library must be set.
+ Handle_static_inline *bool
+
+ // module name of the corresponding cc_library_static which includes the static_inline wrapper
+ // generated functions from bindgen. Must be used together with handle_static_inline.
+ //
+ // If there are no static inline functions provided through the header file,
+ // then bindgen (as of 0.69.2) will silently fail to output a .c file, and
+ // the cc_library_static depending on this module will fail compilation.
+ Static_inline_library *string
}
type bindgenDecorator struct {
@@ -159,6 +168,18 @@
var cflags []string
var implicits android.Paths
+ var implicitOutputs android.WritablePaths
+ var validations android.Paths
+
+ if Bool(b.Properties.Handle_static_inline) && b.Properties.Static_inline_library == nil {
+ ctx.PropertyErrorf("handle_static_inline",
+ "requires declaring static_inline_library to the corresponding cc_library module that includes the generated C source from bindgen.")
+ }
+
+ if b.Properties.Static_inline_library != nil && !Bool(b.Properties.Handle_static_inline) {
+ ctx.PropertyErrorf("static_inline_library",
+ "requires declaring handle_static_inline.")
+ }
implicits = append(implicits, deps.depGeneratedHeaders...)
@@ -235,8 +256,11 @@
bindgenFlags := defaultBindgenFlags
bindgenFlags = append(bindgenFlags, esc(b.Properties.Bindgen_flags)...)
- if b.Properties.Handle_static_inline {
- bindgenFlags = append(bindgenFlags, "--experimental --wrap-static-fns")
+ if Bool(b.Properties.Handle_static_inline) {
+ outputStaticFnsFile := android.PathForModuleOut(ctx, b.BaseSourceProvider.getStem(ctx)+".c")
+ implicitOutputs = append(implicitOutputs, outputStaticFnsFile)
+ validations = append(validations, outputStaticFnsFile)
+ bindgenFlags = append(bindgenFlags, []string{"--experimental", "--wrap-static-fns", "--wrap-static-fns-path=" + outputStaticFnsFile.String()}...)
}
// cat reads from stdin if its command line is empty,
@@ -285,11 +309,13 @@
}
ctx.Build(pctx, android.BuildParams{
- Rule: bindgen,
- Description: strings.Join([]string{cmdDesc, wrapperFile.Path().Rel()}, " "),
- Output: outputFile,
- Input: wrapperFile.Path(),
- Implicits: implicits,
+ Rule: bindgen,
+ Description: strings.Join([]string{cmdDesc, wrapperFile.Path().Rel()}, " "),
+ Output: outputFile,
+ Input: wrapperFile.Path(),
+ Implicits: implicits,
+ ImplicitOutputs: implicitOutputs,
+ Validations: validations,
Args: map[string]string{
"cmd": cmd,
"flags": strings.Join(bindgenFlags, " "),
@@ -299,6 +325,14 @@
})
b.BaseSourceProvider.OutputFiles = android.Paths{outputFile}
+
+ // Append any additional implicit outputs after the entry point source.
+ // We append any generated .c file here so it can picked up by cc_library_static modules.
+ // Those CC modules need to be sure not to pass any included .rs files to Clang.
+ // We don't have to worry about the additional .c files for Rust modules as only the entry point
+ // is passed to rustc.
+ b.BaseSourceProvider.OutputFiles = append(b.BaseSourceProvider.OutputFiles, implicitOutputs.Paths()...)
+
return outputFile
}
@@ -350,6 +384,14 @@
deps = muslDeps(ctx, deps, false)
}
+ if !ctx.RustModule().Source() && b.Properties.Static_inline_library != nil {
+ // This is not the source variant, so add the static inline library as a dependency.
+ //
+ // This is necessary to avoid a circular dependency between the source variant and the
+ // dependent cc module.
+ deps.StaticLibs = append(deps.StaticLibs, String(b.Properties.Static_inline_library))
+ }
+
deps.SharedLibs = append(deps.SharedLibs, b.ClangProperties.Shared_libs...)
deps.StaticLibs = append(deps.StaticLibs, b.ClangProperties.Static_libs...)
deps.HeaderLibs = append(deps.HeaderLibs, b.ClangProperties.Header_libs...)