-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example: ``` my_rule( name = "buildme", deps = select({ "//other/package:some_config": [":mydeps"] })) ``` Today, `//other/package:some_config` is exempt from visibility checking, even though it's technically a target dep of `buildme`. While this dep is "special" vs. other deps in various ways, there's no obvious reason why it needs to be special in this way. It adds an unclear corner case exception to visibility's API. ### Implementation: select() keys are not "normal" dependencies and don't generally follow the same code path. Hence them not being automatically visibility checked like others. In particular, normal dependencies are found in `ConfiguredTargetFunction` and validity-checked in `RuleContext.Builder`. select() keys' only purpose is to figure out which other normal dependencies should exist. There's generally no need to pass them to `RuleContext.Builder`. Instead, Blaze passes their `ConfigMatchingProvider`s, which remain useful for analysis phase attribute lookups. `RuleContext.Builder` needs a `ConfiguredTargetAndData` to do validity-checking. This patch propagates that information for select() keys too. We could alternatively refactor the validity checking logic. But that's an even more invasive change. Or do ad hoc validity checking directly in `ConfiguredTargetFunction`. But that's duplicating logic we really want to keep consolidated. ### Backward compatibility: This would break existing builds if `config_setting` defaulted to private visibility. So this change specially defaults `config_setting` to public visibility, with clarifying documentation. When ready we'll want to create an incompatible change to make `config_setting` the same as everything else. Fixes #12669. Closes #12877. RELNOTES: config_setting now honors `visibility` attribute (and defaults to `//visibility:public`) PiperOrigin-RevId: 354310777
- Loading branch information
1 parent
a2b7e40
commit cac82cf
Showing
13 changed files
with
224 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/com/google/devtools/build/lib/analysis/config/ConfigConditions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2021 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package com.google.devtools.build.lib.analysis.config; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.devtools.build.lib.analysis.ConfiguredTarget; | ||
import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo; | ||
import com.google.devtools.build.lib.analysis.platform.PlatformInfo; | ||
import com.google.devtools.build.lib.cmdline.Label; | ||
import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData; | ||
|
||
/** | ||
* Utility class for temporarily tracking {@code select()} keys' {@link ConfigMatchingProvider}s and | ||
* {@link ConfiguredTarget}s. | ||
* | ||
* <p>This is a utility class because its only purpose is to maintain {@link ConfiguredTarget} long | ||
* enough for {@link RuleContext.Builder} to do prerequisite validation on it (for example, | ||
* visibility checks). | ||
* | ||
* <p>Once {@link RuleContext} is instantiated, it should only have access to {@link | ||
* ConfigMatchingProvider}, on the principle that providers are the correct interfaces for storing | ||
* and sharing target metadata. {@link ConfiguredTarget} isn't meant to persist that long. | ||
*/ | ||
@AutoValue | ||
public abstract class ConfigConditions { | ||
public abstract ImmutableMap<Label, ConfiguredTargetAndData> asConfiguredTargets(); | ||
|
||
public abstract ImmutableMap<Label, ConfigMatchingProvider> asProviders(); | ||
|
||
public static ConfigConditions create( | ||
ImmutableMap<Label, ConfiguredTargetAndData> asConfiguredTargets, | ||
ImmutableMap<Label, ConfigMatchingProvider> asProviders) { | ||
return new AutoValue_ConfigConditions(asConfiguredTargets, asProviders); | ||
} | ||
|
||
public static final ConfigConditions EMPTY = | ||
ConfigConditions.create(ImmutableMap.of(), ImmutableMap.of()); | ||
|
||
/** Exception for when a {@code select()} has an invalid key (for example, wrong target type). */ | ||
public static class InvalidConditionException extends Exception {} | ||
|
||
/** | ||
* Returns a {@link ConfigMatchingProvider} from the given configured target if appropriate, else | ||
* triggers a {@link InvalidConditionException}. | ||
* | ||
* <p>This is the canonical place to extract {@link ConfigMatchingProvider}s from configured | ||
* targets. It's not as simple as {@link ConfiguredTarget#getProvider}. | ||
*/ | ||
public static ConfigMatchingProvider fromConfiguredTarget( | ||
ConfiguredTargetAndData selectKey, PlatformInfo targetPlatform) | ||
throws InvalidConditionException { | ||
ConfiguredTarget selectable = selectKey.getConfiguredTarget(); | ||
// The below handles config_setting (which natively provides ConfigMatchingProvider) and | ||
// constraint_value (which needs a custom-built ConfigMatchingProvider). | ||
ConfigMatchingProvider matchingProvider = selectable.getProvider(ConfigMatchingProvider.class); | ||
if (matchingProvider != null) { | ||
return matchingProvider; | ||
} | ||
ConstraintValueInfo constraintValueInfo = selectable.get(ConstraintValueInfo.PROVIDER); | ||
if (constraintValueInfo != null && targetPlatform != null) { | ||
// If platformInfo == null, that means the owning target doesn't invoke toolchain | ||
// resolution, in which case depending on a constraint_value is nonsensical. | ||
return constraintValueInfo.configMatchingProvider(targetPlatform); | ||
} | ||
|
||
// Not a valid provider for configuration conditions. | ||
throw new InvalidConditionException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.