Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 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 | |
| 15 | package com.google.devtools.build.lib.buildtool; |
| 16 | |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 17 | import com.google.common.annotations.VisibleForTesting; |
Googler | 2c19a57 | 2015-07-16 08:38:49 +0000 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
Ulf Adams | 59dbf68 | 2015-09-17 11:36:43 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.analysis.config.BuildConfigurationCollection; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import com.google.devtools.build.lib.util.ExitCode; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.util.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | |
| 24 | import java.util.Collection; |
| 25 | import java.util.Collections; |
| 26 | |
| 27 | import javax.annotation.Nullable; |
| 28 | |
| 29 | /** |
| 30 | * Contains information about the result of a build. While BuildRequest is immutable, this class is |
| 31 | * mutable. |
| 32 | */ |
| 33 | public 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 Weikert | 63da2e7 | 2016-02-25 00:22:05 +0000 | [diff] [blame] | 39 | private boolean stopOnFirstFailure; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | private ExitCode exitCondition = ExitCode.BLAZE_INTERNAL_ERROR; |
Ulf Adams | 59dbf68 | 2015-09-17 11:36:43 +0000 | [diff] [blame] | 41 | |
| 42 | private BuildConfigurationCollection configurations; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | private Collection<ConfiguredTarget> actualTargets; |
| 44 | private Collection<ConfiguredTarget> testTargets; |
| 45 | private Collection<ConfiguredTarget> successfulTargets; |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 46 | private Collection<ConfiguredTarget> skippedTargets; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | |
| 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 Berki | 06662fe | 2016-11-15 10:56:26 +0000 | [diff] [blame] | 87 | return exitCondition.equals(ExitCode.SUCCESS); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 88 | } |
| 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 Weikert | 63da2e7 | 2016-02-25 00:22:05 +0000 | [diff] [blame] | 121 | * Whether some targets were skipped because of {@code setStopOnFirstFailure}. |
| 122 | */ |
Florian Weikert | 5aa538e | 2016-03-11 16:27:13 +0000 | [diff] [blame] | 123 | public boolean getStopOnFirstFailure() { |
| 124 | return stopOnFirstFailure; |
Florian Weikert | 63da2e7 | 2016-02-25 00:22:05 +0000 | [diff] [blame] | 125 | } |
| 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 136 | * Gets the Blaze crash Throwable. Null if Blaze did not crash. |
| 137 | */ |
| 138 | public Throwable getUnhandledThrowable() { |
| 139 | return crash; |
| 140 | } |
| 141 | |
Ulf Adams | 59dbf68 | 2015-09-17 11:36:43 +0000 | [diff] [blame] | 142 | 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 153 | /** |
| 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 | |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 212 | /** |
| 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 228 | /** For debugging. */ |
| 229 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 230 | public String toString() { |
Googler | 2c19a57 | 2015-07-16 08:38:49 +0000 | [diff] [blame] | 231 | return MoreObjects.toStringHelper(this) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 232 | .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 | } |