Search in sources :

Example 36 with ImmutableSet

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

the class IjProjectTemplateDataPreparer method createExcludes.

public ImmutableSet<IjFolder> createExcludes(final IjModule module) throws IOException {
    final ImmutableSet.Builder<IjFolder> excludesBuilder = ImmutableSet.builder();
    final Path moduleBasePath = module.getModuleBasePath();
    projectFilesystem.walkRelativeFileTree(moduleBasePath, new FileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            // When we create excludes for that module.
            if (filesystemTraversalBoundaryPaths.contains(dir) && !moduleBasePath.equals(dir)) {
                return FileVisitResult.SKIP_SUBTREE;
            }
            if (isRootAndroidResourceDirectory(module, dir)) {
                return FileVisitResult.SKIP_SUBTREE;
            }
            if (!referencedFolderPaths.contains(dir)) {
                excludesBuilder.add(new ExcludeFolder(dir));
                return FileVisitResult.SKIP_SUBTREE;
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            return FileVisitResult.CONTINUE;
        }
    });
    return excludesBuilder.build();
}
Also used : Path(java.nio.file.Path) ImmutableSet(com.google.common.collect.ImmutableSet) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 37 with ImmutableSet

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

the class PythonTestDescription method createBuildRule.

@Override
public <A extends Arg> PythonTest createBuildRule(TargetGraph targetGraph, final BuildRuleParams params, final BuildRuleResolver resolver, final A args) throws HumanReadableException, NoSuchBuildTargetException {
    PythonPlatform pythonPlatform = pythonPlatforms.getValue(params.getBuildTarget()).orElse(pythonPlatforms.getValue(args.platform.<Flavor>map(InternalFlavor::of).orElse(pythonPlatforms.getFlavors().iterator().next())));
    CxxPlatform cxxPlatform = cxxPlatforms.getValue(params.getBuildTarget()).orElse(defaultCxxPlatform);
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    Path baseModule = PythonUtil.getBasePath(params.getBuildTarget(), args.baseModule);
    Optional<ImmutableMap<BuildTarget, Version>> selectedVersions = targetGraph.get(params.getBuildTarget()).getSelectedVersions();
    ImmutableMap<Path, SourcePath> srcs = PythonUtil.getModules(params.getBuildTarget(), resolver, ruleFinder, pathResolver, pythonPlatform, cxxPlatform, "srcs", baseModule, args.srcs, args.platformSrcs, args.versionedSrcs, selectedVersions);
    ImmutableMap<Path, SourcePath> resources = PythonUtil.getModules(params.getBuildTarget(), resolver, ruleFinder, pathResolver, pythonPlatform, cxxPlatform, "resources", baseModule, args.resources, args.platformResources, args.versionedResources, selectedVersions);
    // Convert the passed in module paths into test module names.
    ImmutableSet.Builder<String> testModulesBuilder = ImmutableSet.builder();
    for (Path name : srcs.keySet()) {
        testModulesBuilder.add(PythonUtil.toModuleName(params.getBuildTarget(), name.toString()));
    }
    ImmutableSet<String> testModules = testModulesBuilder.build();
    // Construct a build rule to generate the test modules list source file and
    // add it to the build.
    BuildRule testModulesBuildRule = createTestModulesSourceBuildRule(params, getTestModulesListPath(params.getBuildTarget(), params.getProjectFilesystem()), testModules);
    resolver.addToIndex(testModulesBuildRule);
    String mainModule;
    if (args.mainModule.isPresent()) {
        mainModule = args.mainModule.get();
    } else {
        mainModule = PythonUtil.toModuleName(params.getBuildTarget(), getTestMainName().toString());
    }
    // Build up the list of everything going into the python test.
    PythonPackageComponents testComponents = PythonPackageComponents.of(ImmutableMap.<Path, SourcePath>builder().put(getTestModulesListName(), testModulesBuildRule.getSourcePathToOutput()).put(getTestMainName(), pythonBuckConfig.getPathToTestMain(params.getProjectFilesystem())).putAll(srcs).build(), resources, ImmutableMap.of(), ImmutableSet.of(), args.zipSafe);
    PythonPackageComponents allComponents = PythonUtil.getAllComponents(params, resolver, ruleFinder, testComponents, pythonPlatform, cxxBuckConfig, cxxPlatform, args.linkerFlags.stream().map(MacroArg.toMacroArgFunction(PythonUtil.MACRO_HANDLER, params.getBuildTarget(), params.getCellRoots(), resolver)::apply).collect(MoreCollectors.toImmutableList()), pythonBuckConfig.getNativeLinkStrategy(), args.preloadDeps);
    // Build the PEX using a python binary rule with the minimum dependencies.
    PythonBinary binary = binaryDescription.createPackageRule(params.withBuildTarget(getBinaryBuildTarget(params.getBuildTarget())), resolver, ruleFinder, pythonPlatform, cxxPlatform, mainModule, args.extension, allComponents, args.buildArgs, args.packageStyle.orElse(pythonBuckConfig.getPackageStyle()), PythonUtil.getPreloadNames(resolver, cxxPlatform, args.preloadDeps));
    resolver.addToIndex(binary);
    ImmutableList.Builder<Pair<Float, ImmutableSet<Path>>> neededCoverageBuilder = ImmutableList.builder();
    for (NeededCoverageSpec coverageSpec : args.neededCoverage) {
        BuildRule buildRule = resolver.getRule(coverageSpec.getBuildTarget());
        if (params.getDeps().contains(buildRule) && buildRule instanceof PythonLibrary) {
            PythonLibrary pythonLibrary = (PythonLibrary) buildRule;
            ImmutableSortedSet<Path> paths;
            if (coverageSpec.getPathName().isPresent()) {
                Path path = coverageSpec.getBuildTarget().getBasePath().resolve(coverageSpec.getPathName().get());
                if (!pythonLibrary.getPythonPackageComponents(pythonPlatform, cxxPlatform).getModules().keySet().contains(path)) {
                    throw new HumanReadableException("%s: path %s specified in needed_coverage not found in target %s", params.getBuildTarget(), path, buildRule.getBuildTarget());
                }
                paths = ImmutableSortedSet.of(path);
            } else {
                paths = ImmutableSortedSet.copyOf(pythonLibrary.getPythonPackageComponents(pythonPlatform, cxxPlatform).getModules().keySet());
            }
            neededCoverageBuilder.add(new Pair<Float, ImmutableSet<Path>>(coverageSpec.getNeededCoverageRatio(), paths));
        } else {
            throw new HumanReadableException("%s: needed_coverage requires a python library dependency. Found %s instead", params.getBuildTarget(), buildRule);
        }
    }
    Supplier<ImmutableMap<String, String>> testEnv = () -> ImmutableMap.copyOf(Maps.transformValues(args.env, MACRO_HANDLER.getExpander(params.getBuildTarget(), params.getCellRoots(), resolver)));
    // Generate and return the python test rule, which depends on the python binary rule above.
    return PythonTest.from(params, ruleFinder, testEnv, binary, args.labels, neededCoverageBuilder.build(), args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs), args.contacts);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) SourcePath(com.facebook.buck.rules.SourcePath) ImmutableSet(com.google.common.collect.ImmutableSet) BuildRule(com.facebook.buck.rules.BuildRule) Pair(com.facebook.buck.model.Pair) SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) Optional(java.util.Optional) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) ImmutableMap(com.google.common.collect.ImmutableMap) HumanReadableException(com.facebook.buck.util.HumanReadableException)

