Search in sources :

Example 1 with CppLinkAction

use of com.google.devtools.build.lib.rules.cpp.CppLinkAction in project bazel by bazelbuild.

the class CrosstoolCompilationSupport method registerLinkActions.

@Override
CompilationSupport registerLinkActions(ObjcProvider objcProvider, J2ObjcMappingFileProvider j2ObjcMappingFileProvider, J2ObjcEntryClassProvider j2ObjcEntryClassProvider, ExtraLinkArgs extraLinkArgs, Iterable<Artifact> extraLinkInputs, DsymOutputType dsymOutputType) throws InterruptedException {
    Iterable<Artifact> prunedJ2ObjcArchives = computeAndStripPrunedJ2ObjcArchives(j2ObjcEntryClassProvider, j2ObjcMappingFileProvider, objcProvider);
    ImmutableList<Artifact> bazelBuiltLibraries = Iterables.isEmpty(prunedJ2ObjcArchives) ? objcProvider.getObjcLibraries() : substituteJ2ObjcPrunedLibraries(objcProvider);
    Artifact inputFileList = intermediateArtifacts.linkerObjList();
    ImmutableSet<Artifact> forceLinkArtifacts = getForceLoadArtifacts(objcProvider);
    Iterable<Artifact> objFiles = Iterables.concat(bazelBuiltLibraries, objcProvider.get(IMPORTED_LIBRARY), objcProvider.getCcLibraries());
    // Clang loads archives specified in filelists and also specified as -force_load twice,
    // resulting in duplicate symbol errors unless they are deduped.
    objFiles = Iterables.filter(objFiles, Predicates.not(Predicates.in(forceLinkArtifacts)));
    registerObjFilelistAction(objFiles, inputFileList);
    LinkTargetType linkType = (objcProvider.is(Flag.USES_CPP)) ? LinkTargetType.OBJCPP_EXECUTABLE : LinkTargetType.OBJC_EXECUTABLE;
    ObjcVariablesExtension extension = new ObjcVariablesExtension.Builder().setRuleContext(ruleContext).setObjcProvider(objcProvider).setConfiguration(ruleContext.getConfiguration()).setIntermediateArtifacts(intermediateArtifacts).setFrameworkNames(frameworkNames(objcProvider)).setLibraryNames(libraryNames(objcProvider)).setForceLoadArtifacts(getForceLoadArtifacts(objcProvider)).setAttributeLinkopts(attributes.linkopts()).addVariableCategory(VariableCategory.EXECUTABLE_LINKING_VARIABLES).build();
    Artifact binaryToLink = getBinaryToLink();
    CcToolchainProvider ccToolchain = CppHelper.getToolchain(ruleContext, ":cc_toolchain");
    FdoSupportProvider fdoSupport = CppHelper.getFdoSupport(ruleContext, ":cc_toolchain");
    CppLinkAction executableLinkAction = new CppLinkActionBuilder(ruleContext, binaryToLink, ccToolchain, fdoSupport).setMnemonic("ObjcLink").addActionInputs(bazelBuiltLibraries).addActionInputs(objcProvider.getCcLibraries()).addTransitiveActionInputs(objcProvider.get(IMPORTED_LIBRARY)).addTransitiveActionInputs(objcProvider.get(STATIC_FRAMEWORK_FILE)).addTransitiveActionInputs(objcProvider.get(DYNAMIC_FRAMEWORK_FILE)).setCrosstoolInputs(ccToolchain.getLink()).addActionInputs(prunedJ2ObjcArchives).addActionInput(inputFileList).setLinkType(linkType).setLinkStaticness(LinkStaticness.FULLY_STATIC).addLinkopts(ImmutableList.copyOf(extraLinkArgs)).addVariablesExtension(extension).setFeatureConfiguration(getFeatureConfiguration(ruleContext, buildConfiguration)).build();
    ruleContext.registerAction(executableLinkAction);
    if (objcConfiguration.shouldStripBinary()) {
        registerBinaryStripAction(binaryToLink, StrippingType.DEFAULT);
    }
    return this;
}
Also used : LinkTargetType(com.google.devtools.build.lib.rules.cpp.Link.LinkTargetType) CppLinkAction(com.google.devtools.build.lib.rules.cpp.CppLinkAction) CppLinkActionBuilder(com.google.devtools.build.lib.rules.cpp.CppLinkActionBuilder) FdoSupportProvider(com.google.devtools.build.lib.rules.cpp.FdoSupportProvider) CcToolchainProvider(com.google.devtools.build.lib.rules.cpp.CcToolchainProvider) Artifact(com.google.devtools.build.lib.actions.Artifact) CppLinkActionBuilder(com.google.devtools.build.lib.rules.cpp.CppLinkActionBuilder)

