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.compile_platform; | |
6 | |
7 import 'dart:async' show Future; | |
8 | |
9 import 'dart:io' show File, IOSink; | |
10 | |
11 import 'package:analyzer/src/generated/source.dart' show Source; | |
12 | |
13 import 'package:analyzer/dart/element/element.dart' | |
14 show ExportElement, LibraryElement; | |
15 | |
16 import 'package:kernel/ast.dart' | |
17 show Field, Library, Name, Program, StringLiteral; | |
18 | |
19 import 'package:kernel/binary/ast_to_binary.dart' show BinaryPrinter; | |
20 | |
21 import 'package:kernel/analyzer/loader.dart' | |
22 show DartLoader, DartOptions, createDartSdk; | |
23 | |
24 import 'package:kernel/target/targets.dart' show Target, TargetFlags, getTarget; | |
25 | |
26 import 'package:kernel/ast.dart' show Program; | |
27 | |
28 import 'environment_variable.dart' | |
29 show EnvironmentVariableDirectory, fileExists; | |
30 | |
31 import 'errors.dart' show inputError; | |
32 | |
33 const EnvironmentVariableSdk dartAotSdk = const EnvironmentVariableSdk( | |
34 "DART_AOT_SDK", | |
35 "The environment variable 'DART_AOT_SDK' should point to a patched SDK."); | |
36 | |
37 class EnvironmentVariableSdk extends EnvironmentVariableDirectory { | |
38 const EnvironmentVariableSdk(String name, String what) : super(name, what); | |
39 | |
40 Future<Null> validate(String value) async { | |
41 Uri sdk = Uri.base.resolveUri(new Uri.directory(value)); | |
42 const String asyncDart = "lib/async/async.dart"; | |
43 if (!await fileExists(sdk, asyncDart)) { | |
44 inputError( | |
45 null, | |
46 null, | |
47 "The environment variable '$name' has the value '$value', " | |
48 "that's a directory that doesn't contain '$asyncDart'. $what"); | |
49 } | |
50 const String asyncSources = "lib/async/async_sources.gypi"; | |
51 if (await fileExists(sdk, asyncSources)) { | |
52 inputError( | |
53 null, | |
54 null, | |
55 "The environment variable '$name' has the value '$value', " | |
56 "that's a directory that contains '$asyncSources', so it isn't a " | |
57 "patched SDK. $what"); | |
58 } | |
59 return null; | |
60 } | |
61 } | |
62 | |
63 main(List<String> arguments) async { | |
64 Uri output = Uri.base.resolveUri(new Uri.file(arguments.single)); | |
65 DartOptions options = new DartOptions( | |
66 strongMode: false, sdk: await dartAotSdk.value, packagePath: null); | |
67 Program program = new Program(); | |
68 DartLoader loader = new DartLoader(program, options, null, | |
69 ignoreRedirectingFactories: false, | |
70 dartSdk: createDartSdk(options.sdk, strongMode: options.strongMode)); | |
71 Target target = | |
72 getTarget("vm", new TargetFlags(strongMode: options.strongMode)); | |
73 loader.loadProgram(Uri.base.resolve("pkg/fasta/test/platform.dart"), | |
74 target: target); | |
75 if (loader.errors.isNotEmpty) { | |
76 inputError(null, null, loader.errors.join("\n")); | |
77 } | |
78 Library mainLibrary = program.mainMethod.enclosingLibrary; | |
79 program.uriToSource.remove(mainLibrary.fileUri); | |
80 program = new Program( | |
81 program.libraries | |
82 .where((Library l) => l.importUri.scheme == "dart") | |
83 .toList(), | |
84 program.uriToSource); | |
85 target.performModularTransformations(program); | |
86 target.performGlobalTransformations(program); | |
87 for (LibraryElement analyzerLibrary in loader.libraryElements) { | |
88 Library library = loader.getLibraryReference(analyzerLibrary); | |
89 StringBuffer sb = new StringBuffer(); | |
90 if (analyzerLibrary.exports.isNotEmpty) { | |
91 Source source; | |
92 int offset; | |
93 for (ExportElement export in analyzerLibrary.exports) { | |
94 source ??= export.source; | |
95 offset ??= export.nameOffset; | |
96 Uri uri = export.exportedLibrary.source.uri; | |
97 sb.write("export '"); | |
98 sb.write(uri); | |
99 sb.write("'"); | |
100 if (export.combinators.isNotEmpty) { | |
101 sb.write(" "); | |
102 sb.writeAll(export.combinators, " "); | |
103 } | |
104 sb.write(";"); | |
105 } | |
106 Name exports = new Name("_exports#", library); | |
107 StringLiteral literal = new StringLiteral("$sb")..fileOffset = offset; | |
108 library.addMember(new Field(exports, | |
109 isStatic: true, | |
110 isConst: true, | |
111 initializer: literal, | |
112 fileUri: "${new Uri.file(source.fullName)}")..fileOffset = offset); | |
113 } | |
114 } | |
115 | |
116 IOSink sink = new File.fromUri(output).openWrite(); | |
117 new BinaryPrinter(sink).writeProgramFile(program); | |
118 await sink.close(); | |
119 } | |
OLD | NEW |