Program: 52865
Project: 52865
Buggy source:
private PBXNativeTarget generateBinaryTarget(PBXProject project,Optional<? extends TargetNode<? extends HasAppleBundleFields,?>> bundle,TargetNode<? extends CxxLibraryDescription.CommonArg,?> targetNode,ProductType productType,String productOutputFormat,Optional<Path> infoPlistOptional,boolean includeFrameworks,ImmutableSet<AppleResourceDescriptionArg> recursiveResources,ImmutableSet<AppleResourceDescriptionArg> directResources,ImmutableSet<AppleAssetCatalogDescriptionArg> recursiveAssetCatalogs,ImmutableSet<AppleAssetCatalogDescriptionArg> directAssetCatalogs,ImmutableSet<AppleWrapperResourceArg> wrapperResources,Optional<Iterable<PBXBuildPhase>> copyFilesPhases,Optional<TargetNode<AppleBundleDescriptionArg,?>> bundleLoaderNode) throws IOException {
  LOG.debug("String_Node_Str",targetNode);
  TargetNode<?,?> buildTargetNode=bundle.isPresent() ? bundle.get() : targetNode;
  final BuildTarget buildTarget=buildTargetNode.getBuildTarget();
  final boolean containsSwiftCode=projGenerationStateCache.targetContainsSwiftSourceCode(targetNode);
  String buildTargetName=getProductNameForBuildTargetNode(buildTargetNode);
  CxxLibraryDescription.CommonArg arg=targetNode.getConstructorArg();
  NewNativeTargetProjectMutator mutator=new NewNativeTargetProjectMutator(pathRelativizer,this::resolveSourcePath);
  ImmutableSet<SourcePath> exportedHeaders=ImmutableSet.copyOf(getHeaderSourcePaths(arg.getExportedHeaders()));
  ImmutableSet<SourcePath> headers=ImmutableSet.copyOf(getHeaderSourcePaths(arg.getHeaders()));
  ImmutableMap<CxxSource.Type,ImmutableList<StringWithMacros>> langPreprocessorFlags=targetNode.getConstructorArg().getLangPreprocessorFlags();
  boolean isFocusedOnTarget=focusModules.isFocusedOn(buildTarget);
  Optional<String> swiftVersion=(arg instanceof SwiftCommonArg) ? ((SwiftCommonArg)arg).getSwiftVersion() : Optional.empty();
  final boolean hasSwiftVersionArg=swiftVersion.isPresent();
  if (!swiftVersion.isPresent()) {
    swiftVersion=swiftBuckConfig.getVersion();
  }
  mutator.setTargetName(getXcodeTargetName(buildTarget)).setProduct(productType,buildTargetName,Paths.get(String.format(productOutputFormat,buildTargetName)));
  boolean isModularAppleLibrary=isModularAppleLibrary(targetNode) && isFocusedOnTarget;
  mutator.setFrameworkHeadersEnabled(isModularAppleLibrary);
  if (!shouldGenerateHeaderSymlinkTreesOnly()) {
    if (isFocusedOnTarget) {
      mutator.setLangPreprocessorFlags(ImmutableMap.copyOf(Maps.transformValues(langPreprocessorFlags,f -> convertStringWithMacros(targetNode,f)))).setPublicHeaders(exportedHeaders).setPrefixHeader(getPrefixHeaderSourcePath(arg)).setSourcesWithFlags(ImmutableSet.copyOf(arg.getSrcs())).setPrivateHeaders(headers).setRecursiveResources(recursiveResources).setDirectResources(directResources).setWrapperResources(wrapperResources).setExtraXcodeSources(ImmutableSet.copyOf(arg.getExtraXcodeSources())).setExtraXcodeFiles(ImmutableSet.copyOf(arg.getExtraXcodeFiles()));
    }
    if (bundle.isPresent() && isFocusedOnTarget) {
      HasAppleBundleFields bundleArg=bundle.get().getConstructorArg();
      mutator.setInfoPlist(Optional.of(bundleArg.getInfoPlist()));
    }
    mutator.setBridgingHeader(arg.getBridgingHeader());
    if (options.contains(Option.CREATE_DIRECTORY_STRUCTURE) && isFocusedOnTarget) {
      mutator.setTargetGroupPath(RichStream.from(buildTarget.getBasePath()).map(Object::toString).toImmutableList());
    }
    if (!recursiveAssetCatalogs.isEmpty() && isFocusedOnTarget) {
      mutator.setRecursiveAssetCatalogs(recursiveAssetCatalogs);
    }
    if (!directAssetCatalogs.isEmpty() && isFocusedOnTarget) {
      mutator.setDirectAssetCatalogs(directAssetCatalogs);
    }
    if (includeFrameworks && isFocusedOnTarget) {
      ImmutableSet.Builder<FrameworkPath> frameworksBuilder=ImmutableSet.builder();
      frameworksBuilder.addAll(targetNode.getConstructorArg().getFrameworks());
      frameworksBuilder.addAll(targetNode.getConstructorArg().getLibraries());
      frameworksBuilder.addAll(collectRecursiveFrameworkDependencies(targetNode));
      mutator.setFrameworks(frameworksBuilder.build());
      ImmutableSet<PBXFileReference> targetNodeDeps=collectRecursiveLibraryDependencies(targetNode);
      ImmutableSet<PBXFileReference> excludedDeps=targetNode.castArg(AppleTestDescriptionArg.class).flatMap(testNode -> {
        return testNode.getConstructorArg().getIsUiTest() ? Optional.empty() : bundleLoaderNode;
      }
).map(this::collectRecursiveLibraryDependencies).orElse(ImmutableSet.of());
      mutator.setArchives(Sets.difference(targetNodeDeps,excludedDeps));
    }
    if (!includeFrameworks && isFocusedOnTarget) {
      ImmutableSet<PBXFileReference> swiftDeps=collectRecursiveLibraryDependenciesWithSwiftSources(targetNode);
      if (!swiftDeps.isEmpty()) {
        CopyFilePhaseDestinationSpec.Builder destSpecBuilder=CopyFilePhaseDestinationSpec.builder();
        destSpecBuilder.setDestination(PBXCopyFilesBuildPhase.Destination.PRODUCTS);
        PBXCopyFilesBuildPhase copyFiles=new PBXCopyFilesBuildPhase(destSpecBuilder.build());
        copyFiles.setRunOnlyForDeploymentPostprocessing(Optional.of(Boolean.TRUE));
        copyFiles.setName(Optional.of("String_Node_Str"));
        for (        PBXFileReference fileRef : swiftDeps) {
          PBXBuildFile buildFile=new PBXBuildFile(fileRef);
          copyFiles.getFiles().add(buildFile);
        }
        mutator.setSwiftDependenciesBuildPhase(copyFiles);
      }
    }
    ImmutableList.Builder<TargetNode<?,?>> preScriptPhasesBuilder=ImmutableList.builder();
    ImmutableList.Builder<TargetNode<?,?>> postScriptPhasesBuilder=ImmutableList.builder();
    if (bundle.isPresent() && targetNode != bundle.get() && isFocusedOnTarget) {
      collectBuildScriptDependencies(targetGraph.getAll(bundle.get().getDeclaredDeps()),preScriptPhasesBuilder,postScriptPhasesBuilder);
    }
    collectBuildScriptDependencies(targetGraph.getAll(targetNode.getDeclaredDeps()),preScriptPhasesBuilder,postScriptPhasesBuilder);
    if (isFocusedOnTarget) {
      ImmutableList<TargetNode<?,?>> preScriptPhases=preScriptPhasesBuilder.build();
      ImmutableList<TargetNode<?,?>> postScriptPhases=postScriptPhasesBuilder.build();
      mutator.setPreBuildRunScriptPhasesFromTargetNodes(preScriptPhases,buildRuleResolverForNode);
      if (copyFilesPhases.isPresent()) {
        mutator.setCopyFilesPhases(copyFilesPhases.get());
      }
      mutator.setPostBuildRunScriptPhasesFromTargetNodes(postScriptPhases,buildRuleResolverForNode);
      ImmutableList<TargetNode<?,?>> scriptPhases=Stream.concat(preScriptPhases.stream(),postScriptPhases.stream()).collect(MoreCollectors.toImmutableList());
      mutator.collectFilesToCopyInXcode(filesToCopyInXcodeBuilder,scriptPhases,projectCell,buildRuleResolverForNode);
    }
  }
  NewNativeTargetProjectMutator.Result targetBuilderResult=mutator.buildTargetAndAddToProject(project,isFocusedOnTarget);
  PBXNativeTarget target=targetBuilderResult.target;
  Optional<PBXGroup> targetGroup=targetBuilderResult.targetGroup;
  ImmutableMap.Builder<String,String> extraSettingsBuilder=ImmutableMap.builder();
  ImmutableMap.Builder<String,String> defaultSettingsBuilder=ImmutableMap.builder();
  ImmutableSortedMap<Path,SourcePath> publicCxxHeaders=getPublicCxxHeaders(targetNode);
  if (isModularAppleLibrary(targetNode) && isFrameworkProductType(productType)) {
    publicCxxHeaders=ImmutableSortedMap.of();
  }
  if (!shouldGenerateHeaderSymlinkTreesOnly()) {
    if (isFocusedOnTarget) {
      SourceTreePath buckFilePath=new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT,pathRelativizer.outputPathToBuildTargetPath(buildTarget).resolve(buildFileName),Optional.empty());
      PBXFileReference buckReference=targetGroup.get().getOrCreateFileReferenceBySourceTreePath(buckFilePath);
      buckReference.setExplicitFileType(Optional.of("String_Node_Str"));
    }
    extraSettingsBuilder.put("String_Node_Str",buildTargetName).put("String_Node_Str",pathRelativizer.outputPathToBuildTargetPath(buildTarget).toString());
    if (productType == ProductType.UI_TEST && isFocusedOnTarget) {
      if (bundleLoaderNode.isPresent()) {
        BuildTarget testTarget=bundleLoaderNode.get().getBuildTarget();
        extraSettingsBuilder.put("String_Node_Str",getXcodeTargetName(testTarget));
        addPBXTargetDependency(target,testTarget);
      }
 else {
        throw new HumanReadableException("String_Node_Str",buildTargetName);
      }
    }
 else     if (bundleLoaderNode.isPresent() && isFocusedOnTarget) {
      TargetNode<AppleBundleDescriptionArg,?> bundleLoader=bundleLoaderNode.get();
      String bundleLoaderProductName=getProductName(bundleLoader);
      String bundleLoaderBundleName=bundleLoaderProductName + "String_Node_Str" + getExtensionString(bundleLoader.getConstructorArg().getExtension());
      String bundleLoaderOutputPathConditional="String_Node_Str";
      String bundleLoaderOutputPathDeepSetting="String_Node_Str";
      String bundleLoaderOutputPathDeepValue="String_Node_Str";
      String bundleLoaderOutputPathValue=Joiner.on('/').join(getTargetOutputPath(bundleLoader),bundleLoaderBundleName,bundleLoaderOutputPathConditional,bundleLoaderProductName);
      extraSettingsBuilder.put(bundleLoaderOutputPathDeepSetting,bundleLoaderOutputPathDeepValue).put("String_Node_Str",bundleLoaderOutputPathValue).put("String_Node_Str","String_Node_Str");
      addPBXTargetDependency(target,bundleLoader.getBuildTarget());
    }
    if (infoPlistOptional.isPresent()) {
      Path infoPlistPath=pathRelativizer.outputDirToRootRelative(infoPlistOptional.get());
      extraSettingsBuilder.put("String_Node_Str",infoPlistPath.toString());
    }
    if (arg.getBridgingHeader().isPresent()) {
      Path bridgingHeaderPath=pathRelativizer.outputDirToRootRelative(resolveSourcePath(arg.getBridgingHeader().get()));
      extraSettingsBuilder.put("String_Node_Str",Joiner.on('/').join("String_Node_Str",bridgingHeaderPath.toString()));
    }
    swiftVersion.ifPresent(s -> extraSettingsBuilder.put("String_Node_Str",s));
    if (hasSwiftVersionArg && containsSwiftCode && isFocusedOnTarget) {
      extraSettingsBuilder.put("String_Node_Str",getSwiftObjCGeneratedHeaderName(buildTargetNode));
    }
    Optional<SourcePath> prefixHeaderOptional=getPrefixHeaderSourcePath(targetNode.getConstructorArg());
    if (prefixHeaderOptional.isPresent()) {
      Path prefixHeaderRelative=resolveSourcePath(prefixHeaderOptional.get());
      Path prefixHeaderPath=pathRelativizer.outputDirToRootRelative(prefixHeaderRelative);
      extraSettingsBuilder.put("String_Node_Str",prefixHeaderPath.toString());
      extraSettingsBuilder.put("String_Node_Str","String_Node_Str");
    }
    boolean shouldSetUseHeadermap=false;
    if (isModularAppleLibrary) {
      extraSettingsBuilder.put("String_Node_Str","String_Node_Str");
      extraSettingsBuilder.put("String_Node_Str","String_Node_Str");
      if (isFrameworkProductType(productType)) {
        shouldSetUseHeadermap=true;
      }
    }
    extraSettingsBuilder.put("String_Node_Str",shouldSetUseHeadermap ? "String_Node_Str" : "String_Node_Str");
    defaultSettingsBuilder.put("String_Node_Str",projectFilesystem.getRootPath().toAbsolutePath().normalize().toString());
    if (hasSwiftVersionArg && containsSwiftCode && isFocusedOnTarget) {
      Path derivedSourcesDir=getDerivedSourcesDirectoryForBuildTarget(buildTarget,projectFilesystem);
      Path derivedSourceDirRelativeToProjectRoot=getRelativeToProjectPathForTargetNode(targetNode,derivedSourcesDir);
      defaultSettingsBuilder.put("String_Node_Str",derivedSourceDirRelativeToProjectRoot.toString());
    }
    defaultSettingsBuilder.put(PRODUCT_NAME,getProductName(buildTargetNode));
    bundle.ifPresent(bundleNode -> defaultSettingsBuilder.put("String_Node_Str",getExtensionString(bundleNode.getConstructorArg().getExtension())));
    defaultSettingsBuilder.put("String_Node_Str",Joiner.on('/').join("String_Node_Str","String_Node_Str"));
    defaultSettingsBuilder.put("String_Node_Str","String_Node_Str");
    boolean nodeIsAppleLibrary=targetNode.getDescription() instanceof AppleLibraryDescription;
    boolean nodeIsCxxLibrary=targetNode.getDescription() instanceof CxxLibraryDescription;
    if (!bundle.isPresent() && (nodeIsAppleLibrary || nodeIsCxxLibrary)) {
      defaultSettingsBuilder.put("String_Node_Str","String_Node_Str");
    }
    if (isFocusedOnTarget) {
      ImmutableSet<Path> recursiveHeaderSearchPaths=collectRecursiveHeaderSearchPaths(targetNode);
      ImmutableSet<Path> headerMapBases=recursiveHeaderSearchPaths.isEmpty() ? ImmutableSet.of() : ImmutableSet.of(pathRelativizer.outputDirToRootRelative(buildTargetNode.getFilesystem().getBuckPaths().getBuckOut()));
      ImmutableMap.Builder<String,String> appendConfigsBuilder=ImmutableMap.builder();
      appendConfigsBuilder.putAll(getFrameworkAndLibrarySearchPathConfigs(targetNode,includeFrameworks));
      appendConfigsBuilder.put("String_Node_Str",Joiner.on(' ').join(Iterables.concat(recursiveHeaderSearchPaths,headerMapBases)));
      if (hasSwiftVersionArg && containsSwiftCode && isFocusedOnTarget) {
        appendConfigsBuilder.put("String_Node_Str","String_Node_Str");
      }
      ImmutableList.Builder<String> targetSpecificSwiftFlags=ImmutableList.builder();
      Optional<TargetNode<SwiftCommonArg,?>> swiftTargetNode=targetNode.castArg(SwiftCommonArg.class);
      targetSpecificSwiftFlags.addAll(swiftTargetNode.map(x -> convertStringWithMacros(targetNode,x.getConstructorArg().getSwiftCompilerFlags())).orElse(ImmutableList.of()));
      if (containsSwiftCode && isModularAppleLibrary && publicCxxHeaders.size() > 0) {
        targetSpecificSwiftFlags.add("String_Node_Str");
      }
      Iterable<String> otherSwiftFlags=Iterables.concat(swiftBuckConfig.getCompilerFlags().orElse(DEFAULT_SWIFTFLAGS),targetSpecificSwiftFlags.build());
      Iterable<String> otherCFlags=Iterables.concat(cxxBuckConfig.getFlags("String_Node_Str").orElse(DEFAULT_CFLAGS),convertStringWithMacros(targetNode,collectRecursiveExportedPreprocessorFlags(targetNode)),convertStringWithMacros(targetNode,targetNode.getConstructorArg().getCompilerFlags()),convertStringWithMacros(targetNode,targetNode.getConstructorArg().getPreprocessorFlags()));
      Iterable<String> otherCxxFlags=Iterables.concat(cxxBuckConfig.getFlags("String_Node_Str").orElse(DEFAULT_CXXFLAGS),convertStringWithMacros(targetNode,collectRecursiveExportedPreprocessorFlags(targetNode)),convertStringWithMacros(targetNode,targetNode.getConstructorArg().getCompilerFlags()),convertStringWithMacros(targetNode,targetNode.getConstructorArg().getPreprocessorFlags()));
      ImmutableList<String> otherLdFlags=convertStringWithMacros(targetNode,Iterables.concat(targetNode.getConstructorArg().getLinkerFlags(),collectRecursiveExportedLinkerFlags(targetNode)));
      appendConfigsBuilder.put("String_Node_Str",Joiner.on(' ').join(Iterables.transform(otherSwiftFlags,Escaper.BASH_ESCAPER))).put("String_Node_Str",Joiner.on(' ').join(Iterables.transform(otherCFlags,Escaper.BASH_ESCAPER))).put("String_Node_Str",Joiner.on(' ').join(Iterables.transform(otherCxxFlags,Escaper.BASH_ESCAPER))).put("String_Node_Str",Joiner.on(' ').join(Iterables.transform(otherLdFlags,Escaper.BASH_ESCAPER)));
      ImmutableMultimap.Builder<String,ImmutableList<String>> platformFlagsBuilder=ImmutableMultimap.builder();
      for (      Pair<Pattern,ImmutableList<StringWithMacros>> flags : Iterables.concat(targetNode.getConstructorArg().getPlatformCompilerFlags().getPatternsAndValues(),targetNode.getConstructorArg().getPlatformPreprocessorFlags().getPatternsAndValues(),collectRecursiveExportedPlatformPreprocessorFlags(targetNode))) {
        String sdk=flags.getFirst().pattern().replaceAll("String_Node_Str","String_Node_Str");
        platformFlagsBuilder.put(sdk,convertStringWithMacros(targetNode,flags.getSecond()));
      }
      ImmutableMultimap<String,ImmutableList<String>> platformFlags=platformFlagsBuilder.build();
      for (      String sdk : platformFlags.keySet()) {
        appendConfigsBuilder.put(String.format("String_Node_Str",sdk),Joiner.on(' ').join(Iterables.transform(Iterables.concat(otherCFlags,Iterables.concat(platformFlags.get(sdk))),Escaper.BASH_ESCAPER))).put(String.format("String_Node_Str",sdk),Joiner.on(' ').join(Iterables.transform(Iterables.concat(otherCxxFlags,Iterables.concat(platformFlags.get(sdk))),Escaper.BASH_ESCAPER)));
      }
      ImmutableMultimap.Builder<String,ImmutableList<String>> platformLinkerFlagsBuilder=ImmutableMultimap.builder();
      for (      Pair<Pattern,ImmutableList<StringWithMacros>> flags : Iterables.concat(targetNode.getConstructorArg().getPlatformLinkerFlags().getPatternsAndValues(),collectRecursiveExportedPlatformLinkerFlags(targetNode))) {
        String sdk=flags.getFirst().pattern().replaceAll("String_Node_Str","String_Node_Str");
        platformLinkerFlagsBuilder.put(sdk,convertStringWithMacros(targetNode,flags.getSecond()));
      }
      ImmutableMultimap<String,ImmutableList<String>> platformLinkerFlags=platformLinkerFlagsBuilder.build();
      for (      String sdk : platformLinkerFlags.keySet()) {
        appendConfigsBuilder.put(String.format("String_Node_Str",sdk),Joiner.on(' ').join(Iterables.transform(Iterables.concat(otherLdFlags,Iterables.concat(platformLinkerFlags.get(sdk))),Escaper.BASH_ESCAPER)));
      }
      ImmutableMap<String,String> appendedConfig=appendConfigsBuilder.build();
      Optional<ImmutableSortedMap<String,ImmutableMap<String,String>>> configs=getXcodeBuildConfigurationsForTargetNode(targetNode,appendedConfig);
      setTargetBuildConfigurations(buildTarget,target,project.getMainGroup(),configs.get(),extraSettingsBuilder.build(),defaultSettingsBuilder.build(),appendedConfig);
    }
  }
  createHeaderSymlinkTree(publicCxxHeaders,getSwiftPublicHeaderMapEntriesForTarget(targetNode,isFocusedOnTarget,hasSwiftVersionArg),getPathToHeaderSymlinkTree(targetNode,HeaderVisibility.PUBLIC),arg.getXcodePublicHeadersSymlinks().orElse(cxxBuckConfig.getPublicHeadersSymlinksEnabled()) || isHeaderMapDisabled(),!shouldMergeHeaderMaps());
  if (isFocusedOnTarget) {
    createHeaderSymlinkTree(getPrivateCxxHeaders(targetNode),ImmutableMap.of(),getPathToHeaderSymlinkTree(targetNode,HeaderVisibility.PRIVATE),arg.getXcodePrivateHeadersSymlinks().orElse(cxxBuckConfig.getPrivateHeadersSymlinksEnabled()) || isHeaderMapDisabled(),!isHeaderMapDisabled());
  }
  if (shouldMergeHeaderMaps() && isMainProject) {
    createMergedHeaderMap();
  }
  Optional<TargetNode<AppleNativeTargetDescriptionArg,?>> appleTargetNode=targetNode.castArg(AppleNativeTargetDescriptionArg.class);
  if (appleTargetNode.isPresent() && isFocusedOnTarget && !shouldGenerateHeaderSymlinkTreesOnly()) {
    addCoreDataModelsIntoTarget(appleTargetNode.get(),targetGroup.get());
    addSceneKitAssetsIntoTarget(appleTargetNode.get(),targetGroup.get());
  }
  if (bundle.isPresent() && isFocusedOnTarget && !shouldGenerateHeaderSymlinkTreesOnly()) {
    addEntitlementsPlistIntoTarget(bundle.get(),targetGroup.get());
  }
  return target;
}

