|
| 1 | +// Copyright 2021 The Bazel Authors. All rights reserved. |
| 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.analysis.config; |
| 15 | + |
| 16 | +import com.google.auto.value.AutoValue; |
| 17 | +import com.google.common.collect.ImmutableMap; |
| 18 | +import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
| 19 | +import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo; |
| 20 | +import com.google.devtools.build.lib.analysis.platform.PlatformInfo; |
| 21 | +import com.google.devtools.build.lib.cmdline.Label; |
| 22 | +import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData; |
| 23 | + |
| 24 | +/** |
| 25 | + * Utility class for temporarily tracking {@code select()} keys' {@link ConfigMatchingProvider}s and |
| 26 | + * {@link ConfiguredTarget}s. |
| 27 | + * |
| 28 | + * <p>This is a utility class because its only purpose is to maintain {@link ConfiguredTarget} long |
| 29 | + * enough for {@link RuleContext.Builder} to do prerequisite validation on it (for example, |
| 30 | + * visibility checks). |
| 31 | + * |
| 32 | + * <p>Once {@link RuleContext} is instantiated, it should only have access to {@link |
| 33 | + * ConfigMatchingProvider}, on the principle that providers are the correct interfaces for storing |
| 34 | + * and sharing target metadata. {@link ConfiguredTarget} isn't meant to persist that long. |
| 35 | + */ |
| 36 | +@AutoValue |
| 37 | +public abstract class ConfigConditions { |
| 38 | + public abstract ImmutableMap<Label, ConfiguredTargetAndData> asConfiguredTargets(); |
| 39 | + |
| 40 | + public abstract ImmutableMap<Label, ConfigMatchingProvider> asProviders(); |
| 41 | + |
| 42 | + public static ConfigConditions create( |
| 43 | + ImmutableMap<Label, ConfiguredTargetAndData> asConfiguredTargets, |
| 44 | + ImmutableMap<Label, ConfigMatchingProvider> asProviders) { |
| 45 | + return new AutoValue_ConfigConditions(asConfiguredTargets, asProviders); |
| 46 | + } |
| 47 | + |
| 48 | + public static final ConfigConditions EMPTY = |
| 49 | + ConfigConditions.create(ImmutableMap.of(), ImmutableMap.of()); |
| 50 | + |
| 51 | + /** Exception for when a {@code select()} has an invalid key (for example, wrong target type). */ |
| 52 | + public static class InvalidConditionException extends Exception {} |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns a {@link ConfigMatchingProvider} from the given configured target if appropriate, else |
| 56 | + * triggers a {@link InvalidConditionException}. |
| 57 | + * |
| 58 | + * <p>This is the canonical place to extract {@link ConfigMatchingProvider}s from configured |
| 59 | + * targets. It's not as simple as {@link ConfiguredTarget#getProvider}. |
| 60 | + */ |
| 61 | + public static ConfigMatchingProvider fromConfiguredTarget( |
| 62 | + ConfiguredTargetAndData selectKey, PlatformInfo targetPlatform) |
| 63 | + throws InvalidConditionException { |
| 64 | + ConfiguredTarget selectable = selectKey.getConfiguredTarget(); |
| 65 | + // The below handles config_setting (which natively provides ConfigMatchingProvider) and |
| 66 | + // constraint_value (which needs a custom-built ConfigMatchingProvider). |
| 67 | + ConfigMatchingProvider matchingProvider = selectable.getProvider(ConfigMatchingProvider.class); |
| 68 | + if (matchingProvider != null) { |
| 69 | + return matchingProvider; |
| 70 | + } |
| 71 | + ConstraintValueInfo constraintValueInfo = selectable.get(ConstraintValueInfo.PROVIDER); |
| 72 | + if (constraintValueInfo != null && targetPlatform != null) { |
| 73 | + // If platformInfo == null, that means the owning target doesn't invoke toolchain |
| 74 | + // resolution, in which case depending on a constraint_value is nonsensical. |
| 75 | + return constraintValueInfo.configMatchingProvider(targetPlatform); |
| 76 | + } |
| 77 | + |
| 78 | + // Not a valid provider for configuration conditions. |
| 79 | + throw new InvalidConditionException(); |
| 80 | + } |
| 81 | +} |
0 commit comments