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