Search in sources :

Example 96 with BuildTarget

use of com.facebook.buck.model.BuildTarget in project buck by facebook.

the class IjModuleGraph method createModules.

/**
   * Create all the modules we are capable of representing in IntelliJ from the supplied graph.
   *
   * @param targetGraph graph whose nodes will be converted to {@link IjModule}s.
   * @return map which for every BuildTarget points to the corresponding IjModule. Multiple
   * BuildTarget can point to one IjModule (many:one mapping), the BuildTargets which
   * can't be prepresented in IntelliJ are missing from this mapping.
   */
private static ImmutableMap<BuildTarget, IjModule> createModules(IjProjectConfig projectConfig, TargetGraph targetGraph, IjModuleFactory moduleFactory, final int minimumPathDepth) {
    final BlockedPathNode blockedPathTree = createAggregationHaltPoints(projectConfig, targetGraph);
    ImmutableListMultimap<Path, TargetNode<?, ?>> baseTargetPathMultimap = targetGraph.getNodes().stream().filter(input -> IjModuleFactory.SUPPORTED_MODULE_DESCRIPTION_CLASSES.contains(input.getDescription().getClass())).collect(MoreCollectors.toImmutableListMultimap(targetNode -> {
        Path path;
        Path basePath = targetNode.getBuildTarget().getBasePath();
        if (targetNode.getConstructorArg() instanceof AndroidResourceDescription.Arg) {
            path = basePath;
        } else {
            path = simplifyPath(basePath, minimumPathDepth, blockedPathTree);
        }
        return path;
    }, targetNode -> targetNode));
    ImmutableMap.Builder<BuildTarget, IjModule> moduleMapBuilder = new ImmutableMap.Builder<>();
    for (Path baseTargetPath : baseTargetPathMultimap.keySet()) {
        ImmutableSet<TargetNode<?, ?>> targets = ImmutableSet.copyOf(baseTargetPathMultimap.get(baseTargetPath));
        IjModule module = moduleFactory.createModule(baseTargetPath, targets);
        for (TargetNode<?, ?> target : targets) {
            moduleMapBuilder.put(target.getBuildTarget(), module);
        }
    }
    return moduleMapBuilder.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TargetGraph(com.facebook.buck.rules.TargetGraph) TargetNode(com.facebook.buck.rules.TargetNode) Set(java.util.Set) HashMap(java.util.HashMap) JavacOptions(com.facebook.buck.jvm.java.JavacOptions) BuildTarget(com.facebook.buck.model.BuildTarget) AndroidResourceDescription(com.facebook.buck.android.AndroidResourceDescription) HashSet(java.util.HashSet) Objects(java.util.Objects) Stream(java.util.stream.Stream) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Map(java.util.Map) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) JavaLibraryDescription(com.facebook.buck.jvm.java.JavaLibraryDescription) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) MoreCollectors(com.facebook.buck.util.MoreCollectors) Path(java.nio.file.Path) TargetNode(com.facebook.buck.rules.TargetNode) ImmutableMap(com.google.common.collect.ImmutableMap) AndroidResourceDescription(com.facebook.buck.android.AndroidResourceDescription) BuildTarget(com.facebook.buck.model.BuildTarget)

Example 97 with BuildTarget

use of com.facebook.buck.model.BuildTarget in project buck by facebook.

the class PythonLibraryDescription method createMetadata.

