use of com.facebook.buck.rules.BuildRule in project buck by facebook.
the class AbstractNativeExecutableStarter method getNativeStarterCxxSource.
private CxxSource getNativeStarterCxxSource() {
BuildTarget target = BuildTarget.builder(getBaseParams().getBuildTarget()).addFlavors(InternalFlavor.of("native-starter-cxx-source")).build();
BuildRule rule;
Optional<BuildRule> maybeRule = getRuleResolver().getRuleOptional(target);
if (maybeRule.isPresent()) {
rule = maybeRule.get();
} else {
BuildTarget templateTarget = BuildTarget.builder(getBaseParams().getBuildTarget()).addFlavors(InternalFlavor.of("native-starter-cxx-source-template")).build();
WriteFile templateRule = getRuleResolver().addToIndex(new WriteFile(getBaseParams().withBuildTarget(templateTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.of())), getNativeStarterCxxSourceTemplate(), BuildTargets.getGenPath(getBaseParams().getProjectFilesystem(), templateTarget, "%s/native-starter.cpp.in"), /* executable */
false));
Path output = BuildTargets.getGenPath(getBaseParams().getProjectFilesystem(), target, "%s/native-starter.cpp");
rule = getRuleResolver().addToIndex(WriteStringTemplateRule.from(getBaseParams(), getRuleFinder(), target, output, templateRule.getSourcePathToOutput(), ImmutableMap.of("MAIN_MODULE", Escaper.escapeAsPythonString(getMainModule()), "MODULES_DIR", getRelativeModulesDir().isPresent() ? Escaper.escapeAsPythonString(getRelativeModulesDir().get().toString()) : "NULL", "PY_MODULES_DIR", getRelativePythonModulesDir().isPresent() ? Escaper.escapeAsPythonString(getRelativePythonModulesDir().get().toString()) : "NULL", "EXT_SUFFIX", Escaper.escapeAsPythonString(getCxxPlatform().getSharedLibraryExtension())), /* executable */
false));
}
return CxxSource.of(CxxSource.Type.CXX, Preconditions.checkNotNull(rule.getSourcePathToOutput()), ImmutableList.of());
}
use of com.facebook.buck.rules.BuildRule in project buck by facebook.
the class LuaBinaryDescription method getPackageComponentsFromDeps.
private LuaBinaryPackageComponents getPackageComponentsFromDeps(BuildRuleParams baseParams, BuildRuleResolver ruleResolver, SourcePathResolver pathResolver, SourcePathRuleFinder ruleFinder, final CxxPlatform cxxPlatform, final PythonPlatform pythonPlatform, Optional<BuildTarget> nativeStarterLibrary, String mainModule, LuaConfig.PackageStyle packageStyle, Iterable<BuildRule> deps) throws NoSuchBuildTargetException {
final LuaPackageComponents.Builder builder = LuaPackageComponents.builder();
final OmnibusRoots.Builder omnibusRoots = OmnibusRoots.builder(cxxPlatform, ImmutableSet.of());
final Map<BuildTarget, NativeLinkable> nativeLinkableRoots = new LinkedHashMap<>();
final Map<BuildTarget, CxxLuaExtension> luaExtensions = new LinkedHashMap<>();
final Map<BuildTarget, CxxPythonExtension> pythonExtensions = new LinkedHashMap<>();
// Walk the deps to find all Lua packageables and native linkables.
new AbstractBreadthFirstThrowingTraversal<BuildRule, NoSuchBuildTargetException>(deps) {
private final ImmutableSet<BuildRule> empty = ImmutableSet.of();
@Override
public ImmutableSet<BuildRule> visit(BuildRule rule) throws NoSuchBuildTargetException {
ImmutableSet<BuildRule> deps = empty;
if (rule instanceof LuaPackageable) {
LuaPackageable packageable = (LuaPackageable) rule;
LuaPackageComponents components = packageable.getLuaPackageComponents();
LuaPackageComponents.addComponents(builder, components);
if (components.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 CxxPythonExtension) {
CxxPythonExtension extension = (CxxPythonExtension) rule;
NativeLinkTarget target = extension.getNativeLinkTarget(pythonPlatform);
pythonExtensions.put(target.getBuildTarget(), (CxxPythonExtension) rule);
omnibusRoots.addIncludedRoot(target);
} else if (rule instanceof PythonPackagable) {
PythonPackagable packageable = (PythonPackagable) rule;
PythonPackageComponents components = packageable.getPythonPackageComponents(pythonPlatform, cxxPlatform);
builder.putAllPythonModules(MoreMaps.transformKeys(components.getModules(), Object::toString));
builder.putAllNativeLibraries(MoreMaps.transformKeys(components.getNativeLibraries(), Object::toString));
if (components.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 CxxLuaExtension) {
CxxLuaExtension extension = (CxxLuaExtension) rule;
luaExtensions.put(extension.getBuildTarget(), extension);
omnibusRoots.addIncludedRoot(extension);
} else if (rule instanceof NativeLinkable) {
NativeLinkable linkable = (NativeLinkable) rule;
nativeLinkableRoots.put(linkable.getBuildTarget(), linkable);
omnibusRoots.addPotentialRoot(linkable);
}
return deps;
}
}.start();
// Build the starter.
Starter starter = createStarter(baseParams, ruleResolver, pathResolver, ruleFinder, cxxPlatform, nativeStarterLibrary, mainModule, packageStyle, !nativeLinkableRoots.isEmpty() || !omnibusRoots.isEmpty());
SourcePath starterPath = null;
if (luaConfig.getNativeLinkStrategy() == NativeLinkStrategy.MERGED) {
// If we're using a native starter, include it in omnibus linking.
if (starter instanceof NativeExecutableStarter) {
NativeExecutableStarter nativeStarter = (NativeExecutableStarter) starter;
omnibusRoots.addIncludedRoot(nativeStarter);
}
// Build the omnibus libraries.
OmnibusRoots roots = omnibusRoots.build();
OmnibusLibraries libraries = Omnibus.getSharedLibraries(baseParams, ruleResolver, ruleFinder, cxxBuckConfig, cxxPlatform, ImmutableList.of(), roots.getIncludedRoots().values(), roots.getExcludedRoots().values());
// Add all the roots from the omnibus link. If it's an extension, add it as a module.
for (Map.Entry<BuildTarget, OmnibusRoot> root : libraries.getRoots().entrySet()) {
// If it's a Lua extension add it as a module.
CxxLuaExtension luaExtension = luaExtensions.get(root.getKey());
if (luaExtension != null) {
builder.putModules(luaExtension.getModule(cxxPlatform), root.getValue().getPath());
continue;
}
// If it's a Python extension, add it as a python module.
CxxPythonExtension pythonExtension = pythonExtensions.get(root.getKey());
if (pythonExtension != null) {
builder.putPythonModules(pythonExtension.getModule().toString(), root.getValue().getPath());
continue;
}
// A root named after the top-level target is our native starter.
if (root.getKey().equals(baseParams.getBuildTarget())) {
starterPath = root.getValue().getPath();
continue;
}
// Otherwise, add it as a native library.
NativeLinkTarget target = Preconditions.checkNotNull(roots.getIncludedRoots().get(root.getKey()), "%s: linked unexpected omnibus root: %s", baseParams.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", baseParams.getBuildTarget(), root.getKey());
builder.putNativeLibraries(soname, root.getValue().getPath());
}
// Add all remaining libraries as native libraries.
for (OmnibusLibrary library : libraries.getLibraries()) {
builder.putNativeLibraries(library.getSoname(), library.getPath());
}
} else {
// roots.
for (Map.Entry<BuildTarget, CxxLuaExtension> entry : luaExtensions.entrySet()) {
CxxLuaExtension extension = entry.getValue();
builder.putModules(extension.getModule(cxxPlatform), extension.getExtension(cxxPlatform));
nativeLinkableRoots.putAll(Maps.uniqueIndex(extension.getNativeLinkTargetDeps(cxxPlatform), NativeLinkable::getBuildTarget));
}
// Add in native executable deps.
if (starter instanceof NativeExecutableStarter) {
NativeExecutableStarter executableStarter = (NativeExecutableStarter) starter;
nativeLinkableRoots.putAll(Maps.uniqueIndex(executableStarter.getNativeStarterDeps(), NativeLinkable::getBuildTarget));
}
// python-platform specific deps to the native linkables.
for (Map.Entry<BuildTarget, CxxPythonExtension> entry : pythonExtensions.entrySet()) {
PythonPackageComponents components = entry.getValue().getPythonPackageComponents(pythonPlatform, cxxPlatform);
builder.putAllPythonModules(MoreMaps.transformKeys(components.getModules(), Object::toString));
builder.putAllNativeLibraries(MoreMaps.transformKeys(components.getNativeLibraries(), Object::toString));
nativeLinkableRoots.putAll(Maps.uniqueIndex(entry.getValue().getNativeLinkTarget(pythonPlatform).getNativeLinkTargetDeps(cxxPlatform), NativeLinkable::getBuildTarget));
}
// Add shared libraries from all native linkables.
for (NativeLinkable nativeLinkable : NativeLinkables.getTransitiveNativeLinkables(cxxPlatform, nativeLinkableRoots.values()).values()) {
NativeLinkable.Linkage linkage = nativeLinkable.getPreferredLinkage(cxxPlatform);
if (linkage != NativeLinkable.Linkage.STATIC) {
builder.putAllNativeLibraries(nativeLinkable.getSharedLibraries(cxxPlatform));
}
}
}
// building one directly from the starter.
if (starterPath == null) {
starterPath = starter.build();
}
return LuaBinaryPackageComponents.of(starterPath, builder.build());
}
use of com.facebook.buck.rules.BuildRule in project buck by facebook.
the class Project method writeJsonConfig.
private void writeJsonConfig(File jsonTempFile, List<SerializableModule> modules) throws IOException {
List<SerializablePrebuiltJarRule> libraries = Lists.newArrayListWithCapacity(libraryJars.size());
for (BuildRule libraryJar : libraryJars) {
Preconditions.checkState(libraryJar instanceof PrebuiltJar);
String name = getIntellijNameForRule(libraryJar, null);
PrebuiltJar prebuiltJar = (PrebuiltJar) libraryJar;
Path binaryJarAbsolutePath = resolver.getAbsolutePath(prebuiltJar.getSourcePathToOutput());
String binaryJar = MorePaths.pathWithUnixSeparators(projectFilesystem.relativize(binaryJarAbsolutePath));
String sourceJar = prebuiltJar.getSourceJar().map(resolver::getAbsolutePath).map(projectFilesystem::relativize).map(MorePaths::pathWithUnixSeparators).orElse(null);
String javadocUrl = prebuiltJar.getJavadocUrl().orElse(null);
libraries.add(new SerializablePrebuiltJarRule(name, binaryJar, sourceJar, javadocUrl));
}
List<SerializableAndroidAar> aars = Lists.newArrayListWithCapacity(androidAars.size());
for (BuildRule aar : androidAars) {
Preconditions.checkState(aar instanceof AndroidPrebuiltAar);
AndroidPrebuiltAar preBuiltAar = (AndroidPrebuiltAar) aar;
String name = getIntellijNameForAar(preBuiltAar);
aars.add(createSerializableAndroidAar(name, preBuiltAar));
}
writeJsonConfig(jsonTempFile, modules, libraries, aars);
}
use of com.facebook.buck.rules.BuildRule in project buck by facebook.
the class Project method walkRuleAndAdd.
/**
* Walks the dependencies of a build rule and adds the appropriate DependentModules to the
* specified dependencies collection. All library dependencies will be added before any module
* dependencies. See {@code ProjectTest#testThatJarsAreListedBeforeModules()} for details on why
* this behavior is important.
*/
@SuppressWarnings("PMD.LooseCoupling")
private void walkRuleAndAdd(final BuildRule rule, final boolean isForTests, final LinkedHashSet<SerializableDependentModule> dependencies, @Nullable final BuildRule srcTarget) {
final Path basePathForRule = rule.getBuildTarget().getBasePath();
Set<BuildRule> targetsToWalk;
if (rule instanceof JavaTest) {
targetsToWalk = ((JavaTest) rule).getCompiledTestsLibrary().getDeps();
} else {
targetsToWalk = rule.getDeps();
}
new AbstractBreadthFirstTraversal<BuildRule>(targetsToWalk) {
private final LinkedHashSet<SerializableDependentModule> librariesToAdd = Sets.newLinkedHashSet();
private final LinkedHashSet<SerializableDependentModule> modulesToAdd = Sets.newLinkedHashSet();
@Override
public ImmutableSet<BuildRule> visit(BuildRule dep) {
// Hack: we don't want uber R.java to show up in the deps that IntelliJ sees.
if (dep.getBuildTarget().toString().endsWith("_uber_r_dot_java")) {
return ImmutableSet.of();
}
ImmutableSet<BuildRule> depsToVisit;
if (rule.getProperties().is(PACKAGING) || dep instanceof AndroidResource || dep == rule) {
depsToVisit = dep.getDeps();
} else if (dep.getProperties().is(LIBRARY) && dep instanceof ExportDependencies) {
depsToVisit = ((ExportDependencies) dep).getExportedDeps();
} else {
depsToVisit = ImmutableSet.of();
}
// should contain the union of :lib and :test's deps as dependent modules.
if (isForTests && depsToVisit.isEmpty() && dep.getBuildTarget().getBasePath().equals(basePathForRule) && !dep.equals(srcTarget)) {
depsToVisit = dep.getDeps();
}
SerializableDependentModule dependentModule;
if (androidAars.contains(dep)) {
AndroidPrebuiltAar aar = androidAars.getParentAar(dep);
dependentModule = SerializableDependentModule.newLibrary(aar.getBuildTarget(), getIntellijNameForAar(aar));
} else if (dep instanceof PrebuiltJar) {
libraryJars.add(dep);
String libraryName = getIntellijNameForRule(dep);
dependentModule = SerializableDependentModule.newLibrary(dep.getBuildTarget(), libraryName);
} else if (dep instanceof AndroidPrebuiltAar) {
androidAars.add((AndroidPrebuiltAar) dep);
String libraryName = getIntellijNameForAar(dep);
dependentModule = SerializableDependentModule.newLibrary(dep.getBuildTarget(), libraryName);
} else if ((dep instanceof CxxLibrary) || (dep instanceof NdkLibrary) || (dep instanceof JavaLibrary) || (dep instanceof AndroidResource)) {
String moduleName = getIntellijNameForRule(dep);
dependentModule = SerializableDependentModule.newModule(dep.getBuildTarget(), moduleName);
} else {
return depsToVisit;
}
if (librariesToAdd.contains(dependentModule) || modulesToAdd.contains(dependentModule)) {
return depsToVisit;
}
if (isForTests) {
dependentModule.scope = "TEST";
} else {
// If the dependentModule has already been added in the "TEST" scope, then it should be
// removed and then re-added using the current (compile) scope.
String currentScope = dependentModule.scope;
dependentModule.scope = "TEST";
if (dependencies.contains(dependentModule)) {
dependencies.remove(dependentModule);
}
dependentModule.scope = currentScope;
}
// dependencies collection once the traversal is complete in the onComplete() method.
if (dependentModule.isLibrary()) {
librariesToAdd.add(dependentModule);
} else {
modulesToAdd.add(dependentModule);
}
return depsToVisit;
}
@Override
protected void onComplete() {
dependencies.addAll(librariesToAdd);
dependencies.addAll(modulesToAdd);
}
}.start();
}
use of com.facebook.buck.rules.BuildRule in project buck by facebook.
the class ScalaTestDescription method createBuildRule.
@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, final BuildRuleParams rawParams, final BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
if (CalculateAbi.isAbiTarget(rawParams.getBuildTarget())) {
BuildTarget testTarget = CalculateAbi.getLibraryTarget(rawParams.getBuildTarget());
BuildRule testRule = resolver.requireRule(testTarget);
return CalculateAbi.of(rawParams.getBuildTarget(), ruleFinder, rawParams, Preconditions.checkNotNull(testRule.getSourcePathToOutput()));
}
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
final BuildRule scalaLibrary = resolver.getRule(config.getScalaLibraryTarget());
BuildRuleParams params = rawParams.copyReplacingDeclaredAndExtraDeps(() -> ImmutableSortedSet.<BuildRule>naturalOrder().addAll(rawParams.getDeclaredDeps().get()).add(scalaLibrary).build(), rawParams.getExtraDeps());
JavaTestDescription.CxxLibraryEnhancement cxxLibraryEnhancement = new JavaTestDescription.CxxLibraryEnhancement(params, args.useCxxLibraries, args.cxxLibraryWhitelist, resolver, ruleFinder, cxxPlatform);
params = cxxLibraryEnhancement.updatedParams;
Tool scalac = config.getScalac(resolver);
BuildRuleParams javaLibraryParams = params.copyAppendingExtraDeps(Iterables.concat(BuildRules.getExportedRules(Iterables.concat(params.getDeclaredDeps().get(), resolver.getAllRules(args.providedDeps))), scalac.getDeps(ruleFinder))).withAppendedFlavor(JavaTest.COMPILED_TESTS_LIBRARY_FLAVOR);
JavaLibrary testsLibrary = resolver.addToIndex(new DefaultJavaLibrary(javaLibraryParams, pathResolver, ruleFinder, args.srcs, ResourceValidator.validateResources(pathResolver, params.getProjectFilesystem(), args.resources), /* generatedSourceFolderName */
Optional.empty(), /* proguardConfig */
Optional.empty(), /* postprocessClassesCommands */
ImmutableList.of(), /* exportDeps */
ImmutableSortedSet.of(), /* providedDeps */
ImmutableSortedSet.of(), JavaLibraryRules.getAbiInputs(resolver, javaLibraryParams.getDeps()), /* trackClassUsage */
false, /* additionalClasspathEntries */
ImmutableSet.of(), new ScalacToJarStepFactory(scalac, config.getCompilerFlags(), args.extraArguments, ImmutableSet.of()), args.resourcesRoot, args.manifestFile, args.mavenCoords, /* tests */
ImmutableSortedSet.of(), /* classesToRemoveFromJar */
ImmutableSet.of()));
return new JavaTest(params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(testsLibrary)), Suppliers.ofInstance(ImmutableSortedSet.of())), pathResolver, testsLibrary, /* additionalClasspathEntries */
ImmutableSet.of(), args.labels, args.contacts, args.testType.orElse(TestType.JUNIT), javaOptions.getJavaRuntimeLauncher(), args.vmArgs, cxxLibraryEnhancement.nativeLibsEnvironment, args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs), args.testCaseTimeoutMs, args.env, args.runTestSeparately.orElse(false), args.forkMode.orElse(ForkMode.NONE), args.stdOutLogLevel, args.stdErrLogLevel);
}
Aggregations