Skip to content

Commit

Permalink
pw_protobuf_compiler: New library-include-format
Browse files Browse the repository at this point in the history
Test: Verified that //pw_metric:metric_service_nanopb_test passes in g3
Test: and when run from a downstream Bazel project.

Fixes: 386424625
Change-Id: I54c287e4c3b5b3d9054d0fb9e7d897f8e3f9cf38
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/256732
Docs-Not-Needed: Ted Pudlik <[email protected]>
Commit-Queue: Ted Pudlik <[email protected]>
Reviewed-by: Dave Roth <[email protected]>
Lint: Lint 🤖 <[email protected]>
  • Loading branch information
tpudlik authored and CQ Bot Account committed Dec 31, 2024
1 parent 4afcce7 commit 5fa6e32
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
23 changes: 16 additions & 7 deletions pw_protobuf_compiler/nanopb_proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,25 @@ def nanopb_proto_library(*, name, deps, tags = [], options = None, **kwargs):
**kwargs
)

def _custom_opt_for_library_include_format():
"""Return correctly set --library-include-format.
When using nanopb_proto_library from a monorepo in which nanopb is not an
external repository but just a build target within the main tree, the
#include statements need to be relative to the root of that tree. Handle
this case using --library-include-format.
"""
pb_h = Label("@com_github_nanopb_nanopb//:pb.h")
if pb_h.workspace_root == "":
# Monorepo case
return "--library-include-format=#include \"{}/%s\"".format(pb_h.package)
else:
return "--library-include-format=#include \"%s\""

_nanopb_proto_compiler_aspect = proto_compiler_aspect(
["pb.h", "pb.c"],
Label("@com_github_nanopb_nanopb//:protoc-gen-nanopb"),
[],
{
"_pb_h": attr.label(
default = Label("@com_github_nanopb_nanopb//:pb.h"),
allow_single_file = True,
),
},
[_custom_opt_for_library_include_format()],
)

_nanopb_proto_library = rule(
Expand Down
12 changes: 2 additions & 10 deletions pw_protobuf_compiler/private/proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,6 @@ def _proto_compiler_aspect_impl(target, ctx):
continue
args.add("--custom_opt={}".format(plugin_option))

# In certain environments the path to pb.h must be defined, rather
# than relying on the default location.
# Use a format string rather than `quote`, to maintain support
# for nanopb 3.
if hasattr(ctx.attr, "_pb_h"):
plugin_options_arg = "--library-include-format=#include \"{}/%s\"".format(ctx.file._pb_h.dirname)
args.add("--custom_opt={}".format(plugin_options_arg))

args.add("--custom_out={}".format(out_path))
args.add_all(proto_info.direct_sources)

Expand Down Expand Up @@ -247,7 +239,7 @@ def _proto_compiler_aspect_impl(target, ctx):
includes = transitive_includes,
)]

def proto_compiler_aspect(extensions, protoc_plugin, plugin_options = [], additional_attrs = {}):
def proto_compiler_aspect(extensions, protoc_plugin, plugin_options = []):
"""Returns an aspect that runs the proto compiler.
The aspect propagates through the deps of proto_library targets, running
Expand Down Expand Up @@ -278,7 +270,7 @@ def proto_compiler_aspect(extensions, protoc_plugin, plugin_options = [], additi
executable = True,
cfg = "exec",
),
} | additional_attrs,
},
implementation = _proto_compiler_aspect_impl,
provides = [PwProtoInfo],
toolchains = ["@rules_python//python:exec_tools_toolchain_type"],
Expand Down

0 comments on commit 5fa6e32

Please sign in to comment.