Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [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 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
| 16 | import com.google.common.collect.ImmutableList; |
| 17 | import com.google.devtools.build.lib.events.Event; |
Michajlo Matijkiw | 945a42d | 2015-10-12 16:24:01 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.skyframe.EmptySkyValue; |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.skyframe.SkyFunction; |
| 20 | import com.google.devtools.build.skyframe.SkyKey; |
| 21 | import com.google.devtools.build.skyframe.SkyValue; |
| 22 | |
Janak Ramakrishnan | df0531f | 2015-09-23 17:30:04 +0000 | [diff] [blame] | 23 | /** |
| 24 | * Given a "cycle" of objects of type {@param S}, emits an error message for this cycle. |
| 25 | * The keys for this SkyFunction are assumed to deduplicate cycles that differ only in which element |
| 26 | * of the cycle they start at, so multiple paths to the cycle will be reported by a single execution |
| 27 | * of this function. |
| 28 | * |
| 29 | * <p>The cycle need not actually be a cycle -- any iterable exhibiting an error that is independent |
| 30 | * of the iterable's starting point can be an argument to this function. |
| 31 | */ |
| 32 | abstract class AbstractChainUniquenessFunction<S> implements SkyFunction { |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 33 | protected abstract String getConciseDescription(); |
Janak Ramakrishnan | df0531f | 2015-09-23 17:30:04 +0000 | [diff] [blame] | 34 | |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 35 | protected abstract String getHeaderMessage(); |
Janak Ramakrishnan | df0531f | 2015-09-23 17:30:04 +0000 | [diff] [blame] | 36 | |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 37 | protected abstract String getFooterMessage(); |
Janak Ramakrishnan | df0531f | 2015-09-23 17:30:04 +0000 | [diff] [blame] | 38 | |
Janak Ramakrishnan | df0531f | 2015-09-23 17:30:04 +0000 | [diff] [blame] | 39 | protected abstract String elementToString(S elt); |
| 40 | |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 41 | @Override |
| 42 | public SkyValue compute(SkyKey skyKey, Environment env) { |
| 43 | StringBuilder errorMessage = new StringBuilder(); |
| 44 | errorMessage.append(getConciseDescription() + " detected\n"); |
| 45 | errorMessage.append(getHeaderMessage() + "\n"); |
| 46 | @SuppressWarnings("unchecked") |
Janak Ramakrishnan | df0531f | 2015-09-23 17:30:04 +0000 | [diff] [blame] | 47 | ImmutableList<S> chain = (ImmutableList<S>) skyKey.argument(); |
| 48 | for (S elt : chain) { |
| 49 | errorMessage.append(elementToString(elt) + "\n"); |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 50 | } |
| 51 | errorMessage.append(getFooterMessage() + "\n"); |
| 52 | // The purpose of this SkyFunction is the side effect of emitting an error message exactly |
Janak Ramakrishnan | df0531f | 2015-09-23 17:30:04 +0000 | [diff] [blame] | 53 | // once per build per unique error. |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 54 | env.getListener().handle(Event.error(errorMessage.toString())); |
Michajlo Matijkiw | 945a42d | 2015-10-12 16:24:01 +0000 | [diff] [blame] | 55 | return EmptySkyValue.INSTANCE; |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public String extractTag(SkyKey skyKey) { |
| 60 | return null; |
| 61 | } |
| 62 | } |
| 63 | |