use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.
the class GwtBinaryDescription method createBuildRule.
@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, final BuildRuleParams params, final BuildRuleResolver resolver, A args) {
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
final ImmutableSortedSet.Builder<BuildRule> extraDeps = ImmutableSortedSet.naturalOrder();
// Find all of the reachable JavaLibrary rules and grab their associated GwtModules.
final ImmutableSortedSet.Builder<SourcePath> gwtModuleJarsBuilder = ImmutableSortedSet.naturalOrder();
ImmutableSortedSet<BuildRule> moduleDependencies = resolver.getAllRules(args.moduleDeps);
new AbstractBreadthFirstTraversal<BuildRule>(moduleDependencies) {
@Override
public ImmutableSet<BuildRule> visit(BuildRule rule) {
if (!(rule instanceof JavaLibrary)) {
return ImmutableSet.of();
}
// If the java library doesn't generate any output, it doesn't contribute a GwtModule
JavaLibrary javaLibrary = (JavaLibrary) rule;
if (javaLibrary.getSourcePathToOutput() == null) {
return rule.getDeps();
}
BuildTarget gwtModuleTarget = BuildTargets.createFlavoredBuildTarget(javaLibrary.getBuildTarget().checkUnflavored(), JavaLibrary.GWT_MODULE_FLAVOR);
Optional<BuildRule> gwtModule = resolver.getRuleOptional(gwtModuleTarget);
if (!gwtModule.isPresent() && javaLibrary.getSourcePathToOutput() != null) {
ImmutableSortedSet<SourcePath> filesForGwtModule = ImmutableSortedSet.<SourcePath>naturalOrder().addAll(javaLibrary.getSources()).addAll(javaLibrary.getResources()).build();
ImmutableSortedSet<BuildRule> deps = ImmutableSortedSet.copyOf(ruleFinder.filterBuildRuleInputs(filesForGwtModule));
BuildRule module = resolver.addToIndex(new GwtModule(params.withBuildTarget(gwtModuleTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(deps), Suppliers.ofInstance(ImmutableSortedSet.of())), ruleFinder, filesForGwtModule));
gwtModule = Optional.of(module);
}
// but a rule that exists only as a collection of deps.
if (gwtModule.isPresent()) {
extraDeps.add(gwtModule.get());
gwtModuleJarsBuilder.add(Preconditions.checkNotNull(gwtModule.get().getSourcePathToOutput()));
}
// Traverse all of the deps of this rule.
return rule.getDeps();
}
}.start();
return new GwtBinary(params.copyReplacingExtraDeps(Suppliers.ofInstance(extraDeps.build())), args.modules, javaOptions.getJavaRuntimeLauncher(), args.vmArgs, args.style.orElse(DEFAULT_STYLE), args.draftCompile.orElse(DEFAULT_DRAFT_COMPILE), args.optimize.orElse(DEFAULT_OPTIMIZE), args.localWorkers.orElse(DEFAULT_NUM_LOCAL_WORKERS), args.strict.orElse(DEFAULT_STRICT), args.experimentalArgs, gwtModuleJarsBuilder.build());
}
use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.
the class ParserTest method whenBuildFileContainsSourcesUnderSymLinkNewSourcesNotAddedUntilCacheCleaned.
@Test
public void whenBuildFileContainsSourcesUnderSymLinkNewSourcesNotAddedUntilCacheCleaned() throws Exception {
// This test depends on creating symbolic links which we cannot do on Windows.
assumeTrue(Platform.detect() != Platform.WINDOWS);
tempDir.newFolder("bar");
tempDir.newFile("bar/Bar.java");
tempDir.newFolder("foo");
Path rootPath = tempDir.getRoot().toRealPath();
Files.createSymbolicLink(rootPath.resolve("foo/bar"), rootPath.resolve("bar"));
Path testBuckFile = rootPath.resolve("foo").resolve("BUCK");
Files.write(testBuckFile, "java_library(name = 'lib', srcs=glob(['bar/*.java']))\n".getBytes(UTF_8));
// Fetch //:lib to put it in cache.
BuildTarget libTarget = BuildTarget.builder(cellRoot, "//foo", "lib").build();
Iterable<BuildTarget> buildTargets = ImmutableList.of(libTarget);
{
TargetGraph targetGraph = parser.buildTargetGraph(eventBus, cell, false, executorService, buildTargets);
BuildRuleResolver resolver = buildActionGraph(eventBus, targetGraph);
JavaLibrary libRule = (JavaLibrary) resolver.requireRule(libTarget);
assertEquals(ImmutableSortedSet.of(new PathSourcePath(filesystem, Paths.get("foo/bar/Bar.java"))), libRule.getJavaSrcs());
}
tempDir.newFile("bar/Baz.java");
WatchEvent<Path> createEvent = createPathEvent(Paths.get("bar/Baz.java"), StandardWatchEventKinds.ENTRY_CREATE);
parser.onFileSystemChange(createEvent);
{
TargetGraph targetGraph = parser.buildTargetGraph(eventBus, cell, false, executorService, buildTargets);
BuildRuleResolver resolver = buildActionGraph(eventBus, targetGraph);
JavaLibrary libRule = (JavaLibrary) resolver.requireRule(libTarget);
assertEquals(ImmutableSet.of(new PathSourcePath(filesystem, Paths.get("foo/bar/Bar.java")), new PathSourcePath(filesystem, Paths.get("foo/bar/Baz.java"))), libRule.getJavaSrcs());
}
}
use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.
the class DexProducedFromJavaLibraryThatContainsClassFilesTest method testGetBuildStepsWhenThereAreNoClassesToDex.
@Test
public void testGetBuildStepsWhenThereAreNoClassesToDex() throws Exception {
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
DefaultJavaLibrary javaLibrary = JavaLibraryBuilder.createBuilder("//foo:bar").build(resolver);
javaLibrary.getBuildOutputInitializer().setBuildOutput(new JavaLibrary.Data(ImmutableSortedMap.of()));
BuildContext context = FakeBuildContext.NOOP_CONTEXT;
FakeBuildableContext buildableContext = new FakeBuildableContext();
ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
BuildTarget buildTarget = BuildTargetFactory.newInstance("//foo:bar#dex");
BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).setProjectFilesystem(projectFilesystem).build();
DexProducedFromJavaLibrary preDex = new DexProducedFromJavaLibrary(params, javaLibrary);
List<Step> steps = preDex.getBuildSteps(context, buildableContext);
Path dexOutput = BuildTargets.getGenPath(projectFilesystem, buildTarget, "%s.dex.jar");
ExecutionContext executionContext = TestExecutionContext.newBuilder().build();
MoreAsserts.assertSteps("Do not generate a .dex.jar file.", ImmutableList.of(String.format("rm -f %s", projectFilesystem.resolve(dexOutput)), String.format("mkdir -p %s", projectFilesystem.resolve(dexOutput.getParent())), "record_empty_dx"), steps, executionContext);
Step recordArtifactAndMetadataStep = steps.get(2);
assertThat(recordArtifactAndMetadataStep.getShortName(), startsWith("record_"));
int exitCode = recordArtifactAndMetadataStep.execute(executionContext).getExitCode();
assertEquals(0, exitCode);
}
use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.
the class DexWithClassesTest method testIntermediateDexRuleToDexWithClassesWhenIntermediateDexHasNoClasses.
@Test
public void testIntermediateDexRuleToDexWithClassesWhenIntermediateDexHasNoClasses() {
SourcePathResolver resolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
BuildTarget javaLibraryTarget = BuildTargetFactory.newInstance("//java/com/example:lib");
JavaLibrary javaLibrary = new FakeJavaLibrary(javaLibraryTarget, resolver);
BuildTarget buildTarget = BuildTargetFactory.newInstance("//java/com/example:lib#dex");
BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).build();
DexProducedFromJavaLibrary dexFromJavaLibrary = new DexProducedFromJavaLibrary(params, javaLibrary);
dexFromJavaLibrary.getBuildOutputInitializer().setBuildOutput(new DexProducedFromJavaLibrary.BuildOutput(/* weightEstimate */
1600, /* classNamesToHashes */
ImmutableSortedMap.of(), Optional.empty()));
DexWithClasses dexWithClasses = DexWithClasses.TO_DEX_WITH_CLASSES.apply(dexFromJavaLibrary);
assertNull("If the JavaLibraryRule does not produce any .class files, " + "then DexWithClasses.TO_DEX_WITH_CLASSES should return null.", dexWithClasses);
}
use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.
the class DexWithClassesTest method testIntermediateDexRuleToDexWithClasses.
@Test
public void testIntermediateDexRuleToDexWithClasses() {
SourcePathResolver resolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
BuildTarget javaLibraryTarget = BuildTargetFactory.newInstance("//java/com/example:lib");
JavaLibrary javaLibrary = new FakeJavaLibrary(javaLibraryTarget, resolver);
BuildTarget buildTarget = BuildTargetFactory.newInstance("//java/com/example:lib#dex");
BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).build();
DexProducedFromJavaLibrary dexFromJavaLibrary = new DexProducedFromJavaLibrary(params, javaLibrary);
dexFromJavaLibrary.getBuildOutputInitializer().setBuildOutput(new DexProducedFromJavaLibrary.BuildOutput(/* weightEstimate */
1600, /* classNamesToHashes */
ImmutableSortedMap.of("com/example/Main", HashCode.fromString(Strings.repeat("cafebabe", 5))), Optional.empty()));
DexWithClasses dexWithClasses = DexWithClasses.TO_DEX_WITH_CLASSES.apply(dexFromJavaLibrary);
assertEquals(BuildTargets.getGenPath(javaLibrary.getProjectFilesystem(), buildTarget, "%s.dex.jar"), dexWithClasses.getPathToDexFile());
assertEquals(ImmutableSet.of("com/example/Main"), dexWithClasses.getClassNames());
assertEquals(1600, dexWithClasses.getWeightEstimate());
}
Aggregations