Do not fail on unknown -X flags

For better compatibility if we add experimental stuff in 1.0.X compilers
This commit is contained in:
Alexander Udalov
2016-04-11 14:21:41 +03:00
parent 37d612d346
commit a8629b3836
8 changed files with 61 additions and 16 deletions
@@ -58,6 +58,8 @@ public abstract class CommonCompilerArguments {
public List<String> freeArgs = new SmartList<String>();
public List<String> unknownExtraFlags = new SmartList<String>();
@NotNull
public String executableScriptFileName() {
return "kotlinc";
@@ -22,6 +22,9 @@ import com.intellij.openapi.Disposable;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.SystemInfo;
import com.sampullara.cli.Args;
import kotlin.Pair;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.functions.Function1;
import org.fusesource.jansi.AnsiConsole;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -87,7 +90,7 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
private A parseArguments(@NotNull PrintStream errStream, @NotNull MessageRenderer messageRenderer, @NotNull String[] args) {
try {
A arguments = createArguments();
arguments.freeArgs = Args.parse(arguments, args);
parseArguments(args, arguments);
return arguments;
}
catch (IllegalArgumentException e) {
@@ -104,6 +107,26 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
return null;
}
@SuppressWarnings("WeakerAccess") // Used in maven (see KotlinCompileMojoBase.java)
public void parseArguments(@NotNull String[] args, @NotNull A arguments) {
Pair<List<String>, List<String>> unparsedArgs =
CollectionsKt.partition(Args.parse(arguments, args, false), new Function1<String, Boolean>() {
@Override
public Boolean invoke(String s) {
return s.startsWith("-X");
}
});
arguments.unknownExtraFlags = unparsedArgs.getFirst();
arguments.freeArgs = unparsedArgs.getSecond();
for (String argument : arguments.freeArgs) {
if (argument.startsWith("-")) {
throw new IllegalArgumentException("Invalid argument: " + argument);
}
}
}
/**
* Allow derived classes to add additional command line arguments
*/
@@ -169,6 +192,8 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
messageCollector = new FilteringMessageCollector(messageCollector, Predicates.equalTo(CompilerMessageSeverity.WARNING));
}
reportUnknownExtraFlags(messageCollector, arguments);
GroupingMessageCollector groupingCollector = new GroupingMessageCollector(messageCollector);
try {
ExitCode exitCode = OK;
@@ -226,6 +251,16 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
}
}
private void reportUnknownExtraFlags(@NotNull MessageCollector collector, @NotNull A arguments) {
for (String flag : arguments.unknownExtraFlags) {
collector.report(
CompilerMessageSeverity.WARNING,
"Flag is not supported by this version of the compiler: " + flag,
CompilerMessageLocation.NO_LOCATION
);
}
}
@NotNull
protected abstract ExitCode doExecute(
@NotNull A arguments,
+6
View File
@@ -0,0 +1,6 @@
-Xabcdefghijklm
$TESTDATA_DIR$/simple.kt
-Xnopqrstuvwxyz
-d
$TEMP_DIR$
-XXxxxxxxxxxxxx
+4
View File
@@ -0,0 +1,4 @@
warning: flag is not supported by this version of the compiler: -Xabcdefghijklm
warning: flag is not supported by this version of the compiler: -Xnopqrstuvwxyz
warning: flag is not supported by this version of the compiler: -XXxxxxxxxxxxxx
OK
@@ -241,6 +241,12 @@ public class CliTestGenerated extends AbstractCliTest {
doJvmTest(fileName);
}
@TestMetadata("unknownExtraFlags.args")
public void testUnknownExtraFlags() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/unknownExtraFlags.args");
doJvmTest(fileName);
}
@TestMetadata("version.args")
public void testVersion() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/version.args");
@@ -74,6 +74,7 @@
<arg>-Xno-optimize</arg>
<arg>-Xno-call-assertions</arg>
<arg>-Xno-param-assertions</arg>
<arg>-Xunknown-extra-argument-abcdefghijklmnopqrstuvwxyz</arg>
</args>
</configuration>
</plugin>
@@ -16,8 +16,6 @@
package org.jetbrains.kotlin.maven;
import com.intellij.util.ArrayUtil;
import com.sampullara.cli.Args;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.functions.Function1;
import org.apache.maven.artifact.Artifact;
@@ -100,13 +98,6 @@ public class K2JVMCompileMojo extends KotlinCompileMojoBase<K2JVMCompilerArgumen
arguments.moduleName = moduleName;
getLog().info("Module name is " + moduleName);
try {
Args.parse(arguments, ArrayUtil.toStringArray(args));
}
catch (IllegalArgumentException e) {
throw new MojoExecutionException(e.getMessage());
}
if (arguments.noOptimize) {
getLog().info("Optimization is turned off");
}
@@ -19,7 +19,6 @@ package org.jetbrains.kotlin.maven;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Processor;
import com.sampullara.cli.Args;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
@@ -115,9 +114,9 @@ public abstract class KotlinCompileMojoBase<A extends CommonCompilerArguments> e
}
A arguments = createCompilerArguments();
configureCompilerArguments(arguments);
CLICompiler<A> compiler = createCompiler();
configureCompilerArguments(arguments, compiler);
printCompilerArgumentsIfDebugEnabled(arguments, compiler);
MessageCollector messageCollector = new MessageCollector() {
@@ -224,7 +223,7 @@ public abstract class KotlinCompileMojoBase<A extends CommonCompilerArguments> e
*/
protected abstract void configureSpecificCompilerArguments(@NotNull A arguments) throws MojoExecutionException;
private void configureCompilerArguments(@NotNull A arguments) throws MojoExecutionException {
private void configureCompilerArguments(@NotNull A arguments, @NotNull CLICompiler<A> compiler) throws MojoExecutionException {
if (getLog().isDebugEnabled()) {
arguments.verbose = true;
}
@@ -245,18 +244,19 @@ public abstract class KotlinCompileMojoBase<A extends CommonCompilerArguments> e
arguments.suppressWarnings = nowarn;
arguments.freeArgs.addAll(sources);
getLog().info("Compiling Kotlin sources from " + sources);
configureSpecificCompilerArguments(arguments);
try {
Args.parse(arguments, ArrayUtil.toStringArray(args));
compiler.parseArguments(ArrayUtil.toStringArray(args), arguments);
}
catch (IllegalArgumentException e) {
throw new MojoExecutionException(e.getMessage());
}
arguments.freeArgs.addAll(sources);
if (arguments.noInline) {
getLog().info("Method inlining is turned off");
}