1 // Copyright 2020, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //! This is a helper crate for using bindgen as a library from within
16 //! Android's build system. Some functionality (such as the type selection
17 //! heuristic) is not available on the command line, and so the library
18 //! interface may be necessary. Bindgen also needs to receive configuration
19 //! from Soong however to find appropriate headers, set global cflags, etc.
20 //!
21 //! This crate provides the ability to run a hooked version of the command
22 //! line bindgen tool, with the ability to call a user-provided transformation
23 //! on the the builder before it is used.
24
25 use bindgen;
26 use std::env;
27
28 #[path = "../../../src/options.rs"]
29 mod options;
30
31 /// Takes in a function describing adjustments to make to a builder
32 /// initialized by the command line. `build(|x| x)` is equivalent to
33 /// running bindgen. When converting a build.rs, you will want to convert the
34 /// additional configuration they do into a function, then pass it to `build`
35 /// inside your main function.
build<C: FnOnce(bindgen::Builder) -> bindgen::Builder>(configure: C)36 pub fn build<C: FnOnce(bindgen::Builder) -> bindgen::Builder>(configure: C) {
37 env_logger::init();
38
39 match options::builder_from_flags(env::args()) {
40 Ok((builder, output, _)) => {
41 configure(builder)
42 .generate()
43 .expect("Unable to generate bindings")
44 .write(output)
45 .expect("Unable to write output");
46 }
47 Err(error) => {
48 eprintln!("{}", error);
49 std::process::exit(1);
50 }
51 };
52 }
53