blob: d7828f909c40f41a818de446246a12f3dc1b11f8 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.devtools.build.lib.analysis;
16
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +000017import com.google.common.collect.ImmutableList;
Klaus Aehlig4901d8b2017-04-10 15:13:59 +000018import com.google.common.collect.ImmutableSet;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010019import com.google.common.collect.Iterables;
Jakob Buchgraberfb646092017-02-27 18:53:25 +000020import com.google.devtools.build.lib.actions.Artifact;
Klaus Aehlig4901d8b2017-04-10 15:13:59 +000021import com.google.devtools.build.lib.actions.EventReportingArtifacts;
Jakob Buchgraberfb646092017-02-27 18:53:25 +000022import com.google.devtools.build.lib.analysis.TopLevelArtifactHelper.ArtifactsInOutputGroup;
Klaus Aehlig71c993b2017-05-09 07:49:46 -040023import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
gregce593f7f92017-09-19 02:02:21 +020024import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget;
Klaus Aehligcba24ab2017-09-27 05:14:11 -040025import com.google.devtools.build.lib.analysis.test.InstrumentedFilesProvider;
ulfjackab21d182017-08-10 15:36:14 +020026import com.google.devtools.build.lib.analysis.test.TestProvider;
Klaus Aehlig4901d8b2017-04-10 15:13:59 +000027import com.google.devtools.build.lib.buildeventstream.ArtifactGroupNamer;
Klaus Aehliga708a022017-07-11 12:54:40 +020028import com.google.devtools.build.lib.buildeventstream.BuildEvent;
aehlig617bb892017-04-04 15:03:23 +000029import com.google.devtools.build.lib.buildeventstream.BuildEventConverters;
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +000030import com.google.devtools.build.lib.buildeventstream.BuildEventId;
31import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos;
Klaus Aehlig0a6cd622017-04-21 17:09:40 +020032import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.File;
Jakob Buchgraberfb646092017-02-27 18:53:25 +000033import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.OutputGroup;
Klaus Aehliga708a022017-07-11 12:54:40 +020034import com.google.devtools.build.lib.buildeventstream.BuildEventWithConfiguration;
Klaus Aehlig74d716b2016-11-23 12:38:24 +000035import com.google.devtools.build.lib.buildeventstream.BuildEventWithOrderConstraint;
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +000036import com.google.devtools.build.lib.buildeventstream.GenericBuildEvent;
Klaus Aehligcba24ab2017-09-27 05:14:11 -040037import com.google.devtools.build.lib.buildeventstream.TestFileNameConstants;
Klaus Aehligd1f4a162016-10-25 14:51:55 +000038import com.google.devtools.build.lib.causes.Cause;
Klaus Aehlig1f452c32017-02-02 10:19:15 +000039import com.google.devtools.build.lib.cmdline.Label;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010040import com.google.devtools.build.lib.collect.nestedset.NestedSet;
41import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
Klaus Aehlig4901d8b2017-04-10 15:13:59 +000042import com.google.devtools.build.lib.collect.nestedset.NestedSetView;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010043import com.google.devtools.build.lib.collect.nestedset.Order;
Jakob Buchgraberfb646092017-02-27 18:53:25 +000044import com.google.devtools.build.lib.packages.AttributeMap;
Klaus Aehlig10da4662017-06-27 15:36:27 +020045import com.google.devtools.build.lib.packages.TestSize;
Jakob Buchgraberfb646092017-02-27 18:53:25 +000046import com.google.devtools.build.lib.syntax.Type;
Mark Schaller6df81792015-12-10 18:47:47 +000047import com.google.devtools.build.lib.util.Preconditions;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010048import com.google.devtools.build.skyframe.SkyValue;
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +000049import java.util.Collection;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010050
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +000051/** This event is fired as soon as a target is either built or fails. */
Klaus Aehlig4901d8b2017-04-10 15:13:59 +000052public final class TargetCompleteEvent
Klaus Aehlig71c993b2017-05-09 07:49:46 -040053 implements SkyValue,
54 BuildEventWithOrderConstraint,
55 EventReportingArtifacts,
56 BuildEventWithConfiguration {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010057 private final ConfiguredTarget target;
Klaus Aehligd1f4a162016-10-25 14:51:55 +000058 private final NestedSet<Cause> rootCauses;
Klaus Aehlig74d716b2016-11-23 12:38:24 +000059 private final Collection<BuildEventId> postedAfter;
Jakob Buchgraberfb646092017-02-27 18:53:25 +000060 private final Iterable<ArtifactsInOutputGroup> outputs;
Klaus Aehligcba24ab2017-09-27 05:14:11 -040061 private final NestedSet<Artifact> baselineCoverageArtifacts;
Klaus Aehlig6f978502016-11-30 12:12:56 +000062 private final boolean isTest;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010063
Klaus Aehlig6f978502016-11-30 12:12:56 +000064 private TargetCompleteEvent(
Jakob Buchgraberfb646092017-02-27 18:53:25 +000065 ConfiguredTarget target,
66 NestedSet<Cause> rootCauses,
67 Iterable<ArtifactsInOutputGroup> outputs,
68 boolean isTest) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010069 this.target = target;
Klaus Aehligd1f4a162016-10-25 14:51:55 +000070 this.rootCauses =
71 (rootCauses == null) ? NestedSetBuilder.<Cause>emptySet(Order.STABLE_ORDER) : rootCauses;
Klaus Aehlig74d716b2016-11-23 12:38:24 +000072
73 ImmutableList.Builder postedAfterBuilder = ImmutableList.builder();
74 for (Cause cause : getRootCauses()) {
75 postedAfterBuilder.add(BuildEventId.fromCause(cause));
76 }
77 this.postedAfter = postedAfterBuilder.build();
Jakob Buchgraberfb646092017-02-27 18:53:25 +000078 this.outputs = outputs;
Klaus Aehlig6f978502016-11-30 12:12:56 +000079 this.isTest = isTest;
Klaus Aehligcba24ab2017-09-27 05:14:11 -040080 InstrumentedFilesProvider instrumentedFilesProvider =
81 this.target.getProvider(InstrumentedFilesProvider.class);
82 if (instrumentedFilesProvider == null) {
83 this.baselineCoverageArtifacts = null;
84 } else {
85 NestedSet<Artifact> baselineCoverageArtifacts =
86 instrumentedFilesProvider.getBaselineCoverageArtifacts();
87 if (!baselineCoverageArtifacts.isEmpty()) {
88 this.baselineCoverageArtifacts = baselineCoverageArtifacts;
89 } else {
90 this.baselineCoverageArtifacts = null;
91 }
92 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010093 }
94
Klaus Aehlig6f978502016-11-30 12:12:56 +000095 /** Construct a successful target completion event. */
Jakob Buchgraberfb646092017-02-27 18:53:25 +000096 public static TargetCompleteEvent createSuccessfulTarget(
97 ConfiguredTarget ct, NestedSet<ArtifactsInOutputGroup> outputs) {
98 return new TargetCompleteEvent(ct, null, outputs, false);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010099 }
100
Klaus Aehlig6f978502016-11-30 12:12:56 +0000101 /** Construct a successful target completion event for a target that will be tested. */
102 public static TargetCompleteEvent createSuccessfulTestTarget(ConfiguredTarget ct) {
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000103 return new TargetCompleteEvent(ct, null, ImmutableList.<ArtifactsInOutputGroup>of(), true);
Klaus Aehlig6f978502016-11-30 12:12:56 +0000104 }
105
106
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100107 /**
108 * Construct a target completion event for a failed target, with the given non-empty root causes.
109 */
Klaus Aehligd1f4a162016-10-25 14:51:55 +0000110 public static TargetCompleteEvent createFailed(ConfiguredTarget ct, NestedSet<Cause> rootCauses) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100111 Preconditions.checkArgument(!Iterables.isEmpty(rootCauses));
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000112 return new TargetCompleteEvent(
113 ct, rootCauses, ImmutableList.<ArtifactsInOutputGroup>of(), false);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100114 }
115
116 /**
117 * Returns the target associated with the event.
118 */
119 public ConfiguredTarget getTarget() {
120 return target;
121 }
122
123 /**
124 * Determines whether the target has failed or succeeded.
125 */
126 public boolean failed() {
127 return !rootCauses.isEmpty();
128 }
129
Klaus Aehligd1f4a162016-10-25 14:51:55 +0000130 /** Get the root causes of the target. May be empty. */
131 public Iterable<Cause> getRootCauses() {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100132 return rootCauses;
133 }
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +0000134
135 @Override
136 public BuildEventId getEventId() {
Klaus Aehligebec9252017-06-27 17:29:50 +0200137 BuildConfiguration config = getTarget().getConfiguration();
138 BuildEventId configId =
139 config == null ? BuildEventId.nullConfigurationId() : config.getEventId();
140 return BuildEventId.targetCompleted(getTarget().getLabel(), configId);
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +0000141 }
142
143 @Override
144 public Collection<BuildEventId> getChildrenEvents() {
145 ImmutableList.Builder childrenBuilder = ImmutableList.builder();
146 for (Cause cause : getRootCauses()) {
147 childrenBuilder.add(BuildEventId.fromCause(cause));
148 }
Klaus Aehlig6f978502016-11-30 12:12:56 +0000149 if (isTest) {
Klaus Aehlig1f452c32017-02-02 10:19:15 +0000150 // For tests, announce all the test actions that will minimally happen (except for
151 // interruption). If after the result of a test action another attempt is necessary,
152 // it will be announced with the action that made the new attempt necessary.
153 Label label = target.getTarget().getLabel();
154 TestProvider.TestParams params = target.getProvider(TestProvider.class).getTestParams();
155 for (int run = 0; run < Math.max(params.getRuns(), 1); run++) {
156 for (int shard = 0; shard < Math.max(params.getShards(), 1); shard++) {
Klaus Aehligae5c14c2017-05-09 08:27:38 -0400157 childrenBuilder.add(
158 BuildEventId.testResult(label, run, shard, target.getConfiguration().getEventId()));
Klaus Aehlig1f452c32017-02-02 10:19:15 +0000159 }
160 }
Klaus Aehligae5c14c2017-05-09 08:27:38 -0400161 childrenBuilder.add(BuildEventId.testSummary(label, target.getConfiguration().getEventId()));
Klaus Aehlig8d253622016-11-02 17:24:46 +0000162 }
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +0000163 return childrenBuilder.build();
164 }
165
Klaus Aehligcba24ab2017-09-27 05:14:11 -0400166 // TODO(aehlig): remove as soon as we managed to get rid of the deprecated "important_output"
167 // field.
168 private static void addImportantOutputs(
169 BuildEventStreamProtos.TargetComplete.Builder builder,
170 BuildEventConverters converters,
171 Iterable<Artifact> artifacts) {
172 for (Artifact artifact : artifacts) {
173 String name = artifact.getPath().relativeTo(artifact.getRoot().getPath()).getPathString();
174 String uri = converters.pathConverter().apply(artifact.getPath());
175 builder.addImportantOutput(File.newBuilder().setName(name).setUri(uri).build());
176 }
177 }
178
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +0000179 @Override
aehlig617bb892017-04-04 15:03:23 +0000180 public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventConverters converters) {
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000181 BuildEventStreamProtos.TargetComplete.Builder builder =
182 BuildEventStreamProtos.TargetComplete.newBuilder();
183
184 builder.setSuccess(!failed());
Klaus Aehlig6fcc00f2017-04-27 16:58:24 +0200185 builder.setTargetKind(target.getTarget().getTargetKind());
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000186 builder.addAllTag(getTags());
Klaus Aehlig4901d8b2017-04-10 15:13:59 +0000187 builder.addAllOutputGroup(getOutputFilesByGroup(converters.artifactGroupNamer()));
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000188
Klaus Aehlig10da4662017-06-27 15:36:27 +0200189 if (isTest) {
190 builder.setTestSize(
Klaus Aehlig2a92c902017-08-11 14:03:31 +0200191 TargetConfiguredEvent.bepTestSize(
192 TestSize.getTestSize(target.getTarget().getAssociatedRule())));
Klaus Aehlig10da4662017-06-27 15:36:27 +0200193 }
194
Klaus Aehlig0a6cd622017-04-21 17:09:40 +0200195 // TODO(aehlig): remove direct reporting of artifacts as soon as clients no longer
196 // need it.
197 for (ArtifactsInOutputGroup group : outputs) {
198 if (group.areImportant()) {
Klaus Aehligcba24ab2017-09-27 05:14:11 -0400199 addImportantOutputs(builder, converters, group.getArtifacts());
Klaus Aehlig0a6cd622017-04-21 17:09:40 +0200200 }
201 }
Klaus Aehligcba24ab2017-09-27 05:14:11 -0400202 if (baselineCoverageArtifacts != null) {
203 addImportantOutputs(builder, converters, baselineCoverageArtifacts);
204 }
Klaus Aehlig0a6cd622017-04-21 17:09:40 +0200205
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000206 BuildEventStreamProtos.TargetComplete complete = builder.build();
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +0000207 return GenericBuildEvent.protoChaining(this).setCompleted(complete).build();
208 }
Klaus Aehlig74d716b2016-11-23 12:38:24 +0000209
210 @Override
211 public Collection<BuildEventId> postedAfter() {
212 return postedAfter;
213 }
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000214
Klaus Aehlig4901d8b2017-04-10 15:13:59 +0000215 @Override
216 public Collection<NestedSet<Artifact>> reportedArtifacts() {
217 ImmutableSet.Builder<NestedSet<Artifact>> builder =
218 new ImmutableSet.Builder<NestedSet<Artifact>>();
219 for (ArtifactsInOutputGroup artifactsInGroup : outputs) {
220 builder.add(artifactsInGroup.getArtifacts());
221 }
Klaus Aehligcba24ab2017-09-27 05:14:11 -0400222 if (baselineCoverageArtifacts != null) {
223 builder.add(baselineCoverageArtifacts);
224 }
Klaus Aehlig4901d8b2017-04-10 15:13:59 +0000225 return builder.build();
226 }
227
Klaus Aehlig71c993b2017-05-09 07:49:46 -0400228 @Override
Klaus Aehliga708a022017-07-11 12:54:40 +0200229 public Collection<BuildEvent> getConfigurations() {
Klaus Aehlig71c993b2017-05-09 07:49:46 -0400230 BuildConfiguration configuration = target.getConfiguration();
231 if (configuration != null) {
232 return ImmutableList.of(target.getConfiguration());
233 } else {
Klaus Aehliga708a022017-07-11 12:54:40 +0200234 return ImmutableList.<BuildEvent>of();
Klaus Aehlig71c993b2017-05-09 07:49:46 -0400235 }
236 }
237
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000238 private Iterable<String> getTags() {
239 // We are only interested in targets that are rules.
240 if (!(target instanceof RuleConfiguredTarget)) {
241 return ImmutableList.<String>of();
242 }
243 AttributeMap attributes = ConfiguredAttributeMapper.of((RuleConfiguredTarget) target);
244 // Every rule (implicitly) has a "tags" attribute.
245 return attributes.get("tags", Type.STRING_LIST);
246 }
247
Klaus Aehlig4901d8b2017-04-10 15:13:59 +0000248 private Iterable<OutputGroup> getOutputFilesByGroup(ArtifactGroupNamer namer) {
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000249 ImmutableList.Builder<OutputGroup> groups = ImmutableList.builder();
250 for (ArtifactsInOutputGroup artifactsInOutputGroup : outputs) {
251 OutputGroup.Builder groupBuilder = OutputGroup.newBuilder();
252 groupBuilder.setName(artifactsInOutputGroup.getOutputGroup());
Klaus Aehlig4901d8b2017-04-10 15:13:59 +0000253 groupBuilder.addFileSets(
254 namer.apply(
255 (new NestedSetView<Artifact>(artifactsInOutputGroup.getArtifacts())).identifier()));
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000256 groups.add(groupBuilder.build());
257 }
Klaus Aehligcba24ab2017-09-27 05:14:11 -0400258 if (baselineCoverageArtifacts != null) {
259 groups.add(
260 OutputGroup.newBuilder()
261 .setName(TestFileNameConstants.BASELINE_COVERAGE)
262 .addFileSets(
263 namer.apply(
264 (new NestedSetView<Artifact>(baselineCoverageArtifacts).identifier())))
265 .build());
266 }
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000267 return groups.build();
268 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100269}