Ant task: support additional arguments, drop inline/optimize

Only "-X" options are now supported as additional command line arguments
This commit is contained in:
Alexander Udalov
2014-07-30 13:50:20 -07:00
parent 45a57011d8
commit 9442724821
14 changed files with 97 additions and 118 deletions
@@ -17,15 +17,17 @@
package org.jetbrains.jet.buildtools.ant;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
import org.jetbrains.jet.buildtools.core.BytecodeCompiler;
import org.jetbrains.jet.buildtools.core.Util;
import org.jetbrains.jet.cli.common.arguments.CompilerArgumentsUtil;
import org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentException;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.jetbrains.jet.buildtools.core.Util.getPath;
@@ -48,8 +50,7 @@ public class BytecodeCompilerTask extends Task {
private File module;
private Path compileClasspath;
private boolean includeRuntime = true;
private String inline;
private String optimize;
private final List<Commandline.Argument> additionalArguments = new ArrayList<Commandline.Argument>();
public void setOutput(File output) {
this.output = output;
@@ -93,12 +94,10 @@ public class BytecodeCompilerTask extends Task {
this.includeRuntime = includeRuntime;
}
public void setInline(String inline) {
this.inline = inline;
}
public void setOptimize(String optimize) {
this.optimize = optimize;
public Commandline.Argument createCompilerArg() {
Commandline.Argument argument = new Commandline.Argument();
additionalArguments.add(argument);
return argument;
}
/**
@@ -146,17 +145,11 @@ public class BytecodeCompilerTask extends Task {
String[] classpath = (this.compileClasspath != null ? this.compileClasspath.list() : null);
String[] externalAnnotationsPath = (this.externalAnnotations != null) ? this.externalAnnotations.list() : null;
if (!CompilerArgumentsUtil.checkOption(inline)) {
throw new CompileEnvironmentException(CompilerArgumentsUtil.getWrongCheckOptionErrorMessage("inline", inline));
List<String> args = new ArrayList<String>();
for (Commandline.Argument argument : additionalArguments) {
args.addAll(Arrays.asList(argument.getParts()));
}
if (!CompilerArgumentsUtil.checkOption(optimize)) {
throw new CompileEnvironmentException(CompilerArgumentsUtil.getWrongCheckOptionErrorMessage("optimize", optimize));
}
boolean enableInline = CompilerArgumentsUtil.optionToBooleanFlag(inline, true);
boolean enableOptimization = CompilerArgumentsUtil.optionToBooleanFlag(optimize, true);
if (this.src != null) {
if ((this.output == null) && (this.jar == null)) {
@@ -169,16 +162,10 @@ public class BytecodeCompilerTask extends Task {
log(String.format("Compiling [%s] => [%s]", Arrays.toString(source), destination));
if (this.output != null) {
compiler.sourcesToDir(
source, destination, stdlibPath, classpath, externalAnnotationsPath,
enableInline, enableOptimization
);
compiler.sourcesToDir(source, destination, stdlibPath, classpath, externalAnnotationsPath, args);
}
else {
compiler.sourcesToJar(
source, destination, this.includeRuntime, stdlibPath, classpath, externalAnnotationsPath,
enableInline, enableOptimization
);
compiler.sourcesToJar(source, destination, this.includeRuntime, stdlibPath, classpath, externalAnnotationsPath, args);
}
}
else if (this.module != null) {
@@ -193,10 +180,7 @@ public class BytecodeCompilerTask extends Task {
log(jarPath != null ? String.format("Compiling [%s] => [%s]", modulePath, jarPath) :
String.format("Compiling [%s]", modulePath));
compiler.moduleToJar(
modulePath, jarPath, this.includeRuntime, stdlibPath, classpath, externalAnnotationsPath,
enableInline, enableOptimization
);
compiler.moduleToJar(modulePath, jarPath, this.includeRuntime, stdlibPath, classpath, externalAnnotationsPath, args);
}
else {
throw new CompileEnvironmentException("\"src\" or \"module\" should be specified");
@@ -19,15 +19,20 @@ package org.jetbrains.jet.buildtools.core;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Function;
import com.sampullara.cli.Args;
import kotlin.modules.Module;
import org.apache.tools.ant.BuildException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.cli.common.CLIConfigurationKeys;
import org.jetbrains.jet.cli.common.CompilerPlugin;
import org.jetbrains.jet.cli.common.arguments.K2JVMCompilerArguments;
import org.jetbrains.jet.cli.common.messages.MessageCollectorPlainTextToStream;
import org.jetbrains.jet.cli.common.modules.ModuleScriptData;
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
import org.jetbrains.jet.cli.jvm.K2JVMCompiler;
import org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentException;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler;
@@ -42,10 +47,10 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.jetbrains.jet.cli.jvm.JVMConfigurationKeys.*;
import static org.jetbrains.jet.cli.jvm.JVMConfigurationKeys.ANNOTATIONS_PATH_KEY;
import static org.jetbrains.jet.cli.jvm.JVMConfigurationKeys.CLASSPATH_KEY;
import static org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentUtil.loadModuleDescriptions;
/**
* Wrapper class for Kotlin bytecode compiler.
*/
@@ -57,39 +62,30 @@ public class BytecodeCompiler {
public BytecodeCompiler() {
}
/**
* Creates new instance of {@link JetCoreEnvironment} instance using the arguments specified.
*
* @param stdlib path to "kotlin-runtime.jar", only used if not null and not empty
* @param classpath compilation classpath, only used if not null and not empty
* @param sourceRoots
* @param enableInline
* @param enableOptimization
* @return compile environment instance
*/
private JetCoreEnvironment env(
String stdlib,
String[] classpath,
String[] externalAnnotationsPath,
String[] sourceRoots,
boolean enableInline,
boolean enableOptimization
List<String> args
) {
CompilerConfiguration configuration = createConfiguration(
stdlib, classpath, externalAnnotationsPath, sourceRoots, enableInline, enableOptimization
return JetCoreEnvironment.createForProduction(
Disposer.newDisposable(),
createConfiguration(stdlib, classpath, externalAnnotationsPath, sourceRoots, args)
);
return JetCoreEnvironment.createForProduction(Disposer.newDisposable(), configuration);
}
@NotNull
private CompilerConfiguration createConfiguration(
@Nullable String stdlib,
@Nullable String[] classpath,
@Nullable String[] externalAnnotationsPath,
@NotNull String[] sourceRoots,
boolean enableInline,
boolean enableOptimization
@NotNull List<String> args
) {
KotlinPaths paths = getKotlinPathsForAntTask();
CompilerConfiguration configuration = new CompilerConfiguration();
@@ -127,8 +123,16 @@ public class BytecodeCompiler {
}
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR);
configuration.put(DISABLE_INLINE, !enableInline);
configuration.put(DISABLE_OPTIMIZATION, !enableOptimization);
// TODO: use K2JVMCompiler directly, don't duplicate this code here
K2JVMCompilerArguments arguments = new K2JVMCompilerArguments();
try {
Args.parse(arguments, ArrayUtil.toStringArray(args));
}
catch (IllegalArgumentException e) {
throw new BuildException(e.getMessage());
}
K2JVMCompiler.putAdvancedOptions(configuration, arguments);
// lets register any compiler plugins
configuration.addAll(CLIConfigurationKeys.COMPILER_PLUGINS, getCompilerPlugins());
@@ -164,21 +168,22 @@ public class BytecodeCompiler {
/**
* {@code CompileEnvironment#compileBunchOfSources} wrapper.
*
* @param src compilation source (directories or files)
* @param output compilation destination directory
* @param stdlib "kotlin-runtime.jar" path
* @param classpath compilation classpath, can be <code>null</code> or empty
* @param args additional command line arguments to Kotlin compiler
*/
public void sourcesToDir(@NotNull String[] src,
public void sourcesToDir(
@NotNull String[] src,
@NotNull String output,
@Nullable String stdlib,
@Nullable String[] classpath,
@Nullable String[] externalAnnotationsPath,
boolean enableInline,
boolean enableOptimization) {
@NotNull List<String> args
) {
try {
JetCoreEnvironment environment = env(stdlib, classpath, externalAnnotationsPath, src, enableInline, enableOptimization);
JetCoreEnvironment environment = env(stdlib, classpath, externalAnnotationsPath, src, args);
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, null, new File(output), true);
if (!success) {
@@ -193,23 +198,24 @@ public class BytecodeCompiler {
/**
* {@code CompileEnvironment#compileBunchOfSources} wrapper.
*
* @param src compilation source (directory or file)
* @param jar compilation destination jar
* @param includeRuntime whether Kotlin runtime library is included in destination jar
* @param stdlib "kotlin-runtime.jar" path
* @param classpath compilation classpath, can be <code>null</code> or empty
* @param args additional command line arguments to Kotlin compiler
*/
public void sourcesToJar(@NotNull String[] src,
public void sourcesToJar(
@NotNull String[] src,
@NotNull String jar,
boolean includeRuntime,
@Nullable String stdlib,
@Nullable String[] classpath,
@Nullable String[] externalAnnotationsPath,
boolean enableInline,
boolean enableOptimization) {
@NotNull List<String> args
) {
try {
JetCoreEnvironment environment = env(stdlib, classpath, externalAnnotationsPath, src, enableInline, enableOptimization);
JetCoreEnvironment environment = env(stdlib, classpath, externalAnnotationsPath, src, args);
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, new File(jar), null, includeRuntime);
if (!success) {
@@ -224,13 +230,12 @@ public class BytecodeCompiler {
/**
* {@code CompileEnvironment#compileModules} wrapper.
* @param module compilation module file
* @param module compilation module file
* @param jar compilation destination jar
* @param includeRuntime whether Kotlin runtime library is included in destination jar
* @param stdlib "kotlin-runtime.jar" path
* @param classpath compilation classpath, can be <code>null</code> or empty
* @param enableInline
* @param enableOptimization
* @param args additional command line arguments to Kotlin compiler
*/
public void moduleToJar(
@NotNull String module,
@@ -238,7 +243,8 @@ public class BytecodeCompiler {
boolean includeRuntime,
@Nullable String stdlib,
@Nullable String[] classpath,
@Nullable String[] externalAnnotationsPath, boolean enableInline, boolean enableOptimization
@Nullable String[] externalAnnotationsPath,
@NotNull List<String> args
) {
try {
ModuleScriptData moduleScriptData =
@@ -248,8 +254,8 @@ public class BytecodeCompiler {
for (Module m : modules) {
sourcesRoots.addAll(m.getSourceFiles());
}
CompilerConfiguration configuration = createConfiguration(stdlib, classpath, externalAnnotationsPath, sourcesRoots.toArray(new String[0]),
enableInline, enableOptimization);
CompilerConfiguration configuration = createConfiguration(stdlib, classpath, externalAnnotationsPath,
ArrayUtil.toStringArray(sourcesRoots), args);
File directory = new File(module).getParentFile();
boolean success = KotlinToJVMBytecodeCompiler.compileModules(configuration, modules, directory, new File(jar), includeRuntime);
if (!success) {
@@ -100,10 +100,7 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
? CommandLineScriptUtils.scriptParameters()
: Collections.<AnalyzerScriptParameter>emptyList());
configuration.put(JVMConfigurationKeys.DISABLE_CALL_ASSERTIONS, arguments.noCallAssertions);
configuration.put(JVMConfigurationKeys.DISABLE_PARAM_ASSERTIONS, arguments.noParamAssertions);
configuration.put(JVMConfigurationKeys.DISABLE_INLINE, arguments.noInline);
configuration.put(JVMConfigurationKeys.DISABLE_OPTIMIZATION, arguments.noOptimize);
putAdvancedOptions(configuration, arguments);
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector);
@@ -163,6 +160,12 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
}
}
public static void putAdvancedOptions(@NotNull CompilerConfiguration configuration, @NotNull K2JVMCompilerArguments arguments) {
configuration.put(JVMConfigurationKeys.DISABLE_CALL_ASSERTIONS, arguments.noCallAssertions);
configuration.put(JVMConfigurationKeys.DISABLE_PARAM_ASSERTIONS, arguments.noParamAssertions);
configuration.put(JVMConfigurationKeys.DISABLE_INLINE, arguments.noInline);
configuration.put(JVMConfigurationKeys.DISABLE_OPTIMIZATION, arguments.noOptimize);
}
/**
* Allow derived classes to add additional command line arguments
@@ -85,12 +85,12 @@ public class AntTaskTest extends KotlinIntegrationTestBase {
}
@Test
public void inlineDisabled() throws Exception {
public void antAdditionalArguments() throws Exception {
doJvmAntTest();
}
@Test
public void inlineWrongArg() throws Exception {
public void antWrongArguments() throws Exception {
doAntTest(FAILED);
}
@@ -2,6 +2,10 @@
<taskdef resource="org/jetbrains/jet/buildtools/ant/antlib.xml" classpath="${kotlin.lib}/kotlin-ant.jar"/>
<target name="build">
<kotlinc src="${test.data}/hello.kt" output="${temp}/hello.jar" inline="wrong"/>
<kotlinc src="${test.data}/hello.kt" output="${temp}/hello.jar">
<compilerarg value="-Xno-inline"/>
<compilerarg line="-Xno-call-assertions -Xno-param-assertions"/>
<compilerarg value="-Xno-optimize"/>
</kotlinc>
</target>
</project>
@@ -0,0 +1,7 @@
package hello
fun main(args : Array<String>) {
for (s in listOf("a")) {
println("Hello, $s!")
}
}
@@ -0,0 +1,14 @@
OUT:
Buildfile: [TestData]/build.xml
build:
[kotlinc] Compiling [[[TestData]/hello.kt]] => [[Temp]/hello.jar]
ERR:
BUILD FAILED
[TestData]/build.xml:5: Invalid argument: -option-never-to-be-supported
Total time: [time]
Return code: 1
@@ -2,6 +2,8 @@
<taskdef resource="org/jetbrains/jet/buildtools/ant/antlib.xml" classpath="${kotlin.lib}/kotlin-ant.jar"/>
<target name="build">
<kotlinc src="${test.data}/hello.kt" output="${temp}/hello.jar" inline="false"/>
<kotlinc src="${test.data}/hello.kt" output="${temp}/hello.jar">
<compilerarg value="-option-never-to-be-supported"/>
</kotlinc>
</target>
</project>
@@ -0,0 +1,4 @@
package hello
fun main(args : Array<String>) {
}
@@ -1,7 +0,0 @@
package hello
fun main(args : Array<String>) {
for (s in arrayList("a"))
println("Hello, $s!")
}
@@ -1,31 +0,0 @@
OUT:
Buildfile: [TestData]/build.xml
build:
ERR:
BUILD FAILED
[TestData]/build.xml:5: org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentException: Wrong value for inline option: 'wrong'. Should be 'on'/'off' or 'true'/'false'
at org.jetbrains.jet.buildtools.ant.BytecodeCompilerTask.execute(BytecodeCompilerTask.java:153)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1360)
at org.apache.tools.ant.Project.executeTarget(Project.java:1329)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1212)
at org.apache.tools.ant.Main.runBuild(Main.java:801)
at org.apache.tools.ant.Main.startAnt(Main.java:218)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Total time: [time]
Return code: 1
@@ -1,7 +0,0 @@
package hello
fun main(args : Array<String>) {
for (s in arrayList("a"))
println("Hello, $s!")
}