Skip to content

Commit

Permalink
Refactor how AppleCcToolchain passes data to the CcToolchain
Browse files Browse the repository at this point in the history
This is a preparation for a change moving CcToolchainProvider creation from
CcToolchain to CcToolchainSuite.

#6072.

RELNOTES: None.
PiperOrigin-RevId: 214589270
  • Loading branch information
hlopko authored and Copybara-Service committed Sep 26, 2018
1 parent 0291b3c commit 4348e3c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.analysis.config.BuildConfigurationCollection;
import com.google.devtools.build.lib.analysis.config.ConfigurationResolver;
import com.google.devtools.build.lib.analysis.config.TransitionResolver;
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.cmdline.Label;
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.events.ExtendedEventHandler;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.BuiltinProvider;
Expand Down Expand Up @@ -149,22 +144,6 @@ public static PathFragment getManifestPathFromFilesetPath(PathFragment filesetDi
return outputManifestFrag;
}

/**
* Returns the middleman artifact on the specified attribute of the specified rule for the
* specified mode, or an empty set if it does not exist.
*/
public static NestedSet<Artifact> getMiddlemanFor(RuleContext rule, String attribute, Mode mode) {
TransitiveInfoCollection prereq = rule.getPrerequisite(attribute, mode);
if (prereq == null) {
return NestedSetBuilder.emptySet(Order.STABLE_ORDER);
}
MiddlemanProvider provider = prereq.getProvider(MiddlemanProvider.class);
if (provider == null) {
return NestedSetBuilder.emptySet(Order.STABLE_ORDER);
}
return provider.getMiddlemanArtifact();
}

