| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 | 4 |
| 5 library rasta.rastask; | 5 library rasta.rastask; |
| 6 | 6 |
| 7 import 'dart:async' show | 7 import 'dart:async' show |
| 8 Future; | 8 Future; |
| 9 | 9 |
| 10 import 'dart:isolate' show | 10 import 'dart:isolate' show |
| 11 Isolate; | 11 Isolate; |
| 12 | 12 |
| 13 import 'dart:io' show | 13 import 'dart:io' show |
| 14 File, | 14 File, |
| 15 IOSink; | 15 IOSink; |
| 16 | 16 |
| 17 import 'package:compiler/src/common/tasks.dart' show | 17 import 'package:compiler/src/common/tasks.dart' show |
| 18 CompilerTask; | 18 CompilerTask; |
| 19 | 19 |
| 20 import 'package:rasta/io_compiler_factory.dart' show | 20 import 'package:compiler/src/elements/elements.dart' show |
| 21 CompilationUnitElement, |
| 22 LibraryElement; |
| 23 |
| 24 import 'package:kernel/ast.dart' as ir; |
| 25 |
| 26 import 'package:kernel/binary/ast_to_binary.dart' show |
| 27 BinaryPrinter; |
| 28 |
| 29 import 'package:kernel/text/ast_to_text.dart' show |
| 30 Printer; |
| 31 |
| 32 import '../io_compiler_factory.dart' show |
| 21 IoCompilerFactory; | 33 IoCompilerFactory; |
| 22 | 34 |
| 23 import 'package:rasta/custom_compiler.dart' show | 35 import '../custom_compiler.dart' show |
| 24 CustomCompiler; | 36 CustomCompiler; |
| 25 | 37 |
| 38 import '../kernel.dart' show |
| 39 Kernel; |
| 40 |
| 26 import 'options.dart' show | 41 import 'options.dart' show |
| 27 OptionParser, | 42 OptionParser, |
| 28 Options; | 43 Options; |
| 29 | 44 |
| 30 import 'run_single.dart' show | 45 import 'run_single.dart' show |
| 31 RunSingle; | 46 RunSingle; |
| 32 | 47 |
| 33 import 'run_batch.dart' show | 48 import 'run_batch.dart' show |
| 34 RunBatch; | 49 RunBatch; |
| 35 | 50 |
| 36 Future openWrite(Uri uri, f(IOSink sink)) async { | 51 Future openWrite(Uri uri, f(IOSink sink)) async { |
| 37 IOSink sink = new File.fromUri(uri).openWrite(); | 52 IOSink sink = new File.fromUri(uri).openWrite(); |
| 38 try { | 53 try { |
| 39 await f(sink); | 54 await f(sink); |
| 40 } finally { | 55 } finally { |
| 41 await sink.close(); | 56 await sink.close(); |
| 42 } | 57 } |
| 43 print("Wrote $uri"); | 58 print("Wrote $uri"); |
| 44 } | 59 } |
| 45 | 60 |
| 46 abstract class Rastask extends CompilerTask { | 61 abstract class Rastask extends CompilerTask { |
| 47 final Stopwatch wallClock; | 62 final Stopwatch wallClock; |
| 48 final Options options; | 63 final Options globalOptions; |
| 64 Duration setupDuration; |
| 65 Kernel kernel; |
| 66 ir.Library coreLibrary; |
| 49 | 67 |
| 50 Rastask(CustomCompiler compiler, this.wallClock, this.options) | 68 Rastask(CustomCompiler compiler, this.wallClock, this.globalOptions) |
| 51 : super(compiler); | 69 : super(compiler); |
| 52 | 70 |
| 53 String get name => "rastak"; | 71 String get name => "rastak"; |
| 54 | 72 |
| 55 CustomCompiler get compiler => super.compiler; | 73 CustomCompiler get compiler => super.compiler; |
| 56 | 74 |
| 57 Future run(); | 75 Future run(); |
| 58 | 76 |
| 59 static Future<Rastask> setup( | 77 Future<Null> setup([Uri uri]) async { |
| 78 if (setupDuration != null) { |
| 79 throw new StateError("[setup] can only be called once."); |
| 80 } |
| 81 setupDuration = wallClock.elapsed; |
| 82 await compiler.setupSdk(); |
| 83 await compiler.setupPackages(uri); |
| 84 kernel = new Kernel(compiler); |
| 85 |
| 86 coreLibrary = await loadLibrary(Uri.parse("dart:core")); |
| 87 print( |
| 88 "Loading platform libraries took: " |
| 89 "${(wallClock.elapsedMilliseconds).toStringAsFixed(3)}ms"); |
| 90 } |
| 91 |
| 92 Future<ir.Library> loadLibrary(Uri uri) async { |
| 93 ir.Library library = await kernel.loadLibrary(uri); |
| 94 kernel.processWorkQueue(); |
| 95 return library; |
| 96 } |
| 97 |
| 98 Future<ir.TreeNode> runOne(Options options) async { |
| 99 ir.Library library = await loadLibrary(options.input); |
| 100 |
| 101 bool generateLibrary = options.generateLibrary; |
| 102 if (generateLibrary == null) { |
| 103 generateLibrary = !kernel.hasMainMethod(options.input); |
| 104 } |
| 105 if (!generateLibrary && !kernel.hasMainMethod(options.input)) { |
| 106 throw "No main method in ${options.input}."; |
| 107 } |
| 108 |
| 109 ir.Program program; |
| 110 if (!generateLibrary) { |
| 111 Iterable<ir.Procedure> mainMethods = library.procedures.where( |
| 112 (ir.Procedure function) => function.name.name == "main"); |
| 113 program = new ir.Program(kernel.libraryDependencies(options.input)) |
| 114 ..mainMethod = mainMethods.single; |
| 115 } |
| 116 compiler.printVerboseTimings(setupDuration); |
| 117 if (options.output != null) { |
| 118 await openWrite(options.output, (IOSink sink) { |
| 119 BinaryPrinter printer = new BinaryPrinter(sink); |
| 120 if (generateLibrary) { |
| 121 printer.writeLibraryFile(library); |
| 122 } else { |
| 123 printer.writeProgramFile(program); |
| 124 } |
| 125 }); |
| 126 } else { |
| 127 StringBuffer buffer = new StringBuffer(); |
| 128 Printer printer = new Printer(buffer); |
| 129 if (generateLibrary) { |
| 130 printer.writeLibraryFile(library); |
| 131 } else { |
| 132 printer.writeProgramFile(program); |
| 133 } |
| 134 print("$buffer"); |
| 135 } |
| 136 |
| 137 if (options.dependenciesFile != null) { |
| 138 await openWrite(options.dependenciesFile, (IOSink sink) { |
| 139 void writeCompilationUnit(CompilationUnitElement unit) { |
| 140 sink.write("${unit.script.resourceUri}\n"); |
| 141 } |
| 142 kernel.forEachLibraryElement((LibraryElement library) { |
| 143 library.compilationUnits.forEach(writeCompilationUnit); |
| 144 }); |
| 145 }); |
| 146 } |
| 147 return generateLibrary ? library : program; |
| 148 } |
| 149 |
| 150 static Future<Rastask> create( |
| 60 Stopwatch wallClock, | 151 Stopwatch wallClock, |
| 61 List<String> arguments) async { | 152 List<String> arguments) async { |
| 62 Options options = new OptionParser(arguments, Uri.base).parse(); | 153 Options options = new OptionParser(arguments, Uri.base).parse(); |
| 63 List<String> compilerOptions = | 154 List<String> compilerOptions = |
| 64 options.isVerbose ? <String>['--verbose'] : <String>[]; | 155 options.isVerbose ? <String>['--verbose'] : <String>[]; |
| 65 Uri script = await Isolate.resolvePackageUri( | 156 Uri script = await Isolate.resolvePackageUri( |
| 66 Uri.parse("package:rasta/src/rastak.dart")); | 157 Uri.parse("package:rasta/src/rastak.dart")); |
| 67 Uri targetSpecification = Uri.base.resolveUri(script) | 158 Uri targetSpecification = Uri.base.resolveUri(script) |
| 68 .resolve("../../dart_vm_standalone.json"); | 159 .resolve("../../dart_vm_standalone.json"); |
| 69 IoCompilerFactory factory = new IoCompilerFactory( | 160 IoCompilerFactory factory = new IoCompilerFactory( |
| 70 targetSpecification, compilerOptions, <String, dynamic>{}); | 161 targetSpecification, compilerOptions, <String, dynamic>{}); |
| 71 (await factory.diagnostics) | 162 (await factory.diagnostics) |
| 72 ..verbose = options.isVerbose | 163 ..verbose = options.isVerbose |
| 73 ..showWarnings = true | 164 ..showWarnings = true |
| 74 ..throwOnError = false | 165 ..throwOnError = false |
| 75 ..throwOnErrorCount = 1; | 166 ..throwOnErrorCount = 1; |
| 76 | 167 |
| 77 CustomCompiler compiler = await factory.compiler; | 168 CustomCompiler compiler = await factory.compiler; |
| 78 Rastask task; | 169 Rastask task; |
| 79 if (options.isBatch) { | 170 if (options.isBatch) { |
| 80 task = new RunBatch(compiler, wallClock, options); | 171 task = new RunBatch(compiler, wallClock, options); |
| 81 } else { | 172 } else { |
| 82 task = new RunSingle(compiler, wallClock, options); | 173 task = new RunSingle(compiler, wallClock, options); |
| 83 } | 174 } |
| 84 compiler.tasks.add(task); | 175 compiler.tasks.add(task); |
| 85 return task; | 176 return task; |
| 86 } | 177 } |
| 87 } | 178 } |
| OLD | NEW |