Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cc_proto_aspect_hint to allow copts on cc_proto_library #19706

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion bazel/cc_proto_library.bzl
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
"""cc_proto_library rule"""

load("//bazel/private:bazel_cc_proto_library.bzl", _cc_proto_library = "cc_proto_library") # buildifier: disable=bzl-visibility
load("//bazel/private:bazel_cc_proto_library.bzl", _cc_proto_aspect_hint = "cc_proto_aspect_hint", _cc_proto_library = "cc_proto_library") # buildifier: disable=bzl-visibility

def cc_proto_library(**kwattrs):
# Only use Starlark rules when they are removed from Bazel
if not hasattr(native, "cc_proto_library"):
_cc_proto_library(**kwattrs)
else:
native.cc_proto_library(**kwattrs) # buildifier: disable=native-cc-proto

def cc_proto_aspect_hint(**kwattrs):
if hasattr(native, "cc_proto_library"):
fail("cc_proto_aspect_hint requires bazel 8.x+")
else:
_cc_proto_aspect_hint(**kwattrs)
55 changes: 55 additions & 0 deletions bazel/private/bazel_cc_proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ _CC_PROTO_TOOLCHAIN = Label("//bazel/private:cc_toolchain_type")

_ProtoCcFilesInfo = provider(fields = ["files"], doc = "Provide cc proto files.")
_ProtoCcHeaderInfo = provider(fields = ["headers"], doc = "Provide cc proto headers.")
_ProtoCcAspectHintInfo = provider(
doc = "Provide extra C++ specific info to proto compile actions",
fields = {
"copts": "Compiler flags to pass to the C++ compile acitons for the generated proto sources",
},
)

def _get_output_files(actions, proto_info, suffixes):
result = []
Expand Down Expand Up @@ -87,6 +93,11 @@ def _aspect_impl(target, ctx):
else: # shouldn't generate code
header_provider = _ProtoCcHeaderInfo(headers = depset())

copts = []
for hint in getattr(ctx.rule.attr, "aspect_hints", []):
if _ProtoCcAspectHintInfo in hint:
copts.extend(hint[_ProtoCcAspectHintInfo].copts)

proto_common.compile(
actions = ctx.actions,
proto_info = proto_info,
Expand All @@ -107,6 +118,7 @@ def _aspect_impl(target, ctx):
headers = headers,
textual_hdrs = textual_hdrs,
strip_include_prefix = _get_strip_include_prefix(ctx, proto_info),
user_compile_flags = copts,
)

return [
Expand Down Expand Up @@ -196,3 +208,46 @@ rules to generate C++ code for.""",
provides = [CcInfo],
toolchains = toolchains.use_toolchain(_CC_PROTO_TOOLCHAIN),
)

def _aspect_hint_impl(ctx):
return _ProtoCcAspectHintInfo(copts = ctx.attr.copts)

cc_proto_aspect_hint = rule(
implementation = _aspect_hint_impl,
attrs = {
"copts": attr.string_list(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can add any other attributes we want here, i imagine linkopts would be the next most sensible one, happy to add other useful ones in this PR, or just as use cases come up

},
doc = """
<p>
<code>cc_proto_aspect_hint</code> allows you to custom how generated C++ code compiles.
</p>

<p>
<code>copts</code> custom compiler flags to pass to the C++ compiler.
</p>

<p>
Example:
</p>

<pre>
<code class="lang-starlark">
cc_proto_library(
name = "foo_cc_proto",
deps = [":foo_proto"],
)

cc_proto_aspect_hint(
name = "custom_cc_flags",
copts = ["-Wno-all"],
)

proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
aspect_hints = [":custom_cc_flags"],
)
</code>
</pre>
""",
)
Loading