From ee85859ea0903f64792c04c1b9eff3e9c1fa19e0 Mon Sep 17 00:00:00 2001
From: Stefan Bodewig
Date: Sun, 31 Jul 2016 06:21:43 +0200
Subject: [PATCH] add support for javac -h
https://bz.apache.org/bugzilla/show_bug.cgi?id=59905
---
WHATSNEW | 4 ++
manual/Tasks/javac.html | 17 ++++++
manual/Tasks/javah.html | 8 +++
.../org/apache/tools/ant/taskdefs/Javac.java | 21 +++++++
.../compilers/DefaultCompilerAdapter.java | 10 ++++
src/tests/antunit/taskdefs/javac-test.xml | 59 ++++++++++++++-----
6 files changed, 104 insertions(+), 15 deletions(-)
diff --git a/WHATSNEW b/WHATSNEW
index 019b8a452..9a36711f5 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -67,6 +67,10 @@ Other changes:
* a new implementation "forking" has been added to and is
used as default when running on JDK9.
+ * support for javac's -h switch has been added with the
+ nativeheaderdir attribute.
+ Bugzilla Report 59905
+
Changes from Ant 1.9.6 TO Ant 1.9.7
===================================
diff --git a/manual/Tasks/javac.html b/manual/Tasks/javac.html
index a5fc56cfc..202215f70 100644
--- a/manual/Tasks/javac.html
+++ b/manual/Tasks/javac.html
@@ -125,6 +125,15 @@ classpath of the <javac>
task, and does not release them.
The side effect of this is that you will not be able to delete or move
those files later on in the build. The workaround is to fork when
invoking the compiler.
+If you are using Java 8 or above and your source contains native
+ methods or fields annotated with the @Native
annotation
+ you can set the nativeheaderdir
attribute in order to
+ use the -h
switch of javac
to generate the
+ native header files. Note that the logic Ant uses to determine which
+ files to compile does not take native headers into account, i.e. if
+ the .class
is more recent than the
+ corresponding .java
file the file will not get compiled
+ even if a native header file generated for it would be outdated.
Parameters
@@ -505,6 +514,14 @@ invoking the compiler.
since Ant 1.9.7
No |
+
+ nativeheaderdir |
+
+ Specify where to place generated native header files. Ignored
+ when running on JDK < 8.
+ Since Ant 1.9.8.
+ | No |
+
Parameters specified as nested elements
diff --git a/manual/Tasks/javah.html b/manual/Tasks/javah.html
index 0428e6675..4d3979bd0 100644
--- a/manual/Tasks/javah.html
+++ b/manual/Tasks/javah.html
@@ -33,6 +33,14 @@ whether pre-JDK1.2
systems are used.
+If you are building with Java 8 or above consider
+ using javac
's nativeheaderdir
+ attribute instead which allows you to compile the classes and
+ generate the native header files with a single step.
+
+Note the javah
has been deprecated as of Java 9
+ and is scheduled to be removed with Java 10.
+
It is possible to use different compilers. This can be selected
with the implementation
attribute or a nested element. Here are the choices of the attribute:
diff --git a/src/main/org/apache/tools/ant/taskdefs/Javac.java b/src/main/org/apache/tools/ant/taskdefs/Javac.java
index 892c0f189..3452592fd 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Javac.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Javac.java
@@ -108,6 +108,7 @@ public class Javac extends MatchingTask {
private Path src;
private File destDir;
+ private File nativeHeaderDir;
private Path compileClasspath;
private Path modulepath;
private Path upgrademodulepath;
@@ -285,6 +286,26 @@ public class Javac extends MatchingTask {
return destDir;
}
+ /**
+ * Set the destination directory into which the generated native
+ * header files should be placed.
+ * @param nhDir where to place generated native header files
+ * @since Ant 1.9.8
+ */
+ public void setNativeHeaderDir(final File nhDir) {
+ this.nativeHeaderDir = nhDir;
+ }
+
+ /**
+ * Gets the destination directory into which the generated native
+ * header files should be placed.
+ * @return where to place generated native header files
+ * @since Ant 1.9.8
+ */
+ public File getNativeHeaderDir() {
+ return nativeHeaderDir;
+ }
+
/**
* Set the sourcepath to be used for this compilation.
* @param sourcepath the source path
diff --git a/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java b/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java
index db6cd8551..5f1f1f78d 100644
--- a/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java
+++ b/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java
@@ -414,6 +414,16 @@ public abstract class DefaultCompilerAdapter
cmd.createArgument().setValue("-upgrademodulepath");
cmd.createArgument().setPath(ump);
}
+ if (attributes.getNativeHeaderDir() != null) {
+ if (assumeJava13() || assumeJava14() || assumeJava15()
+ || assumeJava16() || assumeJava17()) {
+ attributes.log("Support for javac -h has been added in Java8,"
+ + " ignoring it");
+ } else {
+ cmd.createArgument().setValue("-h");
+ cmd.createArgument().setFile(attributes.getNativeHeaderDir());
+ }
+ }
return cmd;
}
diff --git a/src/tests/antunit/taskdefs/javac-test.xml b/src/tests/antunit/taskdefs/javac-test.xml
index de193ea3e..05c3269d2 100644
--- a/src/tests/antunit/taskdefs/javac-test.xml
+++ b/src/tests/antunit/taskdefs/javac-test.xml
@@ -18,10 +18,16 @@
-
-
+
+
+
+
+
+
+
-
+
@@ -54,7 +60,7 @@
-
+
@@ -67,7 +73,7 @@
-
+
@@ -80,7 +86,7 @@
-
+
@@ -151,7 +157,7 @@
-
+
@@ -198,16 +204,14 @@ public class Adapter implements CompilerAdapter {
-
+
public class A { }
-
-
-
@@ -256,16 +260,14 @@ public class Adapter implements CompilerAdapter {
-
+
public class A { }
-
-
-
@@ -313,4 +315,31 @@ public class Adapter implements CompilerAdapter {
+
+
+
+
+
+
+
+
+
+
+
+
+