aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java
diff options
context:
space:
mode:
authorGravatar Lukacs Berki <lberki@google.com>2016-08-11 15:48:57 +0000
committerGravatar Yue Gan <yueg@google.com>2016-08-12 08:51:20 +0000
commit0978a8ea1593ef204ea97155014f76baba2508c5 (patch)
tree432293ba19ab848e9d4534ffa772e1406a9209eb /src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java
parentc1ffa41b27ebd9ff405909617227eb9c0dd612da (diff)
Record the category of the artifact to be linked in LinkerInput.
It's currently only used for sanity checks, but the idea is that we'll use this field to decide what to do with a given linker input instead of inferring things from its file name. Also make artifact name creation a bit simpler by using the same set of variables for compiler and linker outputs. -- MOS_MIGRATED_REVID=129990944
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java50
1 files changed, 31 insertions, 19 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java
index 77b7e13e2d..ba76f3e56d 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java
@@ -35,6 +35,8 @@ import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.Var
import com.google.devtools.build.lib.rules.cpp.CppCompileAction.DotdFile;
import com.google.devtools.build.lib.rules.cpp.Link.LinkStaticness;
import com.google.devtools.build.lib.rules.cpp.Link.LinkTargetType;
+import com.google.devtools.build.lib.rules.cpp.Link.Picness;
+import com.google.devtools.build.lib.rules.cpp.Link.Staticness;
import com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink;
import com.google.devtools.build.lib.rules.test.InstrumentedFilesCollector;
import com.google.devtools.build.lib.util.FileType;
@@ -61,7 +63,6 @@ import javax.annotation.Nullable;
public final class CppModel {
private final CppSemantics semantics;
private final RuleContext ruleContext;
- private final CcToolchainFeatures features;
private final BuildConfiguration configuration;
private final CppConfiguration cppConfiguration;
@@ -92,7 +93,6 @@ public final class CppModel {
this.semantics = semantics;
configuration = ruleContext.getConfiguration();
cppConfiguration = ruleContext.getFragment(CppConfiguration.class);
- features = CppHelper.getToolchain(ruleContext).getFeatures();
}
private Artifact getDwoFile(Artifact outputFile) {
@@ -556,7 +556,7 @@ public final class CppModel {
private void createHeaderAction(String outputName, Builder result, AnalysisEnvironment env,
CppCompileActionBuilder builder, boolean generateDotd) {
- String outputNameBase = CppHelper.getCompileArtifactName(ruleContext,
+ String outputNameBase = CppHelper.getArtifactNameForCategory(ruleContext,
ArtifactCategory.GENERATED_HEADER, outputName);
builder
@@ -612,11 +612,11 @@ public final class CppModel {
// Create PIC compile actions (same as non-PIC, but use -fPIC and
// generate .pic.o, .pic.d, .pic.gcno instead of .o, .d, .gcno.)
if (generatePicAction) {
- String picOutputBase = CppHelper.getCompileArtifactName(ruleContext,
+ String picOutputBase = CppHelper.getArtifactNameForCategory(ruleContext,
ArtifactCategory.PIC_FILE, outputName);
CppCompileActionBuilder picBuilder = copyAsPicBuilder(
builder, picOutputBase, outputCategory, generateDotd);
- String gcnoFileName = CppHelper.getCompileArtifactName(ruleContext,
+ String gcnoFileName = CppHelper.getArtifactNameForCategory(ruleContext,
ArtifactCategory.COVERAGE_DATA_FILE, picOutputBase);
Artifact gcnoFile = enableCoverage
? CppHelper.getCompileOutputArtifact(ruleContext, gcnoFileName)
@@ -663,9 +663,9 @@ public final class CppModel {
if (generateNoPicAction) {
Artifact noPicOutputFile = CppHelper.getCompileOutputArtifact(ruleContext,
- CppHelper.getCompileArtifactName(ruleContext, outputCategory, outputName));
+ CppHelper.getArtifactNameForCategory(ruleContext, outputCategory, outputName));
builder.setOutputs(outputCategory, outputName, generateDotd);
- String gcnoFileName = CppHelper.getCompileArtifactName(ruleContext,
+ String gcnoFileName = CppHelper.getArtifactNameForCategory(ruleContext,
ArtifactCategory.COVERAGE_DATA_FILE, outputName);
// Create non-PIC compile actions
@@ -723,7 +723,7 @@ public final class CppModel {
String getOutputNameBaseWith(String base, boolean usePic) {
return usePic
- ? CppHelper.getCompileArtifactName(ruleContext, ArtifactCategory.PIC_FILE, base)
+ ? CppHelper.getArtifactNameForCategory(ruleContext, ArtifactCategory.PIC_FILE, base)
: base;
}
@@ -734,7 +734,7 @@ public final class CppModel {
String outputNameBase = getOutputNameBaseWith(outputName, usePic);
String tempOutputName = ruleContext.getConfiguration().getBinFragment()
.getRelative(CppHelper.getObjDirectory(ruleContext.getLabel()))
- .getRelative(CppHelper.getCompileArtifactName(ruleContext, outputCategory,
+ .getRelative(CppHelper.getArtifactNameForCategory(ruleContext, outputCategory,
getOutputNameBaseWith(outputName + ".temp", usePic)))
.getPathString();
builder
@@ -780,10 +780,16 @@ public final class CppModel {
Artifact linuxDefault = CppHelper.getLinuxLinkedArtifact(ruleContext, linkTargetType);
try {
- String templatedName = features.getArtifactNameForCategory(
- linkTargetType.getLinkerOutput(), ruleContext, ImmutableMap.<String, String>of());
+ String maybePicName = ruleContext.getLabel().getName();
+ if (linkTargetType.picness() == Picness.PIC) {
+ maybePicName = CppHelper.getArtifactNameForCategory(
+ ruleContext, ArtifactCategory.PIC_FILE, maybePicName);
+ }
+
+ String linkedName = CppHelper.getArtifactNameForCategory(
+ ruleContext, linkTargetType.getLinkerOutput(), maybePicName);
PathFragment artifactFragment = new PathFragment(ruleContext.getLabel().getName())
- .getParentDirectory().getRelative(templatedName);
+ .getParentDirectory().getRelative(linkedName);
result = ruleContext.getBinArtifact(artifactFragment);
} catch (ExpansionException e) {
ruleContext.throwWithRuleError(e.getMessage());
@@ -821,7 +827,8 @@ public final class CppModel {
Iterable<Artifact> nonCodeLinkerInputs) throws RuleErrorException {
// For now only handle static links. Note that the dynamic library link below ignores linkType.
// TODO(bazel-team): Either support non-static links or move this check to setLinkType().
- Preconditions.checkState(linkType.isStaticLibraryLink(), "can only handle static links");
+ Preconditions.checkState(
+ linkType.staticness() == Staticness.STATIC, "can only handle static links");
CcLinkingOutputs.Builder result = new CcLinkingOutputs.Builder();
if (cppConfiguration.isLipoContextCollector()) {
@@ -868,7 +875,9 @@ public final class CppModel {
.setFeatureConfiguration(featureConfiguration)
.build();
env.registerAction(maybePicAction);
- result.addStaticLibrary(maybePicAction.getOutputLibrary());
+ if (linkType != LinkTargetType.EXECUTABLE) {
+ result.addStaticLibrary(maybePicAction.getOutputLibrary());
+ }
// Create a second static library (.pic.a). Only in case (2) do we need both PIC and non-PIC
// static libraries. In that case, the first static library contains the non-PIC code, and this
@@ -895,7 +904,9 @@ public final class CppModel {
.setFeatureConfiguration(featureConfiguration)
.build();
env.registerAction(picAction);
- result.addPicStaticLibrary(picAction.getOutputLibrary());
+ if (linkType != LinkTargetType.EXECUTABLE) {
+ result.addPicStaticLibrary(picAction.getOutputLibrary());
+ }
}
if (!createDynamicLibrary) {
@@ -945,6 +956,7 @@ public final class CppModel {
.addLinkopts(linkopts)
.addLinkopts(sonameLinkopts)
.setRuntimeInputs(
+ ArtifactCategory.DYNAMIC_LIBRARY,
CppHelper.getToolchain(ruleContext).getDynamicRuntimeLinkMiddleman(),
CppHelper.getToolchain(ruleContext).getDynamicRuntimeLinkInputs())
.setFeatureConfiguration(featureConfiguration)
@@ -966,16 +978,16 @@ public final class CppModel {
CppLinkAction action = linkActionBuilder.build();
env.registerAction(action);
+ if (linkType == LinkTargetType.EXECUTABLE) {
+ return result.build();
+ }
+
LibraryToLink dynamicLibrary = action.getOutputLibrary();
LibraryToLink interfaceLibrary = action.getInterfaceOutputLibrary();
if (interfaceLibrary == null) {
interfaceLibrary = dynamicLibrary;
}
- if (linkType == LinkTargetType.EXECUTABLE) {
- return result.build();
- }
-
// If shared library has neverlink=1, then leave it untouched. Otherwise,
// create a mangled symlink for it and from now on reference it through
// mangled name only.