Skip to content

Commit

Permalink
Expose cc_toolchain.static_runtime_lib and cc_toolchain.dynamic_runt…
Browse files Browse the repository at this point in the history
…ime_lib to Starlark

    This is required since existing cc_toolchain.all_files doesn't usually contain C++ runtime library.

    Progress towards bazelbuild/bazel#6516

    RELNOTES: `cc_toolchain.static_runtime_lib` and `cc_toolchain.dynamic_runtime_lib` are now exposed to Starlark.
    PiperOrigin-RevId: 242863875
  • Loading branch information
Luca Di Grazia committed Sep 4, 2022
1 parent db5951f commit d021b59
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 355 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ public static ConfiguredTarget init(CppSemantics semantics, RuleContext ruleCont
.addCcCompilationContexts(CppHelper.getCompilationContextsFromDeps(deps))
.addCcCompilationContexts(
ImmutableList.of(CcCompilationHelper.getStlCcCompilationContext(ruleContext)))
.addQuoteIncludeDirs(semantics.getQuoteIncludes(ruleContext))
.setHeadersCheckingMode(semantics.determineHeadersCheckingMode(ruleContext))
.setCodeCoverageEnabled(CcCompilationHelper.isCodeCoverageEnabled(ruleContext))
.setFake(fake);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ public static void init(
.collect(ImmutableList.toImmutableList()))
.addCcCompilationContexts(
ImmutableList.of(CcCompilationHelper.getStlCcCompilationContext(ruleContext)))
.addQuoteIncludeDirs(semantics.getQuoteIncludes(ruleContext))
.setHeadersCheckingMode(semantics.determineHeadersCheckingMode(ruleContext));

CcLinkingHelper linkingHelper =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,18 +454,6 @@ private CcLinkingOutputs createCcLinkActions(CcCompilationOutputs ccOutputs)
ccLinkingOutputsBuilder, libraryToLinkBuilder, usePic, libraryIdentifier, ccOutputs);
}

if (shouldCreateStaticLibraries
&& featureConfiguration.isEnabled(CppRuleClasses.DISABLE_WHOLE_ARCHIVE_FOR_STATIC_LIB)
&& (staticLinkType == LinkTargetType.ALWAYS_LINK_STATIC_LIBRARY)) {
ruleErrorConsumer.throwWithAttributeError(
"alwayslink",
"alwayslink should not be True for a target with the"
+ " disable_whole_archive_for_static_lib feature enabled.");
}

libraryToLinkBuilder.setDisableWholeArchive(
featureConfiguration.isEnabled(CppRuleClasses.DISABLE_WHOLE_ARCHIVE_FOR_STATIC_LIB));