/**
* Returns a path fragment qualified by the rule name and unique fragment to
* disambiguate artifacts produced from the source file appearing in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,13 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.AnalysisUtils;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.rules.apple.AppleConfiguration;
import com.google.devtools.build.lib.rules.apple.ApplePlatform;
import com.google.devtools.build.lib.rules.apple.AppleToolchain;
import com.google.devtools.build.lib.rules.apple.XcodeConfig;
import com.google.devtools.build.lib.rules.apple.XcodeConfigProvider;
import com.google.devtools.build.lib.rules.cpp.CcToolchain;
import com.google.devtools.build.lib.rules.cpp.CcToolchainRule;
import com.google.devtools.build.lib.rules.cpp.CcToolchainVariables;
import java.util.LinkedHashMap;
import java.util.Map;
Expand Down Expand Up @@ -57,7 +51,7 @@ public class AppleCcToolchain extends CcToolchain {
public static final String APPLE_SDK_PLATFORM_VALUE_KEY = "apple_sdk_platform_value";

@Override
protected void addBuildVariables(RuleContext ruleContext, CcToolchainVariables.Builder variables)
protected CcToolchainVariables getAdditionalBuildVariables(RuleContext ruleContext)
throws RuleErrorException {
AppleConfiguration appleConfiguration = ruleContext.getFragment(AppleConfiguration.class);

Expand All @@ -70,7 +64,7 @@ protected void addBuildVariables(RuleContext ruleContext, CcToolchainVariables.B
ApplePlatform platform = appleConfiguration.getSingleArchPlatform();

Map<String, String> appleEnv = getEnvironmentBuildVariables(ruleContext);

CcToolchainVariables.Builder variables = new CcToolchainVariables.Builder();
variables
.addStringVariable(
XCODE_VERSION_KEY,
Expand Down Expand Up @@ -109,16 +103,12 @@ protected void addBuildVariables(RuleContext ruleContext, CcToolchainVariables.B
.addStringVariable(
VERSION_MIN_KEY,
XcodeConfig.getMinimumOsForPlatformType(ruleContext, platform.getType()).toString());
return variables.build();
}

@Override
protected NestedSet<Artifact> fullInputsForLink(
RuleContext ruleContext, NestedSet<Artifact> link) {
return NestedSetBuilder.<Artifact>stableOrder()
.addTransitive(link)
.addTransitive(
AnalysisUtils.getMiddlemanFor(ruleContext, CcToolchainRule.LIBC_TOP_ATTR, Mode.TARGET))
.build();
protected boolean isAppleToolchain() {
return true;
}

private ImmutableMap<String, String> getEnvironmentBuildVariables(RuleContext ruleContext) {
Expand Down
124 changes: 68 additions & 56 deletions src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchain.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.google.devtools.build.lib.actions.Actions;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException;
import com.google.devtools.build.lib.analysis.AnalysisUtils;
import com.google.devtools.build.lib.analysis.CompilationHelper;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.FileProvider;
Expand Down Expand Up @@ -306,7 +305,7 @@ private Artifact convertLLVMRawProfileToIndexed(
ruleContext.registerAction(
new SpawnAction.Builder()
.addInput(rawProfileArtifact)
.addTransitiveInputs(getFiles(ruleContext, "all_files"))
.addTransitiveInputs(getMiddlemanOrFiles(ruleContext, "all_files"))
.addOutput(profileArtifact)
.useDefaultShellEnvironment()
.setExecutable(toolchainInfo.getToolPathFragment(Tool.LLVM_PROFDATA))
Expand Down Expand Up @@ -439,17 +438,20 @@ public ConfiguredTarget create(RuleContext ruleContext)
final Label label = ruleContext.getLabel();
final NestedSet<Artifact> crosstool = ruleContext.getPrerequisite("all_files", Mode.HOST)
.getProvider(FileProvider.class).getFilesToBuild();
final NestedSet<Artifact> crosstoolMiddleman = getFiles(ruleContext, "all_files");
final NestedSet<Artifact> compile = getFiles(ruleContext, "compiler_files");
final NestedSet<Artifact> crosstoolMiddleman = getMiddlemanOrFiles(ruleContext, "all_files");
final NestedSet<Artifact> compile = getMiddlemanOrFiles(ruleContext, "compiler_files");
final NestedSet<Artifact> compileWithoutIncludes =
getOptionalFiles(ruleContext, "compiler_files_without_includes");
final NestedSet<Artifact> strip = getFiles(ruleContext, "strip_files");
final NestedSet<Artifact> objcopy = getFiles(ruleContext, "objcopy_files");
final NestedSet<Artifact> as = getOptionalFiles(ruleContext, "as_files");
final NestedSet<Artifact> ar = getOptionalFiles(ruleContext, "ar_files");
final NestedSet<Artifact> link = getFiles(ruleContext, "linker_files");
final NestedSet<Artifact> dwp = getFiles(ruleContext, "dwp_files");
final NestedSet<Artifact> libcLink = inputsForLibc(ruleContext);
getOptionalMiddlemanOrFiles(ruleContext, "compiler_files_without_includes");
final NestedSet<Artifact> strip = getMiddlemanOrFiles(ruleContext, "strip_files");
final NestedSet<Artifact> objcopy = getMiddlemanOrFiles(ruleContext, "objcopy_files");
final NestedSet<Artifact> as = getOptionalMiddlemanOrFiles(ruleContext, "as_files");
final NestedSet<Artifact> ar = getOptionalMiddlemanOrFiles(ruleContext, "ar_files");
final NestedSet<Artifact> link = getMiddlemanOrFiles(ruleContext, "linker_files");
final NestedSet<Artifact> dwp = getMiddlemanOrFiles(ruleContext, "dwp_files");
final NestedSet<Artifact> libcMiddleman =
getOptionalMiddlemanOrFiles(ruleContext, CcToolchainRule.LIBC_TOP_ATTR, Mode.TARGET);
final NestedSet<Artifact> libc =
getOptionalFiles(ruleContext, CcToolchainRule.LIBC_TOP_ATTR, Mode.TARGET);
String purposePrefix = Actions.escapeLabel(label) + "_";
String runtimeSolibDirBase = "_solib_" + "_" + Actions.escapeLabel(label);
final PathFragment runtimeSolibDir =
Expand Down Expand Up @@ -539,7 +541,7 @@ public ConfiguredTarget create(RuleContext ruleContext)

NestedSetBuilder<Pair<String, String>> coverageEnvironment = NestedSetBuilder.compileOrder();

NestedSet<Artifact> coverage = getOptionalFiles(ruleContext, "coverage_files");
NestedSet<Artifact> coverage = getOptionalMiddlemanOrFiles(ruleContext, "coverage_files");
if (coverage.isEmpty()) {
coverage = crosstool;
}
Expand Down Expand Up @@ -599,18 +601,21 @@ public ConfiguredTarget create(RuleContext ruleContext)
toolchainInfo,
cppConfiguration.getCrosstoolTopPathFragment(),
crosstool,
fullInputsForCrosstool(ruleContext, crosstoolMiddleman),
/* crosstoolMiddleman= */ NestedSetBuilder.<Artifact>stableOrder()
.addTransitive(crosstoolMiddleman)
.addTransitive(libcMiddleman)
.build(),
compile,
compileWithoutIncludes,
strip,
objcopy,
as,
ar,
fullInputsForLink(ruleContext, link),
fullInputsForLink(ruleContext, link, libcMiddleman),
ruleContext.getPrerequisiteArtifact("$interface_library_builder", Mode.HOST),
dwp,
coverage,
libcLink,
libc,
staticRuntimeLinkInputs,
staticRuntimeLinkMiddleman,
dynamicRuntimeLinkSymlinks,
Expand All @@ -620,7 +625,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
supportsParamFiles,
supportsHeaderParsing,
getBuildVariables(ruleContext, toolchainInfo.getDefaultSysroot()),
getBuiltinIncludes(ruleContext),
getBuiltinIncludes(libc),
coverageEnvironment.build(),
toolchainInfo.supportsInterfaceSharedObjects()
? ruleContext.getPrerequisiteArtifact("$link_dynamic_library_tool", Mode.HOST)
Expand Down Expand Up @@ -812,9 +817,9 @@ private CToolchain getToolchainFromAttributes(
}
}