Fixed version (ground truth):
private PBXNativeTarget generateBinaryTarget(PBXProject project,Optional<? extends TargetNode<? extends HasAppleBundleFields,?>> bundle,TargetNode<? extends CxxLibraryDescription.CommonArg,?> targetNode,ProductType productType,String productOutputFormat,Optional<Path> infoPlistOptional,boolean includeFrameworks,ImmutableSet<AppleResourceDescriptionArg> recursiveResources,ImmutableSet<AppleResourceDescriptionArg> directResources,ImmutableSet<AppleAssetCatalogDescriptionArg> recursiveAssetCatalogs,ImmutableSet<AppleAssetCatalogDescriptionArg> directAssetCatalogs,ImmutableSet<AppleWrapperResourceArg> wrapperResources,Optional<Iterable<PBXBuildPhase>> copyFilesPhases,Optional<TargetNode<AppleBundleDescriptionArg,?>> bundleLoaderNode) throws IOException {
  LOG.debug("String_Node_Str",targetNode);
  TargetNode<?,?> buildTargetNode=bundle.isPresent() ? bundle.get() : targetNode;
  final BuildTarget buildTarget=buildTargetNode.getBuildTarget();
  final boolean containsSwiftCode=projGenerationStateCache.targetContainsSwiftSourceCode(targetNode);
  String buildTargetName=getProductNameForBuildTargetNode(buildTargetNode);
  CxxLibraryDescription.CommonArg arg=targetNode.getConstructorArg();
  NewNativeTargetProjectMutator mutator=new NewNativeTargetProjectMutator(pathRelativizer,this::resolveSourcePath);
  ImmutableSet<SourcePath> exportedHeaders=ImmutableSet.copyOf(getHeaderSourcePaths(arg.getExportedHeaders()));
  ImmutableSet<SourcePath> headers=ImmutableSet.copyOf(getHeaderSourcePaths(arg.getHeaders()));
  ImmutableMap<CxxSource.Type,ImmutableList<StringWithMacros>> langPreprocessorFlags=targetNode.getConstructorArg().getLangPreprocessorFlags();
  boolean isFocusedOnTarget=focusModules.isFocusedOn(buildTarget);
  Optional<String> swiftVersion=(arg instanceof SwiftCommonArg) ? ((SwiftCommonArg)arg).getSwiftVersion() : Optional.empty();
  final boolean hasSwiftVersionArg=swiftVersion.isPresent();
  if (!swiftVersion.isPresent()) {
    swiftVersion=swiftBuckConfig.getVersion();
  }
  mutator.setTargetName(getXcodeTargetName(buildTarget)).setProduct(productType,buildTargetName,Paths.get(String.format(productOutputFormat,buildTargetName)));
  boolean isModularAppleLibrary=isModularAppleLibrary(targetNode) && isFocusedOnTarget;
  mutator.setFrameworkHeadersEnabled(isModularAppleLibrary);
  Optional<ImmutableMap<String,String>> swiftDepsSettings=Optional.empty();
  if (!shouldGenerateHeaderSymlinkTreesOnly()) {
    if (isFocusedOnTarget) {
      mutator.setLangPreprocessorFlags(ImmutableMap.copyOf(Maps.transformValues(langPreprocessorFlags,f -> convertStringWithMacros(targetNode,f)))).setPublicHeaders(exportedHeaders).setPrefixHeader(getPrefixHeaderSourcePath(arg)).setSourcesWithFlags(ImmutableSet.copyOf(arg.getSrcs())).setPrivateHeaders(headers).setRecursiveResources(recursiveResources).setDirectResources(directResources).setWrapperResources(wrapperResources).setExtraXcodeSources(ImmutableSet.copyOf(arg.getExtraXcodeSources())).setExtraXcodeFiles(ImmutableSet.copyOf(arg.getExtraXcodeFiles()));
    }
    if (bundle.isPresent() && isFocusedOnTarget) {
      HasAppleBundleFields bundleArg=bundle.get().getConstructorArg();
      mutator.setInfoPlist(Optional.of(bundleArg.getInfoPlist()));
    }
    mutator.setBridgingHeader(arg.getBridgingHeader());
    if (options.contains(Option.CREATE_DIRECTORY_STRUCTURE) && isFocusedOnTarget) {
      mutator.setTargetGroupPath(RichStream.from(buildTarget.getBasePath()).map(Object::toString).toImmutableList());
    }
    if (!recursiveAssetCatalogs.isEmpty() && isFocusedOnTarget) {
      mutator.setRecursiveAssetCatalogs(recursiveAssetCatalogs);
    }
    if (!directAssetCatalogs.isEmpty() && isFocusedOnTarget) {
      mutator.setDirectAssetCatalogs(directAssetCatalogs);
    }
    if (includeFrameworks && isFocusedOnTarget) {
      ImmutableSet.Builder<FrameworkPath> frameworksBuilder=ImmutableSet.builder();
      frameworksBuilder.addAll(targetNode.getConstructorArg().getFrameworks());
      frameworksBuilder.addAll(targetNode.getConstructorArg().getLibraries());
      frameworksBuilder.addAll(collectRecursiveFrameworkDependencies(targetNode));
      mutator.setFrameworks(frameworksBuilder.build());
      ImmutableSet<PBXFileReference> targetNodeDeps=collectRecursiveLibraryDependencies(targetNode);
      ImmutableSet<PBXFileReference> excludedDeps=targetNode.castArg(AppleTestDescriptionArg.class).flatMap(testNode -> {
        return testNode.getConstructorArg().getIsUiTest() ? Optional.empty() : bundleLoaderNode;
      }
).map(this::collectRecursiveLibraryDependencies).orElse(ImmutableSet.of());
      mutator.setArchives(Sets.difference(targetNodeDeps,excludedDeps));
    }
    if (!includeFrameworks && isFocusedOnTarget) {
      ImmutableSet<PBXFileReference> swiftDeps=collectRecursiveLibraryDependenciesWithSwiftSources(targetNode);
      if (!swiftDeps.isEmpty()) {
        CopyFilePhaseDestinationSpec.Builder destSpecBuilder=CopyFilePhaseDestinationSpec.builder();
        destSpecBuilder.setDestination(PBXCopyFilesBuildPhase.Destination.PRODUCTS);
        PBXCopyFilesBuildPhase copyFiles=new PBXCopyFilesBuildPhase(destSpecBuilder.build());
        copyFiles.setRunOnlyForDeploymentPostprocessing(Optional.of(Boolean.TRUE));
        copyFiles.setName(Optional.of("String_Node_Str"));
        for (        PBXFileReference fileRef : swiftDeps) {
          PBXBuildFile buildFile=new PBXBuildFile(fileRef);
          copyFiles.getFiles().add(buildFile);
        }
        swiftDepsSettings=Optional.of(ImmutableMap.of("String_Node_Str","String_Node_Str"));
        mutator.setSwiftDependenciesBuildPhase(copyFiles);
      }
    }
    ImmutableList.Builder<TargetNode<?,?>> preScriptPhasesBuilder=ImmutableList.builder();
    ImmutableList.Builder<TargetNode<?,?>> postScriptPhasesBuilder=ImmutableList.builder();
    if (bundle.isPresent() && targetNode != bundle.get() && isFocusedOnTarget) {
      collectBuildScriptDependencies(targetGraph.getAll(bundle.get().getDeclaredDeps()),preScriptPhasesBuilder,postScriptPhasesBuilder);
    }
    collectBuildScriptDependencies(targetGraph.getAll(targetNode.getDeclaredDeps()),preScriptPhasesBuilder,postScriptPhasesBuilder);
    if (isFocusedOnTarget) {
      ImmutableList<TargetNode<?,?>> preScriptPhases=preScriptPhasesBuilder.build();
      ImmutableList<TargetNode<?,?>> postScriptPhases=postScriptPhasesBuilder.build();
      mutator.setPreBuildRunScriptPhasesFromTargetNodes(preScriptPhases,buildRuleResolverForNode);
      if (copyFilesPhases.isPresent()) {
        mutator.setCopyFilesPhases(copyFilesPhases.get());
      }
      mutator.setPostBuildRunScriptPhasesFromTargetNodes(postScriptPhases,buildRuleResolverForNode);
      ImmutableList<TargetNode<?,?>> scriptPhases=Stream.concat(preScriptPhases.stream(),postScriptPhases.stream()).collect(MoreCollectors.toImmutableList());
      mutator.collectFilesToCopyInXcode(filesToCopyInXcodeBuilder,scriptPhases,projectCell,buildRuleResolverForNode);
    }
  }
  NewNativeTargetProjectMutator.Result targetBuilderResult=mutator.buildTargetAndAddToProject(project,isFocusedOnTarget);
  PBXNativeTarget target=targetBuilderResult.target;
  Optional<PBXGroup> targetGroup=targetBuilderResult.targetGroup;
  ImmutableMap.Builder<String,String> extraSettingsBuilder=ImmutableMap.builder();
  ImmutableMap.Builder<String,String> defaultSettingsBuilder=ImmutableMap.builder();
  swiftDepsSettings.ifPresent(settings -> extraSettingsBuilder.putAll(settings));
  ImmutableSortedMap<Path,SourcePath> publicCxxHeaders=getPublicCxxHeaders(targetNode);
  if (isModularAppleLibrary(targetNode) && isFrameworkProductType(productType)) {
    publicCxxHeaders=ImmutableSortedMap.of();
  }
  if (!shouldGenerateHeaderSymlinkTreesOnly()) {
    if (isFocusedOnTarget) {
      SourceTreePath buckFilePath=new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT,pathRelativizer.outputPathToBuildTargetPath(buildTarget).resolve(buildFileName),Optional.empty());
      PBXFileReference buckReference=targetGroup.get().getOrCreateFileReferenceBySourceTreePath(buckFilePath);
      buckReference.setExplicitFileType(Optional.of("String_Node_Str"));
    }
    extraSettingsBuilder.put("String_Node_Str",buildTargetName).put("String_Node_Str",pathRelativizer.outputPathToBuildTargetPath(buildTarget).toString());
    if (productType == ProductType.UI_TEST && isFocusedOnTarget) {
      if (bundleLoaderNode.isPresent()) {
        BuildTarget testTarget=bundleLoaderNode.get().getBuildTarget();
        extraSettingsBuilder.put("String_Node_Str",getXcodeTargetName(testTarget));
        addPBXTargetDependency(target,testTarget);
      }
 else {
        throw new HumanReadableException("String_Node_Str",buildTargetName);
      }
    }
 else     if (bundleLoaderNode.isPresent() && isFocusedOnTarget) {
      TargetNode<AppleBundleDescriptionArg,?> bundleLoader=bundleLoaderNode.get();
      String bundleLoaderProductName=getProductName(bundleLoader);
      String bundleLoaderBundleName=bundleLoaderProductName + "String_Node_Str" + getExtensionString(bundleLoader.getConstructorArg().getExtension());
      String bundleLoaderOutputPathConditional="String_Node_Str";
      String bundleLoaderOutputPathDeepSetting="String_Node_Str";
      String bundleLoaderOutputPathDeepValue="String_Node_Str";
      String bundleLoaderOutputPathValue=Joiner.on('/').join(getTargetOutputPath(bundleLoader),bundleLoaderBundleName,bundleLoaderOutputPathConditional,bundleLoaderProductName);
      extraSettingsBuilder.put(bundleLoaderOutputPathDeepSetting,bundleLoaderOutputPathDeepValue).put("String_Node_Str",bundleLoaderOutputPathValue).put("String_Node_Str","String_Node_Str");
      addPBXTargetDependency(target,bundleLoader.getBuildTarget());
    }
    if (infoPlistOptional.isPresent()) {
      Path infoPlistPath=pathRelativizer.outputDirToRootRelative(infoPlistOptional.get());
      extraSettingsBuilder.put("String_Node_Str",infoPlistPath.toString());
    }
    if (arg.getBridgingHeader().isPresent()) {
      Path bridgingHeaderPath=pathRelativizer.outputDirToRootRelative(resolveSourcePath(arg.getBridgingHeader().get()));
      extraSettingsBuilder.put("String_Node_Str",Joiner.on('/').join("String_Node_Str",bridgingHeaderPath.toString()));
    }
    swiftVersion.ifPresent(s -> extraSettingsBuilder.put("String_Node_Str",s));
    if (hasSwiftVersionArg && containsSwiftCode && isFocusedOnTarget) {
      extraSettingsBuilder.put("String_Node_Str",getSwiftObjCGeneratedHeaderName(buildTargetNode));
    }
    Optional<SourcePath> prefixHeaderOptional=getPrefixHeaderSourcePath(targetNode.getConstructorArg());
    if (prefixHeaderOptional.isPresent()) {
      Path prefixHeaderRelative=resolveSourcePath(prefixHeaderOptional.get());
      Path prefixHeaderPath=pathRelativizer.outputDirToRootRelative(prefixHeaderRelative);
      extraSettingsBuilder.put("String_Node_Str",prefixHeaderPath.toString());
      extraSettingsBuilder.put("String_Node_Str","String_Node_Str");
    }
    boolean shouldSetUseHeadermap=false;
    if (isModularAppleLibrary) {
      extraSettingsBuilder.put("String_Node_Str","String_Node_Str");
      extraSettingsBuilder.put("String_Node_Str","String_Node_Str");
      if (isFrameworkProductType(productType)) {
        shouldSetUseHeadermap=true;
      }
    }
    extraSettingsBuilder.put("String_Node_Str",shouldSetUseHeadermap ? "String_Node_Str" : "String_Node_Str");
    defaultSettingsBuilder.put("String_Node_Str",projectFilesystem.getRootPath().toAbsolutePath().normalize().toString());
    if (hasSwiftVersionArg && containsSwiftCode && isFocusedOnTarget) {
      Path derivedSourcesDir=getDerivedSourcesDirectoryForBuildTarget(buildTarget,projectFilesystem);
      Path derivedSourceDirRelativeToProjectRoot=getRelativeToProjectPathForTargetNode(targetNode,derivedSourcesDir);
      defaultSettingsBuilder.put("String_Node_Str",derivedSourceDirRelativeToProjectRoot.toString());
    }
    defaultSettingsBuilder.put(PRODUCT_NAME,getProductName(buildTargetNode));
    bundle.ifPresent(bundleNode -> defaultSettingsBuilder.put("String_Node_Str",getExtensionString(bundleNode.getConstructorArg().getExtension())));
    defaultSettingsBuilder.put("String_Node_Str",Joiner.on('/').join("String_Node_Str","String_Node_Str"));
    defaultSettingsBuilder.put("String_Node_Str","String_Node_Str");
    boolean nodeIsAppleLibrary=targetNode.getDescription() instanceof AppleLibraryDescription;
    boolean nodeIsCxxLibrary=targetNode.getDescription() instanceof CxxLibraryDescription;
    if (!bundle.isPresent() && (nodeIsAppleLibrary || nodeIsCxxLibrary)) {
      defaultSettingsBuilder.put("String_Node_Str","String_Node_Str");
    }
    if (isFocusedOnTarget) {
      ImmutableSet<Path> recursiveHeaderSearchPaths=collectRecursiveHeaderSearchPaths(targetNode);
      ImmutableSet<Path> headerMapBases=recursiveHeaderSearchPaths.isEmpty() ? ImmutableSet.of() : ImmutableSet.of(pathRelativizer.outputDirToRootRelative(buildTargetNode.getFilesystem().getBuckPaths().getBuckOut()));
      ImmutableMap.Builder<String,String> appendConfigsBuilder=ImmutableMap.builder();
      appendConfigsBuilder.putAll(getFrameworkAndLibrarySearchPathConfigs(targetNode,includeFrameworks));
      appendConfigsBuilder.put("String_Node_Str",Joiner.on(' ').join(Iterables.concat(recursiveHeaderSearchPaths,headerMapBases)));
      if (hasSwiftVersionArg && containsSwiftCode && isFocusedOnTarget) {
        appendConfigsBuilder.put("String_Node_Str","String_Node_Str");
      }
      ImmutableList.Builder<String> targetSpecificSwiftFlags=ImmutableList.builder();
      Optional<TargetNode<SwiftCommonArg,?>> swiftTargetNode=targetNode.castArg(SwiftCommonArg.class);
      targetSpecificSwiftFlags.addAll(swiftTargetNode.map(x -> convertStringWithMacros(targetNode,x.getConstructorArg().getSwiftCompilerFlags())).orElse(ImmutableList.of()));
      if (containsSwiftCode && isModularAppleLibrary && publicCxxHeaders.size() > 0) {
        targetSpecificSwiftFlags.add("String_Node_Str");
      }
      Iterable<String> otherSwiftFlags=Iterables.concat(swiftBuckConfig.getCompilerFlags().orElse(DEFAULT_SWIFTFLAGS),targetSpecificSwiftFlags.build());
      Iterable<String> otherCFlags=Iterables.concat(cxxBuckConfig.getFlags("String_Node_Str").orElse(DEFAULT_CFLAGS),convertStringWithMacros(targetNode,collectRecursiveExportedPreprocessorFlags(targetNode)),convertStringWithMacros(targetNode,targetNode.getConstructorArg().getCompilerFlags()),convertStringWithMacros(targetNode,targetNode.getConstructorArg().getPreprocessorFlags()));
      Iterable<String> otherCxxFlags=Iterables.concat(cxxBuckConfig.getFlags("String_Node_Str").orElse(DEFAULT_CXXFLAGS),convertStringWithMacros(targetNode,collectRecursiveExportedPreprocessorFlags(targetNode)),convertStringWithMacros(targetNode,targetNode.getConstructorArg().getCompilerFlags()),convertStringWithMacros(targetNode,targetNode.getConstructorArg().getPreprocessorFlags()));
      ImmutableList<String> otherLdFlags=convertStringWithMacros(targetNode,Iterables.concat(targetNode.getConstructorArg().getLinkerFlags(),collectRecursiveExportedLinkerFlags(targetNode)));
      appendConfigsBuilder.put("String_Node_Str",Joiner.on(' ').join(Iterables.transform(otherSwiftFlags,Escaper.BASH_ESCAPER))).put("String_Node_Str",Joiner.on(' ').join(Iterables.transform(otherCFlags,Escaper.BASH_ESCAPER))).put("String_Node_Str",Joiner.on(' ').join(Iterables.transform(otherCxxFlags,Escaper.BASH_ESCAPER))).put("String_Node_Str",Joiner.on(' ').join(Iterables.transform(otherLdFlags,Escaper.BASH_ESCAPER)));
      ImmutableMultimap.Builder<String,ImmutableList<String>> platformFlagsBuilder=ImmutableMultimap.builder();
      for (      Pair<Pattern,ImmutableList<StringWithMacros>> flags : Iterables.concat(targetNode.getConstructorArg().getPlatformCompilerFlags().getPatternsAndValues(),targetNode.getConstructorArg().getPlatformPreprocessorFlags().getPatternsAndValues(),collectRecursiveExportedPlatformPreprocessorFlags(targetNode))) {
        String sdk=flags.getFirst().pattern().replaceAll("String_Node_Str","String_Node_Str");
        platformFlagsBuilder.put(sdk,convertStringWithMacros(targetNode,flags.getSecond()));
      }
      ImmutableMultimap<String,ImmutableList<String>> platformFlags=platformFlagsBuilder.build();
      for (      String sdk : platformFlags.keySet()) {
        appendConfigsBuilder.put(String.format("String_Node_Str",sdk),Joiner.on(' ').join(Iterables.transform(Iterables.concat(otherCFlags,Iterables.concat(platformFlags.get(sdk))),Escaper.BASH_ESCAPER))).put(String.format("String_Node_Str",sdk),Joiner.on(' ').join(Iterables.transform(Iterables.concat(otherCxxFlags,Iterables.concat(platformFlags.get(sdk))),Escaper.BASH_ESCAPER)));
      }
      ImmutableMultimap.Builder<String,ImmutableList<String>> platformLinkerFlagsBuilder=ImmutableMultimap.builder();
      for (      Pair<Pattern,ImmutableList<StringWithMacros>> flags : Iterables.concat(targetNode.getConstructorArg().getPlatformLinkerFlags().getPatternsAndValues(),collectRecursiveExportedPlatformLinkerFlags(targetNode))) {
        String sdk=flags.getFirst().pattern().replaceAll("String_Node_Str","String_Node_Str");
        platformLinkerFlagsBuilder.put(sdk,convertStringWithMacros(targetNode,flags.getSecond()));
      }
      ImmutableMultimap<String,ImmutableList<String>> platformLinkerFlags=platformLinkerFlagsBuilder.build();
      for (      String sdk : platformLinkerFlags.keySet()) {
        appendConfigsBuilder.put(String.format("String_Node_Str",sdk),Joiner.on(' ').join(Iterables.transform(Iterables.concat(otherLdFlags,Iterables.concat(platformLinkerFlags.get(sdk))),Escaper.BASH_ESCAPER)));
      }
      ImmutableMap<String,String> appendedConfig=appendConfigsBuilder.build();
      Optional<ImmutableSortedMap<String,ImmutableMap<String,String>>> configs=getXcodeBuildConfigurationsForTargetNode(targetNode,appendedConfig);
      setTargetBuildConfigurations(buildTarget,target,project.getMainGroup(),configs.get(),extraSettingsBuilder.build(),defaultSettingsBuilder.build(),appendedConfig);
    }
  }
  createHeaderSymlinkTree(publicCxxHeaders,getSwiftPublicHeaderMapEntriesForTarget(targetNode,isFocusedOnTarget,hasSwiftVersionArg),getPathToHeaderSymlinkTree(targetNode,HeaderVisibility.PUBLIC),arg.getXcodePublicHeadersSymlinks().orElse(cxxBuckConfig.getPublicHeadersSymlinksEnabled()) || isHeaderMapDisabled(),!shouldMergeHeaderMaps());
  if (isFocusedOnTarget) {
    createHeaderSymlinkTree(getPrivateCxxHeaders(targetNode),ImmutableMap.of(),getPathToHeaderSymlinkTree(targetNode,HeaderVisibility.PRIVATE),arg.getXcodePrivateHeadersSymlinks().orElse(cxxBuckConfig.getPrivateHeadersSymlinksEnabled()) || isHeaderMapDisabled(),!isHeaderMapDisabled());
  }
  if (shouldMergeHeaderMaps() && isMainProject) {
    createMergedHeaderMap();
  }
  Optional<TargetNode<AppleNativeTargetDescriptionArg,?>> appleTargetNode=targetNode.castArg(AppleNativeTargetDescriptionArg.class);
  if (appleTargetNode.isPresent() && isFocusedOnTarget && !shouldGenerateHeaderSymlinkTreesOnly()) {
    addCoreDataModelsIntoTarget(appleTargetNode.get(),targetGroup.get());
    addSceneKitAssetsIntoTarget(appleTargetNode.get(),targetGroup.get());
  }
  if (bundle.isPresent() && isFocusedOnTarget && !shouldGenerateHeaderSymlinkTreesOnly()) {
    addEntitlementsPlistIntoTarget(bundle.get(),targetGroup.get());
  }
  return target;
}