# Copyright 2022 The Chromium Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. import("//build/config/clang/clang.gni") import("//build/config/rust.gni") import("//build/config/sysroot.gni") import("//build/rust/rust_bindgen_generator.gni") import("//build/rust/rust_static_library.gni") if (is_win) { import("//build/toolchain/win/win_toolchain_data.gni") } _bindgen_path = "${rust_bindgen_root}/bin/bindgen" if (host_os == "win") { _bindgen_path = "${_bindgen_path}.exe" } # On Windows, the libclang.dll is beside the bindgen.exe, otherwise it is in # ../lib. _libclang_path = rust_bindgen_root if (host_os == "win") { _libclang_path += "/bin" } else { _libclang_path += "/lib" } # Template to build Rust/C bindings with bindgen. # # This template expands to a rust_static_library that exports the # bindings generated from bindgen at the root of the library. # # Parameters: # # header: # The .h file to generate bindings for. # # deps: (optional) # C targets on which the headers depend in order to build successfully. # # configs: (optional) # C compilation targets determine the correct list of -D and -I flags based # on their dependencies and any configs applied. The same applies here. Set # any configs here as if this were a C target. # # cpp: (optional) # Use C++ mode to consume the header instead of C mode (the default). # # bindgen_flags: (optional) # The additional bindgen flags which are passed to the executable. A `--` will # be prepended to each flag. So use `bindgen_flags = [ "foo" ]` to pass # `--foo` to bindgen. # # wrap_static_fns: (optional) # If set to true, enables binding `static` and `static inline` functions in # the header. Setting this causes the template to emit a source_set target # named "${target_name}_static_fns", which must be incorporated into the # build. Additionally, `get_target_outputs` will return both the Rust file and # a generated C file, but callers can rely on the Rust file being first. # # # For a small, self-contained example please see: # * C header: //build/rust/tests/bindgen_test # * C++ header: //build/rust/tests/bindgen_cpp_test template("rust_bindgen") { # "_generator" will be added to the rust_bindgen_generator target. _rust_bindgen_generator_name = target_name + "_generator" _wrap_static_fns = false if (defined(invoker.wrap_static_fns) && invoker.wrap_static_fns) { _wrap_static_fns = true } rust_bindgen_generator(_rust_bindgen_generator_name) { forward_variables_from(invoker, "*", [ "library_name", "output_name", ] + TESTONLY_AND_VISIBILITY) # This will allow the rust_static_library to depend on the # `rust_bindgen_generator` through visibility. library_name = target_name # We know the library that is going to consume this rust_bindgen and we're # sure that only a single bindgen is there. So rename the bindings to avoid # passing envflags. envflags are usually problematic for Cronet as Soong # does not support it (b/181221467). output_name = "bindings" } rust_static_library(target_name) { forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "crate_name", "cpp", ]) crate_root = "//build/rust/bindings.rs" sources = [ crate_root ] bindgen_deps = [ ":$_rust_bindgen_generator_name" ] allow_unsafe = true if (_wrap_static_fns) { # Add a dependency on the static_fns library for simplicity if # it's declared. deps = [ ":${_rust_bindgen_generator_name}_static_fns" ] } if (defined(cpp) && cpp) { # This cfg is used to control the bindings public export. rustflags = [ "--cfg", "cpp", ] } } }