Test disabling aspect on protos attr to compile rules
These rules provide Protocol Buffers (Protobuf) and gRPC rules for a range of languages and services.
Each supported language ({lang} below) is generally split into four rule flavours:
{lang}_proto_compile: Provides generated files from the Protobuf protoc plugin for the language. e.g for C++ this provides the generated *.pb.cc and *.pb.h files.
{lang}_proto_library: Provides a language-specific library from the generated Protobuf protoc plugin outputs, along with necessary dependencies. e.g for C++ this provides a Bazel native cpp_library created from the generated *.pb.cc and *pb.h files, with the Protobuf library linked. For languages that do not have a ‘library’ concept, this rule may not exist.
{lang}_grpc_compile: Provides generated files from both the Protobuf and gRPC protoc plugins for the language. e.g for C++ this provides the generated *.pb.cc, *.grpc.pb.cc, *.pb.h and *.grpc.pb.h files.
{lang}_grpc_library: Provides a language-specific library from the generated Protobuf and gRPC protoc plugins outputs, along with necessary dependencies. e.g for C++ this provides a Bazel native cpp_library created from the generated *.pb.cc, *.grpc.pb.cc, *.pb.h and *.grpc.pb.h files, with the Protobuf and gRPC libraries linked. For languages that do not have a ‘library’ concept, this rule may not exist.
Therefore, if you are solely interested in the generated source code artifacts, use the {lang}_{proto|grpc}_compile rules. Otherwise, if you want a ready-to-go library, use the {lang}_{proto|grpc}_library rules.
Add rules_proto_grpc to your WORKSPACE file and then look at the language specific examples linked below:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "rules_proto_grpc", urls = ["https://github.com/rules-proto-grpc/rules_proto_grpc/archive/2.0.0.tar.gz"], sha256 = "d771584bbff98698e7cb3cb31c132ee206a972569f4dc8b65acbdd934d156b33", strip_prefix = "rules_proto_grpc-2.0.0", ) load("@rules_proto_grpc//:repositories.bzl", "rules_proto_grpc_toolchains", "rules_proto_grpc_repos") rules_proto_grpc_toolchains() rules_proto_grpc_repos() load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains") rules_proto_dependencies() rules_proto_toolchains()
It is recommended that you use the tagged releases for stable rules. Master is intended to be ‘ready-to-use’, but may be unstable at certain periods. To be notified of new releases, you can use GitHub's ‘Watch Releases Only’ on the repository.
Important: You will also need to follow instructions in the language-specific
README.mdfor additional workspace dependencies that may be required.
| Language | Rule | Description |
|---|---|---|
| Android | android_proto_compile | Generates an Android protobuf .jar artifact (example) |
| Android | android_grpc_compile | Generates Android protobuf+gRPC .jar artifacts (example) |
| Android | android_proto_library | Generates an Android protobuf library using android_library from rules_android (example) |
| Android | android_grpc_library | Generates Android protobuf+gRPC library using android_library from rules_android (example) |
| C++ | cpp_proto_compile | Generates C++ protobuf .h & .cc artifacts (example) |
| C++ | cpp_grpc_compile | Generates C++ protobuf+gRPC .h & .cc artifacts (example) |
| C++ | cpp_proto_library | Generates a C++ protobuf library using cc_library, with dependencies linked (example) |
| C++ | cpp_grpc_library | Generates a C++ protobuf+gRPC library using cc_library, with dependencies linked (example) |
| C# | csharp_proto_compile | Generates C# protobuf .cs artifacts (example) |
| C# | csharp_grpc_compile | Generates C# protobuf+gRPC .cs artifacts (example) |
| C# | csharp_proto_library | Generates a C# protobuf library using core_library from rules_dotnet. Note that the library name must end in .dll (example) |
| C# | csharp_grpc_library | Generates a C# protobuf+gRPC library using core_library from rules_dotnet. Note that the library name must end in .dll (example) |
| D | d_proto_compile | Generates D protobuf .d artifacts (example) |
| D | d_proto_library | Generates a D protobuf library using d_library from rules_d (example) |
| Go | go_proto_compile | Generates Go protobuf .go artifacts (example) |
| Go | go_grpc_compile | Generates Go protobuf+gRPC .go artifacts (example) |
| Go | go_proto_library | Generates a Go protobuf library using go_library from rules_go (example) |
| Go | go_grpc_library | Generates a Go protobuf+gRPC library using go_library from rules_go (example) |
| grpc-gateway | gateway_grpc_compile | Generates grpc-gateway .go files (example) |
| grpc-gateway | gateway_openapiv2_compile | Generates grpc-gateway OpenAPI v2 .json files (example) |
| grpc-gateway | gateway_grpc_library | Generates grpc-gateway library files (example) |
| Java | java_proto_compile | Generates a Java protobuf srcjar artifact (example) |
| Java | java_grpc_compile | Generates a Java protobuf+gRPC srcjar artifact (example) |
| Java | java_proto_library | Generates a Java protobuf library using java_library (example) |
| Java | java_grpc_library | Generates a Java protobuf+gRPC library using java_library (example) |
| JavaScript | js_proto_compile | Generates JavaScript protobuf .js and .d.ts artifacts (example) |
| JavaScript | js_grpc_node_compile | Generates JavaScript protobuf + gRPC-node .js and .d.ts artifacts (example) |
| JavaScript | js_grpc_web_compile | Generates JavaScript protobuf + gRPC-Web .js and .d.ts artifacts (example) |
| JavaScript | js_proto_library | Generates a JavaScript protobuf library using js_library from rules_nodejs (example) |
| JavaScript | js_grpc_node_library | Generates a Node.js protobuf + gRPC-node library using js_library from rules_nodejs (example) |
| JavaScript | js_grpc_web_library | Generates a JavaScript protobuf + gRPC-Web library using js_library from rules_nodejs (example) |
| Objective-C | objc_proto_compile | Generates Objective-C protobuf .m & .h artifacts (example) |
| Objective-C | objc_grpc_compile | Generates Objective-C protobuf+gRPC .m & .h artifacts (example) |
| Objective-C | objc_proto_library | Generates an Objective-C protobuf library using objc_library (example) |
| PHP | php_proto_compile | Generates PHP protobuf .php artifacts (example) |
| PHP | php_grpc_compile | Generates PHP protobuf+gRPC .php artifacts (example) |
| Python | python_proto_compile | Generates Python protobuf .py artifacts (example) |
| Python | python_grpc_compile | Generates Python protobuf+gRPC .py artifacts (example) |
| Python | python_grpclib_compile | Generates Python protobuf+grpclib .py artifacts (supports Python 3 only) (example) |
| Python | python_proto_library | Generates a Python protobuf library using py_library from rules_python (example) |
| Python | python_grpc_library | Generates a Python protobuf+gRPC library using py_library from rules_python (example) |
| Python | python_grpclib_library | Generates a Python protobuf+grpclib library using py_library from rules_python (supports Python 3 only) (example) |
| Ruby | ruby_proto_compile | Generates Ruby protobuf .rb artifacts (example) |
| Ruby | ruby_grpc_compile | Generates Ruby protobuf+gRPC .rb artifacts (example) |
| Ruby | ruby_proto_library | Generates a Ruby protobuf library using ruby_library from rules_ruby (example) |
| Ruby | ruby_grpc_library | Generates a Ruby protobuf+gRPC library using ruby_library from rules_ruby (example) |
| Rust | rust_proto_compile | Generates Rust protobuf .rs artifacts (example) |
| Rust | rust_grpc_compile | Generates Rust protobuf+gRPC .rs artifacts (example) |
| Rust | rust_proto_library | Generates a Rust protobuf library using rust_library from rules_rust (example) |
| Rust | rust_grpc_library | Generates a Rust protobuf+gRPC library using rust_library from rules_rust (example) |
| Scala | scala_proto_compile | Generates a Scala protobuf .jar artifact (example) |
| Scala | scala_grpc_compile | Generates Scala protobuf+gRPC .jar artifacts (example) |
| Scala | scala_proto_library | Generates a Scala protobuf library using scala_library from rules_scala (example) |
| Scala | scala_grpc_library | Generates a Scala protobuf+gRPC library using scala_library from rules_scala (example) |
| Swift | swift_proto_compile | Generates Swift protobuf .swift artifacts (example) |
| Swift | swift_grpc_compile | Generates Swift protobuf+gRPC .swift artifacts (example) |
| Swift | swift_proto_library | Generates a Swift protobuf library using swift_library from rules_swift (example) |
| Swift | swift_grpc_library | Generates a Swift protobuf+gRPC library using swift_library from rules_swift (example) |
These steps walk through the actions required to go from a raw .proto file to a C++ library. Other languages will have a similar high-level layout.
Step 1: Write a Protocol Buffer .proto file (example: thing.proto):
syntax = "proto3"; package example; import "google/protobuf/any.proto"; message Thing { string name = 1; google.protobuf.Any payload = 2; }
Step 2: Write a BAZEL.build file with a proto_library target:
proto_library( name = "thing_proto", srcs = ["thing.proto"], deps = ["@com_google_protobuf//:any_proto"], )
In this example we have a dependency on a well-known type any.proto, hence the proto_library to proto_library dependency ("@com_google_protobuf//:any_proto")
Step 3: Add a cpp_proto_compile target
NOTE: In this example
thing.protodoes not include service definitions (gRPC). For protos with services, use thecpp_grpc_compilerule instead.
# BUILD.bazel load("@rules_proto_grpc//cpp:defs.bzl", "cpp_proto_compile") cpp_proto_compile( name = "cpp_thing_proto", protos = [":thing_proto"], )
But wait, before we can build this, we need to load the dependencies necessary for this rule (see cpp/README.md):
Step 4: Load the workspace macro corresponding to the build rule.
# WORKSPACE load("@rules_proto_grpc//cpp:repositories.bzl", "cpp_repos") cpp_repos()
We're now ready to build the target:
Step 5: Build it!
$ bazel build //example/proto:cpp_thing_proto Target //example/proto:cpp_thing_proto up-to-date: bazel-genfiles/example/proto/cpp_thing_proto/example/proto/thing.pb.h bazel-genfiles/example/proto/cpp_thing_proto/example/proto/thing.pb.cc
If we were only interested in the generated file artifacts, the cpp_grpc_compile rule would be fine. However, for convenience we‘d rather have the outputs compiled into a C++ library. To do that, let’s change the rule from cpp_proto_compile to cpp_proto_library:
# BUILD.bazel load("@rules_proto_grpc//cpp:defs.bzl", "cpp_proto_library") cpp_proto_library( name = "cpp_thing_proto", protos = [":thing_proto"], )
$ bazel build //example/proto:cpp_thing_proto Target //example/proto:cpp_thing_proto up-to-date: bazel-bin/example/proto/libcpp_thing_proto.a bazel-bin/example/proto/libcpp_thing_proto.so bazel-genfiles/example/proto/cpp_thing_proto/example/proto/thing.pb.h bazel-genfiles/example/proto/cpp_thing_proto/example/proto/thing.pb.cc
This way, we can use //example/proto:cpp_thing_proto as a dependency of any other cc_library or cc_binary target as per normal.
NOTE: The
cpp_proto_librarytarget implicitly callscpp_proto_compile, and we can access that rule's by adding_pbat the end of the target name, likebazel build //example/proto:cpp_thing_proto_pb
Each language {lang} has a top-level subdirectory that contains:
{lang}/README.md: Generated documentation for the language rules
{lang}/repositories.bzl: Macro functions that declare repository rule dependencies for that language
{lang}/{rule}.bzl: Rule implementations of the form {lang}_{kind}_{type}, where kind is one of proto|grpc and type is one of compile|library
{lang}/BUILD.bazel: proto_plugin() declarations for the available plugins for the language
example/{lang}/{rule}/: Generated WORKSPACE and BUILD.bazel demonstrating standalone usage of the rules
{lang}/example/routeguide/: Example routeguide example implementation, if possible
To help maintain consistency of the rule implementations and documentation, all of the rule implementations are generated by the tool //tools/rulegen. Changes in the main README.md should be placed in tools/rulegen/README.header.md or tools/rulegen/README.footer.md. Changes to generated rules should be put in the source files (example: tools/rulegen/java.go).
Generally, follow the pattern seen in the multiple language examples in this repository. The basic idea is:
load("@rules_proto_grpc//:defs.bzl", "proto_plugin")name, options (not mandatory), tool and outputs. tool is a label that refers to the binary executable for the plugin itselfoutputs: A list of strings patterns that predicts the pattern of files generated by the plugin. For plugins that produce one output file per input proto fileout: The name of a single output file generated by the pluginoutput_directory: Set to true if your plugin generates files in a non-predictable way. e.g. if the output paths depend on the service names within the filesload("@rules_proto//proto:defs.bzl", "ProtoInfo") load( "@rules_proto_grpc//:defs.bzl", "ProtoLibraryAspectNodeInfo", "ProtoPluginInfo", "proto_compile_aspect_attrs", "proto_compile_aspect_impl", "proto_compile_attrs", "proto_compile_impl", ) # Create aspect example_aspect = aspect( implementation = proto_compile_aspect_impl, provides = [ProtoLibraryAspectNodeInfo], attr_aspects = ["deps"], attrs = dict( proto_compile_aspect_attrs, _plugins = attr.label_list( doc = "List of protoc plugins to apply", providers = [ProtoPluginInfo], default = [ Label("//<LABEL OF YOUR PLUGIN>"), ], ), _prefix = attr.string( doc = "String used to disambiguate aspects when generating outputs", default = "example_aspect", ), ), toolchains = ["@rules_proto_grpc//protobuf:toolchain_type"], ) # Create compile rule to apply aspect _rule = rule( implementation = proto_compile_impl, attrs = dict( proto_compile_attrs, protos = attr.label_list( mandatory = False, # TODO: set to true in 4.0.0 when deps removed below providers = [ProtoInfo, ProtoLibraryAspectNodeInfo], doc = "List of labels that provide a ProtoInfo (such as rules_proto proto_library)", ), deps = attr.label_list( mandatory = False, providers = [ProtoInfo, ProtoLibraryAspectNodeInfo], aspects = [example_aspect], doc = "DEPRECATED: Use protos attr", ), _plugins = attr.label_list( doc = "List of protoc plugins to apply", providers = [ProtoPluginInfo], default = [ Label("//<LABEL OF YOUR PLUGIN>"), ], ), ), toolchains = [str(Label("//protobuf:toolchain_type"))], ) # Create macro for converting attrs and passing to compile def example_compile(**kwargs): _rule( verbose_string = "{}".format(kwargs.get("verbose", 0)), **kwargs )
This project is derived from stackb/rules_proto under the Apache 2.0 license and this project therefore maintains the terms of that license
Contributions are very welcome. Please see CONTRIBUTING.md for further details.