use of com.facebook.buck.model.BuildTarget in project buck by facebook.
the class CopyResourcesStep method buildSteps.
@VisibleForTesting
ImmutableList<Step> buildSteps() {
ImmutableList.Builder<Step> allSteps = ImmutableList.builder();
if (resources.isEmpty()) {
return allSteps.build();
}
String targetPackageDir = javaPackageFinder.findJavaPackage(target);
for (SourcePath rawResource : resources) {
// If the path to the file defining this rule were:
// "first-party/orca/lib-http/tests/com/facebook/orca/BUCK"
//
// And the value of resource were:
// "first-party/orca/lib-http/tests/com/facebook/orca/protocol/base/batch_exception1.txt"
//
// Assuming that `src_roots = tests` were in the [java] section of the .buckconfig file,
// then javaPackageAsPath would be:
// "com/facebook/orca/protocol/base/"
//
// And the path that we would want to copy to the classes directory would be:
// "com/facebook/orca/protocol/base/batch_exception1.txt"
//
// Therefore, some path-wrangling is required to produce the correct string.
Optional<BuildRule> underlyingRule = ruleFinder.getRule(rawResource);
Path relativePathToResource = resolver.getRelativePath(rawResource);
String resource;
if (underlyingRule.isPresent()) {
BuildTarget underlyingTarget = underlyingRule.get().getBuildTarget();
if (underlyingRule.get() instanceof HasOutputName) {
resource = MorePaths.pathWithUnixSeparators(underlyingTarget.getBasePath().resolve(((HasOutputName) underlyingRule.get()).getOutputName()));
} else {
Path genOutputParent = BuildTargets.getGenPath(filesystem, underlyingTarget, "%s").getParent();
Path scratchOutputParent = BuildTargets.getScratchPath(filesystem, underlyingTarget, "%s").getParent();
Optional<Path> outputPath = MorePaths.stripPrefix(relativePathToResource, genOutputParent).map(Optional::of).orElse(MorePaths.stripPrefix(relativePathToResource, scratchOutputParent));
Preconditions.checkState(outputPath.isPresent(), "%s is used as a resource but does not output to a default output directory", underlyingTarget.getFullyQualifiedName());
resource = MorePaths.pathWithUnixSeparators(underlyingTarget.getBasePath().resolve(outputPath.get()));
}
} else {
resource = MorePaths.pathWithUnixSeparators(relativePathToResource);
}
Path javaPackageAsPath = javaPackageFinder.findJavaPackageFolder(outputDirectory.getFileSystem().getPath(resource));
Path relativeSymlinkPath;
if ("".equals(javaPackageAsPath.toString())) {
// In this case, the project root is acting as the default package, so the resource path
// works fine.
relativeSymlinkPath = relativePathToResource.getFileName();
} else {
int lastIndex = resource.lastIndexOf(MorePaths.pathWithUnixSeparatorsAndTrailingSlash(javaPackageAsPath));
if (lastIndex < 0) {
Preconditions.checkState(rawResource instanceof BuildTargetSourcePath, "If resource path %s does not contain %s, then it must be a BuildTargetSourcePath.", relativePathToResource, javaPackageAsPath);
// Handle the case where we depend on the output of another BuildRule. In that case, just
// grab the output and put in the same package as this target would be in.
relativeSymlinkPath = outputDirectory.getFileSystem().getPath(String.format("%s%s%s", targetPackageDir, targetPackageDir.isEmpty() ? "" : "/", resolver.getRelativePath(rawResource).getFileName()));
} else {
relativeSymlinkPath = outputDirectory.getFileSystem().getPath(resource.substring(lastIndex));
}
}
Path target = outputDirectory.resolve(relativeSymlinkPath);
MkdirAndSymlinkFileStep link = new MkdirAndSymlinkFileStep(filesystem, resolver.getAbsolutePath(rawResource), target);
allSteps.add(link);
}
return allSteps.build();
}
use of com.facebook.buck.model.BuildTarget in project buck by facebook.
the class DefaultJavaLibrary method getBuildSteps.
/**
* Building a java_library() rule entails compiling the .java files specified in the srcs
* attribute. They are compiled into a directory under {@link BuckPaths#getScratchDir()}.
*/
@Override
public final ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
ImmutableList.Builder<Step> steps = ImmutableList.builder();
FluentIterable<JavaLibrary> declaredClasspathDeps = JavaLibraryClasspathProvider.getJavaLibraryDeps(getDepsForTransitiveClasspathEntries());
// Always create the output directory, even if there are no .java files to compile because there
// might be resources that need to be copied there.
BuildTarget target = getBuildTarget();
Path outputDirectory = getClassesDir(target, getProjectFilesystem());
steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), outputDirectory));
SuggestBuildRules suggestBuildRule = DefaultSuggestBuildRules.createSuggestBuildFunction(JAR_RESOLVER, context.getSourcePathResolver(), declaredClasspathDeps.toSet(), ImmutableSet.<JavaLibrary>builder().addAll(getTransitiveClasspathDeps()).add(this).build(), context.getActionGraph().getNodes());
// We don't want to add these to the declared or transitive deps, since they're only used at
// compile time.
Collection<Path> provided = JavaLibraryClasspathProvider.getJavaLibraryDeps(providedDeps).transformAndConcat(JavaLibrary::getOutputClasspaths).filter(Objects::nonNull).transform(context.getSourcePathResolver()::getAbsolutePath).toSet();
Iterable<Path> declaredClasspaths = declaredClasspathDeps.transformAndConcat(JavaLibrary::getOutputClasspaths).transform(context.getSourcePathResolver()::getAbsolutePath);
// Only override the bootclasspath if this rule is supposed to compile Android code.
ImmutableSortedSet<Path> declared = ImmutableSortedSet.<Path>naturalOrder().addAll(declaredClasspaths).addAll(additionalClasspathEntries.stream().map(e -> e.isLeft() ? context.getSourcePathResolver().getAbsolutePath(e.getLeft()) : checkIsAbsolute(e.getRight())).collect(MoreCollectors.toImmutableSet())).addAll(provided).build();
// Make sure that this directory exists because ABI information will be written here.
Step mkdir = new MakeCleanDirectoryStep(getProjectFilesystem(), getPathToAbiOutputDir());
steps.add(mkdir);
// If there are resources, then link them to the appropriate place in the classes directory.
JavaPackageFinder finder = context.getJavaPackageFinder();
if (resourcesRoot.isPresent()) {
finder = new ResourcesRootPackageFinder(resourcesRoot.get(), finder);
}
steps.add(new CopyResourcesStep(getProjectFilesystem(), context.getSourcePathResolver(), ruleFinder, target, resources, outputDirectory, finder));
steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), getOutputJarDirPath(target, getProjectFilesystem())));
// into the built jar.
if (!getJavaSrcs().isEmpty()) {
ClassUsageFileWriter usedClassesFileWriter;
if (trackClassUsage) {
final Path usedClassesFilePath = getUsedClassesFilePath(getBuildTarget(), getProjectFilesystem());
depFileOutputPath = getProjectFilesystem().getPathForRelativePath(usedClassesFilePath);
usedClassesFileWriter = new DefaultClassUsageFileWriter(usedClassesFilePath);
buildableContext.recordArtifact(usedClassesFilePath);
} else {
usedClassesFileWriter = NoOpClassUsageFileWriter.instance();
}
// This adds the javac command, along with any supporting commands.
Path pathToSrcsList = BuildTargets.getGenPath(getProjectFilesystem(), getBuildTarget(), "__%s__srcs");
steps.add(new MkdirStep(getProjectFilesystem(), pathToSrcsList.getParent()));
Path scratchDir = BuildTargets.getGenPath(getProjectFilesystem(), target, "lib__%s____working_directory");
steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), scratchDir));
Optional<Path> workingDirectory = Optional.of(scratchDir);
ImmutableSortedSet<Path> javaSrcs = getJavaSrcs().stream().map(context.getSourcePathResolver()::getRelativePath).collect(MoreCollectors.toImmutableSortedSet());
compileStepFactory.createCompileToJarStep(context, javaSrcs, target, context.getSourcePathResolver(), ruleFinder, getProjectFilesystem(), declared, outputDirectory, workingDirectory, pathToSrcsList, Optional.of(suggestBuildRule), postprocessClassesCommands, ImmutableSortedSet.of(outputDirectory), /* mainClass */
Optional.empty(), manifestFile.map(context.getSourcePathResolver()::getAbsolutePath), outputJar.get(), usedClassesFileWriter, /* output params */
steps, buildableContext, classesToRemoveFromJar);
}
if (outputJar.isPresent()) {
Path output = outputJar.get();
// No source files, only resources
if (getJavaSrcs().isEmpty()) {
steps.add(new JarDirectoryStep(getProjectFilesystem(), output, ImmutableSortedSet.of(outputDirectory), /* mainClass */
null, manifestFile.map(context.getSourcePathResolver()::getAbsolutePath).orElse(null), true, classesToRemoveFromJar));
}
buildableContext.recordArtifact(output);
}
JavaLibraryRules.addAccumulateClassNamesStep(this, buildableContext, context.getSourcePathResolver(), steps);
return steps.build();
}
use of com.facebook.buck.model.BuildTarget in project buck by facebook.
the class AbstractLuaScriptStarter method build.
@Override
public SourcePath build() {
BuildTarget templateTarget = BuildTarget.builder(getBaseParams().getBuildTarget()).addFlavors(InternalFlavor.of("starter-template")).build();
WriteFile templateRule = getRuleResolver().addToIndex(new WriteFile(getBaseParams().withBuildTarget(templateTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.of())), getPureStarterTemplate(), BuildTargets.getGenPath(getBaseParams().getProjectFilesystem(), templateTarget, "%s/starter.lua.in"), /* executable */
false));
final Tool lua = getLuaConfig().getLua(getRuleResolver());
WriteStringTemplateRule writeStringTemplateRule = getRuleResolver().addToIndex(WriteStringTemplateRule.from(getBaseParams(), getRuleFinder(), getTarget(), getOutput(), templateRule.getSourcePathToOutput(), ImmutableMap.of("SHEBANG", lua.getCommandPrefix(getPathResolver()).get(0), "MAIN_MODULE", Escaper.escapeAsPythonString(getMainModule()), "MODULES_DIR", getRelativeModulesDir().isPresent() ? Escaper.escapeAsPythonString(getRelativeModulesDir().get().toString()) : "nil", "PY_MODULES_DIR", getRelativePythonModulesDir().isPresent() ? Escaper.escapeAsPythonString(getRelativePythonModulesDir().get().toString()) : "nil", "EXT_SUFFIX", Escaper.escapeAsPythonString(getCxxPlatform().getSharedLibraryExtension())), /* executable */
true));
return writeStringTemplateRule.getSourcePathToOutput();
}
use of com.facebook.buck.model.BuildTarget in project buck by facebook.
the class AbstractNativeExecutableStarter method build.
@Override
public SourcePath build() throws NoSuchBuildTargetException {
BuildTarget linkTarget = getTarget();
CxxLink linkRule = getRuleResolver().addToIndex(CxxLinkableEnhancer.createCxxLinkableBuildRule(getCxxBuckConfig(), getCxxPlatform(), getBaseParams(), getRuleResolver(), getPathResolver(), getRuleFinder(), linkTarget, Linker.LinkType.EXECUTABLE, Optional.empty(), getOutput(), Linker.LinkableDepType.SHARED, getNativeStarterDeps(), Optional.empty(), Optional.empty(), ImmutableSet.of(), getNativeLinkableInput()));
return linkRule.getSourcePathToOutput();
}
use of com.facebook.buck.model.BuildTarget 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());
}
Aggregations