private ImmutableList<Artifact> getBuiltinIncludes(RuleContext ruleContext) {
private ImmutableList<Artifact> getBuiltinIncludes(NestedSet<Artifact> libc) {
ImmutableList.Builder<Artifact> result = ImmutableList.builder();
for (Artifact artifact : inputsForLibc(ruleContext)) {
for (Artifact artifact : libc) {
if (artifact.getExecPath().endsWith(BUILTIN_INCLUDE_FILE_SUFFIX)) {
result.add(artifact);
}
Expand All @@ -823,36 +828,20 @@ private ImmutableList<Artifact> getBuiltinIncludes(RuleContext ruleContext) {
return result.build();
}

private NestedSet<Artifact> inputsForLibc(RuleContext ruleContext) {
TransitiveInfoCollection libc =
ruleContext.getPrerequisite(CcToolchainRule.LIBC_TOP_ATTR, Mode.TARGET);
return libc != null
? libc.getProvider(FileProvider.class).getFilesToBuild()
: NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER);
}

private NestedSet<Artifact> fullInputsForCrosstool(RuleContext ruleContext,
NestedSet<Artifact> crosstoolMiddleman) {
return NestedSetBuilder.<Artifact>stableOrder()
.addTransitive(crosstoolMiddleman)
.addTransitive(
AnalysisUtils.getMiddlemanFor(ruleContext, CcToolchainRule.LIBC_TOP_ATTR, Mode.TARGET))
.build();
}

/**
* Returns the crosstool-derived link action inputs for a given rule. Adds the given set of
* artifacts as extra inputs.
*/
protected NestedSet<Artifact> fullInputsForLink(
RuleContext ruleContext, NestedSet<Artifact> link) {
return NestedSetBuilder.<Artifact>stableOrder()
.addTransitive(link)
.addTransitive(
AnalysisUtils.getMiddlemanFor(ruleContext, CcToolchainRule.LIBC_TOP_ATTR, Mode.TARGET))
.add(ruleContext.getPrerequisiteArtifact("$interface_library_builder", Mode.HOST))
.add(ruleContext.getPrerequisiteArtifact("$link_dynamic_library_tool", Mode.HOST))
.build();
private NestedSet<Artifact> fullInputsForLink(
RuleContext ruleContext, NestedSet<Artifact> link, NestedSet<Artifact> libcLink) {
NestedSetBuilder<Artifact> builder =
NestedSetBuilder.<Artifact>stableOrder().addTransitive(link).addTransitive(libcLink);
if (!isAppleToolchain()) {
builder
.add(ruleContext.getPrerequisiteArtifact("$interface_library_builder", Mode.HOST))
.add(ruleContext.getPrerequisiteArtifact("$link_dynamic_library_tool", Mode.HOST));
}
return builder.build();
}

private CppModuleMap createCrosstoolModuleMap(RuleContext ruleContext) {
Expand All @@ -877,8 +866,13 @@ private TransitiveInfoCollection selectDep(
return ruleContext.getPrerequisites(attribute, Mode.TARGET).get(0);
}

private NestedSet<Artifact> getFiles(RuleContext context, String attribute) {
TransitiveInfoCollection dep = context.getPrerequisite(attribute, Mode.HOST);
private NestedSet<Artifact> getMiddlemanOrFiles(RuleContext context, String attribute) {
return getMiddlemanOrFiles(context, attribute, Mode.HOST);
}

private NestedSet<Artifact> getMiddlemanOrFiles(
RuleContext context, String attribute, Mode mode) {
TransitiveInfoCollection dep = context.getPrerequisite(attribute, mode);
MiddlemanProvider middlemanProvider = dep.getProvider(MiddlemanProvider.class);
// We use the middleman if we can (if the dep is a filegroup), otherwise, just the regular
// filesToBuild (e.g. if it is a simple input file)
Expand All @@ -887,10 +881,23 @@ private NestedSet<Artifact> getFiles(RuleContext context, String attribute) {
: dep.getProvider(FileProvider.class).getFilesToBuild();
}

private NestedSet<Artifact> getOptionalFiles(RuleContext context, String attribute) {
TransitiveInfoCollection dep = context.getPrerequisite(attribute, Mode.HOST);
private NestedSet<Artifact> getOptionalMiddlemanOrFiles(RuleContext context, String attribute) {
return getOptionalMiddlemanOrFiles(context, attribute, Mode.HOST);
}

private NestedSet<Artifact> getOptionalMiddlemanOrFiles(
RuleContext context, String attribute, Mode mode) {
TransitiveInfoCollection dep = context.getPrerequisite(attribute, mode);
return dep != null
? getFiles(context, attribute)
? getMiddlemanOrFiles(context, attribute, mode)
: NestedSetBuilder.emptySet(Order.STABLE_ORDER);
}

private NestedSet<Artifact> getOptionalFiles(
RuleContext ruleContext, String attribute, Mode mode) {
TransitiveInfoCollection dep = ruleContext.getPrerequisite(attribute, mode);
return dep != null
? dep.getProvider(FileProvider.class).getFilesToBuild()
: NestedSetBuilder.emptySet(Order.STABLE_ORDER);
}

Expand Down Expand Up @@ -943,21 +950,26 @@ private final CcToolchainVariables getBuildVariables(
variables.addStringVariable(CcCommon.SYSROOT_VARIABLE_NAME, sysroot.getPathString());
}

addBuildVariables(ruleContext, variables);
variables.addAllNonTransitive(getAdditionalBuildVariables(ruleContext));

return variables.build();
}

/**
* Add local build variables from subclasses into {@link
* com.google.devtools.build.lib.rules.cpp.CcToolchainVariables} returned from {@link
* #getBuildVariables(RuleContext, PathFragment)}.
* Add local build variables from subclasses into {@link CcToolchainVariables} returned from
* {@link CcToolchain#getBuildVariables(RuleContext, PathFragment)}.
*
* <p>This method is meant to be overridden by subclasses of CcToolchain.
*/
protected void addBuildVariables(RuleContext ruleContext, CcToolchainVariables.Builder variables)
protected boolean isAppleToolchain() {
// To be overridden in subclass.
return false;
}

protected CcToolchainVariables getAdditionalBuildVariables(RuleContext ruleContext)
throws RuleErrorException {
// To be overridden in subclasses.
// To be overridden in subclass.
return CcToolchainVariables.EMPTY;
}

private PathFragment calculateSysroot(RuleContext ruleContext, PathFragment defaultSysroot) {
Expand Down

0 comments on commit 4348e3c

Please sign in to comment.