Search in sources :

Example 6 with WriteFile

use of com.facebook.buck.file.WriteFile in project buck by facebook.

the class PythonBinaryDescription method createEmptyInitModule.

public static SourcePath createEmptyInitModule(BuildRuleParams params, BuildRuleResolver resolver) {
    BuildTarget emptyInitTarget = getEmptyInitTarget(params.getBuildTarget());
    Path emptyInitPath = BuildTargets.getGenPath(params.getProjectFilesystem(), params.getBuildTarget(), "%s/__init__.py");
    WriteFile rule = resolver.addToIndex(new WriteFile(params.withBuildTarget(emptyInitTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.of())), "", emptyInitPath, /* executable */
    false));
    return rule.getSourcePathToOutput();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) WriteFile(com.facebook.buck.file.WriteFile) BuildTarget(com.facebook.buck.model.BuildTarget)

Example 7 with WriteFile

use of com.facebook.buck.file.WriteFile in project buck by facebook.

the class AppleBinaryDescription method requireThinBinary.

private <A extends Arg> BuildRule requireThinBinary(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
    Optional<BuildRule> existingThinRule = resolver.getRuleOptional(params.getBuildTarget());
    if (existingThinRule.isPresent()) {
        return existingThinRule.get();
    }
    Optional<BuildRule> swiftCompanionBuildRule = swiftDelegate.createCompanionBuildRule(targetGraph, params, resolver, args);
    if (swiftCompanionBuildRule.isPresent()) {
        // otherwise, add this swift rule as a dependency.
        if (isSwiftTarget(params.getBuildTarget())) {
            return swiftCompanionBuildRule.get();
        } else {
            params = params.copyAppendingExtraDeps(ImmutableSet.of(swiftCompanionBuildRule.get()));
        }
    }
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    Optional<Path> stubBinaryPath = getStubBinaryPath(params, args);
    if (shouldUseStubBinary(params) && stubBinaryPath.isPresent()) {
        try {
            return resolver.addToIndex(new WriteFile(params, Files.readAllBytes(stubBinaryPath.get()), BuildTargets.getGenPath(params.getProjectFilesystem(), params.getBuildTarget(), "%s"), true));
        } catch (IOException e) {
            throw new HumanReadableException("Could not read stub binary " + stubBinaryPath.get());
        }
    } else {
        CxxBinaryDescription.Arg delegateArg = delegate.createUnpopulatedConstructorArg();
        AppleDescriptions.populateCxxBinaryDescriptionArg(pathResolver, delegateArg, args, params.getBuildTarget());
        return resolver.addToIndex(delegate.createBuildRule(targetGraph, params, resolver, delegateArg));
    }
}
Also used : Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) CxxBinaryDescription(com.facebook.buck.cxx.CxxBinaryDescription) WriteFile(com.facebook.buck.file.WriteFile) HumanReadableException(com.facebook.buck.util.HumanReadableException) BuildRule(com.facebook.buck.rules.BuildRule) IOException(java.io.IOException) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver)

Example 8 with WriteFile

use of com.facebook.buck.file.WriteFile in project buck by facebook.

the class HaskellDescriptionUtils method createLinkRule.

/**
   * Create a Haskell link rule that links the given inputs to a executable or shared library and
   * pulls in transitive native linkable deps from the given dep roots.
   */
