Search in sources :

Example 11 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class OcamlBuildRulesGenerator method generateBytecodeLinking.

/**
   * Links the .cmo files generated by the bytecode compilation
   */
private BuildRule generateBytecodeLinking(ImmutableList<SourcePath> allInputs) {
    BuildRuleParams linkParams = params.withBuildTarget(addBytecodeFlavor(params.getBuildTarget())).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(ruleFinder.filterBuildRuleInputs(allInputs)).addAll(ocamlContext.getBytecodeLinkDeps()).addAll(Stream.concat(ocamlContext.getBytecodeLinkableInput().getArgs().stream(), ocamlContext.getCLinkableInput().getArgs().stream()).flatMap(arg -> arg.getDeps(ruleFinder).stream()).filter(rule -> !(rule instanceof OcamlBuild)).iterator()).addAll(cxxCompiler.getDeps(ruleFinder)).build()), Suppliers.ofInstance(ImmutableSortedSet.of()));
    ImmutableList.Builder<Arg> flags = ImmutableList.builder();
    flags.addAll(ocamlContext.getFlags());
    flags.addAll(StringArg.from(ocamlContext.getCommonCLinkerFlags()));
    OcamlLink link = new OcamlLink(linkParams, allInputs, cxxCompiler.getEnvironment(pathResolver), cxxCompiler.getCommandPrefix(pathResolver), ocamlContext.getOcamlBytecodeCompiler().get(), flags.build(), ocamlContext.getOcamlInteropIncludesDir(), ocamlContext.getBytecodeOutput(), ocamlContext.getNativePluginOutput(), ocamlContext.getBytecodeLinkableInput().getArgs(), ocamlContext.getCLinkableInput().getArgs(), ocamlContext.isLibrary(), /* isBytecode */
    true, /* buildNativePlugin */
    false);
    resolver.addToIndex(link);
    return link;
}
Also used : Iterables(com.google.common.collect.Iterables) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePath(com.facebook.buck.rules.SourcePath) InternalFlavor(com.facebook.buck.model.InternalFlavor) BuildRule(com.facebook.buck.rules.BuildRule) Compiler(com.facebook.buck.cxx.Compiler) ImmutableList(com.google.common.collect.ImmutableList) Files(com.google.common.io.Files) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) StringArg(com.facebook.buck.rules.args.StringArg) Map(java.util.Map) Suppliers(com.google.common.base.Suppliers) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ImmutableMap(com.google.common.collect.ImmutableMap) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) HumanReadableException(com.facebook.buck.util.HumanReadableException) BuildTarget(com.facebook.buck.model.BuildTarget) Maps(com.google.common.collect.Maps) Arg(com.facebook.buck.rules.args.Arg) Stream(java.util.stream.Stream) CxxPreprocessorInput(com.facebook.buck.cxx.CxxPreprocessorInput) Preconditions(com.google.common.base.Preconditions) Flavor(com.facebook.buck.model.Flavor) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Joiner(com.google.common.base.Joiner) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) ImmutableList(com.google.common.collect.ImmutableList) StringArg(com.facebook.buck.rules.args.StringArg) Arg(com.facebook.buck.rules.args.Arg)

Example 12 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class AppleDescriptions method createBuildRuleForTransitiveAssetCatalogDependencies.

