Browse Source

Add a way to pass compiler specific command line arguments to <javac>.

PR: 4406

heavily based upon a Submission by:	Stephen Anderson <anderson@berbee.com>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269869 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 23 years ago
parent
commit
0ba89b453f
8 changed files with 145 additions and 27 deletions
  1. +37
    -0
      docs/manual/CoreTasks/javac.html
  2. +88
    -26
      src/main/org/apache/tools/ant/taskdefs/Javac.java
  3. +10
    -0
      src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java
  4. +3
    -0
      src/main/org/apache/tools/ant/taskdefs/compilers/Gcj.java
  5. +2
    -0
      src/main/org/apache/tools/ant/taskdefs/compilers/Jikes.java
  6. +2
    -0
      src/main/org/apache/tools/ant/taskdefs/compilers/Jvc.java
  7. +2
    -0
      src/main/org/apache/tools/ant/taskdefs/compilers/Kjc.java
  8. +1
    -1
      src/main/org/apache/tools/ant/types/Commandline.java

+ 37
- 0
docs/manual/CoreTasks/javac.html View File

@@ -244,6 +244,43 @@ href="../using.html#path">path-like structures</a> and can also be set via neste
<code>&lt;bootclasspath&gt</code> and
<code>&lt;extdirs&gt</code> elements, respectively.</p>

<h4>compilerarg</h4>

<p>You can specify additional command line arguments for the compiler
with nested <code>&lt;compilerarg&gt;</code> elements. These elements
are specified like <a href="../using.html#arg">Command-line
Arguments</a> but have an additional attribute that can be used to
enable arguments only if a given compiler implementation will be
used.</p>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td width="12%" valign="top"><b>Attribute</b></td>
<td width="78%" valign="top"><b>Description</b></td>
<td width="10%" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">value</td>
<td align="center" rowspan="4">See <a href="../using.html#arg">here</a></td>
<td align="center" rowspan="4">Exactly one of these.</td>
</tr>
<tr>
<td valign="top">line</td>
</tr>
<tr>
<td valign="top">file</td>
</tr>
<tr>
<td valign="top">path</td>
</tr>
<tr>
<td valign="top">implementation</td>
<td align="center">Only use this argument if the chosen
implementation matches this attribute. Possible choices are the
same as those of the build.compiler property.</td>
<td align="center">No.</td>
</tr>
</table>

<h3>Examples</h3>
<pre> &lt;javac srcdir=&quot;${src}&quot;
destdir=&quot;${build}&quot;


+ 88
- 26
src/main/org/apache/tools/ant/taskdefs/Javac.java View File

@@ -57,6 +57,7 @@ package org.apache.tools.ant.taskdefs;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
import org.apache.tools.ant.util.GlobPatternMapper;
@@ -66,6 +67,8 @@ import org.apache.tools.ant.taskdefs.compilers.CompilerAdapterFactory;
import org.apache.tools.ant.taskdefs.condition.Os;

import java.io.File;
import java.util.Enumeration;
import java.util.Vector;

/**
* Task to compile Java source files. This task can take the following
@@ -124,6 +127,7 @@ public class Javac extends MatchingTask {
private boolean nowarn = false;
private String memoryInitialSize;
private String memoryMaximumSize;
private Vector implementationSpecificArgs = new Vector();

protected boolean failOnError = true;
protected File[] compileList = new File[0];
@@ -503,6 +507,36 @@ public class Javac extends MatchingTask {
return nowarn;
}

/**
* Adds an implementation specific command line argument.
*/
public ImplementationSpecificArgument createCompilerArg() {
ImplementationSpecificArgument arg =
new ImplementationSpecificArgument();
implementationSpecificArgs.addElement(arg);
return arg;
}

/**
* Get the additional implementation specific command line arguments.
* @return array of command line arguments, guaranteed to be non-null.
*/
public String[] getCurrentCompilerArgs() {
Vector args = new Vector();
for (Enumeration enum = implementationSpecificArgs.elements();
enum.hasMoreElements();
) {
String[] curr =
((ImplementationSpecificArgument) enum.nextElement()).getParts();
for (int i=0; i<curr.length; i++) {
args.addElement(curr[i]);
}
}
String[] res = new String[args.size()];
args.copyInto(res);
return res;
}

