Search in sources :

Example 31 with BuildRule

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

the class DefaultSuggestBuildRules method isMissingBuildRule.

/**
   *  @param transitiveNotDeclaredRule A {@link BuildRule} that is contained in the transitive
   *      dependency list but is not declared as a dependency.
   *  @param failedImports A Set of remaining failed imports.  This function will mutate this set
   *      and remove any imports satisfied by {@code transitiveNotDeclaredDep}.
   *  @return whether or not adding {@code transitiveNotDeclaredDep} as a dependency to this build
   *      rule would have satisfied one of the {@code failedImports}.
   */
private boolean isMissingBuildRule(BuildRule transitiveNotDeclaredRule, Set<String> failedImports, JarResolver jarResolver) {
    if (!(transitiveNotDeclaredRule instanceof JavaLibrary)) {
        return false;
    }
    ImmutableSet<Path> classPaths = ((JavaLibrary) transitiveNotDeclaredRule).getOutputClasspaths().stream().map(c -> pathResolver.getAbsolutePath(c)).collect(MoreCollectors.toImmutableSet());
    boolean containsMissingBuildRule = false;
    // classpath.
    for (Path classPath : classPaths) {
        ImmutableSet<String> topLevelSymbols = jarResolver.resolve(classPath);
        for (String symbolName : topLevelSymbols) {
            if (failedImports.contains(symbolName)) {
                failedImports.remove(symbolName);
                containsMissingBuildRule = true;
                // If we've found all of the missing imports, bail out early.
                if (failedImports.isEmpty()) {
                    return true;
                }
            }
        }
    }
    return containsMissingBuildRule;
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Supplier(com.google.common.base.Supplier) Set(java.util.Set) BuildRuleDependencyVisitors(com.facebook.buck.rules.BuildRuleDependencyVisitors) Sets(com.google.common.collect.Sets) BuildRule(com.facebook.buck.rules.BuildRule) DirectedAcyclicGraph(com.facebook.buck.graph.DirectedAcyclicGraph) SuggestBuildRules(com.facebook.buck.jvm.core.SuggestBuildRules) ImmutableList(com.google.common.collect.ImmutableList) FluentIterable(com.google.common.collect.FluentIterable) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) Suppliers(com.google.common.base.Suppliers) TopologicalSort(com.facebook.buck.graph.TopologicalSort) Path(java.nio.file.Path) MoreCollectors(com.facebook.buck.util.MoreCollectors) Path(java.nio.file.Path)

Example 32 with BuildRule

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

the class CopyResourcesStep method buildSteps.

