// Copyright 2015 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 static com.google.devtools.build.lib.syntax.Type.STRING; import com.google.devtools.build.lib.analysis.BaseRuleClasses; import com.google.devtools.build.lib.analysis.RuleDefinition; import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment; import com.google.devtools.build.lib.analysis.config.HostTransition; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.packages.Attribute; import com.google.devtools.build.lib.packages.RuleClass; import com.google.devtools.build.lib.util.FileType; /** * Rule definition for the proto_library rule. */ public final class BazelProtoLibraryRule implements RuleDefinition { private static final Label DEFAULT_PROTO_COMPILER = Label.parseAbsoluteUnchecked("@com_google_protobuf//:protoc"); private static final Attribute.LabelLateBoundDefault PROTO_COMPILER = Attribute.LabelLateBoundDefault.fromTargetConfiguration( ProtoConfiguration.class, DEFAULT_PROTO_COMPILER, (rule, attributes, protoConfig) -> protoConfig.protoCompiler() != null ? protoConfig.protoCompiler() : DEFAULT_PROTO_COMPILER); @Override public RuleClass build(RuleClass.Builder builder, final RuleDefinitionEnvironment env) { return builder .requiresConfigurationFragments(ProtoConfiguration.class) .setOutputToGenfiles() .add(attr(":proto_compiler", LABEL) .cfg(HostTransition.INSTANCE) .exec() .value(PROTO_COMPILER)) /* The list of other proto_library rules that the target depends upon. A proto_library may only depend on other proto_library targets. It may not depend on language-specific libraries. */ .override(attr("deps", LABEL_LIST).allowedRuleClasses("proto_library").allowedFileTypes()) /* The list of .proto and .protodevel files that are processed to create the target. This is usually a non empty list. One usecase where srcs can be empty is an alias-library. This is a proto_library rule having one or more other proto_library in deps. This pattern can be used to e.g. export a public api under a persistent name. */ .add( attr("srcs", LABEL_LIST) .direct_compile_time_input() .allowedFileTypes(FileType.of(".proto"), FileType.of(".protodevel"))) /* Directory containing .proto files. If set, it must be equal to the package name. If not set, the source root will be the workspace directory (default). */ .add(attr("proto_source_root", STRING)) .advertiseProvider(ProtoSourcesProvider.class, ProtoSupportDataProvider.class) .build(); } @Override public Metadata getMetadata() { return RuleDefinition.Metadata.builder() .name("proto_library") .ancestors(BaseRuleClasses.RuleBase.class) .factoryClass(BazelProtoLibrary.class) .build(); } } /*

Use proto_library to define libraries of protocol buffers which may be used from multiple languages. A proto_library may be listed in the deps clause of supported rules, such as java_proto_library.

When compiled on the command-line, a proto_library creates a file named foo-descriptor-set.proto.bin, which is the descriptor set for the messages the rule srcs. The file is a serialized FileDescriptorSet, which is described in https://developers.google.com/protocol-buffers/docs/techniques#self-description.

It only contains information about the .proto files directly mentioned by a proto_library rule; the collection of transitive descriptor sets is available through the proto.transitive_descriptor_sets Skylark provider. See documentation in ProtoSourcesProvider.java.

*/