Search in sources :

Example 6 with FrameworkPath

use of com.facebook.buck.rules.coercer.FrameworkPath in project buck by facebook.

the class SwiftCompile method makeCompileStep.

private SwiftCompileStep makeCompileStep(SourcePathResolver resolver) {
    ImmutableList.Builder<String> compilerCommand = ImmutableList.builder();
    compilerCommand.addAll(swiftCompiler.getCommandPrefix(resolver));
    if (bridgingHeader.isPresent()) {
        compilerCommand.add("-import-objc-header", resolver.getRelativePath(bridgingHeader.get()).toString());
    }
    final Function<FrameworkPath, Path> frameworkPathToSearchPath = CxxDescriptionEnhancer.frameworkPathToSearchPath(cxxPlatform, resolver);
    compilerCommand.addAll(frameworks.stream().map(frameworkPathToSearchPath::apply).flatMap(searchPath -> ImmutableSet.of("-F", searchPath.toString()).stream()).iterator());
    compilerCommand.addAll(MoreIterables.zipAndConcat(Iterables.cycle("-Xcc"), getSwiftIncludeArgs(resolver)));
    compilerCommand.addAll(MoreIterables.zipAndConcat(Iterables.cycle(INCLUDE_FLAG), getDeps().stream().filter(SwiftCompile.class::isInstance).map(BuildRule::getSourcePathToOutput).map(input -> resolver.getAbsolutePath(input).toString()).collect(MoreCollectors.toImmutableSet())));
    Optional<Iterable<String>> configFlags = swiftBuckConfig.getFlags();
    if (configFlags.isPresent()) {
        compilerCommand.addAll(configFlags.get());
    }
    boolean hasMainEntry = srcs.stream().map(input -> resolver.getAbsolutePath(input).getFileName().toString()).anyMatch(SwiftDescriptions.SWIFT_MAIN_FILENAME::equalsIgnoreCase);
    compilerCommand.add("-c", enableObjcInterop ? "-enable-objc-interop" : "", hasMainEntry ? "" : "-parse-as-library", "-module-name", moduleName, "-emit-module", "-emit-module-path", modulePath.toString(), "-o", objectPath.toString(), "-emit-objc-header-path", headerPath.toString());
    compilerCommand.addAll(compilerFlags);
    for (SourcePath sourcePath : srcs) {
        compilerCommand.add(resolver.getRelativePath(sourcePath).toString());
    }
    ProjectFilesystem projectFilesystem = getProjectFilesystem();
    return new SwiftCompileStep(projectFilesystem.getRootPath(), ImmutableMap.of(), compilerCommand.build());
}
Also used : FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) Iterables(com.google.common.collect.Iterables) Step(com.facebook.buck.step.Step) SourcePathArg(com.facebook.buck.rules.args.SourcePathArg) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) CxxHeaders(com.facebook.buck.cxx.CxxHeaders) MkdirStep(com.facebook.buck.step.fs.MkdirStep) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) BuildRule(com.facebook.buck.rules.BuildRule) Tool(com.facebook.buck.rules.Tool) ImmutableList(com.google.common.collect.ImmutableList) CxxPreprocessables(com.facebook.buck.cxx.CxxPreprocessables) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) StringArg(com.facebook.buck.rules.args.StringArg) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) CxxDescriptionEnhancer(com.facebook.buck.cxx.CxxDescriptionEnhancer) LinkedHashSet(java.util.LinkedHashSet) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) FileListableLinkerInputArg(com.facebook.buck.rules.args.FileListableLinkerInputArg) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) ImmutableMap(com.google.common.collect.ImmutableMap) BuildableContext(com.facebook.buck.rules.BuildableContext) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) BuildTarget(com.facebook.buck.model.BuildTarget) HeaderVisibility(com.facebook.buck.cxx.HeaderVisibility) AbstractBuildRule(com.facebook.buck.rules.AbstractBuildRule) LinkerMapMode(com.facebook.buck.cxx.LinkerMapMode) Arg(com.facebook.buck.rules.args.Arg) CxxPreprocessorInput(com.facebook.buck.cxx.CxxPreprocessorInput) BuildContext(com.facebook.buck.rules.BuildContext) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MoreIterables(com.facebook.buck.util.MoreIterables) ImmutableList(com.google.common.collect.ImmutableList) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) BuildRule(com.facebook.buck.rules.BuildRule) AbstractBuildRule(com.facebook.buck.rules.AbstractBuildRule) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem)

