CLI: Support "-X" advanced options, simplify some boolean options

This commit is contained in:
Alexander Udalov
2014-07-30 11:10:26 -07:00
parent 991c33d236
commit 160cde09d6
30 changed files with 93 additions and 303 deletions
@@ -41,6 +41,9 @@ public abstract class CommonCompilerArguments {
@ValueDescription(SUPPRESS_WARNINGS)
public String suppress;
@Argument(value = "X", description = "Print a synopsis of advanced options")
public boolean extraHelp;
public List<String> freeArgs = new SmartList<String>();
public boolean suppressAllWarnings() {
@@ -48,12 +48,6 @@ public class K2JVMCompilerArguments extends CommonCompilerArguments {
@Argument(value = "noJdkAnnotations", description = "Don't include JDK external annotations into classpath")
public boolean noJdkAnnotations;
@Argument(value = "notNullAssertions", description = "Generate not-null assertion after each invocation of method returning not-null")
public boolean notNullAssertions;
@Argument(value = "notNullParamAssertions", description = "Generate not-null assertions on parameters of methods accessible from Java")
public boolean notNullParamAssertions;
@Argument(value = "module", description = "Path to the module file to compile")
@ValueDescription("<path>")
public String module;
@@ -65,13 +59,19 @@ public class K2JVMCompilerArguments extends CommonCompilerArguments {
@ValueDescription("<path>")
public String kotlinHome;
@Argument(value = "inline", description = "Inlining mode (default is on)")
@ValueDescription("{on,off}")
public String inline;
// Advanced options
@Argument(value = "optimize", description = "Optimization mode (default is on)")
@ValueDescription("{on,off}")
public String optimize;
@Argument(value = "Xno-call-assertions", description = "Don't generate not-null assertion after each invocation of method returning not-null")
public boolean noCallAssertions;
@Argument(value = "Xno-param-assertions", description = "Don't generate not-null assertions on parameters of methods accessible from Java")
public boolean noParamAssertions;
@Argument(value = "Xno-inline", description = "Disable method inlining")
public boolean noInline;
@Argument(value = "Xno-optimize", description = "Disable optimizations")
public boolean noOptimize;
@Override
@NotNull
@@ -61,12 +61,11 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
protected boolean parseArguments(@NotNull PrintStream errStream, @NotNull A arguments, @NotNull String[] args) {
try {
arguments.freeArgs = Args.parse(arguments, args);
checkArguments(arguments);
return true;
}
catch (IllegalArgumentException e) {
errStream.println(e.getMessage());
usage(errStream);
usage(errStream, false);
}
catch (Throwable t) {
// Always use tags
@@ -75,15 +74,11 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
return false;
}
protected void checkArguments(@NotNull A argument) {
}
/**
* Allow derived classes to add additional command line arguments
*/
protected void usage(@NotNull PrintStream target) {
Usage.print(target, createArguments());
protected void usage(@NotNull PrintStream target, boolean extraHelp) {
Usage.print(target, createArguments(), extraHelp);
}
/**
@@ -102,8 +97,8 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
*/
@NotNull
public ExitCode exec(@NotNull PrintStream errStream, @NotNull A arguments) {
if (arguments.help) {
usage(errStream);
if (arguments.help || arguments.extraHelp) {
usage(errStream, arguments.extraHelp);
return OK;
}
@@ -29,35 +29,39 @@ class Usage {
// The magic number 29 corresponds to the similar padding width in javac and scalac command line compilers
private static final int OPTION_NAME_PADDING_WIDTH = 29;
public static void print(@NotNull PrintStream target, @NotNull CommonCompilerArguments arguments) {
public static void print(@NotNull PrintStream target, @NotNull CommonCompilerArguments arguments, boolean extraHelp) {
target.println("Usage: " + arguments.executableScriptFileName() + " <options> <source files>");
target.println("where possible options include:");
target.println("where " + (extraHelp ? "advanced" : "possible") + " options include:");
for (Class<?> clazz = arguments.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
for (Field field : clazz.getDeclaredFields()) {
String usage = fieldUsage(field);
String usage = fieldUsage(field, extraHelp);
if (usage != null) {
target.println(usage);
}
}
}
if (extraHelp) {
target.println();
target.println("Advanced options are non-standard and may be changed or removed without any notice.");
}
}
@Nullable
private static String fieldUsage(@NotNull Field field) {
private static String fieldUsage(@NotNull Field field, boolean extraHelp) {
Argument argument = field.getAnnotation(Argument.class);
if (argument == null) return null;
ValueDescription description = field.getAnnotation(ValueDescription.class);
String value = argument.value();
boolean extraOption = value.startsWith("X") && value.length() > 1;
if (extraHelp != extraOption) return null;
String prefix = argument.prefix();
StringBuilder sb = new StringBuilder(" ");
sb.append(prefix);
if (argument.value().isEmpty()) {
sb.append(field.getName());
}
else {
sb.append(argument.value());
}
sb.append(value);
if (!argument.alias().isEmpty()) {
sb.append(" (");
sb.append(prefix);
@@ -23,7 +23,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cli.common.CLICompiler;
import org.jetbrains.jet.cli.common.CLIConfigurationKeys;
import org.jetbrains.jet.cli.common.ExitCode;
import org.jetbrains.jet.cli.common.arguments.CompilerArgumentsUtil;
import org.jetbrains.jet.cli.common.arguments.K2JVMCompilerArguments;
import org.jetbrains.jet.cli.common.messages.*;
import org.jetbrains.jet.cli.common.modules.ModuleScriptData;
@@ -33,8 +32,6 @@ import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler;
import org.jetbrains.jet.cli.jvm.repl.ReplFromTerminal;
import org.jetbrains.jet.codegen.CompilationException;
import org.jetbrains.jet.codegen.inline.InlineCodegenUtil;
import org.jetbrains.jet.codegen.optimization.OptimizationUtils;
import org.jetbrains.jet.config.CommonConfigurationKeys;
import org.jetbrains.jet.config.CompilerConfiguration;
import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter;
@@ -103,12 +100,10 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
? CommandLineScriptUtils.scriptParameters()
: Collections.<AnalyzerScriptParameter>emptyList());
configuration.put(JVMConfigurationKeys.GENERATE_NOT_NULL_ASSERTIONS, arguments.notNullAssertions);
configuration.put(JVMConfigurationKeys.GENERATE_NOT_NULL_PARAMETER_ASSERTIONS, arguments.notNullParamAssertions);
configuration.put(JVMConfigurationKeys.ENABLE_INLINE,
CompilerArgumentsUtil.optionToBooleanFlag(arguments.inline, InlineCodegenUtil.DEFAULT_INLINE_FLAG));
configuration.put(JVMConfigurationKeys.ENABLE_OPTIMIZATION,
CompilerArgumentsUtil.optionToBooleanFlag(arguments.optimize, OptimizationUtils.DEFAULT_OPTIMIZATION_FLAG));
configuration.put(JVMConfigurationKeys.GENERATE_NOT_NULL_ASSERTIONS, !arguments.noCallAssertions);
configuration.put(JVMConfigurationKeys.GENERATE_NOT_NULL_PARAMETER_ASSERTIONS, !arguments.noParamAssertions);
configuration.put(JVMConfigurationKeys.ENABLE_INLINE, !arguments.noInline);
configuration.put(JVMConfigurationKeys.ENABLE_OPTIMIZATION, !arguments.noOptimize);
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector);
@@ -208,18 +203,4 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
}
return annotationsPath;
}
@Override
protected void checkArguments(@NotNull K2JVMCompilerArguments argument) {
super.checkArguments(argument);
if (!CompilerArgumentsUtil.checkOption(argument.inline)) {
throw new IllegalArgumentException(CompilerArgumentsUtil.getWrongCheckOptionErrorMessage("inline", argument.inline));
}
if (!CompilerArgumentsUtil.checkOption(argument.optimize)) {
throw new IllegalArgumentException(CompilerArgumentsUtil.getWrongCheckOptionErrorMessage("optimize", argument.optimize));
}
}
}