Example 38 with ImmutableSet

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

the class QueryTargetAccessor method filterAttributeContents.

/**
   * Filters the objects in the given attribute that satisfy the given predicate.
   */
public static <T> ImmutableSet<Object> filterAttributeContents(TargetNode<T, ?> node, String attribute, final Predicate<Object> predicate) {
    try {
        final ImmutableSet.Builder<Object> builder = ImmutableSet.builder();
        Class<?> constructorArgClass = node.getConstructorArg().getClass();
        Field field = constructorArgClass.getField(attribute);
        ParamInfo info = new ParamInfo(typeCoercerFactory, constructorArgClass, field);
        info.traverse(value -> {
            if (predicate.apply(value)) {
                builder.add(value);
            }
        }, node.getConstructorArg());
        return builder.build();
    } catch (NoSuchFieldException e) {
        // Ignore if the field does not exist in this rule.
        return ImmutableSet.of();
    }
}
Also used : Field(java.lang.reflect.Field) ImmutableSet(com.google.common.collect.ImmutableSet) ParamInfo(com.facebook.buck.rules.ParamInfo)

Example 39 with ImmutableSet

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

the class CellProvider method createForLocalBuild.

/**
   * Create a cell provider at a given root.
   */
public static CellProvider createForLocalBuild(ProjectFilesystem rootFilesystem, Watchman watchman, BuckConfig rootConfig, CellConfig rootCellConfigOverrides, KnownBuildRuleTypesFactory knownBuildRuleTypesFactory) throws IOException {
    DefaultCellPathResolver rootCellCellPathResolver = new DefaultCellPathResolver(rootFilesystem.getRootPath(), rootConfig.getConfig());
    ImmutableMap<RelativeCellName, Path> transitiveCellPathMapping = rootCellCellPathResolver.getTransitivePathMapping();
    ImmutableMap<Path, RawConfig> pathToConfigOverrides;
    try {
        pathToConfigOverrides = rootCellConfigOverrides.getOverridesByPath(transitiveCellPathMapping);
    } catch (CellConfig.MalformedOverridesException e) {
        throw new HumanReadableException(e.getMessage());
    }
    ImmutableSet<Path> allRoots = ImmutableSet.copyOf(transitiveCellPathMapping.values());
    return new CellProvider(cellProvider -> new CacheLoader<Path, Cell>() {

        @Override
        public Cell load(Path cellPath) throws IOException, InterruptedException {
            Path normalizedCellPath = cellPath.toRealPath().normalize();
            Preconditions.checkState(allRoots.contains(normalizedCellPath), "Cell %s outside of transitive closure of root cell (%s).", normalizedCellPath, allRoots);
            RawConfig configOverrides = Optional.ofNullable(pathToConfigOverrides.get(normalizedCellPath)).orElse(RawConfig.of(ImmutableMap.of()));
            Config config = Configs.createDefaultConfig(normalizedCellPath, configOverrides);
            DefaultCellPathResolver cellPathResolver = new DefaultCellPathResolver(normalizedCellPath, config);
            cellPathResolver.getCellPaths().forEach((name, path) -> {
                Path pathInRootResolver = rootCellCellPathResolver.getCellPaths().get(name);
                if (pathInRootResolver == null) {
                    throw new HumanReadableException("In the config of %s:  %s.%s must exist in the root cell's cell mappings.", cellPath.toString(), DefaultCellPathResolver.REPOSITORIES_SECTION, name);
                } else if (!pathInRootResolver.equals(path)) {
                    throw new HumanReadableException("In the config of %s:  %s.%s must point to the same directory as the root " + "cell's cell mapping: (root) %s != (current) %s", cellPath.toString(), DefaultCellPathResolver.REPOSITORIES_SECTION, name, pathInRootResolver, path);
                }
            });
            ProjectFilesystem cellFilesystem = new ProjectFilesystem(normalizedCellPath, config);
            BuckConfig buckConfig = new BuckConfig(config, cellFilesystem, rootConfig.getArchitecture(), rootConfig.getPlatform(), rootConfig.getEnvironment(), cellPathResolver);
            return new Cell(cellPathResolver.getKnownRoots(), cellFilesystem, watchman, buckConfig, knownBuildRuleTypesFactory, cellProvider);
        }
    }, cellProvider -> {
        try {
            return new Cell(rootCellCellPathResolver.getKnownRoots(), rootFilesystem, watchman, rootConfig, knownBuildRuleTypesFactory, cellProvider);
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted while loading root cell", e);
        } catch (IOException e) {
            throw new HumanReadableException("Failed to load root cell", e);
        }
    });
}
Also used : Path(java.nio.file.Path) LoadingCache(com.google.common.cache.LoadingCache) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Config(com.facebook.buck.config.Config) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) Configs(com.facebook.buck.config.Configs) HumanReadableException(com.facebook.buck.util.HumanReadableException) Watchman(com.facebook.buck.io.Watchman) Function(java.util.function.Function) RawConfig(com.facebook.buck.config.RawConfig) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) CacheLoader(com.google.common.cache.CacheLoader) ExecutionException(java.util.concurrent.ExecutionException) BuckConfig(com.facebook.buck.cli.BuckConfig) CellConfig(com.facebook.buck.config.CellConfig) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) CacheBuilder(com.google.common.cache.CacheBuilder) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) Config(com.facebook.buck.config.Config) RawConfig(com.facebook.buck.config.RawConfig) BuckConfig(com.facebook.buck.cli.BuckConfig) CellConfig(com.facebook.buck.config.CellConfig) IOException(java.io.IOException) RawConfig(com.facebook.buck.config.RawConfig) BuckConfig(com.facebook.buck.cli.BuckConfig) HumanReadableException(com.facebook.buck.util.HumanReadableException) CellConfig(com.facebook.buck.config.CellConfig) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem)

