rust_bindgen.gni (4076B)
1 # Copyright 2022 The Chromium Authors 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 import("//chromium/build/config/clang/clang.gni") 6 import("//chromium/build/config/rust.gni") 7 import("//chromium/build/config/sysroot.gni") 8 import("//chromium/build/rust/rust_bindgen_generator.gni") 9 import("//chromium/build/rust/rust_static_library.gni") 10 11 if (is_win) { 12 import("//chromium/build/toolchain/win/win_toolchain_data.gni") 13 } 14 15 _bindgen_path = "${rust_bindgen_root}/bin/bindgen" 16 if (host_os == "win") { 17 _bindgen_path = "${_bindgen_path}.exe" 18 } 19 20 # On Windows, the libclang.dll is beside the bindgen.exe, otherwise it is in 21 # ../lib. 22 _libclang_path = rust_bindgen_root 23 if (host_os == "win") { 24 _libclang_path += "/bin" 25 } else { 26 _libclang_path += "/lib" 27 } 28 29 # Template to build Rust/C bindings with bindgen. 30 # 31 # This template expands to a rust_static_library that exports the 32 # bindings generated from bindgen at the root of the library. 33 # 34 # Parameters: 35 # 36 # header: 37 # The .h file to generate bindings for. 38 # 39 # deps: (optional) 40 # C targets on which the headers depend in order to build successfully. 41 # 42 # configs: (optional) 43 # C compilation targets determine the correct list of -D and -I flags based 44 # on their dependencies and any configs applied. The same applies here. Set 45 # any configs here as if this were a C target. 46 # 47 # cpp: (optional) 48 # Use C++ mode to consume the header instead of C mode (the default). 49 # 50 # bindgen_flags: (optional) 51 # The additional bindgen flags which are passed to the executable. A `--` will 52 # be prepended to each flag. So use `bindgen_flags = [ "foo" ]` to pass 53 # `--foo` to bindgen. 54 # 55 # wrap_static_fns: (optional) 56 # If set to true, enables binding `static` and `static inline` functions in 57 # the header. Setting this causes the template to emit a source_set target 58 # named "${target_name}_static_fns", which must be incorporated into the 59 # build. Additionally, `get_target_outputs` will return both the Rust file and 60 # a generated C file, but callers can rely on the Rust file being first. 61 # 62 # 63 # For a small, self-contained example please see: 64 # * C header: //build/rust/tests/bindgen_test 65 # * C++ header: //build/rust/tests/bindgen_cpp_test 66 template("rust_bindgen") { 67 # "_generator" will be added to the rust_bindgen_generator target. 68 _rust_bindgen_generator_name = target_name + "_generator" 69 _wrap_static_fns = false 70 if (defined(invoker.wrap_static_fns) && invoker.wrap_static_fns) { 71 _wrap_static_fns = true 72 } 73 rust_bindgen_generator(_rust_bindgen_generator_name) { 74 forward_variables_from(invoker, 75 "*", 76 [ 77 "library_name", 78 "output_name", 79 ] + TESTONLY_AND_VISIBILITY) 80 81 # This will allow the rust_static_library to depend on the 82 # `rust_bindgen_generator` through visibility. 83 library_name = target_name 84 85 # We know the library that is going to consume this rust_bindgen and we're 86 # sure that only a single bindgen is there. So rename the bindings to avoid 87 # passing envflags. envflags are usually problematic for Cronet as Soong 88 # does not support it (b/181221467). 89 output_name = "bindings" 90 } 91 92 rust_static_library(target_name) { 93 forward_variables_from(invoker, 94 TESTONLY_AND_VISIBILITY + [ 95 "crate_name", 96 "cpp", 97 ]) 98 99 crate_root = "//chromium/build/rust/bindings.rs" 100 sources = [ crate_root ] 101 bindgen_deps = [ ":$_rust_bindgen_generator_name" ] 102 allow_unsafe = true 103 if (_wrap_static_fns) { 104 # Add a dependency on the static_fns library for simplicity if 105 # it's declared. 106 deps = [ ":${_rust_bindgen_generator_name}_static_fns" ] 107 } 108 if (defined(cpp) && cpp) { 109 # This cfg is used to control the bindings public export. 110 rustflags = [ 111 "--cfg", 112 "cpp", 113 ] 114 } 115 } 116 }