| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library fasta.compiler_command_line; | |
| 6 | |
| 7 import 'dart:io' show exit; | |
| 8 | |
| 9 import 'package:kernel/target/targets.dart' | |
| 10 show Target, getTarget, TargetFlags, targets; | |
| 11 | |
| 12 import '../../compiler_options.dart'; | |
| 13 import '../base/processed_options.dart'; | |
| 14 import 'command_line.dart' show CommandLine, deprecated_argumentError; | |
| 15 | |
| 16 import 'compiler_context.dart' show CompilerContext; | |
| 17 | |
| 18 import 'fasta_codes.dart' | |
| 19 show | |
| 20 Message, | |
| 21 messageFastaUsageLong, | |
| 22 messageFastaUsageShort, | |
| 23 templateUnspecified; | |
| 24 | |
| 25 const Map<String, dynamic> optionSpecification = const <String, dynamic>{ | |
| 26 "--compile-sdk": Uri, | |
| 27 "--fatal": ",", | |
| 28 "--output": Uri, | |
| 29 "-o": Uri, | |
| 30 "--packages": Uri, | |
| 31 "--platform": Uri, | |
| 32 "--sdk": Uri, | |
| 33 "--target": String, | |
| 34 "-t": String, | |
| 35 }; | |
| 36 | |
| 37 /// Parser for options accepted by the `fasta` command-line tools. | |
| 38 // TODO(ahe,sigmund): move this and other tools under pkg/front_end/tool/ | |
| 39 class CompilerCommandLine extends CommandLine { | |
| 40 final String programName; | |
| 41 | |
| 42 CompilerCommandLine(String programName, List<String> arguments) | |
| 43 : programName = programName, | |
| 44 super(arguments, | |
| 45 specification: optionSpecification, | |
| 46 usage: computeUsage(programName, false)); | |
| 47 | |
| 48 bool get verify => options.containsKey("--verify"); | |
| 49 | |
| 50 bool get dumpIr => options.containsKey("--dump-ir"); | |
| 51 | |
| 52 bool get excludeSource => options.containsKey("--exclude-source"); | |
| 53 | |
| 54 bool get help { | |
| 55 return options.containsKey("--help") || | |
| 56 options.containsKey("-h") || | |
| 57 options.containsKey("/h") || | |
| 58 options.containsKey("/?"); | |
| 59 } | |
| 60 | |
| 61 void validate() { | |
| 62 if (help) { | |
| 63 print(computeUsage(programName, verbose).message); | |
| 64 exit(0); | |
| 65 } | |
| 66 | |
| 67 if (options.containsKey("-o") && options.containsKey("--output")) { | |
| 68 return deprecated_argumentError( | |
| 69 usage, "Can't specify both '-o' and '--output'."); | |
| 70 } | |
| 71 if (options.containsKey("-t") && options.containsKey("--target")) { | |
| 72 return deprecated_argumentError( | |
| 73 usage, "Can't specify both '-t' and '--target'."); | |
| 74 } | |
| 75 if (options.containsKey("--compile-sdk") && | |
| 76 options.containsKey("--platform")) { | |
| 77 return deprecated_argumentError( | |
| 78 usage, "Can't specify both '--compile-sdk' and '--platform'."); | |
| 79 } | |
| 80 if (programName == "compile_platform") { | |
| 81 if (arguments.length != 3) { | |
| 82 return deprecated_argumentError(usage, "Expected three arguments."); | |
| 83 } | |
| 84 if (options.containsKey("--compile-sdk")) { | |
| 85 return deprecated_argumentError(usage, | |
| 86 "Cannot specify '--compile-sdk' option to compile_platform."); | |
| 87 } | |
| 88 options['--compile-sdk'] = | |
| 89 Uri.base.resolveUri(new Uri.file(arguments[0])); | |
| 90 } else if (arguments.isEmpty) { | |
| 91 return deprecated_argumentError(usage, "No Dart file specified."); | |
| 92 } | |
| 93 | |
| 94 Target target = | |
| 95 getTarget(targetName, new TargetFlags(strongMode: strongMode)); | |
| 96 if (target == null) { | |
| 97 return deprecated_argumentError( | |
| 98 usage, | |
| 99 "Target '${targetName}' not recognized. " | |
| 100 "Valid targets are:\n ${targets.keys.join("\n ")}"); | |
| 101 } | |
| 102 options["target"] = target; | |
| 103 } | |
| 104 | |
| 105 Uri get output { | |
| 106 return options["-o"] ?? options["--output"] ?? defaultOutput; | |
| 107 } | |
| 108 | |
| 109 Uri get defaultOutput => Uri.base.resolve("${arguments.first}.dill"); | |
| 110 | |
| 111 Uri get platform { | |
| 112 return options.containsKey("--compile-sdk") | |
| 113 ? null | |
| 114 : options["--platform"] ?? Uri.base.resolve("platform.dill"); | |
| 115 } | |
| 116 | |
| 117 Uri get packages => options["--packages"]; | |
| 118 | |
| 119 Uri get sdk => options["--sdk"] ?? options["--compile-sdk"]; | |
| 120 | |
| 121 Set<String> get fatal { | |
| 122 return new Set<String>.from(options["--fatal"] ?? <String>[]); | |
| 123 } | |
| 124 | |
| 125 bool get errorsAreFatal => fatal.contains("errors"); | |
| 126 | |
| 127 bool get warningsAreFatal => fatal.contains("warnings"); | |
| 128 | |
| 129 bool get nitsAreFatal => fatal.contains("nits"); | |
| 130 | |
| 131 bool get strongMode => options.containsKey("--strong-mode"); | |
| 132 | |
| 133 String get targetName { | |
| 134 return options["-t"] ?? options["--target"] ?? "vm_fasta"; | |
| 135 } | |
| 136 | |
| 137 Target get target => options["target"]; | |
| 138 | |
| 139 static dynamic withGlobalOptions( | |
| 140 String programName, | |
| 141 List<String> arguments, | |
| 142 bool areRestArgumentsInputs, | |
| 143 dynamic f(CompilerContext context, List<String> restArguments)) { | |
| 144 // TODO(sigmund,ahe): delete this wrapper by moving validation into the | |
| 145 // callback. Note that this requires some subtle changes because validate | |
| 146 // sets some implicit options (like --compile-sdk in compile_platform). | |
| 147 var cl = CompilerContext.runWithDefaultOptions( | |
| 148 (_) => new CompilerCommandLine(programName, arguments)); | |
| 149 var options = new CompilerOptions() | |
| 150 ..compileSdk = cl.options.containsKey("--compile-sdk") | |
| 151 ..sdkRoot = cl.sdk | |
| 152 ..sdkSummary = cl.platform | |
| 153 ..packagesFileUri = cl.packages | |
| 154 ..strongMode = cl.strongMode | |
| 155 ..target = cl.target | |
| 156 ..throwOnErrors = cl.errorsAreFatal | |
| 157 ..throwOnWarnings = cl.warningsAreFatal | |
| 158 ..throwOnNits = cl.nitsAreFatal | |
| 159 ..embedSourceText = !cl.excludeSource | |
| 160 // All command-line tools take only a single entry point and chase | |
| 161 // dependencies, and provide a non-zero exit code when errors are found. | |
| 162 ..chaseDependencies = true | |
| 163 ..setExitCodeOnProblem = true | |
| 164 ..debugDump = cl.dumpIr | |
| 165 ..verbose = cl.verbose | |
| 166 ..verify = cl.verify; | |
| 167 | |
| 168 var inputs = <Uri>[]; | |
| 169 if (areRestArgumentsInputs) { | |
| 170 inputs = cl.arguments.map(Uri.base.resolve).toList(); | |
| 171 } | |
| 172 var pOptions = new ProcessedOptions(options, false, inputs, cl.output); | |
| 173 return CompilerContext.runWithOptions(pOptions, (c) => f(c, cl.arguments)); | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 Message computeUsage(String programName, bool verbose) { | |
| 178 String basicUsage = "Usage: $programName [options] dartfile\n"; | |
| 179 String summary; | |
| 180 String options = | |
| 181 (verbose ? messageFastaUsageLong.message : messageFastaUsageShort.message) | |
| 182 .trim(); | |
| 183 switch (programName) { | |
| 184 case "outline": | |
| 185 summary = | |
| 186 "Creates an outline of a Dart program in the Dill/Kernel IR format."; | |
| 187 break; | |
| 188 | |
| 189 case "compile": | |
| 190 summary = "Compiles a Dart program to the Dill/Kernel IR format."; | |
| 191 break; | |
| 192 | |
| 193 case "run": | |
| 194 summary = "Runs a Dart program."; | |
| 195 break; | |
| 196 | |
| 197 case "compile_platform": | |
| 198 summary = "Compiles Dart SDK platform to the Dill/Kernel IR format."; | |
| 199 basicUsage = "Usage: $programName [options] patched_sdk fullOutput " | |
| 200 "outlineOutput\n"; | |
| 201 } | |
| 202 StringBuffer sb = new StringBuffer(basicUsage); | |
| 203 if (summary != null) { | |
| 204 sb.writeln(); | |
| 205 sb.writeln(summary); | |
| 206 sb.writeln(); | |
| 207 } | |
| 208 sb.write(options); | |
| 209 // TODO(ahe): Don't use [templateUnspecified]. | |
| 210 return templateUnspecified.withArguments("$sb"); | |
| 211 } | |
| OLD | NEW |