@VisibleForTesting
ImmutableList<Step> buildSteps() {
    ImmutableList.Builder<Step> allSteps = ImmutableList.builder();
    if (resources.isEmpty()) {
        return allSteps.build();
    }
    String targetPackageDir = javaPackageFinder.findJavaPackage(target);
    for (SourcePath rawResource : resources) {
        // If the path to the file defining this rule were:
        // "first-party/orca/lib-http/tests/com/facebook/orca/BUCK"
        //
        // And the value of resource were:
        // "first-party/orca/lib-http/tests/com/facebook/orca/protocol/base/batch_exception1.txt"
        //
        // Assuming that `src_roots = tests` were in the [java] section of the .buckconfig file,
        // then javaPackageAsPath would be:
        // "com/facebook/orca/protocol/base/"
        //
        // And the path that we would want to copy to the classes directory would be:
        // "com/facebook/orca/protocol/base/batch_exception1.txt"
        //
        // Therefore, some path-wrangling is required to produce the correct string.
        Optional<BuildRule> underlyingRule = ruleFinder.getRule(rawResource);
        Path relativePathToResource = resolver.getRelativePath(rawResource);
        String resource;
        if (underlyingRule.isPresent()) {
            BuildTarget underlyingTarget = underlyingRule.get().getBuildTarget();
            if (underlyingRule.get() instanceof HasOutputName) {
                resource = MorePaths.pathWithUnixSeparators(underlyingTarget.getBasePath().resolve(((HasOutputName) underlyingRule.get()).getOutputName()));
            } else {
                Path genOutputParent = BuildTargets.getGenPath(filesystem, underlyingTarget, "%s").getParent();
                Path scratchOutputParent = BuildTargets.getScratchPath(filesystem, underlyingTarget, "%s").getParent();
                Optional<Path> outputPath = MorePaths.stripPrefix(relativePathToResource, genOutputParent).map(Optional::of).orElse(MorePaths.stripPrefix(relativePathToResource, scratchOutputParent));
                Preconditions.checkState(outputPath.isPresent(), "%s is used as a resource but does not output to a default output directory", underlyingTarget.getFullyQualifiedName());
                resource = MorePaths.pathWithUnixSeparators(underlyingTarget.getBasePath().resolve(outputPath.get()));
            }
        } else {
            resource = MorePaths.pathWithUnixSeparators(relativePathToResource);
        }
        Path javaPackageAsPath = javaPackageFinder.findJavaPackageFolder(outputDirectory.getFileSystem().getPath(resource));
        Path relativeSymlinkPath;
        if ("".equals(javaPackageAsPath.toString())) {
            // In this case, the project root is acting as the default package, so the resource path
            // works fine.
            relativeSymlinkPath = relativePathToResource.getFileName();
        } else {
            int lastIndex = resource.lastIndexOf(MorePaths.pathWithUnixSeparatorsAndTrailingSlash(javaPackageAsPath));
            if (lastIndex < 0) {
                Preconditions.checkState(rawResource instanceof BuildTargetSourcePath, "If resource path %s does not contain %s, then it must be a BuildTargetSourcePath.", relativePathToResource, javaPackageAsPath);
                // Handle the case where we depend on the output of another BuildRule. In that case, just
                // grab the output and put in the same package as this target would be in.
                relativeSymlinkPath = outputDirectory.getFileSystem().getPath(String.format("%s%s%s", targetPackageDir, targetPackageDir.isEmpty() ? "" : "/", resolver.getRelativePath(rawResource).getFileName()));
            } else {
                relativeSymlinkPath = outputDirectory.getFileSystem().getPath(resource.substring(lastIndex));
            }
        }
        Path target = outputDirectory.resolve(relativeSymlinkPath);
        MkdirAndSymlinkFileStep link = new MkdirAndSymlinkFileStep(filesystem, resolver.getAbsolutePath(rawResource), target);
        allSteps.add(link);
    }
    return allSteps.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) Path(java.nio.file.Path) HasOutputName(com.facebook.buck.model.HasOutputName) ImmutableList(com.google.common.collect.ImmutableList) Step(com.facebook.buck.step.Step) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 33 with BuildRule

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

the class AbstractJavacOptions method getInputs.

public ImmutableSortedSet<SourcePath> getInputs(SourcePathRuleFinder ruleFinder) {
    ImmutableSortedSet.Builder<SourcePath> builder = ImmutableSortedSet.<SourcePath>naturalOrder().addAll(getAnnotationProcessingParams().getInputs());
    Optional<SourcePath> javacJarPath = getJavacJarPath();
    if (javacJarPath.isPresent()) {
        SourcePath sourcePath = javacJarPath.get();
        // Add the original rule regardless of what happens next.
        builder.add(sourcePath);
        Optional<BuildRule> possibleRule = ruleFinder.getRule(sourcePath);
        if (possibleRule.isPresent()) {
            BuildRule rule = possibleRule.get();
            // And now include any transitive deps that contribute to the classpath.
            if (rule instanceof JavaLibrary) {
                builder.addAll(((JavaLibrary) rule).getDepsForTransitiveClasspathEntries().stream().map(BuildRule::getSourcePathToOutput).collect(MoreCollectors.toImmutableList()));
            } else {
                builder.add(sourcePath);
            }
        }
    }
    return builder.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BuildRule(com.facebook.buck.rules.BuildRule)

Example 34 with BuildRule

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

the class AbstractNativeExecutableStarter method getNativeStarterCxxSource.

private CxxSource getNativeStarterCxxSource() {
    BuildTarget target = BuildTarget.builder(getBaseParams().getBuildTarget()).addFlavors(InternalFlavor.of("native-starter-cxx-source")).build();
    BuildRule rule;
    Optional<BuildRule> maybeRule = getRuleResolver().getRuleOptional(target);
    if (maybeRule.isPresent()) {
        rule = maybeRule.get();
    } else {
        BuildTarget templateTarget = BuildTarget.builder(getBaseParams().getBuildTarget()).addFlavors(InternalFlavor.of("native-starter-cxx-source-template")).build();
        WriteFile templateRule = getRuleResolver().addToIndex(new WriteFile(getBaseParams().withBuildTarget(templateTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.of())), getNativeStarterCxxSourceTemplate(), BuildTargets.getGenPath(getBaseParams().getProjectFilesystem(), templateTarget, "%s/native-starter.cpp.in"), /* executable */
        false));
        Path output = BuildTargets.getGenPath(getBaseParams().getProjectFilesystem(), target, "%s/native-starter.cpp");
        rule = getRuleResolver().addToIndex(WriteStringTemplateRule.from(getBaseParams(), getRuleFinder(), target, output, templateRule.getSourcePathToOutput(), ImmutableMap.of("MAIN_MODULE", Escaper.escapeAsPythonString(getMainModule()), "MODULES_DIR", getRelativeModulesDir().isPresent() ? Escaper.escapeAsPythonString(getRelativeModulesDir().get().toString()) : "NULL", "PY_MODULES_DIR", getRelativePythonModulesDir().isPresent() ? Escaper.escapeAsPythonString(getRelativePythonModulesDir().get().toString()) : "NULL", "EXT_SUFFIX", Escaper.escapeAsPythonString(getCxxPlatform().getSharedLibraryExtension())), /* executable */
        false));
    }
    return CxxSource.of(CxxSource.Type.CXX, Preconditions.checkNotNull(rule.getSourcePathToOutput()), ImmutableList.of());
}
Also used : Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) WriteFile(com.facebook.buck.file.WriteFile) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule)