Example 7 with FrameworkPath

use of com.facebook.buck.rules.coercer.FrameworkPath in project buck by facebook.

the class CxxCollectAndLogInferDependenciesStepTest method createCaptureRule.

private CxxInferCapture createCaptureRule(BuildRuleParams buildRuleParams, SourcePathResolver sourcePathResolver, ProjectFilesystem filesystem, InferBuckConfig inferBuckConfig) throws Exception {
    RuleKeyAppendableFunction<FrameworkPath, Path> defaultFrameworkPathSearchPathFunction = new RuleKeyAppendableFunction<FrameworkPath, Path>() {

        @Override
        public void appendToRuleKey(RuleKeyObjectSink sink) {
        // Do nothing.
        }

        @Override
        public Path apply(FrameworkPath input) {
            return Paths.get("test", "framework", "path", input.toString());
        }
    };
    SourcePath preprocessor = new PathSourcePath(filesystem, Paths.get("preprocessor"));
    Tool preprocessorTool = new CommandTool.Builder().addInput(preprocessor).build();
    PreprocessorDelegate preprocessorDelegate = new PreprocessorDelegate(sourcePathResolver, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_PLATFORM.getHeaderVerification(), Paths.get("whatever"), new GccPreprocessor(preprocessorTool), PreprocessorFlags.builder().build(), defaultFrameworkPathSearchPathFunction, Optional.empty(), /* leadingIncludePaths */
    Optional.empty());
    return new CxxInferCapture(buildRuleParams, CxxToolFlags.of(), CxxToolFlags.of(), new FakeSourcePath("src.c"), AbstractCxxSource.Type.C, Paths.get("src.o"), preprocessorDelegate, inferBuckConfig, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER);
}
Also used : FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) RuleKeyAppendableFunction(com.facebook.buck.rules.args.RuleKeyAppendableFunction) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) PathSourcePath(com.facebook.buck.rules.PathSourcePath) RuleKeyObjectSink(com.facebook.buck.rules.RuleKeyObjectSink) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Tool(com.facebook.buck.rules.Tool) CommandTool(com.facebook.buck.rules.CommandTool)

Example 8 with FrameworkPath

use of com.facebook.buck.rules.coercer.FrameworkPath in project buck by facebook.

the class CxxCompilationDatabaseTest method testCompilationDatabase.