/**
* Executes the task.
*/
@@ -539,32 +573,7 @@ public class Javac extends MatchingTask {

// compile the source files

String compiler = project.getProperty("build.compiler");

if (!"false".equals(fork)) {
if (compiler != null) {
if (isJdkCompiler(compiler)) {
log("Since fork is true, ignoring build.compiler setting.",
Project.MSG_WARN);
compiler = "extJavac";
}
else {
log("Since build.compiler setting isn't classic or modern, ignoring fork setting.", Project.MSG_WARN);
}
}
else {
compiler = "extJavac";
}
}

if (compiler == null) {
if (Project.getJavaVersion() != Project.JAVA_1_1 &&
Project.getJavaVersion() != Project.JAVA_1_2) {
compiler = "modern";
} else {
compiler = "classic";
}
}
String compiler = determineCompiler();

if (compileList.length > 0) {

@@ -653,4 +662,57 @@ public class Javac extends MatchingTask {
}
}
private String determineCompiler() {
String compiler = project.getProperty("build.compiler");

if (!"false".equals(fork)) {
if (compiler != null) {
if (isJdkCompiler(compiler)) {
log("Since fork is true, ignoring build.compiler setting.",
Project.MSG_WARN);
compiler = "extJavac";
}
else {
log("Since build.compiler setting isn't classic or modern, ignoring fork setting.", Project.MSG_WARN);
}
}
else {
compiler = "extJavac";
}
}

if (compiler == null) {
if (Project.getJavaVersion() != Project.JAVA_1_1 &&
Project.getJavaVersion() != Project.JAVA_1_2) {
compiler = "modern";
} else {
compiler = "classic";
}
}
return compiler;
}

/**
* Adds an "implementation" attribute to Commandline$Attribute
* used to filter command line attributes based on the current
* implementation.
*/
public class ImplementationSpecificArgument
extends Commandline.Argument {

private String impl;

public void setImplementation(String impl) {
this.impl = impl;
}

public String[] getParts() {
if (impl == null || impl.equals(determineCompiler())) {
return super.getParts();
} else {
return new String[0];
}
}
}

}

+ 10
- 0
src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java View File

@@ -280,6 +280,9 @@ public abstract class DefaultCompilerAdapter implements CompilerAdapter {
if (verbose) {
cmd.createArgument().setValue("-verbose");
}

addCurrentCompilerArgs(cmd);

return cmd;
}

@@ -434,5 +437,12 @@ public abstract class DefaultCompilerAdapter implements CompilerAdapter {
}
}

/**
* Adds the command line arguments specifc to the current implementation.
*/
protected void addCurrentCompilerArgs(Commandline cmd) {
cmd.addArguments(getJavac().getCurrentCompilerArgs());
}

}


+ 3
- 0
src/main/org/apache/tools/ant/taskdefs/compilers/Gcj.java View File

@@ -136,6 +136,9 @@ public class Gcj extends DefaultCompilerAdapter {
* gcj should be set for generate class.
*/
cmd.createArgument().setValue("-C");

addCurrentCompilerArgs(cmd);

return cmd;
}
}

+ 2
- 0
src/main/org/apache/tools/ant/taskdefs/compilers/Jikes.java View File

@@ -210,6 +210,8 @@ public class Jikes extends DefaultCompilerAdapter {
cmd.createArgument().setValue("+F");
}

addCurrentCompilerArgs(cmd);

int firstFileName = cmd.size();
logAndAddFilesToCompile(cmd);



+ 2
- 0
src/main/org/apache/tools/ant/taskdefs/compilers/Jvc.java View File

@@ -130,6 +130,8 @@ public class Jvc extends DefaultCompilerAdapter {
cmd.createArgument().setValue("/verbose");
}

addCurrentCompilerArgs(cmd);

int firstFileName = cmd.size();
logAndAddFilesToCompile(cmd);



+ 2
- 0
src/main/org/apache/tools/ant/taskdefs/compilers/Kjc.java View File

@@ -156,6 +156,8 @@ public class Kjc extends DefaultCompilerAdapter {
cmd.createArgument().setValue("-verbose");
}

addCurrentCompilerArgs(cmd);

logAndAddFilesToCompile(cmd);
return cmd;
}


+ 1
- 1
src/main/org/apache/tools/ant/types/Commandline.java View File

@@ -105,7 +105,7 @@ public class Commandline implements Cloneable {
/**
* Used for nested xml command line definitions.
*/
public class Argument {
public static class Argument {

private String[] parts;



Loading…
Cancel
Save