use of com.facebook.buck.rules.BuildContext in project buck by facebook.
the class DefaultJavaLibraryTest method testStepsPresenceForIntermediateOutputToDiskSpooling.
@Test
public void testStepsPresenceForIntermediateOutputToDiskSpooling() {
BuildTarget buildTarget = BuildTargetFactory.newInstance("//:lib");
BuildRule javaLibraryBuildRule = createDefaultJavaLibraryRuleWithAbiKey(buildTarget, /* srcs */
ImmutableSortedSet.of("foo/Bar.java"), /* deps */
ImmutableSortedSet.of(), /* exportedDeps */
ImmutableSortedSet.of(), Optional.of(AbstractJavacOptions.SpoolMode.INTERMEDIATE_TO_DISK), /* postprocessClassesCommands */
ImmutableList.of());
BuildContext buildContext = createBuildContext(javaLibraryBuildRule, /* bootclasspath */
null);
ImmutableList<Step> steps = javaLibraryBuildRule.getBuildSteps(buildContext, new FakeBuildableContext());
assertThat(steps, Matchers.not(Matchers.hasItem(Matchers.instanceOf(JavacDirectToJarStep.class))));
assertThat(steps, Matchers.hasItem(Matchers.instanceOf(JavacStep.class)));
assertThat(steps, Matchers.hasItem(Matchers.instanceOf(JarDirectoryStep.class)));
}
use of com.facebook.buck.rules.BuildContext in project buck by facebook.
the class Build method executeBuild.
/**
* If {@code isKeepGoing} is false, then this returns a future that succeeds only if all of
* {@code rulesToBuild} build successfully. Otherwise, this returns a future that should always
* succeed, even if individual rules fail to build. In that case, a failed build rule is indicated
* by a {@code null} value in the corresponding position in the iteration order of
* {@code rulesToBuild}.
* @param targetish The targets to build. All targets in this iterable must be unique.
*/
@SuppressWarnings("PMD.EmptyCatchBlock")
public BuildExecutionResult executeBuild(Iterable<? extends BuildTarget> targetish, boolean isKeepGoing) throws IOException, ExecutionException, InterruptedException {
BuildId buildId = executionContext.getBuildId();
BuildEngineBuildContext buildContext = BuildEngineBuildContext.builder().setBuildContext(BuildContext.builder().setActionGraph(actionGraph).setSourcePathResolver(new SourcePathResolver(new SourcePathRuleFinder(ruleResolver))).setJavaPackageFinder(javaPackageFinder).setEventBus(executionContext.getBuckEventBus()).setAndroidPlatformTargetSupplier(executionContext.getAndroidPlatformTargetSupplier()).build()).setClock(clock).setArtifactCache(artifactCache).setBuildId(buildId).setObjectMapper(objectMapper).putAllEnvironment(executionContext.getEnvironment()).setKeepGoing(isKeepGoing).build();
// It is important to use this logic to determine the set of rules to build rather than
// build.getActionGraph().getNodesWithNoIncomingEdges() because, due to graph enhancement,
// there could be disconnected subgraphs in the DependencyGraph that we do not want to build.
ImmutableSet<BuildTarget> targetsToBuild = StreamSupport.stream(targetish.spliterator(), false).collect(MoreCollectors.toImmutableSet());
// It is important to use this logic to determine the set of rules to build rather than
// build.getActionGraph().getNodesWithNoIncomingEdges() because, due to graph enhancement,
// there could be disconnected subgraphs in the DependencyGraph that we do not want to build.
ImmutableList<BuildRule> rulesToBuild = ImmutableList.copyOf(targetsToBuild.stream().map(buildTarget -> {
try {
return getRuleResolver().requireRule(buildTarget);
} catch (NoSuchBuildTargetException e) {
throw new HumanReadableException("No build rule found for target %s", buildTarget);
}
}).collect(MoreCollectors.toImmutableSet()));
// Calculate and post the number of rules that need to built.
int numRules = buildEngine.getNumRulesToBuild(rulesToBuild);
getExecutionContext().getBuckEventBus().post(BuildEvent.ruleCountCalculated(targetsToBuild, numRules));
// Setup symlinks required when configuring the output path.
createConfiguredBuckOutSymlinks();
List<ListenableFuture<BuildResult>> futures = rulesToBuild.stream().map(rule -> buildEngine.build(buildContext, executionContext, rule)).collect(MoreCollectors.toImmutableList());
// Get the Future representing the build and then block until everything is built.
ListenableFuture<List<BuildResult>> buildFuture = Futures.allAsList(futures);
List<BuildResult> results;
try {
results = buildFuture.get();
if (!isKeepGoing) {
for (BuildResult result : results) {
Throwable thrown = result.getFailure();
if (thrown != null) {
throw new ExecutionException(thrown);
}
}
}
} catch (ExecutionException | InterruptedException | RuntimeException e) {
Throwable t = Throwables.getRootCause(e);
if (e instanceof InterruptedException || t instanceof InterruptedException || t instanceof ClosedByInterruptException) {
try {
buildFuture.cancel(true);
} catch (CancellationException ignored) {
// Rethrow original InterruptedException instead.
}
Thread.currentThread().interrupt();
}
throw e;
}
// Insertion order matters
LinkedHashMap<BuildRule, Optional<BuildResult>> resultBuilder = new LinkedHashMap<>();
Preconditions.checkState(rulesToBuild.size() == results.size());
for (int i = 0, len = rulesToBuild.size(); i < len; i++) {
BuildRule rule = rulesToBuild.get(i);
resultBuilder.put(rule, Optional.ofNullable(results.get(i)));
}
return BuildExecutionResult.builder().setFailures(FluentIterable.from(results).filter(input -> input.getSuccess() == null)).setResults(resultBuilder).build();
}
use of com.facebook.buck.rules.BuildContext in project buck by facebook.
the class JsBundle method getBuildSteps.
@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
final SourcePathResolver sourcePathResolver = context.getSourcePathResolver();
final SourcePath jsOutputDir = getSourcePathToOutput();
final SourcePath sourceMapFile = getSourcePathToSourceMap();
String jobArgs = Stream.concat(Stream.of("bundle", JsFlavors.bundleJobArgs(getBuildTarget().getFlavors()), JsUtil.resolveMapJoin(libraries, sourcePathResolver, p -> "--lib " + p), String.format("--sourcemap %s", sourcePathResolver.getAbsolutePath(sourceMapFile)), String.format("--out %s/%s", sourcePathResolver.getAbsolutePath(jsOutputDir), bundleName)), entryPoints.stream()).filter(s -> !s.isEmpty()).collect(Collectors.joining(" "));
buildableContext.recordArtifact(sourcePathResolver.getRelativePath(jsOutputDir));
buildableContext.recordArtifact(sourcePathResolver.getRelativePath(sourceMapFile));
return ImmutableList.of(new RmStep(getProjectFilesystem(), sourcePathResolver.getAbsolutePath(getOutputRoot())), new MkdirStep(getProjectFilesystem(), sourcePathResolver.getAbsolutePath(jsOutputDir)), new MkdirStep(getProjectFilesystem(), sourcePathResolver.getAbsolutePath(sourceMapFile).getParent()), JsUtil.workerShellStep(worker, jobArgs, getBuildTarget(), sourcePathResolver, getProjectFilesystem()));
}
use of com.facebook.buck.rules.BuildContext in project buck by facebook.
the class JsLibrary method getBuildSteps.
@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
final SourcePathResolver sourcePathResolver = context.getSourcePathResolver();
final Path outputPath = sourcePathResolver.getAbsolutePath(getSourcePathToOutput());
final String jobArgs = String.format("library %s --out %s %s", JsUtil.resolveMapJoin(libraryDependencies, sourcePathResolver, p -> "--lib " + p), outputPath, JsUtil.resolveMapJoin(sources, sourcePathResolver, Path::toString));
return ImmutableList.of(new RmStep(getProjectFilesystem(), outputPath), JsUtil.workerShellStep(worker, jobArgs, getBuildTarget(), sourcePathResolver, getProjectFilesystem()));
}
use of com.facebook.buck.rules.BuildContext in project buck by facebook.
the class DexProducedFromJavaLibraryThatContainsClassFilesTest method testGetBuildStepsWhenThereAreClassesToDex.
@Test
public void testGetBuildStepsWhenThereAreClassesToDex() throws IOException, InterruptedException {
ProjectFilesystem filesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
FakeJavaLibrary javaLibraryRule = new FakeJavaLibrary(BuildTargetFactory.newInstance(filesystem.getRootPath(), "//foo:bar"), pathResolver, filesystem, ImmutableSortedSet.of()) {
@Override
public ImmutableSortedMap<String, HashCode> getClassNamesToHashes() {
return ImmutableSortedMap.of("com/example/Foo", HashCode.fromString("cafebabe"));
}
};
resolver.addToIndex(javaLibraryRule);
Path jarOutput = BuildTargets.getGenPath(filesystem, javaLibraryRule.getBuildTarget(), "%s.jar");
javaLibraryRule.setOutputFile(jarOutput.toString());
BuildContext context = FakeBuildContext.withSourcePathResolver(pathResolver);
FakeBuildableContext buildableContext = new FakeBuildableContext();
Path dexOutput = BuildTargets.getGenPath(filesystem, javaLibraryRule.getBuildTarget().withFlavors(AndroidBinaryGraphEnhancer.DEX_FLAVOR), "%s.dex.jar");
createFiles(filesystem, dexOutput.toString(), jarOutput.toString());
BuildTarget buildTarget = BuildTargetFactory.newInstance(filesystem.getRootPath(), "//foo:bar#dex");
BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).setProjectFilesystem(filesystem).build();
DexProducedFromJavaLibrary preDex = new DexProducedFromJavaLibrary(params, javaLibraryRule);
List<Step> steps = preDex.getBuildSteps(context, buildableContext);
AndroidPlatformTarget androidPlatformTarget = createMock(AndroidPlatformTarget.class);
expect(androidPlatformTarget.getDxExecutable()).andStubReturn(Paths.get("/usr/bin/dx"));
EasyMock.replay(androidPlatformTarget);
ExecutionContext executionContext = TestExecutionContext.newBuilder().setAndroidPlatformTargetSupplier(Suppliers.ofInstance(androidPlatformTarget)).build();
String expectedDxCommand = String.format("%s --dex --no-optimize --force-jumbo --output %s %s", Paths.get("/usr/bin/dx"), filesystem.resolve(dexOutput), filesystem.resolve(jarOutput));
MoreAsserts.assertSteps("Generate bar.dex.jar.", ImmutableList.of(String.format("rm -f %s", filesystem.resolve(dexOutput)), String.format("mkdir -p %s", filesystem.resolve(dexOutput).getParent()), "estimate_dex_weight", "(cd " + filesystem.getRootPath() + " && " + expectedDxCommand + ")", String.format("zip-scrub %s", dexOutput), "record_dx_success"), steps, executionContext);
((EstimateDexWeightStep) steps.get(2)).setWeightEstimateForTesting(250);
Step recordArtifactAndMetadataStep = steps.get(5);
int exitCode = recordArtifactAndMetadataStep.execute(executionContext).getExitCode();
assertEquals(0, exitCode);
assertEquals("The generated .dex.jar file should be in the set of recorded artifacts.", ImmutableSet.of(BuildTargets.getGenPath(filesystem, buildTarget, "%s.dex.jar")), buildableContext.getRecordedArtifacts());
buildableContext.assertContainsMetadataMapping(DexProducedFromJavaLibrary.WEIGHT_ESTIMATE, "250");
}
Aggregations