blob: 489cc0e6831a5c02d1cee9251d27e88b970c863a [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Nathan Harmataad810502015-07-29 01:33:49 +00002//
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.
14package com.google.devtools.build.lib.skyframe;
15
16import com.google.common.collect.ImmutableList;
17import com.google.devtools.build.lib.events.Event;
Michajlo Matijkiw945a42d2015-10-12 16:24:01 +000018import com.google.devtools.build.skyframe.EmptySkyValue;
Nathan Harmataad810502015-07-29 01:33:49 +000019import com.google.devtools.build.skyframe.SkyFunction;
20import com.google.devtools.build.skyframe.SkyKey;
21import com.google.devtools.build.skyframe.SkyValue;
22
Janak Ramakrishnandf0531f2015-09-23 17:30:04 +000023/**
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 */
32abstract class AbstractChainUniquenessFunction<S> implements SkyFunction {
Nathan Harmataad810502015-07-29 01:33:49 +000033 protected abstract String getConciseDescription();
Janak Ramakrishnandf0531f2015-09-23 17:30:04 +000034
Nathan Harmataad810502015-07-29 01:33:49 +000035 protected abstract String getHeaderMessage();
Janak Ramakrishnandf0531f2015-09-23 17:30:04 +000036
Nathan Harmataad810502015-07-29 01:33:49 +000037 protected abstract String getFooterMessage();
Janak Ramakrishnandf0531f2015-09-23 17:30:04 +000038
Janak Ramakrishnandf0531f2015-09-23 17:30:04 +000039 protected abstract String elementToString(S elt);
40
Nathan Harmataad810502015-07-29 01:33:49 +000041 @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 Ramakrishnandf0531f2015-09-23 17:30:04 +000047 ImmutableList<S> chain = (ImmutableList<S>) skyKey.argument();
48 for (S elt : chain) {
49 errorMessage.append(elementToString(elt) + "\n");
Nathan Harmataad810502015-07-29 01:33:49 +000050 }
51 errorMessage.append(getFooterMessage() + "\n");
52 // The purpose of this SkyFunction is the side effect of emitting an error message exactly
Janak Ramakrishnandf0531f2015-09-23 17:30:04 +000053 // once per build per unique error.
Nathan Harmataad810502015-07-29 01:33:49 +000054 env.getListener().handle(Event.error(errorMessage.toString()));
Michajlo Matijkiw945a42d2015-10-12 16:24:01 +000055 return EmptySkyValue.INSTANCE;
Nathan Harmataad810502015-07-29 01:33:49 +000056 }
57
58 @Override
59 public String extractTag(SkyKey skyKey) {
60 return null;
61 }
62}
63