Example 40 with ImmutableSet

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

the class UnexpectedFlavorException method createWithSuggestions.

public static UnexpectedFlavorException createWithSuggestions(Cell cell, BuildTarget target) {
    // Get the specific message
    String exceptionMessage = createDefaultMessage(cell, target);
    // Get some suggestions on how to solve it.
    String suggestions = "";
    Optional<ImmutableSet<PatternAndMessage>> configMessagesForFlavors = cell.getBuckConfig().getUnexpectedFlavorsMessages();
    for (Flavor flavor : target.getFlavors()) {
        boolean foundInConfig = false;
        if (configMessagesForFlavors.isPresent()) {
            for (PatternAndMessage flavorPattern : configMessagesForFlavors.get()) {
                if (flavorPattern.getPattern().matcher(flavor.getName()).find()) {
                    foundInConfig = true;
                    suggestions += flavor.getName() + " : " + flavorPattern.getMessage() + "\n";
                }
            }
        }
        if (!foundInConfig) {
            for (PatternAndMessage flavorPattern : suggestedMessagesForFlavors) {
                if (flavorPattern.getPattern().matcher(flavor.getName()).find()) {
                    suggestions += flavor.getName() + " : " + flavorPattern.getMessage() + "\n";
                }
            }
        }
    }
    if (!suggestions.isEmpty()) {
        exceptionMessage += "\nHere are some things you can try to get the following " + "flavors to work::\n" + suggestions;
    }
    return new UnexpectedFlavorException(exceptionMessage);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Flavor(com.facebook.buck.model.Flavor) PatternAndMessage(com.facebook.buck.util.PatternAndMessage)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)196 Path (java.nio.file.Path)65 BuildTarget (com.facebook.buck.model.BuildTarget)58 ImmutableMap (com.google.common.collect.ImmutableMap)44 ImmutableList (com.google.common.collect.ImmutableList)43 IOException (java.io.IOException)42 Optional (java.util.Optional)37 Test (org.junit.Test)32 SourcePath (com.facebook.buck.rules.SourcePath)31 TargetNode (com.facebook.buck.rules.TargetNode)28 BuildRule (com.facebook.buck.rules.BuildRule)26 Map (java.util.Map)26 List (java.util.List)25 Set (java.util.Set)25 HumanReadableException (com.facebook.buck.util.HumanReadableException)23 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)22 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)21 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)20 TargetGraph (com.facebook.buck.rules.TargetGraph)19 VisibleForTesting (com.google.common.annotations.VisibleForTesting)19