Search in sources :

Example 1 with StringWithMacros

use of com.facebook.buck.rules.macros.StringWithMacros 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 2 with StringWithMacros

use of com.facebook.buck.rules.macros.StringWithMacros in project buck by facebook.

the class CxxLibraryDescription method createSharedLibrary.

/**
   * Create all build rules needed to generate the shared library.
   *
   * @return the {@link CxxLink} rule representing the actual shared library.
   */
private static CxxLink createSharedLibrary(BuildRuleParams params, BuildRuleResolver ruleResolver, SourcePathResolver pathResolver, SourcePathRuleFinder ruleFinder, CxxBuckConfig cxxBuckConfig, CxxPlatform cxxPlatform, CxxLibraryDescription.Arg args, ImmutableList<StringWithMacros> linkerFlags, ImmutableSet<FrameworkPath> frameworks, ImmutableSet<FrameworkPath> libraries, Optional<String> soname, Optional<Linker.CxxRuntimeType> cxxRuntimeType, Linker.LinkType linkType, Linker.LinkableDepType linkableDepType, Optional<SourcePath> bundleLoader, ImmutableSet<BuildTarget> blacklist) throws NoSuchBuildTargetException {
    Optional<LinkerMapMode> flavoredLinkerMapMode = LinkerMapMode.FLAVOR_DOMAIN.getValue(params.getBuildTarget());
    params = LinkerMapMode.removeLinkerMapModeFlavorInParams(params, flavoredLinkerMapMode);
    // Create rules for compiling the PIC object files.
    ImmutableMap<CxxPreprocessAndCompile, SourcePath> objects = requireObjects(params, ruleResolver, pathResolver, ruleFinder, cxxBuckConfig, cxxPlatform, CxxSourceRuleFactory.PicType.PIC, args);
    // Setup the rules to link the shared library.
    BuildTarget sharedTarget = CxxDescriptionEnhancer.createSharedLibraryBuildTarget(LinkerMapMode.restoreLinkerMapModeFlavorInParams(params, flavoredLinkerMapMode).getBuildTarget(), cxxPlatform.getFlavor(), linkType);
    String sharedLibrarySoname = CxxDescriptionEnhancer.getSharedLibrarySoname(soname, params.getBuildTarget(), cxxPlatform);
    Path sharedLibraryPath = CxxDescriptionEnhancer.getSharedLibraryPath(params.getProjectFilesystem(), sharedTarget, sharedLibrarySoname);
    ImmutableList.Builder<StringWithMacros> extraLdFlagsBuilder = ImmutableList.builder();
    extraLdFlagsBuilder.addAll(linkerFlags);
    ImmutableList<StringWithMacros> extraLdFlags = extraLdFlagsBuilder.build();
    return CxxLinkableEnhancer.createCxxLinkableBuildRule(cxxBuckConfig, cxxPlatform, LinkerMapMode.restoreLinkerMapModeFlavorInParams(params, flavoredLinkerMapMode), ruleResolver, pathResolver, ruleFinder, sharedTarget, linkType, Optional.of(sharedLibrarySoname), sharedLibraryPath, linkableDepType, Iterables.filter(params.getDeps(), NativeLinkable.class), cxxRuntimeType, bundleLoader, blacklist, NativeLinkableInput.builder().addAllArgs(CxxDescriptionEnhancer.toStringWithMacrosArgs(params.getBuildTarget(), params.getCellRoots(), ruleResolver, cxxPlatform, extraLdFlags)).addAllArgs(SourcePathArg.from(objects.values())).setFrameworks(frameworks).setLibraries(libraries).build());
}
Also used : Path(java.nio.file.Path) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) ImmutableList(com.google.common.collect.ImmutableList) StringWithMacros(com.facebook.buck.rules.macros.StringWithMacros) SourcePath(com.facebook.buck.rules.SourcePath) BuildTarget(com.facebook.buck.model.BuildTarget)

Example 3 with StringWithMacros

use of com.facebook.buck.rules.macros.StringWithMacros in project buck by facebook.

the class CxxLibraryDescriptionTest method locationMacroExpandedExportedPlatformLinkerFlagPlatformMatch.