@Override
public <A extends Arg, U> Optional<U> createMetadata(BuildTarget buildTarget, BuildRuleResolver resolver, A args, Optional<ImmutableMap<BuildTarget, Version>> selectedVersions, Class<U> metadataClass) throws NoSuchBuildTargetException {
    Map.Entry<Flavor, MetadataType> type = METADATA_TYPE.getFlavorAndValue(buildTarget).orElseThrow(IllegalArgumentException::new);
    BuildTarget baseTarget = buildTarget.withoutFlavors(type.getKey());
    switch(type.getValue()) {
        case PACKAGE_COMPONENTS:
            {
                Map.Entry<Flavor, PythonPlatform> pythonPlatform = pythonPlatforms.getFlavorAndValue(baseTarget).orElseThrow(IllegalArgumentException::new);
                Map.Entry<Flavor, CxxPlatform> cxxPlatform = cxxPlatforms.getFlavorAndValue(baseTarget).orElseThrow(IllegalArgumentException::new);
                baseTarget = buildTarget.withoutFlavors(pythonPlatform.getKey(), cxxPlatform.getKey());
                SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
                SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
                Path baseModule = PythonUtil.getBasePath(baseTarget, args.baseModule);
                PythonPackageComponents components = PythonPackageComponents.of(PythonUtil.getModules(baseTarget, resolver, ruleFinder, pathResolver, pythonPlatform.getValue(), cxxPlatform.getValue(), "srcs", baseModule, args.srcs, args.platformSrcs, args.versionedSrcs, selectedVersions), PythonUtil.getModules(baseTarget, resolver, ruleFinder, pathResolver, pythonPlatform.getValue(), cxxPlatform.getValue(), "resources", baseModule, args.resources, args.platformResources, args.versionedResources, selectedVersions), ImmutableMap.of(), ImmutableSet.of(), args.zipSafe);
                return Optional.of(components).map(metadataClass::cast);
            }
    }
    throw new IllegalStateException();
}
Also used : Path(java.nio.file.Path) BuildTarget(com.facebook.buck.model.BuildTarget) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) InternalFlavor(com.facebook.buck.model.InternalFlavor) Flavor(com.facebook.buck.model.Flavor) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver)

Example 98 with BuildTarget

use of com.facebook.buck.model.BuildTarget in project buck by facebook.

the class PythonUtil method getAllComponents.

