use of com.facebook.buck.rules.SourcePathResolver in project buck by facebook.
the class PythonPackagedBinary method getBuildSteps.
@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
ImmutableList.Builder<Step> steps = ImmutableList.builder();
Path binPath = context.getSourcePathResolver().getRelativePath(getSourcePathToOutput());
// Make sure the parent directory exists.
steps.add(new MkdirStep(getProjectFilesystem(), binPath.getParent()));
// Delete any other pex that was there (when switching between pex styles).
steps.add(new RmStep(getProjectFilesystem(), binPath, RmStep.Mode.RECURSIVE));
Path workingDirectory = BuildTargets.getGenPath(getProjectFilesystem(), getBuildTarget(), "__%s__working_directory");
steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), workingDirectory));
SourcePathResolver resolver = context.getSourcePathResolver();
// Generate and return the PEX build step.
steps.add(new PexStep(getProjectFilesystem(), builder.getEnvironment(resolver), ImmutableList.<String>builder().addAll(builder.getCommandPrefix(resolver)).addAll(buildArgs).build(), pythonEnvironment.getPythonPath(), pythonEnvironment.getPythonVersion(), workingDirectory, binPath, mainModule, resolver.getMappedPaths(getComponents().getModules()), resolver.getMappedPaths(getComponents().getResources()), resolver.getMappedPaths(getComponents().getNativeLibraries()), ImmutableSet.copyOf(resolver.getAllAbsolutePaths(getComponents().getPrebuiltLibraries())), preloadLibraries, getComponents().isZipSafe().orElse(true)));
// Record the executable package for caching.
buildableContext.recordArtifact(binPath);
return steps.build();
}
use of com.facebook.buck.rules.SourcePathResolver 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);
}
use of com.facebook.buck.rules.SourcePathResolver 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);
}
};
}
use of com.facebook.buck.rules.SourcePathResolver 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());
}
use of com.facebook.buck.rules.SourcePathResolver 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();
}
}
Aggregations