Program: 53003
Project: 53003
Buggy source:
@Override public <A extends Arg>BuildRule createBuildRule(TargetGraph targetGraph,final BuildRuleParams params,final BuildRuleResolver ruleResolver,final A args) throws NoSuchBuildTargetException {
  Optional<Map.Entry<Flavor,Type>> type=LIBRARY_TYPE.getFlavorAndValue(params.getBuildTarget());
  Optional<Map.Entry<Flavor,CxxPlatform>> platform=cxxPlatforms.getFlavorAndValue(params.getBuildTarget());
  if (type.isPresent()) {
    Preconditions.checkState(platform.isPresent());
    if (type.get().getValue() == Type.EXPORTED_HEADERS) {
      return createExportedHeaderSymlinkTreeBuildRule(params,ruleResolver,platform.get().getValue(),args);
    }
 else     if (type.get().getValue() == Type.SHARED) {
      return createSharedLibraryBuildRule(params,ruleResolver,platform.get().getValue(),args);
    }
  }
  final SourcePathResolver pathResolver=new SourcePathResolver(ruleResolver);
  final boolean headerOnly=args.headerOnly.orElse(false);
  final boolean forceStatic=args.forceStatic.orElse(false);
  return new PrebuiltCxxLibrary(params,pathResolver){
    private final Map<Pair<Flavor,Linker.LinkableDepType>,NativeLinkableInput> nativeLinkableCache=new HashMap<>();
    private final LoadingCache<CxxPreprocessables.CxxPreprocessorInputCacheKey,ImmutableMap<BuildTarget,CxxPreprocessorInput>> transitiveCxxPreprocessorInputCache=CxxPreprocessables.getTransitiveCxxPreprocessorInputCache(this);
    private boolean hasHeaders(    CxxPlatform cxxPlatform){
      if (!args.exportedHeaders.isEmpty()) {
        return true;
      }
      for (      SourceList sourceList : args.exportedPlatformHeaders.getMatchingValues(cxxPlatform.getFlavor().toString())) {
        if (!sourceList.isEmpty()) {
          return true;
        }
      }
      return false;
    }
    private ImmutableListMultimap<CxxSource.Type,String> getExportedPreprocessorFlags(    CxxPlatform cxxPlatform){
      return CxxFlags.getLanguageFlags(args.exportedPreprocessorFlags,args.exportedPlatformPreprocessorFlags,args.exportedLangPreprocessorFlags,cxxPlatform);
    }
    @Override public ImmutableList<String> getExportedLinkerFlags(    CxxPlatform cxxPlatform){
      return CxxFlags.getFlags(args.exportedLinkerFlags,args.exportedPlatformLinkerFlags,cxxPlatform);
    }
    private String getSoname(    CxxPlatform cxxPlatform){
      return PrebuiltCxxLibraryDescription.getSoname(getBuildTarget(),params.getCellRoots(),ruleResolver,cxxPlatform,args.soname,args.libName);
    }
    private boolean isPlatformSupported(    CxxPlatform cxxPlatform){
      return !args.supportedPlatformsRegex.isPresent() || args.supportedPlatformsRegex.get().matcher(cxxPlatform.getFlavor().toString()).find();
    }
    /** 
 * Makes sure all build rules needed to produce the shared library are added to the action graph.
 * @return the {@link SourcePath} representing the actual shared library.
 */
    private SourcePath requireSharedLibrary(    CxxPlatform cxxPlatform) throws NoSuchBuildTargetException {
      SourcePath sharedLibraryPath=PrebuiltCxxLibraryDescription.getSharedLibraryPath(getBuildTarget(),params.getCellRoots(),getProjectFilesystem(),ruleResolver,cxxPlatform,args.libDir,args.libName);
      if (sharedLibraryPath instanceof BuildTargetSourcePath || params.getProjectFilesystem().exists(getResolver().getAbsolutePath(sharedLibraryPath))) {
        return sharedLibraryPath;
      }
      BuildRule sharedLibrary=ruleResolver.requireRule(getBuildTarget().withFlavors(cxxPlatform.getFlavor(),CxxDescriptionEnhancer.SHARED_FLAVOR));
      return new BuildTargetSourcePath(sharedLibrary.getBuildTarget());
    }
    /** 
 * @return the {@link Optional} containing a {@link SourcePath} representing the actualstatic PIC library.
 */
    private Optional<SourcePath> getStaticPicLibrary(    CxxPlatform cxxPlatform){
      SourcePath staticPicLibraryPath=PrebuiltCxxLibraryDescription.getStaticPicLibraryPath(getBuildTarget(),params.getCellRoots(),params.getProjectFilesystem(),ruleResolver,cxxPlatform,args.libDir,args.libName);
      if (params.getProjectFilesystem().exists(getResolver().getAbsolutePath(staticPicLibraryPath))) {
        return Optional.of(staticPicLibraryPath);
      }
      SourcePath staticLibraryPath=PrebuiltCxxLibraryDescription.getStaticLibraryPath(getBuildTarget(),params.getCellRoots(),getProjectFilesystem(),ruleResolver,cxxPlatform,args.libDir,args.libName);
      if (params.getProjectFilesystem().exists(getResolver().getAbsolutePath(staticLibraryPath))) {
        return Optional.of(staticLibraryPath);
      }
      return Optional.empty();
    }
    @Override public Iterable<? extends CxxPreprocessorDep> getCxxPreprocessorDeps(    CxxPlatform cxxPlatform){
      if (!isPlatformSupported(cxxPlatform)) {
        return ImmutableList.of();
      }
      return FluentIterable.from(getDeps()).filter(CxxPreprocessorDep.class);
    }
    @Override public CxxPreprocessorInput getCxxPreprocessorInput(    final CxxPlatform cxxPlatform,    HeaderVisibility headerVisibility) throws NoSuchBuildTargetException {
      CxxPreprocessorInput.Builder builder=CxxPreprocessorInput.builder();
switch (headerVisibility) {
case PUBLIC:
        if (Preconditions.checkNotNull(hasHeaders(cxxPlatform))) {
          CxxPreprocessables.addHeaderSymlinkTree(builder,getBuildTarget(),ruleResolver,cxxPlatform,headerVisibility,CxxPreprocessables.IncludeType.SYSTEM);
        }
      builder.putAllPreprocessorFlags(Preconditions.checkNotNull(getExportedPreprocessorFlags(cxxPlatform)));
    builder.addAllFrameworks(args.frameworks);
  final Iterable<SourcePath> includePaths=args.includeDirs.stream().map(input -> PrebuiltCxxLibraryDescription.getApplicableSourcePath(params.getBuildTarget(),params.getCellRoots(),params.getProjectFilesystem(),ruleResolver,cxxPlatform,input,Optional.empty())).collect(MoreCollectors.toImmutableList());
for (SourcePath includePath : includePaths) {
  builder.addIncludes(CxxHeadersDir.of(CxxPreprocessables.IncludeType.SYSTEM,includePath));
}
return builder.build();
case PRIVATE:
return builder.build();
}
throw new RuntimeException("String_Node_Str" + headerVisibility);
}
@Override public Optional<HeaderSymlinkTree> getExportedHeaderSymlinkTree(CxxPlatform cxxPlatform){
if (hasHeaders(cxxPlatform)) {
return Optional.of(CxxPreprocessables.requireHeaderSymlinkTreeForLibraryTarget(ruleResolver,getBuildTarget(),cxxPlatform.getFlavor()));
}
 else {
return Optional.empty();
}
}
@Override public ImmutableMap<BuildTarget,CxxPreprocessorInput> getTransitiveCxxPreprocessorInput(CxxPlatform cxxPlatform,HeaderVisibility headerVisibility) throws NoSuchBuildTargetException {
return transitiveCxxPreprocessorInputCache.getUnchecked(ImmutableCxxPreprocessorInputCacheKey.of(cxxPlatform,headerVisibility));
}
@Override public Iterable<NativeLinkable> getNativeLinkableDeps(){
return getDeclaredDeps().stream().filter(r -> r instanceof NativeLinkable).map(r -> (NativeLinkable)r).collect(MoreCollectors.toImmutableList());
}
@Override public Iterable<NativeLinkable> getNativeLinkableDepsForPlatform(CxxPlatform cxxPlatform){
if (!isPlatformSupported(cxxPlatform)) {
return ImmutableList.of();
}
return getNativeLinkableDeps();
}
@Override public Iterable<? extends NativeLinkable> getNativeLinkableExportedDeps(){
return args.exportedDeps.stream().map(ruleResolver::getRule).filter(r -> r instanceof NativeLinkable).map(r -> (NativeLinkable)r).collect(MoreCollectors.toImmutableList());
}
@Override public Iterable<? extends NativeLinkable> getNativeLinkableExportedDepsForPlatform(CxxPlatform cxxPlatform){
if (!isPlatformSupported(cxxPlatform)) {
return ImmutableList.of();
}
return getNativeLinkableExportedDeps();
}
private NativeLinkableInput getNativeLinkableInputUncached(CxxPlatform cxxPlatform,Linker.LinkableDepType type) throws NoSuchBuildTargetException {
if (!isPlatformSupported(cxxPlatform)) {
return NativeLinkableInput.of();
}
ImmutableList.Builder<com.facebook.buck.rules.args.Arg> linkerArgsBuilder=ImmutableList.builder();
linkerArgsBuilder.addAll(StringArg.from(Preconditions.checkNotNull(getExportedLinkerFlags(cxxPlatform))));
if (!headerOnly) {
if (type == Linker.LinkableDepType.SHARED) {
Preconditions.checkState(getPreferredLinkage(cxxPlatform) != Linkage.STATIC);
final SourcePath sharedLibrary=requireSharedLibrary(cxxPlatform);
if (args.linkWithoutSoname) {
if (!(sharedLibrary instanceof PathSourcePath)) {
throw new HumanReadableException("String_Node_Str",getBuildTarget());
}
linkerArgsBuilder.add(new RelativeLinkArg((PathSourcePath)sharedLibrary));
}
 else {
linkerArgsBuilder.add(new SourcePathArg(getResolver(),requireSharedLibrary(cxxPlatform)));
}
}
 else {
Preconditions.checkState(getPreferredLinkage(cxxPlatform) != Linkage.SHARED);
SourcePath staticLibraryPath=type == Linker.LinkableDepType.STATIC_PIC ? getStaticPicLibrary(cxxPlatform).get() : PrebuiltCxxLibraryDescription.getStaticLibraryPath(getBuildTarget(),params.getCellRoots(),params.getProjectFilesystem(),ruleResolver,cxxPlatform,args.libDir,args.libName);
SourcePathArg staticLibrary=new SourcePathArg(getResolver(),staticLibraryPath);
if (args.linkWhole) {
Linker linker=cxxPlatform.getLd().resolve(ruleResolver);
linkerArgsBuilder.addAll(linker.linkWhole(staticLibrary));
}
 else {
linkerArgsBuilder.add(FileListableLinkerInputArg.withSourcePathArg(staticLibrary));
}
}
}
final ImmutableList<com.facebook.buck.rules.args.Arg> linkerArgs=linkerArgsBuilder.build();
return NativeLinkableInput.of(linkerArgs,args.frameworks,args.libraries);
}
@Override public NativeLinkableInput getNativeLinkableInput(CxxPlatform cxxPlatform,Linker.LinkableDepType type) throws NoSuchBuildTargetException {
Pair<Flavor,Linker.LinkableDepType> key=new Pair<>(cxxPlatform.getFlavor(),type);
NativeLinkableInput input=nativeLinkableCache.get(key);
if (input == null) {
input=getNativeLinkableInputUncached(cxxPlatform,type);
nativeLinkableCache.put(key,input);
}
return input;
}
@Override public NativeLinkable.Linkage getPreferredLinkage(CxxPlatform cxxPlatform){
if (headerOnly) {
return Linkage.ANY;
}
if (forceStatic) {
return Linkage.STATIC;
}
if (args.provided || !getStaticPicLibrary(cxxPlatform).isPresent()) {
return Linkage.SHARED;
}
return Linkage.ANY;
}
@Override public Iterable<AndroidPackageable> getRequiredPackageables(){
return AndroidPackageableCollector.getPackageableRules(params.getDeps());
}
@Override public void addToCollector(AndroidPackageableCollector collector){
if (args.canBeAsset) {
collector.addNativeLinkableAsset(this);
}
 else {
collector.addNativeLinkable(this);
}
}
@Override public ImmutableMap<String,SourcePath> getSharedLibraries(CxxPlatform cxxPlatform) throws NoSuchBuildTargetException {
if (!isPlatformSupported(cxxPlatform)) {
return ImmutableMap.of();
}
String resolvedSoname=getSoname(cxxPlatform);
ImmutableMap.Builder<String,SourcePath> solibs=ImmutableMap.builder();
if (!headerOnly && !args.provided) {
SourcePath sharedLibrary=requireSharedLibrary(cxxPlatform);
solibs.put(resolvedSoname,sharedLibrary);
}
return solibs.build();
}
@Override public Optional<NativeLinkTarget> getNativeLinkTarget(CxxPlatform cxxPlatform){
if (getPreferredLinkage(cxxPlatform) == Linkage.SHARED) {
return Optional.empty();
}
return Optional.of(new NativeLinkTarget(){
@Override public BuildTarget getBuildTarget(){
return params.getBuildTarget();
}
@Override public NativeLinkTargetMode getNativeLinkTargetMode(CxxPlatform cxxPlatform){
return NativeLinkTargetMode.library(getSoname(cxxPlatform));
}
@Override public Iterable<? extends NativeLinkable> getNativeLinkTargetDeps(CxxPlatform cxxPlatform){
return Iterables.concat(getNativeLinkableDepsForPlatform(cxxPlatform),getNativeLinkableExportedDepsForPlatform(cxxPlatform));
}
@Override public NativeLinkableInput getNativeLinkTargetInput(CxxPlatform cxxPlatform) throws NoSuchBuildTargetException {
return NativeLinkableInput.builder().addAllArgs(StringArg.from(getExportedLinkerFlags(cxxPlatform))).addAllArgs(cxxPlatform.getLd().resolve(ruleResolver).linkWhole(new SourcePathArg(getResolver(),getStaticPicLibrary(cxxPlatform).get()))).build();
}
@Override public Optional<Path> getNativeLinkTargetOutputPath(CxxPlatform cxxPlatform){
return Optional.empty();
}
}
);
}
}
;
}

