From 1f22fa98880a1c1d02dcab629a885ffb9516a29f Mon Sep 17 00:00:00 2001 From: cushon Date: Thu, 26 Apr 2018 23:02:53 -0700 Subject: Add support for native header outputs to VanillaJavaBuilder PiperOrigin-RevId: 194503531 --- .../build/buildjar/VanillaJavaBuilder.java | 48 +++++++++++++---- .../build/buildjar/VanillaJavaBuilderTest.java | 61 ++++++++++++++++++++++ 2 files changed, 100 insertions(+), 9 deletions(-) (limited to 'src/java_tools') diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/VanillaJavaBuilder.java b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/VanillaJavaBuilder.java index ee432d9d6c..0064c894a6 100644 --- a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/VanillaJavaBuilder.java +++ b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/VanillaJavaBuilder.java @@ -14,6 +14,7 @@ package com.google.devtools.build.buildjar; +import static com.google.common.base.MoreObjects.firstNonNull; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Locale.ENGLISH; @@ -152,7 +153,12 @@ public class VanillaJavaBuilder implements Closeable { JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(diagnosticCollector, ENGLISH, UTF_8); - setLocations(optionsParser, fileManager); + + Path tempDir = Paths.get(firstNonNull(optionsParser.getTempDir(), "_tmp")); + Path nativeHeaderDir = tempDir.resolve("native_headers"); + Files.createDirectories(nativeHeaderDir); + + setLocations(optionsParser, fileManager, nativeHeaderDir); ImmutableList sources = getSources(optionsParser, fileManager); boolean ok; if (sources.isEmpty()) { @@ -171,6 +177,7 @@ public class VanillaJavaBuilder implements Closeable { } if (ok) { writeOutput(optionsParser); + writeNativeHeaderOutput(optionsParser, nativeHeaderDir); } writeGeneratedSourceOutput(optionsParser); // the jdeps output doesn't include any information about dependencies, but Bazel still expects @@ -240,7 +247,8 @@ public class VanillaJavaBuilder implements Closeable { } /** Sets the compilation search paths and output directories. */ - private static void setLocations(OptionsParser optionsParser, StandardJavaFileManager fileManager) + private static void setLocations( + OptionsParser optionsParser, StandardJavaFileManager fileManager, Path nativeHeaderDir) throws IOException { fileManager.setLocation(StandardLocation.CLASS_PATH, toFiles(optionsParser.getClassPath())); fileManager.setLocation( @@ -250,14 +258,21 @@ public class VanillaJavaBuilder implements Closeable { fileManager.setLocation( StandardLocation.ANNOTATION_PROCESSOR_PATH, toFiles(optionsParser.getProcessorPath())); if (optionsParser.getSourceGenDir() != null) { - Path sourceGenDir = Paths.get(optionsParser.getSourceGenDir()); - createOutputDirectory(sourceGenDir); - fileManager.setLocation( - StandardLocation.SOURCE_OUTPUT, ImmutableList.of(sourceGenDir.toFile())); + setOutputLocation( + fileManager, StandardLocation.SOURCE_OUTPUT, Paths.get(optionsParser.getSourceGenDir())); } - Path classDir = Paths.get(optionsParser.getClassDir()); - createOutputDirectory(classDir); - fileManager.setLocation(StandardLocation.CLASS_OUTPUT, ImmutableList.of(classDir.toFile())); + if (optionsParser.getNativeHeaderOutput() != null) { + setOutputLocation(fileManager, StandardLocation.NATIVE_HEADER_OUTPUT, nativeHeaderDir); + } + setOutputLocation( + fileManager, StandardLocation.CLASS_OUTPUT, Paths.get(optionsParser.getClassDir())); + } + + private static void setOutputLocation( + StandardJavaFileManager fileManager, StandardLocation location, Path path) + throws IOException { + createOutputDirectory(path); + fileManager.setLocation(location, ImmutableList.of(path.toFile())); } /** Sets the compilation's annotation processors. */ @@ -289,6 +304,21 @@ public class VanillaJavaBuilder implements Closeable { jar.execute(); } + private static void writeNativeHeaderOutput(OptionsParser optionsParser, Path nativeHeaderDir) + throws IOException { + if (optionsParser.getNativeHeaderOutput() == null) { + return; + } + JarCreator jar = new JarCreator(optionsParser.getNativeHeaderOutput()); + try { + jar.setNormalize(true); + jar.setCompression(optionsParser.compressJar()); + jar.addDirectory(nativeHeaderDir); + } finally { + jar.execute(); + } + } + /** Writes the class output jar, including any resource entries. */ private static void writeOutput(OptionsParser optionsParser) throws IOException { JarCreator jar = new JarCreator(optionsParser.getOutputJar()); diff --git a/src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar/VanillaJavaBuilderTest.java b/src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar/VanillaJavaBuilderTest.java index b64e2d8a5b..7194c7bfe8 100644 --- a/src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar/VanillaJavaBuilderTest.java +++ b/src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar/VanillaJavaBuilderTest.java @@ -92,6 +92,8 @@ public class VanillaJavaBuilderTest { output.toString(), "--bootclasspath", Paths.get(System.getProperty("java.home")).resolve("lib/rt.jar").toString(), + "--tempdir", + temporaryFolder.newFolder().toString(), "--classdir", temporaryFolder.newFolder().toString())); @@ -135,6 +137,8 @@ public class VanillaJavaBuilderTest { output.toString(), "--bootclasspath", Paths.get(System.getProperty("java.home")).resolve("lib/rt.jar").toString(), + "--tempdir", + temporaryFolder.newFolder().toString(), "--classdir", temporaryFolder.newFolder().toString())); @@ -174,6 +178,8 @@ public class VanillaJavaBuilderTest { output.toString(), "--bootclasspath", Paths.get(System.getProperty("java.home")).resolve("lib/rt.jar").toString(), + "--tempdir", + temporaryFolder.newFolder().toString(), "--classdir", temporaryFolder.newFolder().toString())); @@ -218,6 +224,8 @@ public class VanillaJavaBuilderTest { output.toString(), "--bootclasspath", Paths.get(System.getProperty("java.home")).resolve("lib/rt.jar").toString(), + "--tempdir", + temporaryFolder.newFolder().toString(), "--classdir", classDir.toString())); @@ -262,10 +270,63 @@ public class VanillaJavaBuilderTest { output.toString(), "--bootclasspath", Paths.get(System.getProperty("java.home")).resolve("lib/rt.jar").toString(), + "--tempdir", + temporaryFolder.newFolder().toString(), "--classdir", temporaryFolder.newFolder().toString())); assertThat(result.output()).isEmpty(); assertThat(result.ok()).isTrue(); } + + @Test + public void nativeHeaders() throws Exception { + Path foo = temporaryFolder.newFile("FooWithNativeMethod.java").toPath(); + Path bar = temporaryFolder.newFile("BarWithNativeMethod.java").toPath(); + Path output = temporaryFolder.newFile("out.jar").toPath(); + Path nativeHeaderOutput = temporaryFolder.newFile("out-native-headers.jar").toPath(); + Files.write( + foo, + ImmutableList.of( + "package test;", + "public class FooWithNativeMethod {", + " public static native byte[] g(String s);", + "}"), + UTF_8); + Files.write( + bar, + ImmutableList.of( + "package test;", + "public class BarWithNativeMethod {", + " public static native byte[] g(String s);", + "}"), + UTF_8); + + VanillaJavaBuilderResult result = + run( + ImmutableList.of( + "--javacopts", + "-Xep:FallThrough:ERROR", + "--", + "--sources", + foo.toString(), + bar.toString(), + "--output", + output.toString(), + "--native_header_output", + nativeHeaderOutput.toString(), + "--bootclasspath", + Paths.get(System.getProperty("java.home")).resolve("lib/rt.jar").toString(), + "--tempdir", + temporaryFolder.newFolder().toString(), + "--classdir", + temporaryFolder.newFolder().toString())); + + assertThat(result.output()).isEmpty(); + assertThat(result.ok()).isTrue(); + + ImmutableMap outputEntries = readJar(nativeHeaderOutput.toFile()); + assertThat(outputEntries.keySet()) + .containsAllOf("test_BarWithNativeMethod.h", "test_FooWithNativeMethod.h"); + } } -- cgit v1.2.3