Search in sources :

Example 91 with BuildRule

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

use of com.facebook.buck.rules.BuildRule 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)

Example 93 with BuildRule

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

the class OcamlRuleBuilder method getBytecodeLinkableInput.

private static NativeLinkableInput getBytecodeLinkableInput(Iterable<BuildRule> deps) {
    List<NativeLinkableInput> inputs = Lists.newArrayList();
    // Add in the linkable input from OCaml libraries.
    ImmutableList<BuildRule> ocamlDeps = getTransitiveOcamlLibraryDeps(deps);
    for (BuildRule dep : ocamlDeps) {
        inputs.add(((OcamlLibrary) dep).getBytecodeLinkableInput());
    }
    return NativeLinkableInput.concat(inputs);
}
Also used : NativeLinkableInput(com.facebook.buck.cxx.NativeLinkableInput) BuildRule(com.facebook.buck.rules.BuildRule)

Example 94 with BuildRule

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

the class SwiftCompile method makeCompileStep.

private SwiftCompileStep makeCompileStep(SourcePathResolver resolver) {
    ImmutableList.Builder<String> compilerCommand = ImmutableList.builder();
    compilerCommand.addAll(swiftCompiler.getCommandPrefix(resolver));
    if (bridgingHeader.isPresent()) {
        compilerCommand.add("-import-objc-header", resolver.getRelativePath(bridgingHeader.get()).toString());
    }
    final Function<FrameworkPath, Path> frameworkPathToSearchPath = CxxDescriptionEnhancer.frameworkPathToSearchPath(cxxPlatform, resolver);
    compilerCommand.addAll(frameworks.stream().map(frameworkPathToSearchPath::apply).flatMap(searchPath -> ImmutableSet.of("-F", searchPath.toString()).stream()).iterator());
    compilerCommand.addAll(MoreIterables.zipAndConcat(Iterables.cycle("-Xcc"), getSwiftIncludeArgs(resolver)));
    compilerCommand.addAll(MoreIterables.zipAndConcat(Iterables.cycle(INCLUDE_FLAG), getDeps().stream().filter(SwiftCompile.class::isInstance).map(BuildRule::getSourcePathToOutput).map(input -> resolver.getAbsolutePath(input).toString()).collect(MoreCollectors.toImmutableSet())));
    Optional<Iterable<String>> configFlags = swiftBuckConfig.getFlags();
    if (configFlags.isPresent()) {
        compilerCommand.addAll(configFlags.get());
    }
    boolean hasMainEntry = srcs.stream().map(input -> resolver.getAbsolutePath(input).getFileName().toString()).anyMatch(SwiftDescriptions.SWIFT_MAIN_FILENAME::equalsIgnoreCase);
    compilerCommand.add("-c", enableObjcInterop ? "-enable-objc-interop" : "", hasMainEntry ? "" : "-parse-as-library", "-module-name", moduleName, "-emit-module", "-emit-module-path", modulePath.toString(), "-o", objectPath.toString(), "-emit-objc-header-path", headerPath.toString());
    compilerCommand.addAll(compilerFlags);
    for (SourcePath sourcePath : srcs) {
        compilerCommand.add(resolver.getRelativePath(sourcePath).toString());
    }
    ProjectFilesystem projectFilesystem = getProjectFilesystem();
    return new SwiftCompileStep(projectFilesystem.getRootPath(), ImmutableMap.of(), compilerCommand.build());
}
Also used : FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) Iterables(com.google.common.collect.Iterables) Step(com.facebook.buck.step.Step) SourcePathArg(com.facebook.buck.rules.args.SourcePathArg) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) CxxHeaders(com.facebook.buck.cxx.CxxHeaders) MkdirStep(com.facebook.buck.step.fs.MkdirStep) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) BuildRule(com.facebook.buck.rules.BuildRule) Tool(com.facebook.buck.rules.Tool) ImmutableList(com.google.common.collect.ImmutableList) CxxPreprocessables(com.facebook.buck.cxx.CxxPreprocessables) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) StringArg(com.facebook.buck.rules.args.StringArg) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) CxxDescriptionEnhancer(com.facebook.buck.cxx.CxxDescriptionEnhancer) LinkedHashSet(java.util.LinkedHashSet) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) FileListableLinkerInputArg(com.facebook.buck.rules.args.FileListableLinkerInputArg) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) ImmutableMap(com.google.common.collect.ImmutableMap) BuildableContext(com.facebook.buck.rules.BuildableContext) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) BuildTarget(com.facebook.buck.model.BuildTarget) HeaderVisibility(com.facebook.buck.cxx.HeaderVisibility) AbstractBuildRule(com.facebook.buck.rules.AbstractBuildRule) LinkerMapMode(com.facebook.buck.cxx.LinkerMapMode) Arg(com.facebook.buck.rules.args.Arg) CxxPreprocessorInput(com.facebook.buck.cxx.CxxPreprocessorInput) BuildContext(com.facebook.buck.rules.BuildContext) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MoreIterables(com.facebook.buck.util.MoreIterables) ImmutableList(com.google.common.collect.ImmutableList) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) BuildRule(com.facebook.buck.rules.BuildRule) AbstractBuildRule(com.facebook.buck.rules.AbstractBuildRule) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem)

Example 95 with BuildRule

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

the class SwiftLibrary method requireSwiftCompileRule.

SwiftCompile requireSwiftCompileRule(Flavor... flavors) throws NoSuchBuildTargetException {
    BuildTarget requiredBuildTarget = getBuildTarget().withAppendedFlavors(flavors).withoutFlavors(ImmutableSet.of(CxxDescriptionEnhancer.SHARED_FLAVOR)).withoutFlavors(ImmutableSet.of(SWIFT_COMPANION_FLAVOR)).withoutFlavors(LinkerMapMode.FLAVOR_DOMAIN.getFlavors()).withAppendedFlavors(SWIFT_COMPILE_FLAVOR);
    BuildRule rule = ruleResolver.requireRule(requiredBuildTarget);
    if (!(rule instanceof SwiftCompile)) {
        throw new RuntimeException(String.format("Could not find SwiftCompile with target %s", requiredBuildTarget));
    }
    return (SwiftCompile) rule;
}
Also used : BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule)

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