| 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:math' as math; | 10 import 'dart:math' as math; |
| 11 | 11 |
| 12 import 'package:analyzer/analyzer.dart'; | 12 import 'package:analyzer/analyzer.dart'; |
| 13 import 'package:analyzer/src/generated/sdk.dart'; | 13 import 'package:analyzer/src/generated/sdk.dart'; |
| 14 import 'package:path/path.dart' as path; | 14 import 'package:path/path.dart' as path; |
| 15 | 15 |
| 16 void main(List<String> argv) { | 16 void main(List<String> argv) { |
| 17 if (argv.length < 2) { | 17 var base = path.fromUri(Platform.script); |
| 18 var self = path.relative(path.fromUri(Platform.script)); | 18 var dartDir = path.dirname(path.dirname(path.absolute(base))); |
| 19 var toolDir = path.relative(path.dirname(path.fromUri(Platform.script))); | |
| 20 | 19 |
| 21 var inputExample = path.join(toolDir, 'input_sdk'); | 20 if (argv.length != 4 || |
| 21 !argv.isEmpty && argv.first != 'vm' && argv.first != 'ddc') { |
| 22 var self = path.relative(base); |
| 23 print('Usage: $self MODE SDK_DIR PATCH_DIR OUTPUT_DIR'); |
| 24 print('MODE must be one of ddc or vm.'); |
| 25 |
| 26 var toolDir = path.relative(path.dirname(base)); |
| 27 var sdkExample = path.join(toolDir, 'input_sdk'); |
| 28 var patchExample = path.join(sdkExample, 'patch'); |
| 22 var outExample = | 29 var outExample = |
| 23 path.relative(path.normalize(path.join('gen', 'patched_sdk'))); | 30 path.relative(path.normalize(path.join('gen', 'patched_sdk'))); |
| 31 print('For example:'); |
| 32 print('\$ $self ddc $sdkExample $patchExample $outExample'); |
| 24 | 33 |
| 25 print('Usage: $self INPUT_DIR OUTPUT_DIR'); | 34 var repositoryDir = path.relative(path.dirname(path.dirname(base))); |
| 26 print('For example:'); | 35 sdkExample = path.relative(path.join(repositoryDir, 'sdk')); |
| 27 print('\$ $self $inputExample $outExample'); | 36 patchExample = path.relative(path.join(repositoryDir, 'out', 'DebugX64', |
| 37 'obj', 'gen', 'patch')); |
| 38 outExample = path.relative(path.join(repositoryDir, 'out', 'DebugX64', |
| 39 'obj', 'gen', 'patched_sdk')); |
| 40 print('or:'); |
| 41 print('\$ $self vm $sdkExample $patchExample $outExample'); |
| 42 |
| 28 exit(1); | 43 exit(1); |
| 29 } | 44 } |
| 30 | 45 |
| 31 var input = argv[0]; | 46 var mode = argv[0]; |
| 47 var input = argv[1]; |
| 32 var sdkLibIn = path.join(input, 'lib'); | 48 var sdkLibIn = path.join(input, 'lib'); |
| 33 var patchIn = path.join(input, 'patch'); | 49 var patchIn = argv[2]; |
| 50 var sdkOut = path.join(argv[3], 'lib'); |
| 51 |
| 34 var privateIn = path.join(input, 'private'); | 52 var privateIn = path.join(input, 'private'); |
| 35 var sdkOut = path.join(argv[1], 'lib'); | |
| 36 | |
| 37 var INTERNAL_PATH = '_internal/compiler/js_lib/'; | 53 var INTERNAL_PATH = '_internal/compiler/js_lib/'; |
| 38 | 54 |
| 39 // Copy libraries.dart and version | 55 // Copy libraries.dart and version |
| 40 var libContents = new File(path.join(sdkLibIn, '_internal', 'libraries.dart')) | 56 var libContents = new File(path.join(sdkLibIn, '_internal', |
| 41 .readAsStringSync(); | 57 'sdk_library_metadata', 'lib', 'libraries.dart')).readAsStringSync(); |
| 42 _writeSync(path.join(sdkOut, '_internal', 'libraries.dart'), libContents); | |
| 43 _writeSync( | 58 _writeSync( |
| 44 path.join( | 59 path.join( |
| 45 sdkOut, '_internal', 'sdk_library_metadata', 'lib', 'libraries.dart'), | 60 sdkOut, '_internal', 'sdk_library_metadata', 'lib', 'libraries.dart'), |
| 46 libContents); | 61 libContents); |
| 47 _writeSync(path.join(sdkOut, '..', 'version'), | 62 if (mode == 'ddc') { |
| 48 new File(path.join(sdkLibIn, '..', 'version')).readAsStringSync()); | 63 _writeSync(path.join(sdkOut, '..', 'version'), |
| 64 new File(path.join(sdkLibIn, '..', 'version')).readAsStringSync()); |
| 65 } |
| 49 | 66 |
| 50 // Parse libraries.dart | 67 // Parse libraries.dart |
| 51 var sdkLibraries = _getSdkLibraries(libContents); | 68 var sdkLibraries = _getSdkLibraries(libContents); |
| 52 | 69 |
| 53 // Enumerate core libraries and apply patches | 70 // Enumerate core libraries and apply patches |
| 54 for (SdkLibrary library in sdkLibraries) { | 71 for (SdkLibrary library in sdkLibraries) { |
| 55 // TODO(jmesserly): analyzer does not handle the default case of | 72 // TODO(jmesserly): analyzer does not handle the default case of |
| 56 // "both platforms" correctly, and treats it as being supported on neither. | 73 // "both platforms" correctly, and treats it as being supported on neither. |
| 57 // So instead we skip explicitly marked as VM libs. | 74 // So instead we skip explicitly marked as either VM or dart2js libs. |
| 58 if (library.isVmLibrary) continue; | 75 if (mode == 'ddc' ? libary.isVmLibrary : library.isDart2JsLibrary) { |
| 76 continue; |
| 77 } |
| 59 | 78 |
| 60 var libraryOut = path.join(sdkLibIn, library.path); | 79 var libraryOut = path.join(sdkLibIn, library.path); |
| 61 var libraryIn; | 80 var libraryIn; |
| 62 if (library.path.contains(INTERNAL_PATH)) { | 81 if (mode == 'vm' && library.path.contains('typed_data.dart')) { |
| 82 // dart:typed_data is unlike the other libraries in the SDK. The VM does |
| 83 // not apply a patch to the base SDK implementation of the library. |
| 84 // Instead, the VM provides a replacement implementation and ignores the |
| 85 // sources in the SDK. |
| 86 libraryIn = |
| 87 path.join(dartDir, 'runtime', 'lib', 'typed_data.dart'); |
| 88 } else if (mode == 'ddc' && library.path.contains(INTERNAL_PATH)) { |
| 63 libraryIn = | 89 libraryIn = |
| 64 path.join(privateIn, library.path.replaceAll(INTERNAL_PATH, '')); | 90 path.join(privateIn, library.path.replaceAll(INTERNAL_PATH, '')); |
| 65 } else { | 91 } else { |
| 66 libraryIn = libraryOut; | 92 libraryIn = libraryOut; |
| 67 } | 93 } |
| 68 | 94 |
| 69 var libraryFile = new File(libraryIn); | 95 var libraryFile = new File(libraryIn); |
| 70 if (libraryFile.existsSync()) { | 96 if (libraryFile.existsSync()) { |
| 71 var outPaths = <String>[libraryOut]; | 97 var outPaths = <String>[libraryOut]; |
| 72 var libraryContents = libraryFile.readAsStringSync(); | 98 var libraryContents = libraryFile.readAsStringSync(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 needsUpdate = true; | 138 needsUpdate = true; |
| 113 break; | 139 break; |
| 114 } | 140 } |
| 115 } | 141 } |
| 116 | 142 |
| 117 if (needsUpdate) { | 143 if (needsUpdate) { |
| 118 var contents = <String>[libraryContents]; | 144 var contents = <String>[libraryContents]; |
| 119 contents.addAll(partFiles.map((f) => f.readAsStringSync())); | 145 contents.addAll(partFiles.map((f) => f.readAsStringSync())); |
| 120 if (patchExists) { | 146 if (patchExists) { |
| 121 var patchContents = patchFile.readAsStringSync(); | 147 var patchContents = patchFile.readAsStringSync(); |
| 122 contents = _patchLibrary(contents, patchContents); | 148 contents = _patchLibrary( |
| 149 patchFile.toString(), contents, patchContents); |
| 123 } | 150 } |
| 124 | 151 |
| 125 for (var i = 0; i < outPaths.length; i++) { | 152 for (var i = 0; i < outPaths.length; i++) { |
| 153 if (path.basename(outPaths[i]) == 'internal.dart') { |
| 154 contents[i] += ''' |
| 155 |
| 156 /// Marks a function as an external implementation ("native" in the Dart VM). |
| 157 /// |
| 158 /// Provides a backend-specific String that can be used to identify the |
| 159 /// function's implementation |
| 160 class ExternalName { |
| 161 final String name; |
| 162 const ExternalName(this.name); |
| 163 } |
| 164 '''; |
| 165 } |
| 166 |
| 126 _writeSync(outPaths[i], contents[i]); | 167 _writeSync(outPaths[i], contents[i]); |
| 127 } | 168 } |
| 128 } | 169 } |
| 129 } | 170 } |
| 130 } | 171 } |
| 172 if (mode == 'vm') { |
| 173 for (var tuple in [['nativewrappers', 'nativewrappers.dart'], |
| 174 ['_builtin', 'builtin.dart']]) { |
| 175 var vmLibrary = tuple[0]; |
| 176 var dartFile = tuple[1]; |
| 177 |
| 178 // The "dart:_builtin" library is only available for the DartVM. |
| 179 var builtinLibraryIn = path.join(dartDir, 'runtime', 'bin', dartFile); |
| 180 var builtinLibraryOut = path.join(sdkOut, vmLibrary, '${vmLibrary}.dart'); |
| 181 _writeSync(builtinLibraryOut, new File(builtinLibraryIn).readAsStringSync(
)); |
| 182 } |
| 183 |
| 184 for (var file in ['loader.dart', 'server.dart', 'vmservice_io.dart']) { |
| 185 var libraryIn = path.join(dartDir, 'runtime', 'bin', 'vmservice', file); |
| 186 var libraryOut = path.join(sdkOut, 'vmservice_io', file); |
| 187 _writeSync(libraryOut, new File(libraryIn).readAsStringSync()); |
| 188 } |
| 189 } |
| 131 } | 190 } |
| 132 | 191 |
| 133 /// Writes a file, creating the directory if needed. | 192 /// Writes a file, creating the directory if needed. |
| 134 void _writeSync(String filePath, String contents) { | 193 void _writeSync(String filePath, String contents) { |
| 135 var outDir = new Directory(path.dirname(filePath)); | 194 var outDir = new Directory(path.dirname(filePath)); |
| 136 if (!outDir.existsSync()) outDir.createSync(recursive: true); | 195 if (!outDir.existsSync()) outDir.createSync(recursive: true); |
| 137 | 196 |
| 138 new File(filePath).writeAsStringSync(contents); | 197 new File(filePath).writeAsStringSync(contents); |
| 139 } | 198 } |
| 140 | 199 |
| 141 /// Merges dart:* library code with code from *_patch.dart file. | 200 /// Merges dart:* library code with code from *_patch.dart file. |
| 142 /// | 201 /// |
| 143 /// Takes a list of the library's parts contents, with the main library contents | 202 /// Takes a list of the library's parts contents, with the main library contents |
| 144 /// first in the list, and the contents of the patch file. | 203 /// first in the list, and the contents of the patch file. |
| 145 /// | 204 /// |
| 146 /// The result will have `@patch` implementations merged into the correct place | 205 /// The result will have `@patch` implementations merged into the correct place |
| 147 /// (e.g. the class or top-level function declaration) and all other | 206 /// (e.g. the class or top-level function declaration) and all other |
| 148 /// declarations introduced by the patch will be placed into the main library | 207 /// declarations introduced by the patch will be placed into the main library |
| 149 /// file. | 208 /// file. |
| 150 /// | 209 /// |
| 151 /// This is purely a syntactic transformation. Unlike dart2js patch files, there | 210 /// This is purely a syntactic transformation. Unlike dart2js patch files, there |
| 152 /// is no semantic meaning given to the *_patch files, and they do not magically | 211 /// is no semantic meaning given to the *_patch files, and they do not magically |
| 153 /// get their own library scope, etc. | 212 /// get their own library scope, etc. |
| 154 /// | 213 /// |
| 155 /// Editorializing: the dart2js approach requires a Dart front end such as | 214 /// Editorializing: the dart2js approach requires a Dart front end such as |
| 156 /// package:analyzer to semantically model a feature beyond what is specified | 215 /// package:analyzer to semantically model a feature beyond what is specified |
| 157 /// in the Dart language. Since this feature is only for the convenience of | 216 /// in the Dart language. Since this feature is only for the convenience of |
| 158 /// writing the dart:* libraries, and not a tool given to Dart developers, it | 217 /// writing the dart:* libraries, and not a tool given to Dart developers, it |
| 159 /// seems like a non-ideal situation. Instead we keep the preprocessing simple. | 218 /// seems like a non-ideal situation. Instead we keep the preprocessing simple. |
| 160 List<String> _patchLibrary(List<String> partsContents, String patchContents) { | 219 List<String> _patchLibrary(String name, |
| 220 List<String> partsContents, |
| 221 String patchContents) { |
| 161 var results = <StringEditBuffer>[]; | 222 var results = <StringEditBuffer>[]; |
| 162 | 223 |
| 163 // Parse the patch first. We'll need to extract bits of this as we go through | 224 // Parse the patch first. We'll need to extract bits of this as we go through |
| 164 // the other files. | 225 // the other files. |
| 165 var patchFinder = new PatchFinder.parseAndVisit(patchContents); | 226 final patchFinder = new PatchFinder.parseAndVisit(name, patchContents); |
| 166 | 227 |
| 167 // Merge `external` declarations with the corresponding `@patch` code. | 228 // Merge `external` declarations with the corresponding `@patch` code. |
| 168 for (var partContent in partsContents) { | 229 for (var partContent in partsContents) { |
| 169 var partEdits = new StringEditBuffer(partContent); | 230 var partEdits = new StringEditBuffer(partContent); |
| 170 var partUnit = parseCompilationUnit(partContent); | 231 var partUnit = parseCompilationUnit(partContent); |
| 171 partUnit.accept(new PatchApplier(partEdits, patchFinder)); | 232 partUnit.accept(new PatchApplier(partEdits, patchFinder)); |
| 172 results.add(partEdits); | 233 results.add(partEdits); |
| 173 } | 234 } |
| 174 return new List<String>.from(results.map((e) => e.toString())); | 235 return new List<String>.from(results.map((e) => e.toString())); |
| 175 } | 236 } |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 } | 332 } |
| 272 | 333 |
| 273 class PatchFinder extends GeneralizingAstVisitor { | 334 class PatchFinder extends GeneralizingAstVisitor { |
| 274 final String contents; | 335 final String contents; |
| 275 final CompilationUnit unit; | 336 final CompilationUnit unit; |
| 276 | 337 |
| 277 final Map patches = <String, Declaration>{}; | 338 final Map patches = <String, Declaration>{}; |
| 278 final Map mergeMembers = <String, List<ClassMember>>{}; | 339 final Map mergeMembers = <String, List<ClassMember>>{}; |
| 279 final List mergeDeclarations = <CompilationUnitMember>[]; | 340 final List mergeDeclarations = <CompilationUnitMember>[]; |
| 280 | 341 |
| 281 PatchFinder.parseAndVisit(String contents) | 342 PatchFinder.parseAndVisit(String name, String contents) |
| 282 : contents = contents, | 343 : contents = contents, |
| 283 unit = parseCompilationUnit(contents) { | 344 unit = parseCompilationUnit(contents, name: name) { |
| 284 visitCompilationUnit(unit); | 345 visitCompilationUnit(unit); |
| 285 } | 346 } |
| 286 | 347 |
| 287 @override | 348 @override |
| 288 visitCompilationUnitMember(CompilationUnitMember node) { | 349 visitCompilationUnitMember(CompilationUnitMember node) { |
| 289 mergeDeclarations.add(node); | 350 mergeDeclarations.add(node); |
| 290 } | 351 } |
| 291 | 352 |
| 292 @override | 353 @override |
| 293 visitClassDeclaration(ClassDeclaration node) { | 354 visitClassDeclaration(ClassDeclaration node) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 321 visitFunctionBody(node) {} // skip method bodies | 382 visitFunctionBody(node) {} // skip method bodies |
| 322 } | 383 } |
| 323 | 384 |
| 324 String _qualifiedName(Declaration node) { | 385 String _qualifiedName(Declaration node) { |
| 325 var parent = node.parent; | 386 var parent = node.parent; |
| 326 var className = ''; | 387 var className = ''; |
| 327 if (parent is ClassDeclaration) { | 388 if (parent is ClassDeclaration) { |
| 328 className = parent.name.name + '.'; | 389 className = parent.name.name + '.'; |
| 329 } | 390 } |
| 330 var name = (node as dynamic).name; | 391 var name = (node as dynamic).name; |
| 331 return className + (name != null ? name.name : ''); | 392 name = (name != null ? name.name : ''); |
| 393 |
| 394 var accessor = ''; |
| 395 if (node is MethodDeclaration) { |
| 396 if (node.isGetter) accessor = 'get:'; |
| 397 else if (node.isSetter) accessor = 'set:'; |
| 398 } |
| 399 return className + accessor + name; |
| 332 } | 400 } |
| 333 | 401 |
| 334 bool _isPatch(AnnotatedNode node) => node.metadata.any(_isPatchAnnotation); | 402 bool _isPatch(AnnotatedNode node) => node.metadata.any(_isPatchAnnotation); |
| 335 | 403 |
| 336 bool _isPatchAnnotation(Annotation m) => | 404 bool _isPatchAnnotation(Annotation m) => |
| 337 m.name.name == 'patch' && m.constructorName == null && m.arguments == null; | 405 m.name.name == 'patch' && m.constructorName == null && m.arguments == null; |
| 338 | 406 |
| 339 /// Editable string buffer. | 407 /// Editable string buffer. |
| 340 /// | 408 /// |
| 341 /// Applies a series of edits (insertions, removals, replacements) using | 409 /// Applies a series of edits (insertions, removals, replacements) using |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 if (diff != 0) return diff; | 495 if (diff != 0) return diff; |
| 428 return end - other.end; | 496 return end - other.end; |
| 429 } | 497 } |
| 430 } | 498 } |
| 431 | 499 |
| 432 List<SdkLibrary> _getSdkLibraries(String contents) { | 500 List<SdkLibrary> _getSdkLibraries(String contents) { |
| 433 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(true); | 501 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(true); |
| 434 parseCompilationUnit(contents).accept(libraryBuilder); | 502 parseCompilationUnit(contents).accept(libraryBuilder); |
| 435 return libraryBuilder.librariesMap.sdkLibraries; | 503 return libraryBuilder.librariesMap.sdkLibraries; |
| 436 } | 504 } |
| OLD | NEW |