// Copyright 2016 The Bazel Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.devtools.build.lib.rules.proto; import static com.google.devtools.build.lib.packages.Attribute.attr; import static com.google.devtools.build.lib.packages.BuildType.LABEL; import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST; import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.analysis.BaseRuleClasses; import com.google.devtools.build.lib.analysis.FileProvider; import com.google.devtools.build.lib.analysis.RuleDefinition; import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment; import com.google.devtools.build.lib.analysis.TransitiveInfoProvider; import com.google.devtools.build.lib.analysis.config.HostTransition; import com.google.devtools.build.lib.packages.RuleClass; import com.google.devtools.build.lib.syntax.Type; /** Implements {code proto_lang_toolchain}. */ public class ProtoLangToolchainRule implements RuleDefinition { @Override public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment environment) { return builder /* This value will be passed to proto-compiler to generate the code. Only include the parts specific to this code-generator/plugin (e.g., do not include -I parameters)
$(OUT)
is LANG_proto_library-specific. The rules are expected to define
how they interpret this variable. For Java, for example, $(OUT) will be replaced with
the src-jar filename to create.$(PLUGIN_out)
will be substituted to work with a
`--plugin=protoc-gen-PLUGIN` command line.--plugin=protoc-gen-PLUGIN=.
*/
.add(attr("plugin", LABEL).exec().cfg(HostTransition.INSTANCE).allowedFileTypes())
/*
A language-specific library that the generated code is compiled against.
The exact behavior is LANG_proto_librar-specific.
Java, for example, should compile against the runtime.
*/
.add(attr("runtime", LABEL).allowedFileTypes())
/*
No code will be generated for files in the srcs
attribute of
blacklisted_protos
.
This is used for .proto files that are already linked into proto runtimes, such as
any.proto
.
*/
.add(
attr("blacklisted_protos", LABEL_LIST)
.allowedFileTypes()
.mandatoryNativeProviders(
ImmutableList.
Specifies how a LANG_proto_library rule (e.g., java_proto_library
) should invoke the
proto-compiler.
Some LANG_proto_library rules allow specifying which toolchain to use using command-line flags;
consult their documentation.
Normally you should not write those kind of rules unless you want to tune your Java compiler.
There's no compiler. The proto-compiler is taken from the proto_library rule we attach to. It is
passed as a command-line flag to Blaze.
Several features require a proto-compiler to be invoked on the proto_library rule itself.
It's beneficial to enforce the compiler that LANG_proto_library uses is the same as the one
proto_library
does.
A simple example would be:
proto_lang_toolchain( name = "javalite_toolchain", command_line = "--$(PLUGIN_OUT)=shared,immutable:$(OUT)", plugin = ":javalite_plugin", runtime = ":protobuf_lite", )*/