-
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.
bazel starlark transitions: grab build setting label from Starlark ca…
…ll stack in order to canonicalize. There are 5 locations a user might write the label string of a build setting: (1) on the command line (2) in the transition definition inputs (3) in the transition definition output (4) in the transition impl function while reading the settings dict (5) in the transition impl function return dict We know that, depending on the situation, there are multiple ways to write "a build setting in the main repo of this project" - //myflag, @//myflag, @main_repo//my:flag. In all 5 places listed above, bazel needs to recognize that the three different versions of //myflag above all map back to a single target. We need this for deduping purposes and consistency. 1 is taken care of in a grandparent CL to this CL. This Cl addresses locations 2-5. To do this, whenever we are interpreting a user's input at one of the locations above, we covert that input string to the canonical string form of that build setting for our back end book keeping purposes. The logic to do this was modeled off of the `Label()` function logic. We also keep a map of canonicalized form to the user-given form in order to display messages (or key the dict in 4) to the user using the forms of the flags they used themselves. work towards #11128 PiperOrigin-RevId: 351426279
- Loading branch information
1 parent
6d8f067
commit 98d376f
Showing
7 changed files
with
471 additions
and
96 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
310 changes: 267 additions & 43 deletions
310
...n/java/com/google/devtools/build/lib/analysis/config/StarlarkDefinedConfigTransition.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
165 changes: 165 additions & 0 deletions
165
src/test/java/com/google/devtools/build/lib/analysis/StarlarkTransitionTest.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,165 @@ | ||
// Copyright 2020 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; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; | ||
import com.google.devtools.build.lib.cmdline.Label; | ||
import com.google.devtools.build.lib.testutil.Scratch; | ||
import java.util.Map; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** | ||
* Test of common logic between Starlark-defined transitions. Rule-transition- or | ||
* attr-transition-specific logic should be tested in {@link StarlarkRuleTransitionProviderTest} and | ||
* {@link StarlarkAttrTransitionProviderTest}. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class StarlarkTransitionTest extends BuildViewTestCase { | ||
static void writeAllowlistFile(Scratch scratch) throws Exception { | ||
scratch.file( | ||
"tools/allowlists/function_transition_allowlist/BUILD", | ||
"package_group(", | ||
" name = 'function_transition_allowlist',", | ||
" packages = [", | ||
" '//test/...',", | ||
" ],", | ||
")"); | ||
} | ||
|
||
@Test | ||
public void testDupeSettingsInInputsThrowsError() throws Exception { | ||
scratch.file( | ||
"test/defs.bzl", | ||
"def _setting_impl(ctx):", | ||
" return []", | ||
"string_flag = rule(", | ||
" implementation = _setting_impl,", | ||
" build_setting = config.string(flag=True),", | ||
")", | ||
"def _transition_impl(settings, attr):", | ||
" return {'//test:formation': 'mesa'}", | ||
"formation_transition = transition(", | ||
" implementation = _transition_impl,", | ||
" inputs = ['@//test:formation', '//test:formation'],", // duplicates here | ||
" outputs = ['//test:formation'],", | ||
")", | ||
"def _impl(ctx):", | ||
" return []", | ||
"state = rule(", | ||
" implementation = _impl,", | ||
" cfg = formation_transition,", | ||
" attrs = {", | ||
" '_allowlist_function_transition': attr.label(", | ||
" default = '//tools/allowlists/function_transition_allowlist',", | ||
" ),", | ||
" })"); | ||
scratch.file( | ||
"test/BUILD", | ||
"load('//test:defs.bzl', 'state', 'string_flag')", | ||
"state(name = 'arizona')", | ||
"string_flag(name = 'formation', build_setting_default = 'canyon')"); | ||
|
||
reporter.removeHandler(failFastHandler); | ||
getConfiguredTarget("//test:arizona"); | ||
assertContainsEvent( | ||
"Transition declares duplicate build setting '@//test:formation' in INPUTS"); | ||
} | ||
|
||
@Test | ||
public void testDupeSettingsInOutputsThrowsError() throws Exception { | ||
scratch.file( | ||
"test/defs.bzl", | ||
"def _setting_impl(ctx):", | ||
" return []", | ||
"string_flag = rule(", | ||
" implementation = _setting_impl,", | ||
" build_setting = config.string(flag=True),", | ||
")", | ||
"def _transition_impl(settings, attr):", | ||
" return {'//test:formation': 'mesa'}", | ||
"formation_transition = transition(", | ||
" implementation = _transition_impl,", | ||
" inputs = ['//test:formation'],", | ||
" outputs = ['@//test:formation', '//test:formation'],", // duplicates here | ||
")", | ||
"def _impl(ctx):", | ||
" return []", | ||
"state = rule(", | ||
" implementation = _impl,", | ||
" cfg = formation_transition,", | ||
" attrs = {", | ||
" '_allowlist_function_transition': attr.label(", | ||
" default = '//tools/allowlists/function_transition_allowlist',", | ||
" ),", | ||
" })"); | ||
scratch.file( | ||
"test/BUILD", | ||
"load('//test:defs.bzl', 'state', 'string_flag')", | ||
"state(name = 'arizona')", | ||
"string_flag(name = 'formation', build_setting_default = 'canyon')"); | ||
|
||
reporter.removeHandler(failFastHandler); | ||
getConfiguredTarget("//test:arizona"); | ||
assertContainsEvent( | ||
"Transition declares duplicate build setting '@//test:formation' in OUTPUTS"); | ||
} | ||
|
||
@Test | ||
public void testDifferentFormsOfFlagInInputsAndOutputs() throws Exception { | ||
writeAllowlistFile(scratch); | ||
scratch.file( | ||
"test/defs.bzl", | ||
"def _setting_impl(ctx):", | ||
" return []", | ||
"string_flag = rule(", | ||
" implementation = _setting_impl,", | ||
" build_setting = config.string(flag=True),", | ||
")", | ||
"def _transition_impl(settings, attr):", | ||
" return {", | ||
" '//test:formation': settings['@//test:formation']+'-transitioned',", | ||
" }", | ||
"formation_transition = transition(", | ||
" implementation = _transition_impl,", | ||
" inputs = ['@//test:formation'],", | ||
" outputs = ['//test:formation'],", | ||
")", | ||
"def _impl(ctx):", | ||
" return []", | ||
"state = rule(", | ||
" implementation = _impl,", | ||
" cfg = formation_transition,", | ||
" attrs = {", | ||
" '_allowlist_function_transition': attr.label(", | ||
" default = '//tools/allowlists/function_transition_allowlist',", | ||
" ),", | ||
" })"); | ||
scratch.file( | ||
"test/BUILD", | ||
"load('//test:defs.bzl', 'state', 'string_flag')", | ||
"state(name = 'arizona')", | ||
"string_flag(name = 'formation', build_setting_default = 'canyon')"); | ||
|
||
Map<Label, Object> starlarkOptions = | ||
getConfiguration(getConfiguredTarget("//test:arizona")).getOptions().getStarlarkOptions(); | ||
assertThat(starlarkOptions).hasSize(1); | ||
assertThat(starlarkOptions.get(Label.parseAbsoluteUnchecked("//test:formation"))) | ||
.isEqualTo("canyon-transitioned"); | ||
} | ||
} |