public static HaskellLinkRule createLinkRule(BuildTarget target, BuildRuleParams baseParams, BuildRuleResolver resolver, SourcePathRuleFinder ruleFinder, CxxPlatform cxxPlatform, HaskellConfig haskellConfig, Linker.LinkType linkType, ImmutableList<String> extraFlags, Iterable<Arg> linkerInputs, Iterable<? extends NativeLinkable> deps, Linker.LinkableDepType depType) throws NoSuchBuildTargetException {
    Tool linker = haskellConfig.getLinker().resolve(resolver);
    String name = target.getShortName();
    ImmutableList.Builder<Arg> linkerArgsBuilder = ImmutableList.builder();
    ImmutableList.Builder<Arg> argsBuilder = ImmutableList.builder();
    // Add the base flags from the `.buckconfig` first.
    argsBuilder.addAll(StringArg.from(haskellConfig.getLinkerFlags()));
    // Pass in the appropriate flags to link a shared library.
    if (linkType.equals(Linker.LinkType.SHARED)) {
        name = CxxDescriptionEnhancer.getSharedLibrarySoname(Optional.empty(), target.withFlavors(), cxxPlatform);
        argsBuilder.addAll(StringArg.from("-shared", "-dynamic"));
        argsBuilder.addAll(StringArg.from(MoreIterables.zipAndConcat(Iterables.cycle("-optl"), cxxPlatform.getLd().resolve(resolver).soname(name))));
    }
    // Add in extra flags passed into this function.
    argsBuilder.addAll(StringArg.from(extraFlags));
    // We pass in the linker inputs and all native linkable deps by prefixing with `-optl` so that
    // the args go straight to the linker, and preserve their order.
    linkerArgsBuilder.addAll(linkerInputs);
    for (NativeLinkable nativeLinkable : NativeLinkables.getNativeLinkables(cxxPlatform, deps, depType).values()) {
        linkerArgsBuilder.addAll(NativeLinkables.getNativeLinkableInput(cxxPlatform, depType, nativeLinkable).getArgs());
    }
    // Since we use `-optl` to pass all linker inputs directly to the linker, the haskell linker
    // will complain about not having any input files.  So, create a dummy archive with an empty
    // module and pass that in normally to work around this.
    BuildTarget emptyModuleTarget = target.withAppendedFlavors(InternalFlavor.of("empty-module"));
    WriteFile emptyModule = resolver.addToIndex(new WriteFile(baseParams.withBuildTarget(emptyModuleTarget), "module Unused where", BuildTargets.getGenPath(baseParams.getProjectFilesystem(), emptyModuleTarget, "%s/Unused.hs"), /* executable */
    false));
    HaskellCompileRule emptyCompiledModule = resolver.addToIndex(createCompileRule(target.withAppendedFlavors(InternalFlavor.of("empty-compiled-module")), baseParams, resolver, ruleFinder, // Buck dependency.
    baseParams.getDeps(), cxxPlatform, haskellConfig, depType, Optional.empty(), Optional.empty(), ImmutableList.of(), HaskellSources.builder().putModuleMap("Unused", emptyModule.getSourcePathToOutput()).build()));
    BuildTarget emptyArchiveTarget = target.withAppendedFlavors(InternalFlavor.of("empty-archive"));
    Archive emptyArchive = resolver.addToIndex(Archive.from(emptyArchiveTarget, baseParams, ruleFinder, cxxPlatform, Archive.Contents.NORMAL, BuildTargets.getGenPath(baseParams.getProjectFilesystem(), emptyArchiveTarget, "%s/libempty.a"), emptyCompiledModule.getObjects()));
    argsBuilder.add(SourcePathArg.of(emptyArchive.getSourcePathToOutput()));
    ImmutableList<Arg> args = argsBuilder.build();
    ImmutableList<Arg> linkerArgs = linkerArgsBuilder.build();
    return resolver.addToIndex(new HaskellLinkRule(baseParams.withBuildTarget(target).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(linker.getDeps(ruleFinder)).addAll(Stream.of(args, linkerArgs).flatMap(Collection::stream).flatMap(arg -> arg.getDeps(ruleFinder).stream()).iterator()).build()), Suppliers.ofInstance(ImmutableSortedSet.of())), linker, name, args, linkerArgs, haskellConfig.shouldCacheLinks()));
}
Also used : NativeLinkables(com.facebook.buck.cxx.NativeLinkables) Iterables(com.google.common.collect.Iterables) Linker(com.facebook.buck.cxx.Linker) SourcePathArg(com.facebook.buck.rules.args.SourcePathArg) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) Archive(com.facebook.buck.cxx.Archive) SourcePath(com.facebook.buck.rules.SourcePath) InternalFlavor(com.facebook.buck.model.InternalFlavor) ExplicitCxxToolFlags(com.facebook.buck.cxx.ExplicitCxxToolFlags) BuildRule(com.facebook.buck.rules.BuildRule) CxxSourceTypes(com.facebook.buck.cxx.CxxSourceTypes) AbstractBreadthFirstThrowingTraversal(com.facebook.buck.graph.AbstractBreadthFirstThrowingTraversal) Tool(com.facebook.buck.rules.Tool) ImmutableList(com.google.common.collect.ImmutableList) CxxPlatforms(com.facebook.buck.cxx.CxxPlatforms) CxxPreprocessables(com.facebook.buck.cxx.CxxPreprocessables) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) StringArg(com.facebook.buck.rules.args.StringArg) Map(java.util.Map) CxxToolFlags(com.facebook.buck.cxx.CxxToolFlags) WriteFile(com.facebook.buck.file.WriteFile) NativeLinkable(com.facebook.buck.cxx.NativeLinkable) Suppliers(com.google.common.base.Suppliers) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) CxxDescriptionEnhancer(com.facebook.buck.cxx.CxxDescriptionEnhancer) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) CxxSourceRuleFactory(com.facebook.buck.cxx.CxxSourceRuleFactory) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) CxxSource(com.facebook.buck.cxx.CxxSource) BuildTarget(com.facebook.buck.model.BuildTarget) Arg(com.facebook.buck.rules.args.Arg) Stream(java.util.stream.Stream) TreeMap(java.util.TreeMap) CxxPreprocessorInput(com.facebook.buck.cxx.CxxPreprocessorInput) PreprocessorFlags(com.facebook.buck.cxx.PreprocessorFlags) Optional(java.util.Optional) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildTargets(com.facebook.buck.model.BuildTargets) MoreIterables(com.facebook.buck.util.MoreIterables) WriteFile(com.facebook.buck.file.WriteFile) Archive(com.facebook.buck.cxx.Archive) ImmutableList(com.google.common.collect.ImmutableList) NativeLinkable(com.facebook.buck.cxx.NativeLinkable) BuildTarget(com.facebook.buck.model.BuildTarget) SourcePathArg(com.facebook.buck.rules.args.SourcePathArg) StringArg(com.facebook.buck.rules.args.StringArg) Arg(com.facebook.buck.rules.args.Arg) Collection(java.util.Collection) Tool(com.facebook.buck.rules.Tool)

