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 bindgen_cli;
27 use std::env;
28 
29 /// Takes in a function describing adjustments to make to a builder
30 /// initialized by the command line. `build(|x| x)` is equivalent to
31 /// running bindgen. When converting a build.rs, you will want to convert the
32 /// additional configuration they do into a function, then pass it to `build`
33 /// inside your main function.
build<C: FnOnce(bindgen::Builder) -> bindgen::Builder>(configure: C)34 pub fn build<C: FnOnce(bindgen::Builder) -> bindgen::Builder>(configure: C) {
35     env_logger::init();
36 
37     match bindgen_cli::builder_from_flags(env::args()) {
38         Ok((builder, output, _)) => {
39             configure(builder)
40                 .generate()
41                 .expect("Unable to generate bindings")
42                 .write(output)
43                 .expect("Unable to write output");
44         }
45         Err(error) => {
46             eprintln!("{}", error);
47             std::process::exit(1);
48         }
49     };
50 }
51