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 | |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 16 | import com.google.common.collect.ImmutableList; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 17 | import com.google.devtools.build.lib.util.Preconditions; |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.skyframe.SkyFunctionName; |
| 19 | import com.google.devtools.build.skyframe.SkyKey; |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 20 | |
| 21 | /** |
Janak Ramakrishnan | df0531f | 2015-09-23 17:30:04 +0000 | [diff] [blame] | 22 | * A value for ensuring that an error for a cycle/chain is reported exactly once. This is achieved |
| 23 | * by forcing the same value key for two logically equivalent errors and letting Skyframe do its |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 24 | * magic. |
| 25 | */ |
Michajlo Matijkiw | 945a42d | 2015-10-12 16:24:01 +0000 | [diff] [blame] | 26 | class ChainUniquenessUtils { |
| 27 | |
| 28 | private ChainUniquenessUtils() {} |
| 29 | |
| 30 | /** |
| 31 | * Create a SkyKey for {@code functionName} with a canonicalized representation of the cycle |
| 32 | * specified by {@code chain} as the argument. {@code chain} must be non-empty. |
| 33 | */ |
| 34 | static SkyKey key(SkyFunctionName functionName, ImmutableList<? extends Object> chain) { |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 35 | Preconditions.checkState(!chain.isEmpty()); |
Janak Ramakrishnan | f745e99 | 2016-03-03 08:08:50 +0000 | [diff] [blame] | 36 | return SkyKey.create(functionName, canonicalize(chain)); |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Janak Ramakrishnan | df0531f | 2015-09-23 17:30:04 +0000 | [diff] [blame] | 39 | private static ImmutableList<Object> canonicalize(ImmutableList<? extends Object> cycle) { |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 40 | int minPos = 0; |
| 41 | String minString = cycle.get(0).toString(); |
| 42 | for (int i = 1; i < cycle.size(); i++) { |
Michajlo Matijkiw | 945a42d | 2015-10-12 16:24:01 +0000 | [diff] [blame] | 43 | // TOOD(bazel-team): Is the toString representation stable enough? |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 44 | String candidateString = cycle.get(i).toString(); |
| 45 | if (candidateString.compareTo(minString) < 0) { |
| 46 | minPos = i; |
| 47 | minString = candidateString; |
| 48 | } |
| 49 | } |
Janak Ramakrishnan | df0531f | 2015-09-23 17:30:04 +0000 | [diff] [blame] | 50 | ImmutableList.Builder<Object> builder = ImmutableList.builder(); |
Nathan Harmata | ad81050 | 2015-07-29 01:33:49 +0000 | [diff] [blame] | 51 | for (int i = 0; i < cycle.size(); i++) { |
| 52 | int pos = (minPos + i) % cycle.size(); |
| 53 | builder.add(cycle.get(pos)); |
| 54 | } |
| 55 | return builder.build(); |
| 56 | } |
| 57 | } |
| 58 | |