blob: 03c469191bc4d8a6de326f947241607e2e189405 [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 Aehlig4901d8b2017-04-10 15:13:59 +000023import com.google.devtools.build.lib.buildeventstream.ArtifactGroupNamer;
aehlig617bb892017-04-04 15:03:23 +000024import com.google.devtools.build.lib.buildeventstream.BuildEventConverters;
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +000025import com.google.devtools.build.lib.buildeventstream.BuildEventId;
26import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos;
Jakob Buchgraberfb646092017-02-27 18:53:25 +000027import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.OutputGroup;
Klaus Aehlig74d716b2016-11-23 12:38:24 +000028import com.google.devtools.build.lib.buildeventstream.BuildEventWithOrderConstraint;
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +000029import com.google.devtools.build.lib.buildeventstream.GenericBuildEvent;
Klaus Aehligd1f4a162016-10-25 14:51:55 +000030import com.google.devtools.build.lib.causes.Cause;
Klaus Aehlig1f452c32017-02-02 10:19:15 +000031import com.google.devtools.build.lib.cmdline.Label;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010032import com.google.devtools.build.lib.collect.nestedset.NestedSet;
33import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
Klaus Aehlig4901d8b2017-04-10 15:13:59 +000034import com.google.devtools.build.lib.collect.nestedset.NestedSetView;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010035import com.google.devtools.build.lib.collect.nestedset.Order;
Jakob Buchgraberfb646092017-02-27 18:53:25 +000036import com.google.devtools.build.lib.packages.AttributeMap;
Klaus Aehlig1f452c32017-02-02 10:19:15 +000037import com.google.devtools.build.lib.rules.test.TestProvider;
Jakob Buchgraberfb646092017-02-27 18:53:25 +000038import com.google.devtools.build.lib.syntax.Type;
Mark Schaller6df81792015-12-10 18:47:47 +000039import com.google.devtools.build.lib.util.Preconditions;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010040import com.google.devtools.build.skyframe.SkyValue;
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +000041import java.util.Collection;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010042
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +000043/** This event is fired as soon as a target is either built or fails. */
Klaus Aehlig4901d8b2017-04-10 15:13:59 +000044public final class TargetCompleteEvent
45 implements SkyValue, BuildEventWithOrderConstraint, EventReportingArtifacts {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010046
47 private final ConfiguredTarget target;
Klaus Aehligd1f4a162016-10-25 14:51:55 +000048 private final NestedSet<Cause> rootCauses;
Klaus Aehlig74d716b2016-11-23 12:38:24 +000049 private final Collection<BuildEventId> postedAfter;
Jakob Buchgraberfb646092017-02-27 18:53:25 +000050 private final Iterable<ArtifactsInOutputGroup> outputs;
Klaus Aehlig6f978502016-11-30 12:12:56 +000051 private final boolean isTest;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010052
Klaus Aehlig6f978502016-11-30 12:12:56 +000053 private TargetCompleteEvent(
Jakob Buchgraberfb646092017-02-27 18:53:25 +000054 ConfiguredTarget target,
55 NestedSet<Cause> rootCauses,
56 Iterable<ArtifactsInOutputGroup> outputs,
57 boolean isTest) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010058 this.target = target;
Klaus Aehligd1f4a162016-10-25 14:51:55 +000059 this.rootCauses =
60 (rootCauses == null) ? NestedSetBuilder.<Cause>emptySet(Order.STABLE_ORDER) : rootCauses;
Klaus Aehlig74d716b2016-11-23 12:38:24 +000061
62 ImmutableList.Builder postedAfterBuilder = ImmutableList.builder();
63 for (Cause cause : getRootCauses()) {
64 postedAfterBuilder.add(BuildEventId.fromCause(cause));
65 }
66 this.postedAfter = postedAfterBuilder.build();
Jakob Buchgraberfb646092017-02-27 18:53:25 +000067 this.outputs = outputs;
Klaus Aehlig6f978502016-11-30 12:12:56 +000068 this.isTest = isTest;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010069 }
70
Klaus Aehlig6f978502016-11-30 12:12:56 +000071 /** Construct a successful target completion event. */
Jakob Buchgraberfb646092017-02-27 18:53:25 +000072 public static TargetCompleteEvent createSuccessfulTarget(
73 ConfiguredTarget ct, NestedSet<ArtifactsInOutputGroup> outputs) {
74 return new TargetCompleteEvent(ct, null, outputs, false);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010075 }
76
Klaus Aehlig6f978502016-11-30 12:12:56 +000077 /** Construct a successful target completion event for a target that will be tested. */
78 public static TargetCompleteEvent createSuccessfulTestTarget(ConfiguredTarget ct) {
Jakob Buchgraberfb646092017-02-27 18:53:25 +000079 return new TargetCompleteEvent(ct, null, ImmutableList.<ArtifactsInOutputGroup>of(), true);
Klaus Aehlig6f978502016-11-30 12:12:56 +000080 }
81
82
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010083 /**
84 * Construct a target completion event for a failed target, with the given non-empty root causes.
85 */
Klaus Aehligd1f4a162016-10-25 14:51:55 +000086 public static TargetCompleteEvent createFailed(ConfiguredTarget ct, NestedSet<Cause> rootCauses) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010087 Preconditions.checkArgument(!Iterables.isEmpty(rootCauses));
Jakob Buchgraberfb646092017-02-27 18:53:25 +000088 return new TargetCompleteEvent(
89 ct, rootCauses, ImmutableList.<ArtifactsInOutputGroup>of(), false);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010090 }
91
92 /**
93 * Returns the target associated with the event.
94 */
95 public ConfiguredTarget getTarget() {
96 return target;
97 }
98
99 /**
100 * Determines whether the target has failed or succeeded.
101 */
102 public boolean failed() {
103 return !rootCauses.isEmpty();
104 }
105
Klaus Aehligd1f4a162016-10-25 14:51:55 +0000106 /** Get the root causes of the target. May be empty. */
107 public Iterable<Cause> getRootCauses() {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100108 return rootCauses;
109 }
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +0000110
111 @Override
112 public BuildEventId getEventId() {
113 return BuildEventId.targetCompleted(getTarget().getLabel());
114 }
115
116 @Override
117 public Collection<BuildEventId> getChildrenEvents() {
118 ImmutableList.Builder childrenBuilder = ImmutableList.builder();
119 for (Cause cause : getRootCauses()) {
120 childrenBuilder.add(BuildEventId.fromCause(cause));
121 }
Klaus Aehlig6f978502016-11-30 12:12:56 +0000122 if (isTest) {
Klaus Aehlig1f452c32017-02-02 10:19:15 +0000123 // For tests, announce all the test actions that will minimally happen (except for
124 // interruption). If after the result of a test action another attempt is necessary,
125 // it will be announced with the action that made the new attempt necessary.
126 Label label = target.getTarget().getLabel();
127 TestProvider.TestParams params = target.getProvider(TestProvider.class).getTestParams();
128 for (int run = 0; run < Math.max(params.getRuns(), 1); run++) {
129 for (int shard = 0; shard < Math.max(params.getShards(), 1); shard++) {
130 childrenBuilder.add(BuildEventId.testResult(label, run, shard));
131 }
132 }
133 childrenBuilder.add(BuildEventId.testSummary(label));
Klaus Aehlig8d253622016-11-02 17:24:46 +0000134 }
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +0000135 return childrenBuilder.build();
136 }
137
138 @Override
aehlig617bb892017-04-04 15:03:23 +0000139 public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventConverters converters) {
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000140 BuildEventStreamProtos.TargetComplete.Builder builder =
141 BuildEventStreamProtos.TargetComplete.newBuilder();
142
143 builder.setSuccess(!failed());
144 builder.addAllTag(getTags());
Klaus Aehlig4901d8b2017-04-10 15:13:59 +0000145 builder.addAllOutputGroup(getOutputFilesByGroup(converters.artifactGroupNamer()));
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000146
147 BuildEventStreamProtos.TargetComplete complete = builder.build();
Klaus Aehlig8a8a7fc2016-10-26 14:27:48 +0000148 return GenericBuildEvent.protoChaining(this).setCompleted(complete).build();
149 }
Klaus Aehlig74d716b2016-11-23 12:38:24 +0000150
151 @Override
152 public Collection<BuildEventId> postedAfter() {
153 return postedAfter;
154 }
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000155
Klaus Aehlig4901d8b2017-04-10 15:13:59 +0000156 @Override
157 public Collection<NestedSet<Artifact>> reportedArtifacts() {
158 ImmutableSet.Builder<NestedSet<Artifact>> builder =
159 new ImmutableSet.Builder<NestedSet<Artifact>>();
160 for (ArtifactsInOutputGroup artifactsInGroup : outputs) {
161 builder.add(artifactsInGroup.getArtifacts());
162 }
163 return builder.build();
164 }
165
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000166 private Iterable<String> getTags() {
167 // We are only interested in targets that are rules.
168 if (!(target instanceof RuleConfiguredTarget)) {
169 return ImmutableList.<String>of();
170 }
171 AttributeMap attributes = ConfiguredAttributeMapper.of((RuleConfiguredTarget) target);
172 // Every rule (implicitly) has a "tags" attribute.
173 return attributes.get("tags", Type.STRING_LIST);
174 }
175
Klaus Aehlig4901d8b2017-04-10 15:13:59 +0000176 private Iterable<OutputGroup> getOutputFilesByGroup(ArtifactGroupNamer namer) {
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000177 ImmutableList.Builder<OutputGroup> groups = ImmutableList.builder();
178 for (ArtifactsInOutputGroup artifactsInOutputGroup : outputs) {
179 OutputGroup.Builder groupBuilder = OutputGroup.newBuilder();
180 groupBuilder.setName(artifactsInOutputGroup.getOutputGroup());
Klaus Aehlig4901d8b2017-04-10 15:13:59 +0000181 groupBuilder.addFileSets(
182 namer.apply(
183 (new NestedSetView<Artifact>(artifactsInOutputGroup.getArtifacts())).identifier()));
Jakob Buchgraberfb646092017-02-27 18:53:25 +0000184 groups.add(groupBuilder.build());
185 }
186 return groups.build();
187 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100188}