Example 2 with CppLinkAction

use of com.google.devtools.build.lib.rules.cpp.CppLinkAction in project bazel by bazelbuild.

the class NativeDepsHelper method createNativeDepsAction.

public static NativeDepsRunfiles createNativeDepsAction(final RuleContext ruleContext, CcLinkParams linkParams, Collection<String> extraLinkOpts, BuildConfiguration configuration, CcToolchainProvider toolchain, Artifact nativeDeps, String libraryIdentifier, Root bindirIfShared, boolean useDynamicRuntime) throws InterruptedException {
    Preconditions.checkState(ruleContext.isLegalFragment(CppConfiguration.class), "%s does not have access to CppConfiguration", ruleContext.getRule().getRuleClass());
    List<String> linkopts = new ArrayList<>(extraLinkOpts);
    linkopts.addAll(linkParams.flattenedLinkopts());
    Map<Artifact, NestedSet<Artifact>> linkstamps = CppHelper.resolveLinkstamps(ruleContext, linkParams);
    List<Artifact> buildInfoArtifacts = linkstamps.isEmpty() ? ImmutableList.<Artifact>of() : ruleContext.getAnalysisEnvironment().getBuildInfo(ruleContext, CppBuildInfo.KEY, configuration);
    boolean shareNativeDeps = configuration.getFragment(CppConfiguration.class).shareNativeDeps();
    NestedSet<LibraryToLink> linkerInputs = linkParams.getLibraries();
    Artifact sharedLibrary;
    if (shareNativeDeps) {
        PathFragment sharedPath = getSharedNativeDepsPath(LinkerInputs.toLibraryArtifacts(linkerInputs), linkopts, linkstamps.keySet(), buildInfoArtifacts, ruleContext.getFeatures());
        libraryIdentifier = sharedPath.getPathString();
        sharedLibrary = ruleContext.getShareableArtifact(sharedPath.replaceName(sharedPath.getBaseName() + ".so"), configuration.getBinDirectory(ruleContext.getRule().getRepository()));
    } else {
        sharedLibrary = nativeDeps;
    }
    FdoSupportProvider fdoSupport = CppHelper.getFdoSupport(ruleContext, ":cc_toolchain");
    CppLinkActionBuilder builder = new CppLinkActionBuilder(ruleContext, sharedLibrary, configuration, toolchain, fdoSupport);
    if (useDynamicRuntime) {
        builder.setRuntimeInputs(ArtifactCategory.DYNAMIC_LIBRARY, toolchain.getDynamicRuntimeLinkMiddleman(), toolchain.getDynamicRuntimeLinkInputs());
    } else {
        builder.setRuntimeInputs(ArtifactCategory.STATIC_LIBRARY, toolchain.getStaticRuntimeLinkMiddleman(), toolchain.getStaticRuntimeLinkInputs());
    }
    CppLinkAction linkAction = builder.setLinkArtifactFactory(SHAREABLE_LINK_ARTIFACT_FACTORY).setCrosstoolInputs(toolchain.getLink()).addLibraries(linkerInputs).setLinkType(LinkTargetType.DYNAMIC_LIBRARY).setLinkStaticness(LinkStaticness.MOSTLY_STATIC).setLibraryIdentifier(libraryIdentifier).addLinkopts(linkopts).setNativeDeps(true).addLinkstamps(linkstamps).build();
    ruleContext.registerAction(linkAction);
    Artifact linkerOutput = linkAction.getPrimaryOutput();
    if (shareNativeDeps) {
        // Collect dynamic-linker-resolvable symlinks for C++ runtime library dependencies.
        // Note we only need these symlinks when --share_native_deps is on, as shared native deps
        // mangle path names such that the library's conventional _solib RPATH entry
        // no longer resolves (because the target directory's relative depth gets lost).
        List<Artifact> runtimeSymlinks;
        if (useDynamicRuntime) {
            runtimeSymlinks = new LinkedList<>();
            for (final Artifact runtimeInput : toolchain.getDynamicRuntimeLinkInputs()) {
                final Artifact runtimeSymlink = ruleContext.getPackageRelativeArtifact(getRuntimeLibraryPath(ruleContext, runtimeInput), bindirIfShared);
                // Since runtime library symlinks are underneath the target's output directory and
                // multiple targets may share the same output directory, we need to make sure this
                // symlink's generating action is only set once.
                ruleContext.registerAction(new SymlinkAction(ruleContext.getActionOwner(), runtimeInput, runtimeSymlink, null));
                runtimeSymlinks.add(runtimeSymlink);
            }
        } else {
            runtimeSymlinks = ImmutableList.of();
        }
        ruleContext.registerAction(new SymlinkAction(ruleContext.getActionOwner(), linkerOutput, nativeDeps, null));
        return new NativeDepsRunfiles(nativeDeps, runtimeSymlinks);
    }
    return new NativeDepsRunfiles(linkerOutput, ImmutableList.<Artifact>of());
}
Also used : NestedSet(com.google.devtools.build.lib.collect.nestedset.NestedSet) FdoSupportProvider(com.google.devtools.build.lib.rules.cpp.FdoSupportProvider) SymlinkAction(com.google.devtools.build.lib.analysis.actions.SymlinkAction) ArrayList(java.util.ArrayList) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Artifact(com.google.devtools.build.lib.actions.Artifact) LibraryToLink(com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink) CppLinkAction(com.google.devtools.build.lib.rules.cpp.CppLinkAction) CppConfiguration(com.google.devtools.build.lib.rules.cpp.CppConfiguration) CppLinkActionBuilder(com.google.devtools.build.lib.rules.cpp.CppLinkActionBuilder)