Example 35 with BuildRule

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

the class LuaBinaryDescription method getPackageComponentsFromDeps.

private LuaBinaryPackageComponents getPackageComponentsFromDeps(BuildRuleParams baseParams, BuildRuleResolver ruleResolver, SourcePathResolver pathResolver, SourcePathRuleFinder ruleFinder, final CxxPlatform cxxPlatform, final PythonPlatform pythonPlatform, Optional<BuildTarget> nativeStarterLibrary, String mainModule, LuaConfig.PackageStyle packageStyle, Iterable<BuildRule> deps) throws NoSuchBuildTargetException {
    final LuaPackageComponents.Builder builder = LuaPackageComponents.builder();
    final OmnibusRoots.Builder omnibusRoots = OmnibusRoots.builder(cxxPlatform, ImmutableSet.of());
    final Map<BuildTarget, NativeLinkable> nativeLinkableRoots = new LinkedHashMap<>();
    final Map<BuildTarget, CxxLuaExtension> luaExtensions = new LinkedHashMap<>();
    final Map<BuildTarget, CxxPythonExtension> pythonExtensions = new LinkedHashMap<>();
    // Walk the deps to find all Lua packageables and native linkables.
    new AbstractBreadthFirstThrowingTraversal<BuildRule, NoSuchBuildTargetException>(deps) {

        private final ImmutableSet<BuildRule> empty = ImmutableSet.of();

        @Override
        public ImmutableSet<BuildRule> visit(BuildRule rule) throws NoSuchBuildTargetException {
            ImmutableSet<BuildRule> deps = empty;
            if (rule instanceof LuaPackageable) {
                LuaPackageable packageable = (LuaPackageable) rule;
                LuaPackageComponents components = packageable.getLuaPackageComponents();
                LuaPackageComponents.addComponents(builder, components);
                if (components.hasNativeCode(cxxPlatform)) {
                    for (BuildRule dep : rule.getDeps()) {
                        if (dep instanceof NativeLinkable) {
                            NativeLinkable linkable = (NativeLinkable) dep;
                            nativeLinkableRoots.put(linkable.getBuildTarget(), linkable);
                            omnibusRoots.addExcludedRoot(linkable);
                        }
                    }
                }
                deps = rule.getDeps();
            } else if (rule instanceof CxxPythonExtension) {
                CxxPythonExtension extension = (CxxPythonExtension) rule;
                NativeLinkTarget target = extension.getNativeLinkTarget(pythonPlatform);
                pythonExtensions.put(target.getBuildTarget(), (CxxPythonExtension) rule);
                omnibusRoots.addIncludedRoot(target);
            } else if (rule instanceof PythonPackagable) {
                PythonPackagable packageable = (PythonPackagable) rule;
                PythonPackageComponents components = packageable.getPythonPackageComponents(pythonPlatform, cxxPlatform);
                builder.putAllPythonModules(MoreMaps.transformKeys(components.getModules(), Object::toString));
                builder.putAllNativeLibraries(MoreMaps.transformKeys(components.getNativeLibraries(), Object::toString));
                if (components.hasNativeCode(cxxPlatform)) {
                    for (BuildRule dep : rule.getDeps()) {
                        if (dep instanceof NativeLinkable) {
                            NativeLinkable linkable = (NativeLinkable) dep;
                            nativeLinkableRoots.put(linkable.getBuildTarget(), linkable);
                            omnibusRoots.addExcludedRoot(linkable);
                        }
                    }
                }
                deps = rule.getDeps();
            } else if (rule instanceof CxxLuaExtension) {
                CxxLuaExtension extension = (CxxLuaExtension) rule;
                luaExtensions.put(extension.getBuildTarget(), extension);
                omnibusRoots.addIncludedRoot(extension);
            } else if (rule instanceof NativeLinkable) {
                NativeLinkable linkable = (NativeLinkable) rule;
                nativeLinkableRoots.put(linkable.getBuildTarget(), linkable);
                omnibusRoots.addPotentialRoot(linkable);
            }
            return deps;
        }
    }.start();
    // Build the starter.
    Starter starter = createStarter(baseParams, ruleResolver, pathResolver, ruleFinder, cxxPlatform, nativeStarterLibrary, mainModule, packageStyle, !nativeLinkableRoots.isEmpty() || !omnibusRoots.isEmpty());
    SourcePath starterPath = null;
    if (luaConfig.getNativeLinkStrategy() == NativeLinkStrategy.MERGED) {
        // If we're using a native starter, include it in omnibus linking.
        if (starter instanceof NativeExecutableStarter) {
            NativeExecutableStarter nativeStarter = (NativeExecutableStarter) starter;
            omnibusRoots.addIncludedRoot(nativeStarter);
        }
        // Build the omnibus libraries.
        OmnibusRoots roots = omnibusRoots.build();
        OmnibusLibraries libraries = Omnibus.getSharedLibraries(baseParams, ruleResolver, ruleFinder, cxxBuckConfig, cxxPlatform, ImmutableList.of(), roots.getIncludedRoots().values(), roots.getExcludedRoots().values());
        // Add all the roots from the omnibus link.  If it's an extension, add it as a module.
        for (Map.Entry<BuildTarget, OmnibusRoot> root : libraries.getRoots().entrySet()) {
            // If it's a Lua extension add it as a module.
            CxxLuaExtension luaExtension = luaExtensions.get(root.getKey());
            if (luaExtension != null) {
                builder.putModules(luaExtension.getModule(cxxPlatform), root.getValue().getPath());
                continue;
            }
            // If it's a Python extension, add it as a python module.
            CxxPythonExtension pythonExtension = pythonExtensions.get(root.getKey());
            if (pythonExtension != null) {
                builder.putPythonModules(pythonExtension.getModule().toString(), root.getValue().getPath());
                continue;
            }
            // A root named after the top-level target is our native starter.
            if (root.getKey().equals(baseParams.getBuildTarget())) {
                starterPath = root.getValue().getPath();
                continue;
            }
            // Otherwise, add it as a native library.
            NativeLinkTarget target = Preconditions.checkNotNull(roots.getIncludedRoots().get(root.getKey()), "%s: linked unexpected omnibus root: %s", baseParams.getBuildTarget(), root.getKey());
            NativeLinkTargetMode mode = target.getNativeLinkTargetMode(cxxPlatform);
            String soname = Preconditions.checkNotNull(mode.getLibraryName().orElse(null), "%s: omnibus library for %s was built without soname", baseParams.getBuildTarget(), root.getKey());
            builder.putNativeLibraries(soname, root.getValue().getPath());
        }
        // Add all remaining libraries as native libraries.
        for (OmnibusLibrary library : libraries.getLibraries()) {
            builder.putNativeLibraries(library.getSoname(), library.getPath());
        }
    } else {
        // roots.
        for (Map.Entry<BuildTarget, CxxLuaExtension> entry : luaExtensions.entrySet()) {
            CxxLuaExtension extension = entry.getValue();
            builder.putModules(extension.getModule(cxxPlatform), extension.getExtension(cxxPlatform));
            nativeLinkableRoots.putAll(Maps.uniqueIndex(extension.getNativeLinkTargetDeps(cxxPlatform), NativeLinkable::getBuildTarget));
        }
        // Add in native executable deps.
        if (starter instanceof NativeExecutableStarter) {
            NativeExecutableStarter executableStarter = (NativeExecutableStarter) starter;
            nativeLinkableRoots.putAll(Maps.uniqueIndex(executableStarter.getNativeStarterDeps(), NativeLinkable::getBuildTarget));
        }
        // python-platform specific deps to the native linkables.
        for (Map.Entry<BuildTarget, CxxPythonExtension> entry : pythonExtensions.entrySet()) {
            PythonPackageComponents components = entry.getValue().getPythonPackageComponents(pythonPlatform, cxxPlatform);
            builder.putAllPythonModules(MoreMaps.transformKeys(components.getModules(), Object::toString));
            builder.putAllNativeLibraries(MoreMaps.transformKeys(components.getNativeLibraries(), Object::toString));
            nativeLinkableRoots.putAll(Maps.uniqueIndex(entry.getValue().getNativeLinkTarget(pythonPlatform).getNativeLinkTargetDeps(cxxPlatform), NativeLinkable::getBuildTarget));
        }
        // Add shared libraries from all native linkables.
        for (NativeLinkable nativeLinkable : NativeLinkables.getTransitiveNativeLinkables(cxxPlatform, nativeLinkableRoots.values()).values()) {
            NativeLinkable.Linkage linkage = nativeLinkable.getPreferredLinkage(cxxPlatform);
            if (linkage != NativeLinkable.Linkage.STATIC) {
                builder.putAllNativeLibraries(nativeLinkable.getSharedLibraries(cxxPlatform));
            }
        }
    }
    // building one directly from the starter.
    if (starterPath == null) {
        starterPath = starter.build();
    }
    return LuaBinaryPackageComponents.of(starterPath, builder.build());
}
Also used : CxxPythonExtension(com.facebook.buck.python.CxxPythonExtension) NativeLinkable(com.facebook.buck.cxx.NativeLinkable) NativeLinkTargetMode(com.facebook.buck.cxx.NativeLinkTargetMode) OmnibusRoot(com.facebook.buck.cxx.OmnibusRoot) LinkedHashMap(java.util.LinkedHashMap) SourcePath(com.facebook.buck.rules.SourcePath) OmnibusLibraries(com.facebook.buck.cxx.OmnibusLibraries) ImmutableSet(com.google.common.collect.ImmutableSet) BuildTarget(com.facebook.buck.model.BuildTarget) OmnibusRoots(com.facebook.buck.cxx.OmnibusRoots) BuildRule(com.facebook.buck.rules.BuildRule) PythonPackageComponents(com.facebook.buck.python.PythonPackageComponents) OmnibusLibrary(com.facebook.buck.cxx.OmnibusLibrary) PythonPackagable(com.facebook.buck.python.PythonPackagable) NativeLinkTarget(com.facebook.buck.cxx.NativeLinkTarget) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap)

Aggregations

BuildRule (com.facebook.buck.rules.BuildRule)361 BuildTarget (com.facebook.buck.model.BuildTarget)199 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)190 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)189 Test (org.junit.Test)175 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)165 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)162 SourcePath (com.facebook.buck.rules.SourcePath)118 Path (java.nio.file.Path)95 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)70 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)70 FakeBuildRule (com.facebook.buck.rules.FakeBuildRule)66 TargetGraph (com.facebook.buck.rules.TargetGraph)63 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)61 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)61 ImmutableList (com.google.common.collect.ImmutableList)58 PathSourcePath (com.facebook.buck.rules.PathSourcePath)51 ImmutableSet (com.google.common.collect.ImmutableSet)45 ImmutableMap (com.google.common.collect.ImmutableMap)44 RuleKey (com.facebook.buck.rules.RuleKey)43