@Test
public void testCompilationDatabase() throws Exception {
    BuildTarget testBuildTarget = BuildTarget.builder(BuildTargetFactory.newInstance("//foo:baz")).addAllFlavors(ImmutableSet.of(CxxCompilationDatabase.COMPILATION_DATABASE)).build();
    final String root = "/Users/user/src";
    final Path fakeRoot = Paths.get(root);
    ProjectFilesystem filesystem = new FakeProjectFilesystem(fakeRoot);
    BuildRuleParams testBuildRuleParams = new FakeBuildRuleParamsBuilder(testBuildTarget).setProjectFilesystem(filesystem).build();
    BuildRuleResolver testBuildRuleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver testSourcePathResolver = new SourcePathResolver(new SourcePathRuleFinder(testBuildRuleResolver));
    HeaderSymlinkTree privateSymlinkTree = CxxDescriptionEnhancer.createHeaderSymlinkTree(testBuildRuleParams, testBuildRuleResolver, CxxPlatformUtils.DEFAULT_PLATFORM, ImmutableMap.of(), HeaderVisibility.PRIVATE, true);
    testBuildRuleResolver.addToIndex(privateSymlinkTree);
    HeaderSymlinkTree exportedSymlinkTree = CxxDescriptionEnhancer.createHeaderSymlinkTree(testBuildRuleParams, testBuildRuleResolver, CxxPlatformUtils.DEFAULT_PLATFORM, ImmutableMap.of(), HeaderVisibility.PUBLIC, true);
    testBuildRuleResolver.addToIndex(exportedSymlinkTree);
    BuildTarget compileTarget = BuildTarget.builder(testBuildRuleParams.getBuildTarget().getUnflavoredBuildTarget()).addFlavors(InternalFlavor.of("compile-test.cpp")).build();
    PreprocessorFlags preprocessorFlags = PreprocessorFlags.builder().addIncludes(CxxHeadersDir.of(CxxPreprocessables.IncludeType.SYSTEM, new FakeSourcePath("/foo/bar")), CxxHeadersDir.of(CxxPreprocessables.IncludeType.SYSTEM, new FakeSourcePath("/test"))).build();
    ImmutableSortedSet.Builder<CxxPreprocessAndCompile> rules = ImmutableSortedSet.naturalOrder();
    BuildRuleParams compileBuildRuleParams = new FakeBuildRuleParamsBuilder(compileTarget).setProjectFilesystem(filesystem).setDeclaredDeps(ImmutableSortedSet.of(privateSymlinkTree, exportedSymlinkTree)).build();
    rules.add(testBuildRuleResolver.addToIndex(CxxPreprocessAndCompile.preprocessAndCompile(compileBuildRuleParams, new PreprocessorDelegate(testSourcePathResolver, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_PLATFORM.getHeaderVerification(), filesystem.getRootPath(), new GccPreprocessor(new HashedFileTool(Paths.get("preprocessor"))), preprocessorFlags, new RuleKeyAppendableFunction<FrameworkPath, Path>() {

        @Override
        public void appendToRuleKey(RuleKeyObjectSink sink) {
        // Do nothing.
        }

        @Override
        public Path apply(FrameworkPath input) {
            throw new UnsupportedOperationException("should not be called");
        }
    }, Optional.empty(), /* leadingIncludePaths */
    Optional.empty()), new CompilerDelegate(testSourcePathResolver, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, new GccCompiler(new HashedFileTool(Paths.get("compiler"))), CxxToolFlags.of()), Paths.get("test.o"), new FakeSourcePath(filesystem, "test.cpp"), CxxSource.Type.CXX, Optional.empty(), CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_ASSEMBLER_DEBUG_PATH_SANITIZER, Optional.empty())));
    CxxCompilationDatabase compilationDatabase = CxxCompilationDatabase.createCompilationDatabase(testBuildRuleParams, rules.build());
    testBuildRuleResolver.addToIndex(compilationDatabase);
    assertThat(compilationDatabase.getRuntimeDeps().collect(MoreCollectors.toImmutableSet()), Matchers.contains(exportedSymlinkTree.getBuildTarget(), privateSymlinkTree.getBuildTarget()));
    assertEquals("getPathToOutput() should be a function of the build target.", BuildTargets.getGenPath(filesystem, testBuildTarget, "__%s.json"), testSourcePathResolver.getRelativePath(compilationDatabase.getSourcePathToOutput()));
    List<Step> buildSteps = compilationDatabase.getBuildSteps(FakeBuildContext.withSourcePathResolver(testSourcePathResolver), new FakeBuildableContext());
    assertEquals(2, buildSteps.size());
    assertTrue(buildSteps.get(0) instanceof MkdirStep);
    assertTrue(buildSteps.get(1) instanceof CxxCompilationDatabase.GenerateCompilationCommandsJson);
    CxxCompilationDatabase.GenerateCompilationCommandsJson step = (CxxCompilationDatabase.GenerateCompilationCommandsJson) buildSteps.get(1);
    Iterable<CxxCompilationDatabaseEntry> observedEntries = step.createEntries();
    Iterable<CxxCompilationDatabaseEntry> expectedEntries = ImmutableList.of(CxxCompilationDatabaseEntry.of(root, root + "/test.cpp", ImmutableList.of("compiler", "-isystem", "/foo/bar", "-isystem", "/test", "-x", "c++", "-c", "-MD", "-MF", "test.o.dep", "test.cpp", "-o", "test.o")));
    MoreAsserts.assertIterablesEquals(expectedEntries, observedEntries);
}
Also used : MkdirStep(com.facebook.buck.step.fs.MkdirStep) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) Step(com.facebook.buck.step.Step) MkdirStep(com.facebook.buck.step.fs.MkdirStep) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) HashedFileTool(com.facebook.buck.rules.HashedFileTool) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) Path(java.nio.file.Path) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) RuleKeyAppendableFunction(com.facebook.buck.rules.args.RuleKeyAppendableFunction) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) RuleKeyObjectSink(com.facebook.buck.rules.RuleKeyObjectSink) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Test(org.junit.Test)

Example 9 with FrameworkPath

use of com.facebook.buck.rules.coercer.FrameworkPath in project buck by facebook.

the class CxxLibraryTest method headerOnlyExports.

