Search in sources :

Example 71 with BuildTarget

use of com.facebook.buck.model.BuildTarget in project buck by facebook.

the class AuditFlavorsCommand method runWithoutHelp.

@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
    ImmutableSet<BuildTarget> targets = getArgumentsFormattedAsBuildTargets(params.getBuckConfig()).stream().map(input -> BuildTargetParser.INSTANCE.parse(input, BuildTargetPatternParser.fullyQualified(), params.getCell().getCellPathResolver())).collect(MoreCollectors.toImmutableSet());
    if (targets.isEmpty()) {
        params.getBuckEventBus().post(ConsoleEvent.severe("Please specify at least one build target."));
        return 1;
    }
    ImmutableList.Builder<TargetNode<?, ?>> builder = ImmutableList.builder();
    try (CommandThreadManager pool = new CommandThreadManager("Audit", getConcurrencyLimit(params.getBuckConfig()))) {
        for (BuildTarget target : targets) {
            TargetNode<?, ?> targetNode = params.getParser().getTargetNode(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), pool.getExecutor(), target);
            builder.add(targetNode);
        }
    } catch (BuildFileParseException | BuildTargetException e) {
        params.getBuckEventBus().post(ConsoleEvent.severe(MoreExceptions.getHumanReadableOrLocalizedMessage(e)));
        return 1;
    }
    ImmutableList<TargetNode<?, ?>> targetNodes = builder.build();
    if (shouldGenerateJsonOutput()) {
        printJsonFlavors(targetNodes, params);
    } else {
        printFlavors(targetNodes, params);
    }
    return 0;
}
Also used : MoreExceptions(com.facebook.buck.util.MoreExceptions) RichStream(com.facebook.buck.util.RichStream) Flavored(com.facebook.buck.model.Flavored) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) FlavorDomain(com.facebook.buck.model.FlavorDomain) Lists(com.google.common.collect.Lists) Argument(org.kohsuke.args4j.Argument) ImmutableList(com.google.common.collect.ImmutableList) BuildTargetPatternParser(com.facebook.buck.parser.BuildTargetPatternParser) BuildTargetParser(com.facebook.buck.parser.BuildTargetParser) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSet(com.google.common.collect.ImmutableSet) TargetNode(com.facebook.buck.rules.TargetNode) BuildTargetException(com.facebook.buck.model.BuildTargetException) IOException(java.io.IOException) Option(org.kohsuke.args4j.Option) BuildTarget(com.facebook.buck.model.BuildTarget) DirtyPrintStreamDecorator(com.facebook.buck.util.DirtyPrintStreamDecorator) List(java.util.List) TreeMap(java.util.TreeMap) UserFlavor(com.facebook.buck.model.UserFlavor) Optional(java.util.Optional) SortedMap(java.util.SortedMap) Description(com.facebook.buck.rules.Description) BuildTargetException(com.facebook.buck.model.BuildTargetException) TargetNode(com.facebook.buck.rules.TargetNode) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableList(com.google.common.collect.ImmutableList) BuildFileParseException(com.facebook.buck.json.BuildFileParseException)

Example 72 with BuildTarget

use of com.facebook.buck.model.BuildTarget in project buck by facebook.

the class CxxLibraryDescription method createSharedLibraryInterface.

private static <A extends Arg> BuildRule createSharedLibraryInterface(BuildRuleParams baseParams, BuildRuleResolver resolver, CxxPlatform cxxPlatform) throws NoSuchBuildTargetException {
    BuildTarget baseTarget = baseParams.getBuildTarget();
    Optional<SharedLibraryInterfaceFactory> factory = cxxPlatform.getSharedLibraryInterfaceFactory();
    if (!factory.isPresent()) {
        throw new HumanReadableException("%s: C/C++ platform %s does not support shared library interfaces", baseTarget, cxxPlatform.getFlavor());
    }
    CxxLink sharedLibrary = (CxxLink) resolver.requireRule(baseTarget.withAppendedFlavors(cxxPlatform.getFlavor(), Type.SHARED.getFlavor()));
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    return factory.get().createSharedInterfaceLibrary(baseTarget.withAppendedFlavors(Type.SHARED_INTERFACE.getFlavor(), cxxPlatform.getFlavor()), baseParams, resolver, pathResolver, ruleFinder, sharedLibrary.getSourcePathToOutput());
}
Also used : BuildTarget(com.facebook.buck.model.BuildTarget) HumanReadableException(com.facebook.buck.util.HumanReadableException) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver)