Fixed version (ground truth):
@Override public <A extends Arg>BuildRule createBuildRule(TargetGraph targetGraph,final BuildRuleParams params,final BuildRuleResolver ruleResolver,final A args) throws NoSuchBuildTargetException {
  Optional<Map.Entry<Flavor,Type>> type=LIBRARY_TYPE.getFlavorAndValue(params.getBuildTarget());
  Optional<Map.Entry<Flavor,CxxPlatform>> platform=cxxPlatforms.getFlavorAndValue(params.getBuildTarget());
  if (type.isPresent()) {
    Preconditions.checkState(platform.isPresent());
    if (type.get().getValue() == Type.EXPORTED_HEADERS) {
      return createExportedHeaderSymlinkTreeBuildRule(params,ruleResolver,platform.get().getValue(),args);
    }
 else     if (type.get().getValue() == Type.SHARED) {
      return createSharedLibraryBuildRule(params,ruleResolver,platform.get().getValue(),args);
    }
  }
  final SourcePathResolver pathResolver=new SourcePathResolver(ruleResolver);
  final boolean headerOnly=args.headerOnly.orElse(false);
  final boolean forceStatic=args.forceStatic.orElse(false);
  return new PrebuiltCxxLibrary(params,pathResolver){
    private final Map<Pair<Flavor,Linker.LinkableDepType>,NativeLinkableInput> nativeLinkableCache=new HashMap<>();
    private final LoadingCache<CxxPreprocessables.CxxPreprocessorInputCacheKey,ImmutableMap<BuildTarget,CxxPreprocessorInput>> transitiveCxxPreprocessorInputCache=CxxPreprocessables.getTransitiveCxxPreprocessorInputCache(this);
    private boolean hasHeaders(    CxxPlatform cxxPlatform){
      if (!args.exportedHeaders.isEmpty()) {
        return true;
      }
      for (      SourceList sourceList : args.exportedPlatformHeaders.getMatchingValues(cxxPlatform.getFlavor().toString())) {
        if (!sourceList.isEmpty()) {
          return true;
        }
      }
      return false;
    }
    private ImmutableListMultimap<CxxSource.Type,String> getExportedPreprocessorFlags(    CxxPlatform cxxPlatform){
      return CxxFlags.getLanguageFlags(args.exportedPreprocessorFlags,args.exportedPlatformPreprocessorFlags,args.exportedLangPreprocessorFlags,cxxPlatform);
    }
    @Override public ImmutableList<String> getExportedLinkerFlags(    CxxPlatform cxxPlatform){
      return CxxFlags.getFlags(args.exportedLinkerFlags,args.exportedPlatformLinkerFlags,cxxPlatform);
    }
    private String getSoname(    CxxPlatform cxxPlatform){
      return PrebuiltCxxLibraryDescription.getSoname(getBuildTarget(),params.getCellRoots(),ruleResolver,cxxPlatform,args.soname,args.libName);
    }
    private boolean isPlatformSupported(    CxxPlatform cxxPlatform){
      return !args.supportedPlatformsRegex.isPresent() || args.supportedPlatformsRegex.get().matcher(cxxPlatform.getFlavor().toString()).find();
    }
    /** 
 * Makes sure all build rules needed to produce the shared library are added to the action graph.
 * @return the {@link SourcePath} representing the actual shared library.
 */
    private SourcePath requireSharedLibrary(    CxxPlatform cxxPlatform) throws NoSuchBuildTargetException {
      SourcePath sharedLibraryPath=PrebuiltCxxLibraryDescription.getSharedLibraryPath(getBuildTarget(),params.getCellRoots(),getProjectFilesystem(),ruleResolver,cxxPlatform,args.libDir,args.libName);
      if (sharedLibraryPath instanceof BuildTargetSourcePath || params.getProjectFilesystem().exists(getResolver().getAbsolutePath(sharedLibraryPath))) {
        return sharedLibraryPath;
      }
      BuildRule sharedLibrary=ruleResolver.requireRule(getBuildTarget().withFlavors(cxxPlatform.getFlavor(),CxxDescriptionEnhancer.SHARED_FLAVOR));
      return new BuildTargetSourcePath(sharedLibrary.getBuildTarget());
    }
    /** 
 * @return the {@link Optional} containing a {@link SourcePath} representing the actualstatic PIC library.
 */
    private Optional<SourcePath> getStaticPicLibrary(    CxxPlatform cxxPlatform){
      SourcePath staticPicLibraryPath=PrebuiltCxxLibraryDescription.getStaticPicLibraryPath(getBuildTarget(),params.getCellRoots(),params.getProjectFilesystem(),ruleResolver,cxxPlatform,args.libDir,args.libName);
      if (params.getProjectFilesystem().exists(getResolver().getAbsolutePath(staticPicLibraryPath))) {
        return Optional.of(staticPicLibraryPath);
      }
      SourcePath staticLibraryPath=PrebuiltCxxLibraryDescription.getStaticLibraryPath(getBuildTarget(),params.getCellRoots(),getProjectFilesystem(),ruleResolver,cxxPlatform,args.libDir,args.libName);
      if (params.getProjectFilesystem().exists(getResolver().getAbsolutePath(staticLibraryPath))) {
        return Optional.of(staticLibraryPath);
      }
      return Optional.empty();
    }
    @Override public Iterable<? extends CxxPreprocessorDep> getCxxPreprocessorDeps(    CxxPlatform cxxPlatform){
      if (!isPlatformSupported(cxxPlatform)) {
        return ImmutableList.of();
      }
      return FluentIterable.from(getDeps()).filter(CxxPreprocessorDep.class);
    }
    @Override public CxxPreprocessorInput getCxxPreprocessorInput(    final CxxPlatform cxxPlatform,    HeaderVisibility headerVisibility) throws NoSuchBuildTargetException {
      CxxPreprocessorInput.Builder builder=CxxPreprocessorInput.builder();
switch (headerVisibility) {
case PUBLIC:
        if (hasHeaders(cxxPlatform)) {
          CxxPreprocessables.addHeaderSymlinkTree(builder,getBuildTarget(),ruleResolver,cxxPlatform,headerVisibility,CxxPreprocessables.IncludeType.SYSTEM);
        }
      builder.putAllPreprocessorFlags(Preconditions.checkNotNull(getExportedPreprocessorFlags(cxxPlatform)));
    builder.addAllFrameworks(args.frameworks);
  final Iterable<SourcePath> includePaths=args.includeDirs.stream().map(input -> PrebuiltCxxLibraryDescription.getApplicableSourcePath(params.getBuildTarget(),params.getCellRoots(),params.getProjectFilesystem(),ruleResolver,cxxPlatform,input,Optional.empty())).collect(MoreCollectors.toImmutableList());
for (SourcePath includePath : includePaths) {
  builder.addIncludes(CxxHeadersDir.of(CxxPreprocessables.IncludeType.SYSTEM,includePath));
}
return builder.build();
case PRIVATE:
return builder.build();
}
throw new RuntimeException("String_Node_Str" + headerVisibility);
}
@Override public Optional<HeaderSymlinkTree> getExportedHeaderSymlinkTree(CxxPlatform cxxPlatform){
if (hasHeaders(cxxPlatform)) {
return Optional.of(CxxPreprocessables.requireHeaderSymlinkTreeForLibraryTarget(ruleResolver,getBuildTarget(),cxxPlatform.getFlavor()));
}
 else {
return Optional.empty();
}
}
@Override public ImmutableMap<BuildTarget,CxxPreprocessorInput> getTransitiveCxxPreprocessorInput(CxxPlatform cxxPlatform,HeaderVisibility headerVisibility) throws NoSuchBuildTargetException {
return transitiveCxxPreprocessorInputCache.getUnchecked(ImmutableCxxPreprocessorInputCacheKey.of(cxxPlatform,headerVisibility));
}
@Override public Iterable<NativeLinkable> getNativeLinkableDeps(){
return getDeclaredDeps().stream().filter(r -> r instanceof NativeLinkable).map(r -> (NativeLinkable)r).collect(MoreCollectors.toImmutableList());
}
@Override public Iterable<NativeLinkable> getNativeLinkableDepsForPlatform(CxxPlatform cxxPlatform){
if (!isPlatformSupported(cxxPlatform)) {
return ImmutableList.of();
}
return getNativeLinkableDeps();
}
@Override public Iterable<? extends NativeLinkable> getNativeLinkableExportedDeps(){
return args.exportedDeps.stream().map(ruleResolver::getRule).filter(r -> r instanceof NativeLinkable).map(r -> (NativeLinkable)r).collect(MoreCollectors.toImmutableList());
}
@Override public Iterable<? extends NativeLinkable> getNativeLinkableExportedDepsForPlatform(CxxPlatform cxxPlatform){
if (!isPlatformSupported(cxxPlatform)) {
return ImmutableList.of();
}
return getNativeLinkableExportedDeps();
}
private NativeLinkableInput getNativeLinkableInputUncached(CxxPlatform cxxPlatform,Linker.LinkableDepType type) throws NoSuchBuildTargetException {
if (!isPlatformSupported(cxxPlatform)) {
return NativeLinkableInput.of();
}
ImmutableList.Builder<com.facebook.buck.rules.args.Arg> linkerArgsBuilder=ImmutableList.builder();
linkerArgsBuilder.addAll(StringArg.from(Preconditions.checkNotNull(getExportedLinkerFlags(cxxPlatform))));
if (!headerOnly) {
if (type == Linker.LinkableDepType.SHARED) {
Preconditions.checkState(getPreferredLinkage(cxxPlatform) != Linkage.STATIC);
final SourcePath sharedLibrary=requireSharedLibrary(cxxPlatform);
if (args.linkWithoutSoname) {
if (!(sharedLibrary instanceof PathSourcePath)) {
throw new HumanReadableException("String_Node_Str",getBuildTarget());
}
linkerArgsBuilder.add(new RelativeLinkArg((PathSourcePath)sharedLibrary));
}
 else {
linkerArgsBuilder.add(new SourcePathArg(getResolver(),requireSharedLibrary(cxxPlatform)));
}
}
 else {
Preconditions.checkState(getPreferredLinkage(cxxPlatform) != Linkage.SHARED);
SourcePath staticLibraryPath=type == Linker.LinkableDepType.STATIC_PIC ? getStaticPicLibrary(cxxPlatform).get() : PrebuiltCxxLibraryDescription.getStaticLibraryPath(getBuildTarget(),params.getCellRoots(),params.getProjectFilesystem(),ruleResolver,cxxPlatform,args.libDir,args.libName);
SourcePathArg staticLibrary=new SourcePathArg(getResolver(),staticLibraryPath);
if (args.linkWhole) {
Linker linker=cxxPlatform.getLd().resolve(ruleResolver);
linkerArgsBuilder.addAll(linker.linkWhole(staticLibrary));
}
 else {
linkerArgsBuilder.add(FileListableLinkerInputArg.withSourcePathArg(staticLibrary));
}
}
}
final ImmutableList<com.facebook.buck.rules.args.Arg> linkerArgs=linkerArgsBuilder.build();
return NativeLinkableInput.of(linkerArgs,args.frameworks,args.libraries);
}
@Override public NativeLinkableInput getNativeLinkableInput(CxxPlatform cxxPlatform,Linker.LinkableDepType type) throws NoSuchBuildTargetException {
Pair<Flavor,Linker.LinkableDepType> key=new Pair<>(cxxPlatform.getFlavor(),type);
NativeLinkableInput input=nativeLinkableCache.get(key);
if (input == null) {
input=getNativeLinkableInputUncached(cxxPlatform,type);
nativeLinkableCache.put(key,input);
}
return input;
}
@Override public NativeLinkable.Linkage getPreferredLinkage(CxxPlatform cxxPlatform){
if (headerOnly) {
return Linkage.ANY;
}
if (forceStatic) {
return Linkage.STATIC;
}
if (args.provided || !getStaticPicLibrary(cxxPlatform).isPresent()) {
return Linkage.SHARED;
}
return Linkage.ANY;
}
@Override public Iterable<AndroidPackageable> getRequiredPackageables(){
return AndroidPackageableCollector.getPackageableRules(params.getDeps());
}
@Override public void addToCollector(AndroidPackageableCollector collector){
if (args.canBeAsset) {
collector.addNativeLinkableAsset(this);
}
 else {
collector.addNativeLinkable(this);
}
}
@Override public ImmutableMap<String,SourcePath> getSharedLibraries(CxxPlatform cxxPlatform) throws NoSuchBuildTargetException {
if (!isPlatformSupported(cxxPlatform)) {
return ImmutableMap.of();
}
String resolvedSoname=getSoname(cxxPlatform);
ImmutableMap.Builder<String,SourcePath> solibs=ImmutableMap.builder();
if (!headerOnly && !args.provided) {
SourcePath sharedLibrary=requireSharedLibrary(cxxPlatform);
solibs.put(resolvedSoname,sharedLibrary);
}
return solibs.build();
}
@Override public Optional<NativeLinkTarget> getNativeLinkTarget(CxxPlatform cxxPlatform){
if (getPreferredLinkage(cxxPlatform) == Linkage.SHARED) {
return Optional.empty();
}
return Optional.of(new NativeLinkTarget(){
@Override public BuildTarget getBuildTarget(){
return params.getBuildTarget();
}
@Override public NativeLinkTargetMode getNativeLinkTargetMode(CxxPlatform cxxPlatform){
return NativeLinkTargetMode.library(getSoname(cxxPlatform));
}
@Override public Iterable<? extends NativeLinkable> getNativeLinkTargetDeps(CxxPlatform cxxPlatform){
return Iterables.concat(getNativeLinkableDepsForPlatform(cxxPlatform),getNativeLinkableExportedDepsForPlatform(cxxPlatform));
}
@Override public NativeLinkableInput getNativeLinkTargetInput(CxxPlatform cxxPlatform) throws NoSuchBuildTargetException {
return NativeLinkableInput.builder().addAllArgs(StringArg.from(getExportedLinkerFlags(cxxPlatform))).addAllArgs(cxxPlatform.getLd().resolve(ruleResolver).linkWhole(new SourcePathArg(getResolver(),getStaticPicLibrary(cxxPlatform).get()))).build();
}
@Override public Optional<Path> getNativeLinkTargetOutputPath(CxxPlatform cxxPlatform){
return Optional.empty();
}
}
);
}
}
;
}