static PythonPackageComponents getAllComponents(BuildRuleParams params, BuildRuleResolver ruleResolver, SourcePathRuleFinder ruleFinder, final PythonPackageComponents packageComponents, final PythonPlatform pythonPlatform, CxxBuckConfig cxxBuckConfig, final CxxPlatform cxxPlatform, ImmutableList<? extends Arg> extraLdflags, final NativeLinkStrategy nativeLinkStrategy, final ImmutableSet<BuildTarget> preloadDeps) throws NoSuchBuildTargetException {
    final PythonPackageComponents.Builder allComponents = new PythonPackageComponents.Builder(params.getBuildTarget());
    final Map<BuildTarget, CxxPythonExtension> extensions = new LinkedHashMap<>();
    final Map<BuildTarget, NativeLinkable> nativeLinkableRoots = new LinkedHashMap<>();
    final OmnibusRoots.Builder omnibusRoots = OmnibusRoots.builder(cxxPlatform, preloadDeps);
    // Add the top-level components.
    allComponents.addComponent(packageComponents, params.getBuildTarget());
    // Walk all our transitive deps to build our complete package that we'll
    // turn into an executable.
    new AbstractBreadthFirstThrowingTraversal<BuildRule, NoSuchBuildTargetException>(params.getDeps()) {

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

        @Override
        public Iterable<BuildRule> visit(BuildRule rule) throws NoSuchBuildTargetException {
            Iterable<BuildRule> deps = empty;
            if (rule instanceof CxxPythonExtension) {
                CxxPythonExtension extension = (CxxPythonExtension) rule;
                NativeLinkTarget target = ((CxxPythonExtension) rule).getNativeLinkTarget(pythonPlatform);
                extensions.put(target.getBuildTarget(), extension);
                omnibusRoots.addIncludedRoot(target);
                List<BuildRule> cxxpydeps = new ArrayList<>();
                for (BuildRule dep : rule.getDeps()) {
                    if (dep instanceof CxxPythonExtension) {
                        cxxpydeps.add(dep);
                    }
                }
                deps = cxxpydeps;
            } else if (rule instanceof PythonPackagable) {
                PythonPackagable packagable = (PythonPackagable) rule;
                PythonPackageComponents comps = packagable.getPythonPackageComponents(pythonPlatform, cxxPlatform);
                allComponents.addComponent(comps, rule.getBuildTarget());
                if (comps.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 NativeLinkable) {
                NativeLinkable linkable = (NativeLinkable) rule;
                nativeLinkableRoots.put(linkable.getBuildTarget(), linkable);
                omnibusRoots.addPotentialRoot(linkable);
            }
            return deps;
        }
    }.start();
    // excluded native linkable roots.
    if (nativeLinkStrategy == NativeLinkStrategy.MERGED) {
        OmnibusRoots roots = omnibusRoots.build();
        OmnibusLibraries libraries = Omnibus.getSharedLibraries(params, ruleResolver, ruleFinder, cxxBuckConfig, cxxPlatform, extraLdflags, roots.getIncludedRoots().values(), roots.getExcludedRoots().values());
        // Otherwise, add it as a native library.
        for (Map.Entry<BuildTarget, OmnibusRoot> root : libraries.getRoots().entrySet()) {
            CxxPythonExtension extension = extensions.get(root.getKey());
            if (extension != null) {
                allComponents.addModule(extension.getModule(), root.getValue().getPath(), root.getKey());
            } else {
                NativeLinkTarget target = Preconditions.checkNotNull(roots.getIncludedRoots().get(root.getKey()), "%s: linked unexpected omnibus root: %s", params.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", params.getBuildTarget(), root.getKey());
                allComponents.addNativeLibraries(Paths.get(soname), root.getValue().getPath(), root.getKey());
            }
        }
        // Add all remaining libraries as native libraries.
        for (OmnibusLibrary library : libraries.getLibraries()) {
            allComponents.addNativeLibraries(Paths.get(library.getSoname()), library.getPath(), params.getBuildTarget());
        }
    } else {
        // For regular linking, add all extensions via the package components interface.
        Map<BuildTarget, NativeLinkable> extensionNativeDeps = new LinkedHashMap<>();
        for (Map.Entry<BuildTarget, CxxPythonExtension> entry : extensions.entrySet()) {
            allComponents.addComponent(entry.getValue().getPythonPackageComponents(pythonPlatform, cxxPlatform), entry.getValue().getBuildTarget());
            extensionNativeDeps.putAll(Maps.uniqueIndex(entry.getValue().getNativeLinkTarget(pythonPlatform).getNativeLinkTargetDeps(cxxPlatform), NativeLinkable::getBuildTarget));
        }
        // Add all the native libraries.
        ImmutableMap<BuildTarget, NativeLinkable> nativeLinkables = NativeLinkables.getTransitiveNativeLinkables(cxxPlatform, Iterables.concat(nativeLinkableRoots.values(), extensionNativeDeps.values()));
        for (NativeLinkable nativeLinkable : nativeLinkables.values()) {
            NativeLinkable.Linkage linkage = nativeLinkable.getPreferredLinkage(cxxPlatform);
            if (nativeLinkableRoots.containsKey(nativeLinkable.getBuildTarget()) || linkage != NativeLinkable.Linkage.STATIC) {
                ImmutableMap<String, SourcePath> libs = nativeLinkable.getSharedLibraries(cxxPlatform);
                for (Map.Entry<String, SourcePath> ent : libs.entrySet()) {
                    allComponents.addNativeLibraries(Paths.get(ent.getKey()), ent.getValue(), nativeLinkable.getBuildTarget());
                }
            }
        }
    }
    return allComponents.build();
}
Also used : FluentIterable(com.google.common.collect.FluentIterable) NativeLinkable(com.facebook.buck.cxx.NativeLinkable) NativeLinkTargetMode(com.facebook.buck.cxx.NativeLinkTargetMode) OmnibusRoot(com.facebook.buck.cxx.OmnibusRoot) LinkedHashMap(java.util.LinkedHashMap) OmnibusLibraries(com.facebook.buck.cxx.OmnibusLibraries) SourcePath(com.facebook.buck.rules.SourcePath) BuildTarget(com.facebook.buck.model.BuildTarget) OmnibusRoots(com.facebook.buck.cxx.OmnibusRoots) BuildRule(com.facebook.buck.rules.BuildRule) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) SourceList(com.facebook.buck.rules.coercer.SourceList) List(java.util.List) OmnibusLibrary(com.facebook.buck.cxx.OmnibusLibrary) NativeLinkTarget(com.facebook.buck.cxx.NativeLinkTarget) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 99 with BuildTarget

use of com.facebook.buck.model.BuildTarget in project buck by facebook.

the class TargetNodeParsePipeline method computeNode.

@Override
protected TargetNode<?, ?> computeNode(final Cell cell, final BuildTarget buildTarget, final Map<String, Object> rawNode) throws BuildTargetException {
    try (final SimplePerfEvent.Scope scope = SimplePerfEvent.scopeIgnoringShortEvents(eventBus, PerfEventId.of("GetTargetNode"), "target", buildTarget, targetNodePipelineLifetimeEventScope, getMinimumPerfEventTimeMs(), TimeUnit.MILLISECONDS)) {
        Function<PerfEventId, SimplePerfEvent.Scope> perfEventScopeFunction = perfEventId -> SimplePerfEvent.scopeIgnoringShortEvents(eventBus, perfEventId, scope, getMinimumPerfEventTimeMs(), TimeUnit.MILLISECONDS);
        final TargetNode<?, ?> targetNode = delegate.createTargetNode(cell, cell.getAbsolutePathToBuildFile(buildTarget), buildTarget, rawNode, perfEventScopeFunction);
        if (speculativeDepsTraversal) {
            executorService.submit(() -> {
                for (BuildTarget depTarget : targetNode.getDeps()) {
                    Path depCellPath = depTarget.getCellPath();
                    // non-threadsafe way making it inconvenient to access from the pipeline.
                    if (depCellPath.equals(cell.getRoot())) {
                        try {
                            if (depTarget.isFlavored()) {
                                getNodeJob(cell, BuildTarget.of(depTarget.getUnflavoredBuildTarget()));
                            }
                            getNodeJob(cell, depTarget);
                        } catch (BuildTargetException e) {
                            // No biggie, we'll hit the error again in the non-speculative path.
                            LOG.info(e, "Could not schedule speculative parsing for %s", depTarget);
                        }
                    }
                }
            });
        }
        return targetNode;
    }
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) Logger(com.facebook.buck.log.Logger) PerfEventId(com.facebook.buck.event.PerfEventId) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TargetNode(com.facebook.buck.rules.TargetNode) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) BuildTargetException(com.facebook.buck.model.BuildTargetException) ThreadSafe(javax.annotation.concurrent.ThreadSafe) BuildTarget(com.facebook.buck.model.BuildTarget) TimeUnit(java.util.concurrent.TimeUnit) Map(java.util.Map) Cache(com.facebook.buck.parser.PipelineNodeCache.Cache) Cell(com.facebook.buck.rules.Cell) Path(java.nio.file.Path) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Path(java.nio.file.Path) BuildTargetException(com.facebook.buck.model.BuildTargetException) PerfEventId(com.facebook.buck.event.PerfEventId) BuildTarget(com.facebook.buck.model.BuildTarget) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent)

