diff options
author | 2018-01-16 07:24:36 -0800 | |
---|---|---|
committer | 2018-01-16 07:26:05 -0800 | |
commit | 80bc160b4534abd411909ab8deb74417e356634c (patch) | |
tree | 764e9fe07284b30baf7e173bb7f80d72b6d99e66 /src/main/java | |
parent | 9059d3935ad2cbee8114b2d8273387857d44de60 (diff) |
Refactor py_wrap_cc to go through CcLibraryHelper.
RELNOTES:none
PiperOrigin-RevId: 182051418
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java | 37 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java | 161 |
2 files changed, 129 insertions, 69 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java index 103a083024..a85223f7d1 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java @@ -146,7 +146,6 @@ public final class CcLibraryHelper { private final TransitiveInfoProviderMapBuilder providers; private final ImmutableMap<String, NestedSet<Artifact>> outputGroups; private final CcCompilationOutputs compilationOutputs; - private final CcLinkingOutputs linkingOutputs; private final CcLinkingOutputs linkingOutputsExcludingPrecompiledLibraries; private final CppCompilationContext context; @@ -258,7 +257,6 @@ public final class CcLibraryHelper { compilationInfo.getOutputGroups().entrySet(), linkingInfo.getOutputGroups().entrySet())); this.compilationOutputs = compilationInfo.getCcCompilationOutputs(); - this.linkingOutputs = linkingInfo.getCcLinkingOutputs(); this.linkingOutputsExcludingPrecompiledLibraries = linkingInfo.getCcLinkingOutputsExcludingPrecompiledLibraries(); this.context = compilationInfo.getCppCompilationContext(); @@ -276,10 +274,6 @@ public final class CcLibraryHelper { return compilationOutputs; } - public CcLinkingOutputs getCcLinkingOutputs() { - return linkingOutputs; - } - /** * Returns the linking outputs before adding the pre-compiled libraries. Avoid using this - * pre-compiled and locally compiled libraries should be treated identically. This method only @@ -362,7 +356,8 @@ public final class CcLibraryHelper { private boolean emitCcNativeLibrariesProvider; private boolean emitCcSpecificLinkParamsProvider; private boolean emitInterfaceSharedObjects; - private boolean emitDynamicLibrary = true; + private boolean createDynamicLibrary = true; + private boolean createStaticLibraries = true; private boolean checkDepsGenerateCpp = true; private boolean emitCompileProviders; private final SourceCategory sourceCategory; @@ -950,7 +945,18 @@ public final class CcLibraryHelper { * performed at the binary rule level. */ public CcLibraryHelper setCreateDynamicLibrary(boolean emitDynamicLibrary) { - this.emitDynamicLibrary = emitDynamicLibrary; + this.createDynamicLibrary = emitDynamicLibrary; + return this; + } + + /** When createStaticLibraries is true, there are no actions created for static libraries. */ + public CcLibraryHelper setCreateStaticLibraries(boolean emitStaticLibraries) { + this.createStaticLibraries = emitStaticLibraries; + return this; + } + + public CcLibraryHelper setNeverlink(boolean neverlink) { + this.neverlink = neverlink; return this; } @@ -1270,7 +1276,8 @@ public final class CcLibraryHelper { .addLinkActionInputs(linkActionInputs) .setFake(fake) .setAllowInterfaceSharedObjects(emitInterfaceSharedObjects) - .setCreateDynamicLibrary(emitDynamicLibrary) + .setCreateDynamicLibrary(createDynamicLibrary) + .setCreateStaticLibraries(createStaticLibraries) // Note: this doesn't actually save the temps, it just makes the CppModel use the // configurations --save_temps setting to decide whether to actually save the temps. .setSaveTemps(true) @@ -1412,6 +1419,11 @@ public final class CcLibraryHelper { .getRelative(ruleContext.getUniqueDirectory("_virtual_includes"))); } + /** Creates context for cc compile action from generated inputs. */ + public CppCompilationContext initializeCppCompilationContext() { + return initializeCppCompilationContext(initializeCppModel()); + } + /** * Create context for cc compile action from generated inputs. * @@ -1573,13 +1585,6 @@ public final class CcLibraryHelper { !featureConfiguration.isEnabled(CppRuleClasses.MODULE_MAP_WITHOUT_EXTERN_MODULE)); } - /** - * Creates context for cc compile action from generated inputs. - */ - public CppCompilationContext initializeCppCompilationContext() { - return initializeCppCompilationContext(initializeCppModel()); - } - private Iterable<CppModuleMap> collectModuleMaps() { // Cpp module maps may be null for some rules. We filter the nulls out at the end. List<CppModuleMap> result = 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 ced5525ebb..7d983b51f3 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 @@ -194,6 +194,7 @@ public final class CppModel { private final List<Artifact> linkActionInputs = new ArrayList<>(); private boolean allowInterfaceSharedObjects; private boolean createDynamicLibrary = true; + private boolean createStaticLibraries = true; private Artifact soImplArtifact; private FeatureConfiguration featureConfiguration; private List<VariablesExtension> variablesExtensions = new ArrayList<>(); @@ -386,6 +387,11 @@ public final class CppModel { return this; } + public CppModel setCreateStaticLibraries(boolean createStaticLibraries) { + this.createStaticLibraries = createStaticLibraries; + return this; + } + public CppModel setDynamicLibrary(Artifact soImplFilename) { this.soImplArtifact = soImplFilename; return this; @@ -1308,12 +1314,45 @@ public final class CppModel { // because it needs some data that's not available at this point. return result.build(); } - AnalysisEnvironment env = ruleContext.getAnalysisEnvironment(); boolean usePicForBinaries = CppHelper.usePic(ruleContext, ccToolchain, /* forBinary= */ true); boolean usePicForSharedLibs = CppHelper.usePic(ruleContext, ccToolchain, /* forBinary= */ false); + PathFragment labelName = PathFragment.create(ruleContext.getLabel().getName()); + String libraryIdentifier = + ruleContext + .getPackageDirectory() + .getRelative(labelName.replaceName("lib" + labelName.getBaseName())) + .getPathString(); + + if (createStaticLibraries) { + createStaticLibraries( + result, + env, + usePicForBinaries, + usePicForSharedLibs, + libraryIdentifier, + ccOutputs, + nonCodeLinkerInputs); + } + + if (createDynamicLibrary) { + createDynamicLibrary(result, env, usePicForSharedLibs, libraryIdentifier, ccOutputs); + } + + return result.build(); + } + + private void createStaticLibraries( + CcLinkingOutputs.Builder result, + AnalysisEnvironment env, + boolean usePicForBinaries, + boolean usePicForSharedLibs, + String libraryIdentifier, + CcCompilationOutputs ccOutputs, + Iterable<Artifact> nonCodeLinkerInputs) + throws RuleErrorException, InterruptedException { // Create static library (.a). The linkType only reflects whether the library is alwayslink or // not. The PIC-ness is determined by whether we need to use PIC or not. There are three cases // for (usePicForSharedLibs usePicForBinaries): @@ -1322,18 +1361,18 @@ public final class CppModel { // (2) (true false) -> shared libraries as pic, but not binaries // (3) (true true) -> both shared libraries and binaries as pic // - // In case (3), we always need PIC, so only create one static library containing the PIC object + // In case (3), we always need PIC, so only create one static library containing the PIC + // object // files. The name therefore does not match the content. // - // Presumably, it is done this way because the .a file is an implicit output of every cc_library + // Presumably, it is done this way because the .a file is an implicit output of every + // cc_library // rule, so we can't use ".pic.a" that in the always-PIC case. // If the crosstool is configured to select an output artifact, we use that selection. // Otherwise, we use linux defaults. Artifact linkedArtifact = getLinkedArtifact(linkType); - PathFragment labelName = PathFragment.create(ruleContext.getLabel().getName()); - String libraryIdentifier = ruleContext.getPackageDirectory().getRelative( - labelName.replaceName("lib" + labelName.getBaseName())).getPathString(); + CppLinkAction maybePicAction = newLinkActionBuilder(linkedArtifact) .addObjectFiles(ccOutputs.getObjectFiles(usePicForBinaries)) @@ -1346,37 +1385,46 @@ public final class CppModel { .addVariablesExtensions(variablesExtensions) .build(); env.registerAction(maybePicAction); - 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 - // one contains the PIC code, so the names match the content. - if (!usePicForBinaries && usePicForSharedLibs) { - LinkTargetType picLinkType = (linkType == LinkTargetType.ALWAYS_LINK_STATIC_LIBRARY) - ? LinkTargetType.ALWAYS_LINK_PIC_STATIC_LIBRARY - : LinkTargetType.PIC_STATIC_LIBRARY; - - // If the crosstool is configured to select an output artifact, we use that selection. - // Otherwise, we use linux defaults. - Artifact picArtifact = getLinkedArtifact(picLinkType); - CppLinkAction picAction = - newLinkActionBuilder(picArtifact) - .addObjectFiles(ccOutputs.getObjectFiles(/* usePic= */ true)) - .addLtoBitcodeFiles(ccOutputs.getLtoBitcodeFiles()) - .setLinkType(picLinkType) - .setLinkStaticness(LinkStaticness.FULLY_STATIC) - .addActionInputs(linkActionInputs) - .setLibraryIdentifier(libraryIdentifier) - .addVariablesExtensions(variablesExtensions) - .build(); - env.registerAction(picAction); - result.addPicStaticLibrary(picAction.getOutputLibrary()); - } - - if (!createDynamicLibrary) { - return result.build(); + if (usePicForBinaries) { + result.addPicStaticLibrary(maybePicAction.getOutputLibrary()); + } else { + 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 + // one contains the PIC code, so the names match the content. + if (usePicForSharedLibs) { + LinkTargetType picLinkType = + (linkType == LinkTargetType.ALWAYS_LINK_STATIC_LIBRARY) + ? LinkTargetType.ALWAYS_LINK_PIC_STATIC_LIBRARY + : LinkTargetType.PIC_STATIC_LIBRARY; + + // If the crosstool is configured to select an output artifact, we use that selection. + // Otherwise, we use linux defaults. + Artifact picArtifact = getLinkedArtifact(picLinkType); + CppLinkAction picAction = + newLinkActionBuilder(picArtifact) + .addObjectFiles(ccOutputs.getObjectFiles(/* usePic= */ true)) + .addLtoBitcodeFiles(ccOutputs.getLtoBitcodeFiles()) + .setLinkType(picLinkType) + .setLinkStaticness(LinkStaticness.FULLY_STATIC) + .addActionInputs(linkActionInputs) + .setLibraryIdentifier(libraryIdentifier) + .addVariablesExtensions(variablesExtensions) + .build(); + env.registerAction(picAction); + result.addPicStaticLibrary(picAction.getOutputLibrary()); + } } + } + private void createDynamicLibrary( + CcLinkingOutputs.Builder result, + AnalysisEnvironment env, + boolean usePicForSharedLibs, + String libraryIdentifier, + CcCompilationOutputs ccOutputs) + throws RuleErrorException, InterruptedException { // Create dynamic library. Artifact soImpl; String mainLibraryIdentifier; @@ -1483,9 +1531,6 @@ public final class CppModel { LibraryToLink dynamicLibrary = dynamicLinkAction.getOutputLibrary(); LibraryToLink interfaceLibrary = dynamicLinkAction.getInterfaceOutputLibrary(); - if (interfaceLibrary == null) { - interfaceLibrary = dynamicLibrary; - } // If shared library has neverlink=1, then leave it untouched. Otherwise, // create a mangled symlink for it and from now on reference it through @@ -1495,20 +1540,10 @@ public final class CppModel { // solibDir, instead we use the original interface library and dynamic library. if (neverLink || featureConfiguration.isEnabled(CppRuleClasses.COPY_DYNAMIC_LIBRARIES_TO_BINARY)) { - result.addDynamicLibrary(interfaceLibrary); + result.addDynamicLibrary(interfaceLibrary == null ? dynamicLibrary : interfaceLibrary); result.addExecutionDynamicLibrary(dynamicLibrary); } else { - Artifact libraryLink = - SolibSymlinkAction.getDynamicLibrarySymlink( - ruleContext, - ccToolchain.getSolibDirectory(), - interfaceLibrary.getArtifact(), - /* preserveName= */ false, - /* prefixConsumer= */ false, - ruleContext.getConfiguration()); - result.addDynamicLibrary(LinkerInputs.solibLibraryToLink( - libraryLink, interfaceLibrary.getArtifact(), libraryIdentifier)); - Artifact implLibraryLink = + Artifact implLibraryLinkArtifact = SolibSymlinkAction.getDynamicLibrarySymlink( ruleContext, ccToolchain.getSolibDirectory(), @@ -1516,12 +1551,32 @@ public final class CppModel { /* preserveName= */ false, /* prefixConsumer= */ false, ruleContext.getConfiguration()); - result.addExecutionDynamicLibrary(LinkerInputs.solibLibraryToLink( - implLibraryLink, dynamicLibrary.getArtifact(), libraryIdentifier)); + LibraryToLink implLibraryLink = + LinkerInputs.solibLibraryToLink( + implLibraryLinkArtifact, dynamicLibrary.getArtifact(), libraryIdentifier); + result.addExecutionDynamicLibrary(implLibraryLink); + + LibraryToLink libraryLink; + if (interfaceLibrary == null) { + libraryLink = implLibraryLink; + } else { + Artifact libraryLinkArtifact = + SolibSymlinkAction.getDynamicLibrarySymlink( + ruleContext, + ccToolchain.getSolibDirectory(), + interfaceLibrary.getArtifact(), + /* preserveName= */ false, + /* prefixConsumer= */ false, + ruleContext.getConfiguration()); + libraryLink = + LinkerInputs.solibLibraryToLink( + libraryLinkArtifact, interfaceLibrary.getArtifact(), libraryIdentifier); + } + result.addDynamicLibrary(libraryLink); } - return result.build(); } + private CppLinkActionBuilder newLinkActionBuilder(Artifact outputArtifact) { return new CppLinkActionBuilder( ruleContext, outputArtifact, ccToolchain, fdoSupport, featureConfiguration, semantics) |