Skip to content

Commit

Permalink
Merge pull request #1416 from wmatthews-google/bazel-common-attributes
Browse files Browse the repository at this point in the history
Add support for Bazel common attributes [1] in `rust_cxx_bridge`.
  • Loading branch information
dtolnay authored Dec 24, 2024
2 parents ee011db + 7ae867a commit c6f9191
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ rust_library(
":impl",
"//:cxx",
],
testonly = True,
)

cc_library(
Expand All @@ -40,16 +41,19 @@ cc_library(
":module/include",
"//:core",
],
testonly = True,
)

rust_cxx_bridge(
name = "bridge",
src = "ffi/lib.rs",
deps = [":impl"],
testonly = True,
)

rust_cxx_bridge(
name = "module",
src = "ffi/module.rs",
deps = [":impl"],
testonly = True,
)
8 changes: 7 additions & 1 deletion tools/bazel/rust_cxx_bridge.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@
load("@bazel_skylib//rules:run_binary.bzl", "run_binary")
load("@rules_cc//cc:defs.bzl", "cc_library")

def rust_cxx_bridge(name, src, deps = []):
def rust_cxx_bridge(name, src, deps = [], **kwargs):
"""A macro defining a cxx bridge library
Args:
name (string): The name of the new target
src (string): The rust source file to generate a bridge for
deps (list, optional): A list of dependencies for the underlying cc_library. Defaults to [].
**kwargs: Common arguments to pass through to underlying rules.
"""
native.alias(
name = "%s/header" % name,
actual = src + ".h",
**kwargs,
)

native.alias(
name = "%s/source" % name,
actual = src + ".cc",
**kwargs,
)

run_binary(
Expand All @@ -35,15 +38,18 @@ def rust_cxx_bridge(name, src, deps = []):
"$(location %s.cc)" % src,
],
tool = "@cxx.rs//:codegen",
**kwargs,
)

cc_library(
name = name,
srcs = [src + ".cc"],
deps = deps + [":%s/include" % name],
**kwargs,
)

cc_library(
name = "%s/include" % name,
hdrs = [src + ".h"],
**kwargs,
)

0 comments on commit c6f9191

Please sign in to comment.