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; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 path.join(repositoryDir, 'out', 'DebugX64', 'obj', 'gen', 'patch')); | 67 path.join(repositoryDir, 'out', 'DebugX64', 'obj', 'gen', 'patch')); |
68 final outExample = path.relative( | 68 final outExample = path.relative( |
69 path.join(repositoryDir, 'out', 'DebugX64', 'obj', 'gen', 'patched_sdk')); | 69 path.join(repositoryDir, 'out', 'DebugX64', 'obj', 'gen', 'patched_sdk')); |
70 final packagesExample = path.relative(path.join(repositoryDir, '.packages')); | 70 final packagesExample = path.relative(path.join(repositoryDir, '.packages')); |
71 print('For example:'); | 71 print('For example:'); |
72 print('\$ $self vm $sdkExample $patchExample $outExample $packagesExample'); | 72 print('\$ $self vm $sdkExample $patchExample $outExample $packagesExample'); |
73 | 73 |
74 exit(1); | 74 exit(1); |
75 } | 75 } |
76 | 76 |
77 const validModes = const ['vm', 'dart2js', 'flutter']; | |
78 String mode; | |
79 bool get forVm => mode == 'vm'; | |
80 bool get forFlutter => mode == 'flutter'; | |
81 bool get forDart2js => mode == 'dart2js'; | |
82 | |
77 Future _main(List<String> argv) async { | 83 Future _main(List<String> argv) async { |
78 if (argv.isEmpty) usage('[vm|dart2js]'); | 84 if (argv.isEmpty) usage('[${validModes.join('|')}]'); |
79 var mode = argv.first; | 85 mode = argv.first; |
80 if (mode != 'vm' && mode != 'dart2js') usage('[vm|dart2js]'); | 86 if (!validModes.contains(mode)) usage('[${validModes.join('|')}]'); |
81 if (argv.length != 5) usage(mode); | 87 if (argv.length != 5) usage(mode); |
82 | 88 |
83 bool forVm = mode == 'vm'; | |
84 bool forDart2js = mode == 'dart2js'; | |
85 var input = argv[1]; | 89 var input = argv[1]; |
86 var sdkLibIn = path.join(input, 'lib'); | 90 var sdkLibIn = path.join(input, 'lib'); |
87 var patchIn = argv[2]; | 91 var patchIn = argv[2]; |
88 var outDir = argv[3]; | 92 var outDir = argv[3]; |
89 var outDirUri = Uri.base.resolveUri(new Uri.directory(outDir)); | 93 var outDirUri = Uri.base.resolveUri(new Uri.directory(outDir)); |
90 var sdkOut = path.join(outDir, 'lib'); | 94 var sdkOut = path.join(outDir, 'lib'); |
91 var packagesFile = argv[4]; | 95 var packagesFile = argv[4]; |
92 | 96 |
93 // Parse libraries.dart | 97 // Parse libraries.dart |
94 var libContents = readInputFile(path.join( | 98 var libContents = readInputFile(path.join( |
95 sdkLibIn, '_internal', 'sdk_library_metadata', 'lib', 'libraries.dart')); | 99 sdkLibIn, '_internal', 'sdk_library_metadata', 'lib', 'libraries.dart')); |
96 if (forVm) libContents = _updateLibraryMetadata(sdkOut, libContents); | 100 libContents = _updateLibraryMetadata(sdkOut, libContents); |
97 var sdkLibraries = _getSdkLibraries(libContents, forDart2js); | 101 var sdkLibraries = _getSdkLibraries(libContents); |
98 | 102 |
99 Map<String, String> locations = <String, String>{}; | 103 Map<String, String> locations = <String, String>{}; |
100 | 104 |
101 // Enumerate core libraries and apply patches | 105 // Enumerate core libraries and apply patches |
102 for (SdkLibrary library in sdkLibraries) { | 106 for (SdkLibrary library in sdkLibraries) { |
103 if (forDart2js && library.isVmLibrary) continue; | 107 if (forDart2js && library.isVmLibrary) continue; |
104 if (forVm && library.isDart2JsLibrary) continue; | 108 if ((forVm || forFlutter) && library.isDart2JsLibrary) continue; |
ahe
2017/06/09 09:23:35
!forDart2js
Siggi Cherem (dart-lang)
2017/06/14 17:55:37
Done.
| |
105 _applyPatch(library, sdkLibIn, patchIn, sdkOut, locations); | 109 _applyPatch(library, sdkLibIn, patchIn, sdkOut, locations); |
106 } | 110 } |
107 | 111 |
108 if (forVm) _copyExtraVmLibraries(sdkOut, locations); | 112 _copyExtraLibraries(sdkOut, locations); |
109 | 113 |
110 Uri platform = outDirUri.resolve('platform.dill.tmp'); | 114 Uri platform = outDirUri.resolve('platform.dill.tmp'); |
111 Uri outline = outDirUri.resolve('outline.dill'); | 115 Uri outline = outDirUri.resolve('outline.dill'); |
112 Uri librariesJson = outDirUri.resolve("lib/libraries.json"); | 116 Uri librariesJson = outDirUri.resolve("lib/libraries.json"); |
113 Uri packages = Uri.base.resolveUri(new Uri.file(packagesFile)); | 117 Uri packages = Uri.base.resolveUri(new Uri.file(packagesFile)); |
114 | 118 |
115 await _writeSync( | 119 await _writeSync( |
116 librariesJson.toFilePath(), JSON.encode({"libraries": locations})); | 120 librariesJson.toFilePath(), JSON.encode({"libraries": locations})); |
117 | 121 |
118 if (forVm) { | 122 if (forVm || forFlutter) { |
119 await fasta.compilePlatform(outDirUri, platform, | 123 await fasta.compilePlatform(outDirUri, platform, |
120 packages: packages, outlineOutput: outline); | 124 packages: packages, |
125 outlineOutput: outline, | |
126 backendTarget: forVm ? 'vm_fasta' : 'flutter_fasta'); | |
ahe
2017/06/09 09:23:35
I think you'll get a simple conflict here as Dima
Siggi Cherem (dart-lang)
2017/06/14 17:55:37
Thanks for the heads up. Tuns out this API is stil
| |
121 } else { | 127 } else { |
122 await dart2js.compilePlatform(outDirUri, platform, | 128 await dart2js.compilePlatform(outDirUri, platform, |
123 packages: packages, outlineOutput: outline); | 129 packages: packages, outlineOutput: outline); |
124 } | 130 } |
125 | 131 |
126 Uri platformFinalLocation = outDirUri.resolve('platform.dill'); | 132 Uri platformFinalLocation = outDirUri.resolve('platform.dill'); |
127 | 133 |
128 // To properly regenerate the patched_sdk, patched_dart2js_sdk, and | 134 // To properly regenerate the patched_sdk, patched_dart2js_sdk, and |
129 // platform.dill only when necessary, we track dependencies as follows: | 135 // platform.dill only when necessary, we track dependencies as follows: |
130 // - inputs like the sdk libraries and patch files are covered by the | 136 // - inputs like the sdk libraries and patch files are covered by the |
131 // extraDependencies argument. | 137 // extraDependencies argument. |
132 // - this script and its script dependencies are handled by writeDepsFile | 138 // - this script and its script dependencies are handled by writeDepsFile |
133 // here. | 139 // here. |
134 // - the internal platform libraries that may affect how this script | 140 // - the internal platform libraries that may affect how this script |
135 // runs in the VM are discovered by providing the `platform` argument | 141 // runs in the VM are discovered by providing the `platform` argument |
136 // below. Regardless of patched_sdk or patched_dart2js_sdk we provide below | 142 // below. For this, we provide below the `platform.dill` of the VM (since |
137 // the .dill file of patched_sdk (since the script runs in the VM and not | 143 // the script runs in the VM, not in dart2js or flutter). At the BUILD.gn |
138 // in dart2js). At the BUILD.gn level we have a dependency from | 144 // level we need a dependency from patched_dart2js_sdk to patched_sdk (and |
139 // patched_dart2js_sdk to patched_sdk to ensure that file already exists. | 145 // from flutter_patched_sdk to patched_sdk) to ensure that file already |
146 // exists. | |
ahe
2017/06/09 09:23:35
I don't understand this.
Siggi Cherem (dart-lang)
2017/06/14 17:55:37
Just clarified, hope this helps.
| |
147 var platformForDeps = platform; | |
148 var sdkDir = outDirUri; | |
149 if (forDart2js || forFlutter) { | |
150 platformForDeps = outDirUri.resolve('../patched_sdk/platform.dill'); | |
151 sdkDir = outDirUri.resolve('../patched_sdk/'); | |
152 } | |
140 await fasta.writeDepsFile(Platform.script, | 153 await fasta.writeDepsFile(Platform.script, |
141 Uri.base.resolveUri(new Uri.file("$outDir.d")), platformFinalLocation, | 154 Uri.base.resolveUri(new Uri.file("$outDir.d")), platformFinalLocation, |
142 sdk: outDirUri, | 155 sdk: sdkDir, |
143 packages: packages, | 156 packages: packages, |
144 platform: | 157 platform: platformForDeps, |
145 forVm ? platform : outDirUri.resolve('../patched_sdk/platform.dill'), | |
146 extraDependencies: deps); | 158 extraDependencies: deps); |
147 | 159 |
148 await new File.fromUri(platform).rename(platformFinalLocation.toFilePath()); | 160 await new File.fromUri(platform).rename(platformFinalLocation.toFilePath()); |
149 } | 161 } |
150 | 162 |
151 /// Updates the contents of | 163 /// Updates the contents of |
152 /// sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart to include | 164 /// sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart to include |
153 /// declarations for vm internal libraries. | 165 /// declarations for vm internal libraries. |
154 String _updateLibraryMetadata(String sdkOut, String libContents) { | 166 String _updateLibraryMetadata(String sdkOut, String libContents) { |
155 // Copy and patch libraries.dart and version | 167 if (!forVm && !forFlutter) return libContents; |
156 libContents = libContents.replaceAll( | 168 var extraLibraries = new StringBuffer(); |
157 ' libraries = const {', | 169 extraLibraries.write(''' |
158 ''' libraries = const { | |
159 | |
160 "_builtin": const LibraryInfo( | 170 "_builtin": const LibraryInfo( |
161 "_builtin/_builtin.dart", | 171 "_builtin/_builtin.dart", |
162 categories: "Client,Server", | 172 categories: "Client,Server", |
163 implementation: true, | 173 implementation: true, |
164 documented: false, | 174 documented: false, |
165 platforms: VM_PLATFORM), | 175 platforms: VM_PLATFORM), |
166 | 176 |
167 "profiler": const LibraryInfo( | 177 "profiler": const LibraryInfo( |
168 "profiler/profiler.dart", | 178 "profiler/profiler.dart", |
169 maturity: Maturity.DEPRECATED, | 179 maturity: Maturity.DEPRECATED, |
170 documented: false), | 180 documented: false), |
171 | 181 |
172 "_vmservice": const LibraryInfo( | 182 "_vmservice": const LibraryInfo( |
173 "vmservice/vmservice.dart", | 183 "vmservice/vmservice.dart", |
174 implementation: true, | 184 implementation: true, |
175 documented: false, | 185 documented: false, |
176 platforms: VM_PLATFORM), | 186 platforms: VM_PLATFORM), |
187 '''); | |
177 | 188 |
178 "vmservice_io": const LibraryInfo( | 189 if (forVm) { |
179 "vmservice_io/vmservice_io.dart", | 190 extraLibraries.write(''' |
180 implementation: true, | 191 "vmservice_io": const LibraryInfo( |
Siggi Cherem (dart-lang)
2017/06/08 22:18:53
Just discovered from talking with Siva that vmserv
| |
181 documented: false, | 192 "vmservice_io/vmservice_io.dart", |
182 platforms: VM_PLATFORM), | 193 implementation: true, |
194 documented: false, | |
195 platforms: VM_PLATFORM), | |
196 '''); | |
197 } else { | |
198 extraLibraries.write(''' | |
199 "vmservice_sky": const LibraryInfo( | |
200 "vmservice_io/vmservice_io.dart", | |
201 implementation: true, | |
202 documented: false, | |
203 platforms: VM_PLATFORM), | |
183 | 204 |
184 '''); | 205 "ui": const LibraryInfo( |
206 "ui/ui.dart", | |
207 categories: "Client,Server", | |
208 implementation: true, | |
209 documented: false, | |
210 platforms: VM_PLATFORM), | |
211 '''); | |
212 } | |
213 | |
214 libContents = libContents.replaceAll( | |
215 ' libraries = const {', ' libraries = const { $extraLibraries'); | |
185 _writeSync( | 216 _writeSync( |
186 path.join( | 217 path.join( |
187 sdkOut, '_internal', 'sdk_library_metadata', 'lib', 'libraries.dart'), | 218 sdkOut, '_internal', 'sdk_library_metadata', 'lib', 'libraries.dart'), |
188 libContents); | 219 libContents); |
189 return libContents; | 220 return libContents; |
190 } | 221 } |
191 | 222 |
192 /// Copy internal libraries that are developed under 'runtime/bin/' to the | 223 /// Copy internal libraries that are developed outside the sdk folder into the |
193 /// patched_sdk folder. | 224 /// patched_sdk folder. For the VM< this includes files under 'runtime/bin/', |
194 _copyExtraVmLibraries(String sdkOut, Map<String, String> locations) { | 225 /// for flutter, this is includes also the ui library. |
226 _copyExtraLibraries(String sdkOut, Map<String, String> locations) { | |
227 if (forDart2js) return; | |
195 var base = path.fromUri(Platform.script); | 228 var base = path.fromUri(Platform.script); |
196 var dartDir = path.dirname(path.dirname(path.absolute(base))); | 229 var dartDir = path.dirname(path.dirname(path.absolute(base))); |
197 | 230 |
198 for (var tuple in [ | 231 var builtinLibraryIn = path.join(dartDir, 'runtime', 'bin', 'builtin.dart'); |
199 ['_builtin', 'builtin.dart'] | 232 var builtinLibraryOut = path.join(sdkOut, '_builtin', '_builtin.dart'); |
200 ]) { | 233 _writeSync(builtinLibraryOut, readInputFile(builtinLibraryIn)); |
201 var vmLibrary = tuple[0]; | 234 locations['_builtin'] = path.join('_builtin', '_builtin.dart'); |
202 var dartFile = tuple[1]; | |
203 | |
204 // The "dart:_builtin" library is only available for the DartVM. | |
205 var builtinLibraryIn = path.join(dartDir, 'runtime', 'bin', dartFile); | |
206 var builtinLibraryOut = path.join(sdkOut, vmLibrary, '${vmLibrary}.dart'); | |
207 _writeSync(builtinLibraryOut, readInputFile(builtinLibraryIn)); | |
208 locations[vmLibrary] = path.join(vmLibrary, '${vmLibrary}.dart'); | |
209 } | |
210 | 235 |
211 for (var file in ['loader.dart', 'server.dart', 'vmservice_io.dart']) { | 236 for (var file in ['loader.dart', 'server.dart', 'vmservice_io.dart']) { |
212 var libraryIn = path.join(dartDir, 'runtime', 'bin', 'vmservice', file); | 237 var libraryIn = path.join(dartDir, 'runtime', 'bin', 'vmservice', file); |
213 var libraryOut = path.join(sdkOut, 'vmservice_io', file); | 238 var libraryOut = path.join(sdkOut, 'vmservice_io', file); |
214 _writeSync(libraryOut, readInputFile(libraryIn)); | 239 _writeSync(libraryOut, readInputFile(libraryIn)); |
215 } | 240 } |
216 locations["vmservice_io"] = "vmservice_io/vmservice_io.dart"; | 241 locations[forVm ? "vmservice_io" : "vmservice_sky"] = |
242 path.join('vmservice_io', 'vmservice_io.dart'); | |
243 | |
244 if (forFlutter) { | |
245 // Flutter repo has this layout: | |
246 // engine/src/ | |
247 // dart/ | |
248 // flutter/ | |
249 var srcDir = path.dirname(path.dirname(path.dirname(path.absolute(base)))); | |
250 var uiLibraryInDir = path.join(srcDir, 'flutter', 'lib', 'ui'); | |
251 for (var file in new Directory(uiLibraryInDir).listSync()) { | |
252 if (!file.path.endsWith('.dart')) continue; | |
253 var name = path.basename(file.path); | |
254 var uiLibraryOut = path.join(sdkOut, 'ui', name); | |
255 _writeSync(uiLibraryOut, readInputFile(file.path)); | |
256 } | |
257 locations['ui'] = 'ui/ui.dart'; | |
258 } | |
217 } | 259 } |
218 | 260 |
219 _applyPatch(SdkLibrary library, String sdkLibIn, String patchIn, String sdkOut, | 261 _applyPatch(SdkLibrary library, String sdkLibIn, String patchIn, String sdkOut, |
220 Map<String, String> locations) { | 262 Map<String, String> locations) { |
221 var libraryOut = path.join(sdkLibIn, library.path); | 263 var libraryOut = path.join(sdkLibIn, library.path); |
222 var libraryIn = libraryOut; | 264 var libraryIn = libraryOut; |
223 | 265 |
224 var libraryFile = getInputFile(libraryIn, canBeMissing: true); | 266 var libraryFile = getInputFile(libraryIn, canBeMissing: true); |
225 if (libraryFile != null) { | 267 if (libraryFile != null) { |
226 locations[Uri.parse(library.shortName).path] = | 268 locations[Uri.parse(library.shortName).path] = |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
617 | 659 |
618 String toString() => '(Edit @ $begin,$end: "$replace")'; | 660 String toString() => '(Edit @ $begin,$end: "$replace")'; |
619 | 661 |
620 int compareTo(_StringEdit other) { | 662 int compareTo(_StringEdit other) { |
621 int diff = begin - other.begin; | 663 int diff = begin - other.begin; |
622 if (diff != 0) return diff; | 664 if (diff != 0) return diff; |
623 return end - other.end; | 665 return end - other.end; |
624 } | 666 } |
625 } | 667 } |
626 | 668 |
627 List<SdkLibrary> _getSdkLibraries(String contents, bool useDart2js) { | 669 List<SdkLibrary> _getSdkLibraries(String contents) { |
628 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(useDart2js); | 670 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(forDart2js); |
629 parseCompilationUnit(contents).accept(libraryBuilder); | 671 parseCompilationUnit(contents).accept(libraryBuilder); |
630 return libraryBuilder.librariesMap.sdkLibraries; | 672 return libraryBuilder.librariesMap.sdkLibraries; |
631 } | 673 } |
OLD | NEW |