use of com.facebook.buck.step.Step in project buck by facebook.
the class GoCompile method getBuildSteps.
@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
buildableContext.recordArtifact(output);
ImmutableList.Builder<Path> compileSrcListBuilder = ImmutableList.builder();
ImmutableList.Builder<Path> headerSrcListBuilder = ImmutableList.builder();
ImmutableList.Builder<Path> asmSrcListBuilder = ImmutableList.builder();
for (SourcePath path : srcs) {
Path srcPath = context.getSourcePathResolver().getAbsolutePath(path);
String extension = MorePaths.getFileExtension(srcPath).toLowerCase();
if (extension.equals("s")) {
asmSrcListBuilder.add(srcPath);
} else if (extension.equals("go")) {
compileSrcListBuilder.add(srcPath);
} else {
headerSrcListBuilder.add(srcPath);
}
}
ImmutableList<Path> compileSrcs = compileSrcListBuilder.build();
ImmutableList<Path> headerSrcs = headerSrcListBuilder.build();
ImmutableList<Path> asmSrcs = asmSrcListBuilder.build();
ImmutableList.Builder<Step> steps = ImmutableList.builder();
steps.add(new MkdirStep(getProjectFilesystem(), output.getParent()));
Optional<Path> asmHeaderPath;
if (!asmSrcs.isEmpty()) {
asmHeaderPath = Optional.of(BuildTargets.getScratchPath(getProjectFilesystem(), getBuildTarget(), "%s/" + getBuildTarget().getShortName() + "__asm_hdr").resolve("go_asm.h"));
steps.add(new MkdirStep(getProjectFilesystem(), asmHeaderPath.get().getParent()));
} else {
asmHeaderPath = Optional.empty();
}
boolean allowExternalReferences = !asmSrcs.isEmpty();
if (compileSrcs.isEmpty()) {
steps.add(new TouchStep(getProjectFilesystem(), output));
} else {
steps.add(new GoCompileStep(getProjectFilesystem().getRootPath(), compiler.getEnvironment(context.getSourcePathResolver()), compiler.getCommandPrefix(context.getSourcePathResolver()), compilerFlags, packageName, compileSrcs, importPathMap, ImmutableList.of(symlinkTree.getRoot()), asmHeaderPath, allowExternalReferences, platform, output));
}
if (!asmSrcs.isEmpty()) {
Path asmIncludeDir = BuildTargets.getScratchPath(getProjectFilesystem(), getBuildTarget(), "%s/" + getBuildTarget().getShortName() + "__asm_includes");
steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), asmIncludeDir));
if (!headerSrcs.isEmpty()) {
// TODO(mikekap): Allow header-map style input.
for (Path header : FluentIterable.from(headerSrcs).append(asmSrcs)) {
steps.add(new SymlinkFileStep(getProjectFilesystem(), header, asmIncludeDir.resolve(header.getFileName()), /* useAbsolutePaths */
true));
}
}
Path asmOutputDir = BuildTargets.getScratchPath(getProjectFilesystem(), getBuildTarget(), "%s/" + getBuildTarget().getShortName() + "__asm_compile");
steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), asmOutputDir));
ImmutableList.Builder<Path> asmOutputs = ImmutableList.builder();
for (Path asmSrc : asmSrcs) {
Path outputPath = asmOutputDir.resolve(asmSrc.getFileName().toString().replaceAll("\\.[sS]$", ".o"));
steps.add(new GoAssembleStep(getProjectFilesystem().getRootPath(), assembler.getEnvironment(context.getSourcePathResolver()), assembler.getCommandPrefix(context.getSourcePathResolver()), assemblerFlags, asmSrc, ImmutableList.<Path>builder().addAll(assemblerIncludeDirs).add(asmHeaderPath.get().getParent()).add(asmIncludeDir).build(), platform, outputPath));
asmOutputs.add(outputPath);
}
steps.add(new GoPackStep(getProjectFilesystem().getRootPath(), packer.getEnvironment(context.getSourcePathResolver()), packer.getCommandPrefix(context.getSourcePathResolver()), GoPackStep.Operation.APPEND, asmOutputs.build(), output));
}
return steps.build();
}
use of com.facebook.buck.step.Step 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.step.Step 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.step.Step in project buck by facebook.
the class BaseCompileToJarStepFactory method addPostprocessClassesCommands.
/**
* Adds a BashStep for each postprocessClasses command that runs the command followed by the
* outputDirectory of javac outputs.
*
* The expectation is that the command will inspect and update the directory by
* modifying, adding, and deleting the .class files in the directory.
*
* The outputDirectory should be a valid java root. I.e., if outputDirectory
* is buck-out/bin/java/abc/lib__abc__classes/, then a contained class abc.AbcModule
* should be at buck-out/bin/java/abc/lib__abc__classes/abc/AbcModule.class
*
* @param filesystem the project filesystem.
* @param postprocessClassesCommands the list of commands to post-process .class files.
* @param outputDirectory the directory that will contain all the javac output.
* @param declaredClasspathEntries the list of classpath entries.
* @param bootClasspath the compilation boot classpath.
*/
@VisibleForTesting
static ImmutableList<Step> addPostprocessClassesCommands(ProjectFilesystem filesystem, List<String> postprocessClassesCommands, Path outputDirectory, ImmutableSortedSet<Path> declaredClasspathEntries, Optional<String> bootClasspath) {
if (postprocessClassesCommands.isEmpty()) {
return ImmutableList.of();
}
ImmutableList.Builder<Step> commands = new ImmutableList.Builder<Step>();
ImmutableMap.Builder<String, String> envVarBuilder = ImmutableMap.builder();
envVarBuilder.put("COMPILATION_CLASSPATH", Joiner.on(':').join(Iterables.transform(declaredClasspathEntries, filesystem::resolve)));
if (bootClasspath.isPresent()) {
envVarBuilder.put("COMPILATION_BOOTCLASSPATH", bootClasspath.get());
}
ImmutableMap<String, String> envVars = envVarBuilder.build();
for (final String postprocessClassesCommand : postprocessClassesCommands) {
BashStep bashStep = new BashStep(filesystem.getRootPath(), postprocessClassesCommand + " " + outputDirectory) {
@Override
public ImmutableMap<String, String> getEnvironmentVariables(ExecutionContext context) {
return envVars;
}
};
commands.add(bashStep);
}
return commands.build();
}
use of com.facebook.buck.step.Step in project buck by facebook.
the class OcamlBuildStep method executeCCompilation.
private StepExecutionResult executeCCompilation(ExecutionContext context, ImmutableList.Builder<Path> linkerInputs) throws IOException, InterruptedException {
ImmutableList.Builder<String> cCompileFlags = ImmutableList.builder();
cCompileFlags.addAll(ocamlContext.getCCompileFlags());
cCompileFlags.addAll(ocamlContext.getCommonCFlags());
CxxPreprocessorInput cxxPreprocessorInput = ocamlContext.getCxxPreprocessorInput();
for (SourcePath cSrc : ocamlContext.getCInput()) {
Path outputPath = ocamlContext.getCOutput(resolver.getAbsolutePath(cSrc));
linkerInputs.add(outputPath);
Step compileStep = new OcamlCCompileStep(resolver, filesystem.getRootPath(), new OcamlCCompileStep.Args(cCompilerEnvironment, cCompiler, ocamlContext.getOcamlCompiler().get(), ocamlContext.getOcamlInteropIncludesDir(), outputPath, cSrc, cCompileFlags.build(), cxxPreprocessorInput.getIncludes()));
StepExecutionResult compileExecutionResult = compileStep.execute(context);
if (!compileExecutionResult.isSuccess()) {
return compileExecutionResult;
}
}
return StepExecutionResult.SUCCESS;
}
Aggregations