if (hasBuiltDynamicLibrary || shouldCreateStaticLibraries) {
ccLinkingOutputsBuilder.setLibraryToLink(libraryToLinkBuilder.build());
}
Expand Down Expand Up @@ -622,11 +610,12 @@ private CppLinkAction registerActionForStaticLibrary(
throws RuleErrorException, InterruptedException {
Artifact linkedArtifact = getLinkedArtifact(linkTargetTypeUsedForNaming);
CppLinkAction action =
newLinkActionBuilder(linkedArtifact, linkTargetTypeUsedForNaming)
newLinkActionBuilder(linkedArtifact)
.addObjectFiles(ccOutputs.getObjectFiles(usePic))
.addNonCodeInputs(nonCodeLinkerInputs)
.addLtoCompilationContext(ccOutputs.getLtoCompilationContext())
.setUsePicForLtoBackendActions(usePic)
.setLinkType(linkTargetTypeUsedForNaming)
.setLinkingMode(LinkingMode.STATIC)
.addActionInputs(linkActionInputs)
.setLibraryIdentifier(libraryIdentifier)
Expand Down Expand Up @@ -678,13 +667,14 @@ private boolean createDynamicLinkAction(
}

CppLinkActionBuilder dynamicLinkActionBuilder =
newLinkActionBuilder(linkerOutput, dynamicLinkType)
newLinkActionBuilder(linkerOutput)
.setWholeArchive(wholeArchive)
.setNativeDeps(nativeDeps)
.setAdditionalLinkstampDefines(additionalLinkstampDefines.build())
.setInterfaceOutput(soInterface)
.addNonCodeInputs(ccOutputs.getHeaderTokenFiles())
.addLtoCompilationContext(ccOutputs.getLtoCompilationContext())
.setLinkType(dynamicLinkType)
.setLinkingMode(linkingMode)
.setFake(fake)
.addActionInputs(linkActionInputs)
Expand Down Expand Up @@ -821,8 +811,7 @@ private boolean createDynamicLinkAction(
return hasBuiltDynamicLibrary;
}

private CppLinkActionBuilder newLinkActionBuilder(
Artifact outputArtifact, LinkTargetType linkType) {
private CppLinkActionBuilder newLinkActionBuilder(Artifact outputArtifact) {
return new CppLinkActionBuilder(
ruleErrorConsumer,
actionConstructionContext,
Expand All @@ -836,12 +825,7 @@ private CppLinkActionBuilder newLinkActionBuilder(
.setGrepIncludes(grepIncludes)
.setIsStampingEnabled(isStampingEnabled)
.setTestOrTestOnlyTarget(isTestOrTestOnlyTarget)
.setLinkType(linkType)
.setLinkerFiles(
(cppConfiguration.useSpecificToolFiles()
&& linkType.linkerOrArchiver() == LinkerOrArchiver.ARCHIVER)
? ccToolchain.getArFiles()
: ccToolchain.getLinkerFiles())
.setLinkerFiles(ccToolchain.getLinkerFiles())
.setLinkArtifactFactory(linkArtifactFactory)
.setUseTestOnlyFlags(useTestOnlyFlags);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public final class CcToolchainProvider extends ToolchainInfo
/* targetSysroot= */ null,
/* fdoContext= */ null,
/* isHostConfiguration= */ false,
/* licensesProvider= */ null,
/* toolPaths= */ ImmutableMap.of());
/* licensesProvider= */ null);

@Nullable private final CppConfiguration cppConfiguration;
private final CppToolchainInfo toolchainInfo;
Expand Down Expand Up @@ -123,7 +122,6 @@ public final class CcToolchainProvider extends ToolchainInfo
@Nullable private final PathFragment sysroot;
private final PathFragment targetSysroot;
private final boolean isHostConfiguration;
private final ImmutableMap<String, PathFragment> toolPaths;
/**
* WARNING: We don't like {@link FdoContext}. Its {@link FdoContext#fdoProfilePath} is pure path
* and that is horrible as it breaks many Bazel assumptions! Don't do bad stuff with it, don't
Expand Down Expand Up @@ -170,8 +168,7 @@ public CcToolchainProvider(
@Nullable PathFragment targetSysroot,
FdoContext fdoContext,
boolean isHostConfiguration,
LicensesProvider licensesProvider,
ImmutableMap<String, PathFragment> toolPaths) {
LicensesProvider licensesProvider) {
super(values, Location.BUILTIN);
this.cppConfiguration = cppConfiguration;
this.toolchainInfo = toolchainInfo;
Expand Down Expand Up @@ -213,7 +210,6 @@ public CcToolchainProvider(
this.fdoContext = fdoContext == null ? FdoContext.getDisabledContext() : fdoContext;
this.isHostConfiguration = isHostConfiguration;
this.licensesProvider = licensesProvider;
this.toolPaths = toolPaths;
}

/**
Expand Down Expand Up @@ -349,16 +345,11 @@ public String getToolPathStringOrNull(Tool tool) {
return toolPathFragment == null ? null : toolPathFragment.getPathString();
}

/**
* Returns the path fragment that is either absolute or relative to the execution root that can be
* used to execute the given tool.
*/
@Nullable
public PathFragment getToolPathFragmentOrNull(CppConfiguration.Tool tool) {
return CcToolchainProviderHelper.getToolPathFragment(toolPaths, tool);
return toolchainInfo.getToolPathFragment(tool);
}


@Override
public ImmutableList<String> getBuiltInIncludeDirectoriesAsStrings() {
return builtInIncludeDirectories
Expand Down Expand Up @@ -891,6 +882,11 @@ public boolean requireCtxInConfigureFeatures() {
.requireCtxInConfigureFeatures();
}

public boolean disableGenruleCcToolchainDependency() {
return getCppConfigurationEvenThoughItCanBeDifferentThatWhatTargetHas()
.disableGenruleCcToolchainDependency();
}

@VisibleForTesting
NestedSet<Artifact> getStaticRuntimeLibForTesting() {
return staticRuntimeLinkInputs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
package com.google.devtools.build.lib.rules.cpp;

import static com.google.devtools.build.lib.packages.BuildType.LABEL;
import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST;
import static com.google.devtools.build.lib.packages.BuildType.NODEP_LABEL;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
Expand All @@ -34,10 +32,8 @@
import com.google.devtools.build.lib.actions.MiddlemanFactory;
import com.google.devtools.build.lib.actions.ParamFileInfo;
import com.google.devtools.build.lib.actions.ParameterFile;
import com.google.devtools.build.lib.analysis.AliasProvider;
import com.google.devtools.build.lib.analysis.AnalysisUtils;
import com.google.devtools.build.lib.analysis.Expander;
import com.google.devtools.build.lib.analysis.FileProvider;
import com.google.devtools.build.lib.analysis.PlatformConfiguration;
import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
import com.google.devtools.build.lib.analysis.RuleContext;
Expand Down Expand Up @@ -169,18 +165,7 @@ static ImmutableList<String> getPackageCopts(RuleContext ruleContext) {
public static List<String> expandLinkopts(
RuleContext ruleContext, String attrName, Iterable<String> values) {
List<String> result = new ArrayList<>();
ImmutableMap.Builder<Label, ImmutableCollection<Artifact>> builder = ImmutableMap.builder();

if (ruleContext.attributes().has("additional_linker_inputs", LABEL_LIST)) {
for (TransitiveInfoCollection current :
ruleContext.getPrerequisites("additional_linker_inputs", Mode.TARGET)) {
builder.put(
AliasProvider.getDependencyLabel(current),
ImmutableList.copyOf(current.getProvider(FileProvider.class).getFilesToBuild()));
}
}

Expander expander = ruleContext.getExpander(builder.build()).withDataExecLocations();
Expander expander = ruleContext.getExpander().withDataExecLocations();
for (String value : values) {
expander.tokenizeAndExpandMakeVars(result, attrName, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@
import com.google.devtools.build.lib.skylarkinterface.Param;
import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.SkylarkNestedSet;
import javax.annotation.Nullable;

/** Information about the C++ toolchain. */
@SkylarkModule(
name = "CcToolchainInfo",
category = SkylarkModuleCategory.PROVIDER,
doc = "Information about the C++ compiler being used.")
@SkylarkModule(name = "CcToolchainInfo", doc = "Information about the C++ compiler being used.")
public interface CcToolchainProviderApi<FeatureConfigurationT extends FeatureConfigurationApi>
extends ToolchainInfoApi {

Expand Down
Loading

0 comments on commit d021b59

Please sign in to comment.