Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 1 | #!/usr/bin/env dart |
| 2 | // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 3 | // for details. All rights reserved. Use of this source code is governed by a |
| 4 | // BSD-style license that can be found in the LICENSE file. |
Martin Kustermann | 5f7ed25 | 2016-08-05 11:27:22 | [diff] [blame] | 5 | |
Asger Feldthaus | a44bf70 | 2016-06-28 09:15:48 | [diff] [blame] | 6 | import 'dart:async'; |
| 7 | import 'dart:io'; |
Martin Kustermann | 5f7ed25 | 2016-08-05 11:27:22 | [diff] [blame] | 8 | |
| 9 | import 'batch_util.dart'; |
| 10 | |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 11 | import 'package:args/args.dart'; |
Asger Feldthaus | c9244b4 | 2016-07-22 15:42:32 | [diff] [blame] | 12 | import 'package:kernel/analyzer/loader.dart'; |
Asger Feldthaus | a44bf70 | 2016-06-28 09:15:48 | [diff] [blame] | 13 | import 'package:kernel/checks.dart'; |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 14 | import 'package:kernel/kernel.dart'; |
| 15 | import 'package:kernel/log.dart'; |
Asger Feldthaus | 760da8e | 2016-08-24 14:46:50 | [diff] [blame] | 16 | import 'package:kernel/target/targets.dart'; |
Martin Kustermann | 5f7ed25 | 2016-08-05 11:27:22 | [diff] [blame] | 17 | import 'package:path/path.dart' as path; |
Martin Kustermann | 5f7ed25 | 2016-08-05 11:27:22 | [diff] [blame] | 18 | |
| 19 | // Returns the path to the current sdk based on `Platform.resolvedExecutable`. |
| 20 | String currentSdk() { |
| 21 | // The dart executable should be inside dart-sdk/bin/dart. |
| 22 | return path.dirname(path.dirname(path.absolute(Platform.resolvedExecutable))); |
| 23 | } |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 24 | |
Asger Feldthaus | dee38eb | 2016-07-20 12:57:59 | [diff] [blame] | 25 | ArgParser parser = new ArgParser(allowTrailingOptions: true) |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 26 | ..addOption('format', |
| 27 | abbr: 'f', |
| 28 | allowed: ['text', 'bin'], |
| 29 | help: 'Output format.\n' |
Asger Feldthaus | 5ad5fa8 | 2016-07-19 15:52:33 | [diff] [blame] | 30 | '(defaults to "text" unless output file ends with ".dill")') |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 31 | ..addOption('out', |
| 32 | abbr: 'o', |
| 33 | help: 'Output file.\n' |
Asger Feldthaus | 5ad5fa8 | 2016-07-19 15:52:33 | [diff] [blame] | 34 | '(defaults to "out.dill" if format is "bin", otherwise stdout)') |
Asger Feldthaus | 760da8e | 2016-08-24 14:46:50 | [diff] [blame] | 35 | ..addOption('sdk', defaultsTo: currentSdk(), help: 'Path to the Dart SDK.') |
Asger Feldthaus | c7cbf95 | 2016-09-22 12:18:18 | [diff] [blame] | 36 | ..addOption('packages', |
| 37 | abbr: 'p', help: 'Path to the .packages file or packages folder.') |
| 38 | ..addOption('package-root', help: 'Deprecated alias for --packages') |
Asger Feldthaus | 760da8e | 2016-08-24 14:46:50 | [diff] [blame] | 39 | ..addOption('target', |
| 40 | abbr: 't', |
| 41 | help: 'Tailor the IR to the given target.', |
Asger Feldthaus | 6851cc4 | 2016-08-30 09:26:05 | [diff] [blame] | 42 | allowed: targetNames, |
| 43 | defaultsTo: 'vm') |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 44 | ..addFlag('strong', |
| 45 | help: 'Load .dart files in strong mode.\n' |
| 46 | 'Does not affect loading of binary files. Strong mode support is very\n' |
| 47 | 'unstable and not well integrated yet.') |
| 48 | ..addFlag('link', abbr: 'l', help: 'Link the whole program into one file.') |
| 49 | ..addFlag('no-output', negatable: false, help: 'Do not output any files.') |
Martin Kustermann | 87c67ed | 2016-09-22 11:16:08 | [diff] [blame] | 50 | ..addOption('url-mapping', |
| 51 | allowMultiple: true, |
| 52 | help: 'A custom url mapping of the form `<scheme>:<name>::<uri>`.') |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 53 | ..addFlag('verbose', |
| 54 | abbr: 'v', |
| 55 | negatable: false, |
| 56 | help: 'Print internal warnings and diagnostics to stderr.') |
| 57 | ..addFlag('print-metrics', |
| 58 | negatable: false, help: 'Print performance metrics.') |
| 59 | ..addOption('write-dependencies', |
Asger Feldthaus | a44bf70 | 2016-06-28 09:15:48 | [diff] [blame] | 60 | help: 'Write all the .dart that were loaded to the given file.') |
Asger Feldthaus | c9244b4 | 2016-07-22 15:42:32 | [diff] [blame] | 61 | ..addFlag('sanity-check', help: 'Perform slow internal correctness checks.') |
| 62 | ..addFlag('tolerant', |
| 63 | help: 'Generate kernel even if there are compile-time errors.', |
Asger Feldthaus | df39706 | 2016-09-23 10:28:33 | [diff] [blame] | 64 | defaultsTo: false) |
| 65 | ..addOption('D', |
| 66 | abbr: 'D', |
| 67 | allowMultiple: true, |
| 68 | help: 'Define an environment variable.', |
Asger Feldthaus | 3ac36ac | 2016-10-04 11:30:46 | [diff] [blame] | 69 | hide: true) |
| 70 | ..addFlag('show-external', |
| 71 | help: 'When printing a library as text, also print its dependencies\n' |
| 72 | 'on external libraries.'); |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 73 | |
| 74 | String getUsage() => """ |
| 75 | Usage: dartk [options] FILE |
| 76 | |
Asger Feldthaus | 5ad5fa8 | 2016-07-19 15:52:33 | [diff] [blame] | 77 | Convert .dart or .dill files to kernel's IR and print out its textual |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 78 | or binary form. |
| 79 | |
| 80 | Examples: |
| 81 | dartk foo.dart # print text IR for foo.dart |
Asger Feldthaus | 5ad5fa8 | 2016-07-19 15:52:33 | [diff] [blame] | 82 | dartk foo.dart -ofoo.dill # write binary IR for foo.dart to foo.dill |
| 83 | dartk foo.dill # print text IR for binary file foo.dill |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 84 | |
| 85 | Options: |
| 86 | ${parser.usage} |
Asger Feldthaus | df39706 | 2016-09-23 10:28:33 | [diff] [blame] | 87 | |
| 88 | -D<name>=<value> Define an environment variable. |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 89 | """; |
| 90 | |
| 91 | dynamic fail(String message) { |
| 92 | stderr.writeln(message); |
| 93 | exit(1); |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | ArgResults options; |
| 98 | |
| 99 | String defaultFormat() { |
Asger Feldthaus | 5ad5fa8 | 2016-07-19 15:52:33 | [diff] [blame] | 100 | if (options['out'] != null && options['out'].endsWith('.dill')) { |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 101 | return 'bin'; |
| 102 | } |
| 103 | return 'text'; |
| 104 | } |
| 105 | |
| 106 | String defaultOutput() { |
| 107 | if (options['format'] == 'bin') { |
Asger Feldthaus | 5ad5fa8 | 2016-07-19 15:52:33 | [diff] [blame] | 108 | return 'out.dill'; |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 109 | } |
| 110 | return null; |
| 111 | } |
| 112 | |
| 113 | void checkIsDirectoryOrNull(String path, String option) { |
| 114 | if (path == null) return; |
| 115 | var stat = new File(path).statSync(); |
| 116 | switch (stat.type) { |
| 117 | case FileSystemEntityType.DIRECTORY: |
| 118 | case FileSystemEntityType.LINK: |
| 119 | return; |
| 120 | case FileSystemEntityType.NOT_FOUND: |
| 121 | throw fail('$option not found: $path'); |
| 122 | default: |
Asger Feldthaus | c7cbf95 | 2016-09-22 12:18:18 | [diff] [blame] | 123 | fail('$option is not a directory: $path'); |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | void checkIsFile(String path, {String option}) { |
| 128 | var stat = new File(path).statSync(); |
| 129 | switch (stat.type) { |
| 130 | case FileSystemEntityType.DIRECTORY: |
| 131 | throw fail('$option is a directory: $path'); |
| 132 | |
| 133 | case FileSystemEntityType.NOT_FOUND: |
| 134 | throw fail('$option not found: $path'); |
| 135 | } |
| 136 | } |
| 137 | |
Asger Feldthaus | c7cbf95 | 2016-09-22 12:18:18 | [diff] [blame] | 138 | void checkIsFileOrDirectoryOrNull(String path, String option) { |
| 139 | if (path == null) return; |
| 140 | var stat = new File(path).statSync(); |
| 141 | if (stat.type == FileSystemEntityType.NOT_FOUND) { |
| 142 | fail('$option not found: $path'); |
| 143 | } |
| 144 | } |
| 145 | |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 146 | int getTotalSourceSize(List<String> files) { |
| 147 | int size = 0; |
| 148 | for (var filename in files) { |
| 149 | size += new File(filename).statSync().size; |
| 150 | } |
| 151 | return size; |
| 152 | } |
| 153 | |
| 154 | bool get shouldReportMetrics => options['print-metrics']; |
| 155 | |
| 156 | void dumpString(String value, [String filename]) { |
| 157 | if (filename == null) { |
| 158 | print(value); |
| 159 | } else { |
| 160 | new File(filename).writeAsStringSync(value); |
| 161 | } |
| 162 | } |
| 163 | |
Martin Kustermann | 87c67ed | 2016-09-22 11:16:08 | [diff] [blame] | 164 | Map<Uri, Uri> parseCustomUriMappings(List<String> mappings) { |
| 165 | Map<Uri, Uri> customUriMappings = <Uri, Uri>{}; |
| 166 | |
| 167 | fatal(String mapping) { |
| 168 | fail('Invalid uri mapping "$mapping". Each mapping should have the ' |
Asger Feldthaus | c7cbf95 | 2016-09-22 12:18:18 | [diff] [blame] | 169 | 'form "<scheme>:<name>::<uri>".'); |
Martin Kustermann | 87c67ed | 2016-09-22 11:16:08 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // Each mapping has the form <uri>::<uri>. |
| 173 | for (var mapping in mappings) { |
| 174 | List<String> parts = mapping.split('::'); |
| 175 | if (parts.length != 2) { |
| 176 | fatal(mapping); |
| 177 | } |
| 178 | Uri fromUri = Uri.parse(parts[0]); |
Asger Feldthaus | c7cbf95 | 2016-09-22 12:18:18 | [diff] [blame] | 179 | if (fromUri.scheme == '' || fromUri.path.contains('/')) { |
Martin Kustermann | 87c67ed | 2016-09-22 11:16:08 | [diff] [blame] | 180 | fatal(mapping); |
| 181 | } |
| 182 | Uri toUri = Uri.parse(parts[1]); |
| 183 | if (toUri.scheme == '') { |
| 184 | toUri = new Uri.file(path.absolute(parts[1])); |
| 185 | } |
| 186 | customUriMappings[fromUri] = toUri; |
| 187 | } |
| 188 | |
| 189 | return customUriMappings; |
| 190 | } |
| 191 | |
Asger Feldthaus | c9244b4 | 2016-07-22 15:42:32 | [diff] [blame] | 192 | /// Maintains state that should be shared between batched executions when |
| 193 | /// running in batch mode (for testing purposes). |
Asger Feldthaus | 60a8a7a | 2016-08-30 09:15:52 | [diff] [blame] | 194 | /// |
Asger Feldthaus | fc1d7b1 | 2016-09-07 11:15:58 | [diff] [blame] | 195 | /// This reuses the analyzer's in-memory copy of the Dart SDK between runs. |
Asger Feldthaus | c9244b4 | 2016-07-22 15:42:32 | [diff] [blame] | 196 | class BatchModeState { |
Asger Feldthaus | c7cbf95 | 2016-09-22 12:18:18 | [diff] [blame] | 197 | bool isBatchMode = false; |
Asger Feldthaus | 60928ce | 2016-09-23 14:11:41 | [diff] [blame] | 198 | DartLoaderBatch batch = new DartLoaderBatch(); |
Asger Feldthaus | c9244b4 | 2016-07-22 15:42:32 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | main(List<String> args) async { |
| 202 | if (args.isNotEmpty && args[0] == '--batch') { |
| 203 | if (args.length != 1) { |
| 204 | return fail('--batch cannot be used with other arguments'); |
| 205 | } |
Asger Feldthaus | c7cbf95 | 2016-09-22 12:18:18 | [diff] [blame] | 206 | var batchModeState = new BatchModeState()..isBatchMode = true; |
Asger Feldthaus | c9244b4 | 2016-07-22 15:42:32 | [diff] [blame] | 207 | await runBatch((args) => batchMain(args, batchModeState)); |
| 208 | } else { |
| 209 | CompilerOutcome outcome = await batchMain(args, new BatchModeState()); |
| 210 | exit(outcome == CompilerOutcome.Ok ? 0 : 1); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | bool isSupportedArgument(String arg) { |
| 215 | if (arg.startsWith('--')) { |
| 216 | int equals = arg.indexOf('='); |
| 217 | var name = equals != -1 ? arg.substring(2, equals) : arg.substring(2); |
| 218 | return parser.options.containsKey(name); |
| 219 | } |
| 220 | if (arg.startsWith('-')) { |
| 221 | return parser.findByAbbreviation(arg.substring(1)) != null; |
| 222 | } |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | Future<CompilerOutcome> batchMain( |
| 227 | List<String> args, BatchModeState batchModeState) async { |
| 228 | if (args.contains('--ignore-unrecognized-flags')) { |
| 229 | args = args.where(isSupportedArgument).toList(); |
| 230 | } |
| 231 | |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 232 | if (args.isEmpty) { |
| 233 | return fail(getUsage()); |
| 234 | } |
| 235 | |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 236 | try { |
| 237 | options = parser.parse(args); |
| 238 | } on FormatException catch (e) { |
| 239 | return fail(e.message); // Don't puke stack traces. |
| 240 | } |
| 241 | |
| 242 | checkIsDirectoryOrNull(options['sdk'], 'Dart SDK'); |
Asger Feldthaus | c7cbf95 | 2016-09-22 12:18:18 | [diff] [blame] | 243 | |
| 244 | String packagePath = options['packages'] ?? options['package-root']; |
| 245 | checkIsFileOrDirectoryOrNull(packagePath, 'Package root or .packages'); |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 246 | |
| 247 | // Set up logging. |
| 248 | if (options['verbose']) { |
| 249 | log.onRecord.listen((LogRecord rec) { |
| 250 | stderr.writeln(rec.message); |
| 251 | }); |
| 252 | } |
| 253 | |
Asger Feldthaus | dee38eb | 2016-07-20 12:57:59 | [diff] [blame] | 254 | if (options.rest.length != 1) { |
| 255 | return fail('Exactly one FILE should be given.'); |
| 256 | } |
| 257 | |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 258 | var file = options.rest.single; |
| 259 | |
| 260 | checkIsFile(file, option: 'Input file'); |
| 261 | |
| 262 | String format = options['format'] ?? defaultFormat(); |
| 263 | String outputFile = options['out'] ?? defaultOutput(); |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 264 | |
Martin Kustermann | 87c67ed | 2016-09-22 11:16:08 | [diff] [blame] | 265 | var customUriMappings = parseCustomUriMappings(options['url-mapping']); |
Asger Feldthaus | 60928ce | 2016-09-23 14:11:41 | [diff] [blame] | 266 | var repository = new Repository(); |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 267 | |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 268 | Program program; |
| 269 | |
| 270 | var watch = new Stopwatch()..start(); |
| 271 | List<String> loadedFiles; |
| 272 | Function getLoadedFiles; |
Asger Feldthaus | c9244b4 | 2016-07-22 15:42:32 | [diff] [blame] | 273 | List errors = const []; |
Asger Feldthaus | 144bd99 | 2016-10-07 13:40:46 | [diff] [blame] | 274 | TargetFlags targetFlags = new TargetFlags(strongMode: options['strong']); |
Asger Feldthaus | 9677d68 | 2016-09-09 15:49:34 | [diff] [blame] | 275 | Target target = getTarget(options['target'], targetFlags); |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 276 | |
Asger Feldthaus | df39706 | 2016-09-23 10:28:33 | [diff] [blame] | 277 | var declaredVariables = <String, String>{}; |
| 278 | declaredVariables.addAll(target.extraDeclaredVariables); |
| 279 | for (String define in options['D']) { |
| 280 | int separator = define.indexOf('='); |
| 281 | if (separator == -1) { |
| 282 | fail('Invalid define: -D$define. Format is -D<name>=<value>'); |
| 283 | } |
| 284 | String name = define.substring(0, separator); |
| 285 | String value = define.substring(separator + 1); |
| 286 | declaredVariables[name] = value; |
| 287 | } |
| 288 | |
Asger Feldthaus | 5ad5fa8 | 2016-07-19 15:52:33 | [diff] [blame] | 289 | if (file.endsWith('.dill')) { |
Asger Feldthaus | 3ac36ac | 2016-10-04 11:30:46 | [diff] [blame] | 290 | program = loadProgramFromBinary(file, repository); |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 291 | getLoadedFiles = () => [file]; |
| 292 | } else { |
Asger Feldthaus | 60928ce | 2016-09-23 14:11:41 | [diff] [blame] | 293 | DartOptions dartOptions = new DartOptions( |
Asger Feldthaus | 144bd99 | 2016-10-07 13:40:46 | [diff] [blame] | 294 | strongMode: target.strongMode, |
| 295 | strongModeSdk: target.strongModeSdk, |
Asger Feldthaus | 60928ce | 2016-09-23 14:11:41 | [diff] [blame] | 296 | sdk: options['sdk'], |
| 297 | packagePath: packagePath, |
| 298 | customUriMappings: customUriMappings, |
| 299 | declaredVariables: declaredVariables); |
| 300 | String packageDiscoveryPath = batchModeState.isBatchMode ? null : file; |
| 301 | DartLoader loader = await batchModeState.batch.getLoader( |
| 302 | repository, dartOptions, |
| 303 | packageDiscoveryPath: packageDiscoveryPath); |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 304 | if (options['link']) { |
Kevin Millikin | 420b00e | 2016-09-01 11:13:41 | [diff] [blame] | 305 | program = loader.loadProgram(file, target: target); |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 306 | } else { |
Asger Feldthaus | 3ac36ac | 2016-10-04 11:30:46 | [diff] [blame] | 307 | var library = loader.loadLibrary(file); |
| 308 | assert(library == repository.getLibrary(file)); |
| 309 | program = new Program(repository.libraries); |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 310 | } |
Asger Feldthaus | c9244b4 | 2016-07-22 15:42:32 | [diff] [blame] | 311 | errors = loader.errors; |
| 312 | if (errors.isNotEmpty) { |
Asger Feldthaus | eb3c3af | 2016-09-13 11:16:05 | [diff] [blame] | 313 | const int errorLimit = 100; |
| 314 | stderr.writeln(errors.take(errorLimit).join('\n')); |
| 315 | if (errors.length > errorLimit) { |
| 316 | stderr |
| 317 | .writeln('[error] ${errors.length - errorLimit} errors not shown'); |
| 318 | } |
Asger Feldthaus | c9244b4 | 2016-07-22 15:42:32 | [diff] [blame] | 319 | } |
| 320 | getLoadedFiles = () => loadedFiles ??= loader.getLoadedFileNames(); |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 321 | } |
| 322 | |
Asger Feldthaus | 760da8e | 2016-08-24 14:46:50 | [diff] [blame] | 323 | bool canContinueCompilation = errors.isEmpty || options['tolerant']; |
| 324 | |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 325 | int loadTime = watch.elapsedMilliseconds; |
| 326 | if (shouldReportMetrics) { |
| 327 | print('loader.time = $loadTime ms'); |
| 328 | } |
| 329 | |
Asger Feldthaus | 760da8e | 2016-08-24 14:46:50 | [diff] [blame] | 330 | void sanityCheck() { |
| 331 | if (options['sanity-check']) { |
Asger Feldthaus | d5fe5f1 | 2016-09-27 11:01:09 | [diff] [blame] | 332 | runSanityChecks(program); |
Asger Feldthaus | 760da8e | 2016-08-24 14:46:50 | [diff] [blame] | 333 | } |
Asger Feldthaus | a44bf70 | 2016-06-28 09:15:48 | [diff] [blame] | 334 | } |
| 335 | |
Asger Feldthaus | 760da8e | 2016-08-24 14:46:50 | [diff] [blame] | 336 | sanityCheck(); |
| 337 | |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 338 | String outputDependencies = options['write-dependencies']; |
| 339 | if (outputDependencies != null) { |
| 340 | new File(outputDependencies).writeAsStringSync(getLoadedFiles().join('\n')); |
| 341 | } |
| 342 | |
Asger Feldthaus | 760da8e | 2016-08-24 14:46:50 | [diff] [blame] | 343 | // Apply target-specific transformations. |
Asger Feldthaus | 3ac36ac | 2016-10-04 11:30:46 | [diff] [blame] | 344 | if (target != null && options['link'] && canContinueCompilation) { |
Asger Feldthaus | 760da8e | 2016-08-24 14:46:50 | [diff] [blame] | 345 | target.transformProgram(program); |
| 346 | sanityCheck(); |
| 347 | } |
| 348 | |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 349 | if (options['no-output']) { |
Asger Feldthaus | c9244b4 | 2016-07-22 15:42:32 | [diff] [blame] | 350 | return CompilerOutcome.Ok; |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | watch.reset(); |
| 354 | |
| 355 | Future ioFuture; |
Asger Feldthaus | 760da8e | 2016-08-24 14:46:50 | [diff] [blame] | 356 | if (canContinueCompilation) { |
Asger Feldthaus | c9244b4 | 2016-07-22 15:42:32 | [diff] [blame] | 357 | switch (format) { |
| 358 | case 'text': |
Asger Feldthaus | 3ac36ac | 2016-10-04 11:30:46 | [diff] [blame] | 359 | writeProgramToText(program, |
| 360 | path: outputFile, showExternal: options['show-external']); |
Asger Feldthaus | c9244b4 | 2016-07-22 15:42:32 | [diff] [blame] | 361 | break; |
| 362 | case 'bin': |
Asger Feldthaus | 3ac36ac | 2016-10-04 11:30:46 | [diff] [blame] | 363 | ioFuture = writeProgramToBinary(program, outputFile); |
Asger Feldthaus | c9244b4 | 2016-07-22 15:42:32 | [diff] [blame] | 364 | break; |
| 365 | } |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 366 | } |
| 367 | |
Asger Feldthaus | c9244b4 | 2016-07-22 15:42:32 | [diff] [blame] | 368 | int time = watch.elapsedMilliseconds; |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 369 | if (shouldReportMetrics) { |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 370 | print('writer.time = $time ms'); |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 371 | } |
Asger Feldthaus | c9244b4 | 2016-07-22 15:42:32 | [diff] [blame] | 372 | |
| 373 | await ioFuture; |
| 374 | |
| 375 | if (shouldReportMetrics) { |
| 376 | int flushTime = watch.elapsedMilliseconds - time; |
| 377 | print('writer.flush_time = $flushTime ms'); |
| 378 | } |
| 379 | |
| 380 | return errors.length > 0 ? CompilerOutcome.Fail : CompilerOutcome.Ok; |
Asger Feldthaus | 02604e9 | 2016-04-26 08:39:15 | [diff] [blame] | 381 | } |