Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim input build settings after transition #13971

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,15 @@ public static Map<String, BuildOptions> validate(
Map<PackageValue.Key, PackageValue> buildSettingPackages,
Map<String, BuildOptions> toOptions)
throws TransitionException {
// collect settings changed during this transition and their types
// Collect settings changed during this transition and their types. This includes settings that
// were only used as inputs as to the transition and thus had their default values added to the
// fromOptions, which in case of a no-op transition directly end up in toOptions.
Map<Label, Rule> changedSettingToRule = Maps.newHashMap();
root.visit(
(StarlarkTransitionVisitor)
transition -> {
ImmutableSet<Label> changedSettings =
getRelevantStarlarkSettingsFromTransition(transition, Settings.OUTPUTS);
ImmutableSet<Label> changedSettings = getRelevantStarlarkSettingsFromTransition(
transition, Settings.INPUTS_AND_OUTPUTS);
for (Label setting : changedSettings) {
changedSettingToRule.put(
setting, getActual(buildSettingPackages, setting).getAssociatedRule());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2249,4 +2249,165 @@ public void testNoPlatformChange() throws Exception {
assertThat(getConfiguration(dep).getOptions().get(PlatformOptions.class).platforms)
.containsExactly(Label.parseAbsoluteUnchecked("//platforms:my_platform"));
}

@Test
public void testEffectiveNoopTransitionTrimsInputBuildSettings() throws Exception {
writeAllowlistFile();
scratch.file(
"test/starlark/rules.bzl",
"def _string_impl(ctx):",
" return []",
"string_flag = rule(",
" implementation = _string_impl,",
" build_setting = config.string(flag = True),",
")",
"def _no_op_transition_impl(settings, attr):",
" return {",
" '//test/starlark:input_and_output': settings['//test/starlark:input_and_output'],",
" '//test/starlark:output_only': 'output_only_default',",
" }",
"_no_op_transition = transition(",
" implementation = _no_op_transition_impl,",
" inputs = [",
" '//test/starlark:input_only',",
" '//test/starlark:input_and_output',",
" ],",
" outputs = [",
" '//test/starlark:input_and_output',",
" '//test/starlark:output_only',",
" ],",
")",
"def _apply_transition_impl(ctx):",
" ctx.actions.symlink(",
" output = ctx.outputs.out,",
" target_file = ctx.file.target,",
" )",
" return [DefaultInfo(executable = ctx.outputs.out)]",
"apply_transition = rule(",
" implementation = _apply_transition_impl,",
" attrs = {",
" 'target': attr.label(",
" cfg = _no_op_transition,",
" allow_single_file = True,",
" ),",
" 'out': attr.output(),",
" '_allowlist_function_transition': attr.label(",
" default = '//tools/allowlists/function_transition_allowlist',",
" ),",
" },",
" executable = False,",
")");
scratch.file(
"test/starlark/BUILD",
"load('//test/starlark:rules.bzl', 'apply_transition', 'string_flag')",
"",
"string_flag(",
" name = 'input_only',",
" build_setting_default = 'input_only_default',",
")",
"string_flag(",
" name = 'input_and_output',",
" build_setting_default = 'input_and_output_default',",
")",
"string_flag(",
" name = 'output_only',",
" build_setting_default = 'output_only_default',",
")",
"cc_binary(",
" name = 'main',",
" srcs = ['main.cc'],",
")",
"apply_transition(",
" name = 'transitioned_main',",
" target = ':main',",
" out = 'main_out',",
")");

update(
ImmutableList.of("//test/starlark:main", "//test/starlark:transitioned_main"),
/*keepGoing=*/ false,
LOADING_PHASE_THREADS,
/*doAnalysis=*/ true,
new EventBus());
assertNoEvents();
}

@Test
public void testExplicitNoopTransitionTrimsInputBuildSettings() throws Exception {
writeAllowlistFile();
scratch.file(
"test/starlark/rules.bzl",
"def _string_impl(ctx):",
" return []",
"string_flag = rule(",
" implementation = _string_impl,",
" build_setting = config.string(flag = True),",
")",
"def _no_op_transition_impl(settings, attr):",
" return {}",
"_no_op_transition = transition(",
" implementation = _no_op_transition_impl,",
" inputs = [",
" '//test/starlark:input_only',",
" '//test/starlark:input_and_output',",
" ],",
" outputs = [",
" '//test/starlark:input_and_output',",
" '//test/starlark:output_only',",
" ],",
")",
"def _apply_transition_impl(ctx):",
" ctx.actions.symlink(",
" output = ctx.outputs.out,",
" target_file = ctx.file.target,",
" )",
" return [DefaultInfo(executable = ctx.outputs.out)]",
"apply_transition = rule(",
" implementation = _apply_transition_impl,",
" attrs = {",
" 'target': attr.label(",
" cfg = _no_op_transition,",
" allow_single_file = True,",
" ),",
" 'out': attr.output(),",
" '_allowlist_function_transition': attr.label(",
" default = '//tools/allowlists/function_transition_allowlist',",
" ),",
" },",
" executable = False,",
")");
scratch.file(
"test/starlark/BUILD",
"load('//test/starlark:rules.bzl', 'apply_transition', 'string_flag')",
"",
"string_flag(",
" name = 'input_only',",
" build_setting_default = 'input_only_default',",
")",
"string_flag(",
" name = 'input_and_output',",
" build_setting_default = 'input_and_output_default',",
")",
"string_flag(",
" name = 'output_only',",
" build_setting_default = 'output_only_default',",
")",
"cc_binary(",
" name = 'main',",
" srcs = ['main.cc'],",
")",
"apply_transition(",
" name = 'transitioned_main',",
" target = ':main',",
" out = 'main_out',",
")");

update(
ImmutableList.of("//test/starlark:main", "//test/starlark:transitioned_main"),
/*keepGoing=*/ false,
LOADING_PHASE_THREADS,
/*doAnalysis=*/ true,
new EventBus());
assertNoEvents();
}
}