Example 9 with WriteFile

use of com.facebook.buck.file.WriteFile in project buck by facebook.

the class PythonTestDescription method createTestModulesSourceBuildRule.

/**
   * Return a {@link BuildRule} that constructs the source file which contains the list
   * of test modules this python test rule will run.  Setting up a separate build rule
   * for this allows us to use the existing python binary rule without changes to account
   * for the build-time creation of this file.
   */
private static BuildRule createTestModulesSourceBuildRule(BuildRuleParams params, Path outputPath, ImmutableSet<String> testModules) {
    // Modify the build rule params to change the target, type, and remove all deps.
    BuildRuleParams newParams = params.withBuildTarget(BuildTargets.createFlavoredBuildTarget(params.getBuildTarget().checkUnflavored(), InternalFlavor.of("test_module"))).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.of()));
    String contents = getTestModulesListContents(testModules);
    return new WriteFile(newParams, contents, outputPath, /* executable */
    false);
}
Also used : WriteFile(com.facebook.buck.file.WriteFile) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams)

Aggregations

WriteFile (com.facebook.buck.file.WriteFile)9 SourcePath (com.facebook.buck.rules.SourcePath)6 BuildTarget (com.facebook.buck.model.BuildTarget)5 BuildRule (com.facebook.buck.rules.BuildRule)5 Path (java.nio.file.Path)4 Tool (com.facebook.buck.rules.Tool)3 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)2 ExplicitBuildTargetSourcePath (com.facebook.buck.rules.ExplicitBuildTargetSourcePath)2 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)2 MkdirStep (com.facebook.buck.step.fs.MkdirStep)2 Archive (com.facebook.buck.cxx.Archive)1 CxxBinaryDescription (com.facebook.buck.cxx.CxxBinaryDescription)1 CxxDescriptionEnhancer (com.facebook.buck.cxx.CxxDescriptionEnhancer)1 CxxPlatform (com.facebook.buck.cxx.CxxPlatform)1 CxxPlatforms (com.facebook.buck.cxx.CxxPlatforms)1 CxxPreprocessables (com.facebook.buck.cxx.CxxPreprocessables)1 CxxPreprocessorInput (com.facebook.buck.cxx.CxxPreprocessorInput)1 CxxSource (com.facebook.buck.cxx.CxxSource)1 CxxSourceRuleFactory (com.facebook.buck.cxx.CxxSourceRuleFactory)1 CxxSourceTypes (com.facebook.buck.cxx.CxxSourceTypes)1