Example 73 with BuildTarget

use of com.facebook.buck.model.BuildTarget in project buck by facebook.

the class CxxLibraryDescription method createBuildRule.

public <A extends Arg> BuildRule createBuildRule(final BuildRuleParams params, final BuildRuleResolver resolver, final A args, Optional<Linker.LinkableDepType> linkableDepType, final Optional<SourcePath> bundleLoader, ImmutableSet<BuildTarget> blacklist) throws NoSuchBuildTargetException {
    BuildTarget buildTarget = params.getBuildTarget();
    // See if we're building a particular "type" and "platform" of this library, and if so, extract
    // them from the flavors attached to the build target.
    Optional<Map.Entry<Flavor, Type>> type = getLibType(buildTarget);
    Optional<CxxPlatform> platform = cxxPlatforms.getValue(buildTarget);
    if (params.getBuildTarget().getFlavors().contains(CxxCompilationDatabase.COMPILATION_DATABASE)) {
        // XXX: This needs bundleLoader for tests..
        CxxPlatform cxxPlatform = platform.orElse(defaultCxxPlatform);
        SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
        SourcePathResolver sourcePathResolver = new SourcePathResolver(ruleFinder);
        BuildRuleParams paramsWithoutFlavor = params.withoutFlavor(CxxCompilationDatabase.COMPILATION_DATABASE);
        ImmutableMap<CxxPreprocessAndCompile, SourcePath> objects = requireObjects(paramsWithoutFlavor, resolver, sourcePathResolver, ruleFinder, cxxBuckConfig, cxxPlatform, CxxSourceRuleFactory.PicType.PIC, args);
        return CxxCompilationDatabase.createCompilationDatabase(params, objects.keySet());
    } else if (params.getBuildTarget().getFlavors().contains(CxxCompilationDatabase.UBER_COMPILATION_DATABASE)) {
        return CxxDescriptionEnhancer.createUberCompilationDatabase(platform.isPresent() ? params : params.withAppendedFlavor(defaultCxxPlatform.getFlavor()), resolver);
    } else if (params.getBuildTarget().getFlavors().contains(CxxInferEnhancer.InferFlavors.INFER.get())) {
        return CxxInferEnhancer.requireInferAnalyzeAndReportBuildRuleForCxxDescriptionArg(params, resolver, cxxBuckConfig, platform.orElse(defaultCxxPlatform), args, inferBuckConfig, new CxxInferSourceFilter(inferBuckConfig));
    } else if (params.getBuildTarget().getFlavors().contains(CxxInferEnhancer.InferFlavors.INFER_ANALYZE.get())) {
        return CxxInferEnhancer.requireInferAnalyzeBuildRuleForCxxDescriptionArg(params, resolver, cxxBuckConfig, platform.orElse(defaultCxxPlatform), args, inferBuckConfig, new CxxInferSourceFilter(inferBuckConfig));
    } else if (params.getBuildTarget().getFlavors().contains(CxxInferEnhancer.InferFlavors.INFER_CAPTURE_ALL.get())) {
        return CxxInferEnhancer.requireAllTransitiveCaptureBuildRules(params, resolver, cxxBuckConfig, platform.orElse(defaultCxxPlatform), inferBuckConfig, new CxxInferSourceFilter(inferBuckConfig), args);
    } else if (params.getBuildTarget().getFlavors().contains(CxxInferEnhancer.InferFlavors.INFER_CAPTURE_ONLY.get())) {
        return CxxInferEnhancer.requireInferCaptureAggregatorBuildRuleForCxxDescriptionArg(params, resolver, cxxBuckConfig, platform.orElse(defaultCxxPlatform), args, inferBuckConfig, new CxxInferSourceFilter(inferBuckConfig));
    } else if (type.isPresent() && !platform.isPresent()) {
        BuildRuleParams untypedParams = getUntypedParams(params);
        switch(type.get().getValue()) {
            case EXPORTED_HEADERS:
                Optional<CxxPreprocessables.HeaderMode> mode = HEADER_MODE.getValue(params.getBuildTarget());
                if (mode.isPresent()) {
                    return createExportedHeaderSymlinkTreeBuildRule(untypedParams, resolver, mode.get(), args);
                }
                break;
            // $CASES-OMITTED$
            default:
        }
    } else if (type.isPresent() && platform.isPresent()) {
        // If we *are* building a specific type of this lib, call into the type specific
        // rule builder methods.
        BuildRuleParams untypedParams = getUntypedParams(params);
        switch(type.get().getValue()) {
            case HEADERS:
                return createHeaderSymlinkTreeBuildRule(untypedParams, resolver, platform.get(), args);
            case EXPORTED_HEADERS:
                return createExportedPlatformHeaderSymlinkTreeBuildRule(untypedParams, resolver, platform.get(), args);
            case SHARED:
                return createSharedLibraryBuildRule(untypedParams, resolver, cxxBuckConfig, platform.get(), args, Linker.LinkType.SHARED, linkableDepType.orElse(Linker.LinkableDepType.SHARED), Optional.empty(), blacklist);
            case SHARED_INTERFACE:
                return createSharedLibraryInterface(untypedParams, resolver, platform.get());
            case MACH_O_BUNDLE:
                return createSharedLibraryBuildRule(untypedParams, resolver, cxxBuckConfig, platform.get(), args, Linker.LinkType.MACH_O_BUNDLE, linkableDepType.orElse(Linker.LinkableDepType.SHARED), bundleLoader, blacklist);
            case STATIC:
                return createStaticLibraryBuildRule(untypedParams, resolver, cxxBuckConfig, platform.get(), args, CxxSourceRuleFactory.PicType.PDC);
            case STATIC_PIC:
                return createStaticLibraryBuildRule(untypedParams, resolver, cxxBuckConfig, platform.get(), args, CxxSourceRuleFactory.PicType.PIC);
            case SANDBOX_TREE:
                return CxxDescriptionEnhancer.createSandboxTreeBuildRule(resolver, args, platform.get(), untypedParams);
        }
        throw new RuntimeException("unhandled library build type");
    }
    boolean hasObjectsForAnyPlatform = !args.srcs.isEmpty();
    Predicate<CxxPlatform> hasObjects;
    if (hasObjectsForAnyPlatform) {
        hasObjects = x -> true;
    } else {
        hasObjects = input -> !args.platformSrcs.getMatchingValues(input.getFlavor().toString()).isEmpty();
    }
    Predicate<CxxPlatform> hasExportedHeaders;
    if (!args.exportedHeaders.isEmpty()) {
        hasExportedHeaders = x -> true;
    } else {
        hasExportedHeaders = input -> !args.exportedPlatformHeaders.getMatchingValues(input.getFlavor().toString()).isEmpty();
    }
    // Otherwise, we return the generic placeholder of this library, that dependents can use
    // get the real build rules via querying the action graph.
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    final SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    return new CxxLibrary(params, resolver, FluentIterable.from(args.exportedDeps).transform(resolver::getRule), hasExportedHeaders, Predicates.not(hasObjects), input -> {
        ImmutableList<StringWithMacros> flags = CxxFlags.getFlagsWithMacrosWithPlatformMacroExpansion(args.exportedLinkerFlags, args.exportedPlatformLinkerFlags, input);
        return CxxDescriptionEnhancer.toStringWithMacrosArgs(params.getBuildTarget(), params.getCellRoots(), resolver, input, flags);
    }, cxxPlatform -> {
        try {
            return getSharedLibraryNativeLinkTargetInput(params, resolver, pathResolver, ruleFinder, cxxBuckConfig, cxxPlatform, args, CxxFlags.getFlagsWithMacrosWithPlatformMacroExpansion(args.linkerFlags, args.platformLinkerFlags, cxxPlatform), CxxFlags.getFlagsWithMacrosWithPlatformMacroExpansion(args.exportedLinkerFlags, args.exportedPlatformLinkerFlags, cxxPlatform), args.frameworks, args.libraries);
        } catch (NoSuchBuildTargetException e) {
            throw new RuntimeException(e);
        }
    }, args.supportedPlatformsRegex, args.frameworks, args.libraries, args.forceStatic.orElse(false) ? NativeLinkable.Linkage.STATIC : args.preferredLinkage.orElse(NativeLinkable.Linkage.ANY), args.linkWhole.orElse(false), args.soname, args.tests, args.canBeAsset.orElse(false), !params.getBuildTarget().getFlavors().contains(CxxDescriptionEnhancer.EXPORTED_HEADER_SYMLINK_TREE_FLAVOR));
}
Also used : SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) StringWithMacros(com.facebook.buck.rules.macros.StringWithMacros) SourcePath(com.facebook.buck.rules.SourcePath) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException)