Example 100 with BuildTarget

use of com.facebook.buck.model.BuildTarget in project buck by facebook.

the class CxxPythonExtensionDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, final BuildRuleParams params, final BuildRuleResolver ruleResolver, final A args) throws NoSuchBuildTargetException {
    Optional<Map.Entry<Flavor, CxxPlatform>> platform = cxxPlatforms.getFlavorAndValue(params.getBuildTarget());
    if (params.getBuildTarget().getFlavors().contains(CxxDescriptionEnhancer.SANDBOX_TREE_FLAVOR)) {
        return CxxDescriptionEnhancer.createSandboxTreeBuildRule(ruleResolver, args, platform.get().getValue(), params);
    }
    // See if we're building a particular "type" of this library, and if so, extract
    // it as an enum.
    final Optional<Map.Entry<Flavor, Type>> type = LIBRARY_TYPE.getFlavorAndValue(params.getBuildTarget());
    final Optional<Map.Entry<Flavor, PythonPlatform>> pythonPlatform = pythonPlatforms.getFlavorAndValue(params.getBuildTarget());
    // pre-existing static lib, which we do here.
    if (type.isPresent() && platform.isPresent() && pythonPlatform.isPresent()) {
        Preconditions.checkState(type.get().getValue() == Type.EXTENSION);
        return createExtensionBuildRule(params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.copyOf(getPlatformDeps(params, ruleResolver, pythonPlatform.get().getValue(), args))), Suppliers.ofInstance(ImmutableSortedSet.of())), ruleResolver, pythonPlatform.get().getValue(), platform.get().getValue(), args);
    }
    // Otherwise, we return the generic placeholder of this library, that dependents can use
    // get the real build rules via querying the action graph.
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
    final SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    Path baseModule = PythonUtil.getBasePath(params.getBuildTarget(), args.baseModule);
    String moduleName = args.moduleName.orElse(params.getBuildTarget().getShortName());
    final Path module = baseModule.resolve(getExtensionName(moduleName));
    return new CxxPythonExtension(params) {

        @Override
        protected BuildRule getExtension(PythonPlatform pythonPlatform, CxxPlatform cxxPlatform) throws NoSuchBuildTargetException {
            return ruleResolver.requireRule(getBuildTarget().withAppendedFlavors(pythonPlatform.getFlavor(), cxxPlatform.getFlavor(), CxxDescriptionEnhancer.SHARED_FLAVOR));
        }

        @Override
        public Path getModule() {
            return module;
        }

        @Override
        public PythonPackageComponents getPythonPackageComponents(PythonPlatform pythonPlatform, CxxPlatform cxxPlatform) throws NoSuchBuildTargetException {
            BuildRule extension = getExtension(pythonPlatform, cxxPlatform);
            SourcePath output = extension.getSourcePathToOutput();
            return PythonPackageComponents.of(ImmutableMap.of(module, output), ImmutableMap.of(), ImmutableMap.of(), ImmutableSet.of(), Optional.of(false));
        }

        @Override
        public NativeLinkTarget getNativeLinkTarget(final PythonPlatform pythonPlatform) {
            return new NativeLinkTarget() {

                @Override
                public BuildTarget getBuildTarget() {
                    return params.getBuildTarget().withAppendedFlavors(pythonPlatform.getFlavor());
                }

                @Override
                public NativeLinkTargetMode getNativeLinkTargetMode(CxxPlatform cxxPlatform) {
                    return NativeLinkTargetMode.library();
                }

                @Override
                public Iterable<? extends NativeLinkable> getNativeLinkTargetDeps(CxxPlatform cxxPlatform) {
                    return FluentIterable.from(getPlatformDeps(params, ruleResolver, pythonPlatform, args)).filter(NativeLinkable.class);
                }

                @Override
                public NativeLinkableInput getNativeLinkTargetInput(CxxPlatform cxxPlatform) throws NoSuchBuildTargetException {
                    return NativeLinkableInput.builder().addAllArgs(getExtensionArgs(params.withBuildTarget(params.getBuildTarget().withAppendedFlavors(pythonPlatform.getFlavor(), CxxDescriptionEnhancer.SHARED_FLAVOR)).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.copyOf(getPlatformDeps(params, ruleResolver, pythonPlatform, args))), Suppliers.ofInstance(ImmutableSortedSet.of())), ruleResolver, pathResolver, ruleFinder, cxxPlatform, args)).addAllFrameworks(args.frameworks).build();
                }

                @Override
                public Optional<Path> getNativeLinkTargetOutputPath(CxxPlatform cxxPlatform) {
                    return Optional.empty();
                }
            };
        }

        @Override
        public Stream<BuildTarget> getRuntimeDeps() {
            return getDeclaredDeps().stream().map(BuildRule::getBuildTarget);
        }
    };
}
Also used : Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) NativeLinkTarget(com.facebook.buck.cxx.NativeLinkTarget) SourcePath(com.facebook.buck.rules.SourcePath) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule)

Aggregations

BuildTarget (com.facebook.buck.model.BuildTarget)1045 Test (org.junit.Test)758 Path (java.nio.file.Path)323 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)289 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)254 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)248 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)226 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)216 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)209 BuildRule (com.facebook.buck.rules.BuildRule)196 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)178 SourcePath (com.facebook.buck.rules.SourcePath)163 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)161 TargetGraph (com.facebook.buck.rules.TargetGraph)156 PathSourcePath (com.facebook.buck.rules.PathSourcePath)116 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)108 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)91 ImmutableSet (com.google.common.collect.ImmutableSet)90 ImmutableMap (com.google.common.collect.ImmutableMap)78 FakeBuildRuleParamsBuilder (com.facebook.buck.rules.FakeBuildRuleParamsBuilder)75