@Override public NativeLinkableInput getNativeLinkableInput( TargetGraph targetGraph, CxxPlatform cxxPlatform, Linker.LinkableDepType type) { if (!isPlatformSupported(cxxPlatform)) { return NativeLinkableInput.of(); } if (headerOnly.apply(cxxPlatform)) { return NativeLinkableInput.of( ImmutableList.<Arg>of(), Preconditions.checkNotNull(frameworks), ImmutableSet.<FrameworkPath>of()); } // Build up the arguments used to link this library. If we're linking the // whole archive, wrap the library argument in the necessary "ld" flags. ImmutableList.Builder<Arg> linkerArgsBuilder = ImmutableList.builder(); linkerArgsBuilder.addAll(Preconditions.checkNotNull(exportedLinkerFlags.apply(cxxPlatform))); if (type != Linker.LinkableDepType.SHARED || linkage == Linkage.STATIC) { BuildRule rule = requireBuildRule( targetGraph, cxxPlatform.getFlavor(), type == Linker.LinkableDepType.STATIC ? CxxDescriptionEnhancer.STATIC_FLAVOR : CxxDescriptionEnhancer.STATIC_PIC_FLAVOR); Arg library = new SourcePathArg(getResolver(), new BuildTargetSourcePath(rule.getBuildTarget())); if (linkWhole) { Linker linker = cxxPlatform.getLd(); linkerArgsBuilder.addAll(linker.linkWhole(library)); } else { linkerArgsBuilder.add(library); } } else { BuildRule rule = requireBuildRule( targetGraph, cxxPlatform.getFlavor(), CxxDescriptionEnhancer.SHARED_FLAVOR); linkerArgsBuilder.add( new SourcePathArg(getResolver(), new BuildTargetSourcePath(rule.getBuildTarget()))); } final ImmutableList<Arg> linkerArgs = linkerArgsBuilder.build(); return NativeLinkableInput.of( linkerArgs, Preconditions.checkNotNull(frameworks), Preconditions.checkNotNull(libraries)); }
@Override public NativeLinkableInput getNativeLinkableInput( CxxPlatform cxxPlatform, Linker.LinkableDepType type) throws NoSuchBuildTargetException { // Build the library path and linker arguments that we pass through the // {@link NativeLinkable} interface for linking. ImmutableList.Builder<Arg> linkerArgsBuilder = ImmutableList.builder(); linkerArgsBuilder.addAll( StringArg.from(Preconditions.checkNotNull(exportedLinkerFlags.apply(cxxPlatform)))); if (!headerOnly) { if (type == Linker.LinkableDepType.SHARED) { Preconditions.checkState(getPreferredLinkage(cxxPlatform) != Linkage.STATIC); final SourcePath sharedLibrary = requireSharedLibrary(cxxPlatform); if (linkWithoutSoname) { if (!(sharedLibrary instanceof PathSourcePath)) { throw new HumanReadableException( "%s: can only link prebuilt DSOs without sonames", getBuildTarget()); } linkerArgsBuilder.add(new RelativeLinkArg((PathSourcePath) sharedLibrary)); } else { linkerArgsBuilder.add( new SourcePathArg(getResolver(), requireSharedLibrary(cxxPlatform))); } } else { Preconditions.checkState(getPreferredLinkage(cxxPlatform) != Linkage.SHARED); Path staticLibraryPath = type == Linker.LinkableDepType.STATIC_PIC ? getStaticPicLibrary(cxxPlatform).get() : PrebuiltCxxLibraryDescription.getStaticLibraryPath( getBuildTarget(), params.getCellRoots(), ruleResolver, cxxPlatform, libDir, libName); SourcePathArg staticLibrary = new SourcePathArg( getResolver(), new PathSourcePath(getProjectFilesystem(), staticLibraryPath)); if (linkWhole) { Linker linker = cxxPlatform.getLd(); linkerArgsBuilder.addAll(linker.linkWhole(staticLibrary)); } else { linkerArgsBuilder.add(staticLibrary); } } } final ImmutableList<Arg> linkerArgs = linkerArgsBuilder.build(); return NativeLinkableInput.of( linkerArgs, ImmutableSet.<FrameworkPath>of(), ImmutableSet.<FrameworkPath>of()); }
@Override public NativeLinkableInput getNativeLinkableInput( TargetGraph targetGraph, CxxPlatform cxxPlatform, Linker.LinkableDepType type) { if (!isPlatformSupported(cxxPlatform)) { return NativeLinkableInput.of(); } if (headerOnly.apply(cxxPlatform)) { return NativeLinkableInput.of( ImmutableList.<SourcePath>of(), ImmutableList.<String>of(), Preconditions.checkNotNull(frameworks), ImmutableSet.<FrameworkPath>of()); } // Build up the arguments used to link this library. If we're linking the // whole archive, wrap the library argument in the necessary "ld" flags. final Pair<ImmutableList<String>, ImmutableSet<SourcePath>> flagsAndBuildInputs = exportedLinkerFlags.apply(cxxPlatform); ImmutableList.Builder<String> linkerArgsBuilder = ImmutableList.builder(); linkerArgsBuilder.addAll(flagsAndBuildInputs.getFirst()); final BuildRule libraryRule; if (type != Linker.LinkableDepType.SHARED || linkage == Linkage.STATIC) { libraryRule = requireBuildRule( targetGraph, cxxPlatform.getFlavor(), type == Linker.LinkableDepType.STATIC ? CxxDescriptionEnhancer.STATIC_FLAVOR : CxxDescriptionEnhancer.STATIC_PIC_FLAVOR); Path staticLibraryPath = CxxDescriptionEnhancer.getStaticLibraryPath( getBuildTarget(), cxxPlatform.getFlavor(), type == Linker.LinkableDepType.STATIC ? CxxSourceRuleFactory.PicType.PDC : CxxSourceRuleFactory.PicType.PIC); if (linkWhole) { Linker linker = cxxPlatform.getLd(); linkerArgsBuilder.addAll(linker.linkWhole(staticLibraryPath.toString())); } else { linkerArgsBuilder.add(staticLibraryPath.toString()); } } else { String sharedLibrarySoname = soname.or( CxxDescriptionEnhancer.getDefaultSharedLibrarySoname( params.getBuildTarget(), cxxPlatform)); Path sharedLibraryPath = CxxDescriptionEnhancer.getSharedLibraryPath( getBuildTarget(), sharedLibrarySoname, cxxPlatform); libraryRule = requireBuildRule( targetGraph, cxxPlatform.getFlavor(), CxxDescriptionEnhancer.SHARED_FLAVOR); linkerArgsBuilder.add(sharedLibraryPath.toString()); } final ImmutableList<String> linkerArgs = linkerArgsBuilder.build(); return NativeLinkableInput.of( ImmutableList.<SourcePath>builder() .add(new BuildTargetSourcePath(libraryRule.getBuildTarget())) .addAll(flagsAndBuildInputs.getSecond()) .build(), linkerArgs, Preconditions.checkNotNull(frameworks), Preconditions.checkNotNull(libraries)); }