use of com.facebook.buck.rules.SourcePath in project buck by facebook.
the class JavacOptionsFactory method create.
public static JavacOptions create(JavacOptions defaultOptions, BuildRuleParams params, BuildRuleResolver resolver, SourcePathRuleFinder ruleFinder, JvmLibraryArg jvmLibraryArg) {
if ((jvmLibraryArg.source.isPresent() || jvmLibraryArg.target.isPresent()) && jvmLibraryArg.javaVersion.isPresent()) {
throw new HumanReadableException("Please set either source and target or java_version.");
}
JavacOptions.Builder builder = JavacOptions.builder(defaultOptions);
if (jvmLibraryArg.javaVersion.isPresent()) {
builder.setSourceLevel(jvmLibraryArg.javaVersion.get());
builder.setTargetLevel(jvmLibraryArg.javaVersion.get());
}
if (jvmLibraryArg.source.isPresent()) {
builder.setSourceLevel(jvmLibraryArg.source.get());
}
if (jvmLibraryArg.target.isPresent()) {
builder.setTargetLevel(jvmLibraryArg.target.get());
}
if (jvmLibraryArg.generateAbiFromSource.isPresent() && !jvmLibraryArg.generateAbiFromSource.get()) {
// This parameter can only be used to turn off ABI generation from source where it would
// otherwise be employed.
builder.setAbiGenerationMode(AbstractJavacOptions.AbiGenerationMode.CLASS);
}
builder.addAllExtraArguments(jvmLibraryArg.extraArguments);
builder.addAllClassesToRemoveFromJar(jvmLibraryArg.removeClasses);
if (jvmLibraryArg.compiler.isPresent()) {
Either<BuiltInJavac, SourcePath> either = jvmLibraryArg.compiler.get();
if (either.isRight()) {
SourcePath sourcePath = either.getRight();
Optional<BuildRule> possibleRule = ruleFinder.getRule(sourcePath);
if (possibleRule.isPresent() && possibleRule.get() instanceof PrebuiltJar) {
builder.setJavacJarPath(((PrebuiltJar) possibleRule.get()).getSourcePathToOutput());
} else {
builder.setJavacPath(Either.ofRight(sourcePath));
}
}
} else {
if (jvmLibraryArg.javac.isPresent() || jvmLibraryArg.javacJar.isPresent()) {
if (jvmLibraryArg.javac.isPresent() && jvmLibraryArg.javacJar.isPresent()) {
throw new HumanReadableException("Cannot set both javac and javacjar");
}
builder.setJavacPath(jvmLibraryArg.javac.map(Either::ofLeft));
builder.setJavacJarPath(jvmLibraryArg.javacJar);
builder.setCompilerClassName(jvmLibraryArg.compilerClassName);
}
}
AnnotationProcessingParams annotationParams = jvmLibraryArg.buildAnnotationProcessingParams(params.getBuildTarget(), params.getProjectFilesystem(), resolver, defaultOptions.getSafeAnnotationProcessors());
builder.setAnnotationProcessingParams(annotationParams);
return builder.build();
}
use of com.facebook.buck.rules.SourcePath 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.SourcePath 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();
}
use of com.facebook.buck.rules.SourcePath 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.SourcePath in project buck by facebook.
the class CxxPythonExtensionDescription method getExtensionArgs.
private ImmutableList<com.facebook.buck.rules.args.Arg> getExtensionArgs(BuildRuleParams params, BuildRuleResolver ruleResolver, SourcePathResolver pathResolver, SourcePathRuleFinder ruleFinder, CxxPlatform cxxPlatform, Arg args) throws NoSuchBuildTargetException {
// Extract all C/C++ sources from the constructor arg.
ImmutableMap<String, CxxSource> srcs = CxxDescriptionEnhancer.parseCxxSources(params.getBuildTarget(), ruleResolver, ruleFinder, pathResolver, cxxPlatform, args);
ImmutableMap<Path, SourcePath> headers = CxxDescriptionEnhancer.parseHeaders(params.getBuildTarget(), ruleResolver, ruleFinder, pathResolver, Optional.of(cxxPlatform), args);
// Setup the header symlink tree and combine all the preprocessor input from this rule
// and all dependencies.
HeaderSymlinkTree headerSymlinkTree = CxxDescriptionEnhancer.requireHeaderSymlinkTree(params, ruleResolver, cxxPlatform, headers, HeaderVisibility.PRIVATE, true);
Optional<SymlinkTree> sandboxTree = Optional.empty();
if (cxxBuckConfig.sandboxSources()) {
sandboxTree = CxxDescriptionEnhancer.createSandboxTree(params, ruleResolver, cxxPlatform);
}
ImmutableList<CxxPreprocessorInput> cxxPreprocessorInput = CxxDescriptionEnhancer.collectCxxPreprocessorInput(params, cxxPlatform, CxxFlags.getLanguageFlags(args.preprocessorFlags, args.platformPreprocessorFlags, args.langPreprocessorFlags, cxxPlatform), ImmutableList.of(headerSymlinkTree), ImmutableSet.of(), CxxPreprocessables.getTransitiveCxxPreprocessorInput(cxxPlatform, params.getDeps()), args.includeDirs, sandboxTree);
// Generate rule to build the object files.
ImmutableMap<CxxPreprocessAndCompile, SourcePath> picObjects = CxxSourceRuleFactory.requirePreprocessAndCompileRules(params, ruleResolver, pathResolver, ruleFinder, cxxBuckConfig, cxxPlatform, cxxPreprocessorInput, CxxFlags.getLanguageFlags(args.compilerFlags, args.platformCompilerFlags, args.langCompilerFlags, cxxPlatform), args.prefixHeader, args.precompiledHeader, srcs, CxxSourceRuleFactory.PicType.PIC, sandboxTree);
ImmutableList.Builder<com.facebook.buck.rules.args.Arg> argsBuilder = ImmutableList.builder();
argsBuilder.addAll(CxxDescriptionEnhancer.toStringWithMacrosArgs(params.getBuildTarget(), params.getCellRoots(), ruleResolver, cxxPlatform, CxxFlags.getFlagsWithMacrosWithPlatformMacroExpansion(args.linkerFlags, args.platformLinkerFlags, cxxPlatform)));
// Embed a origin-relative library path into the binary so it can find the shared libraries.
argsBuilder.addAll(StringArg.from(Linkers.iXlinker("-rpath", String.format("%s/", cxxPlatform.getLd().resolve(ruleResolver).libOrigin()))));
// Add object files into the args.
argsBuilder.addAll(SourcePathArg.from(picObjects.values()));
return argsBuilder.build();
}
Aggregations