@Test
public void headerOnlyExports() throws Exception {
    BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(ruleResolver));
    BuildTarget target = BuildTargetFactory.newInstance("//foo:bar");
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(target).build();
    CxxPlatform cxxPlatform = CxxPlatformUtils.build(new CxxBuckConfig(FakeBuckConfig.builder().build()));
    BuildTarget staticPicLibraryTarget = params.getBuildTarget().withAppendedFlavors(cxxPlatform.getFlavor(), CxxDescriptionEnhancer.STATIC_PIC_FLAVOR);
    ruleResolver.addToIndex(new FakeBuildRule(new FakeBuildRuleParamsBuilder(staticPicLibraryTarget).build(), pathResolver));
    FrameworkPath frameworkPath = FrameworkPath.ofSourcePath(new DefaultBuildTargetSourcePath(BuildTargetFactory.newInstance("//foo:baz")));
    // Construct a CxxLibrary object to test.
    CxxLibrary cxxLibrary = new CxxLibrary(params, ruleResolver, FluentIterable.from(params.getDeclaredDeps().get()), /* hasExportedHeaders */
    x -> true, /* headerOnly */
    x -> true, Functions.constant(StringArg.from("-ldl")), /* linkTargetInput */
    Functions.constant(NativeLinkableInput.of()), /* supportedPlatformsRegex */
    Optional.empty(), ImmutableSet.of(frameworkPath), ImmutableSet.of(), NativeLinkable.Linkage.STATIC, /* linkWhole */
    false, Optional.empty(), ImmutableSortedSet.of(), /* isAsset */
    false, true);
    NativeLinkableInput expectedSharedNativeLinkableInput = NativeLinkableInput.of(StringArg.from("-ldl"), ImmutableSet.of(frameworkPath), ImmutableSet.of());
    assertEquals(expectedSharedNativeLinkableInput, cxxLibrary.getNativeLinkableInput(cxxPlatform, Linker.LinkableDepType.SHARED));
}
Also used : FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 10 with FrameworkPath

use of com.facebook.buck.rules.coercer.FrameworkPath in project buck by facebook.

the class ProjectGenerator method generateBinaryTarget.

