blob: 741ef98a94f367a6f9d3c2b39d4b75381daec3ac [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.buildtool;
16
gregce45ae0962017-07-22 00:11:13 +020017import com.google.common.annotations.VisibleForTesting;
Googler2c19a572015-07-16 08:38:49 +000018import com.google.common.base.MoreObjects;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010019import com.google.devtools.build.lib.analysis.ConfiguredTarget;
Ulf Adams59dbf682015-09-17 11:36:43 +000020import com.google.devtools.build.lib.analysis.config.BuildConfigurationCollection;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010021import com.google.devtools.build.lib.util.ExitCode;
Mark Schaller6df81792015-12-10 18:47:47 +000022import com.google.devtools.build.lib.util.Preconditions;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010023
24import java.util.Collection;
25import java.util.Collections;
26
27import javax.annotation.Nullable;
28
29/**
30 * Contains information about the result of a build. While BuildRequest is immutable, this class is
31 * mutable.
32 */
33public final class BuildResult {
34 private long startTimeMillis = 0; // milliseconds since UNIX epoch.
35 private long stopTimeMillis = 0;
36
37 private Throwable crash = null;
38 private boolean catastrophe = false;
Florian Weikert63da2e72016-02-25 00:22:05 +000039 private boolean stopOnFirstFailure;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010040 private ExitCode exitCondition = ExitCode.BLAZE_INTERNAL_ERROR;
Ulf Adams59dbf682015-09-17 11:36:43 +000041
42 private BuildConfigurationCollection configurations;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010043 private Collection<ConfiguredTarget> actualTargets;
44 private Collection<ConfiguredTarget> testTargets;
45 private Collection<ConfiguredTarget> successfulTargets;
gregce45ae0962017-07-22 00:11:13 +020046 private Collection<ConfiguredTarget> skippedTargets;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010047
48 public BuildResult(long startTimeMillis) {
49 this.startTimeMillis = startTimeMillis;
50 }
51
52 /**
53 * Record the time (according to System.currentTimeMillis()) at which the
54 * service of this request was completed.
55 */
56 public void setStopTime(long stopTimeMillis) {
57 this.stopTimeMillis = stopTimeMillis;
58 }
59
60 /**
61 * Return the time (according to System.currentTimeMillis()) at which the
62 * service of this request was completed.
63 */
64 public long getStopTime() {
65 return stopTimeMillis;
66 }
67
68 /**
69 * Returns the elapsed time in seconds for the service of this request. Not
70 * defined for requests that have not been serviced.
71 */
72 public double getElapsedSeconds() {
73 if (startTimeMillis == 0 || stopTimeMillis == 0) {
74 throw new IllegalStateException("BuildRequest has not been serviced");
75 }
76 return (stopTimeMillis - startTimeMillis) / 1000.0;
77 }
78
79 public void setExitCondition(ExitCode exitCondition) {
80 this.exitCondition = exitCondition;
81 }
82
83 /**
84 * True iff the build request has been successfully completed.
85 */
86 public boolean getSuccess() {
Lukacs Berki06662fe2016-11-15 10:56:26 +000087 return exitCondition.equals(ExitCode.SUCCESS);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010088 }
89
90 /**
91 * Gets the Blaze exit condition.
92 */
93 public ExitCode getExitCondition() {
94 return exitCondition;
95 }
96
97 /**
98 * Sets the RuntimeException / Error that induced a Blaze crash.
99 */
100 public void setUnhandledThrowable(Throwable crash) {
101 Preconditions.checkState(crash == null ||
102 ((crash instanceof RuntimeException) || (crash instanceof Error)));
103 this.crash = crash;
104 }
105
106 /**
107 * Sets a "catastrophe": A build failure severe enough to halt a keep_going build.
108 */
109 public void setCatastrophe() {
110 this.catastrophe = true;
111 }
112
113 /**
114 * Was the build a "catastrophe": A build failure severe enough to halt a keep_going build.
115 */
116 public boolean wasCatastrophe() {
117 return catastrophe;
118 }
119
120 /**
Florian Weikert63da2e72016-02-25 00:22:05 +0000121 * Whether some targets were skipped because of {@code setStopOnFirstFailure}.
122 */
Florian Weikert5aa538e2016-03-11 16:27:13 +0000123 public boolean getStopOnFirstFailure() {
124 return stopOnFirstFailure;
Florian Weikert63da2e72016-02-25 00:22:05 +0000125 }
126
127 /**
128 * Indicates that remaining targets should be skipped once a target breaks/fails.
129 * This will be set when --nokeep_going or --notest_keep_going is set.
130 */
131 public void setStopOnFirstFailure(boolean stopOnFirstFailure) {
132 this.stopOnFirstFailure = stopOnFirstFailure;
133 }
134
135 /**
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100136 * Gets the Blaze crash Throwable. Null if Blaze did not crash.
137 */
138 public Throwable getUnhandledThrowable() {
139 return crash;
140 }
141
Ulf Adams59dbf682015-09-17 11:36:43 +0000142 public void setBuildConfigurationCollection(BuildConfigurationCollection configurations) {
143 this.configurations = configurations;
144 }
145
146 /**
147 * Returns the build configuration collection used for the build.
148 */
149 public BuildConfigurationCollection getBuildConfigurationCollection() {
150 return configurations;
151 }
152
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100153 /**
154 * @see #getActualTargets
155 */
156 public void setActualTargets(Collection<ConfiguredTarget> actualTargets) {
157 this.actualTargets = actualTargets;
158 }
159
160 /**
161 * Returns the actual set of targets which we attempted to build. This value
162 * is set during the build, after the target patterns have been parsed and
163 * resolved. If --keep_going is specified, this set may exclude targets that
164 * could not be found or successfully analyzed. It may be examined after the
165 * build. May be null even after the build, if there were errors in the
166 * loading or analysis phases.
167 */
168 public Collection<ConfiguredTarget> getActualTargets() {
169 return actualTargets;
170 }
171
172 /**
173 * @see #getTestTargets
174 */
175 public void setTestTargets(@Nullable Collection<ConfiguredTarget> testTargets) {
176 this.testTargets = testTargets == null ? null : Collections.unmodifiableCollection(testTargets);
177 }
178
179 /**
180 * Returns the actual unmodifiable collection of targets which we attempted to
181 * test. This value is set at the end of the build analysis phase, after the
182 * test target patterns have been parsed and resolved. If --keep_going is
183 * specified, this collection may exclude targets that could not be found or
184 * successfully analyzed. It may be examined after the build. May be null even
185 * after the build, if there were errors in the loading or analysis phases or
186 * if testing was not requested.
187 */
188 public Collection<ConfiguredTarget> getTestTargets() {
189 return testTargets;
190 }
191
192 /**
193 * @see #getSuccessfulTargets
194 */
195 void setSuccessfulTargets(Collection<ConfiguredTarget> successfulTargets) {
196 this.successfulTargets = successfulTargets;
197 }
198
199 /**
200 * Returns the set of targets which successfully built. This value
201 * is set at the end of the build, after the target patterns have been parsed
202 * and resolved and after attempting to build the targets. If --keep_going
203 * is specified, this set may exclude targets that could not be found or
204 * successfully analyzed, or could not be built. It may be examined after
205 * the build. May be null if the execution phase was not attempted, as
206 * may happen if there are errors in the loading phase, for example.
207 */
208 public Collection<ConfiguredTarget> getSuccessfulTargets() {
209 return successfulTargets;
210 }
211
gregce45ae0962017-07-22 00:11:13 +0200212 /**
213 * See {@link #getSkippedTargets()}.
214 */
215 void setSkippedTargets(Collection<ConfiguredTarget> skippedTargets) {
216 this.skippedTargets = skippedTargets;
217 }
218
219 /**
220 * Returns the set of targets which were skipped (Blaze didn't attempt to execute them)
221 * because they're not compatible with the build's target platform.
222 */
223 @VisibleForTesting
224 public Collection<ConfiguredTarget> getSkippedTargets() {
225 return skippedTargets;
226 }
227
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100228 /** For debugging. */
229 @Override
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100230 public String toString() {
Googler2c19a572015-07-16 08:38:49 +0000231 return MoreObjects.toStringHelper(this)
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100232 .add("startTimeMillis", startTimeMillis)
233 .add("stopTimeMillis", stopTimeMillis)
234 .add("crash", crash)
235 .add("catastrophe", catastrophe)
236 .add("exitCondition", exitCondition)
237 .add("actualTargets", actualTargets)
238 .add("testTargets", testTargets)
239 .add("successfulTargets", successfulTargets)
240 .toString();
241 }
242}