Search in sources :

Example 11 with PathSourcePath

use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.

the class AppleDescriptions method createBuildRulesForCoreDataDependencies.

public static Optional<CoreDataModel> createBuildRulesForCoreDataDependencies(TargetGraph targetGraph, BuildRuleParams params, String moduleName, AppleCxxPlatform appleCxxPlatform) {
    TargetNode<?, ?> targetNode = targetGraph.get(params.getBuildTarget());
    ImmutableSet<AppleWrapperResourceArg> coreDataModelArgs = AppleBuildRules.collectTransitiveBuildRules(targetGraph, Optional.empty(), AppleBuildRules.CORE_DATA_MODEL_DESCRIPTION_CLASSES, ImmutableList.of(targetNode));
    BuildRuleParams coreDataModelParams = params.withAppendedFlavor(CoreDataModel.FLAVOR).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.of()));
    if (coreDataModelArgs.isEmpty()) {
        return Optional.empty();
    } else {
        return Optional.of(new CoreDataModel(coreDataModelParams, appleCxxPlatform, moduleName, coreDataModelArgs.stream().map(input -> new PathSourcePath(params.getProjectFilesystem(), input.path)).collect(MoreCollectors.toImmutableSet())));
    }
}
Also used : OptionalCompat(com.facebook.buck.util.OptionalCompat) ProvidesLinkedBinaryDeps(com.facebook.buck.cxx.ProvidesLinkedBinaryDeps) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) AbstractGenruleDescription(com.facebook.buck.shell.AbstractGenruleDescription) RichStream(com.facebook.buck.util.RichStream) InternalFlavor(com.facebook.buck.model.InternalFlavor) FlavorDomain(com.facebook.buck.model.FlavorDomain) CxxCompilationDatabase(com.facebook.buck.cxx.CxxCompilationDatabase) FluentIterable(com.google.common.collect.FluentIterable) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourceList(com.facebook.buck.rules.coercer.SourceList) StripStyle(com.facebook.buck.cxx.StripStyle) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) CxxDescriptionEnhancer(com.facebook.buck.cxx.CxxDescriptionEnhancer) BuildRules(com.facebook.buck.rules.BuildRules) FrameworkDependencies(com.facebook.buck.cxx.FrameworkDependencies) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TargetGraph(com.facebook.buck.rules.TargetGraph) Set(java.util.Set) BuildTarget(com.facebook.buck.model.BuildTarget) Sets(com.google.common.collect.Sets) LinkerMapMode(com.facebook.buck.cxx.LinkerMapMode) SourceWithFlags(com.facebook.buck.rules.SourceWithFlags) Predicate(com.google.common.base.Predicate) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Optional(java.util.Optional) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) CxxBinaryDescription(com.facebook.buck.cxx.CxxBinaryDescription) Joiner(com.google.common.base.Joiner) CxxStrip(com.facebook.buck.cxx.CxxStrip) CxxLibraryDescription(com.facebook.buck.cxx.CxxLibraryDescription) SourcePath(com.facebook.buck.rules.SourcePath) Either(com.facebook.buck.model.Either) BuildRule(com.facebook.buck.rules.BuildRule) HashSet(java.util.HashSet) Tool(com.facebook.buck.rules.Tool) ImmutableList(com.google.common.collect.ImmutableList) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) Predicates(com.google.common.base.Predicates) Suppliers(com.google.common.base.Suppliers) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) CxxConstructorArg(com.facebook.buck.cxx.CxxConstructorArg) TargetNode(com.facebook.buck.rules.TargetNode) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) HumanReadableException(com.facebook.buck.util.HumanReadableException) MorePaths(com.facebook.buck.io.MorePaths) Ordering(com.google.common.collect.Ordering) BuildRuleWithBinary(com.facebook.buck.cxx.BuildRuleWithBinary) Paths(java.nio.file.Paths) SWIFT_EXTENSION(com.facebook.buck.swift.SwiftDescriptions.SWIFT_EXTENSION) Preconditions(com.google.common.base.Preconditions) Flavor(com.facebook.buck.model.Flavor) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) PathSourcePath(com.facebook.buck.rules.PathSourcePath)