Example 74 with BuildTarget

use of com.facebook.buck.model.BuildTarget in project buck by facebook.

the class CxxLibraryDescription method createStaticLibraryBuildRule.

/**
   * Create all build rules needed to generate the static library.
   *
   * @return build rule that builds the static library version of this C/C++ library.
   */
private static BuildRule createStaticLibraryBuildRule(BuildRuleParams params, BuildRuleResolver resolver, CxxBuckConfig cxxBuckConfig, CxxPlatform cxxPlatform, Arg args, CxxSourceRuleFactory.PicType pic) throws NoSuchBuildTargetException {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver sourcePathResolver = new SourcePathResolver(ruleFinder);
    // Create rules for compiling the object files.
    ImmutableMap<CxxPreprocessAndCompile, SourcePath> objects = requireObjects(params, resolver, sourcePathResolver, ruleFinder, cxxBuckConfig, cxxPlatform, pic, args);
    // Write a build rule to create the archive for this C/C++ library.
    BuildTarget staticTarget = CxxDescriptionEnhancer.createStaticLibraryBuildTarget(params.getBuildTarget(), cxxPlatform.getFlavor(), pic);
    if (objects.isEmpty()) {
        return new NoopBuildRule(new BuildRuleParams(staticTarget, Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.of()), params.getProjectFilesystem(), params.getCellRoots()));
    }
    Path staticLibraryPath = CxxDescriptionEnhancer.getStaticLibraryPath(params.getProjectFilesystem(), params.getBuildTarget(), cxxPlatform.getFlavor(), pic, cxxPlatform.getStaticLibraryExtension());
    return Archive.from(staticTarget, params, ruleFinder, cxxPlatform, cxxBuckConfig.getArchiveContents(), staticLibraryPath, ImmutableList.copyOf(objects.values()));
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) BuildTarget(com.facebook.buck.model.BuildTarget) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver)

