-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathrust_cxx_bridge.bzl
More file actions
127 lines (115 loc) · 4.2 KB
/
rust_cxx_bridge.bzl
File metadata and controls
127 lines (115 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Copyright 2025 The ODML Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Rust cxxbridge rules."""
# [Google-internal load of `cc_library`]
load("@rules_cc//cc:defs.bzl", "cc_library")
def rust_cxx_bridge(name, src, deps = [], visibility = None, crate_features = []):
"""Defines the C++ side of a cxx bridge between C++ and Rust.
Args:
name: Names the C++ library exposing the bridge interface.
"name/include" will also be generated, providing headers used by the impl library.
src: Rust source file that contains CXX bridge definitions.
deps: This includes the impl library and any other dependencies used in the bridge.
visibility: Visibility of the bridge target.
crate_features: Feature flags to enable for codegen. See https://doc.rust-lang.org/cargo/reference/features.html.
"""
out_h = "%s.h" % src
out_cc = "%s.cc" % src
run_cxxbridge_cmd(
name = "%s/generated" % name,
srcs = [src],
outs = [out_h, out_cc],
args = [
"$(location %s)" % src,
"-o",
"$(location %s)" % out_h,
"-o",
"$(location %s)" % out_cc,
],
crate_features = crate_features,
)
# This library provides the generated header only.
#
# The impl library will depend on this.
# This allows it to see shared data types, and call `extern "Rust"` functions.
#
# Should not be exposed publicly: it does not contain the generated thunk code we need to link.
# This library exists to avoid a circular dependency between the impl and bridge cc_librarys.
cc_library(
name = "%s/include" % name,
# Textual: this header necessarily #includes hdrs from the impl library.
# We can't depend on that here, because it depends on this!
textual_hdrs = [out_h],
visibility = ["//visibility:private"],
)
kwargs = {}
if visibility != None:
kwargs["visibility"] = visibility
# The public bridge library.
#
# This exposes the bridge header, and links in the generated thunk code and the impl library.
cc_library(
name = name,
srcs = [out_cc],
hdrs = [out_h],
deps = deps + ["%s/include" % name],
**kwargs
)
def _run_cxxbridge_cmd_impl(ctx):
args = [
ctx.expand_location(a)
for a in ctx.attr.args
]
for f in ctx.attr.crate_features:
args.append("--cfg")
args.append("feature=\"%s\"" % f)
# https://bazel.build/rules/lib/builtins/actions.html#run
ctx.actions.run(
outputs = ctx.outputs.outs,
inputs = ctx.files.srcs,
executable = ctx.executable._cxxbridge,
arguments = args,
mnemonic = "RunCxxbridgeCmd",
)
return DefaultInfo(
files = depset(ctx.outputs.outs),
runfiles = ctx.runfiles(ctx.outputs.outs),
)
run_cxxbridge_cmd = rule(
implementation = _run_cxxbridge_cmd_impl,
attrs = {
"srcs": attr.label_list(
doc = "Source dependencies for this rule",
allow_files = True,
mandatory = True,
),
"outs": attr.output_list(
doc = "C++ output files generated by cxxbridge_cmd.",
mandatory = True,
),
"args": attr.string_list(
doc = "Arguments to `cxxbridge_cmd`.",
mandatory = True,
),
"crate_features": attr.string_list(
doc = "Optional list of cargo features that CXX bridge definitions may depend on.",
),
"_cxxbridge": attr.label(
default = Label("@cxxbridge_cmd//:cxxbridge_cmd"),
allow_single_file = True,
executable = True,
cfg = "exec",
),
},
)