public static Optional<AppleAssetCatalog> createBuildRuleForTransitiveAssetCatalogDependencies(TargetGraph targetGraph, BuildRuleParams params, SourcePathResolver sourcePathResolver, ApplePlatform applePlatform, Tool actool) {
    TargetNode<?, ?> targetNode = targetGraph.get(params.getBuildTarget());
    ImmutableSet<AppleAssetCatalogDescription.Arg> assetCatalogArgs = AppleBuildRules.collectRecursiveAssetCatalogs(targetGraph, Optional.empty(), ImmutableList.of(targetNode));
    ImmutableSortedSet.Builder<SourcePath> assetCatalogDirsBuilder = ImmutableSortedSet.naturalOrder();
    Optional<String> appIcon = Optional.empty();
    Optional<String> launchImage = Optional.empty();
    AppleAssetCatalogDescription.Optimization optimization = null;
    for (AppleAssetCatalogDescription.Arg arg : assetCatalogArgs) {
        if (optimization == null) {
            optimization = arg.optimization;
        }
        assetCatalogDirsBuilder.addAll(arg.dirs);
        if (arg.appIcon.isPresent()) {
            if (appIcon.isPresent()) {
                throw new HumanReadableException("At most one asset catalog in the dependencies of %s " + "can have a app_icon", params.getBuildTarget());
            }
            appIcon = arg.appIcon;
        }
        if (arg.launchImage.isPresent()) {
            if (launchImage.isPresent()) {
                throw new HumanReadableException("At most one asset catalog in the dependencies of %s " + "can have a launch_image", params.getBuildTarget());
            }
            launchImage = arg.launchImage;
        }
        if (arg.optimization != optimization) {
            throw new HumanReadableException("At most one asset catalog optimisation style can be " + "specified in the dependencies %s", params.getBuildTarget());
        }
    }
    ImmutableSortedSet<SourcePath> assetCatalogDirs = assetCatalogDirsBuilder.build();
    if (assetCatalogDirs.isEmpty()) {
        return Optional.empty();
    }
    Preconditions.checkNotNull(optimization, "optimization was null even though assetCatalogArgs was not empty");
    for (SourcePath assetCatalogDir : assetCatalogDirs) {
        Path baseName = sourcePathResolver.getRelativePath(assetCatalogDir).getFileName();
        if (!baseName.toString().endsWith(".xcassets")) {
            throw new HumanReadableException("Target %s had asset catalog dir %s - asset catalog dirs must end with .xcassets", params.getBuildTarget(), assetCatalogDir);
        }
    }
    BuildRuleParams assetCatalogParams = params.withAppendedFlavor(AppleAssetCatalog.FLAVOR).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.of()));
    return Optional.of(new AppleAssetCatalog(assetCatalogParams, applePlatform.getName(), actool, assetCatalogDirs, appIcon, launchImage, optimization, MERGED_ASSET_CATALOG_NAME));
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) HumanReadableException(com.facebook.buck.util.HumanReadableException) CxxConstructorArg(com.facebook.buck.cxx.CxxConstructorArg) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet)

Example 13 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class NdkLibraryDescription method findSources.

