Index: pkg/front_end/lib/src/fasta/fasta.dart |
diff --git a/pkg/front_end/lib/src/fasta/fasta.dart b/pkg/front_end/lib/src/fasta/fasta.dart |
index 0dfe70dcd31938925872ab4336a9cba52f8f8b56..865192b2aa6fed78a8eaa327703b6683a72d2e65 100644 |
--- a/pkg/front_end/lib/src/fasta/fasta.dart |
+++ b/pkg/front_end/lib/src/fasta/fasta.dart |
@@ -8,18 +8,23 @@ import 'dart:async' show Future; |
import 'dart:convert' show JSON; |
-import 'dart:io' show BytesBuilder, File, exitCode; |
+import 'dart:io' show BytesBuilder, Directory, File, exitCode; |
+import 'package:front_end/file_system.dart'; |
import 'package:front_end/physical_file_system.dart'; |
import 'package:front_end/src/fasta/kernel/utils.dart'; |
+import 'package:kernel/binary/ast_to_binary.dart' |
+ show LibraryFilteringBinaryPrinter; |
-import 'package:kernel/kernel.dart' show Program, loadProgramFromBytes; |
+import 'package:kernel/kernel.dart' show Library, Program, loadProgramFromBytes; |
+ |
+import 'package:kernel/target/targets.dart' show Target; |
import 'compiler_command_line.dart' show CompilerCommandLine; |
import 'compiler_context.dart' show CompilerContext; |
-import 'errors.dart' show InputError, inputError; |
+import 'errors.dart' show InputError, formatUnexpected, inputError, reportCrash; |
import 'kernel/kernel_target.dart' show KernelTarget; |
@@ -31,6 +36,8 @@ import 'ticker.dart' show Ticker; |
import 'translate_uri.dart' show TranslateUri; |
+import 'vm.dart' show CompilationResult; |
+ |
const bool summary = const bool.fromEnvironment("summary", defaultValue: false); |
const int iterations = const int.fromEnvironment("iterations", defaultValue: 1); |
@@ -164,6 +171,69 @@ class CompileTask { |
} |
} |
+Future<CompilationResult> parseScript( |
+ Uri fileName, Uri packages, Uri patchedSdk, Target backendTarget, |
+ {bool verbose: false}) async { |
+ return parseScriptInFileSystem(fileName, PhysicalFileSystem.instance, |
+ packages, patchedSdk, backendTarget, |
+ verbose: verbose); |
+} |
+ |
+Future<CompilationResult> parseScriptInFileSystem(Uri fileName, |
+ FileSystem fileSystem, Uri packages, Uri patchedSdk, Target backendTarget, |
+ {bool verbose: false}) async { |
+ try { |
+ if (!await fileSystem.entityForUri(fileName).exists()) { |
+ return new CompilationResult.error( |
+ formatUnexpected(fileName, -1, "No such file.")); |
+ } |
+ if (!await new Directory.fromUri(patchedSdk).exists()) { |
+ return new CompilationResult.error( |
+ formatUnexpected(patchedSdk, -1, "Patched sdk directory not found.")); |
+ } |
+ |
+ Program program; |
+ try { |
+ TranslateUri uriTranslator = |
+ await TranslateUri.parse(fileSystem, patchedSdk, packages: packages); |
+ final Ticker ticker = new Ticker(isVerbose: verbose); |
+ final DillTarget dillTarget = |
+ new DillTarget(ticker, uriTranslator, backendTarget); |
+ _appendDillForUri(dillTarget, patchedSdk.resolve('platform.dill')); |
+ final KernelTarget kernelTarget = |
+ new KernelTarget(fileSystem, dillTarget, uriTranslator); |
+ kernelTarget.read(fileName); |
+ await dillTarget.buildOutlines(); |
+ await kernelTarget.buildOutlines(); |
+ program = await kernelTarget.buildProgram(); |
+ if (kernelTarget.errors.isNotEmpty) { |
+ return new CompilationResult.errors(kernelTarget.errors); |
+ } |
+ } on InputError catch (e) { |
+ return new CompilationResult.error(e.format()); |
+ } |
+ |
+ if (program.mainMethod == null) { |
+ return new CompilationResult.error("No 'main' method found."); |
+ } |
+ |
+ // Write the program to a list of bytes and return it. Do not include |
+ // libraries that have a dart: import URI. |
+ // |
+ // TODO(kmillikin): This is intended to exclude platform libraries that are |
+ // included in the Kernel binary platform platform.dill. It does not |
+ // necessarily exclude exactly the platform libraries. Use a better |
+ // predicate that knows what is included in platform.dill. |
+ var sink = new ByteSink(); |
+ bool predicate(Library library) => !library.importUri.isScheme('dart'); |
+ new LibraryFilteringBinaryPrinter(sink, predicate) |
+ .writeProgramFile(program); |
+ return new CompilationResult.ok(sink.builder.takeBytes()); |
+ } catch (e, s) { |
+ return reportCrash(e, s, fileName); |
+ } |
+} |
+ |
Future compilePlatform(Uri patchedSdk, Uri fullOutput, |
{Uri outlineOutput, |
Uri packages, |
@@ -183,16 +253,16 @@ Future compilePlatform(Uri patchedSdk, Uri fullOutput, |
}); |
} |
-// TODO(sigmund): reimplement this API using the directive listener intead. |
-Future<List<Uri>> getDependencies(Uri script, |
+Future writeDepsFile(Uri script, Uri depsFile, Uri output, |
{Uri sdk, |
Uri packages, |
Uri platform, |
+ Iterable<Uri> extraDependencies, |
bool verbose: false, |
String backendTarget}) async { |
backendTarget ??= "vm_fasta"; |
Ticker ticker = new Ticker(isVerbose: verbose); |
- return await CompilerCommandLine.withGlobalOptions("", [""], |
+ await CompilerCommandLine.withGlobalOptions("", [""], |
(CompilerContext c) async { |
c.options.options["--target"] = backendTarget; |
c.options.options["--strong-mode"] = false; |
@@ -208,14 +278,15 @@ Future<List<Uri>> getDependencies(Uri script, |
ticker.logMs("Read packages file"); |
DillTarget dillTarget = |
new DillTarget(ticker, uriTranslator, c.options.target); |
- if (platform != null) _appendDillForUri(dillTarget, platform); |
+ _appendDillForUri(dillTarget, platform); |
KernelTarget kernelTarget = new KernelTarget( |
PhysicalFileSystem.instance, dillTarget, uriTranslator, c.uriToSource); |
kernelTarget.read(script); |
await dillTarget.buildOutlines(); |
await kernelTarget.loader.buildOutlines(); |
- return await kernelTarget.loader.getDependencies(); |
+ await kernelTarget.writeDepsFile(output, depsFile, |
+ extraDependencies: extraDependencies); |
}); |
} |