Example 12 with PathSourcePath

use of com.facebook.buck.rules.PathSourcePath 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 13 with PathSourcePath

use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.

the class FineGrainedJavaDependencySuggester method extractProvidedSymbolInfoFromSourceFile.

/**
   * Extracts the features from {@code src} and updates the collections accordingly.
   */
private void extractProvidedSymbolInfoFromSourceFile(SourcePath src, JavaFileParser javaFileParser, Multimap<String, String> providedSymbolToRequiredSymbols, Map<String, PathSourcePath> providedSymbolToSrc) {
    if (!(src instanceof PathSourcePath)) {
        return;
    }
    PathSourcePath path = (PathSourcePath) src;
    ProjectFilesystem filesystem = path.getFilesystem();
    Optional<String> contents = filesystem.readFileIfItExists(path.getRelativePath());
    if (!contents.isPresent()) {
        throw new RuntimeException(String.format("Could not read file '%s'", path.getRelativePath()));
    }
    JavaFileParser.JavaFileFeatures features = javaFileParser.extractFeaturesFromJavaCode(contents.get());
    // If there are multiple provided symbols, that is because there are inner classes. Choosing
    // the shortest name will effectively select the top-level type.
    String providedSymbol = Iterables.getFirst(features.providedSymbols, /* defaultValue */
    null);
    if (providedSymbol == null) {
        console.getStdErr().printf("%s cowardly refuses to provide any types.\n", path.getRelativePath());
        return;
    }
    providedSymbolToSrc.put(providedSymbol, path);
    providedSymbolToRequiredSymbols.putAll(providedSymbol, features.requiredSymbols);
    providedSymbolToRequiredSymbols.putAll(providedSymbol, features.exportedSymbols);
}
Also used : PathSourcePath(com.facebook.buck.rules.PathSourcePath) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) JavaFileParser(com.facebook.buck.jvm.java.JavaFileParser)

Example 14 with PathSourcePath

use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.

the class FineGrainedJavaDependencySuggester method createBuildRuleDefinition.

/**
   * Creates the build rule definition for the {@code namedComponent}.
   */
