• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1diff --git a/android/bindgen_cmd/Android.bp b/android/bindgen_cmd/Android.bp
2new file mode 100644
3index 0000000..689e7ae
4--- /dev/null
5+++ b/android/bindgen_cmd/Android.bp
6@@ -0,0 +1,28 @@
7+package {
8+    // See: http://go/android-license-faq
9+    // A large-scale-change added 'default_applicable_licenses' to import
10+    // all of the 'license_kinds' from "external_rust_crates_bindgen_license"
11+    // to get the below license kinds:
12+    //   SPDX-license-identifier-Apache-2.0
13+    default_applicable_licenses: ["external_rust_crates_bindgen_license"],
14+}
15+
16+rust_library_host {
17+    name: "libbindgen_cmd",
18+    crate_name: "bindgen_cmd",
19+    srcs: ["src/lib.rs"],
20+    edition: "2018",
21+    features: [
22+        "clap",
23+        "runtime",
24+        "which",
25+        "which-rustfmt",
26+    ],
27+    rustlibs: [
28+        "libbindgen",
29+        "libbindgen_cli",
30+        "libclap",
31+        "libenv_logger",
32+    ],
33+    compile_multilib: "first",
34+}
35diff --git a/android/bindgen_cmd/src/lib.rs b/android/bindgen_cmd/src/lib.rs
36new file mode 100644
37index 0000000..d33da7f
38--- /dev/null
39+++ b/android/bindgen_cmd/src/lib.rs
40@@ -0,0 +1,50 @@
41+// Copyright 2020, The Android Open Source Project
42+//
43+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
45+// You may obtain a copy of the License at
46+//
47+//     http://www.apache.org/licenses/LICENSE-2.0
48+//
49+// Unless required by applicable law or agreed to in writing, software
50+// distributed under the License is distributed on an "AS IS" BASIS,
51+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
52+// See the License for the specific language governing permissions and
53+// limitations under the License.
54+
55+//! This is a helper crate for using bindgen as a library from within
56+//! Android's build system. Some functionality (such as the type selection
57+//! heuristic) is not available on the command line, and so the library
58+//! interface may be necessary. Bindgen also needs to receive configuration
59+//! from Soong however to find appropriate headers, set global cflags, etc.
60+//!
61+//! This crate provides the ability to run a hooked version of the command
62+//! line bindgen tool, with the ability to call a user-provided transformation
63+//! on the the builder before it is used.
64+
65+use bindgen;
66+use bindgen_cli;
67+use std::env;
68+
69+/// Takes in a function describing adjustments to make to a builder
70+/// initialized by the command line. `build(|x| x)` is equivalent to
71+/// running bindgen. When converting a build.rs, you will want to convert the
72+/// additional configuration they do into a function, then pass it to `build`
73+/// inside your main function.
74+pub fn build<C: FnOnce(bindgen::Builder) -> bindgen::Builder>(configure: C) {
75+    env_logger::init();
76+
77+    match bindgen_cli::builder_from_flags(env::args()) {
78+        Ok((builder, output, _)) => {
79+            configure(builder)
80+                .generate()
81+                .expect("Unable to generate bindings")
82+                .write(output)
83+                .expect("Unable to write output");
84+        }
85+        Err(error) => {
86+            eprintln!("{}", error);
87+            std::process::exit(1);
88+        }
89+    };
90+}
91