Skip to content

Commit

Permalink
Migrate more tests to use Starlark cc_toolchain configuration.
Browse files Browse the repository at this point in the history
Issue bazelbuild#5380
RELNOTES: None.
PiperOrigin-RevId: 239182940
  • Loading branch information
scentini authored and copybara-github committed Mar 19, 2019
1 parent d6df980 commit 814fb29
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 369 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ _FEATURE_NAMES = struct(
compiler_param_file = "compiler_param_file",
objcopy_embed_flags = "objcopy_embed_flags",
ld_embed_flags = "ld_embed_flags",
opt = "opt",
fastbuild = "fastbuild",
dbg = "dbg",
)

_no_legacy_features_feature = feature(name = _FEATURE_NAMES.no_legacy_features)
Expand Down Expand Up @@ -698,6 +701,55 @@ _ld_embed_flags_feature = feature(
],
)

_dbg_compilation_feature = feature(
name = _FEATURE_NAMES.dbg,
flag_sets = [
flag_set(
actions = [ACTION_NAMES.cpp_compile],
flag_groups = [
flag_group(flags = ["-dbg"]),
],
),
],
)

_fastbuild_compilation_feature = feature(
name = _FEATURE_NAMES.fastbuild,
flag_sets = [
flag_set(
actions = [ACTION_NAMES.cpp_compile],
flag_groups = [
flag_group(flags = ["-fastbuild"]),
],
),
],
)

_opt_compilation_feature = feature(
name = _FEATURE_NAMES.opt,
flag_sets = [
flag_set(
actions = [ACTION_NAMES.cpp_compile],
flag_groups = [
flag_group(flags = ["-opt"]),
],
),
],
)

_compilation_mode_features = [
_dbg_compilation_feature,
_fastbuild_compilation_feature,
_opt_compilation_feature,
]

_compile_header_modules_feature_configuration = [
_supports_pic_feature,
feature(name = "header_modules", implies = ["use_header_modules"]),
_module_maps_feature,
feature(name = "use_header_modules"),
]

