diff --git a/build-tools/ant/src/org/jetbrains/jet/buildtools/ant/BytecodeCompilerTask.java b/build-tools/ant/src/org/jetbrains/jet/buildtools/ant/BytecodeCompilerTask.java index fd9332ad389..53be409abd5 100644 --- a/build-tools/ant/src/org/jetbrains/jet/buildtools/ant/BytecodeCompilerTask.java +++ b/build-tools/ant/src/org/jetbrains/jet/buildtools/ant/BytecodeCompilerTask.java @@ -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 additionalArguments = new ArrayList(); 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 args = new ArrayList(); + 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"); diff --git a/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java b/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java index b6c8f84e636..37cda4ea5ab 100644 --- a/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java +++ b/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java @@ -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 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 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 null 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 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 null 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 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 null 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 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) { diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java index befc144676e..426051d5649 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java @@ -100,10 +100,7 @@ public class K2JVMCompiler extends CLICompiler { ? CommandLineScriptUtils.scriptParameters() : Collections.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 { } } + 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 diff --git a/compiler/integration-tests/src/org/jetbrains/kotlin/AntTaskTest.java b/compiler/integration-tests/src/org/jetbrains/kotlin/AntTaskTest.java index d1756ff0f3e..b284bf824e4 100644 --- a/compiler/integration-tests/src/org/jetbrains/kotlin/AntTaskTest.java +++ b/compiler/integration-tests/src/org/jetbrains/kotlin/AntTaskTest.java @@ -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); } diff --git a/compiler/integration-tests/testData/inlineDisabled/build.log.expected b/compiler/integration-tests/testData/antAdditionalArguments/build.log.expected similarity index 100% rename from compiler/integration-tests/testData/inlineDisabled/build.log.expected rename to compiler/integration-tests/testData/antAdditionalArguments/build.log.expected diff --git a/compiler/integration-tests/testData/inlineWrongArg/build.xml b/compiler/integration-tests/testData/antAdditionalArguments/build.xml similarity index 57% rename from compiler/integration-tests/testData/inlineWrongArg/build.xml rename to compiler/integration-tests/testData/antAdditionalArguments/build.xml index 7023c02f096..adb0a9839fa 100644 --- a/compiler/integration-tests/testData/inlineWrongArg/build.xml +++ b/compiler/integration-tests/testData/antAdditionalArguments/build.xml @@ -2,6 +2,10 @@ - + + + + + diff --git a/compiler/integration-tests/testData/antAdditionalArguments/hello.kt b/compiler/integration-tests/testData/antAdditionalArguments/hello.kt new file mode 100644 index 00000000000..dea9cf93f9d --- /dev/null +++ b/compiler/integration-tests/testData/antAdditionalArguments/hello.kt @@ -0,0 +1,7 @@ +package hello + +fun main(args : Array) { + for (s in listOf("a")) { + println("Hello, $s!") + } +} diff --git a/compiler/integration-tests/testData/inlineDisabled/hello.run.expected b/compiler/integration-tests/testData/antAdditionalArguments/hello.run.expected similarity index 100% rename from compiler/integration-tests/testData/inlineDisabled/hello.run.expected rename to compiler/integration-tests/testData/antAdditionalArguments/hello.run.expected diff --git a/compiler/integration-tests/testData/antWrongArguments/build.log.expected b/compiler/integration-tests/testData/antWrongArguments/build.log.expected new file mode 100644 index 00000000000..68ea6c0dcea --- /dev/null +++ b/compiler/integration-tests/testData/antWrongArguments/build.log.expected @@ -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 diff --git a/compiler/integration-tests/testData/inlineDisabled/build.xml b/compiler/integration-tests/testData/antWrongArguments/build.xml similarity index 74% rename from compiler/integration-tests/testData/inlineDisabled/build.xml rename to compiler/integration-tests/testData/antWrongArguments/build.xml index 018d9dbe233..33b6930c19b 100644 --- a/compiler/integration-tests/testData/inlineDisabled/build.xml +++ b/compiler/integration-tests/testData/antWrongArguments/build.xml @@ -2,6 +2,8 @@ - + + + diff --git a/compiler/integration-tests/testData/antWrongArguments/hello.kt b/compiler/integration-tests/testData/antWrongArguments/hello.kt new file mode 100644 index 00000000000..952c9795d52 --- /dev/null +++ b/compiler/integration-tests/testData/antWrongArguments/hello.kt @@ -0,0 +1,4 @@ +package hello + +fun main(args : Array) { +} diff --git a/compiler/integration-tests/testData/inlineDisabled/hello.kt b/compiler/integration-tests/testData/inlineDisabled/hello.kt deleted file mode 100644 index 5a1704e14e7..00000000000 --- a/compiler/integration-tests/testData/inlineDisabled/hello.kt +++ /dev/null @@ -1,7 +0,0 @@ -package hello - -fun main(args : Array) { - for (s in arrayList("a")) - println("Hello, $s!") - -} diff --git a/compiler/integration-tests/testData/inlineWrongArg/build.log.expected b/compiler/integration-tests/testData/inlineWrongArg/build.log.expected deleted file mode 100644 index c58046fb685..00000000000 --- a/compiler/integration-tests/testData/inlineWrongArg/build.log.expected +++ /dev/null @@ -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 diff --git a/compiler/integration-tests/testData/inlineWrongArg/hello.kt b/compiler/integration-tests/testData/inlineWrongArg/hello.kt deleted file mode 100644 index 5a1704e14e7..00000000000 --- a/compiler/integration-tests/testData/inlineWrongArg/hello.kt +++ /dev/null @@ -1,7 +0,0 @@ -package hello - -fun main(args : Array) { - for (s in arrayList("a")) - println("Hello, $s!") - -}