Skip to content

Commit

Permalink
Add --compilation_mode support in transitions
Browse files Browse the repository at this point in the history
Add support for converting //command_line_option:compilation_mode from
Java value to Starlark value so that it can be read in a transition.

Fixes --compilation_mode in #10690, but not the general case.
  • Loading branch information
moroten committed May 18, 2020
1 parent a067335 commit d97af80
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/main/java/com/google/devtools/build/lib/analysis/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,10 @@ java_library(
java_library(
name = "config/compilation_mode",
srcs = ["config/CompilationMode.java"],
deps = ["//src/main/java/com/google/devtools/common/options"],
deps = [
"//src/main/java/com/google/devtools/build/lib/syntax:evaluator",
"//src/main/java/com/google/devtools/common/options",
],
)

java_library(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
// limitations under the License.
package com.google.devtools.build.lib.analysis.config;

import com.google.devtools.build.lib.syntax.Printer;
import com.google.devtools.build.lib.syntax.StarlarkValue;
import com.google.devtools.common.options.EnumConverter;

/**
* This class represents the debug/optimization mode the binaries will be
* built for.
*/
public enum CompilationMode {
public enum CompilationMode implements StarlarkValue {

// Fast build mode (-g0).
FASTBUILD("fastbuild"),
Expand Down Expand Up @@ -47,4 +49,9 @@ public Converter() {
super(CompilationMode.class, "compilation mode");
}
}

@Override
public void repr(Printer printer) {
printer.append(toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,92 @@ public void testOptionConversionCpu() throws Exception {
assertThat(getConfiguration(Iterables.getOnlyElement(dep)).getCpu()).isEqualTo("armeabi-v7a");
}

private void writeReadAndPassthroughOptionsTestFiles() throws Exception {
setStarlarkSemanticsOptions("--experimental_starlark_config_transitions=true");
writeWhitelistFile();

scratch.file(
"test/skylark/my_rule.bzl",
"load('//myinfo:myinfo.bzl', 'MyInfo')",
"settings_under_test = {",
" '//command_line_option:cpu': 'armeabi-v7a',",
" '//command_line_option:compilation_mode': 'dbg',",
" '//command_line_option:crosstool_top': '//android/crosstool:everything',",
" '//command_line_option:platform_suffix': 'my-platform-suffix',",
"}",
"def set_options_transition_func(settings, attr):",
" return settings_under_test",
"def passthrough_transition_func(settings, attr):",
// All values in this test should be possible to copy within Starlark.
" ret = dict(settings)",
// All values in this test should be possible to read within Starlark.
// This does not mean that it is possible to set a string value for all settings,
// e.g. //command_line_option:test_arg should be set to a list of strings.
" for key, expected_value in settings_under_test.items():",
" if str(ret[key]) != expected_value:",
" fail('%s did not pass through, got %r expected %r' %",
" (key, str(ret[key]), expected_value))",
" ret['//command_line_option:test_arg'] = ['ok']",
" return ret",
"my_set_options_transition = transition(",
" implementation = set_options_transition_func,",
" inputs = [],",
" outputs = settings_under_test.keys())",
"my_passthrough_transition = transition(",
" implementation = passthrough_transition_func,",
" inputs = settings_under_test.keys(),",
" outputs = ['//command_line_option:test_arg'] + settings_under_test.keys())",
"def impl(ctx): ",
" return MyInfo(attr_dep = ctx.attr.dep)",
"my_set_options_rule = rule(",
" implementation = impl,",
" attrs = {",
" 'dep': attr.label(cfg = my_set_options_transition),",
" '_whitelist_function_transition': attr.label(",
" default = '//tools/whitelists/function_transition_whitelist',",
" ),",
" })",
"my_passthrough_rule = rule(",
" implementation = impl,",
" attrs = {",
" 'dep': attr.label(cfg = my_passthrough_transition),",
" '_whitelist_function_transition': attr.label(",
" default = '//tools/whitelists/function_transition_whitelist',",
" ),",
" })");

scratch.file(
"test/skylark/BUILD",
"load('//test/skylark:my_rule.bzl', 'my_passthrough_rule', 'my_set_options_rule')",
"my_set_options_rule(name = 'top', dep = ':test')",
"my_passthrough_rule(name = 'test', dep = ':main')",
"cc_binary(name = 'main', srcs = ['main.c'])");
}

@Test
public void testReadAndPassthroughOptions() throws Exception {
writeReadAndPassthroughOptionsTestFiles();
BazelMockAndroidSupport.setupNdk(mockToolsConfig);

ConfiguredTarget topTarget = getConfiguredTarget("//test/skylark:top");

@SuppressWarnings("unchecked")
List<ConfiguredTarget> topDep =
(List<ConfiguredTarget>) getMyInfoFromTarget(topTarget).getValue("attr_dep");
assertThat(topDep).hasSize(1);
ConfiguredTarget testTarget = Iterables.getOnlyElement(topDep);
@SuppressWarnings("unchecked")
List<ConfiguredTarget> testDep =
(List<ConfiguredTarget>) getMyInfoFromTarget(testTarget).getValue("attr_dep");
assertThat(testDep).hasSize(1);
ConfiguredTarget mainTarget = Iterables.getOnlyElement(testDep);
List<String> testArguments = getConfiguration(mainTarget)
.getOptions()
.get(TestOptions.class)
.testArguments;
assertThat(testArguments).containsExactly("ok");
}

@Test
public void testUndeclaredOptionKey() throws Exception {
setStarlarkSemanticsOptions("--experimental_starlark_config_transitions=true");
Expand Down

0 comments on commit d97af80

Please sign in to comment.