_feature_name_to_feature = {
_FEATURE_NAMES.no_legacy_features: _no_legacy_features_feature,
_FEATURE_NAMES.do_not_split_linking_cmdline: _do_not_split_linking_cmdline_feature,
Expand Down Expand Up @@ -740,23 +792,8 @@ _feature_name_to_feature = {
"env_var_feature_configuration": _env_var_feature_configuration,
"host_and_nonhost_configuration": _host_and_nonhost_configuration,
"simple_layering_check": _simple_layering_check_feature,
}

_static_link_as_dot_lib_pattern = artifact_name_pattern(
category_name = "static_library",
prefix = "lib",
extension = ".lib",
)

_static_link_as_dot_a_pattern = artifact_name_pattern(
category_name = "static_library",
prefix = "lib",
extension = ".a",
)

_artifact_name_to_artifact_pattern = {
"static_link_as_dot_lib": _static_link_as_dot_lib_pattern,
"static_link_as_dot_a": _static_link_as_dot_a_pattern,
"compilation_mode_features": _compilation_mode_features,
"compile_header_modules": _compile_header_modules_feature_configuration,
}

_tool_for_action_config = {
Expand All @@ -780,11 +817,12 @@ def _get_action_config(name, path):
tools = [tool(path = path)],
)

def _get_artifact_name_pattern(name):
artifact = _artifact_name_to_artifact_pattern[name]
if artifact == None:
fail("Artifact name pattern not defined: " + name)
return artifact
def _get_artifact_name_pattern(category, prefix, extension):
return artifact_name_pattern(
category_name = category,
prefix = prefix,
extension = extension,
)

def _get_tool_path(name, path):
return tool_path(name = name, path = path)
Expand Down Expand Up @@ -846,8 +884,8 @@ def _impl(ctx):

artifact_name_patterns = []

for name in ctx.attr.artifact_name_patterns:
artifact_name_patterns.append(_get_artifact_name_pattern(name))
for category, values in ctx.attr.artifact_name_patterns.items():
artifact_name_patterns.append(_get_artifact_name_pattern(category, values[0], values[1]))

action_configs = []

Expand Down Expand Up @@ -922,7 +960,7 @@ cc_toolchain_config = rule(
"abi_libc_version": attr.string(default = "local"),
"feature_names": attr.string_list(),
"action_configs": attr.string_list(),
"artifact_name_patterns": attr.string_list(),
"artifact_name_patterns": attr.string_list_dict(),
"cc_target_os": attr.string(),
"builtin_sysroot": attr.string(default = "/usr/grte/v1"),
"tool_paths": attr.string_dict(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.google.devtools.build.lib.packages.util;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.rules.cpp.CppRuleClasses;
import com.google.devtools.build.lib.testutil.TestConstants;
Expand Down Expand Up @@ -52,7 +53,7 @@ public static final class CcToolchainConfig {
private final String targetLibc;
private final ImmutableList<String> features;
private final ImmutableList<String> actionConfigs;
private final ImmutableList<String> artifactNamePatterns;
private final ImmutableList<ImmutableList<String>> artifactNamePatterns;
private final ImmutableList<Pair<String, String>> toolPaths;

private CcToolchainConfig(
Expand All @@ -66,7 +67,7 @@ private CcToolchainConfig(
String targetLibc,
ImmutableList<String> features,
ImmutableList<String> actionConfigs,
ImmutableList<String> artifactNamePatterns,
ImmutableList<ImmutableList<String>> artifactNamePatterns,
ImmutableList<Pair<String, String>> toolPaths) {
this.cpu = cpu;
this.compiler = compiler;
Expand All @@ -90,7 +91,7 @@ public static Builder builder() {
public static class Builder {
private ImmutableList<String> features = ImmutableList.of();
private ImmutableList<String> actionConfigs = ImmutableList.of();
private ImmutableList<String> artifactNamePatterns = ImmutableList.of();
private ImmutableList<ImmutableList<String>> artifactNamePatterns = ImmutableList.of();
private ImmutableList<Pair<String, String>> toolPaths = ImmutableList.of();

public Builder withFeatures(String... features) {
Expand All @@ -103,7 +104,13 @@ public Builder withActionConfigs(String... actionConfigs) {
return this;
}

public Builder withArtifactNamePatterns(String... artifactNamePatterns) {
public Builder withArtifactNamePatterns(ImmutableList<String>... artifactNamePatterns) {
for (ImmutableList<String> pattern : artifactNamePatterns) {
Preconditions.checkArgument(
pattern.size() == 3,
"Artifact name pattern should have three attributes: category_name, prefix and"
+ " extension");
}
this.artifactNamePatterns = ImmutableList.copyOf(artifactNamePatterns);
return this;
}
Expand Down Expand Up @@ -177,7 +184,10 @@ String getCcToolchainConfigRule() {
.collect(ImmutableList.toImmutableList());
ImmutableList<String> patternsList =
artifactNamePatterns.stream()
.map(pattern -> "'" + pattern + "'")
.map(
pattern ->
String.format(
"'%s': ['%s', '%s']", pattern.get(0), pattern.get(1), pattern.get(2)))
.collect(ImmutableList.toImmutableList());
ImmutableList<String> toolPathsList =
toolPaths.stream()
Expand All @@ -200,7 +210,7 @@ String getCcToolchainConfigRule() {
String.format(
" action_configs = [%s],", Joiner.on(",\n ").join(actionConfigsList)),
String.format(
" artifact_name_patterns = [%s],", Joiner.on(",\n ").join(patternsList)),
" artifact_name_patterns = {%s},", Joiner.on(",\n ").join(patternsList)),
String.format(" tool_paths = {%s},", Joiner.on(",\n ").join(toolPathsList)),
" )");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,19 +425,11 @@ public boolean apply(Artifact artifact) {
+ " enabled: true"
+ "}";

public static final String STATIC_LINK_TWEAKED_CONFIGURATION =
"artifact_name_pattern {"
+ " category_name: 'static_library'"
+ " prefix: 'lib'"
+ " extension: '.lib'"
+ "}";
public static final ImmutableList<String> STATIC_LINK_TWEAKED_ARTIFACT_NAME_PATTERN =
ImmutableList.of("static_library", "lib", ".lib");

public static final String STATIC_LINK_AS_DOT_A_CONFIGURATION =
"artifact_name_pattern {"
+ " category_name: 'static_library'"
+ " prefix: 'lib'"
+ " extension: '.a'"
+ "}";
public static final ImmutableList<String> STATIC_LINK_AS_DOT_A_ARTIFACT_NAME_PATTERN =
ImmutableList.of("static_library", "lib", ".a");

public static final String MODULE_MAPS_FEATURE =
"feature {"
Expand Down Expand Up @@ -487,6 +479,26 @@ public boolean apply(Artifact artifact) {
public static final String STATIC_LINK_CPP_RUNTIMES_FEATURE =
"feature { name: 'static_link_cpp_runtimes' enabled: true }";

public static final String EMPTY_CC_TOOLCHAIN =
Joiner.on("\n")
.join(
"def _impl(ctx):",
" return cc_common.create_cc_toolchain_config_info(",
" ctx = ctx,",
" toolchain_identifier = 'mock-llvm-toolchain-k8',",
" host_system_name = 'mock-system-name-for-k8',",
" target_system_name = 'mock-target-system-name-for-k8',",
" target_cpu = 'k8',",
" target_libc = 'mock-libc-for-k8',",
" compiler = 'mock-compiler-for-k8',",
" abi_libc_version = 'mock-abi-libc-for-k8',",
" abi_version = 'mock-abi-version-for-k8')",
"cc_toolchain_config = rule(",
" implementation = _impl,",
" attrs = {},",
" provides = [CcToolchainConfigInfo],",
")");

public static final String EMPTY_CROSSTOOL =
"major_version: 'foo'\nminor_version:' foo'\n" + emptyToolchainForCpu("k8");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.util.CompileOnlyTestCase;
import com.google.devtools.build.lib.packages.util.MockCcSupport;
import com.google.devtools.build.lib.packages.util.Crosstool.CcToolchainConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -32,7 +32,8 @@ public class CcCompileOnlyTest extends CompileOnlyTestCase {
public void testCcCompileOnly() throws Exception {
getAnalysisMock()
.ccSupport()
.setupCrosstool(mockToolsConfig, MockCcSupport.SUPPORTS_PIC_FEATURE);
.setupCcToolchainConfig(
mockToolsConfig, CcToolchainConfig.builder().withFeatures(CppRuleClasses.SUPPORTS_PIC));
useConfiguration("--cpu=k8");
scratch.file("package/BUILD",
"cc_binary(name='foo', srcs=['foo.cc', ':bar'], deps = [':foolib'])",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void testCcHostToolchainAliasRuleHasHostConfiguration() throws Exception
public void testThatHostCrosstoolTopCommandLineArgumentWorks() throws Exception {
scratch.file(
"b/BUILD",
"load(':cc_toolchain_config.bzl', 'cc_toolchain_config')",
"cc_toolchain_suite(",
" name = 'my_custom_toolchain_suite',",
" toolchains = {",
Expand All @@ -56,6 +57,7 @@ public void testThatHostCrosstoolTopCommandLineArgumentWorks() throws Exception
"cc_toolchain(",
" name = 'toolchain_b',",
" toolchain_identifier = 'mock-llvm-toolchain-k8',",
" toolchain_config = ':mock_config',",
" cpu = 'ED-E',",
" all_files = ':banana',",
" ar_files = ':empty',",
Expand All @@ -64,8 +66,10 @@ public void testThatHostCrosstoolTopCommandLineArgumentWorks() throws Exception
" dwp_files = ':empty',",
" linker_files = ':empty',",
" strip_files = ':empty',",
" objcopy_files = ':empty')");
scratch.file("b/CROSSTOOL", MockCcSupport.EMPTY_CROSSTOOL);
" objcopy_files = ':empty')",
"cc_toolchain_config(name='mock_config')");

scratch.file("b/cc_toolchain_config.bzl", MockCcSupport.EMPTY_CC_TOOLCHAIN);

scratch.file("a/BUILD", "cc_host_toolchain_alias(name='current_cc_host_toolchain')");

Expand Down
Loading

0 comments on commit 814fb29

Please sign in to comment.