diff options
author | mark <mark@astranis.com> | 2023-10-12 23:44:52 +0300 |
---|---|---|
committer | Petteri Aimonen <jpa@github.mail.kapsi.fi> | 2023-10-14 08:22:44 +0300 |
commit | 09234696e0ef821432a8541b950e8866f0c61f8c (patch) | |
tree | 909c15747f645cac8636768bfdd0d1aa5139a210 | |
parent | 33383170274e89d9c7cb73b12d5f30d7239f3532 (diff) |
Add support for nanopb options files without patching rules_proto_grpc
-rw-r--r-- | BUILD.bazel | 2 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | docs/bazel_build.md | 10 | ||||
-rw-r--r-- | extra/bazel/nanopb_cc_proto_library.bzl | 30 | ||||
-rw-r--r-- | extra/bazel/nanopb_deps.bzl | 7 | ||||
-rw-r--r-- | extra/bazel/rules_proto_grpc.patch | 30 |
6 files changed, 41 insertions, 40 deletions
diff --git a/BUILD.bazel b/BUILD.bazel index 9fde6ee..e51e42b 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -98,7 +98,7 @@ proto_library( cc_nanopb_proto_library( name = "all_types_nanopb", protos = [":all_types_proto"], - nanopb_options_file = "tests/alltypes/alltypes.options", + nanopb_options_files = ["tests/alltypes/alltypes.options"], visibility = ["//visibility:private"], ) @@ -87,7 +87,7 @@ There exist build rules for several systems: * **Makefiles**: `extra/nanopb.mk`, see `examples/simple` * **CMake**: `extra/FindNanopb.cmake`, see `examples/cmake` * **SCons**: `tests/site_scons` (generator only) -* **Bazel**: `BUILD` in source root +* **Bazel**: `BUILD.bazel` in source root * **Conan**: `conanfile.py` in source root * **PlatformIO**: https://platformio.org/lib/show/431/Nanopb * **PyPI/pip**: https://pypi.org/project/nanopb/ diff --git a/docs/bazel_build.md b/docs/bazel_build.md index dc08a8f..c93d64a 100644 --- a/docs/bazel_build.md +++ b/docs/bazel_build.md @@ -59,3 +59,13 @@ cc_library( ) ``` +If you have a custom nanopb options file, use the `nanopb_options_files` argument shown below. +``` py +# Generated library with options. +cc_nanopb_proto_library( + name = "descriptor_nanopb", + protos = [":descriptor"], + nanopb_options_files = ["descriptor.options"], + visibility = ["//visibility:private"], +) +``` diff --git a/extra/bazel/nanopb_cc_proto_library.bzl b/extra/bazel/nanopb_cc_proto_library.bzl index 478ab58..e1a58d5 100644 --- a/extra/bazel/nanopb_cc_proto_library.bzl +++ b/extra/bazel/nanopb_cc_proto_library.bzl @@ -7,14 +7,35 @@ load( "@rules_proto_grpc//:defs.bzl", "ProtoPluginInfo", "proto_compile_attrs", - "proto_compile_impl", + "proto_compile", ) +def cc_nanopb_proto_compile_impl(ctx): + """Nanopb proto compile implementation to add options files.""" + extra_protoc_args = getattr(ctx.attr, "extra_protoc_args", []) + extra_protoc_files = getattr(ctx.files, "extra_protoc_files", []) + for options_target in ctx.attr.nanopb_options_files: + for options_file in options_target.files.to_list(): + extra_protoc_args = extra_protoc_args + [ + "--nanopb_plugin_opt=-f{}".format(options_file.path)] + extra_protoc_files = extra_protoc_files + [options_file] + return proto_compile(ctx, ctx.attr.options, extra_protoc_args, extra_protoc_files) + + +nanopb_proto_compile_attrs = dict( + nanopb_options_files = attr.label_list( + allow_files = [".options"], + doc = "An optional list of additional nanopb options files to apply", + ), + **proto_compile_attrs, +) + + # Create compile rule cc_nanopb_proto_compile = rule( - implementation = proto_compile_impl, + implementation = cc_nanopb_proto_compile_impl, attrs = dict( - proto_compile_attrs, + nanopb_proto_compile_attrs, _plugins = attr.label_list( providers = [ProtoPluginInfo], default = [ @@ -26,6 +47,7 @@ cc_nanopb_proto_compile = rule( toolchains = [str(Label("@rules_proto_grpc//protobuf:toolchain_type"))], ) + def cc_nanopb_proto_library(name, **kwargs): # buildifier: disable=function-docstring # Compile protos name_pb = name + "_pb" @@ -34,7 +56,7 @@ def cc_nanopb_proto_library(name, **kwargs): # buildifier: disable=function-doc **{ k: v for (k, v) in kwargs.items() - if k in proto_compile_attrs.keys() + if k in nanopb_proto_compile_attrs.keys() } # Forward args ) diff --git a/extra/bazel/nanopb_deps.bzl b/extra/bazel/nanopb_deps.bzl index 7a752e4..a87c872 100644 --- a/extra/bazel/nanopb_deps.bzl +++ b/extra/bazel/nanopb_deps.bzl @@ -32,8 +32,7 @@ def nanopb_deps(): if "rules_proto_grpc" not in native.existing_rules(): http_archive( name = "rules_proto_grpc", - patches = ["//extra/bazel:rules_proto_grpc.patch"], - sha256 = "507e38c8d95c7efa4f3b1c0595a8e8f139c885cb41a76cab7e20e4e67ae87731", - strip_prefix = "rules_proto_grpc-4.1.1", - urls = ["https://github.com/rules-proto-grpc/rules_proto_grpc/archive/4.1.1.tar.gz"], + sha256 = "b244cbede34638ad0e1aec0769f62b8348c7ed71f431a027e252a07d6adba3d6", + strip_prefix = "rules_proto_grpc-4.4.0", + urls = ["https://github.com/rules-proto-grpc/rules_proto_grpc/archive/4.4.0.tar.gz"], ) diff --git a/extra/bazel/rules_proto_grpc.patch b/extra/bazel/rules_proto_grpc.patch deleted file mode 100644 index 641f4dc..0000000 --- a/extra/bazel/rules_proto_grpc.patch +++ /dev/null @@ -1,30 +0,0 @@ -diff --git internal/compile.bzl internal/compile.bzl -index 0f060f8..2e7f960 100644 ---- internal/compile.bzl -+++ internal/compile.bzl -@@ -39,6 +39,11 @@ proto_compile_attrs = { - values = ["PREFIXED", "NO_PREFIX"], - doc = "The output mode for the target. PREFIXED (the default) will output to a directory named by the target within the current package root, NO_PREFIX will output directly to the current package. Using NO_PREFIX may lead to conflicting writes", - ), -+ "nanopb_options_file": attr.label( -+ doc = "Optional Nanopb options file for nanopb specfiically since options files couldn't be found by Bazel", -+ allow_single_file = True, -+ mandatory = False, -+ ) - } - - def proto_compile_impl(ctx): -@@ -58,10 +63,13 @@ def proto_compile_impl(ctx): - # Load attrs that we pass as args - # This is done to allow writing rules that can call proto_compile with mutable attributes, - # such as in doc_template_compile - options = ctx.attr.options - extra_protoc_args = getattr(ctx.attr, "extra_protoc_args", []) - extra_protoc_files = ctx.files.extra_protoc_files - -+ if ctx.file.nanopb_options_file: -+ extra_protoc_args = extra_protoc_args + ["--nanopb_plugin_opt=-f{}".format(ctx.file.nanopb_options_file.path)] -+ extra_protoc_files = ctx.files.extra_protoc_files + [ctx.file.nanopb_options_file] - # Execute with extracted attrs - return proto_compile(ctx, options, extra_protoc_args, extra_protoc_files) - |