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 c213defcd55656..153a85764a070b 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 @@ -24,6 +24,7 @@ load( "feature_set", "flag_group", "flag_set", + "make_variable", "tool", "tool_path", "with_feature_set", @@ -70,6 +71,7 @@ _FEATURE_NAMES = struct( targets_windows = "targets_windows", static_link_cpp_runtimes = "static_link_cpp_runtimes", simple_compile_feature = "simple_compile_feature", + simple_link_feature = "simple_link_feature", link_env = "link_env", dynamic_linking_mode = "dynamic_linking_mode", static_linking_mode = "static_linking_mode", @@ -85,6 +87,14 @@ _FEATURE_NAMES = struct( absolute_path_directories = "absolute_path_directories", from_package = "from_package", change_tool = "change_tool", + module_map_without_extern_module = "module_map_without_extern_module", + generate_submodules = "generate_submodules", + foo = "foo_feature", + library_search_directories = "library_search_directories", + runtime_library_search_directories = "runtime_library_search_directories", + uses_ifso_variables = "uses_ifso_variables", + def_feature = "def", + strip_debug_symbols = "strip_debug_symbols", ) _no_legacy_features_feature = feature(name = _FEATURE_NAMES.no_legacy_features) @@ -632,6 +642,18 @@ _simple_compile_feature = feature( ], ) +_simple_link_feature = feature( + name = _FEATURE_NAMES.simple_link_feature, + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_link_executable], + flag_groups = [ + flag_group(flags = ["testlinkopt"]), + ], + ), + ], +) + _link_env_feature = feature( name = _FEATURE_NAMES.link_env, env_sets = [ @@ -846,6 +868,14 @@ _change_tool_feature = feature( name = _FEATURE_NAMES.change_tool, ) +_module_map_without_extern_module_feature = feature( + name = _FEATURE_NAMES.module_map_without_extern_module, +) + +_generate_submodules_feature = feature( + name = _FEATURE_NAMES.generate_submodules, +) + _multiple_tools_action_config = action_config( action_name = ACTION_NAMES.cpp_compile, tools = [ @@ -859,6 +889,92 @@ _multiple_tools_action_config = action_config( ], ) +_foo_feature = feature( + name = _FEATURE_NAMES.foo, +) + +_library_search_directories_feature = feature( + name = _FEATURE_NAMES.library_search_directories, + enabled = True, + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_link_executable], + flag_groups = [ + flag_group( + expand_if_available = "library_search_directories", + iterate_over = "library_search_directories", + flags = ["--library=%{library_search_directories}"], + ), + ], + ), + ], +) + +_runtime_library_search_directories_feature = feature( + name = _FEATURE_NAMES.runtime_library_search_directories, + enabled = True, + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_link_executable], + flag_groups = [ + flag_group( + expand_if_available = "runtime_library_search_directories", + iterate_over = "runtime_library_search_directories", + flags = ["--runtime_library=%{runtime_library_search_directories}"], + ), + ], + ), + ], +) + +_uses_ifso_variables_feature = feature( + name = _FEATURE_NAMES.uses_ifso_variables, + enabled = True, + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_link_dynamic_library], + flag_groups = [ + flag_group( + expand_if_available = "generate_interface_library", + flags = ["--generate_interface_library_was_available"], + ), + ], + ), + ], +) + +_def_feature = feature( + name = _FEATURE_NAMES.def_feature, + enabled = True, + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_link_executable], + flag_groups = [ + flag_group( + expand_if_available = "def_file_path", + flags = ["-qux_%{def_file_path}"], + ), + ], + ), + ], +) + +_strip_debug_symbols_feature = feature( + name = _FEATURE_NAMES.strip_debug_symbols, + enabled = True, + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_link_executable], + flag_groups = [ + flag_group( + expand_if_available = "strip_debug_symbols", + flags = ["-strip_stuff"], + ), + ], + ), + ], +) + _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, @@ -892,6 +1008,7 @@ _feature_name_to_feature = { _FEATURE_NAMES.module_maps: _module_maps_feature, _FEATURE_NAMES.static_link_cpp_runtimes: _static_link_cpp_runtimes_feature, _FEATURE_NAMES.simple_compile_feature: _simple_compile_feature, + _FEATURE_NAMES.simple_link_feature: _simple_link_feature, _FEATURE_NAMES.link_env: _link_env_feature, _FEATURE_NAMES.static_linking_mode: _static_linking_mode_feature, _FEATURE_NAMES.dynamic_linking_mode: _dynamic_linking_mode_feature, @@ -903,6 +1020,14 @@ _feature_name_to_feature = { _FEATURE_NAMES.from_package: _from_package_feature, _FEATURE_NAMES.absolute_path_directories: _absolute_path_directories_feature, _FEATURE_NAMES.change_tool: _change_tool_feature, + _FEATURE_NAMES.module_map_without_extern_module: _module_map_without_extern_module_feature, + _FEATURE_NAMES.foo: _foo_feature, + _FEATURE_NAMES.library_search_directories: _library_search_directories_feature, + _FEATURE_NAMES.runtime_library_search_directories: _runtime_library_search_directories_feature, + _FEATURE_NAMES.generate_submodules: _generate_submodules_feature, + _FEATURE_NAMES.uses_ifso_variables: _uses_ifso_variables_feature, + _FEATURE_NAMES.def_feature: _def_feature, + _FEATURE_NAMES.strip_debug_symbols: _strip_debug_symbols_feature, "header_modules_feature_configuration": _header_modules_feature_configuration, "env_var_feature_configuration": _env_var_feature_configuration, "host_and_nonhost_configuration": _host_and_nonhost_configuration, @@ -1019,7 +1144,10 @@ def _impl(ctx): if should_add_multiple_tools_action_config: action_configs.append(_multiple_tools_action_config) - make_variables = [] + make_variables = [ + make_variable(name = name, value = value) + for name, value in ctx.attr.make_variables.items() + ] if ctx.attr.tool_paths == {}: tool_paths = [ @@ -1090,6 +1218,7 @@ cc_toolchain_config = rule( "builtin_sysroot": attr.string(default = "/usr/grte/v1"), "tool_paths": attr.string_dict(), "cxx_builtin_include_directories": attr.string_list(), + "make_variables": attr.string_dict(), }, provides = [CcToolchainConfigInfo], executable = True, 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 810277013083c3..9482dd25123455 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 @@ -57,6 +57,7 @@ public static final class CcToolchainConfig { private final ImmutableList> artifactNamePatterns; private final ImmutableList> toolPaths; private final ImmutableList cxxBuiltinIncludeDirectories; + private final ImmutableList> makeVariables; private CcToolchainConfig( String cpu, @@ -72,7 +73,8 @@ private CcToolchainConfig( ImmutableList actionConfigs, ImmutableList> artifactNamePatterns, ImmutableList> toolPaths, - ImmutableList cxxBuiltinIncludeDirectories) { + ImmutableList cxxBuiltinIncludeDirectories, + ImmutableList> makeVariables) { this.cpu = cpu; this.compiler = compiler; this.toolchainIdentifier = toolchainIdentifier; @@ -87,6 +89,7 @@ private CcToolchainConfig( this.toolPaths = toolPaths; this.builtinSysroot = builtinSysroot; this.cxxBuiltinIncludeDirectories = cxxBuiltinIncludeDirectories; + this.makeVariables = makeVariables; } public static Builder builder() { @@ -101,6 +104,7 @@ public static class Builder { private ImmutableList> toolPaths = ImmutableList.of(); private String builtinSysroot = "/usr/grte/v1"; private ImmutableList cxxBuiltinIncludeDirectories = ImmutableList.of(); + private ImmutableList> makeVariables = ImmutableList.of(); public Builder withFeatures(String... features) { this.features = ImmutableList.copyOf(features); @@ -138,6 +142,11 @@ public Builder withCxxBuiltinIncludeDirectories(String... directories) { return this; } + public Builder withMakeVariables(Pair... makeVariables) { + this.makeVariables = ImmutableList.copyOf(makeVariables); + return this; + } + public CcToolchainConfig build() { return new CcToolchainConfig( /* cpu= */ "k8", @@ -153,7 +162,8 @@ public CcToolchainConfig build() { actionConfigs, artifactNamePatterns, toolPaths, - cxxBuiltinIncludeDirectories); + cxxBuiltinIncludeDirectories, + makeVariables); } } @@ -188,7 +198,8 @@ public static CcToolchainConfig getCcToolchainConfigForCpu(String cpu) { /* actionConfigs= */ ImmutableList.of(), /* artifactNamePatterns= */ ImmutableList.of(), /* toolPaths= */ ImmutableList.of(), - /* cxxBuiltinIncludeDirectories= */ ImmutableList.of()); + /* cxxBuiltinIncludeDirectories= */ ImmutableList.of(), + /* makeVariables= */ ImmutableList.of()); } public static CcToolchainConfig getDefaultCcToolchainConfig() { @@ -219,6 +230,10 @@ String getCcToolchainConfigRule() { cxxBuiltinIncludeDirectories.stream() .map(directory -> "'" + directory + "'") .collect(ImmutableList.toImmutableList()); + ImmutableList makeVariablesList = + makeVariables.stream() + .map(variable -> String.format("'%s': '%s'", variable.first, variable.second)) + .collect(ImmutableList.toImmutableList()); return Joiner.on("\n") .join( @@ -240,8 +255,10 @@ String getCcToolchainConfigRule() { String.format(" tool_paths = {%s},", Joiner.on(",\n ").join(toolPathsList)), " builtin_sysroot = '" + builtinSysroot + "',", String.format( - " cxx_builtin_include_directories = [%s]", + " cxx_builtin_include_directories = [%s],", Joiner.on(",\n ").join(directoriesList)), + String.format( + " make_variables = {%s},", Joiner.on(",\n ").join(makeVariablesList)), " )"); } } @@ -329,7 +346,8 @@ public void write() throws IOException { /* actionConfigs= */ ImmutableList.of(), /* artifactNamePatterns= */ ImmutableList.of(), /* toolPaths= */ ImmutableList.of(), - /* cxxBuiltinIncludeDirectories= */ ImmutableList.of())); + /* cxxBuiltinIncludeDirectories= */ ImmutableList.of(), + /* makeVariables= */ ImmutableList.of())); } ccToolchainConfigs = toolchainConfigInfoBuilder.build(); } diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTest.java index d159149a806d10..e7f29e2518cb42 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTest.java @@ -29,7 +29,7 @@ import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; import com.google.devtools.build.lib.packages.SkylarkInfo; import com.google.devtools.build.lib.packages.StructImpl; -import com.google.devtools.build.lib.packages.util.MockCcSupport; +import com.google.devtools.build.lib.packages.util.Crosstool.CcToolchainConfig; import com.google.devtools.build.lib.packages.util.ResourceLoader; import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.ActionConfig; import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.ArtifactNamePattern; @@ -144,7 +144,8 @@ public void testGetToolForAction() throws Exception { public void testFeatureConfigurationWithAdditionalEnabledFeature() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, "feature { name: 'foo_feature' }"); + .setupCcToolchainConfig( + mockToolsConfig, CcToolchainConfig.builder().withFeatures("foo_feature")); useConfiguration(); scratch.file( "a/BUILD", @@ -181,7 +182,8 @@ public void testFeatureConfigurationWithAdditionalEnabledFeature() throws Except public void testFeatureConfigurationWithAdditionalUnsupportedFeature() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, "feature { name: 'foo_feature' }"); + .setupCcToolchainConfig( + mockToolsConfig, CcToolchainConfig.builder().withFeatures("foo_feature")); useConfiguration("--features=foo_feature"); scratch.file( "a/BUILD", @@ -466,7 +468,9 @@ public void testEmptyCompileBuildVariables() throws Exception { AnalysisMock.get() .ccSupport() .setupCrosstool(mockToolsConfig, "compiler_flag: '-foo'", "cxx_flag: '-foo_for_cxx_only'"); - useConfiguration("--noincompatible_disable_legacy_crosstool_fields"); + useConfiguration( + "--noincompatible_disable_legacy_crosstool_fields", + "--noincompatible_disable_crosstool_file"); SkylarkList commandLine = commandLineForVariables( CppActionNames.CPP_COMPILE, @@ -483,7 +487,9 @@ public void testEmptyCompileBuildVariablesForCxx() throws Exception { AnalysisMock.get() .ccSupport() .setupCrosstool(mockToolsConfig, "compiler_flag: '-foo'", "cxx_flag: '-foo_for_cxx_only'"); - useConfiguration("--noincompatible_disable_legacy_crosstool_fields"); + useConfiguration( + "--noincompatible_disable_legacy_crosstool_fields", + "--noincompatible_disable_crosstool_file"); assertThat( commandLineForVariables( CppActionNames.CPP_COMPILE, @@ -552,10 +558,12 @@ public void testCompileBuildVariablesForDefines() throws Exception { @Test public void testCompileBuildVariablesForPic() throws Exception { - getAnalysisMock() + AnalysisMock.get() .ccSupport() - .setupCrosstool( - mockToolsConfig, MockCcSupport.SUPPORTS_PIC_FEATURE, MockCcSupport.PIC_FEATURE); + .setupCcToolchainConfig( + mockToolsConfig, + CcToolchainConfig.builder() + .withFeatures(CppRuleClasses.SUPPORTS_PIC, CppRuleClasses.PIC)); useConfiguration(); assertThat( commandLineForVariables( @@ -628,7 +636,8 @@ public void testEmptyLinkVariables() throws Exception { public void testEmptyLinkVariablesContainSysroot() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool(mockToolsConfig, "builtin_sysroot: '/foo/bar/sysroot'"); + .setupCcToolchainConfig( + mockToolsConfig, CcToolchainConfig.builder().withSysroot("/foo/bar/sysroot")); useConfiguration(); assertThat( commandLineForVariables( @@ -644,20 +653,9 @@ public void testEmptyLinkVariablesContainSysroot() throws Exception { public void testLibrarySearchDirectoriesLinkVariables() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - "feature {", - " name: 'library_search_directories'", - " enabled: true", - " flag_set {", - " action: 'c++-link-executable'", - " flag_group {", - " expand_if_all_available: 'library_search_directories'", - " iterate_over: 'library_search_directories'", - " flag: '--library=%{library_search_directories}'", - " }", - " }", - "}"); + CcToolchainConfig.builder().withFeatures("library_search_directories")); useConfiguration(); assertThat( commandLineForVariables( @@ -675,20 +673,9 @@ public void testLibrarySearchDirectoriesLinkVariables() throws Exception { public void testRuntimeLibrarySearchDirectoriesLinkVariables() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - "feature {", - " name: 'runtime_library_search_directories'", - " enabled: true", - " flag_set {", - " action: 'c++-link-executable'", - " flag_group {", - " expand_if_all_available: 'runtime_library_search_directories'", - " iterate_over: 'runtime_library_search_directories'", - " flag: '--runtime_library=%{runtime_library_search_directories}'", - " }", - " }", - "}"); + CcToolchainConfig.builder().withFeatures("runtime_library_search_directories")); useConfiguration(); assertThat( commandLineForVariables( @@ -749,19 +736,8 @@ public void testUserLinkFlagsLinkVariablesAsDepsetWhenDisabled() throws Exceptio public void testIfsoRelatedVariablesAreNotExposed() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( - mockToolsConfig, - "feature {", - " name: 'uses_ifso_variables'", - " enabled: true", - " flag_set {", - " action: 'c++-link-dynamic-library'", - " flag_group {", - " expand_if_all_available: 'generate_interface_library'", - " flag: '--generate_interface_library_was_available'", - " }", - " }", - "}"); + .setupCcToolchainConfig( + mockToolsConfig, CcToolchainConfig.builder().withFeatures("uses_ifso_variables")); useConfiguration(); assertThat( commandLineForVariables( @@ -790,8 +766,9 @@ public void testOutputFileLinkVariables() throws Exception { public void testParamFileLinkVariables() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( - mockToolsConfig, "feature {", " name: 'do_not_split_linking_cmdline'", "}"); + .setupCcToolchainConfig( + mockToolsConfig, + CcToolchainConfig.builder().withFeatures(CppRuleClasses.DO_NOT_SPLIT_LINKING_CMDLINE)); assertThat( commandLineForVariables( CppActionNames.CPP_LINK_EXECUTABLE, @@ -807,19 +784,7 @@ public void testParamFileLinkVariables() throws Exception { public void testDefFileLinkVariables() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( - mockToolsConfig, - "feature {", - " name: 'def'", - " enabled: true", - " flag_set {", - " action: 'c++-link-executable'", - " flag_group {", - " expand_if_all_available: 'def_file_path'", - " flag: '-qux_%{def_file_path}'", - " }", - " }", - "}"); + .setupCcToolchainConfig(mockToolsConfig, CcToolchainConfig.builder().withFeatures("def")); useConfiguration(); assertThat( commandLineForVariables( @@ -836,19 +801,9 @@ public void testDefFileLinkVariables() throws Exception { public void testMustKeepDebugLinkVariables() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( - mockToolsConfig, - "feature {", - " name: 'strip_debug_symbols'", - " enabled: true", - " flag_set {", - " action: 'c++-link-executable'", - " flag_group {", - " expand_if_all_available: 'strip_debug_symbols'", - " flag: '-strip_stuff'", - " }", - " }", - "}"); + .setupCcToolchainConfig( + mockToolsConfig, CcToolchainConfig.builder().withFeatures("strip_debug_symbols")); + useConfiguration(); assertThat( commandLineForVariables( @@ -930,7 +885,9 @@ public void testUseTestOnlyFlagsLinkVariables() throws Exception { AnalysisMock.get() .ccSupport() .setupCrosstool(mockToolsConfig, "test_only_linker_flag: '-im_only_testing_flag'"); - useConfiguration("--noincompatible_disable_legacy_crosstool_fields"); + useConfiguration( + "--noincompatible_disable_legacy_crosstool_fields", + "--noincompatible_disable_crosstool_file"); assertThat( commandLineForVariables( CppActionNames.CPP_LINK_EXECUTABLE, @@ -967,7 +924,9 @@ public void testIsStaticLinkingModeLinkVariables() throws Exception { " mode: DYNAMIC", " linker_flag: '-dynamic_linking_mode_flag'", "}"); - useConfiguration("--noincompatible_disable_legacy_crosstool_fields"); + useConfiguration( + "--noincompatible_disable_legacy_crosstool_fields", + "--noincompatible_disable_crosstool_file"); SkylarkList staticLinkingModeFlags = commandLineForVariables( CppActionNames.CPP_LINK_EXECUTABLE, @@ -1200,11 +1159,13 @@ public void testFlagWhitelist() throws Exception { public void testCcLinkingContextOnWindows() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.COPY_DYNAMIC_LIBRARIES_TO_BINARY_CONFIGURATION, - MockCcSupport.TARGETS_WINDOWS_CONFIGURATION, - MockCcSupport.SUPPORTS_DYNAMIC_LINKER_FEATURE); + CcToolchainConfig.builder() + .withFeatures( + CppRuleClasses.COPY_DYNAMIC_LIBRARIES_TO_BINARY, + CppRuleClasses.TARGETS_WINDOWS, + CppRuleClasses.SUPPORTS_DYNAMIC_LINKER)); doTestCcLinkingContext( ImmutableList.of("a.a", "libdep2.a", "b.a", "c.a", "d.a", "libdep1.a"), ImmutableList.of("a.pic.a", "b.pic.a", "c.pic.a", "e.pic.a"), @@ -1215,11 +1176,13 @@ public void testCcLinkingContextOnWindows() throws Exception { public void testCcLinkingContext() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - MockCcSupport.PIC_FEATURE, - MockCcSupport.SUPPORTS_PIC_FEATURE, - MockCcSupport.SUPPORTS_DYNAMIC_LINKER_FEATURE); + CcToolchainConfig.builder() + .withFeatures( + CppRuleClasses.PIC, + CppRuleClasses.SUPPORTS_PIC, + CppRuleClasses.SUPPORTS_DYNAMIC_LINKER)); doTestCcLinkingContext( ImmutableList.of("a.a", "b.a", "c.a", "d.a"), ImmutableList.of("a.pic.a", "libdep2.a", "b.pic.a", "c.pic.a", "e.pic.a", "libdep1.a"), @@ -4613,12 +4576,10 @@ public void testCcToolchainInfoToProtoBareMinimum() throws Exception { public void testGetLegacyCcFlagsMakeVariable() throws Exception { AnalysisMock.get() .ccSupport() - .setupCrosstool( + .setupCcToolchainConfig( mockToolsConfig, - "make_variable {", - " name: 'CC_FLAGS'", - " value: '-test-cflag1 -testcflag2'", - "}"); + CcToolchainConfig.builder() + .withMakeVariables(Pair.of("CC_FLAGS", "-test-cflag1 -testcflag2"))); useConfiguration("--incompatible_disable_genrule_cc_toolchain_dependency"); loadCcToolchainConfigLib();