diff --git a/src/test/java/com/google/devtools/build/lib/analysis/mock/cc_toolchain_config.bzl b/src/test/java/com/google/devtools/build/lib/analysis/mock/cc_toolchain_config.bzl index ffd508cee0eb0d..312b6c25fcd1fe 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/mock/cc_toolchain_config.bzl +++ b/src/test/java/com/google/devtools/build/lib/analysis/mock/cc_toolchain_config.bzl @@ -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) @@ -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, @@ -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 = { @@ -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) @@ -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 = [] @@ -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(), diff --git a/src/test/java/com/google/devtools/build/lib/packages/util/Crosstool.java b/src/test/java/com/google/devtools/build/lib/packages/util/Crosstool.java index a334fa3d98396d..91c52f91e77b45 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/util/Crosstool.java +++ b/src/test/java/com/google/devtools/build/lib/packages/util/Crosstool.java @@ -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; @@ -52,7 +53,7 @@ public static final class CcToolchainConfig { private final String targetLibc; private final ImmutableList features; private final ImmutableList actionConfigs; - private final ImmutableList artifactNamePatterns; + private final ImmutableList> artifactNamePatterns; private final ImmutableList> toolPaths; private CcToolchainConfig( @@ -66,7 +67,7 @@ private CcToolchainConfig( String targetLibc, ImmutableList features, ImmutableList actionConfigs, - ImmutableList artifactNamePatterns, + ImmutableList> artifactNamePatterns, ImmutableList> toolPaths) { this.cpu = cpu; this.compiler = compiler; @@ -90,7 +91,7 @@ public static Builder builder() { public static class Builder { private ImmutableList features = ImmutableList.of(); private ImmutableList actionConfigs = ImmutableList.of(); - private ImmutableList artifactNamePatterns = ImmutableList.of(); + private ImmutableList> artifactNamePatterns = ImmutableList.of(); private ImmutableList> toolPaths = ImmutableList.of(); public Builder withFeatures(String... features) { @@ -103,7 +104,13 @@ public Builder withActionConfigs(String... actionConfigs) { return this; } - public Builder withArtifactNamePatterns(String... artifactNamePatterns) { + public Builder withArtifactNamePatterns(ImmutableList... artifactNamePatterns) { + for (ImmutableList 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; } @@ -177,7 +184,10 @@ String getCcToolchainConfigRule() { .collect(ImmutableList.toImmutableList()); ImmutableList 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 toolPathsList = toolPaths.stream() @@ -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)), " )"); } diff --git a/src/test/java/com/google/devtools/build/lib/packages/util/MockCcSupport.java b/src/test/java/com/google/devtools/build/lib/packages/util/MockCcSupport.java index 1a529d75057911..e0eb315f3c5cb2 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/util/MockCcSupport.java +++ b/src/test/java/com/google/devtools/build/lib/packages/util/MockCcSupport.java @@ -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 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 STATIC_LINK_AS_DOT_A_ARTIFACT_NAME_PATTERN = + ImmutableList.of("static_library", "lib", ".a"); public static final String MODULE_MAPS_FEATURE = "feature {" @@ -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"); diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcCompileOnlyTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcCompileOnlyTest.java index 4755530686c6c9..6abaf9ad6ff9a7 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcCompileOnlyTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcCompileOnlyTest.java @@ -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; @@ -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'])", diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcHostToolchainAliasTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcHostToolchainAliasTest.java index e34ef5850b4099..84529fd8062aca 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcHostToolchainAliasTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcHostToolchainAliasTest.java @@ -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 = {", @@ -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',", @@ -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')"); diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcLibraryConfiguredTargetTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcLibraryConfiguredTargetTest.java index bccc6fd4850576..102d12eefb9437 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcLibraryConfiguredTargetTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcLibraryConfiguredTargetTest.java @@ -34,14 +34,13 @@ import com.google.devtools.build.lib.analysis.util.AnalysisMock; import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; import com.google.devtools.build.lib.packages.ImplicitOutputsFunction; +import com.google.devtools.build.lib.packages.util.Crosstool.CcToolchainConfig; import com.google.devtools.build.lib.packages.util.MockCcSupport; import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData; import com.google.devtools.build.lib.testutil.TestRuleClassProvider; import com.google.devtools.build.lib.util.FileType; import com.google.devtools.build.lib.util.StringUtil; import com.google.devtools.build.lib.vfs.PathFragment; -import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig; -import com.google.protobuf.TextFormat; import java.util.ArrayList; import java.util.List; import org.junit.Before; @@ -125,18 +124,12 @@ public void checkWrongExtensionInArtifactNamePattern( reporter.removeHandler(failFastHandler); AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.COPY_DYNAMIC_LIBRARIES_TO_BINARY_CONFIGURATION, - MockCcSupport.TARGETS_WINDOWS_CONFIGURATION, - "supports_interface_shared_objects: true", - "artifact_name_pattern {" - + " category_name: '" - + categoryName - + "'" - + " prefix: ''" - + " extension: '.wrong_ext'" - + "}"); + CcToolchainConfig.builder() + .withFeatures( + CppRuleClasses.COPY_DYNAMIC_LIBRARIES_TO_BINARY, CppRuleClasses.TARGETS_WINDOWS) + .withArtifactNamePatterns(ImmutableList.of(categoryName, "", ".wrong_ext"))); useConfiguration(); getConfiguredTarget( ruleClassProvider.getToolsRepository() + "//tools/cpp:current_cc_toolchain"); @@ -144,7 +137,7 @@ public void checkWrongExtensionInArtifactNamePattern( String.format( "Unrecognized file extension '.wrong_ext', allowed " + "extensions are %s, please check artifact_name_pattern configuration for " - + "%s in your CROSSTOOL.", + + "%s in your rule.", StringUtil.joinEnglishList(correctExtensions, "or", "'"), categoryName)); } @@ -160,11 +153,11 @@ public void testDefinesAndMakeVariables() throws Exception { public void testMisconfiguredCrosstoolRaisesErrorWhenLinking() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.NO_LEGACY_FEATURES_FEATURE, - MockCcSupport.EMPTY_COMPILE_ACTION_CONFIG, - MockCcSupport.PIC_FEATURE); + CcToolchainConfig.builder() + .withFeatures(CppRuleClasses.NO_LEGACY_FEATURES, CppRuleClasses.PIC) + .withActionConfigs(CppActionNames.CPP_COMPILE)); useConfiguration(); checkError( @@ -178,11 +171,11 @@ public void testMisconfiguredCrosstoolRaisesErrorWhenLinking() throws Exception public void testMisconfiguredCrosstoolRaisesErrorWhenCompiling() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.NO_LEGACY_FEATURES_FEATURE, - MockCcSupport.EMPTY_STATIC_LIBRARY_ACTION_CONFIG, - MockCcSupport.PIC_FEATURE); + CcToolchainConfig.builder() + .withFeatures(CppRuleClasses.NO_LEGACY_FEATURES, CppRuleClasses.PIC) + .withActionConfigs(CppActionNames.CPP_LINK_STATIC_LIBRARY)); useConfiguration(); checkError( @@ -196,10 +189,12 @@ public void testMisconfiguredCrosstoolRaisesErrorWhenCompiling() throws Exceptio public void testFilesToBuild() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.SUPPORTS_DYNAMIC_LINKER_FEATURE, - MockCcSupport.SUPPORTS_INTERFACE_SHARED_LIBRARIES_FEATURE); + CcToolchainConfig.builder() + .withFeatures( + CppRuleClasses.SUPPORTS_DYNAMIC_LINKER, + CppRuleClasses.SUPPORTS_INTERFACE_SHARED_LIBRARIES)); useConfiguration("--cpu=k8"); ConfiguredTarget hello = getConfiguredTarget("//hello:hello"); String cpu = getTargetConfiguration().getCpu(); @@ -226,39 +221,8 @@ public void testFilesToBuild() throws Exception { @Test public void testFilesToBuildWithoutDSO() throws Exception { - CrosstoolConfig.CrosstoolRelease.Builder release = CrosstoolConfig.CrosstoolRelease.newBuilder() - .mergeFrom(CrosstoolConfigurationHelper.simpleCompleteToolchainProto()); - release - .getToolchainBuilder(0) - .setTargetCpu("k8") - .setCompiler("compiler") - // To remove "supports_dynamic_linker" feature - .clearFeature(); - - scratch.file("crosstool/BUILD", - "cc_toolchain_suite(", - " name = 'crosstool',", - " toolchains = {'k8|compiler': ':cc-compiler-k8'})", - "filegroup(name = 'empty')", - "cc_toolchain(", - " name = 'cc-compiler-k8',", - " output_licenses = ['unencumbered'],", - " cpu = 'k8',", - " ar_files = ':empty',", - " as_files = ':empty',", - " compiler_files = ':empty',", - " dwp_files = ':empty',", - " coverage_files = ':empty',", - " linker_files = ':empty',", - " strip_files = ':empty',", - " objcopy_files = ':empty',", - " all_files = ':empty',", - " licenses = ['unencumbered'])"); - scratch.file("crosstool/CROSSTOOL", TextFormat.printToString(release)); - // This is like the preceding test, but with a toolchain that can't build '.so' files - useConfiguration("--crosstool_top=//crosstool:crosstool", "--compiler=compiler", - "--cpu=k8", "--host_cpu=k8"); + useConfiguration("--cpu=k8", "--host_cpu=k8"); ConfiguredTarget hello = getConfiguredTarget("//hello:hello"); Artifact archive = getBinArtifact("libhello.a", hello); assertThat(getFilesToBuild(hello)).containsExactly(archive); @@ -268,10 +232,12 @@ public void testFilesToBuildWithoutDSO() throws Exception { public void testFilesToBuildWithInterfaceSharedObjects() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.SUPPORTS_DYNAMIC_LINKER_FEATURE, - MockCcSupport.SUPPORTS_INTERFACE_SHARED_LIBRARIES_FEATURE); + CcToolchainConfig.builder() + .withFeatures( + CppRuleClasses.SUPPORTS_DYNAMIC_LINKER, + CppRuleClasses.SUPPORTS_INTERFACE_SHARED_LIBRARIES)); useConfiguration("--cpu=k8"); ConfiguredTarget hello = getConfiguredTarget("//hello:hello"); String cpu = getTargetConfiguration().getCpu(); @@ -306,10 +272,12 @@ public void testEmptyLinkopts() throws Exception { public void testSoName() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.SUPPORTS_DYNAMIC_LINKER_FEATURE, - MockCcSupport.SUPPORTS_INTERFACE_SHARED_LIBRARIES_FEATURE); + CcToolchainConfig.builder() + .withFeatures( + CppRuleClasses.SUPPORTS_DYNAMIC_LINKER, + CppRuleClasses.SUPPORTS_INTERFACE_SHARED_LIBRARIES)); // Without interface shared libraries. useConfiguration("--nointerface_shared_objects"); ConfiguredTarget hello = getConfiguredTarget("//hello:hello"); @@ -335,10 +303,12 @@ public void testSoName() throws Exception { public void testCppLinkActionExtraActionInfoWithoutSharedLibraries() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.SUPPORTS_DYNAMIC_LINKER_FEATURE, - MockCcSupport.SUPPORTS_INTERFACE_SHARED_LIBRARIES_FEATURE); + CcToolchainConfig.builder() + .withFeatures( + CppRuleClasses.SUPPORTS_DYNAMIC_LINKER, + CppRuleClasses.SUPPORTS_INTERFACE_SHARED_LIBRARIES)); useConfiguration("--nointerface_shared_objects"); ConfiguredTarget hello = getConfiguredTarget("//hello:hello"); @@ -376,7 +346,9 @@ public void testCppLinkActionExtraActionInfoWithoutSharedLibraries() throws Exce public void testCppLinkActionExtraActionInfoWithSharedLibraries() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, MockCcSupport.SUPPORTS_DYNAMIC_LINKER_FEATURE); + .setupCcToolchainConfig( + mockToolsConfig, + CcToolchainConfig.builder().withFeatures(CppRuleClasses.SUPPORTS_DYNAMIC_LINKER)); useConfiguration("--cpu=k8"); ConfiguredTarget hello = getConfiguredTarget("//hello:hello"); Artifact sharedObject = @@ -412,7 +384,10 @@ public void testCppLinkActionExtraActionInfoWithSharedLibraries() throws Excepti public void testLinkActionCanConsumeArtifactExtensions() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, MockCcSupport.STATIC_LINK_TWEAKED_CONFIGURATION); + .setupCcToolchainConfig( + mockToolsConfig, + CcToolchainConfig.builder() + .withArtifactNamePatterns(MockCcSupport.STATIC_LINK_TWEAKED_ARTIFACT_NAME_PATTERN)); useConfiguration("--features=" + Link.LinkTargetType.STATIC_LIBRARY.getActionName()); ConfiguredTarget hello = getConfiguredTarget("//hello:hello"); Artifact archive = @@ -427,13 +402,10 @@ public void testLinkActionCanConsumeArtifactExtensions() throws Exception { public void testObjectFileNamesCanBeSpecifiedInToolchain() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - "artifact_name_pattern {" - + " category_name: 'object_file'" - + " prefix: ''" - + " extension: '.obj'" - + "}"); + CcToolchainConfig.builder() + .withArtifactNamePatterns(ImmutableList.of("object_file", "", ".obj"))); useConfiguration(); ConfiguredTarget hello = getConfiguredTarget("//hello:hello"); @@ -444,42 +416,21 @@ public void testObjectFileNamesCanBeSpecifiedInToolchain() throws Exception { public void testWindowsFileNamePatternsCanBeSpecifiedInToolchain() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.SUPPORTS_DYNAMIC_LINKER_FEATURE, - MockCcSupport.COPY_DYNAMIC_LIBRARIES_TO_BINARY_CONFIGURATION, - MockCcSupport.SUPPORTS_INTERFACE_SHARED_LIBRARIES_FEATURE, - MockCcSupport.TARGETS_WINDOWS_CONFIGURATION, - "artifact_name_pattern {" - + " category_name: 'object_file'" - + " prefix: ''" - + " extension: '.obj'" - + "}", - "artifact_name_pattern {" - + " category_name: 'static_library'" - + " prefix: ''" - + " extension: '.lib'" - + "}", - "artifact_name_pattern {" - + " category_name: 'alwayslink_static_library'" - + " prefix: ''" - + " extension: '.lo.lib'" - + "}", - "artifact_name_pattern {" - + " category_name: 'executable'" - + " prefix: ''" - + " extension: '.exe'" - + "}", - "artifact_name_pattern {" - + " category_name: 'dynamic_library'" - + " prefix: ''" - + " extension: '.dll'" - + "}", - "artifact_name_pattern {" - + " category_name: 'interface_library'" - + " prefix: ''" - + " extension: '.if.lib'" - + "}"); + CcToolchainConfig.builder() + .withFeatures( + CppRuleClasses.SUPPORTS_DYNAMIC_LINKER, + CppRuleClasses.COPY_DYNAMIC_LIBRARIES_TO_BINARY, + CppRuleClasses.SUPPORTS_INTERFACE_SHARED_LIBRARIES, + CppRuleClasses.TARGETS_WINDOWS) + .withArtifactNamePatterns( + ImmutableList.of("object_file", "", ".obj"), + ImmutableList.of("static_library", "", ".lib"), + ImmutableList.of("alwayslink_static_library", "", ".lo.lib"), + ImmutableList.of("executable", "", ".exe"), + ImmutableList.of("dynamic_library", "", ".dll"), + ImmutableList.of("interface_library", "", ".if.lib"))); useConfiguration(); ConfiguredTarget hello = getConfiguredTarget("//hello:hello"); @@ -554,7 +505,11 @@ public void testWrongInterfaceLibraryArtifactNamePattern() throws Exception { public void testArtifactSelectionBaseNameTemplating() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, MockCcSupport.STATIC_LINK_AS_DOT_A_CONFIGURATION); + .setupCcToolchainConfig( + mockToolsConfig, + CcToolchainConfig.builder() + .withArtifactNamePatterns( + MockCcSupport.STATIC_LINK_AS_DOT_A_ARTIFACT_NAME_PATTERN)); useConfiguration("--features=" + Link.LinkTargetType.STATIC_LIBRARY.getActionName()); ConfiguredTarget hello = getConfiguredTarget("//hello:hello"); Artifact archive = @@ -566,10 +521,10 @@ public void testArtifactSelectionBaseNameTemplating() throws Exception { public void testArtifactsToAlwaysBuild() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.SUPPORTS_PIC_FEATURE, - MockCcSupport.SUPPORTS_DYNAMIC_LINKER_FEATURE); + CcToolchainConfig.builder() + .withFeatures(CppRuleClasses.SUPPORTS_PIC, CppRuleClasses.SUPPORTS_DYNAMIC_LINKER)); useConfiguration("--cpu=k8"); // ArtifactsToAlwaysBuild should apply both for static libraries. ConfiguredTarget helloStatic = getConfiguredTarget("//hello:hello_static"); @@ -590,7 +545,8 @@ public void testArtifactsToAlwaysBuild() throws Exception { public void testTransitiveArtifactsToAlwaysBuildStatic() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, MockCcSupport.SUPPORTS_PIC_FEATURE); + .setupCcToolchainConfig( + mockToolsConfig, CcToolchainConfig.builder().withFeatures(CppRuleClasses.SUPPORTS_PIC)); useConfiguration("--cpu=k8"); ConfiguredTarget x = scratchConfiguredTarget( @@ -607,10 +563,10 @@ public void testTransitiveArtifactsToAlwaysBuildStatic() throws Exception { public void testBuildHeaderModulesAsPrerequisites() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.HEADER_MODULES_FEATURE_CONFIGURATION, - MockCcSupport.SUPPORTS_PIC_FEATURE); + CcToolchainConfig.builder() + .withFeatures(MockCcSupport.HEADER_MODULES_FEATURES, CppRuleClasses.SUPPORTS_PIC)); useConfiguration("--cpu=k8"); ConfiguredTarget x = @@ -629,10 +585,10 @@ public void testBuildHeaderModulesAsPrerequisites() throws Exception { public void testCodeCoverage() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.HEADER_MODULES_FEATURE_CONFIGURATION, - MockCcSupport.SUPPORTS_PIC_FEATURE); + CcToolchainConfig.builder() + .withFeatures(MockCcSupport.HEADER_MODULES_FEATURES, CppRuleClasses.SUPPORTS_PIC)); useConfiguration("--cpu=k8", "--collect_code_coverage"); ConfiguredTarget x = scratchConfiguredTarget( @@ -650,7 +606,9 @@ public void testCodeCoverage() throws Exception { public void testDisablingHeaderModulesWhenDependingOnModuleBuildTransitively() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, MockCcSupport.HEADER_MODULES_FEATURE_CONFIGURATION); + .setupCcToolchainConfig( + mockToolsConfig, + CcToolchainConfig.builder().withFeatures(MockCcSupport.HEADER_MODULES_FEATURES)); useConfiguration(); scratch.file("module/BUILD", "package(features = ['header_modules'])", @@ -715,14 +673,11 @@ private Iterable getHeaderModuleFlags(Iterable input) { public void testCompileHeaderModules() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( - mockToolsConfig, - MockCcSupport.SUPPORTS_PIC_FEATURE, - "feature { name: 'header_modules' implies: 'use_header_modules' }", - MockCcSupport.MODULE_MAPS_FEATURE, - "feature { name: 'use_header_modules' }"); + .setupCcToolchainConfig( + mockToolsConfig, CcToolchainConfig.builder().withFeatures("compile_header_modules")); useConfiguration("--cpu=k8"); - scratch.file("module/BUILD", + scratch.file( + "module/BUILD", "package(features = ['header_modules'])", "cc_library(", " name = 'a',", @@ -766,7 +721,8 @@ private void setupPackagesForSourcesWithSameBaseNameTests() throws Exception { public void testContainingSourcesWithSameBaseName() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, MockCcSupport.SUPPORTS_PIC_FEATURE); + .setupCcToolchainConfig( + mockToolsConfig, CcToolchainConfig.builder().withFeatures(CppRuleClasses.SUPPORTS_PIC)); useConfiguration("--cpu=k8"); setupPackagesForSourcesWithSameBaseNameTests(); getConfiguredTarget("//foo:lib"); @@ -861,10 +817,10 @@ private void setupPackagesForModuleTests(boolean useHeaderModules) throws Except public void testCompileHeaderModulesTransitively() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.HEADER_MODULES_FEATURE_CONFIGURATION, - MockCcSupport.SUPPORTS_PIC_FEATURE); + CcToolchainConfig.builder() + .withFeatures(MockCcSupport.HEADER_MODULES_FEATURES, CppRuleClasses.SUPPORTS_PIC)); useConfiguration("--cpu=k8"); setupPackagesForModuleTests(/* useHeaderModules= */ false); @@ -943,10 +899,10 @@ public void testCompileHeaderModulesTransitively() throws Exception { public void testCompileUsingHeaderModulesTransitively() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.HEADER_MODULES_FEATURE_CONFIGURATION, - MockCcSupport.SUPPORTS_PIC_FEATURE); + CcToolchainConfig.builder() + .withFeatures(MockCcSupport.HEADER_MODULES_FEATURES, CppRuleClasses.SUPPORTS_PIC)); useConfiguration("--cpu=k8"); setupPackagesForModuleTests(/* useHeaderModules= */ true); invalidatePackages(); @@ -996,15 +952,17 @@ private void writeSimpleCcLibrary() throws Exception { public void testToolchainWithoutPicForNoPicCompilation() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.EMPTY_COMPILE_ACTION_CONFIG, - MockCcSupport.EMPTY_EXECUTABLE_ACTION_CONFIG, - MockCcSupport.EMPTY_DYNAMIC_LIBRARY_ACTION_CONFIG, - MockCcSupport.EMPTY_TRANSITIVE_DYNAMIC_LIBRARY_ACTION_CONFIG, - MockCcSupport.EMPTY_STATIC_LIBRARY_ACTION_CONFIG, - MockCcSupport.EMPTY_STRIP_ACTION_CONFIG, - MockCcSupport.NO_LEGACY_FEATURES_FEATURE); + CcToolchainConfig.builder() + .withFeatures(CppRuleClasses.NO_LEGACY_FEATURES) + .withActionConfigs( + CppActionNames.CPP_COMPILE, + CppActionNames.CPP_LINK_EXECUTABLE, + CppActionNames.CPP_LINK_NODEPS_DYNAMIC_LIBRARY, + CppActionNames.CPP_LINK_DYNAMIC_LIBRARY, + CppActionNames.CPP_LINK_STATIC_LIBRARY, + CppActionNames.STRIP)); useConfiguration("--features=-supports_pic"); scratchConfiguredTarget("a", "a", "cc_binary(name='a', srcs=['a.cc'], deps=[':b'])", @@ -1015,15 +973,16 @@ public void testToolchainWithoutPicForNoPicCompilation() throws Exception { public void testNoCppModuleMap() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.EMPTY_COMPILE_ACTION_CONFIG, - MockCcSupport.EMPTY_EXECUTABLE_ACTION_CONFIG, - MockCcSupport.EMPTY_STATIC_LIBRARY_ACTION_CONFIG, - MockCcSupport.EMPTY_DYNAMIC_LIBRARY_ACTION_CONFIG, - MockCcSupport.EMPTY_TRANSITIVE_DYNAMIC_LIBRARY_ACTION_CONFIG, - MockCcSupport.NO_LEGACY_FEATURES_FEATURE, - MockCcSupport.PIC_FEATURE); + CcToolchainConfig.builder() + .withFeatures(CppRuleClasses.NO_LEGACY_FEATURES, CppRuleClasses.PIC) + .withActionConfigs( + CppActionNames.CPP_COMPILE, + CppActionNames.CPP_LINK_EXECUTABLE, + CppActionNames.CPP_LINK_NODEPS_DYNAMIC_LIBRARY, + CppActionNames.CPP_LINK_DYNAMIC_LIBRARY, + CppActionNames.CPP_LINK_STATIC_LIBRARY)); useConfiguration(); writeSimpleCcLibrary(); assertNoCppModuleMapAction("//module:map"); @@ -1033,7 +992,8 @@ public void testNoCppModuleMap() throws Exception { public void testCppModuleMap() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, MockCcSupport.MODULE_MAPS_FEATURE); + .setupCcToolchainConfig( + mockToolsConfig, CcToolchainConfig.builder().withFeatures(CppRuleClasses.MODULE_MAPS)); useConfiguration(); writeSimpleCcLibrary(); CppModuleMapAction action = getCppModuleMapAction("//module:map"); @@ -1072,7 +1032,9 @@ public void testAllowDuplicateNonCompiledSources() throws Exception { public void testDoNotCompileSourceFilesInHeaders() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, MockCcSupport.PARSE_HEADERS_FEATURE_CONFIGURATION); + .setupCcToolchainConfig( + mockToolsConfig, + CcToolchainConfig.builder().withFeatures(CppRuleClasses.PARSE_HEADERS)); useConfiguration("--features=parse_headers"); ConfiguredTarget x = scratchConfiguredTarget("x", "x", "cc_library(name = 'x', hdrs = ['x.cc'])"); @@ -1083,7 +1045,9 @@ public void testDoNotCompileSourceFilesInHeaders() throws Exception { public void testProcessHeadersInDependencies() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, MockCcSupport.PARSE_HEADERS_FEATURE_CONFIGURATION); + .setupCcToolchainConfig( + mockToolsConfig, + CcToolchainConfig.builder().withFeatures(CppRuleClasses.PARSE_HEADERS)); useConfiguration("--features=parse_headers", "--process_headers_in_dependencies"); ConfiguredTarget x = scratchConfiguredTarget( @@ -1099,7 +1063,9 @@ public void testProcessHeadersInDependencies() throws Exception { public void testProcessHeadersInDependenciesOfBinaries() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, MockCcSupport.PARSE_HEADERS_FEATURE_CONFIGURATION); + .setupCcToolchainConfig( + mockToolsConfig, + CcToolchainConfig.builder().withFeatures(CppRuleClasses.PARSE_HEADERS)); useConfiguration("--features=parse_headers", "--process_headers_in_dependencies"); ConfiguredTarget x = scratchConfiguredTarget( @@ -1118,7 +1084,9 @@ public void testProcessHeadersInDependenciesOfBinaries() throws Exception { public void testDoNotProcessHeadersInDependencies() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, MockCcSupport.PARSE_HEADERS_FEATURE_CONFIGURATION); + .setupCcToolchainConfig( + mockToolsConfig, + CcToolchainConfig.builder().withFeatures(CppRuleClasses.PARSE_HEADERS)); useConfiguration("--features=parse_headers"); ConfiguredTarget x = scratchConfiguredTarget( @@ -1134,7 +1102,9 @@ public void testDoNotProcessHeadersInDependencies() throws Exception { public void testProcessHeadersInCompileOnlyMode() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, MockCcSupport.PARSE_HEADERS_FEATURE_CONFIGURATION); + .setupCcToolchainConfig( + mockToolsConfig, + CcToolchainConfig.builder().withFeatures(CppRuleClasses.PARSE_HEADERS)); useConfiguration("--features=parse_headers", "--process_headers_in_dependencies"); ConfiguredTarget y = scratchConfiguredTarget( @@ -1210,34 +1180,17 @@ public void testLinkOptsNotPassedToStaticLink() throws Exception { ")"); } - private static final String COMPILATION_MODE_FEATURES = "" - + "feature {" - + " name: 'dbg'" - + " flag_set {" - + " action: 'c++-compile'" - + " flag_group { flag: '-dbg' }" - + " }" - + "}" - + "feature {" - + " name: 'fastbuild'" - + " flag_set {" - + " action: 'c++-compile'" - + " flag_group { flag: '-fastbuild' }" - + " }" - + "}" - + "feature {" - + " name: 'opt'" - + " flag_set {" - + " action: 'c++-compile'" - + " flag_group { flag: '-opt' }" - + " }" - + "}"; + // cc_toolchain_config.bzl provides "dbg", "fastbuild" and "opt" feature when + // compilation_mode_features are requested. + private static final String COMPILATION_MODE_FEATURES = "compilation_mode_features"; private List getCompilationModeFlags(String... flags) throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( - mockToolsConfig, COMPILATION_MODE_FEATURES, MockCcSupport.SUPPORTS_PIC_FEATURE); + .setupCcToolchainConfig( + mockToolsConfig, + CcToolchainConfig.builder() + .withFeatures(COMPILATION_MODE_FEATURES, CppRuleClasses.SUPPORTS_PIC)); useConfiguration(flags); scratch.overwriteFile("mode/BUILD", "cc_library(name = 'a', srcs = ['a.cc'])"); getConfiguredTarget("//mode:a"); @@ -1270,10 +1223,12 @@ private List getHostAndTargetFlags(boolean useHost, boolean isDisabledBy throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.HOST_AND_NONHOST_CONFIGURATION, - MockCcSupport.SUPPORTS_PIC_FEATURE); + CcToolchainConfig.builder() + .withFeatures( + MockCcSupport.HOST_AND_NONHOST_CONFIGURATION_FEATURES, + CppRuleClasses.SUPPORTS_PIC)); scratch.overwriteFile("mode/BUILD", "cc_library(name = 'a', srcs = ['a.cc'])"); useConfiguration( "--cpu=k8", @@ -1409,10 +1364,12 @@ public void testConfigurableSrcs() throws Exception { public void alwaysAddStaticAndDynamicLibraryToFilesToBuildWhenBuilding() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.SUPPORTS_DYNAMIC_LINKER_FEATURE, - MockCcSupport.SUPPORTS_INTERFACE_SHARED_LIBRARIES_FEATURE); + CcToolchainConfig.builder() + .withFeatures( + CppRuleClasses.SUPPORTS_DYNAMIC_LINKER, + CppRuleClasses.SUPPORTS_INTERFACE_SHARED_LIBRARIES)); useConfiguration("--cpu=k8"); ConfiguredTarget target = scratchConfiguredTarget("a", "b", "cc_library(name = 'b', srcs = ['source.cc'])"); @@ -1480,10 +1437,12 @@ public void onlyAddOneWrappedLibraryWithSameLibraryIdentifierToLibraries() throw public void testCcLinkParamsHasDynamicLibrariesForRuntime() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.COPY_DYNAMIC_LIBRARIES_TO_BINARY_CONFIGURATION, - MockCcSupport.SUPPORTS_DYNAMIC_LINKER_FEATURE); + CcToolchainConfig.builder() + .withFeatures( + CppRuleClasses.COPY_DYNAMIC_LIBRARIES_TO_BINARY, + CppRuleClasses.SUPPORTS_DYNAMIC_LINKER)); useConfiguration("--cpu=k8", "--features=copy_dynamic_libraries_to_binary"); ConfiguredTarget target = scratchConfiguredTarget("a", "foo", "cc_library(name = 'foo', srcs = ['foo.cc'])"); @@ -1500,7 +1459,9 @@ public void testCcLinkParamsHasDynamicLibrariesForRuntime() throws Exception { public void testCcLinkParamsHasDynamicLibrariesForRuntimeWithoutCopyFeature() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, MockCcSupport.SUPPORTS_DYNAMIC_LINKER_FEATURE); + .setupCcToolchainConfig( + mockToolsConfig, + CcToolchainConfig.builder().withFeatures(CppRuleClasses.SUPPORTS_DYNAMIC_LINKER)); useConfiguration("--cpu=k8"); invalidatePackages(); ConfiguredTarget target = @@ -1545,7 +1506,9 @@ public void forbidBuildingAndWrappingSameLibraryIdentifier() throws Exception { public void testProcessedHeadersWithPicSharedLibsAndNoPicBinaries() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, MockCcSupport.PARSE_HEADERS_FEATURE_CONFIGURATION); + .setupCcToolchainConfig( + mockToolsConfig, + CcToolchainConfig.builder().withFeatures(CppRuleClasses.PARSE_HEADERS)); useConfiguration("--features=parse_headers", "-c", "opt"); // Should not crash scratchConfiguredTarget("a", "a", "cc_library(name='a', hdrs=['a.h'])"); diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderTest.java index bae9ebe99c2d99..ae86a79c5c0426 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderTest.java @@ -23,7 +23,7 @@ import com.google.devtools.build.lib.analysis.platform.ToolchainInfo; import com.google.devtools.build.lib.analysis.util.AnalysisMock; import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; -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; @@ -37,7 +37,8 @@ public class CcToolchainProviderTest extends BuildViewTestCase { public void testSkylarkCallables() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, MockCcSupport.SUPPORTS_PIC_FEATURE); + .setupCcToolchainConfig( + mockToolsConfig, CcToolchainConfig.builder().withFeatures(CppRuleClasses.SUPPORTS_PIC)); useConfiguration("--cpu=k8", "--force_pic"); scratch.file( "test/rule.bzl", @@ -229,7 +230,9 @@ public void testDisablingLinkingModeFlags() throws Exception { "linking_mode_flags { mode: MOSTLY_STATIC linker_flag: '-foo_from_linking_mode' }"); scratch.file("a/BUILD", "cc_library(name='a', srcs=['a.cc'])"); - useConfiguration("--noincompatible_disable_legacy_crosstool_fields"); + useConfiguration( + "--noincompatible_disable_legacy_crosstool_fields", + "--noincompatible_disable_crosstool_file"); CcToolchainProvider ccToolchainProvider = getCcToolchainProvider(); assertThat(ccToolchainProvider.getLegacyMostlyStaticLinkFlags(CompilationMode.OPT)) .contains("-foo_from_linking_mode"); diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigurationHelper.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigurationHelper.java deleted file mode 100644 index 5c43c3f7286c79..00000000000000 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigurationHelper.java +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright 2015 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.rules.cpp; - -import com.google.devtools.build.lib.analysis.util.AnalysisMock; -import com.google.devtools.build.lib.util.CPU; -import com.google.devtools.build.lib.util.OS; -import com.google.devtools.build.lib.vfs.FileSystemUtils; -import com.google.devtools.build.lib.vfs.Path; -import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig; -import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.ToolPath; -import com.google.protobuf.TextFormat; -import java.io.IOException; - -/** - * Helper class for the creation of crosstool toolchain factories. - */ -public class CrosstoolConfigurationHelper { - public static Path overwriteCrosstoolFile(Path workspace, String content) throws IOException { - Path crosstool = - workspace.getRelative(AnalysisMock.get().ccSupport().getMockCrosstoolPath() + "/CROSSTOOL"); - long newMTime = crosstool.exists() ? crosstool.getLastModifiedTime() + 1 : -1; - crosstool.delete(); - FileSystemUtils.createDirectoryAndParents(crosstool.getParentDirectory()); - FileSystemUtils.writeContentAsLatin1(crosstool, content); - crosstool.setLastModifiedTime(newMTime); - return crosstool; - } - - /** - * Overwrites the default CROSSTOOL file with one obtained by merging the simple complete - * toolchain with the given additional partial toolchain. Only {@code --cpu=piii} is valid. - */ - public static void overwriteCrosstoolWithToolchain( - Path workspace, CrosstoolConfig.CToolchain partial) throws IOException { - CrosstoolConfig.CrosstoolRelease.Builder release = - CrosstoolConfig.CrosstoolRelease.newBuilder(); - release.mergeFrom(simpleCompleteToolchainProto()); - CrosstoolConfig.CToolchain.Builder toolchain = CrosstoolConfig.CToolchain.newBuilder(); - toolchain.mergeFrom(release.getToolchain(0)); - toolchain.mergeFrom(partial); - release.setToolchain(0, toolchain.build()); - overwriteCrosstoolFile(workspace, TextFormat.printToString(release.build())); - } - - public static String defaultCpu() { - if (OS.getCurrent() == OS.WINDOWS) { - return "x64_windows"; - } else if (OS.getCurrent() == OS.LINUX) { - switch (CPU.getCurrent()) { - case X86_32: - return "piii"; - case X86_64: - return "k8"; - case PPC: - return "ppc"; - case ARM: - return "arm"; - case S390X: - return "s390x"; - default: - return "unknown"; - } - } - return OS.getCurrent() == OS.DARWIN ? "darwin" : "k8"; - } - - public static CrosstoolConfig.CrosstoolRelease simpleCompleteToolchainProto() { - CrosstoolConfig.CrosstoolRelease.Builder builder = - CrosstoolConfig.CrosstoolRelease.newBuilder() - .setMajorVersion("12") - .setMinorVersion("0") - .setDefaultTargetCpu(defaultCpu()); - CrosstoolConfig.CToolchain.Builder toolchainBuilder = newIncompleteToolchain(); - toolchainBuilder - .setToolchainIdentifier(defaultCpu() + "-toolchain") - .setHostSystemName("i686-unknown-linux-gnu") - .setTargetSystemName("i686-unknown-linux-gnu") - .setTargetCpu("k8") - .setTargetLibc("glibc-2.3.6") - .setCompiler("gcc-4.3.1") - .setAbiVersion("gcc-3.4") - .setAbiLibcVersion("2.3.2") - // add a submessage that implies support for '.so' files - .addFeature( - CrosstoolConfig.CToolchain.Feature.newBuilder() - .setName(CppRuleClasses.SUPPORTS_DYNAMIC_LINKER) - .setEnabled(true)) - .addCxxBuiltinIncludeDirectory("/include/directory"); - builder.addToolchain(toolchainBuilder); - return builder.build(); - } - - private static CrosstoolConfig.CToolchain.Builder newIncompleteToolchain() { - CrosstoolConfig.CToolchain.Builder builder = CrosstoolConfig.CToolchain.newBuilder(); - for (String tool : - new String[] { - "ar", - "cpp", - "gcc", - "gcov", - "ld", - "compat-ld", - "nm", - "objcopy", - "objdump", - "strip", - "dwp", - "gcov-tool" - }) { - builder.addToolPath(ToolPath.newBuilder().setName(tool).setPath(tool)); - } - return builder; - } -}