| OLD | NEW |
| 1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
| 2 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
| 4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 /// Command line tool to merge the SDK libraries and our patch files. | 6 /// Command line tool to merge the SDK libraries and our patch files. |
| 7 /// This is currently designed as an offline tool, but we could automate it. | 7 /// This is currently designed as an offline tool, but we could automate it. |
| 8 | 8 |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 import 'dart:isolate' show RawReceivePort; | 10 import 'dart:isolate' show RawReceivePort; |
| 11 import 'dart:async'; | 11 import 'dart:async'; |
| 12 import 'dart:math' as math; | 12 import 'dart:math' as math; |
| 13 import 'dart:convert' show JSON; |
| 13 | 14 |
| 14 import 'package:analyzer/analyzer.dart'; | 15 import 'package:analyzer/analyzer.dart'; |
| 15 import 'package:analyzer/src/generated/sdk.dart'; | 16 import 'package:analyzer/src/generated/sdk.dart'; |
| 16 import 'package:path/path.dart' as path; | 17 import 'package:path/path.dart' as path; |
| 17 | 18 |
| 18 import 'package:front_end/src/fasta/fasta.dart' as fasta | 19 import 'package:front_end/src/fasta/fasta.dart' as fasta |
| 19 show compilePlatform, writeDepsFile; | 20 show compilePlatform, writeDepsFile; |
| 20 | 21 |
| 21 import 'package:compiler/src/kernel/fasta_support.dart' as dart2js | 22 import 'package:compiler/src/kernel/fasta_support.dart' as dart2js |
| 22 show compilePlatform; | 23 show compilePlatform; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 var outDirUri = Uri.base.resolveUri(new Uri.directory(outDir)); | 89 var outDirUri = Uri.base.resolveUri(new Uri.directory(outDir)); |
| 89 var sdkOut = path.join(outDir, 'lib'); | 90 var sdkOut = path.join(outDir, 'lib'); |
| 90 var packagesFile = argv[4]; | 91 var packagesFile = argv[4]; |
| 91 | 92 |
| 92 // Parse libraries.dart | 93 // Parse libraries.dart |
| 93 var libContents = readInputFile(path.join( | 94 var libContents = readInputFile(path.join( |
| 94 sdkLibIn, '_internal', 'sdk_library_metadata', 'lib', 'libraries.dart')); | 95 sdkLibIn, '_internal', 'sdk_library_metadata', 'lib', 'libraries.dart')); |
| 95 if (forVm) libContents = _updateLibraryMetadata(sdkOut, libContents); | 96 if (forVm) libContents = _updateLibraryMetadata(sdkOut, libContents); |
| 96 var sdkLibraries = _getSdkLibraries(libContents); | 97 var sdkLibraries = _getSdkLibraries(libContents); |
| 97 | 98 |
| 99 Map<String, String> locations = <String, String>{}; |
| 100 |
| 98 // Enumerate core libraries and apply patches | 101 // Enumerate core libraries and apply patches |
| 99 for (SdkLibrary library in sdkLibraries) { | 102 for (SdkLibrary library in sdkLibraries) { |
| 100 if (forDart2js && library.isVmLibrary) continue; | 103 if (forDart2js && library.isVmLibrary) continue; |
| 101 if (forVm && library.isDart2JsLibrary) continue; | 104 if (forVm && library.isDart2JsLibrary) continue; |
| 102 _applyPatch(library, sdkLibIn, patchIn, sdkOut); | 105 _applyPatch(library, sdkLibIn, patchIn, sdkOut, locations); |
| 103 } | 106 } |
| 104 | 107 |
| 105 if (forVm) _copyExtraVmLibraries(sdkOut); | 108 if (forVm) _copyExtraVmLibraries(sdkOut, locations); |
| 106 | 109 |
| 107 Uri platform = outDirUri.resolve('platform.dill.tmp'); | 110 Uri platform = outDirUri.resolve('platform.dill.tmp'); |
| 108 Uri outline = outDirUri.resolve('outline.dill'); | 111 Uri outline = outDirUri.resolve('outline.dill'); |
| 112 Uri librariesJson = outDirUri.resolve("lib/libraries.json"); |
| 109 Uri packages = Uri.base.resolveUri(new Uri.file(packagesFile)); | 113 Uri packages = Uri.base.resolveUri(new Uri.file(packagesFile)); |
| 114 |
| 115 await _writeSync( |
| 116 librariesJson.toFilePath(), JSON.encode({"libraries": locations})); |
| 117 |
| 110 if (forVm) { | 118 if (forVm) { |
| 111 await fasta.compilePlatform(outDirUri, platform, | 119 await fasta.compilePlatform(outDirUri, platform, |
| 112 packages: packages, outlineOutput: outline); | 120 packages: packages, outlineOutput: outline); |
| 113 } else { | 121 } else { |
| 114 await dart2js.compilePlatform(outDirUri, platform, | 122 await dart2js.compilePlatform(outDirUri, platform, |
| 115 packages: packages, outlineOutput: outline); | 123 packages: packages, outlineOutput: outline); |
| 116 } | 124 } |
| 117 | 125 |
| 118 Uri platformFinalLocation = outDirUri.resolve('platform.dill'); | 126 Uri platformFinalLocation = outDirUri.resolve('platform.dill'); |
| 119 | 127 |
| 120 // To properly regenerate the patched_sdk, patched_dart2js_sdk, and | 128 // To properly regenerate the patched_sdk, patched_dart2js_sdk, and |
| 121 // platform.dill only when necessary, we track dependencies as follows: | 129 // platform.dill only when necessary, we track dependencies as follows: |
| 122 // - inputs like the sdk libraries and patch files are covered by the | 130 // - inputs like the sdk libraries and patch files are covered by the |
| 123 // extraDependencies argument. | 131 // extraDependencies argument. |
| 124 // - this script and its script dependencies are handled by writeDepsFile | 132 // - this script and its script dependencies are handled by writeDepsFile |
| 125 // here. | 133 // here. |
| 126 // - the internal platform libraries that may affect how this script | 134 // - the internal platform libraries that may affect how this script |
| 127 // runs in the VM are discovered by providing the `platform` argument | 135 // runs in the VM are discovered by providing the `platform` argument |
| 128 // below. Regardless of patched_sdk or patched_dart2js_sdk we provide below | 136 // below. Regardless of patched_sdk or patched_dart2js_sdk we provide below |
| 129 // the .dill file of patched_sdk (since the script runs in the VM and not | 137 // the .dill file of patched_sdk (since the script runs in the VM and not |
| 130 // in dart2js). At the BUILD.gn level we have a dependency from | 138 // in dart2js). At the BUILD.gn level we have a dependency from |
| 131 // patched_dart2js_sdk to patched_sdk to ensure that file already exists. | 139 // patched_dart2js_sdk to patched_sdk to ensure that file already exists. |
| 132 await fasta.writeDepsFile(Platform.script, | 140 await fasta.writeDepsFile(Platform.script, |
| 133 Uri.base.resolveUri(new Uri.file("$outDir.d")), platformFinalLocation, | 141 Uri.base.resolveUri(new Uri.file("$outDir.d")), platformFinalLocation, |
| 142 sdk: outDirUri, |
| 134 packages: packages, | 143 packages: packages, |
| 135 platform: | 144 platform: |
| 136 forVm ? platform : outDirUri.resolve('../patched_sdk/platform.dill'), | 145 forVm ? platform : outDirUri.resolve('../patched_sdk/platform.dill'), |
| 137 extraDependencies: deps, | 146 extraDependencies: deps); |
| 138 verbose: false); | |
| 139 | 147 |
| 140 await new File.fromUri(platform).rename(platformFinalLocation.toFilePath()); | 148 await new File.fromUri(platform).rename(platformFinalLocation.toFilePath()); |
| 141 } | 149 } |
| 142 | 150 |
| 143 /// Updates the contents of | 151 /// Updates the contents of |
| 144 /// sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart to include | 152 /// sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart to include |
| 145 /// declarations for vm internal libraries. | 153 /// declarations for vm internal libraries. |
| 146 String _updateLibraryMetadata(String sdkOut, String libContents) { | 154 String _updateLibraryMetadata(String sdkOut, String libContents) { |
| 147 // Copy and patch libraries.dart and version | 155 // Copy and patch libraries.dart and version |
| 148 libContents = libContents.replaceAll( | 156 libContents = libContents.replaceAll( |
| (...skipping 27 matching lines...) Expand all Loading... |
| 176 '''); | 184 '''); |
| 177 _writeSync( | 185 _writeSync( |
| 178 path.join( | 186 path.join( |
| 179 sdkOut, '_internal', 'sdk_library_metadata', 'lib', 'libraries.dart'), | 187 sdkOut, '_internal', 'sdk_library_metadata', 'lib', 'libraries.dart'), |
| 180 libContents); | 188 libContents); |
| 181 return libContents; | 189 return libContents; |
| 182 } | 190 } |
| 183 | 191 |
| 184 /// Copy internal libraries that are developed under 'runtime/bin/' to the | 192 /// Copy internal libraries that are developed under 'runtime/bin/' to the |
| 185 /// patched_sdk folder. | 193 /// patched_sdk folder. |
| 186 _copyExtraVmLibraries(String sdkOut) { | 194 _copyExtraVmLibraries(String sdkOut, Map<String, String> locations) { |
| 187 var base = path.fromUri(Platform.script); | 195 var base = path.fromUri(Platform.script); |
| 188 var dartDir = path.dirname(path.dirname(path.absolute(base))); | 196 var dartDir = path.dirname(path.dirname(path.absolute(base))); |
| 189 | 197 |
| 190 for (var tuple in [ | 198 for (var tuple in [ |
| 191 ['_builtin', 'builtin.dart'] | 199 ['_builtin', 'builtin.dart'] |
| 192 ]) { | 200 ]) { |
| 193 var vmLibrary = tuple[0]; | 201 var vmLibrary = tuple[0]; |
| 194 var dartFile = tuple[1]; | 202 var dartFile = tuple[1]; |
| 195 | 203 |
| 196 // The "dart:_builtin" library is only available for the DartVM. | 204 // The "dart:_builtin" library is only available for the DartVM. |
| 197 var builtinLibraryIn = path.join(dartDir, 'runtime', 'bin', dartFile); | 205 var builtinLibraryIn = path.join(dartDir, 'runtime', 'bin', dartFile); |
| 198 var builtinLibraryOut = path.join(sdkOut, vmLibrary, '${vmLibrary}.dart'); | 206 var builtinLibraryOut = path.join(sdkOut, vmLibrary, '${vmLibrary}.dart'); |
| 199 _writeSync(builtinLibraryOut, readInputFile(builtinLibraryIn)); | 207 _writeSync(builtinLibraryOut, readInputFile(builtinLibraryIn)); |
| 208 locations[vmLibrary] = path.join(vmLibrary, '${vmLibrary}.dart'); |
| 200 } | 209 } |
| 201 | 210 |
| 202 for (var file in ['loader.dart', 'server.dart', 'vmservice_io.dart']) { | 211 for (var file in ['loader.dart', 'server.dart', 'vmservice_io.dart']) { |
| 203 var libraryIn = path.join(dartDir, 'runtime', 'bin', 'vmservice', file); | 212 var libraryIn = path.join(dartDir, 'runtime', 'bin', 'vmservice', file); |
| 204 var libraryOut = path.join(sdkOut, 'vmservice_io', file); | 213 var libraryOut = path.join(sdkOut, 'vmservice_io', file); |
| 205 _writeSync(libraryOut, readInputFile(libraryIn)); | 214 _writeSync(libraryOut, readInputFile(libraryIn)); |
| 206 } | 215 } |
| 216 locations["vmservice_io"] = "vmservice_io/vmservice_io.dart"; |
| 207 } | 217 } |
| 208 | 218 |
| 209 _applyPatch( | 219 _applyPatch(SdkLibrary library, String sdkLibIn, String patchIn, String sdkOut, |
| 210 SdkLibrary library, String sdkLibIn, String patchIn, String sdkOut) { | 220 Map<String, String> locations) { |
| 211 var libraryOut = path.join(sdkLibIn, library.path); | 221 var libraryOut = path.join(sdkLibIn, library.path); |
| 212 var libraryIn = libraryOut; | 222 var libraryIn = libraryOut; |
| 213 | 223 |
| 214 var libraryFile = getInputFile(libraryIn, canBeMissing: true); | 224 var libraryFile = getInputFile(libraryIn, canBeMissing: true); |
| 215 if (libraryFile != null) { | 225 if (libraryFile != null) { |
| 226 locations[Uri.parse(library.shortName).path] = |
| 227 path.relative(libraryOut, from: sdkLibIn); |
| 216 var outPaths = <String>[libraryOut]; | 228 var outPaths = <String>[libraryOut]; |
| 217 var libraryContents = libraryFile.readAsStringSync(); | 229 var libraryContents = libraryFile.readAsStringSync(); |
| 218 | 230 |
| 219 int inputModifyTime = libraryFile.lastModifiedSync().millisecondsSinceEpoch; | 231 int inputModifyTime = libraryFile.lastModifiedSync().millisecondsSinceEpoch; |
| 220 var partFiles = <File>[]; | 232 var partFiles = <File>[]; |
| 221 for (var part in parseDirectives(libraryContents).directives) { | 233 for (var part in parseDirectives(libraryContents).directives) { |
| 222 if (part is PartDirective) { | 234 if (part is PartDirective) { |
| 223 var partPath = part.uri.stringValue; | 235 var partPath = part.uri.stringValue; |
| 224 outPaths.add(path.join(path.dirname(libraryOut), partPath)); | 236 outPaths.add(path.join(path.dirname(libraryOut), partPath)); |
| 225 | 237 |
| (...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 if (diff != 0) return diff; | 622 if (diff != 0) return diff; |
| 611 return end - other.end; | 623 return end - other.end; |
| 612 } | 624 } |
| 613 } | 625 } |
| 614 | 626 |
| 615 List<SdkLibrary> _getSdkLibraries(String contents) { | 627 List<SdkLibrary> _getSdkLibraries(String contents) { |
| 616 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(true); | 628 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(true); |
| 617 parseCompilationUnit(contents).accept(libraryBuilder); | 629 parseCompilationUnit(contents).accept(libraryBuilder); |
| 618 return libraryBuilder.librariesMap.sdkLibraries; | 630 return libraryBuilder.librariesMap.sdkLibraries; |
| 619 } | 631 } |
| OLD | NEW |