-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
ProtoInfo.java
241 lines (216 loc) · 9.82 KB
/
ProtoInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2014 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 com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.packages.BuiltinProvider;
import com.google.devtools.build.lib.packages.NativeInfo;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.skylarkbuildapi.ProtoInfoApi;
import com.google.devtools.build.lib.skylarkbuildapi.proto.ProtoBootstrap;
import com.google.devtools.build.lib.syntax.Depset;
import com.google.devtools.build.lib.syntax.SkylarkType;
import com.google.devtools.build.lib.util.Pair;
import javax.annotation.Nullable;
/**
* Configured target classes that implement this class can contribute .proto files to the
* compilation of proto_library rules.
*/
@Immutable
@AutoCodec
public final class ProtoInfo extends NativeInfo implements ProtoInfoApi<Artifact> {
/** Provider class for {@link ProtoInfo} objects. */
public static class Provider extends BuiltinProvider<ProtoInfo> implements ProtoInfoApi.Provider {
public Provider() {
super(ProtoBootstrap.PROTO_INFO_STARLARK_NAME, ProtoInfo.class);
}
}
public static final Provider PROVIDER = new Provider();
/**
* The name of the field in Skylark used to access this class.
*
* <p>This is for legacy {@code ctx.attr.deps.proto.}-style access. Those should eventually be
* migrated to {@code ctx.attr.deps[ProtoInfo]}.
*/
public static final String LEGACY_SKYLARK_NAME = "proto";
private final ImmutableList<Artifact> directProtoSources;
private final ImmutableList<Artifact> originalDirectProtoSources;
private final String directProtoSourceRoot;
private final NestedSet<Artifact> transitiveProtoSources;
private final NestedSet<String> transitiveProtoSourceRoots;
private final NestedSet<Artifact> strictImportableProtoSourcesForDependents;
private final NestedSet<Pair<Artifact, String>> strictImportableProtoSourcesImportPaths;
private final NestedSet<Pair<Artifact, String>>
strictImportableProtoSourcesImportPathsForDependents;
private final NestedSet<String> strictImportableProtoSourceRoots;
private final NestedSet<Pair<Artifact, String>> exportedProtoSourcesImportPaths;
private final NestedSet<String> exportedProtoSourceRoots;
private final Artifact directDescriptorSet;
private final NestedSet<Artifact> transitiveDescriptorSets;
@AutoCodec.Instantiator
public ProtoInfo(
ImmutableList<Artifact> directProtoSources,
ImmutableList<Artifact> originalDirectProtoSources,
String directProtoSourceRoot,
NestedSet<Artifact> transitiveProtoSources,
NestedSet<String> transitiveProtoSourceRoots,
NestedSet<Artifact> strictImportableProtoSourcesForDependents,
NestedSet<Pair<Artifact, String>> strictImportableProtoSourcesImportPaths,
NestedSet<Pair<Artifact, String>> strictImportableProtoSourcesImportPathsForDependents,
NestedSet<String> strictImportableProtoSourceRoots,
NestedSet<Pair<Artifact, String>> exportedProtoSourcesImportPaths,
NestedSet<String> exportedProtoSourceRoots,
Artifact directDescriptorSet,
NestedSet<Artifact> transitiveDescriptorSets,
Location location) {
super(PROVIDER, location);
this.directProtoSources = directProtoSources;
this.originalDirectProtoSources = originalDirectProtoSources;
this.directProtoSourceRoot = directProtoSourceRoot;
this.transitiveProtoSources = transitiveProtoSources;
this.transitiveProtoSourceRoots = transitiveProtoSourceRoots;
this.strictImportableProtoSourcesForDependents = strictImportableProtoSourcesForDependents;
this.strictImportableProtoSourcesImportPaths = strictImportableProtoSourcesImportPaths;
this.strictImportableProtoSourcesImportPathsForDependents =
strictImportableProtoSourcesImportPathsForDependents;
this.strictImportableProtoSourceRoots = strictImportableProtoSourceRoots;
this.exportedProtoSourcesImportPaths = exportedProtoSourcesImportPaths;
this.exportedProtoSourceRoots = exportedProtoSourceRoots;
this.directDescriptorSet = directDescriptorSet;
this.transitiveDescriptorSets = transitiveDescriptorSets;
}
/**
* The proto source files that are used in compiling this {@code proto_library}.
*
* <p>Different from {@link #getOriginalDirectProtoSources()} when a virtual import root is used.
*/
@Override
public ImmutableList<Artifact> getDirectProtoSources() {
return directProtoSources;
}
/** The proto sources of the {@code proto_library} declaring this provider. */
public ImmutableList<Artifact> getOriginalDirectProtoSources() {
return originalDirectProtoSources;
}
/** The source root of the current library. */
@Override
public String getDirectProtoSourceRoot() {
return directProtoSourceRoot;
}
/** The proto sources in the transitive closure of this rule. */
@Override
public Depset /*<Artifact>*/ getTransitiveProtoSourcesForStarlark() {
return Depset.of(Artifact.TYPE, getTransitiveProtoSources());
}
public NestedSet<Artifact> getTransitiveProtoSources() {
return transitiveProtoSources;
}
/**
* The proto source roots of the transitive closure of this rule. These flags will be passed to
* {@code protoc} in the specified order, via the {@code --proto_path} flag.
*/
@Override
public Depset /*<String>*/ getTransitiveProtoSourceRootsForStarlark() {
return Depset.of(SkylarkType.STRING, transitiveProtoSourceRoots);
}
public NestedSet<String> getTransitiveProtoSourceRoots() {
return transitiveProtoSourceRoots;
}
@Deprecated
@Override
public Depset /*<Artifact>*/ getTransitiveImports() {
return getTransitiveProtoSourcesForStarlark();
}
/**
* Returns the set of source files importable by rules directly depending on the rule declaring
* this provider if strict dependency checking is in effect.
*
* <p>(strict dependency checking: when a target can only include / import source files from its
* direct dependencies, but not from transitive ones)
*/
@Override
public Depset /*<Artifact>*/ getStrictImportableProtoSourcesForDependentsForStarlark() {
return Depset.of(Artifact.TYPE, strictImportableProtoSourcesForDependents);
}
public NestedSet<Artifact> getStrictImportableProtoSourcesForDependents() {
return strictImportableProtoSourcesForDependents;
}
/**
* Returns the set of source files and import paths importable by rules directly depending on the
* rule declaring this provider if strict dependency checking is in effect.
*
* <p>(strict dependency checking: when a target can only include / import source files from its
* direct dependencies, but not from transitive ones)
*/
public NestedSet<Pair<Artifact, String>>
getStrictImportableProtoSourcesImportPathsForDependents() {
return strictImportableProtoSourcesImportPathsForDependents;
}
/**
* Returns the set of source files importable by the rule declaring this provider if strict
* dependency checking is in effect.
*
* <p>(strict dependency checking: when a target can only include / import source files from its
* direct dependencies, but not from transitive ones)
*/
public NestedSet<Pair<Artifact, String>> getStrictImportableProtoSourcesImportPaths() {
return strictImportableProtoSourcesImportPaths;
}
/**
* Returns the proto source roots of the dependencies whose sources can be imported if strict
* dependency checking is in effect.
*
* <p>(strict dependency checking: when a target can only include / import source files from its
* direct dependencies, but not from transitive ones)
*/
public NestedSet<String> getStrictImportableProtoSourceRoots() {
return strictImportableProtoSourceRoots;
}
/**
* Returns the .proto files that are the direct srcs of the exported dependencies of this rule.
*/
@Nullable
public NestedSet<Pair<Artifact, String>> getExportedProtoSourcesImportPaths() {
return exportedProtoSourcesImportPaths;
}
public NestedSet<String> getExportedProtoSourceRoots() {
return exportedProtoSourceRoots;
}
/**
* Be careful while using this artifact - it is the parsing of the transitive set of .proto files.
* It's possible to cause a O(n^2) behavior, where n is the length of a proto chain-graph.
* (remember that proto-compiler reads all transitive .proto files, even when producing the
* direct-srcs descriptor set)
*/
@Override
public Artifact getDirectDescriptorSet() {
return directDescriptorSet;
}
/**
* Be careful while using this artifact - it is the parsing of the transitive set of .proto files.
* It's possible to cause a O(n^2) behavior, where n is the length of a proto chain-graph.
* (remember that proto-compiler reads all transitive .proto files, even when producing the
* direct-srcs descriptor set)
*/
@Override
public Depset /*<Artifact>*/ getTransitiveDescriptorSetsForStarlark() {
return Depset.of(Artifact.TYPE, transitiveDescriptorSets);
}
public NestedSet<Artifact> getTransitiveDescriptorSets() {
return transitiveDescriptorSets;
}
}