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