From 92cba040904c2ccbd0b5a70263de739f72c592ae Mon Sep 17 00:00:00 2001 From: Googler Date: Fri, 22 Dec 2023 06:42:10 -0800 Subject: [PATCH] Starlarkify `CcToolchainAttributesProvider` and delete it. After `incompatible_enable_cc_toolchain_resolution` has been made no-op - this provider served no purpose other than being a simple placeholder. This should be the final cl from the series of preparing `CcToolchainProvider` for Starlarkification. After this I think all blockers are gone and I will try to switch `CcToolchainInfo` to Starlark version. PiperOrigin-RevId: 593108847 Change-Id: I3b66288df7b123fbcdb9ed699e04ba54ddb1b275 --- .../lib/rules/cpp/CcStarlarkInternal.java | 229 ++++-- .../cpp/CcToolchainAttributesProvider.java | 692 ------------------ .../lib/rules/cpp/CcToolchainProvider.java | 20 +- .../builtins_bzl/common/cc/cc_toolchain.bzl | 122 ++- .../cc/cc_toolchain_provider_helper.bzl | 66 +- 5 files changed, 317 insertions(+), 812 deletions(-) delete mode 100644 src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainAttributesProvider.java diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcStarlarkInternal.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcStarlarkInternal.java index 331965fa18749c..f93651425e3e94 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcStarlarkInternal.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcStarlarkInternal.java @@ -20,8 +20,15 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.devtools.build.docgen.annot.DocCategory; +import com.google.devtools.build.lib.actions.Actions; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.actions.CommandLineExpansionException; +import com.google.devtools.build.lib.analysis.LicensesProvider; +import com.google.devtools.build.lib.analysis.LicensesProvider.TargetLicense; +import com.google.devtools.build.lib.analysis.LicensesProviderImpl; +import com.google.devtools.build.lib.analysis.OutputGroupInfo; +import com.google.devtools.build.lib.analysis.PackageSpecificationProvider; +import com.google.devtools.build.lib.analysis.RuleContext; import com.google.devtools.build.lib.analysis.actions.ActionConstructionContext; import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue; import com.google.devtools.build.lib.analysis.starlark.StarlarkActionFactory; @@ -30,8 +37,11 @@ import com.google.devtools.build.lib.collect.nestedset.Depset; import com.google.devtools.build.lib.collect.nestedset.Depset.TypeException; import com.google.devtools.build.lib.collect.nestedset.NestedSet; +import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; +import com.google.devtools.build.lib.collect.nestedset.Order; import com.google.devtools.build.lib.packages.Attribute.ComputedDefault; import com.google.devtools.build.lib.packages.AttributeMap; +import com.google.devtools.build.lib.packages.License; import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException; import com.google.devtools.build.lib.packages.StarlarkProvider; import com.google.devtools.build.lib.packages.Type; @@ -83,18 +93,6 @@ private ImmutableMap castDict(Dict d) throws EvalExc .collect(toImmutableMap(Pair::getFirst, Pair::getSecond)); } - @StarlarkMethod( - name = "construct_cc_toolchain_attributes_info", - documented = false, - parameters = { - @Param(name = "ctx", positional = false, named = true), - @Param(name = "is_apple", positional = false, named = true), - }) - public CcToolchainAttributesProvider constructCcToolchainAttributesInfo( - StarlarkRuleContext ruleContext, boolean isApple) throws EvalException { - return new CcToolchainAttributesProvider(ruleContext.getRuleContext(), isApple); - } - @StarlarkMethod( name = "construct_toolchain_provider", documented = false, @@ -103,7 +101,6 @@ public CcToolchainAttributesProvider constructCcToolchainAttributesInfo( @Param(name = "cpp_config", positional = false, named = true), @Param(name = "toolchain_features", positional = false, named = true), @Param(name = "tools_directory", positional = false, named = true), - @Param(name = "attributes", positional = false, named = true), @Param( name = "static_runtime_link_inputs", positional = false, @@ -145,13 +142,31 @@ public CcToolchainAttributesProvider constructCcToolchainAttributesInfo( @Param(name = "gcov", positional = false, named = true), @Param(name = "vars", positional = false, named = true), @Param(name = "xcode_config_info", positional = false, named = true), + @Param(name = "all_files", positional = false, named = true), + @Param(name = "all_files_including_libc", positional = false, named = true), + @Param(name = "compiler_files", positional = false, named = true), + @Param(name = "compiler_files_without_includes", positional = false, named = true), + @Param(name = "strip_files", positional = false, named = true), + @Param(name = "objcopy_files", positional = false, named = true), + @Param(name = "as_files", positional = false, named = true), + @Param(name = "ar_files", positional = false, named = true), + @Param(name = "linker_files", positional = false, named = true), + @Param(name = "if_so_builder", positional = false, named = true), + @Param(name = "dwp_files", positional = false, named = true), + @Param(name = "coverage_files", positional = false, named = true), + @Param(name = "supports_param_files", positional = false, named = true), + @Param(name = "supports_header_parsing", positional = false, named = true), + @Param(name = "link_dynamic_library_tool", positional = false, named = true), + @Param(name = "grep_includes", positional = false, named = true), + @Param(name = "licenses_provider", positional = false, named = true), + @Param(name = "allowlist_for_layering_check", positional = false, named = true), + @Param(name = "build_info_files", positional = false, named = true), }) public CcToolchainProvider getCcToolchainProvider( StarlarkRuleContext ruleContext, Object cppConfigurationObject, CcToolchainFeatures toolchainFeatures, String toolsDirectoryStr, - CcToolchainAttributesProvider attributes, Object staticRuntimeLinkInputsObject, Object dynamicRuntimeLinkInputsObject, String dynamicRuntimeSolibDirStr, @@ -178,7 +193,26 @@ public CcToolchainProvider getCcToolchainProvider( String ldExecutable, String gcovExecutable, Object vars, - Object xcodeConfigInfoObject) + Object xcodeConfigInfoObject, + Depset allFiles, + Depset allFilesIncludingLibc, + Depset compilerFiles, + Depset compilerFilesWithoutIncludes, + Depset stripFiles, + Depset objcopyFiles, + Depset asFiles, + Depset arFiles, + Depset fullInputsForLink, + Artifact ifsoBuilder, + Depset dwpFiles, + Depset coverageFiles, + Boolean supportsParamFiles, + Boolean supportsHeaderParsing, + Artifact linkDynamicLibraryTool, + Object grepIncludesObject, + Object licensesProviderObject, + PackageSpecificationProvider allowlistForLayeringCheck, + OutputGroupInfo buildInfoFiles) throws EvalException, InterruptedException { CppConfiguration cppConfiguration = CcModule.convertFromNoneable(cppConfigurationObject, null); PathFragment toolsDirectory = PathFragment.create(toolsDirectoryStr); @@ -219,68 +253,76 @@ public CcToolchainProvider getCcToolchainProvider( if (xcodeConfigInfoObject != Starlark.NONE) { xcodeConfigInfo = (XcodeConfigInfo) xcodeConfigInfoObject; } - return new CcToolchainProvider( - /* cppConfiguration= */ cppConfiguration, - /* toolchainFeatures= */ toolchainFeatures, - /* crosstoolTopPathFragment= */ toolsDirectory, - /* allFiles= */ attributes.getAllFiles(), - /* allFilesIncludingLibc= */ attributes.getFullInputsForCrosstool(), - /* compilerFiles= */ attributes.getCompilerFiles(), - /* compilerFilesWithoutIncludes= */ attributes.getCompilerFilesWithoutIncludes(), - /* stripFiles= */ attributes.getStripFiles(), - /* objcopyFiles= */ attributes.getObjcopyFiles(), - /* asFiles= */ attributes.getAsFiles(), - /* arFiles= */ attributes.getArFiles(), - /* linkerFiles= */ attributes.getFullInputsForLink(), - /* interfaceSoBuilder= */ attributes.getIfsoBuilder(), - /* dwpFiles= */ attributes.getDwpFiles(), - /* coverageFiles= */ attributes.getCoverage(), - /* staticRuntimeLinkInputs= */ staticRuntimeLinkInputsSet, - /* dynamicRuntimeLinkInputs= */ dynamicRuntimeLinkInputsSet, - /* dynamicRuntimeSolibDir= */ dynamicRuntimeSolibDir, - /* ccCompilationContext= */ ccCompilationContext, - /* supportsParamFiles= */ attributes.isSupportsParamFiles(), - /* supportsHeaderParsing= */ attributes.isSupportsHeaderParsing(), - /* buildVariables= */ (CcToolchainVariables) vars, - /* builtinIncludeFiles= */ Sequence.cast( - builtinIncludeFiles, Artifact.class, "builtin_include_files") - .getImmutableList(), - /* linkDynamicLibraryTool= */ attributes.getLinkDynamicLibraryTool(), - /* grepIncludes= */ attributes.getGrepIncludes(), - /* builtInIncludeDirectories= */ builtInIncludeDirectories, - /* sysroot= */ sysroot, - /* fdoContext= */ fdoContext, - /* isToolConfiguration= */ isToolConfiguration, - /* licensesProvider= */ attributes.getLicensesProvider(), - /* toolPaths= */ ImmutableMap.copyOf( - Dict.cast(toolPathsDict, String.class, String.class, "tool_paths")), - /* toolchainIdentifier= */ toolchainConfigInfo.getToolchainIdentifier(), - /* compiler= */ toolchainConfigInfo.getCompiler(), - /* abiGlibcVersion= */ toolchainConfigInfo.getAbiLibcVersion(), - /* targetCpu= */ toolchainConfigInfo.getTargetCpu(), - /* targetOS= */ toolchainConfigInfo.getCcTargetOs(), - /* defaultSysroot= */ defaultSysroot, - /* runtimeSysroot= */ runtimeSysroot, - /* targetLibc= */ toolchainConfigInfo.getTargetLibc(), - /* ccToolchainLabel= */ ruleContext.getRuleContext().getLabel(), - /* solibDirectory= */ solibDirectory, - /* abi= */ toolchainConfigInfo.getAbiVersion(), - /* targetSystemName= */ toolchainConfigInfo.getTargetSystemName(), - /* additionalMakeVariables= */ ImmutableMap.copyOf(additionalMakeVariables), - /* legacyCcFlagsMakeVariable= */ legacyCcFlagsMakeVariable, - /* allowlistForLayeringCheck= */ attributes.getAllowlistForLayeringCheck(), - /* objcopyExecutable= */ objcopyExecutable, - /* compilerExecutable= */ compilerExecutable, - /* preprocessorExecutable= */ preprocessorExecutable, - /* nmExecutable= */ nmExecutable, - /* objdumpExecutable= */ objdumpExecutable, - /* arExecutable= */ arExecutable, - /* stripExecutable= */ stripExecutable, - /* ldExecutable= */ ldExecutable, - /* gcovExecutable= */ gcovExecutable, - /* ccToolchainVariablesBuilderFunc */ buildFunc, - /* xcodeConfigInfo */ xcodeConfigInfo, - /* ccBuildInfoTranslator */ attributes.getCcBuildInfoTranslator()); + try { + return new CcToolchainProvider( + /* cppConfiguration= */ cppConfiguration, + /* toolchainFeatures= */ toolchainFeatures, + /* crosstoolTopPathFragment= */ toolsDirectory, + /* allFiles= */ allFiles.getSet(Artifact.class), + /* allFilesIncludingLibc= */ allFilesIncludingLibc.getSet(Artifact.class), + /* compilerFiles= */ compilerFiles.getSet(Artifact.class), + /* compilerFilesWithoutIncludes= */ compilerFilesWithoutIncludes.getSet(Artifact.class), + /* stripFiles= */ stripFiles.getSet(Artifact.class), + /* objcopyFiles= */ objcopyFiles.getSet(Artifact.class), + /* asFiles= */ asFiles.getSet(Artifact.class), + /* arFiles= */ arFiles.getSet(Artifact.class), + /* linkerFiles= */ fullInputsForLink.getSet(Artifact.class), + /* interfaceSoBuilder= */ ifsoBuilder, + /* dwpFiles= */ dwpFiles.getSet(Artifact.class), + /* coverageFiles= */ coverageFiles.getSet(Artifact.class), + /* staticRuntimeLinkInputs= */ staticRuntimeLinkInputsSet, + /* dynamicRuntimeLinkInputs= */ dynamicRuntimeLinkInputsSet, + /* dynamicRuntimeSolibDir= */ dynamicRuntimeSolibDir, + /* ccCompilationContext= */ ccCompilationContext, + /* supportsParamFiles= */ supportsParamFiles, + /* supportsHeaderParsing= */ supportsHeaderParsing, + /* buildVariables= */ (CcToolchainVariables) vars, + /* builtinIncludeFiles= */ Sequence.cast( + builtinIncludeFiles, Artifact.class, "builtin_include_files") + .getImmutableList(), + /* linkDynamicLibraryTool= */ linkDynamicLibraryTool, + /* grepIncludes= */ grepIncludesObject == Starlark.NONE + ? null + : (Artifact) grepIncludesObject, + /* builtInIncludeDirectories= */ builtInIncludeDirectories, + /* sysroot= */ sysroot, + /* fdoContext= */ fdoContext, + /* isToolConfiguration= */ isToolConfiguration, + /* licensesProvider= */ licensesProviderObject == Starlark.NONE + ? null + : (LicensesProvider) licensesProviderObject, + /* toolPaths= */ ImmutableMap.copyOf( + Dict.cast(toolPathsDict, String.class, String.class, "tool_paths")), + /* toolchainIdentifier= */ toolchainConfigInfo.getToolchainIdentifier(), + /* compiler= */ toolchainConfigInfo.getCompiler(), + /* abiGlibcVersion= */ toolchainConfigInfo.getAbiLibcVersion(), + /* targetCpu= */ toolchainConfigInfo.getTargetCpu(), + /* targetOS= */ toolchainConfigInfo.getCcTargetOs(), + /* defaultSysroot= */ defaultSysroot, + /* runtimeSysroot= */ runtimeSysroot, + /* targetLibc= */ toolchainConfigInfo.getTargetLibc(), + /* ccToolchainLabel= */ ruleContext.getRuleContext().getLabel(), + /* solibDirectory= */ solibDirectory, + /* abi= */ toolchainConfigInfo.getAbiVersion(), + /* targetSystemName= */ toolchainConfigInfo.getTargetSystemName(), + /* additionalMakeVariables= */ ImmutableMap.copyOf(additionalMakeVariables), + /* legacyCcFlagsMakeVariable= */ legacyCcFlagsMakeVariable, + /* allowlistForLayeringCheck= */ allowlistForLayeringCheck, + /* objcopyExecutable= */ objcopyExecutable, + /* compilerExecutable= */ compilerExecutable, + /* preprocessorExecutable= */ preprocessorExecutable, + /* nmExecutable= */ nmExecutable, + /* objdumpExecutable= */ objdumpExecutable, + /* arExecutable= */ arExecutable, + /* stripExecutable= */ stripExecutable, + /* ldExecutable= */ ldExecutable, + /* gcovExecutable= */ gcovExecutable, + /* ccToolchainVariablesBuilderFunc= */ buildFunc, + /* xcodeConfigInfo= */ xcodeConfigInfo, + /* ccBuildInfoTranslator= */ buildInfoFiles); + } catch (TypeException e) { + throw new EvalException(e); + } } @StarlarkMethod( @@ -702,4 +744,37 @@ public AppleConfiguration getAppleConfigIfAvailable(StarlarkRuleContext ruleCont public StarlarkProvider buildSettingInfo() throws EvalException { return buildSettingInfo; } + + @StarlarkMethod( + name = "escape_label", + documented = false, + parameters = { + @Param(name = "label", positional = false, named = true), + }) + public String escapeLabel(Label label) { + return Actions.escapeLabel(label); + } + + @StarlarkMethod( + name = "licenses", + documented = false, + parameters = { + @Param(name = "ctx", positional = false, named = true), + }, + allowReturnNones = true) + @Nullable + public LicensesProvider getLicenses(StarlarkRuleContext starlarkRuleContext) { + RuleContext ruleContext = starlarkRuleContext.getRuleContext(); + final License outputLicense = + ruleContext.getRule().getToolOutputLicense(ruleContext.attributes()); + if (outputLicense != null && !outputLicense.equals(License.NO_LICENSE)) { + final NestedSet license = + NestedSetBuilder.create( + Order.STABLE_ORDER, new TargetLicense(ruleContext.getLabel(), outputLicense)); + return new LicensesProviderImpl( + license, new TargetLicense(ruleContext.getLabel(), outputLicense)); + } else { + return null; + } + } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainAttributesProvider.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainAttributesProvider.java deleted file mode 100644 index 571dbfcc1183f7..00000000000000 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainAttributesProvider.java +++ /dev/null @@ -1,692 +0,0 @@ -// Copyright 2018 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 static com.google.devtools.build.lib.packages.Type.BOOLEAN; - -import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableList; -import com.google.devtools.build.lib.actions.Actions; -import com.google.devtools.build.lib.actions.Artifact; -import com.google.devtools.build.lib.analysis.Allowlist; -import com.google.devtools.build.lib.analysis.FileProvider; -import com.google.devtools.build.lib.analysis.LicensesProvider; -import com.google.devtools.build.lib.analysis.LicensesProvider.TargetLicense; -import com.google.devtools.build.lib.analysis.LicensesProviderImpl; -import com.google.devtools.build.lib.analysis.OutputGroupInfo; -import com.google.devtools.build.lib.analysis.PackageSpecificationProvider; -import com.google.devtools.build.lib.analysis.RuleContext; -import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; -import com.google.devtools.build.lib.cmdline.Label; -import com.google.devtools.build.lib.collect.nestedset.Depset; -import com.google.devtools.build.lib.collect.nestedset.NestedSet; -import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; -import com.google.devtools.build.lib.collect.nestedset.Order; -import com.google.devtools.build.lib.packages.BuildType; -import com.google.devtools.build.lib.packages.BuiltinProvider; -import com.google.devtools.build.lib.packages.Info; -import com.google.devtools.build.lib.packages.License; -import com.google.devtools.build.lib.packages.NativeInfo; -import com.google.devtools.build.lib.packages.Type; -import javax.annotation.Nullable; -import net.starlark.java.annot.StarlarkMethod; -import net.starlark.java.eval.EvalException; -import net.starlark.java.eval.Sequence; -import net.starlark.java.eval.StarlarkList; -import net.starlark.java.eval.StarlarkThread; - -/** - * Provider encapsulating all the information from the cc_toolchain rule that affects creation of - * {@link CcToolchainProvider} - */ -// TODO(adonovan): rename s/Provider/Info/. -public class CcToolchainAttributesProvider extends NativeInfo implements HasCcToolchainLabel { - - public static final BuiltinProvider PROVIDER = - new BuiltinProvider( - "CcToolchainAttributesInfo", CcToolchainAttributesProvider.class) {}; - - private final boolean supportsParamFiles; - private final boolean supportsHeaderParsing; - private final NestedSet allFiles; - private final NestedSet compilerFiles; - private final NestedSet compilerFilesWithoutIncludes; - private final NestedSet stripFiles; - private final NestedSet objcopyFiles; - private final NestedSet asFiles; - private final NestedSet arFiles; - private final NestedSet linkerFiles; - private final NestedSet dwpFiles; - private final NestedSet libc; - private final TransitiveInfoCollection libcTop; - private final NestedSet targetLibc; - private final TransitiveInfoCollection targetLibcTop; - private final NestedSet fullInputsForCrosstool; - private final NestedSet fullInputsForLink; - private final NestedSet coverage; - private final Artifact ifsoBuilder; - private final Artifact linkDynamicLibraryTool; - @Nullable private final Artifact grepIncludes; - private final TransitiveInfoCollection fdoOptimize; - private final ImmutableList fdoOptimizeArtifacts; - private final FdoPrefetchHintsProvider fdoPrefetch; - private final PropellerOptimizeProvider propellerOptimize; - private final MemProfProfileProvider memprofProfileProvider; - private final TransitiveInfoCollection moduleMap; - private final Artifact moduleMapArtifact; - private final Artifact zipper; - private final String purposePrefix; - private final String runtimeSolibDirBase; - private final LicensesProvider licensesProvider; - private final Label toolchainType; - private final CcToolchainConfigInfo ccToolchainConfigInfo; - private final String toolchainIdentifier; - private final FdoProfileProvider fdoOptimizeProvider; - private final FdoProfileProvider fdoProfileProvider; - private final FdoProfileProvider csFdoProfileProvider; - private final FdoProfileProvider xfdoProfileProvider; - private final Label ccToolchainLabel; - private final TransitiveInfoCollection staticRuntimeLib; - private final TransitiveInfoCollection dynamicRuntimeLib; - private final PackageSpecificationProvider allowlistForLayeringCheck; - private final String lateBoundLibc; - private final String lateBoundTargetLibc; - private final OutputGroupInfo ccBuildInfoTranslator; - - public CcToolchainAttributesProvider(RuleContext ruleContext, boolean isAppleToolchain) { - super(); - this.ccToolchainLabel = ruleContext.getLabel(); - this.toolchainIdentifier = ruleContext.attributes().get("toolchain_identifier", Type.STRING); - - // grep_includes is not supported by Bazel. - String toolsRepository = ruleContext.getRuleClassProvider().getToolsRepository().getName(); - this.grepIncludes = - toolsRepository.isEmpty() ? ruleContext.getPrerequisiteArtifact("$grep_includes") : null; - - this.supportsParamFiles = ruleContext.attributes().get("supports_param_files", BOOLEAN); - this.supportsHeaderParsing = ruleContext.attributes().get("supports_header_parsing", BOOLEAN); - this.allFiles = getFiles(ruleContext, "all_files"); - this.compilerFiles = getFiles(ruleContext, "compiler_files"); - this.compilerFilesWithoutIncludes = - getOptionalFiles(ruleContext, "compiler_files_without_includes"); - this.stripFiles = getFiles(ruleContext, "strip_files"); - this.objcopyFiles = getFiles(ruleContext, "objcopy_files"); - this.asFiles = getOptionalFiles(ruleContext, "as_files"); - this.arFiles = getOptionalFiles(ruleContext, "ar_files"); - this.linkerFiles = getFiles(ruleContext, "linker_files"); - this.dwpFiles = getFiles(ruleContext, "dwp_files"); - - this.lateBoundLibc = getLateBoundLibc(ruleContext, "libc_top", ":libc_top"); - this.lateBoundTargetLibc = getLateBoundLibc(ruleContext, "libc_top", ":target_libc_top"); - - this.libc = getOptionalFiles(ruleContext, lateBoundLibc); - this.libcTop = ruleContext.getPrerequisite(lateBoundLibc); - - this.targetLibc = getOptionalFiles(ruleContext, lateBoundTargetLibc); - this.targetLibcTop = ruleContext.getPrerequisite(lateBoundTargetLibc); - - this.fullInputsForCrosstool = - NestedSetBuilder.stableOrder() - .addTransitive(allFiles) - .addTransitive(libc) - .build(); - this.fullInputsForLink = fullInputsForLink(ruleContext, linkerFiles, libc, isAppleToolchain); - NestedSet coverageFiles = getOptionalFiles(ruleContext, "coverage_files"); - if (coverageFiles.isEmpty()) { - this.coverage = Preconditions.checkNotNull(this.allFiles); - } else { - this.coverage = coverageFiles; - } - this.ifsoBuilder = ruleContext.getPrerequisiteArtifact("$interface_library_builder"); - this.linkDynamicLibraryTool = ruleContext.getPrerequisiteArtifact("$link_dynamic_library_tool"); - this.fdoProfileProvider = - ruleContext.getPrerequisite(CcToolchainRule.FDO_PROFILE_ATTR, FdoProfileProvider.PROVIDER); - this.csFdoProfileProvider = - ruleContext.getPrerequisite( - CcToolchainRule.CSFDO_PROFILE_ATTR, FdoProfileProvider.PROVIDER); - this.xfdoProfileProvider = - ruleContext.getPrerequisite(CcToolchainRule.XFDO_PROFILE_ATTR, FdoProfileProvider.PROVIDER); - this.fdoOptimizeProvider = - ruleContext.getPrerequisite(CcToolchainRule.FDO_OPTIMIZE_ATTR, FdoProfileProvider.PROVIDER); - this.fdoOptimize = ruleContext.getPrerequisite(CcToolchainRule.FDO_OPTIMIZE_ATTR); - this.fdoOptimizeArtifacts = - ruleContext.getPrerequisiteArtifacts(CcToolchainRule.FDO_OPTIMIZE_ATTR).list(); - this.fdoPrefetch = - ruleContext.getPrerequisite(":fdo_prefetch_hints", FdoPrefetchHintsProvider.PROVIDER); - this.propellerOptimize = - ruleContext.getPrerequisite(":propeller_optimize", PropellerOptimizeProvider.PROVIDER); - this.memprofProfileProvider = - ruleContext.getPrerequisite(":memprof_profile", MemProfProfileProvider.PROVIDER); - this.moduleMap = ruleContext.getPrerequisite("module_map"); - this.moduleMapArtifact = ruleContext.getPrerequisiteArtifact("module_map"); - this.zipper = ruleContext.getPrerequisiteArtifact(":zipper"); - this.purposePrefix = Actions.escapeLabel(ruleContext.getLabel()) + "_"; - this.runtimeSolibDirBase = "_solib_" + "_" + Actions.escapeLabel(ruleContext.getLabel()); - this.staticRuntimeLib = ruleContext.getPrerequisite("static_runtime_lib"); - this.dynamicRuntimeLib = ruleContext.getPrerequisite("dynamic_runtime_lib"); - this.ccToolchainConfigInfo = - ruleContext.getPrerequisite( - CcToolchainRule.TOOLCHAIN_CONFIG_ATTR, - CcToolchainConfigInfo.PROVIDER); - - // If output_license is specified on the cc_toolchain rule, override the transitive licenses - // with that one. This is necessary because cc_toolchain is used in the target configuration, - // but it is sort-of-kind-of a tool, but various parts of it are linked into the output... - // ...so we trust the judgment of the author of the cc_toolchain rule to figure out what - // licenses should be propagated to C++ targets. - // TODO(elenairina): Remove this and use Attribute.Builder.useOutputLicenses() on the - // :cc_toolchain attribute instead. - final License outputLicense = - ruleContext.getRule().getToolOutputLicense(ruleContext.attributes()); - if (outputLicense != null && !outputLicense.equals(License.NO_LICENSE)) { - final NestedSet license = - NestedSetBuilder.create( - Order.STABLE_ORDER, new TargetLicense(ruleContext.getLabel(), outputLicense)); - this.licensesProvider = - new LicensesProviderImpl( - license, new TargetLicense(ruleContext.getLabel(), outputLicense)); - } else { - this.licensesProvider = null; - } - // TODO(b/65835260): Remove this conditional once j2objc can learn the toolchain type. - if (ruleContext.attributes().has(CcToolchainRule.CC_TOOLCHAIN_TYPE_ATTRIBUTE_NAME)) { - this.toolchainType = - ruleContext - .attributes() - .get(CcToolchainRule.CC_TOOLCHAIN_TYPE_ATTRIBUTE_NAME, BuildType.LABEL); - } else { - this.toolchainType = null; - } - this.allowlistForLayeringCheck = - Allowlist.fetchPackageSpecificationProvider( - ruleContext, CcToolchainRule.ALLOWED_LAYERING_CHECK_FEATURES_ALLOWLIST); - this.ccBuildInfoTranslator = - OutputGroupInfo.get(ruleContext.getPrerequisite("$build_info_translator")); - } - - // This is to avoid Starlark limitation of not being able to have complex logic in configuration - // field. The logic here was encapsulated in native cc_toolchain rule's :libc_top's and - // :target_libc_top's LateBoundDefault attributes. - // In case :libc_top or :target_libc_top were not specified from command line, i.e. grte_top was - // not set we will try to use public attributes instead. - private static String getLateBoundLibc( - RuleContext ruleContext, String attribute, String implicitAttribute) { - if (ruleContext.getPrerequisite(implicitAttribute) == null) { - return attribute; - } - return implicitAttribute; - } - - @Override - public BuiltinProvider getProvider() { - return PROVIDER; - } - - public boolean isSupportsParamFiles() { - return supportsParamFiles; - } - - public String getPurposePrefix() { - return purposePrefix; - } - - @StarlarkMethod(name = "runtime_solib_dir_base", documented = false, useStarlarkThread = true) - public String getRuntimeSolibDirBaseForStarlark(StarlarkThread thread) throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return getRuntimeSolibDirBase(); - } - - public String getRuntimeSolibDirBase() { - return runtimeSolibDirBase; - } - - @StarlarkMethod( - name = "fdo_prefetch_provider", - documented = false, - useStarlarkThread = true, - allowReturnNones = true) - @Nullable - public FdoPrefetchHintsProvider getFdoPrefetchForStarlark(StarlarkThread thread) - throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return fdoPrefetch; - } - - public FdoPrefetchHintsProvider getFdoPrefetch() { - return fdoPrefetch; - } - - @StarlarkMethod( - name = "propeller_optimize_provider", - documented = false, - useStarlarkThread = true, - allowReturnNones = true) - @Nullable - public PropellerOptimizeProvider getPropellerOptimizeForStarlark(StarlarkThread thread) - throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return propellerOptimize; - } - - public PropellerOptimizeProvider getPropellerOptimize() { - return propellerOptimize; - } - - @StarlarkMethod( - name = "mem_prof_profile_provider", - documented = false, - useStarlarkThread = true, - allowReturnNones = true) - @Nullable - public MemProfProfileProvider getMemProfProfileProviderForStarlark(StarlarkThread thread) - throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return memprofProfileProvider; - } - - public MemProfProfileProvider getMemProfProfileProvider() { - return memprofProfileProvider; - } - - public String getToolchainIdentifier() { - return toolchainIdentifier; - } - - public Label getToolchainType() { - return toolchainType; - } - - @StarlarkMethod(name = "cc_toolchain_config_info", documented = false, useStarlarkThread = true) - public CcToolchainConfigInfo getCcToolchainConfigInfoForStarlark(StarlarkThread thread) - throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return getCcToolchainConfigInfo(); - } - - public CcToolchainConfigInfo getCcToolchainConfigInfo() { - return ccToolchainConfigInfo; - } - - @StarlarkMethod(name = "fdo_optimize_artifacts", documented = false, useStarlarkThread = true) - public Sequence getFdoOptimizeArtifactsForStarlark(StarlarkThread thread) - throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return StarlarkList.immutableCopyOf(fdoOptimizeArtifacts); - } - - public ImmutableList getFdoOptimizeArtifacts() { - return fdoOptimizeArtifacts; - } - - @StarlarkMethod( - name = "licenses_provider", - documented = false, - useStarlarkThread = true, - allowReturnNones = true) - @Nullable - public LicensesProvider getLicensesProviderForStarlark(StarlarkThread thread) - throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return getLicensesProvider(); - } - - public LicensesProvider getLicensesProvider() { - return licensesProvider; - } - - @StarlarkMethod( - name = "static_runtime_lib", - documented = false, - useStarlarkThread = true, - allowReturnNones = true) - @Nullable - public TransitiveInfoCollection getStaticRuntimeLibForStarlark(StarlarkThread thread) - throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return getStaticRuntimeLib(); - } - - public TransitiveInfoCollection getStaticRuntimeLib() { - return staticRuntimeLib; - } - - @StarlarkMethod( - name = "dynamic_runtime_lib", - documented = false, - useStarlarkThread = true, - allowReturnNones = true) - @Nullable - public TransitiveInfoCollection getDynamicRuntimeLibForStarlark(StarlarkThread thread) - throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return getDynamicRuntimeLib(); - } - - public TransitiveInfoCollection getDynamicRuntimeLib() { - return dynamicRuntimeLib; - } - - public boolean isSupportsHeaderParsing() { - return supportsHeaderParsing; - } - - @StarlarkMethod(name = "all_files", documented = false, useStarlarkThread = true) - public Depset getAllFilesForstarlark(StarlarkThread thread) throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return Depset.of(Artifact.class, allFiles); - } - - public NestedSet getAllFiles() { - return allFiles; - } - - public NestedSet getCompilerFiles() { - return compilerFiles; - } - - public NestedSet getStripFiles() { - return stripFiles; - } - - public NestedSet getObjcopyFiles() { - return objcopyFiles; - } - - @StarlarkMethod( - name = "fdo_optimize_label", - documented = false, - useStarlarkThread = true, - allowReturnNones = true) - @Nullable - public Label getFdoOptimizeForstarlark(StarlarkThread thread) throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - if (fdoOptimize == null) { - return null; - } - return fdoOptimize.getLabel(); - } - - public TransitiveInfoCollection getFdoOptimize() { - return fdoOptimize; - } - - public Artifact getLinkDynamicLibraryTool() { - return linkDynamicLibraryTool; - } - - @Nullable - public Artifact getGrepIncludes() { - return grepIncludes; - } - - @StarlarkMethod( - name = "module_map", - documented = false, - useStarlarkThread = true, - allowReturnNones = true) - @Nullable - public TransitiveInfoCollection getModuleMapForStarlark(StarlarkThread thread) - throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return getModuleMap(); - } - - public TransitiveInfoCollection getModuleMap() { - return moduleMap; - } - - public NestedSet getAsFiles() { - return asFiles; - } - - public NestedSet getArFiles() { - return arFiles; - } - - public TransitiveInfoCollection getLibcTop() { - return libcTop; - } - - public NestedSet getLinkerFiles() { - return linkerFiles; - } - - public NestedSet getDwpFiles() { - return dwpFiles; - } - - @StarlarkMethod( - name = "fdo_optimize_provider", - documented = false, - useStarlarkThread = true, - allowReturnNones = true) - @Nullable - public FdoProfileProvider getFdoOptimizeProviderForStarlark(StarlarkThread thread) - throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return fdoOptimizeProvider; - } - - public FdoProfileProvider getFdoOptimizeProvider() { - return fdoOptimizeProvider; - } - - @StarlarkMethod(name = "module_map_artifact", documented = false, useStarlarkThread = true) - public Artifact getModuleMapArtifactForStarlark(StarlarkThread thread) throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return getModuleMapArtifact(); - } - - public Artifact getModuleMapArtifact() { - return moduleMapArtifact; - } - - public NestedSet getFullInputsForCrosstool() { - return fullInputsForCrosstool; - } - - @StarlarkMethod( - name = "fdo_profile_provider", - documented = false, - useStarlarkThread = true, - allowReturnNones = true) - @Nullable - public FdoProfileProvider getFdoProfileProviderForStarlark(StarlarkThread thread) - throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return fdoProfileProvider; - } - - public FdoProfileProvider getFdoProfileProvider() { - return fdoProfileProvider; - } - - @StarlarkMethod( - name = "cs_fdo_profile_provider", - documented = false, - useStarlarkThread = true, - allowReturnNones = true) - @Nullable - public FdoProfileProvider getCsFdoProfileProviderForStarlark(StarlarkThread thread) - throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return csFdoProfileProvider; - } - - public FdoProfileProvider getCSFdoProfileProvider() { - return csFdoProfileProvider; - } - - @StarlarkMethod( - name = "x_fdo_profile_provider", - documented = false, - useStarlarkThread = true, - allowReturnNones = true) - @Nullable - public FdoProfileProvider getXFdoProfileProviderForStarlark(StarlarkThread thread) - throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return xfdoProfileProvider; - } - - public FdoProfileProvider getXFdoProfileProvider() { - return xfdoProfileProvider; - } - - @StarlarkMethod( - name = "zipper", - documented = false, - useStarlarkThread = true, - allowReturnNones = true) - @Nullable - public Artifact getZipperForStarlark(StarlarkThread thread) throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return zipper; - } - - public Artifact getZipper() { - return zipper; - } - - public NestedSet getFullInputsForLink() { - return fullInputsForLink; - } - - @StarlarkMethod(name = "cc_toolchain_label", documented = false, useStarlarkThread = true) - public Label getCcToolchainLabelForStarlark(StarlarkThread thread) throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return getCcToolchainLabel(); - } - - @Override - public Label getCcToolchainLabel() { - return ccToolchainLabel; - } - - public NestedSet getCoverage() { - return coverage; - } - - public NestedSet getCompilerFilesWithoutIncludes() { - return compilerFilesWithoutIncludes; - } - - @StarlarkMethod(name = "libc", documented = false, useStarlarkThread = true) - public Depset getLibcForStarlark(StarlarkThread thread) throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return Depset.of(Artifact.class, getLibc()); - } - - @StarlarkMethod(name = "target_libc", documented = false, useStarlarkThread = true) - public Depset getTargetLibcForstarlark(StarlarkThread thread) throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return Depset.of(Artifact.class, getTargetLibc()); - } - - public NestedSet getLibc() { - return libc; - } - - public NestedSet getTargetLibc() { - return targetLibc; - } - - public TransitiveInfoCollection getTargetLibcTop() { - return targetLibcTop; - } - - @StarlarkMethod( - name = "libc_top_label", - documented = false, - allowReturnNones = true, - useStarlarkThread = true) - @Nullable - public Label getLibcTopLabelForStarlark(StarlarkThread thread) throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return getLibcTopLabel(); - } - - @Nullable - public Label getLibcTopLabel() { - return getLibcTop() == null ? null : getLibcTop().getLabel(); - } - - @StarlarkMethod( - name = "target_libc_top_label", - documented = false, - allowReturnNones = true, - useStarlarkThread = true) - @Nullable - public Label getTargetLibcTopLabelForStarlark(StarlarkThread thread) throws EvalException { - CcModule.checkPrivateStarlarkificationAllowlist(thread); - return getTargetLibcTopLabel(); - } - - @Nullable - public Label getTargetLibcTopLabel() { - return getTargetLibcTop() == null ? null : getTargetLibcTop().getLabel(); - } - - public Artifact getIfsoBuilder() { - return ifsoBuilder; - } - - public PackageSpecificationProvider getAllowlistForLayeringCheck() { - return allowlistForLayeringCheck; - } - - public OutputGroupInfo getCcBuildInfoTranslator() { - return ccBuildInfoTranslator; - } - - private static NestedSet getFiles(RuleContext context, String attribute) { - TransitiveInfoCollection dep = context.getPrerequisite(attribute); - return dep.getProvider(FileProvider.class).getFilesToBuild(); - } - - private static NestedSet getOptionalFiles(RuleContext ruleContext, String attribute) { - TransitiveInfoCollection dep = ruleContext.getPrerequisite(attribute); - return dep != null - ? dep.getProvider(FileProvider.class).getFilesToBuild() - : NestedSetBuilder.emptySet(Order.STABLE_ORDER); - } - - /** - * Returns the allFiles-derived link action inputs for a given rule. Adds the given set of - * artifacts as extra inputs. - */ - private static NestedSet fullInputsForLink( - RuleContext ruleContext, - NestedSet link, - NestedSet libcMiddleman, - boolean isAppleToolchain) { - NestedSetBuilder builder = - NestedSetBuilder.stableOrder().addTransitive(link).addTransitive(libcMiddleman); - if (!isAppleToolchain) { - builder - .add(ruleContext.getPrerequisiteArtifact("$interface_library_builder")) - .add(ruleContext.getPrerequisiteArtifact("$link_dynamic_library_tool")); - } - return builder.build(); - } -} - -/** - * Temporary interface to cover common interface of {@link CcToolchainAttributesProvider} and {@link - * CcToolchainProvider}. - */ -// TODO(b/113849758): Remove once behavior is migrated. -interface HasCcToolchainLabel extends Info { - Label getCcToolchainLabel(); -} diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProvider.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProvider.java index 51b58eea06cc53..bd85b311d7788d 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProvider.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProvider.java @@ -56,16 +56,15 @@ @Immutable public final class CcToolchainProvider extends NativeInfo implements CcToolchainProviderApi< - FeatureConfigurationForStarlark, - BranchFdoProfile, - FdoContext, - ConstraintValueInfo, - StarlarkRuleContext, - InvalidConfigurationException, - CppConfiguration, - CcToolchainVariables, - OutputGroupInfo>, - HasCcToolchainLabel { + FeatureConfigurationForStarlark, + BranchFdoProfile, + FdoContext, + ConstraintValueInfo, + StarlarkRuleContext, + InvalidConfigurationException, + CppConfiguration, + CcToolchainVariables, + OutputGroupInfo> { public static final BuiltinProvider PROVIDER = new BuiltinProvider("CcToolchainInfo", CcToolchainProvider.class) {}; @@ -622,7 +621,6 @@ public CcToolchainFeatures getFeatures() { return toolchainFeatures; } - @Override public Label getCcToolchainLabel() { return ccToolchainLabel; } diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_toolchain.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_toolchain.bzl index b7e9d964e785ae..ebe7851606504b 100644 --- a/src/main/starlark/builtins_bzl/common/cc/cc_toolchain.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_toolchain.bzl @@ -16,11 +16,18 @@ load(":common/cc/cc_helper.bzl", "cc_helper") load(":common/cc/cc_toolchain_provider_helper.bzl", "get_cc_toolchain_provider") +load(":common/cc/semantics.bzl", "semantics") cc_internal = _builtins.internal.cc_internal ToolchainInfo = _builtins.toplevel.platform_common.ToolchainInfo TemplateVariableInfo = _builtins.toplevel.platform_common.TemplateVariableInfo apple_common = _builtins.toplevel.apple_common +FdoProfileInfo = _builtins.internal.FdoProfileInfo +FdoPrefetchHintsInfo = _builtins.internal.FdoPrefetchHintsInfo +PropellerOptimizeInfo = _builtins.internal.PropellerOptimizeInfo +PackageSpecificationInfo = _builtins.toplevel.PackageSpecificationInfo +CcToolchainConfigInfo = _builtins.toplevel.CcToolchainConfigInfo +MemProfProfileInfo = _builtins.internal.MemProfProfileInfo def _validate_toolchain(ctx, is_apple): if not is_apple: @@ -30,20 +37,119 @@ def _validate_toolchain(ctx, is_apple): "changed recently, verify that \"xcode-select -p\" is correct and then try: " + "\"bazel shutdown\" to re-run Xcode configuration") +def _files(ctx, attr_name): + attr = getattr(ctx.attr, attr_name, None) + if attr != None and DefaultInfo in attr: + return attr[DefaultInfo].files + return depset() + +def _provider(attr, provider): + if attr != None and provider in attr: + return attr[provider] + return None + +def _latebound_libc(ctx, attr_name, implicit_attr_name): + if getattr(ctx.attr, implicit_attr_name, None) == None: + return attr_name + return implicit_attr_name + +def _full_inputs_for_link(ctx, linker_files, libc, is_apple_toolchain): + if not is_apple_toolchain: + return depset( + [ctx.file._interface_library_builder, ctx.file._link_dynamic_library_tool], + transitive = [linker_files, libc], + ) + return depset(transitive = [linker_files, libc]) + +def _label(ctx, attr_name): + if getattr(ctx.attr, attr_name, None) != None: + return getattr(ctx.attr, attr_name).label + return None + +def _package_specification_provider(ctx, allowlist_name): + possible_attr_names = ["_whitelist_" + allowlist_name, "_allowlist_" + allowlist_name] + for attr_name in possible_attr_names: + if hasattr(ctx.attr, attr_name): + package_specification_provider = getattr(ctx.attr, attr_name)[PackageSpecificationInfo] + if package_specification_provider != None: + return package_specification_provider + fail("Allowlist argument for " + allowlist_name + " not found") + +def _single_file(ctx, attr_name): + files = getattr(ctx.files, attr_name, []) + if len(files) > 1: + fail(ctx.label.name + " expected a single artifact", attr = attr_name) + if len(files) == 1: + return files[0] + return None + +def _attributes(ctx, is_apple): + grep_includes = None + if not semantics.is_bazel: + grep_includes = _single_file(ctx, "_grep_includes") + + latebound_libc = _latebound_libc(ctx, "libc_top", "_libc_top") + latebound_target_libc = _latebound_libc(ctx, "libc_top", "_target_libc_top") + + return struct( + supports_param_files = ctx.attr.supports_param_files, + runtime_solib_dir_base = "_solib__" + cc_internal.escape_label(label = ctx.label), + fdo_prefetch_provider = _provider(ctx.attr._fdo_prefetch_hints, FdoPrefetchHintsInfo), + propeller_optimize_provider = _provider(ctx.attr._propeller_optimize, PropellerOptimizeInfo), + mem_prof_profile_provider = _provider(ctx.attr._memprof_profile, MemProfProfileInfo), + cc_toolchain_config_info = _provider(ctx.attr.toolchain_config, CcToolchainConfigInfo), + fdo_optimize_artifacts = ctx.files._fdo_optimize, + licenses_provider = cc_internal.licenses(ctx = ctx), + static_runtime_lib = ctx.attr.static_runtime_lib, + dynamic_runtime_lib = ctx.attr.dynamic_runtime_lib, + supports_header_parsing = ctx.attr.supports_header_parsing, + all_files = _files(ctx, "all_files"), + compiler_files = _files(ctx, "compiler_files"), + strip_files = _files(ctx, "strip_files"), + objcopy_files = _files(ctx, "objcopy_files"), + fdo_optimize_label = _label(ctx, "_fdo_optimize"), + link_dynamic_library_tool = ctx.file._link_dynamic_library_tool, + grep_includes = grep_includes, + module_map = ctx.attr.module_map, + as_files = _files(ctx, "as_files"), + ar_files = _files(ctx, "ar_files"), + dwp_files = _files(ctx, "dwp_files"), + fdo_optimize_provider = _provider(ctx.attr._fdo_optimize, FdoProfileInfo), + module_map_artifact = _single_file(ctx, "module_map"), + all_files_including_libc = depset(transitive = [_files(ctx, "all_files"), _files(ctx, latebound_libc)]), + fdo_profile_provider = _provider(ctx.attr._fdo_profile, FdoProfileInfo), + cs_fdo_profile_provider = _provider(ctx.attr._csfdo_profile, FdoProfileInfo), + x_fdo_profile_provider = _provider(ctx.attr._xfdo_profile, FdoProfileInfo), + zipper = ctx.file._zipper, + linker_files = _full_inputs_for_link( + ctx, + _files(ctx, "linker_files"), + _files(ctx, latebound_libc), + is_apple, + ), + cc_toolchain_label = ctx.label, + coverage_files = _files(ctx, "coverage_files"), + compiler_files_without_includes = _files(ctx, "compiler_files_without_includes"), + libc = _files(ctx, latebound_libc), + target_libc = _files(ctx, latebound_target_libc), + libc_top_label = _label(ctx, latebound_libc), + target_libc_top_label = _label(ctx, latebound_target_libc), + if_so_builder = ctx.file._interface_library_builder, + allowlist_for_layering_check = _package_specification_provider(ctx, "disabling_parse_headers_and_layering_check_allowed"), + build_info_files = _provider(ctx.attr._build_info_translator, OutputGroupInfo), + ) + def _cc_toolchain_impl(ctx): _validate_toolchain(ctx, ctx.attr._is_apple) xcode_config_info = None if ctx.attr._is_apple: xcode_config_info = ctx.attr._xcode_config[apple_common.XcodeVersionConfig] - attributes_provider = cc_internal.construct_cc_toolchain_attributes_info( - ctx = ctx, - is_apple = ctx.attr._is_apple, - ) - providers = [attributes_provider] - if attributes_provider.licenses_provider() != None: - providers.append(attributes_provider.licenses_provider()) + attributes = _attributes(ctx, ctx.attr._is_apple) + providers = [] + if attributes.licenses_provider != None: + providers.append(attributes.licenses_provider) - cc_toolchain = get_cc_toolchain_provider(ctx, attributes_provider, xcode_config_info) + cc_toolchain = get_cc_toolchain_provider(ctx, attributes, xcode_config_info) if cc_toolchain == None: fail("This should never happen") template_variable_info = TemplateVariableInfo( diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_toolchain_provider_helper.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_toolchain_provider_helper.bzl index e276f6d25f8a4f..2c72a2b92cc93f 100644 --- a/src/main/starlark/builtins_bzl/common/cc/cc_toolchain_provider_helper.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_toolchain_provider_helper.bzl @@ -160,15 +160,15 @@ def get_cc_toolchain_provider(ctx, attributes, xcode_config_info): Args: ctx: rule context. - attributes: an instance of CcToolchainAttributesProvider. + attributes: encapsulated attributes of cc_toolchain rule. xcode_config_info: XcodeConfigInfo provider can be none if not present. Returns: A constructed CcToolchainProvider instance. """ - toolchain_config_info = attributes.cc_toolchain_config_info() + toolchain_config_info = attributes.cc_toolchain_config_info tools_directory = cc_helper.package_exec_path( ctx, - attributes.cc_toolchain_label().package, + attributes.cc_toolchain_label.package, ctx.configuration.is_sibling_repository_layout(), ) tool_paths = _compute_tool_paths(toolchain_config_info, tools_directory) @@ -178,39 +178,39 @@ def get_cc_toolchain_provider(ctx, attributes, xcode_config_info): configuration = ctx.configuration, cpp_config = ctx.fragments.cpp, tool_paths = tool_paths, - fdo_prefetch_provider = attributes.fdo_prefetch_provider(), - propeller_optimize_provider = attributes.propeller_optimize_provider(), - mem_prof_profile_provider = attributes.mem_prof_profile_provider(), - fdo_optimize_provider = attributes.fdo_optimize_provider(), - fdo_profile_provider = attributes.fdo_profile_provider(), - x_fdo_profile_provider = attributes.x_fdo_profile_provider(), - cs_fdo_profile_provider = attributes.cs_fdo_profile_provider(), - all_files = attributes.all_files(), - zipper = attributes.zipper(), - cc_toolchain_config_info = attributes.cc_toolchain_config_info(), - fdo_optimize_artifacts = attributes.fdo_optimize_artifacts(), - fdo_optimize_label = attributes.fdo_optimize_label(), + fdo_prefetch_provider = attributes.fdo_prefetch_provider, + propeller_optimize_provider = attributes.propeller_optimize_provider, + mem_prof_profile_provider = attributes.mem_prof_profile_provider, + fdo_optimize_provider = attributes.fdo_optimize_provider, + fdo_profile_provider = attributes.fdo_profile_provider, + x_fdo_profile_provider = attributes.x_fdo_profile_provider, + cs_fdo_profile_provider = attributes.cs_fdo_profile_provider, + all_files = attributes.all_files, + zipper = attributes.zipper, + cc_toolchain_config_info = attributes.cc_toolchain_config_info, + fdo_optimize_artifacts = attributes.fdo_optimize_artifacts, + fdo_optimize_label = attributes.fdo_optimize_label, ) if fdo_context == None: return None - runtime_solib_dir_base = attributes.runtime_solib_dir_base() + runtime_solib_dir_base = attributes.runtime_solib_dir_base runtime_solib_dir = paths.get_relative(ctx.bin_dir.path, runtime_solib_dir_base) solib_directory = "_solib_" + toolchain_config_info.target_cpu() default_sysroot = None if toolchain_config_info.builtin_sysroot() != "": default_sysroot = toolchain_config_info.builtin_sysroot() - if attributes.libc_top_label() == None: + if attributes.libc_top_label == None: sysroot = default_sysroot else: - sysroot = attributes.libc_top_label().package + sysroot = attributes.libc_top_label.package - static_runtime_lib = attributes.static_runtime_lib() + static_runtime_lib = attributes.static_runtime_lib if static_runtime_lib != None: static_runtime_link_inputs = static_runtime_lib[DefaultInfo].files else: static_runtime_link_inputs = None - dynamic_runtime_lib = attributes.dynamic_runtime_lib() + dynamic_runtime_lib = attributes.dynamic_runtime_lib if dynamic_runtime_lib != None: dynamic_runtime_link_symlinks_elems = [] for artifact in dynamic_runtime_lib[DefaultInfo].files.to_list(): @@ -229,8 +229,8 @@ def get_cc_toolchain_provider(ctx, attributes, xcode_config_info): dynamic_runtime_link_symlinks = None module_map = None - if attributes.module_map() != None and attributes.module_map_artifact() != None: - module_map = cc_common.create_module_map(file = attributes.module_map_artifact(), name = "crosstool") + if attributes.module_map != None and attributes.module_map_artifact != None: + module_map = cc_common.create_module_map(file = attributes.module_map_artifact, name = "crosstool") cc_compilation_context = cc_common.create_compilation_context(module_map = module_map) @@ -254,12 +254,11 @@ def get_cc_toolchain_provider(ctx, attributes, xcode_config_info): cpp_config = ctx.fragments.cpp, toolchain_features = toolchain_features, tools_directory = tools_directory, - attributes = attributes, static_runtime_link_inputs = static_runtime_link_inputs, dynamic_runtime_link_symlinks = dynamic_runtime_link_symlinks, runtime_solib_dir = runtime_solib_dir, cc_compilation_context = cc_compilation_context, - builtin_include_files = _builtin_includes(attributes.libc()), + builtin_include_files = _builtin_includes(attributes.libc), builtin_include_directories = builtin_include_directories, sysroot = sysroot, fdo_context = fdo_context, @@ -286,4 +285,23 @@ def get_cc_toolchain_provider(ctx, attributes, xcode_config_info): gcov = tool_paths.get("gcov", ""), vars = build_vars, xcode_config_info = xcode_config_info, + all_files = attributes.all_files, + all_files_including_libc = attributes.all_files_including_libc, + compiler_files = attributes.compiler_files, + compiler_files_without_includes = attributes.compiler_files_without_includes, + strip_files = attributes.strip_files, + objcopy_files = attributes.objcopy_files, + as_files = attributes.as_files, + ar_files = attributes.ar_files, + linker_files = attributes.linker_files, + if_so_builder = attributes.if_so_builder, + dwp_files = attributes.dwp_files, + coverage_files = attributes.coverage_files, + supports_param_files = attributes.supports_param_files, + supports_header_parsing = attributes.supports_header_parsing, + link_dynamic_library_tool = attributes.link_dynamic_library_tool, + grep_includes = attributes.grep_includes, + licenses_provider = attributes.licenses_provider, + allowlist_for_layering_check = attributes.allowlist_for_layering_check, + build_info_files = attributes.build_info_files, )