@VisibleForTesting
protected ImmutableSortedSet<SourcePath> findSources(final ProjectFilesystem filesystem, final Path buildRulePath) {
    final ImmutableSortedSet.Builder<SourcePath> srcs = ImmutableSortedSet.naturalOrder();
    try {
        final Path rootDirectory = filesystem.resolve(buildRulePath);
        Files.walkFileTree(rootDirectory, EnumSet.of(FileVisitOption.FOLLOW_LINKS), /* maxDepth */
        Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (EXTENSIONS_REGEX.matcher(file.toString()).matches()) {
                    srcs.add(new PathSourcePath(filesystem, buildRulePath.resolve(rootDirectory.relativize(file))));
                }
                return super.visitFile(file, attrs);
            }
        });
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return srcs.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 14 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class UnzipAar method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    ImmutableList.Builder<Step> steps = ImmutableList.builder();
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), unpackDirectory));
    steps.add(new UnzipStep(getProjectFilesystem(), context.getSourcePathResolver().getAbsolutePath(aarFile), unpackDirectory));
    steps.add(new TouchStep(getProjectFilesystem(), getProguardConfig()));
    steps.add(new MkdirStep(getProjectFilesystem(), context.getSourcePathResolver().getAbsolutePath(getAssetsDirectory())));
    steps.add(new MkdirStep(getProjectFilesystem(), getNativeLibsDirectory()));
    steps.add(new TouchStep(getProjectFilesystem(), getTextSymbolsFile()));
    // We take the classes.jar file that is required to exist in an .aar and merge it with any
    // .jar files under libs/ into an "uber" jar. We do this for simplicity because we do not know
    // how many entries there are in libs/ at graph enhancement time, but we need to make sure
    // that all of the .class files in the .aar get packaged. As it is implemented today, an
    // android_library that depends on an android_prebuilt_aar can compile against anything in the
    // .aar's classes.jar or libs/.
    steps.add(new MkdirStep(getProjectFilesystem(), uberClassesJar.getParent()));
    steps.add(new AbstractExecutionStep("create_uber_classes_jar") {

        @Override
        public StepExecutionResult execute(ExecutionContext context) {
            Path libsDirectory = unpackDirectory.resolve("libs");
            boolean dirDoesNotExistOrIsEmpty;
            if (!getProjectFilesystem().exists(libsDirectory)) {
                dirDoesNotExistOrIsEmpty = true;
            } else {
                try {
                    dirDoesNotExistOrIsEmpty = getProjectFilesystem().getDirectoryContents(libsDirectory).isEmpty();
                } catch (IOException e) {
                    context.logError(e, "Failed to get directory contents of %s", libsDirectory);
                    return StepExecutionResult.ERROR;
                }
            }
            Path classesJar = unpackDirectory.resolve("classes.jar");
            JavacEventSinkToBuckEventBusBridge eventSink = new JavacEventSinkToBuckEventBusBridge(context.getBuckEventBus());
            if (!getProjectFilesystem().exists(classesJar)) {
                try {
                    JarDirectoryStepHelper.createEmptyJarFile(getProjectFilesystem(), classesJar, eventSink, context.getStdErr());
                } catch (IOException e) {
                    context.logError(e, "Failed to create empty jar %s", classesJar);
                    return StepExecutionResult.ERROR;
                }
            }
            if (dirDoesNotExistOrIsEmpty) {
                try {
                    getProjectFilesystem().copy(classesJar, uberClassesJar, ProjectFilesystem.CopySourceMode.FILE);
                } catch (IOException e) {
                    context.logError(e, "Failed to copy from %s to %s", classesJar, uberClassesJar);
                    return StepExecutionResult.ERROR;
                }
            } else {
                // Glob all of the contents from classes.jar and the entries in libs/ into a single JAR.
                ImmutableSortedSet.Builder<Path> entriesToJarBuilder = ImmutableSortedSet.naturalOrder();
                entriesToJarBuilder.add(classesJar);
                try {
                    entriesToJarBuilder.addAll(getProjectFilesystem().getDirectoryContents(libsDirectory));
                } catch (IOException e) {
                    context.logError(e, "Failed to get directory contents of %s", libsDirectory);
                    return StepExecutionResult.ERROR;
                }
                ImmutableSortedSet<Path> entriesToJar = entriesToJarBuilder.build();
                try {
                    JarDirectoryStepHelper.createJarFile(getProjectFilesystem(), uberClassesJar, entriesToJar, /* mainClass */
                    Optional.empty(), /* manifestFile */
                    Optional.empty(), /* mergeManifests */
                    true, /* blacklist */
                    ImmutableSet.of(), eventSink, context.getStdErr());
                } catch (IOException e) {
                    context.logError(e, "Failed to jar %s into %s", entriesToJar, uberClassesJar);
                    return StepExecutionResult.ERROR;
                }
            }
            return StepExecutionResult.SUCCESS;
        }
    });
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), pathToTextSymbolsDir));
    steps.add(new ExtractFromAndroidManifestStep(getAndroidManifest(), getProjectFilesystem(), buildableContext, METADATA_KEY_FOR_R_DOT_JAVA_PACKAGE, pathToRDotJavaPackageFile));
    steps.add(CopyStep.forFile(getProjectFilesystem(), getTextSymbolsFile(), pathToTextSymbolsFile));
    buildableContext.recordArtifact(unpackDirectory);
    buildableContext.recordArtifact(uberClassesJar);
    buildableContext.recordArtifact(pathToTextSymbolsFile);
    buildableContext.recordArtifact(pathToRDotJavaPackageFile);
    return steps.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) UnzipStep(com.facebook.buck.zip.UnzipStep) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) ImmutableList(com.google.common.collect.ImmutableList) MkdirStep(com.facebook.buck.step.fs.MkdirStep) Step(com.facebook.buck.step.Step) CopyStep(com.facebook.buck.step.fs.CopyStep) UnzipStep(com.facebook.buck.zip.UnzipStep) MkdirStep(com.facebook.buck.step.fs.MkdirStep) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) TouchStep(com.facebook.buck.step.fs.TouchStep) TouchStep(com.facebook.buck.step.fs.TouchStep) IOException(java.io.IOException) JavacEventSinkToBuckEventBusBridge(com.facebook.buck.jvm.java.JavacEventSinkToBuckEventBusBridge) ExecutionContext(com.facebook.buck.step.ExecutionContext) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep)

