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