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

Side by Side Diff: tools/patch_sdk.dart

Issue 2953703002: Tweak public APIs and use them in patch_sdk, dart2js, and kernel-service (Closed)
Patch Set: Created 3 years, 5 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: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 import 'dart:convert' show JSON;
14 14
15 import 'package:analyzer/analyzer.dart'; 15 import 'package:analyzer/analyzer.dart';
16 import 'package:analyzer/src/generated/sdk.dart'; 16 import 'package:analyzer/src/generated/sdk.dart';
17 import 'package:path/path.dart' as path; 17 import 'package:path/path.dart' as path;
18 18
19 import 'package:front_end/src/fasta/fasta.dart' as fasta 19 import 'package:front_end/front_end.dart';
20 show compile, compilePlatform, writeDepsFile; 20 import 'package:front_end/src/kernel_generator_impl.dart';
21 import 'package:front_end/src/fasta/util/relativize.dart' show relativizeUri;
21 22
22 import 'package:compiler/src/kernel/fasta_support.dart' as dart2js 23 import 'package:front_end/src/fasta/fasta.dart' as fasta show getDependencies;
23 show compilePlatform; 24 import 'package:front_end/src/fasta/kernel/utils.dart' show writeProgramToFile;
25
26 import 'package:kernel/target/targets.dart';
27 import 'package:kernel/target/vm_fasta.dart';
28 import 'package:kernel/target/flutter_fasta.dart';
29 import 'package:compiler/src/kernel/dart2js_target.dart' show Dart2jsTarget;
24 30
25 /// Set of input files that were read by this script to generate patched SDK. 31 /// Set of input files that were read by this script to generate patched SDK.
26 /// We will dump it out into the depfile for ninja to use. 32 /// We will dump it out into the depfile for ninja to use.
27 /// 33 ///
28 /// For more information see GN and Ninja references: 34 /// For more information see GN and Ninja references:
29 /// https://chromium.googlesource.com/chromium/src/+/56807c6cb383140af0c03da8 f6731d77785d7160/tools/gn/docs/reference.md#depfile_string_File-name-for-input-d ependencies-for-actions 35 /// https://chromium.googlesource.com/chromium/src/+/56807c6cb383140af0c03da8 f6731d77785d7160/tools/gn/docs/reference.md#depfile_string_File-name-for-input-d ependencies-for-actions
30 /// https://ninja-build.org/manual.html#_depfile 36 /// https://ninja-build.org/manual.html#_depfile
31 /// 37 ///
32 final deps = new Set<Uri>(); 38 final deps = new Set<Uri>();
33 39
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 _copyExtraLibraries(sdkOut, locations); 118 _copyExtraLibraries(sdkOut, locations);
113 119
114 Uri platform = outDirUri.resolve('platform.dill.tmp'); 120 Uri platform = outDirUri.resolve('platform.dill.tmp');
115 Uri outline = outDirUri.resolve('outline.dill'); 121 Uri outline = outDirUri.resolve('outline.dill');
116 Uri librariesJson = outDirUri.resolve("lib/libraries.json"); 122 Uri librariesJson = outDirUri.resolve("lib/libraries.json");
117 Uri packages = Uri.base.resolveUri(new Uri.file(packagesFile)); 123 Uri packages = Uri.base.resolveUri(new Uri.file(packagesFile));
118 124
119 await _writeSync( 125 await _writeSync(
120 librariesJson.toFilePath(), JSON.encode({"libraries": locations})); 126 librariesJson.toFilePath(), JSON.encode({"libraries": locations}));
121 127
122 if (forVm || forFlutter) { 128 var flags = new TargetFlags();
123 await fasta.compilePlatform(outDirUri, platform, 129 var target = forVm
ahe 2017/07/11 09:34:26 This seems brittle compared to: switch (mode) {
Siggi Cherem (dart-lang) 2017/07/11 23:16:32 will do
124 packages: packages, 130 ? new VmFastaTarget(flags)
125 outlineOutput: outline, 131 : (forFlutter ? new FlutterFastaTarget(flags) : new Dart2jsTarget(flags));
126 backendTarget: forVm ? 'vm_fasta' : 'flutter_fasta'); 132 var platformDeps =
127 } else { 133 await compilePlatform(outDirUri, target, packages, platform, outline);
128 await dart2js.compilePlatform(outDirUri, platform, 134 deps.addAll(platformDeps);
129 packages: packages, outlineOutput: outline);
130 }
131 135
132 if (forVm) { 136 if (forVm) {
137 // TODO(sigmund): add support for the flutter vmservice_sky as well.
138 var vmserviceName = 'vmservice_io';
133 var base = path.fromUri(Platform.script); 139 var base = path.fromUri(Platform.script);
134 Uri repositoryDir = 140 Uri dartDir =
135 new Uri.directory(path.dirname(path.dirname(path.absolute(base)))); 141 new Uri.directory(path.dirname(path.dirname(path.absolute(base))));
136 var vmserviceName = 'vmservice_io'; 142 var program = await kernelForProgram(
137 Uri vmserviceSdk = repositoryDir.resolve('runtime/bin/vmservice_sdk/'); 143 Uri.parse('dart:$vmserviceName'),
144 new CompilerOptions()
145 ..sdkSummary = outline
146 ..dartLibraries = <String, Uri>{
147 '_vmservice': outDirUri.resolve('lib/vmservice/vmservice.dart'),
148 'vmservice_io':
149 dartDir.resolve('runtime/bin/vmservice/vmservice_io.dart'),
150 }
151 ..packagesFileUri = packages);
138 Uri vmserviceUri = outDirUri.resolve('$vmserviceName.dill'); 152 Uri vmserviceUri = outDirUri.resolve('$vmserviceName.dill');
139 // TODO(sigmundch): Specify libraries.json directly instead of "--sdk" 153 await writeProgramToFile(program, vmserviceUri);
140 // after #29882 is fixed.
141 await fasta.compile([
142 "--sdk=$vmserviceSdk",
143 "--platform=$outline",
144 "--target=vm_fasta",
145 "--packages=$packages",
146 "dart:$vmserviceName",
147 "-o",
148 "$vmserviceUri",
149 ]);
150 } 154 }
151 155
152 Uri platformFinalLocation = outDirUri.resolve('platform.dill'); 156 Uri platformFinalLocation = outDirUri.resolve('platform.dill');
153 157
154 // We generate a dependency file for GN to properly regenerate the patched sdk 158 // We generate a dependency file for GN to properly regenerate the patched sdk
155 // folder, outline.dill and platform.dill files when necessary: either when 159 // folder, outline.dill and platform.dill files when necessary: either when
156 // the sdk sources change or when this script is updated. In particular: 160 // the sdk sources change or when this script is updated. In particular:
157 // 161 //
158 // - sdk changes: we track the actual sources we are compiling. If we are 162 // - sdk changes: we track the actual sources we are compiling. If we are
159 // building the dart2js sdk, this includes the dart2js-specific patch 163 // building the dart2js sdk, this includes the dart2js-specific patch
160 // files. 164 // files.
161 // 165 //
162 // These files are tracked by [deps] and passed below to [writeDepsFile] in 166 // These files are tracked by [deps] and passed below to [writeDepsFile] in
163 // the extraDependencies argument. 167 // the extraDependencies argument.
164 // 168 //
165 // - script updates: we track this script file and any code it imports (even 169 // - script updates: we track this script file and any code it imports (even
166 // sdk libraries). Note that this script runs on the standalone VM, so any 170 // sdk libraries). Note that this script runs on the standalone VM, so any
167 // sdk library used by this script indirectly depends on a VM-specific 171 // sdk library used by this script indirectly depends on a VM-specific
168 // patch file. 172 // patch file.
169 // 173 //
170 // These set of files is discovered by `writeDepsFile` below, and the 174 // These set of files is discovered by `getDependencies` below, and the
171 // [platformForDeps] is always the VM-specific `platform.dill` file. 175 // [platformForDeps] is always the VM-specific `platform.dill` file.
172 //
173 // TODO(sigmund): we should change this:
174 // - we should rewrite writeDepsFile: fasta could provide an API to crawl
175 // the dependencies, but anything that is GN specific, should be on
176 // this file instead.
177 //
178 // - We don't need to include sdk dependencies of the script because
179 // those are already included indirectly (either in [deps] when
180 // building the sdk for the VM, or via the .GN dependencies in the
181 // build files for dart2js and flutter).
182 var platformForDeps = platform; 176 var platformForDeps = platform;
183 var sdkDir = outDirUri; 177 var sdkDir = outDirUri;
184 if (forDart2js || forFlutter) { 178 if (forDart2js || forFlutter) {
185 // Note: this would fail if `../patched_sdk/platform.dill` doesn't exist. We 179 // Note: this would fail if `../patched_sdk/platform.dill` doesn't exist. We
186 // added an explicit dependency in the .GN rules so patched_dart2js_sdk (and 180 // added an explicit dependency in the .GN rules so patched_dart2js_sdk (and
187 // patched_flutter_sdk) depend on patched_sdk to ensure that it exists. 181 // patched_flutter_sdk) depend on patched_sdk to ensure that it exists.
188 platformForDeps = outDirUri.resolve('../patched_sdk/platform.dill'); 182 platformForDeps = outDirUri.resolve('../patched_sdk/platform.dill');
189 sdkDir = outDirUri.resolve('../patched_sdk/'); 183 sdkDir = outDirUri.resolve('../patched_sdk/');
190 } 184 }
191 await fasta.writeDepsFile(Platform.script, 185 deps.addAll(await fasta.getDependencies(Platform.script,
192 Uri.base.resolveUri(new Uri.file("$outDir.d")), platformFinalLocation, 186 sdk: sdkDir, packages: packages, platform: platformForDeps));
193 sdk: sdkDir, 187 await writeDepsFile(Uri.base.resolveUri(new Uri.file("$outDir.d")),
194 packages: packages, 188 platformFinalLocation, deps);
195 platform: platformForDeps, 189 await new File.fromUri(platform).rename(platformFinalLocation.toFilePath());
196 extraDependencies: deps); 190 }
197 191
198 await new File.fromUri(platform).rename(platformFinalLocation.toFilePath()); 192 /// Generates an outline.dill and platform.dill file containing the result of
193 /// compiling a platform's SDK.
194 ///
195 /// Returns a list of dependencies read by the compiler. This list can be used
196 /// to create GN dependency files.
197 Future<List<Uri>> compilePlatform(Uri patchedSdk, Target target, Uri packages,
198 Uri fullOutput, Uri outlineOutput) async {
199 var options = new CompilerOptions()
200 ..strongMode = false
201 ..compileSdk = true
202 ..sdkRoot = patchedSdk
203 ..packagesFileUri = packages
204 ..chaseDependencies = true
205 ..target = target;
206
207 var result = await generateKernel([Uri.parse('dart:core')], options,
208 buildSummary: true, buildProgram: true);
209 new File.fromUri(outlineOutput).writeAsBytesSync(result.summary);
210 await writeProgramToFile(result.program, fullOutput);
211 return result.deps;
212 }
213
214 Future writeDepsFile(
215 Uri output, Uri depsFile, Iterable<Uri> allDependencies) async {
216 if (allDependencies.isEmpty) return;
217 String toRelativeFilePath(Uri uri) {
218 // Ninja expects to find file names relative to the current working
219 // directory. We've tried making them relative to the deps file, but that
220 // doesn't work for downstream projects. Making them absolute also
221 // doesn't work.
222 //
223 // We can test if it works by running ninja twice, for example:
224 //
225 // ninja -C xcodebuild/ReleaseX64 runtime_kernel -d explain
226 // ninja -C xcodebuild/ReleaseX64 runtime_kernel -d explain
227 //
228 // The second time, ninja should say:
229 //
230 // ninja: Entering directory `xcodebuild/ReleaseX64'
231 // ninja: no work to do.
232 //
233 // It's broken if it says something like this:
234 //
235 // ninja explain: expected depfile 'patched_sdk.d' to mention
236 // 'patched_sdk/platform.dill', got
237 // '/.../xcodebuild/ReleaseX64/patched_sdk/platform.dill'
238 return Uri.parse(relativizeUri(uri, base: Uri.base)).toFilePath();
239 }
240
241 StringBuffer sb = new StringBuffer();
242 sb.write(toRelativeFilePath(output));
243 sb.write(":");
244 for (Uri uri in allDependencies) {
245 sb.write(" ");
246 sb.write(toRelativeFilePath(uri));
247 }
248 sb.writeln();
249 await new File.fromUri(depsFile).writeAsString("$sb");
199 } 250 }
200 251
201 /// Updates the contents of 252 /// Updates the contents of
202 /// sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart to include 253 /// sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart to include
203 /// declarations for vm internal libraries. 254 /// declarations for vm internal libraries.
204 String _updateLibraryMetadata(String sdkOut, String libContents) { 255 String _updateLibraryMetadata(String sdkOut, String libContents) {
205 if (!forVm && !forFlutter) return libContents; 256 if (!forVm && !forFlutter) return libContents;
206 var extraLibraries = new StringBuffer(); 257 var extraLibraries = new StringBuffer();
207 extraLibraries.write(''' 258 extraLibraries.write('''
208 "_builtin": const LibraryInfo( 259 "_builtin": const LibraryInfo(
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after
674 if (diff != 0) return diff; 725 if (diff != 0) return diff;
675 return end - other.end; 726 return end - other.end;
676 } 727 }
677 } 728 }
678 729
679 List<SdkLibrary> _getSdkLibraries(String contents) { 730 List<SdkLibrary> _getSdkLibraries(String contents) {
680 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(forDart2js); 731 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(forDart2js);
681 parseCompilationUnit(contents).accept(libraryBuilder); 732 parseCompilationUnit(contents).accept(libraryBuilder);
682 return libraryBuilder.librariesMap.sdkLibraries; 733 return libraryBuilder.librariesMap.sdkLibraries;
683 } 734 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698