blob: ab391c6330392576f7f7f9851ceb04f6b2a9b84f [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
Googler2c19a572015-07-16 08:38:49 +000017import com.google.common.base.MoreObjects;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018import com.google.common.base.Preconditions;
19import 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;
22
23import java.util.Collection;
24import java.util.Collections;
25
26import javax.annotation.Nullable;
27
28/**
29 * Contains information about the result of a build. While BuildRequest is immutable, this class is
30 * mutable.
31 */
32public final class BuildResult {
33 private long startTimeMillis = 0; // milliseconds since UNIX epoch.
34 private long stopTimeMillis = 0;
35
36 private Throwable crash = null;
37 private boolean catastrophe = false;
38 private ExitCode exitCondition = ExitCode.BLAZE_INTERNAL_ERROR;
Ulf Adams59dbf682015-09-17 11:36:43 +000039
40 private BuildConfigurationCollection configurations;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010041 private Collection<ConfiguredTarget> actualTargets;
42 private Collection<ConfiguredTarget> testTargets;
43 private Collection<ConfiguredTarget> successfulTargets;
44
45 public BuildResult(long startTimeMillis) {
46 this.startTimeMillis = startTimeMillis;
47 }
48
49 /**
50 * Record the time (according to System.currentTimeMillis()) at which the
51 * service of this request was completed.
52 */
53 public void setStopTime(long stopTimeMillis) {
54 this.stopTimeMillis = stopTimeMillis;
55 }
56
57 /**
58 * Return the time (according to System.currentTimeMillis()) at which the
59 * service of this request was completed.
60 */
61 public long getStopTime() {
62 return stopTimeMillis;
63 }
64
65 /**
66 * Returns the elapsed time in seconds for the service of this request. Not
67 * defined for requests that have not been serviced.
68 */
69 public double getElapsedSeconds() {
70 if (startTimeMillis == 0 || stopTimeMillis == 0) {
71 throw new IllegalStateException("BuildRequest has not been serviced");
72 }
73 return (stopTimeMillis - startTimeMillis) / 1000.0;
74 }
75
76 public void setExitCondition(ExitCode exitCondition) {
77 this.exitCondition = exitCondition;
78 }
79
80 /**
81 * True iff the build request has been successfully completed.
82 */
83 public boolean getSuccess() {
84 return exitCondition == ExitCode.SUCCESS;
85 }
86
87 /**
88 * Gets the Blaze exit condition.
89 */
90 public ExitCode getExitCondition() {
91 return exitCondition;
92 }
93
94 /**
95 * Sets the RuntimeException / Error that induced a Blaze crash.
96 */
97 public void setUnhandledThrowable(Throwable crash) {
98 Preconditions.checkState(crash == null ||
99 ((crash instanceof RuntimeException) || (crash instanceof Error)));
100 this.crash = crash;
101 }
102
103 /**
104 * Sets a "catastrophe": A build failure severe enough to halt a keep_going build.
105 */
106 public void setCatastrophe() {
107 this.catastrophe = true;
108 }
109
110 /**
111 * Was the build a "catastrophe": A build failure severe enough to halt a keep_going build.
112 */
113 public boolean wasCatastrophe() {
114 return catastrophe;
115 }
116
117 /**
118 * Gets the Blaze crash Throwable. Null if Blaze did not crash.
119 */
120 public Throwable getUnhandledThrowable() {
121 return crash;
122 }
123
Ulf Adams59dbf682015-09-17 11:36:43 +0000124 public void setBuildConfigurationCollection(BuildConfigurationCollection configurations) {
125 this.configurations = configurations;
126 }
127
128 /**
129 * Returns the build configuration collection used for the build.
130 */
131 public BuildConfigurationCollection getBuildConfigurationCollection() {
132 return configurations;
133 }
134
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100135 /**
136 * @see #getActualTargets
137 */
138 public void setActualTargets(Collection<ConfiguredTarget> actualTargets) {
139 this.actualTargets = actualTargets;
140 }
141
142 /**
143 * Returns the actual set of targets which we attempted to build. This value
144 * is set during the build, after the target patterns have been parsed and
145 * resolved. If --keep_going is specified, this set may exclude targets that
146 * could not be found or successfully analyzed. It may be examined after the
147 * build. May be null even after the build, if there were errors in the
148 * loading or analysis phases.
149 */
150 public Collection<ConfiguredTarget> getActualTargets() {
151 return actualTargets;
152 }
153
154 /**
155 * @see #getTestTargets
156 */
157 public void setTestTargets(@Nullable Collection<ConfiguredTarget> testTargets) {
158 this.testTargets = testTargets == null ? null : Collections.unmodifiableCollection(testTargets);
159 }
160
161 /**
162 * Returns the actual unmodifiable collection of targets which we attempted to
163 * test. This value is set at the end of the build analysis phase, after the
164 * test target patterns have been parsed and resolved. If --keep_going is
165 * specified, this collection may exclude targets that could not be found or
166 * successfully analyzed. It may be examined after the build. May be null even
167 * after the build, if there were errors in the loading or analysis phases or
168 * if testing was not requested.
169 */
170 public Collection<ConfiguredTarget> getTestTargets() {
171 return testTargets;
172 }
173
174 /**
175 * @see #getSuccessfulTargets
176 */
177 void setSuccessfulTargets(Collection<ConfiguredTarget> successfulTargets) {
178 this.successfulTargets = successfulTargets;
179 }
180
181 /**
182 * Returns the set of targets which successfully built. This value
183 * is set at the end of the build, after the target patterns have been parsed
184 * and resolved and after attempting to build the targets. If --keep_going
185 * is specified, this set may exclude targets that could not be found or
186 * successfully analyzed, or could not be built. It may be examined after
187 * the build. May be null if the execution phase was not attempted, as
188 * may happen if there are errors in the loading phase, for example.
189 */
190 public Collection<ConfiguredTarget> getSuccessfulTargets() {
191 return successfulTargets;
192 }
193
194 /** For debugging. */
195 @Override
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100196 public String toString() {
Googler2c19a572015-07-16 08:38:49 +0000197 return MoreObjects.toStringHelper(this)
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100198 .add("startTimeMillis", startTimeMillis)
199 .add("stopTimeMillis", stopTimeMillis)
200 .add("crash", crash)
201 .add("catastrophe", catastrophe)
202 .add("exitCondition", exitCondition)
203 .add("actualTargets", actualTargets)
204 .add("testTargets", testTargets)
205 .add("successfulTargets", successfulTargets)
206 .toString();
207 }
208}