Example 75 with BuildTarget

use of com.facebook.buck.model.BuildTarget in project buck by facebook.

the class CxxLibraryDescription method createMetadata.

@Override
public <A extends Arg, U> Optional<U> createMetadata(BuildTarget buildTarget, BuildRuleResolver resolver, A args, Optional<ImmutableMap<BuildTarget, Version>> selectedVersions, final Class<U> metadataClass) throws NoSuchBuildTargetException {
    Map.Entry<Flavor, MetadataType> type = METADATA_TYPE.getFlavorAndValue(buildTarget).orElseThrow(IllegalArgumentException::new);
    BuildTarget baseTarget = buildTarget.withoutFlavors(type.getKey());
    switch(type.getValue()) {
        case CXX_HEADERS:
            {
                Optional<CxxHeaders> symlinkTree = Optional.empty();
                if (!args.exportedHeaders.isEmpty()) {
                    CxxPreprocessables.HeaderMode mode = HEADER_MODE.getRequiredValue(buildTarget);
                    baseTarget = baseTarget.withoutFlavors(mode.getFlavor());
                    symlinkTree = Optional.of(CxxSymlinkTreeHeaders.from((HeaderSymlinkTree) resolver.requireRule(baseTarget.withAppendedFlavors(Type.EXPORTED_HEADERS.getFlavor(), mode.getFlavor())), CxxPreprocessables.IncludeType.LOCAL));
                }
                return symlinkTree.map(metadataClass::cast);
            }
        case CXX_PREPROCESSOR_INPUT:
            {
                Map.Entry<Flavor, CxxPlatform> platform = cxxPlatforms.getFlavorAndValue(buildTarget).orElseThrow(IllegalArgumentException::new);
                Map.Entry<Flavor, HeaderVisibility> visibility = HEADER_VISIBILITY.getFlavorAndValue(buildTarget).orElseThrow(IllegalArgumentException::new);
                baseTarget = baseTarget.withoutFlavors(platform.getKey(), visibility.getKey());
                CxxPreprocessorInput.Builder cxxPreprocessorInputBuilder = CxxPreprocessorInput.builder();
                // TODO(andrewjcg): We currently always add exported flags and frameworks to the
                // preprocessor input to mimic existing behavior, but this should likely be fixed.
                cxxPreprocessorInputBuilder.putAllPreprocessorFlags(CxxFlags.getLanguageFlags(args.exportedPreprocessorFlags, args.exportedPlatformPreprocessorFlags, args.exportedLangPreprocessorFlags, platform.getValue()));
                cxxPreprocessorInputBuilder.addAllFrameworks(args.frameworks);
                if (visibility.getValue() == HeaderVisibility.PRIVATE && !args.headers.isEmpty()) {
                    HeaderSymlinkTree symlinkTree = (HeaderSymlinkTree) resolver.requireRule(baseTarget.withAppendedFlavors(platform.getKey(), Type.HEADERS.getFlavor()));
                    cxxPreprocessorInputBuilder.addIncludes(CxxSymlinkTreeHeaders.from(symlinkTree, CxxPreprocessables.IncludeType.LOCAL));
                }
                if (visibility.getValue() == HeaderVisibility.PUBLIC) {
                    // Add platform-agnostic headers.
                    boolean shouldCreatePublicHeaderSymlinks = args.xcodePublicHeadersSymlinks.orElse(true);
                    CxxPreprocessables.HeaderMode mode = CxxDescriptionEnhancer.getHeaderModeForPlatform(resolver, platform.getValue(), shouldCreatePublicHeaderSymlinks);
                    Optional<CxxHeaders> exportedHeaders = resolver.requireMetadata(baseTarget.withAppendedFlavors(MetadataType.CXX_HEADERS.getFlavor(), mode.getFlavor()), CxxHeaders.class);
                    exportedHeaders.ifPresent(cxxPreprocessorInputBuilder::addIncludes);
                    // Add platform-specific headers.
                    if (!args.exportedPlatformHeaders.getMatchingValues(platform.getKey().toString()).isEmpty()) {
                        HeaderSymlinkTree symlinkTree = (HeaderSymlinkTree) resolver.requireRule(baseTarget.withAppendedFlavors(platform.getKey(), Type.EXPORTED_HEADERS.getFlavor()));
                        cxxPreprocessorInputBuilder.addIncludes(CxxSymlinkTreeHeaders.from(symlinkTree, CxxPreprocessables.IncludeType.LOCAL));
                    }
                }
                CxxPreprocessorInput cxxPreprocessorInput = cxxPreprocessorInputBuilder.build();
                return Optional.of(cxxPreprocessorInput).map(metadataClass::cast);
            }
        case COMPILATION_DATABASE_DEPS:
            {
                return CxxDescriptionEnhancer.createCompilationDatabaseDependencies(buildTarget, cxxPlatforms, resolver, args).map(metadataClass::cast);
            }
    }
    throw new IllegalStateException(String.format("unhandled metadata type: %s", type.getValue()));
}
Also used : Optional(java.util.Optional) InternalFlavor(com.facebook.buck.model.InternalFlavor) Flavor(com.facebook.buck.model.Flavor) BuildTarget(com.facebook.buck.model.BuildTarget) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

BuildTarget (com.facebook.buck.model.BuildTarget)1045 Test (org.junit.Test)758 Path (java.nio.file.Path)323 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)289 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)254 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)248 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)226 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)216 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)209 BuildRule (com.facebook.buck.rules.BuildRule)196 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)178 SourcePath (com.facebook.buck.rules.SourcePath)163 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)161 TargetGraph (com.facebook.buck.rules.TargetGraph)156 PathSourcePath (com.facebook.buck.rules.PathSourcePath)116 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)108 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)91 ImmutableSet (com.google.common.collect.ImmutableSet)90 ImmutableMap (com.google.common.collect.ImmutableMap)78 FakeBuildRuleParamsBuilder (com.facebook.buck.rules.FakeBuildRuleParamsBuilder)75