Example 15 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class DDescriptionUtils method requireBuildRule.

/**
   * Ensures that a DCompileBuildRule exists for the given target, creating a DCompileBuildRule
   * if neccesary.
   * @param baseParams build parameters for the rule
   * @param buildRuleResolver BuildRuleResolver the rule should be in
   * @param src the source file to be compiled
   * @param compilerFlags flags to pass to the compiler
   * @param compileTarget the target the rule should be for
   * @param dBuckConfig the Buck configuration for D
   * @return the build rule
   */
public static DCompileBuildRule requireBuildRule(BuildTarget compileTarget, BuildRuleParams baseParams, BuildRuleResolver buildRuleResolver, SourcePathRuleFinder ruleFinder, DBuckConfig dBuckConfig, ImmutableList<String> compilerFlags, String name, SourcePath src, DIncludes includes) throws NoSuchBuildTargetException {
    Optional<BuildRule> existingRule = buildRuleResolver.getRuleOptional(compileTarget);
    if (existingRule.isPresent()) {
        return (DCompileBuildRule) existingRule.get();
    } else {
        Tool compiler = dBuckConfig.getDCompiler();
        Map<BuildTarget, DIncludes> transitiveIncludes = new TreeMap<>();
        transitiveIncludes.put(baseParams.getBuildTarget(), includes);
        for (Map.Entry<BuildTarget, DLibrary> library : getTransitiveDLibraryRules(baseParams.getDeps()).entrySet()) {
            transitiveIncludes.put(library.getKey(), library.getValue().getIncludes());
        }
        ImmutableSortedSet.Builder<BuildRule> depsBuilder = ImmutableSortedSet.naturalOrder();
        depsBuilder.addAll(compiler.getDeps(ruleFinder));
        depsBuilder.addAll(ruleFinder.filterBuildRuleInputs(src));
        for (DIncludes dIncludes : transitiveIncludes.values()) {
            depsBuilder.addAll(dIncludes.getDeps(ruleFinder));
        }
        ImmutableSortedSet<BuildRule> deps = depsBuilder.build();
        return buildRuleResolver.addToIndex(new DCompileBuildRule(baseParams.withBuildTarget(compileTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(deps), Suppliers.ofInstance(ImmutableSortedSet.of())), compiler, ImmutableList.<String>builder().addAll(dBuckConfig.getBaseCompilerFlags()).addAll(compilerFlags).build(), name, ImmutableSortedSet.of(src), ImmutableList.copyOf(transitiveIncludes.values())));
    }
}
Also used : TreeMap(java.util.TreeMap) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BuildRule(com.facebook.buck.rules.BuildRule) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) TreeMap(java.util.TreeMap) Tool(com.facebook.buck.rules.Tool)

Aggregations

ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)51 BuildTarget (com.facebook.buck.model.BuildTarget)26 BuildRule (com.facebook.buck.rules.BuildRule)26 Path (java.nio.file.Path)25 SourcePath (com.facebook.buck.rules.SourcePath)22 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)21 ImmutableList (com.google.common.collect.ImmutableList)21 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)19 HumanReadableException (com.facebook.buck.util.HumanReadableException)17 Optional (java.util.Optional)17 ImmutableSet (com.google.common.collect.ImmutableSet)16 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)15 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)14 Map (java.util.Map)13 Flavor (com.facebook.buck.model.Flavor)12 ImmutableMap (com.google.common.collect.ImmutableMap)12 IOException (java.io.IOException)12 Preconditions (com.google.common.base.Preconditions)11 Suppliers (com.google.common.base.Suppliers)10 TargetGraph (com.facebook.buck.rules.TargetGraph)9