private String createBuildRuleDefinition(NamedStronglyConnectedComponent namedComponent, Map<String, PathSourcePath> providedSymbolToSrc, Multimap<String, String> providedSymbolToRequiredSymbols, Map<String, NamedStronglyConnectedComponent> namedComponentsIndex, JavaDepsFinder.DependencyInfo dependencyInfo, MutableDirectedGraph<String> symbolsDependencies, String visibilityArg) {
    final TargetNode<?, ?> suggestedNode = graph.get(suggestedTarget);
    SortedSet<String> deps = new TreeSet<>(LOCAL_DEPS_FIRST_COMPARATOR);
    SortedSet<PathSourcePath> srcs = new TreeSet<>();
    for (String providedSymbol : namedComponent.symbols) {
        PathSourcePath src = providedSymbolToSrc.get(providedSymbol);
        srcs.add(src);
        for (String requiredSymbol : providedSymbolToRequiredSymbols.get(providedSymbol)) {
            // First, check to see whether the requiredSymbol is in one of the newly created
            // strongly connected components. If so, add it to the deps so long as it is not the
            // strongly connected component that we are currently exploring.
            NamedStronglyConnectedComponent requiredComponent = namedComponentsIndex.get(requiredSymbol);
            if (requiredComponent != null) {
                if (!requiredComponent.equals(namedComponent)) {
                    deps.add(":" + requiredComponent.name);
                }
                continue;
            }
            Set<TargetNode<?, ?>> depProviders = dependencyInfo.symbolToProviders.get(requiredSymbol);
            if (depProviders == null || depProviders.size() == 0) {
                console.getStdErr().printf("# Suspicious: no provider for '%s'\n", requiredSymbol);
                continue;
            }
            depProviders = FluentIterable.from(depProviders).filter(provider -> provider.isVisibleTo(graph, suggestedNode)).toSet();
            TargetNode<?, ?> depProvider;
            if (depProviders.size() == 1) {
                depProvider = Iterables.getOnlyElement(depProviders);
            } else {
                console.getStdErr().printf("# Suspicious: no lone provider for '%s': [%s]\n", requiredSymbol, Joiner.on(", ").join(depProviders));
                continue;
            }
            if (!depProvider.equals(suggestedNode)) {
                deps.add(depProvider.toString());
            }
        }
        // Find deps within package.
        for (String requiredSymbol : symbolsDependencies.getOutgoingNodesFor(providedSymbol)) {
            NamedStronglyConnectedComponent componentDep = Preconditions.checkNotNull(namedComponentsIndex.get(requiredSymbol));
            if (!componentDep.equals(namedComponent)) {
                deps.add(":" + componentDep.name);
            }
        }
    }
    final Path basePathForSuggestedTarget = suggestedTarget.getBasePath();
    Iterable<String> relativeSrcs = FluentIterable.from(srcs).transform(input -> basePathForSuggestedTarget.relativize(input.getRelativePath()).toString());
    StringBuilder rule = new StringBuilder("\njava_library(\n" + "  name = '" + namedComponent.name + "',\n" + "  srcs = [\n");
    for (String src : relativeSrcs) {
        rule.append(String.format("    '%s',\n", src));
    }
    rule.append("  ],\n" + "  deps = [\n");
    for (String dep : deps) {
        rule.append(String.format("    '%s',\n", dep));
    }
    rule.append("  ],\n" + visibilityArg + ")\n");
    return rule.toString();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) TargetNode(com.facebook.buck.rules.TargetNode) TreeSet(java.util.TreeSet) PathSourcePath(com.facebook.buck.rules.PathSourcePath)

Example 15 with PathSourcePath

use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.

the class FineGrainedJavaDependencySuggester method suggestRefactoring.

/**
   * Suggests a refactoring by printing it to stdout (with warnings printed to stderr).
   * @throws IllegalArgumentException
   */