private PBXNativeTarget generateBinaryTarget(PBXProject project, Optional<? extends TargetNode<? extends HasAppleBundleFields, ?>> bundle, TargetNode<? extends CxxLibraryDescription.Arg, ?> targetNode, ProductType productType, String productOutputFormat, Optional<Path> infoPlistOptional, boolean includeFrameworks, ImmutableSet<AppleResourceDescription.Arg> recursiveResources, ImmutableSet<AppleResourceDescription.Arg> directResources, ImmutableSet<AppleAssetCatalogDescription.Arg> recursiveAssetCatalogs, ImmutableSet<AppleAssetCatalogDescription.Arg> directAssetCatalogs, ImmutableSet<AppleWrapperResourceArg> wrapperResources, Optional<Iterable<PBXBuildPhase>> copyFilesPhases, Optional<TargetNode<AppleBundleDescription.Arg, ?>> bundleLoaderNode) throws IOException {
    LOG.debug("Generating binary target for node %s", targetNode);
    TargetNode<?, ?> buildTargetNode = bundle.isPresent() ? bundle.get() : targetNode;
    final BuildTarget buildTarget = buildTargetNode.getBuildTarget();
    String buildTargetName = getProductNameForBuildTarget(buildTarget);
    CxxLibraryDescription.Arg arg = targetNode.getConstructorArg();
    NewNativeTargetProjectMutator mutator = new NewNativeTargetProjectMutator(pathRelativizer, this::resolveSourcePath);
    ImmutableSet<SourcePath> exportedHeaders = ImmutableSet.copyOf(getHeaderSourcePaths(arg.exportedHeaders));
    ImmutableSet<SourcePath> headers = ImmutableSet.copyOf(getHeaderSourcePaths(arg.headers));
    ImmutableMap<CxxSource.Type, ImmutableList<String>> langPreprocessorFlags = targetNode.getConstructorArg().langPreprocessorFlags;
    mutator.setTargetName(getXcodeTargetName(buildTarget)).setProduct(productType, buildTargetName, Paths.get(String.format(productOutputFormat, buildTargetName)));
    boolean isFocusedOnTarget = buildTarget.matchesUnflavoredTargets(focusModules);
    Optional<TargetNode<AppleNativeTargetDescriptionArg, ?>> appleTargetNode = targetNode.castArg(AppleNativeTargetDescriptionArg.class);
    if (!shouldGenerateHeaderSymlinkTreesOnly()) {
        if (isFocusedOnTarget) {
            mutator.setLangPreprocessorFlags(langPreprocessorFlags).setPublicHeaders(exportedHeaders).setPrefixHeader(arg.prefixHeader).setSourcesWithFlags(ImmutableSet.copyOf(arg.srcs)).setPrivateHeaders(headers).setRecursiveResources(recursiveResources).setDirectResources(directResources).setWrapperResources(wrapperResources);
        }
        if (bundle.isPresent() && isFocusedOnTarget) {
            HasAppleBundleFields bundleArg = bundle.get().getConstructorArg();
            mutator.setInfoPlist(Optional.of(bundleArg.getInfoPlist()));
        }
        mutator.setBridgingHeader(arg.bridgingHeader);
        if (appleTargetNode.isPresent() && isFocusedOnTarget) {
            AppleNativeTargetDescriptionArg appleArg = appleTargetNode.get().getConstructorArg();
            mutator = mutator.setExtraXcodeSources(ImmutableSet.copyOf(appleArg.extraXcodeSources));
        }
        if (options.contains(Option.CREATE_DIRECTORY_STRUCTURE) && isFocusedOnTarget) {
            mutator.setTargetGroupPath(StreamSupport.stream(buildTarget.getBasePath().spliterator(), false).map(Object::toString).collect(MoreCollectors.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().frameworks);
            frameworksBuilder.addAll(targetNode.getConstructorArg().libraries);
            frameworksBuilder.addAll(collectRecursiveFrameworkDependencies(ImmutableList.of(targetNode)));
            mutator.setFrameworks(frameworksBuilder.build());
            mutator.setArchives(collectRecursiveLibraryDependencies(ImmutableList.of(targetNode)));
        }
        // TODO(Task #3772930): Go through all dependencies of the rule
        // and add any shell script rules here
        ImmutableList.Builder<TargetNode<?, ?>> preScriptPhases = ImmutableList.builder();
        ImmutableList.Builder<TargetNode<?, ?>> postScriptPhases = ImmutableList.builder();
        if (bundle.isPresent() && targetNode != bundle.get() && isFocusedOnTarget) {
            collectBuildScriptDependencies(targetGraph.getAll(bundle.get().getDeclaredDeps()), preScriptPhases, postScriptPhases);
        }
        collectBuildScriptDependencies(targetGraph.getAll(targetNode.getDeclaredDeps()), preScriptPhases, postScriptPhases);
        if (isFocusedOnTarget) {
            mutator.setPreBuildRunScriptPhasesFromTargetNodes(preScriptPhases.build());
            if (copyFilesPhases.isPresent()) {
                mutator.setCopyFilesPhases(copyFilesPhases.get());
            }
            mutator.setPostBuildRunScriptPhasesFromTargetNodes(postScriptPhases.build());
        }
    }
    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();
    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("text.script.python"));
        }
        // -- configurations
        extraSettingsBuilder.put("TARGET_NAME", buildTargetName).put("SRCROOT", pathRelativizer.outputPathToBuildTargetPath(buildTarget).toString());
        if (productType == ProductType.UI_TEST && isFocusedOnTarget) {
            if (bundleLoaderNode.isPresent()) {
                BuildTarget testTarget = bundleLoaderNode.get().getBuildTarget();
                extraSettingsBuilder.put("TEST_TARGET_NAME", getXcodeTargetName(testTarget));
            } else {
                throw new HumanReadableException("The test rule '%s' is configured with 'is_ui_test' but has no test_host_app", buildTargetName);
            }
        } else if (bundleLoaderNode.isPresent() && isFocusedOnTarget) {
            TargetNode<AppleBundleDescription.Arg, ?> bundleLoader = bundleLoaderNode.get();
            String bundleLoaderProductName = getProductNameForBuildTarget(bundleLoader.getBuildTarget());
            String bundleLoaderBundleName = bundleLoaderProductName + "." + getExtensionString(bundleLoader.getConstructorArg().getExtension());
            // NOTE(grp): This is a hack. We need to support both deep (OS X) and flat (iOS)
            // style bundles for the bundle loader, but at this point we don't know what platform
            // the bundle loader (or current target) is going to be built for. However, we can be
            // sure that it's the same as the target (presumably a test) we're building right now.
            //
            // Using that knowledge, we can do build setting tricks to defer choosing the bundle
            // loader path until Xcode build time, when the platform is known. There's no build
            // setting that conclusively says whether the current platform uses deep bundles:
            // that would be too easy. But in the cases we care about (unit test bundles), the
            // current bundle will have a style matching the style of the bundle loader app, so
            // we can take advantage of that to do the determination.
            //
            // Unfortunately, the build setting for the bundle structure (CONTENTS_FOLDER_PATH)
            // includes the WRAPPER_NAME, so we can't just interpolate that in. Instead, we have
            // to use another trick with build setting operations and evaluation. By using the
            // $(:file) operation, we can extract the last component of the contents path: either
            // "Contents" or the current bundle name. Then, we can interpolate with that expected
            // result in the build setting name to conditionally choose a different loader path.
            // The conditional that decides which path is used. This is a complex Xcode build setting
            // expression that expands to one of two values, depending on the last path component of
            // the CONTENTS_FOLDER_PATH variable. As described above, this will be either "Contents"
            // for deep bundles or the bundle file name itself for flat bundles. Finally, to santiize
            // the potentially invalid build setting names from the bundle file name, it converts that
            // to an identifier. We rely on BUNDLE_LOADER_BUNDLE_STYLE_CONDITIONAL_<bundle file name>
            // being undefined (and thus expanding to nothing) for the path resolution to work.
            //
            // The operations on the CONTENTS_FOLDER_PATH are documented here:
            // http://codeworkshop.net/posts/xcode-build-setting-transformations
            String bundleLoaderOutputPathConditional = "$(BUNDLE_LOADER_BUNDLE_STYLE_CONDITIONAL_$(CONTENTS_FOLDER_PATH:file:identifier))";
            // If the $(CONTENTS_FOLDER_PATH:file:identifier) expands to this, we add the deep bundle
            // path into the bundle loader. See above for the case when it will expand to this value.
            String bundleLoaderOutputPathDeepSetting = "BUNDLE_LOADER_BUNDLE_STYLE_CONDITIONAL_Contents";
            String bundleLoaderOutputPathDeepValue = "Contents/MacOS/";
            String bundleLoaderOutputPathValue = Joiner.on('/').join(getTargetOutputPath(bundleLoader), bundleLoaderBundleName, bundleLoaderOutputPathConditional, bundleLoaderProductName);
            extraSettingsBuilder.put(bundleLoaderOutputPathDeepSetting, bundleLoaderOutputPathDeepValue).put("BUNDLE_LOADER", bundleLoaderOutputPathValue).put("TEST_HOST", "$(BUNDLE_LOADER)");
        }
        if (infoPlistOptional.isPresent()) {
            Path infoPlistPath = pathRelativizer.outputDirToRootRelative(infoPlistOptional.get());
            extraSettingsBuilder.put("INFOPLIST_FILE", infoPlistPath.toString());
        }
        if (arg.bridgingHeader.isPresent()) {
            Path bridgingHeaderPath = pathRelativizer.outputDirToRootRelative(resolveSourcePath(arg.bridgingHeader.get()));
            extraSettingsBuilder.put("SWIFT_OBJC_BRIDGING_HEADER", Joiner.on('/').join("$(SRCROOT)", bridgingHeaderPath.toString()));
        }
        Optional<String> swiftVersion = swiftBuckConfig.getVersion();
        if (swiftVersion.isPresent()) {
            extraSettingsBuilder.put("SWIFT_VERSION", swiftVersion.get());
        }
        Optional<SourcePath> prefixHeaderOptional = targetNode.getConstructorArg().prefixHeader;
        if (prefixHeaderOptional.isPresent()) {
            Path prefixHeaderRelative = resolveSourcePath(prefixHeaderOptional.get());
            Path prefixHeaderPath = pathRelativizer.outputDirToRootRelative(prefixHeaderRelative);
            extraSettingsBuilder.put("GCC_PREFIX_HEADER", prefixHeaderPath.toString());
            extraSettingsBuilder.put("GCC_PRECOMPILE_PREFIX_HEADER", "YES");
        }
        extraSettingsBuilder.put("USE_HEADERMAP", "NO");
        defaultSettingsBuilder.put("REPO_ROOT", projectFilesystem.getRootPath().toAbsolutePath().normalize().toString());
        defaultSettingsBuilder.put(PRODUCT_NAME, getProductName(buildTargetNode, buildTarget));
        if (bundle.isPresent()) {
            defaultSettingsBuilder.put("WRAPPER_EXTENSION", getExtensionString(bundle.get().getConstructorArg().getExtension()));
        }
        // We use BUILT_PRODUCTS_DIR as the root for the everything being built. Target-
        // specific output is placed within CONFIGURATION_BUILD_DIR, inside BUILT_PRODUCTS_DIR.
        // That allows Copy Files build phases to reference files in the CONFIGURATION_BUILD_DIR
        // of other targets by using paths relative to the target-independent BUILT_PRODUCTS_DIR.
        defaultSettingsBuilder.put("BUILT_PRODUCTS_DIR", // $SYMROOT/Debug-iphonesimulator
        Joiner.on('/').join("$SYMROOT", "$CONFIGURATION$EFFECTIVE_PLATFORM_NAME"));
        defaultSettingsBuilder.put("CONFIGURATION_BUILD_DIR", "$BUILT_PRODUCTS_DIR");
        boolean nodeIsAppleLibrary = targetNode.getDescription() instanceof AppleLibraryDescription;
        boolean nodeIsCxxLibrary = targetNode.getDescription() instanceof CxxLibraryDescription;
        if (!bundle.isPresent() && (nodeIsAppleLibrary || nodeIsCxxLibrary)) {
            defaultSettingsBuilder.put("EXECUTABLE_PREFIX", "lib");
        }
        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.put("HEADER_SEARCH_PATHS", Joiner.on(' ').join(Iterables.concat(recursiveHeaderSearchPaths, headerMapBases))).put("LIBRARY_SEARCH_PATHS", Joiner.on(' ').join(collectRecursiveLibrarySearchPaths(ImmutableSet.of(targetNode)))).put("FRAMEWORK_SEARCH_PATHS", Joiner.on(' ').join(collectRecursiveFrameworkSearchPaths(ImmutableList.of(targetNode))));
            Iterable<String> otherCFlags = Iterables.concat(cxxBuckConfig.getFlags("cflags").orElse(DEFAULT_CFLAGS), collectRecursiveExportedPreprocessorFlags(ImmutableList.of(targetNode)), targetNode.getConstructorArg().compilerFlags, targetNode.getConstructorArg().preprocessorFlags);
            Iterable<String> otherCxxFlags = Iterables.concat(cxxBuckConfig.getFlags("cxxflags").orElse(DEFAULT_CXXFLAGS), collectRecursiveExportedPreprocessorFlags(ImmutableList.of(targetNode)), targetNode.getConstructorArg().compilerFlags, targetNode.getConstructorArg().preprocessorFlags);
            ImmutableList<String> otherLdFlags = convertStringWithMacros(targetNode, Iterables.concat(targetNode.getConstructorArg().linkerFlags, collectRecursiveExportedLinkerFlags(ImmutableList.of(targetNode))));
            appendConfigsBuilder.put("OTHER_CFLAGS", Joiner.on(' ').join(Iterables.transform(otherCFlags, Escaper.BASH_ESCAPER))).put("OTHER_CPLUSPLUSFLAGS", Joiner.on(' ').join(Iterables.transform(otherCxxFlags, Escaper.BASH_ESCAPER))).put("OTHER_LDFLAGS", Joiner.on(' ').join(Iterables.transform(otherLdFlags, Escaper.BASH_ESCAPER)));
            ImmutableMultimap.Builder<String, ImmutableList<String>> platformFlagsBuilder = ImmutableMultimap.builder();
            for (Pair<Pattern, ImmutableList<String>> flags : Iterables.concat(targetNode.getConstructorArg().platformCompilerFlags.getPatternsAndValues(), targetNode.getConstructorArg().platformPreprocessorFlags.getPatternsAndValues(), collectRecursiveExportedPlatformPreprocessorFlags(ImmutableList.of(targetNode)))) {
                String sdk = flags.getFirst().pattern().replaceAll("[*.]", "");
                platformFlagsBuilder.put(sdk, flags.getSecond());
            }
            ImmutableMultimap<String, ImmutableList<String>> platformFlags = platformFlagsBuilder.build();
            for (String sdk : platformFlags.keySet()) {
                appendConfigsBuilder.put(String.format("OTHER_CFLAGS[sdk=*%s*]", sdk), Joiner.on(' ').join(Iterables.transform(Iterables.concat(otherCFlags, Iterables.concat(platformFlags.get(sdk))), Escaper.BASH_ESCAPER))).put(String.format("OTHER_CPLUSPLUSFLAGS[sdk=*%s*]", 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().platformLinkerFlags.getPatternsAndValues(), collectRecursiveExportedPlatformLinkerFlags(ImmutableList.of(targetNode)))) {
                String sdk = flags.getFirst().pattern().replaceAll("[*.]", "");
                platformLinkerFlagsBuilder.put(sdk, convertStringWithMacros(targetNode, flags.getSecond()));
            }
            ImmutableMultimap<String, ImmutableList<String>> platformLinkerFlags = platformLinkerFlagsBuilder.build();
            for (String sdk : platformLinkerFlags.keySet()) {
                appendConfigsBuilder.put(String.format("OTHER_LDFLAGS[sdk=*%s*]", 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(getConfigurationNameToXcconfigPath(buildTarget), target, project.getMainGroup(), configs.get(), extraSettingsBuilder.build(), defaultSettingsBuilder.build(), appendedConfig);
        }
    }
    // -- phases
    createHeaderSymlinkTree(getPublicCxxHeaders(targetNode), getPathToHeaderSymlinkTree(targetNode, HeaderVisibility.PUBLIC), arg.xcodePublicHeadersSymlinks.orElse(true) || isHeaderMapDisabled(), !shouldMergeHeaderMaps());
    if (isFocusedOnTarget) {
        createHeaderSymlinkTree(getPrivateCxxHeaders(targetNode), getPathToHeaderSymlinkTree(targetNode, HeaderVisibility.PRIVATE), arg.xcodePrivateHeadersSymlinks.orElse(true) || isHeaderMapDisabled(), !isHeaderMapDisabled());
    }
    if (shouldMergeHeaderMaps() && isMainProject) {
        createMergedHeaderMap();
    }
    if (appleTargetNode.isPresent() && isFocusedOnTarget && !shouldGenerateHeaderSymlinkTreesOnly()) {
        // Use Core Data models from immediate dependencies only.
        addCoreDataModelsIntoTarget(appleTargetNode.get(), targetGroup.get());
        addSceneKitAssetsIntoTarget(appleTargetNode.get(), targetGroup.get());
    }
    return target;
}
Also used : TargetNode(com.facebook.buck.rules.TargetNode) ImmutableList(com.google.common.collect.ImmutableList) AppleNativeTargetDescriptionArg(com.facebook.buck.apple.AppleNativeTargetDescriptionArg) NSString(com.dd.plist.NSString) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) HasAppleBundleFields(com.facebook.buck.apple.HasAppleBundleFields) ImmutableSet(com.google.common.collect.ImmutableSet) BuildTarget(com.facebook.buck.model.BuildTarget) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) AppleLibraryDescription(com.facebook.buck.apple.AppleLibraryDescription) SourceTreePath(com.facebook.buck.apple.xcode.xcodeproj.SourceTreePath) SourceTreePath(com.facebook.buck.apple.xcode.xcodeproj.SourceTreePath) Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) Pattern(java.util.regex.Pattern) PBXNativeTarget(com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget) CxxLibraryDescription(com.facebook.buck.cxx.CxxLibraryDescription) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap) ProductType(com.facebook.buck.apple.xcode.xcodeproj.ProductType) HumanReadableException(com.facebook.buck.util.HumanReadableException) PBXGroup(com.facebook.buck.apple.xcode.xcodeproj.PBXGroup) AppleBundleDescription(com.facebook.buck.apple.AppleBundleDescription) NSObject(com.dd.plist.NSObject) PBXFileReference(com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)

Aggregations

FrameworkPath (com.facebook.buck.rules.coercer.FrameworkPath)14 BuildTarget (com.facebook.buck.model.BuildTarget)6 ImmutableList (com.google.common.collect.ImmutableList)6 SourcePath (com.facebook.buck.rules.SourcePath)5 Path (java.nio.file.Path)5 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)4 BuildRule (com.facebook.buck.rules.BuildRule)4 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)4 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)4 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)4 Test (org.junit.Test)4 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)3 FakeBuildRuleParamsBuilder (com.facebook.buck.rules.FakeBuildRuleParamsBuilder)3 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)3 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)3 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)3 PBXFileReference (com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)2 PBXGroup (com.facebook.buck.apple.xcode.xcodeproj.PBXGroup)2 SourceTreePath (com.facebook.buck.apple.xcode.xcodeproj.SourceTreePath)2 LinkerMapMode (com.facebook.buck.cxx.LinkerMapMode)2