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) {