void suggestRefactoring() {
    final TargetNode<?, ?> suggestedNode = graph.get(suggestedTarget);
    if (!(suggestedNode.getConstructorArg() instanceof JavaLibraryDescription.Arg)) {
        console.printErrorText(String.format("'%s' does not correspond to a Java rule", suggestedTarget));
        throw new IllegalArgumentException();
    }
    JavaLibraryDescription.Arg arg = (JavaLibraryDescription.Arg) suggestedNode.getConstructorArg();
    JavaFileParser javaFileParser = javaDepsFinder.getJavaFileParser();
    Multimap<String, String> providedSymbolToRequiredSymbols = HashMultimap.create();
    Map<String, PathSourcePath> providedSymbolToSrc = new HashMap<>();
    for (SourcePath src : arg.srcs) {
        extractProvidedSymbolInfoFromSourceFile(src, javaFileParser, providedSymbolToRequiredSymbols, providedSymbolToSrc);
    }
    // Create a MutableDirectedGraph from the providedSymbolToRequiredSymbols.
    MutableDirectedGraph<String> symbolsDependencies = new MutableDirectedGraph<>();
    // dependencies.
    for (String providedSymbol : providedSymbolToSrc.keySet()) {
        // Add a node for the providedSymbol in case it has no edges.
        symbolsDependencies.addNode(providedSymbol);
        for (String requiredSymbol : providedSymbolToRequiredSymbols.get(providedSymbol)) {
            if (providedSymbolToRequiredSymbols.containsKey(requiredSymbol) && !providedSymbol.equals(requiredSymbol)) {
                symbolsDependencies.addEdge(providedSymbol, requiredSymbol);
            }
        }
    }
    // Determine the strongly connected components.
    Set<Set<String>> stronglyConnectedComponents = symbolsDependencies.findStronglyConnectedComponents();
    // Maps a providedSymbol to the component that contains it.
    Map<String, NamedStronglyConnectedComponent> namedComponentsIndex = new TreeMap<>();
    Set<NamedStronglyConnectedComponent> namedComponents = new TreeSet<>();
    for (Set<String> stronglyConnectedComponent : stronglyConnectedComponents) {
        // We just use the first provided symbol in the strongly connected component as the canonical
        // name for the component. Maybe not the best name, but certainly not the worst.
        String name = Iterables.getFirst(stronglyConnectedComponent, /* defaultValue */
        null);
        if (name == null) {
            throw new IllegalStateException("A strongly connected component was created with zero nodes.");
        }
        NamedStronglyConnectedComponent namedComponent = new NamedStronglyConnectedComponent(name, stronglyConnectedComponent);
        namedComponents.add(namedComponent);
        for (String providedSymbol : stronglyConnectedComponent) {
            namedComponentsIndex.put(providedSymbol, namedComponent);
        }
    }
    // Visibility argument.
    StringBuilder visibilityBuilder = new StringBuilder("  visibility = [\n");
    SortedSet<String> visibilities = FluentIterable.from(suggestedNode.getVisibilityPatterns()).transform(VisibilityPattern::getRepresentation).toSortedSet(Ordering.natural());
    for (String visibility : visibilities) {
        visibilityBuilder.append("    '" + visibility + "',\n");
    }
    visibilityBuilder.append("  ],\n");
    String visibilityArg = visibilityBuilder.toString();
    // Print out the new version of the original rule.
    console.getStdOut().printf("java_library(\n" + "  name = '%s',\n" + "  exported_deps = [\n", suggestedTarget.getShortName());
    for (NamedStronglyConnectedComponent namedComponent : namedComponents) {
        console.getStdOut().printf("    ':%s',\n", namedComponent.name);
    }
    console.getStdOut().print("  ],\n" + visibilityArg + ")\n");
    // Print out a rule for each of the strongly connected components.
    JavaDepsFinder.DependencyInfo dependencyInfo = javaDepsFinder.findDependencyInfoForGraph(graph);
    for (NamedStronglyConnectedComponent namedComponent : namedComponents) {
        String buildRuleDefinition = createBuildRuleDefinition(namedComponent, providedSymbolToSrc, providedSymbolToRequiredSymbols, namedComponentsIndex, dependencyInfo, symbolsDependencies, visibilityArg);
        console.getStdOut().print(buildRuleDefinition);
    }
}
Also used : JavaDepsFinder(com.facebook.buck.jvm.java.autodeps.JavaDepsFinder) SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) Set(java.util.Set) HashMap(java.util.HashMap) JavaFileParser(com.facebook.buck.jvm.java.JavaFileParser) SourcePath(com.facebook.buck.rules.SourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) TreeSet(java.util.TreeSet) JavaLibraryDescription(com.facebook.buck.jvm.java.JavaLibraryDescription) PathSourcePath(com.facebook.buck.rules.PathSourcePath) TreeMap(java.util.TreeMap) MutableDirectedGraph(com.facebook.buck.graph.MutableDirectedGraph)

Aggregations

PathSourcePath (com.facebook.buck.rules.PathSourcePath)114 Test (org.junit.Test)82 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)69 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)68 SourcePath (com.facebook.buck.rules.SourcePath)65 Path (java.nio.file.Path)63 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)60 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)58 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)56 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)52 BuildTarget (com.facebook.buck.model.BuildTarget)51 TargetGraph (com.facebook.buck.rules.TargetGraph)37 BuildRule (com.facebook.buck.rules.BuildRule)26 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)23 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)21 StackedFileHashCache (com.facebook.buck.util.cache.StackedFileHashCache)16 RuleKey (com.facebook.buck.rules.RuleKey)15 DefaultFileHashCache (com.facebook.buck.util.cache.DefaultFileHashCache)15 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)14 ImmutableList (com.google.common.collect.ImmutableList)14