@Test
public void locationMacroExpandedExportedPlatformLinkerFlagPlatformMatch() throws Exception {
    BuildTarget location = BuildTargetFactory.newInstance("//:loc");
    BuildTarget target = BuildTargetFactory.newInstance("//foo:bar");
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    ExportFileBuilder locBuilder = ExportFileBuilder.newExportFileBuilder(location);
    locBuilder.setOut("somewhere.over.the.rainbow");
    CxxLibraryBuilder libBuilder = new CxxLibraryBuilder(target, cxxBuckConfig);
    libBuilder.setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new PathSourcePath(filesystem, Paths.get("test.cpp")))));
    libBuilder.setExportedPlatformLinkerFlags(PatternMatchedCollection.<ImmutableList<StringWithMacros>>builder().add(Pattern.compile(CxxLibraryBuilder.createDefaultPlatform().getFlavor().toString()), ImmutableList.of(StringWithMacrosUtils.format("-Wl,--version-script=%s", LocationMacro.of(location)))).build());
    TargetGraph targetGraph = TargetGraphFactory.newInstance(libBuilder.build(), locBuilder.build());
    BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    ExportFile loc = locBuilder.build(resolver, filesystem, targetGraph);
    CxxLibrary lib = (CxxLibrary) libBuilder.build(resolver, filesystem, targetGraph);
    NativeLinkableInput nativeLinkableInput = lib.getNativeLinkableInput(CxxLibraryBuilder.createDefaultPlatform(), Linker.LinkableDepType.SHARED);
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    assertThat(FluentIterable.from(nativeLinkableInput.getArgs()).transformAndConcat(arg -> arg.getDeps(ruleFinder)).toSet(), hasItem(loc));
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    assertThat(Arg.stringify(nativeLinkableInput.getArgs(), pathResolver), hasItem(containsString(pathResolver.getRelativePath(Preconditions.checkNotNull(loc.getSourcePathToOutput())).toString())));
}
Also used : FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ExportFileBuilder(com.facebook.buck.shell.ExportFileBuilder) PathSourcePath(com.facebook.buck.rules.PathSourcePath) TargetGraph(com.facebook.buck.rules.TargetGraph) StringWithMacros(com.facebook.buck.rules.macros.StringWithMacros) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ExportFile(com.facebook.buck.shell.ExportFile) BuildTarget(com.facebook.buck.model.BuildTarget) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 4 with StringWithMacros

use of com.facebook.buck.rules.macros.StringWithMacros in project buck by facebook.

the class ProjectGenerator method convertStringWithMacros.

private ImmutableList<String> convertStringWithMacros(TargetNode<?, ?> node, Iterable<StringWithMacros> flags) {
    ImmutableList.Builder<String> result = new ImmutableList.Builder<>();
    ImmutableList<? extends AbstractMacroExpander<? extends Macro>> expanders = ImmutableList.of(new AsIsLocationMacroExpander());
    for (StringWithMacros flag : flags) {
        BuildRuleResolver buildRuleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
        SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(buildRuleResolver));
        StringWithMacrosArg.of(flag, expanders, node.getBuildTarget(), node.getCellNames(), buildRuleResolver).appendToCommandLine(result, pathResolver);
    }
    return result.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) CacheBuilder(com.google.common.cache.CacheBuilder) NSString(com.dd.plist.NSString) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) StringWithMacros(com.facebook.buck.rules.macros.StringWithMacros) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver)

Example 5 with StringWithMacros

use of com.facebook.buck.rules.macros.StringWithMacros in project buck by facebook.

the class CxxLibraryDescription method createSharedLibraryBuildRule.

/**
   * @return a {@link CxxLink} rule which builds a shared library version of this C/C++ library.
   */
private static <A extends Arg> CxxLink createSharedLibraryBuildRule(BuildRuleParams params, BuildRuleResolver resolver, CxxBuckConfig cxxBuckConfig, CxxPlatform cxxPlatform, A args, Linker.LinkType linkType, Linker.LinkableDepType linkableDepType, Optional<SourcePath> bundleLoader, ImmutableSet<BuildTarget> blacklist) throws NoSuchBuildTargetException {
    ImmutableList.Builder<StringWithMacros> linkerFlags = ImmutableList.builder();
    linkerFlags.addAll(CxxFlags.getFlagsWithMacrosWithPlatformMacroExpansion(args.linkerFlags, args.platformLinkerFlags, cxxPlatform));
    linkerFlags.addAll(CxxFlags.getFlagsWithMacrosWithPlatformMacroExpansion(args.exportedLinkerFlags, args.exportedPlatformLinkerFlags, cxxPlatform));
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver sourcePathResolver = new SourcePathResolver(ruleFinder);
    return createSharedLibrary(params, resolver, sourcePathResolver, ruleFinder, cxxBuckConfig, cxxPlatform, args, linkerFlags.build(), args.frameworks, args.libraries, args.soname, args.cxxRuntimeType, linkType, linkableDepType, bundleLoader, blacklist);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) StringWithMacros(com.facebook.buck.rules.macros.StringWithMacros) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver)

Aggregations

StringWithMacros (com.facebook.buck.rules.macros.StringWithMacros)8 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)7 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)7 BuildTarget (com.facebook.buck.model.BuildTarget)6 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)5 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)5 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)4 PathSourcePath (com.facebook.buck.rules.PathSourcePath)4 TargetGraph (com.facebook.buck.rules.TargetGraph)4 ExportFile (com.facebook.buck.shell.ExportFile)4 ExportFileBuilder (com.facebook.buck.shell.ExportFileBuilder)4 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)4 Test (org.junit.Test)4 ImmutableList (com.google.common.collect.ImmutableList)3 SourcePath (com.facebook.buck.rules.SourcePath)2 NSString (com.dd.plist.NSString)1 NoSuchBuildTargetException (com.facebook.buck.parser.NoSuchBuildTargetException)1 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)1 FrameworkPath (com.facebook.buck.rules.coercer.FrameworkPath)1 CacheBuilder (com.google.common.cache.CacheBuilder)1