Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: tools/patch_sdk.dart

Issue 2434123003: Merge more Kernel infrastructure from kernel_sdk SDK fork. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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++) {
126 _writeSync(outPaths[i], contents[i]); 153 _writeSync(outPaths[i], contents[i]);
127 } 154 }
128 } 155 }
129 } 156 }
130 } 157 }
158 if (mode == 'vm') {
159 for (var tuple in [['nativewrappers', 'nativewrappers.dart'],
160 ['_builtin', 'builtin.dart']]) {
161 var vmLibrary = tuple[0];
162 var dartFile = tuple[1];
163
164 // The "dart:_builtin" library is only available for the DartVM.
165 var builtinLibraryIn = path.join(dartDir, 'runtime', 'bin', dartFile);
166 var builtinLibraryOut = path.join(sdkOut, vmLibrary, '${vmLibrary}.dart');
167 _writeSync(builtinLibraryOut, new File(builtinLibraryIn).readAsStringSync( ));
168 }
169
170 for (var file in ['loader.dart', 'server.dart', 'vmservice_io.dart']) {
171 var libraryIn = path.join(dartDir, 'runtime', 'bin', 'vmservice', file);
172 var libraryOut = path.join(sdkOut, 'vmservice_io', file);
173 _writeSync(libraryOut, new File(libraryIn).readAsStringSync());
174 }
175 }
131 } 176 }
132 177
133 /// Writes a file, creating the directory if needed. 178 /// Writes a file, creating the directory if needed.
134 void _writeSync(String filePath, String contents) { 179 void _writeSync(String filePath, String contents) {
135 var outDir = new Directory(path.dirname(filePath)); 180 var outDir = new Directory(path.dirname(filePath));
136 if (!outDir.existsSync()) outDir.createSync(recursive: true); 181 if (!outDir.existsSync()) outDir.createSync(recursive: true);
137 182
138 new File(filePath).writeAsStringSync(contents); 183 new File(filePath).writeAsStringSync(contents);
139 } 184 }
140 185
141 /// Merges dart:* library code with code from *_patch.dart file. 186 /// Merges dart:* library code with code from *_patch.dart file.
142 /// 187 ///
143 /// Takes a list of the library's parts contents, with the main library contents 188 /// 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. 189 /// first in the list, and the contents of the patch file.
145 /// 190 ///
146 /// The result will have `@patch` implementations merged into the correct place 191 /// The result will have `@patch` implementations merged into the correct place
147 /// (e.g. the class or top-level function declaration) and all other 192 /// (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 193 /// declarations introduced by the patch will be placed into the main library
149 /// file. 194 /// file.
150 /// 195 ///
151 /// This is purely a syntactic transformation. Unlike dart2js patch files, there 196 /// 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 197 /// is no semantic meaning given to the *_patch files, and they do not magically
153 /// get their own library scope, etc. 198 /// get their own library scope, etc.
154 /// 199 ///
155 /// Editorializing: the dart2js approach requires a Dart front end such as 200 /// Editorializing: the dart2js approach requires a Dart front end such as
156 /// package:analyzer to semantically model a feature beyond what is specified 201 /// 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 202 /// 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 203 /// 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. 204 /// seems like a non-ideal situation. Instead we keep the preprocessing simple.
160 List<String> _patchLibrary(List<String> partsContents, String patchContents) { 205 List<String> _patchLibrary(String name,
206 List<String> partsContents,
207 String patchContents) {
161 var results = <StringEditBuffer>[]; 208 var results = <StringEditBuffer>[];
162 209
163 // Parse the patch first. We'll need to extract bits of this as we go through 210 // Parse the patch first. We'll need to extract bits of this as we go through
164 // the other files. 211 // the other files.
165 var patchFinder = new PatchFinder.parseAndVisit(patchContents); 212 final patchFinder = new PatchFinder.parseAndVisit(name, patchContents);
166 213
167 // Merge `external` declarations with the corresponding `@patch` code. 214 // Merge `external` declarations with the corresponding `@patch` code.
168 for (var partContent in partsContents) { 215 for (var partContent in partsContents) {
169 var partEdits = new StringEditBuffer(partContent); 216 var partEdits = new StringEditBuffer(partContent);
170 var partUnit = parseCompilationUnit(partContent); 217 var partUnit = parseCompilationUnit(partContent);
171 partUnit.accept(new PatchApplier(partEdits, patchFinder)); 218 partUnit.accept(new PatchApplier(partEdits, patchFinder));
172 results.add(partEdits); 219 results.add(partEdits);
173 } 220 }
174 return new List<String>.from(results.map((e) => e.toString())); 221 return new List<String>.from(results.map((e) => e.toString()));
175 } 222 }
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 } 318 }
272 319
273 class PatchFinder extends GeneralizingAstVisitor { 320 class PatchFinder extends GeneralizingAstVisitor {
274 final String contents; 321 final String contents;
275 final CompilationUnit unit; 322 final CompilationUnit unit;
276 323
277 final Map patches = <String, Declaration>{}; 324 final Map patches = <String, Declaration>{};
278 final Map mergeMembers = <String, List<ClassMember>>{}; 325 final Map mergeMembers = <String, List<ClassMember>>{};
279 final List mergeDeclarations = <CompilationUnitMember>[]; 326 final List mergeDeclarations = <CompilationUnitMember>[];
280 327
281 PatchFinder.parseAndVisit(String contents) 328 PatchFinder.parseAndVisit(String name, String contents)
282 : contents = contents, 329 : contents = contents,
283 unit = parseCompilationUnit(contents) { 330 unit = parseCompilationUnit(contents, name: name) {
284 visitCompilationUnit(unit); 331 visitCompilationUnit(unit);
285 } 332 }
286 333
287 @override 334 @override
288 visitCompilationUnitMember(CompilationUnitMember node) { 335 visitCompilationUnitMember(CompilationUnitMember node) {
289 mergeDeclarations.add(node); 336 mergeDeclarations.add(node);
290 } 337 }
291 338
292 @override 339 @override
293 visitClassDeclaration(ClassDeclaration node) { 340 visitClassDeclaration(ClassDeclaration node) {
(...skipping 27 matching lines...) Expand all
321 visitFunctionBody(node) {} // skip method bodies 368 visitFunctionBody(node) {} // skip method bodies
322 } 369 }
323 370
324 String _qualifiedName(Declaration node) { 371 String _qualifiedName(Declaration node) {
325 var parent = node.parent; 372 var parent = node.parent;
326 var className = ''; 373 var className = '';
327 if (parent is ClassDeclaration) { 374 if (parent is ClassDeclaration) {
328 className = parent.name.name + '.'; 375 className = parent.name.name + '.';
329 } 376 }
330 var name = (node as dynamic).name; 377 var name = (node as dynamic).name;
331 return className + (name != null ? name.name : ''); 378 name = (name != null ? name.name : '');
379
380 var accessor = '';
381 if (node is MethodDeclaration) {
382 if (node.isGetter) accessor = 'get:';
383 else if (node.isSetter) accessor = 'set:';
384 }
385 return className + accessor + name;
332 } 386 }
333 387
334 bool _isPatch(AnnotatedNode node) => node.metadata.any(_isPatchAnnotation); 388 bool _isPatch(AnnotatedNode node) => node.metadata.any(_isPatchAnnotation);
335 389
336 bool _isPatchAnnotation(Annotation m) => 390 bool _isPatchAnnotation(Annotation m) =>
337 m.name.name == 'patch' && m.constructorName == null && m.arguments == null; 391 m.name.name == 'patch' && m.constructorName == null && m.arguments == null;
338 392
339 /// Editable string buffer. 393 /// Editable string buffer.
340 /// 394 ///
341 /// Applies a series of edits (insertions, removals, replacements) using 395 /// Applies a series of edits (insertions, removals, replacements) using
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 if (diff != 0) return diff; 481 if (diff != 0) return diff;
428 return end - other.end; 482 return end - other.end;
429 } 483 }
430 } 484 }
431 485
432 List<SdkLibrary> _getSdkLibraries(String contents) { 486 List<SdkLibrary> _getSdkLibraries(String contents) {
433 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(true); 487 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(true);
434 parseCompilationUnit(contents).accept(libraryBuilder); 488 parseCompilationUnit(contents).accept(libraryBuilder);
435 return libraryBuilder.librariesMap.sdkLibraries; 489 return libraryBuilder.librariesMap.sdkLibraries;
436 } 490 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698