Example 3 with CppLinkAction

use of com.google.devtools.build.lib.rules.cpp.CppLinkAction in project bazel by bazelbuild.

the class CrosstoolCompilationSupport method registerFullyLinkAction.

@Override
protected CompilationSupport registerFullyLinkAction(ObjcProvider objcProvider, Iterable<Artifact> inputArtifacts, Artifact outputArchive, @Nullable CcToolchainProvider ccToolchain, @Nullable FdoSupportProvider fdoSupport) throws InterruptedException {
    Preconditions.checkNotNull(ccToolchain);
    Preconditions.checkNotNull(fdoSupport);
    PathFragment labelName = new PathFragment(ruleContext.getLabel().getName());
    String libraryIdentifier = ruleContext.getPackageDirectory().getRelative(labelName.replaceName("lib" + labelName.getBaseName())).getPathString();
    ObjcVariablesExtension extension = new ObjcVariablesExtension.Builder().setRuleContext(ruleContext).setObjcProvider(objcProvider).setConfiguration(ruleContext.getConfiguration()).setIntermediateArtifacts(intermediateArtifacts).setFullyLinkArchive(outputArchive).addVariableCategory(VariableCategory.FULLY_LINK_VARIABLES).build();
    CppLinkAction fullyLinkAction = new CppLinkActionBuilder(ruleContext, outputArchive, ccToolchain, fdoSupport).addActionInputs(objcProvider.getObjcLibraries()).addActionInputs(objcProvider.getCcLibraries()).addActionInputs(objcProvider.get(IMPORTED_LIBRARY).toSet()).setCrosstoolInputs(ccToolchain.getLink()).setLinkType(LinkTargetType.OBJC_FULLY_LINKED_ARCHIVE).setLinkStaticness(LinkStaticness.FULLY_STATIC).setLibraryIdentifier(libraryIdentifier).addVariablesExtension(extension).setFeatureConfiguration(getFeatureConfiguration(ruleContext, buildConfiguration)).build();
    ruleContext.registerAction(fullyLinkAction);
    return this;
}
Also used : CppLinkAction(com.google.devtools.build.lib.rules.cpp.CppLinkAction) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) CppLinkActionBuilder(com.google.devtools.build.lib.rules.cpp.CppLinkActionBuilder)

Aggregations

CppLinkAction (com.google.devtools.build.lib.rules.cpp.CppLinkAction)3 CppLinkActionBuilder (com.google.devtools.build.lib.rules.cpp.CppLinkActionBuilder)3 Artifact (com.google.devtools.build.lib.actions.Artifact)2 FdoSupportProvider (com.google.devtools.build.lib.rules.cpp.FdoSupportProvider)2 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)2 SymlinkAction (com.google.devtools.build.lib.analysis.actions.SymlinkAction)1 NestedSet (com.google.devtools.build.lib.collect.nestedset.NestedSet)1 CcToolchainProvider (com.google.devtools.build.lib.rules.cpp.CcToolchainProvider)1 CppConfiguration (com.google.devtools.build.lib.rules.cpp.CppConfiguration)1 LinkTargetType (com.google.devtools.build.lib.rules.cpp.Link.LinkTargetType)1 LibraryToLink (com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink)1 ArrayList (java.util.ArrayList)1