blob: f5512b2b3d1283aaab4d1ef77e3663172edcce6e [file] [log] [blame]
# Copyright 2021 The Chromium Authors. All rights reserved.
# 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")
set_defaults("rust_cxx") {
native_header_deps = []
}
# Template to build Rust/C++ bindings using the tooling
# described at https://cxx.rs.
#
# (Specifically, this builds the C++ side of the bindings.
# The Rust side of the bindings are not 'built' as such, but
# automatically expanded by the Rust compiler by virtue of a
# proc macro which is a simple dependency relationship on the
# Rust target).
#
# This template expands to a source_set containing the
# C++ side of the bindings. Simply treat it as a dependency.
#
# If you're using this, consider whether you should be using
# rust_source_set.gni or mixed_source_set.gni which have
# facilities to do the same code generation in an easier
# way.
#
# Parameters:
#
# inputs:
# The .rs files containing a #[cxx::bridge] section.
#
# native_header_deps (optional)
# C++ targets on which the headers depend in order to build
# successfully.
#
# generate_source_set (optional; default is true)
# Whether to generate a source_set target here.
# (See below for why you might choose false)
#
# Consumers of this template may alternatively depend upon
# the :${target_name}_gen target if you wish to include
# the generated source code in an *existing* source set.
# To do this:
# * depend on the target name with a _gen suffix;
# * for each of the inputs, include in your sources:
# file_name.rs.h
# file_name.rs.cc
# * ensure you also depend upon //build/rust:cxx_cppdeps
#
# A good way to get the file names is:
# process_file_template(list-of-bindings-rs-files,
# [ "{{source_gen_dir}}/{{source_file_part}}.h",
# "{{source_gen_dir}}/{{source_file_part}}.cc" ]
#
# You might want to depend on this template in this mode if
# you have an existing source_set which is referred to
# using 'visibility' rules throughout the codebase.
#
template("rust_cxx") {
assert(defined(invoker.inputs), "Must specify the Rust file to use as input.")
action_foreach("${target_name}_gen") {
sources = invoker.inputs
output_h = "{{source_gen_dir}}/{{source_file_part}}.h"
output_cc = "{{source_gen_dir}}/{{source_file_part}}.cc"
cxxbridge_target =
"//third_party/rust/cxxbridge_cmd/v1:cxxbridge($host_toolchain)"
cxxbridge_obj_dir = get_label_info(cxxbridge_target, "root_out_dir")
cxxbridge_executable = rebase_path("${cxxbridge_obj_dir}/cxxbridge")
if (is_win) {
cxxbridge_executable = rebase_path("${cxxbridge_executable}.exe")
}
script = rebase_path("//build/rust/run_cxxbridge.py")
inputs = [
cxxbridge_executable,
script,
]
outputs = [
output_h,
output_cc,
]
deps = [ cxxbridge_target ]
args = [
"--exe",
cxxbridge_executable,
"--header",
output_h,
"--cc",
output_cc,
"{{source}}",
]
# Anyone building the header files we generate needs to have
# RUST_CXX_NO_EXCEPTIONS defined so that they build without
# exception support.
public_configs = [ "//build/rust:cxx_cppconfig" ]
}
if (!defined(invoker.generate_source_set) || invoker.generate_source_set) {
source_set(target_name) {
sources = get_target_outputs(":${target_name}_gen")
deps = [ "//build/rust:cxx_cppdeps" ]
deps += invoker.native_header_deps
public_deps = [ ":${target_name}_gen" ]
}
}
}