Skip to content

Commit

Permalink
C++: Split C++ library into two classes, compilation and linking
Browse files Browse the repository at this point in the history
These will be separate calls in the Skylark API.

RELNOTES:none
PiperOrigin-RevId: 184961734
  • Loading branch information
oquenchil authored and Copybara-Service committed Feb 8, 2018
1 parent e754126 commit 8a96fe9
Show file tree
Hide file tree
Showing 10 changed files with 1,091 additions and 976 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import com.google.devtools.build.lib.packages.TargetUtils;
import com.google.devtools.build.lib.rules.apple.ApplePlatform;
import com.google.devtools.build.lib.rules.cpp.CcCommon.CcFlagsSupplier;
import com.google.devtools.build.lib.rules.cpp.CcLibraryHelper.Info;
import com.google.devtools.build.lib.rules.cpp.CcCompilationHelper.CompilationInfo;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration;
import com.google.devtools.build.lib.rules.cpp.CppConfiguration.DynamicMode;
import com.google.devtools.build.lib.rules.cpp.CppConfiguration.Tool;
Expand Down Expand Up @@ -212,14 +212,15 @@ public static ConfiguredTarget init(CppSemantics semantics, RuleContext ruleCont
return null;
}

CcLibraryHelper compilationHelper =
new CcLibraryHelper(ruleContext, semantics, featureConfiguration, ccToolchain, fdoSupport)
CcCompilationHelper compilationHelper =
new CcCompilationHelper(
ruleContext, semantics, featureConfiguration, ccToolchain, fdoSupport)
.fromCommon(common)
.addSources(common.getSources())
.addDeps(ImmutableList.of(CppHelper.mallocForTarget(ruleContext)))
.setFake(fake)
.addPrecompiledFiles(precompiledFiles);
Info.CompilationInfo compilationInfo = compilationHelper.compile();
CompilationInfo compilationInfo = compilationHelper.compile();
CppCompilationContext cppCompilationContext = compilationInfo.getCppCompilationContext();
CcCompilationOutputs ccCompilationOutputs = compilationInfo.getCcCompilationOutputs();

Expand All @@ -232,11 +233,11 @@ public static ConfiguredTarget init(CppSemantics semantics, RuleContext ruleCont
&& cppConfiguration.getLinkCompileOutputSeparately()
&& linkStaticness == LinkStaticness.DYNAMIC;
// When linking the object files directly into the resulting binary, we do not need
// library-level link outputs; thus, we do not let CcLibraryHelper produce link outputs
// library-level link outputs; thus, we do not let CcCompilationHelper produce link outputs
// (either shared object files or archives) for a non-library link type [*], and add
// the object files explicitly in determineLinkerArguments.
//
// When linking the object files into their own library, we want CcLibraryHelper to
// When linking the object files into their own library, we want CcCompilationHelper to
// take care of creating the library link outputs for us, so we need to set the link
// type to STATIC_LIBRARY.
//
Expand All @@ -245,8 +246,14 @@ public static ConfiguredTarget init(CppSemantics semantics, RuleContext ruleCont
// output matching a shared object, for example cc_binary(name="foo.so", ...) on linux.
CcLinkingOutputs ccLinkingOutputs = CcLinkingOutputs.EMPTY;
if (linkCompileOutputSeparately) {
CcLibraryHelper linkingHelper =
new CcLibraryHelper(ruleContext, semantics, featureConfiguration, ccToolchain, fdoSupport)
CcLinkingHelper linkingHelper =
new CcLinkingHelper(
ruleContext,
semantics,
featureConfiguration,
ccToolchain,
fdoSupport,
ruleContext.getConfiguration())
.fromCommon(common)
.addDeps(ImmutableList.of(CppHelper.mallocForTarget(ruleContext)))
.setFake(fake)
Expand Down Expand Up @@ -868,7 +875,7 @@ private static void addTransitiveInfoProviders(
instrumentedObjectFiles, !TargetUtils.isTestRule(ruleContext.getRule()) && !fake);

NestedSet<Artifact> headerTokens =
CcLibraryHelper.collectHeaderTokens(ruleContext, ccCompilationOutputs);
CcCompilationHelper.collectHeaderTokens(ruleContext, ccCompilationOutputs);
NestedSet<Artifact> filesToCompile =
ccCompilationOutputs.getFilesToCompile(
cppConfiguration.isLipoContextCollector(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.rules.apple.ApplePlatform;
import com.google.devtools.build.lib.rules.cpp.CcLibraryHelper.SourceCategory;
import com.google.devtools.build.lib.rules.cpp.CcCompilationHelper.SourceCategory;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.CollidingProvidesException;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables;
Expand All @@ -60,6 +60,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -148,6 +149,31 @@ public CcCommon(RuleContext ruleContext) {
CppHelper.getFdoSupportUsingDefaultCcToolchainAttribute(ruleContext));
}

/**
* Merges a list of output groups into one. The sets for each entry with a given key are merged.
*/
public static Map<String, NestedSet<Artifact>> mergeOutputGroups(
ImmutableList<Map<String, NestedSet<Artifact>>> outputGroups) {
Map<String, NestedSetBuilder<Artifact>> mergedOutputGroupsBuilder = new TreeMap<>();

for (Map<String, NestedSet<Artifact>> outputGroup : outputGroups) {
for (Map.Entry<String, NestedSet<Artifact>> entryOutputGroup : outputGroup.entrySet()) {
String key = entryOutputGroup.getKey();
mergedOutputGroupsBuilder.computeIfAbsent(
key, (String k) -> NestedSetBuilder.compileOrder());
mergedOutputGroupsBuilder.get(key).addTransitive(entryOutputGroup.getValue());
}
}

Map<String, NestedSet<Artifact>> mergedOutputGroups = new TreeMap<>();
for (Map.Entry<String, NestedSetBuilder<Artifact>> entryOutputGroupBuilder :
mergedOutputGroupsBuilder.entrySet()) {
mergedOutputGroups.put(
entryOutputGroupBuilder.getKey(), entryOutputGroupBuilder.getValue().build());
}
return mergedOutputGroups;
}

/**
* Returns our own linkopts from the rule attribute. This determines linker
* options to use when building this target and anything that depends on it.
Expand Down
Loading

0 comments on commit 8a96fe9

Please sign in to comment.