forked from XianyuTech/aspectd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0001-aspectd.patch
488 lines (479 loc) · 19.4 KB
/
0001-aspectd.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
From 97856855446bab0bd8e49bd2c5a589ab5383f83c Mon Sep 17 00:00:00 2001
From: KyleWong <kang.wang1988@gmail.com>
Date: Sat, 1 Feb 2020 22:46:29 +0800
Subject: [PATCH] aspectd
---
packages/flutter_tools/lib/src/aot.dart | 3 +
packages/flutter_tools/lib/src/aspectd.dart | 378 ++++++++++++++++++
.../lib/src/build_system/targets/dart.dart | 5 +
.../lib/src/commands/build_bundle.dart | 6 +
packages/flutter_tools/lib/src/compile.dart | 2 +
5 files changed, 394 insertions(+)
create mode 100644 packages/flutter_tools/lib/src/aspectd.dart
diff --git a/packages/flutter_tools/lib/src/aot.dart b/packages/flutter_tools/lib/src/aot.dart
index 750d20d25..3e4e383ee 100644
--- a/packages/flutter_tools/lib/src/aot.dart
+++ b/packages/flutter_tools/lib/src/aot.dart
@@ -6,6 +6,7 @@ import 'dart:async';
import 'package:meta/meta.dart';
+import 'aspectd.dart';
import 'base/build.dart';
import 'base/common.dart';
import 'base/io.dart';
@@ -88,6 +89,8 @@ class AotBuilder {
if (kernelOut == null) {
throwToolExit('Compiler terminated unexpectedly.');
return;
+ } else if (await AspectdHook.isAspectdEnabled()) {
+ await AspectdHook().runBuildAOTDillCommand(platform, outputPath, buildMode, extraFrontEndOptions, dartDefines);
}
// Build AOT snapshot.
diff --git a/packages/flutter_tools/lib/src/aspectd.dart b/packages/flutter_tools/lib/src/aspectd.dart
new file mode 100644
index 000000000..f5c2edaa2
--- /dev/null
+++ b/packages/flutter_tools/lib/src/aspectd.dart
@@ -0,0 +1,378 @@
+// Copyright 2018 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'dart:io';
+
+import 'package:args/args.dart';
+
+import 'artifacts.dart';
+import 'base/build.dart';
+import 'base/common.dart';
+import 'base/file_system.dart';
+import 'build_info.dart';
+import 'build_system/build_system.dart';
+import 'build_system/exceptions.dart';
+import 'build_system/targets/dart.dart';
+import 'bundle.dart';
+import 'cache.dart';
+import 'compile.dart';
+import 'dart/package_map.dart';
+import 'dart/sdk.dart';
+import 'globals.dart';
+import 'project.dart';
+import 'runner/flutter_command.dart';
+
+const String unmatchDartKernelBinaryErrMsg =
+ "Can't load Kernel binary: Invalid kernel binary format version.";
+const String aspectdImplPackageRelPath = '.';
+const String aspectdImplPackageName = 'aspectd_impl';
+
+class AspectdHook {
+ static String aspectdTransformerSnapshot;
+ static Directory getAspectdDirectory(Directory rootProjectDir) {
+ return fs.directory(fs.path.normalize(fs.path.join(rootProjectDir.path,
+ aspectdImplPackageRelPath, aspectdImplPackageName)));
+ }
+
+ static Future<void> checkAspectdTransformerSnapshot() async {
+ final PackageMap packageMap = PackageMap(fs.path.join(
+ getAspectdDirectory(fs.currentDirectory).path,
+ PackageMap.globalPackagesPath));
+ if (packageMap.map != null) {
+ final String aspectdLibPath = packageMap.map['aspectd']?.toFilePath();
+ if (aspectdLibPath != null) {
+ final String expectedTransformerSnapshotPath = fs.path.join(
+ fs.directory(aspectdLibPath).parent.path,
+ 'snapshot',
+ 'aspectd.dart.snapshot');
+ final File expectedTransformerSnapshot =
+ fs.file(expectedTransformerSnapshotPath);
+ final String expectedDartSha = getExpectedDartSha();
+ final String aspectdPubspecPath = fs.path
+ .join(fs.directory(aspectdLibPath).parent.path, 'pubspec.yaml');
+ final String defaultDartSha = getDartShaFromPubspec(aspectdPubspecPath);
+ if (defaultDartSha == null || expectedDartSha == null) {
+ return;
+ }
+ if (defaultDartSha != expectedDartSha) {
+ if (expectedTransformerSnapshot.existsSync()) {
+ expectedTransformerSnapshot.deleteSync();
+ }
+ final File aspectdPubspecFile = fs.file(aspectdPubspecPath);
+ final String aspectdPubspecContent = aspectdPubspecFile
+ .readAsStringSync()
+ .replaceAll(defaultDartSha, expectedDartSha);
+ aspectdPubspecFile.writeAsStringSync(aspectdPubspecContent,
+ flush: true);
+ }
+ if (!expectedTransformerSnapshot.existsSync()) {
+ await generateAspectdSnapshot(expectedTransformerSnapshotPath);
+ }
+ if (expectedTransformerSnapshot.existsSync()) {
+ final List<String> command = <String>[
+ artifacts.getArtifactPath(Artifact.engineDartBinary),
+ expectedTransformerSnapshotPath,
+ ];
+ final ProcessResult result = await processManager.run(command);
+ final String outputStr = result.stderr.toString().trim();
+ if (outputStr == unmatchDartKernelBinaryErrMsg) {
+ fs.file(expectedTransformerSnapshotPath).deleteSync();
+ return;
+ }
+ aspectdTransformerSnapshot = expectedTransformerSnapshotPath;
+ }
+ }
+ }
+ }
+
+ static String getExpectedDartSha() {
+ final File engineVersionFile = fs.file(fs.path
+ .join(Cache.flutterRoot, 'bin', 'cache', 'dart-sdk', 'revision'));
+ final String engineVersion = engineVersionFile.readAsStringSync().trim();
+ return engineVersion;
+ }
+
+ static String getDartShaFromPubspec(String pubspecFilePath) {
+ final File pubspecFile = fs.file(pubspecFilePath);
+ final String pubspecContent = pubspecFile.readAsStringSync();
+ final RegExp kernelItemReg = RegExp(
+ r'(\s+kernel\s*\:)(\s+git\s*\:)(\s+[a-z]+\s*\:.*)(\s+[a-z]+\s*\:.*)(\s+[a-z]+\s*\:.*)');
+ final Match matches = kernelItemReg.firstMatch(pubspecContent);
+ if (matches == null) {
+ return null;
+ }
+ final String matchItem = matches.group(0);
+ final RegExp kernelRefReg = RegExp(r'ref\s*\:\s*[0-9a-z]+');
+ return kernelRefReg.firstMatch(matchItem).group(0).split(':')[1].trim();
+ }
+
+ static Future<void> generateAspectdSnapshot(
+ String aspectdSnapshotPath) async {
+ final Directory snapshotDir = fs.file(aspectdSnapshotPath).parent;
+ if (!snapshotDir.existsSync()) {
+ fs.directory(snapshotDir).createSync(recursive: true);
+ }
+ final File pubspecLockFile =
+ fs.file(fs.path.join(snapshotDir.parent.path, 'pubspec.lock'));
+ if (pubspecLockFile.existsSync()) {
+ pubspecLockFile.deleteSync();
+ }
+ await processManager.run(
+ <String>[sdkBinaryName('pub'), 'get', '--verbosity=warning'],
+ workingDirectory: snapshotDir.parent.path,
+ environment: <String, String>{'FLUTTER_ROOT': Cache.flutterRoot});
+ if (pubspecLockFile.existsSync()) {
+ await processManager.run(<String>[
+ sdkBinaryName('dart'),
+ '--snapshot=snapshot/aspectd.dart.snapshot',
+ 'tool/starter.dart'
+ ], workingDirectory: snapshotDir.parent.path);
+ }
+ }
+
+ static Future<bool> isAspectdEnabled() async {
+ final Directory aspectdDirectory = getAspectdDirectory(fs.currentDirectory);
+ if (!(aspectdDirectory.existsSync() &&
+ fs.file(fs.path.join(aspectdDirectory.path, 'pubspec.yaml')).existsSync() &&
+ fs.file(fs.path.join(aspectdDirectory.path, PackageMap.globalPackagesPath))
+ .existsSync() &&
+ fs.file(fs.path.join(aspectdDirectory.path, 'lib', aspectdImplPackageName + '.dart'))
+ .existsSync())) {
+ return false;
+ }
+ await checkAspectdTransformerSnapshot();
+ if (aspectdTransformerSnapshot == null ||
+ !fs.file(aspectdTransformerSnapshot).existsSync()) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ Future<FlutterCommandResult> runBuildBundleDillCommand(
+ FlutterCommand flutterCommand) async {
+ final ArgResults argResults = flutterCommand.argResults;
+ final String targetPlatform = argResults['target-platform'] as String;
+ final TargetPlatform platform = getTargetPlatformForName(targetPlatform);
+ if (platform == null) {
+ throwToolExit('Unknown platform: $targetPlatform');
+ }
+
+ final BuildMode buildMode = flutterCommand.getBuildMode();
+ final Directory mainDirectory = fs.currentDirectory;
+ final Directory aspectdDirectory = getAspectdDirectory(fs.currentDirectory);
+ final String origAssetsDir = argResults['asset-dir'] as String;
+ final String originKernelBlob = fs.path.join(origAssetsDir, 'kernel_blob.bin');
+ if (!fs.file(originKernelBlob).existsSync()) {
+ return null;
+ }
+
+ final String assetsDir = origAssetsDir
+ .replaceAll(mainDirectory.path, aspectdDirectory.path);
+ final String assetKernelBlob = fs.path.join(assetsDir, 'kernel_blob.bin');
+ final String mainPath = fs.path.join(aspectdDirectory.path, 'lib', aspectdImplPackageName + '.dart');
+ final String aspectdTransformedKernelFilePath =
+ fs.path.join(assetsDir, 'app.aspectd.dill');
+ fs.currentDirectory = aspectdDirectory;
+
+ await BundleBuilder().build(
+ platform: platform,
+ buildMode: buildMode,
+ mainPath: mainPath,
+ manifestPath: argResults['manifest'] as String,
+ depfilePath: argResults['depfile'] as String,
+ privateKeyPath: argResults['private-key'] as String,
+ assetDirPath: assetsDir,
+ precompiledSnapshot: argResults['precompiled'] as bool,
+ reportLicensedPackages: argResults['report-licensed-packages'] as bool,
+ trackWidgetCreation: argResults['track-widget-creation'] as bool,
+ extraFrontEndOptions: argResults[FlutterOptions.kExtraFrontEndOptions] as List<String>,
+ extraGenSnapshotOptions: argResults[FlutterOptions.kExtraGenSnapshotOptions] as List<String>,
+ fileSystemScheme: argResults['filesystem-scheme'] as String,
+ fileSystemRoots: argResults['filesystem-root'] as List<String>,
+ );
+
+ if (!fs.file(assetKernelBlob).existsSync()) {
+ fs.currentDirectory = mainDirectory;
+ return null;
+ }
+
+ final ProcessResult result = await transformDill(buildMode, assetKernelBlob, aspectdTransformedKernelFilePath);
+ if (result.exitCode != 0) {
+ fs.currentDirectory = mainDirectory;
+ throwToolExit('Transformer terminated unexpectedly.');
+ return null;
+ }
+
+ final File originKernelFile = fs.file(originKernelBlob);
+ if (originKernelFile.existsSync()) {
+ originKernelFile.deleteSync();
+ }
+
+ fs.file(aspectdTransformedKernelFilePath).copySync(originKernelBlob);
+ fs.currentDirectory = mainDirectory;
+ return null;
+ }
+
+ Future<FlutterCommandResult> runBuildAOTDillCommand(
+ TargetPlatform platform, String outputDir, BuildMode buildMode,
+ List<String> extraFrontEndOptions, List<String> dartDefines) async {
+ if (platform == null) {
+ throwToolExit('Unknown platform: $platform');
+ }
+
+ final Directory mainDirectory = fs.currentDirectory;
+ final Directory aspectdDirectory = getAspectdDirectory(mainDirectory);
+ String mainPath =
+ fs.path.join(aspectdDirectory.path, 'lib', aspectdImplPackageName + '.dart');
+ fs.currentDirectory = aspectdDirectory;
+ final AOTSnapshotter snapshotter = AOTSnapshotter();
+ final String outputPath =
+ (outputDir ?? getAotBuildDirectory())
+ .replaceAll(mainDirectory.path, aspectdDirectory.path);
+
+ // Compile to kernel.
+ mainPath = await snapshotter.compileKernel(
+ platform: platform,
+ buildMode: buildMode,
+ mainPath: mainPath,
+ packagesPath: PackageMap.globalPackagesPath,
+ trackWidgetCreation: false,
+ outputPath: outputPath,
+ extraFrontEndOptions: extraFrontEndOptions,
+ dartDefines: dartDefines
+ );
+
+ if (mainPath == null) {
+ fs.currentDirectory = mainDirectory;
+ throwToolExit('Compiler terminated unexpectedly.');
+ return null;
+ }
+
+ if (!mainPath.startsWith(fs.currentDirectory.path)) {
+ mainPath = fs.path.join(fs.currentDirectory.path, mainPath);
+ }
+
+ final String aspectdTransformedKernelFilePath = mainPath + '.aspectd.dill';
+ final String defaultKernelFilePath =
+ mainPath.replaceAll(aspectdDirectory.path, mainDirectory.path);
+
+ final ProcessResult result = await transformDill(buildMode, mainPath, aspectdTransformedKernelFilePath);
+ if (result.exitCode != 0) {
+ fs.currentDirectory = mainDirectory;
+ throwToolExit('Transformer terminated unexpectedly.');
+ return null;
+ }
+
+ final File defaultKernelFile = fs.file(defaultKernelFilePath);
+ if (defaultKernelFile.existsSync()) {
+ defaultKernelFile.deleteSync();
+ }
+ fs.file(aspectdTransformedKernelFilePath).copySync(defaultKernelFilePath);
+ fs.currentDirectory = mainDirectory;
+ return null;
+ }
+
+ Future<void> runKernelDillSnapshotCommand(
+ KernelSnapshot kernelSnapshot, Environment originalEnvironment,
+ String originalDill) async {
+ final Directory mainDirectory = fs.currentDirectory;
+ final Directory aspectdDirectory = getAspectdDirectory(mainDirectory);
+ fs.currentDirectory = aspectdDirectory;
+
+ final String outputDir =
+ (originalEnvironment.outputDir.absolute.path ?? getAotBuildDirectory())
+ .replaceAll(mainDirectory.path, aspectdDirectory.path);
+
+ final FlutterProject flutterProject = FlutterProject.current();
+ final Environment environment = Environment(
+ projectDir: flutterProject.directory,
+ outputDir: fs.directory(outputDir),
+ cacheDir: cache.getRoot(),
+ flutterRootDir: fs.directory(Cache.flutterRoot),
+ buildDir: flutterProject.directory
+ .childDirectory('.dart_tool')
+ .childDirectory('flutter_build'),
+ defines: Map<String, String>.from(originalEnvironment.defines)
+ );
+
+ final KernelCompiler compiler = await kernelCompilerFactory.create(
+ FlutterProject.fromDirectory(aspectdDirectory),
+ );
+ if (environment.defines[kBuildMode] == null) {
+ throw MissingDefineException(kBuildMode, 'kernel_snapshot');
+ }
+ if (environment.defines[kTargetPlatform] == null) {
+ throw MissingDefineException(kTargetPlatform, 'kernel_snapshot');
+ }
+ final BuildMode buildMode = getBuildModeForName(environment.defines[kBuildMode]);
+ final String targetFile = fs.path.join(aspectdDirectory.path, 'lib', aspectdImplPackageName + '.dart');
+ final String packagesPath = aspectdDirectory.childFile('.packages').path;
+ final String targetFileAbsolute = fs.file(targetFile).absolute.path;
+ // everything besides 'false' is considered to be enabled.
+ final bool trackWidgetCreation = environment.defines[kTrackWidgetCreation] != 'false';
+ final TargetPlatform targetPlatform = getTargetPlatformForName(environment.defines[kTargetPlatform]);
+
+ TargetModel targetModel = TargetModel.flutter;
+ if (targetPlatform == TargetPlatform.fuchsia_x64 ||
+ targetPlatform == TargetPlatform.fuchsia_arm64) {
+ targetModel = TargetModel.flutterRunner;
+ }
+
+ final CompilerOutput output = await compiler.compile(
+ sdkRoot: artifacts.getArtifactPath(
+ Artifact.flutterPatchedSdkPath,
+ platform: targetPlatform,
+ mode: buildMode,
+ ),
+ aot: buildMode != BuildMode.debug,
+ buildMode: buildMode,
+ trackWidgetCreation: trackWidgetCreation && buildMode == BuildMode.debug,
+ targetModel: targetModel,
+ outputFilePath: environment.buildDir.childFile('app.dill').path,
+ packagesPath: packagesPath,
+ linkPlatformKernelIn: buildMode != BuildMode.debug,
+ mainPath: targetFileAbsolute,
+ depFilePath: environment.buildDir.childFile('kernel_snapshot.d').path,
+ dartDefines: parseDartDefines(environment),
+ );
+
+ if (output == null || output.errorCount != 0) {
+ fs.currentDirectory = mainDirectory;
+ throwToolExit('Compiler terminated unexpectedly.');
+ return Future<void>((){});
+ }
+
+ final String aspectdTransformedKernelFilePath = output.outputFilename+'.aspectd.dill';
+ final ProcessResult result = await transformDill(buildMode, output.outputFilename, aspectdTransformedKernelFilePath);
+ if (result.exitCode != 0) {
+ fs.currentDirectory = mainDirectory;
+ throwToolExit('Transformer terminated unexpectedly.');
+ return Future<void>((){});
+ }
+
+ final File originalDillFile = fs.file(originalDill);
+ if (originalDillFile.existsSync()) {
+ originalDillFile.renameSync(originalDill+'.bak');
+ }
+ fs.file(aspectdTransformedKernelFilePath).copySync(originalDill);
+
+ fs.currentDirectory = mainDirectory;
+ }
+
+ Future<ProcessResult> transformDill(BuildMode buildMode, String inputDill, String outputDill) async {
+ final List<String> command = <String>[
+ artifacts.getArtifactPath(Artifact.engineDartBinary),
+ aspectdTransformerSnapshot,
+ '--input',
+ inputDill,
+ if (buildMode != BuildMode.release )...<String>['--sdk-root',fs.file(artifacts.getArtifactPath(Artifact.platformKernelDill)).parent
+ .path + fs.path.separator],
+ '--output',
+ outputDill
+ ];
+
+ return processManager.run(command);
+ }
+}
diff --git a/packages/flutter_tools/lib/src/build_system/targets/dart.dart b/packages/flutter_tools/lib/src/build_system/targets/dart.dart
index 8472cf573..ba315488a 100644
--- a/packages/flutter_tools/lib/src/build_system/targets/dart.dart
+++ b/packages/flutter_tools/lib/src/build_system/targets/dart.dart
@@ -3,6 +3,7 @@
// found in the LICENSE file.
import '../../artifacts.dart';
+import '../../aspectd.dart';
import '../../base/build.dart';
import '../../base/file_system.dart';
import '../../build_info.dart';
@@ -235,6 +236,10 @@ class KernelSnapshot extends Target {
);
if (output == null || output.errorCount != 0) {
throw Exception('Errors during snapshot creation: $output');
+ } else {
+ if (await AspectdHook.isAspectdEnabled()) {
+ await AspectdHook().runKernelDillSnapshotCommand(this, environment, output.outputFilename);
+ }
}
}
}
diff --git a/packages/flutter_tools/lib/src/commands/build_bundle.dart b/packages/flutter_tools/lib/src/commands/build_bundle.dart
index 582cd4300..5b19d8129 100644
--- a/packages/flutter_tools/lib/src/commands/build_bundle.dart
+++ b/packages/flutter_tools/lib/src/commands/build_bundle.dart
@@ -4,6 +4,7 @@
import 'dart:async';
+import '../aspectd.dart';
import '../base/common.dart';
import '../build_info.dart';
import '../bundle.dart';
@@ -139,6 +140,11 @@ class BuildBundleCommand extends BuildSubCommand {
fileSystemScheme: stringArg('filesystem-scheme'),
fileSystemRoots: stringsArg('filesystem-root'),
);
+
+ if (await AspectdHook.isAspectdEnabled()) {
+ await AspectdHook().runBuildBundleDillCommand(this);
+ }
+
return FlutterCommandResult.success();
}
}
diff --git a/packages/flutter_tools/lib/src/compile.dart b/packages/flutter_tools/lib/src/compile.dart
index c00182118..21be733e4 100644
--- a/packages/flutter_tools/lib/src/compile.dart
+++ b/packages/flutter_tools/lib/src/compile.dart
@@ -254,12 +254,14 @@ List<String> _buildModeOptions(BuildMode mode) {
'-Ddart.vm.profile=true',
'-Ddart.vm.product=false',
'--bytecode-options=source-positions',
+ '--no-drop-ast',
];
case BuildMode.release:
return <String>[
'-Ddart.vm.profile=false',
'-Ddart.vm.product=true',
'--bytecode-options=source-positions',
+ '--no-drop-ast',
];
}
throw Exception('Unknown BuildMode: $mode');
--
2.21.1 (Apple Git-122.3)