Search in sources :

Example 86 with SourcePathRuleFinder

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

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

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

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

the class PrebuiltOcamlLibraryDescription method createBuildRule.

@Override
public <A extends Arg> OcamlLibrary createBuildRule(TargetGraph targetGraph, final BuildRuleParams params, BuildRuleResolver resolver, final A args) {
    final BuildTarget target = params.getBuildTarget();
    final boolean bytecodeOnly = args.bytecodeOnly.orElse(false);
    final String libDir = args.libDir.orElse("lib");
    final String libName = args.libName.orElse(target.getShortName());
    final String nativeLib = args.nativeLib.orElse(String.format("%s.cmxa", libName));
    final String bytecodeLib = args.bytecodeLib.orElse(String.format("%s.cma", libName));
    final ImmutableList<String> cLibs = args.cLibs;
    final Path libPath = target.getBasePath().resolve(libDir);
    final Path includeDir = libPath.resolve(args.includeDir.orElse(""));
    final Optional<SourcePath> staticNativeLibraryPath = bytecodeOnly ? Optional.empty() : Optional.of(new PathSourcePath(params.getProjectFilesystem(), libPath.resolve(nativeLib)));
    final SourcePath staticBytecodeLibraryPath = new PathSourcePath(params.getProjectFilesystem(), libPath.resolve(bytecodeLib));
    final ImmutableList<SourcePath> staticCLibraryPaths = cLibs.stream().map(input -> new PathSourcePath(params.getProjectFilesystem(), libPath.resolve(input))).collect(MoreCollectors.toImmutableList());
    final SourcePath bytecodeLibraryPath = new PathSourcePath(params.getProjectFilesystem(), libPath.resolve(bytecodeLib));
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    return new PrebuiltOcamlLibrary(params, ruleFinder, staticNativeLibraryPath, staticBytecodeLibraryPath, staticCLibraryPaths, bytecodeLibraryPath, libPath, includeDir);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) TargetGraph(com.facebook.buck.rules.TargetGraph) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePath(com.facebook.buck.rules.SourcePath) VersionPropagator(com.facebook.buck.versions.VersionPropagator) BuildTarget(com.facebook.buck.model.BuildTarget) SuppressFieldNotInitialized(com.facebook.infer.annotation.SuppressFieldNotInitialized) AbstractDescriptionArg(com.facebook.buck.rules.AbstractDescriptionArg) ImmutableList(com.google.common.collect.ImmutableList) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Optional(java.util.Optional) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) Description(com.facebook.buck.rules.Description) MoreCollectors(com.facebook.buck.util.MoreCollectors) BuildTarget(com.facebook.buck.model.BuildTarget) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder)

Example 90 with SourcePathRuleFinder

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

the class SwiftLibraryDescription method createCompanionBuildRule.

public <A extends CxxLibraryDescription.Arg> Optional<BuildRule> createCompanionBuildRule(final TargetGraph targetGraph, final BuildRuleParams params, final BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
    BuildTarget buildTarget = params.getBuildTarget();
    if (!isSwiftTarget(buildTarget)) {
        boolean hasSwiftSource = !SwiftDescriptions.filterSwiftSources(new SourcePathResolver(new SourcePathRuleFinder(resolver)), args.srcs).isEmpty();
        return hasSwiftSource ? Optional.of(resolver.requireRule(buildTarget.withAppendedFlavors(SWIFT_COMPANION_FLAVOR))) : Optional.empty();
    }
    final SwiftLibraryDescription.Arg delegateArgs = createUnpopulatedConstructorArg();
    SwiftDescriptions.populateSwiftLibraryDescriptionArg(new SourcePathResolver(new SourcePathRuleFinder(resolver)), delegateArgs, args, buildTarget);
    if (!delegateArgs.srcs.isEmpty()) {
        return Optional.of(resolver.addToIndex(createBuildRule(targetGraph, params, resolver, delegateArgs)));
    } else {
        return Optional.empty();
    }
}
Also used : BuildTarget(com.facebook.buck.model.BuildTarget) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder)

Aggregations

SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)517 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)466 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)405 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)376 Test (org.junit.Test)352 BuildTarget (com.facebook.buck.model.BuildTarget)250 BuildRule (com.facebook.buck.rules.BuildRule)188 SourcePath (com.facebook.buck.rules.SourcePath)151 Path (java.nio.file.Path)151 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)149 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)139 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)131 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)94 PathSourcePath (com.facebook.buck.rules.PathSourcePath)92 TargetGraph (com.facebook.buck.rules.TargetGraph)91 FakeBuildRuleParamsBuilder (com.facebook.buck.rules.FakeBuildRuleParamsBuilder)87 FakeBuildRule (com.facebook.buck.rules.FakeBuildRule)73 RuleKey (com.facebook.buck.rules.RuleKey)72 ImmutableList (com.google.common.collect.ImmutableList)52 ExecutionContext (com.facebook.buck.step.ExecutionContext)50