Repository navigation
Expand file tree
/
Copy pathpatch.diff
More file actions
103 lines (101 loc) · 4.16 KB
/
Copy pathpatch.diff
File metadata and controls
103 lines (101 loc) · 4.16 KB
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
diff --git a/packages/flutter_tools/lib/src/build_system/targets/common.dart b/packages/flutter_tools/lib/src/build_system/targets/common.dart
index 692bc456838..b60e4788116 100644
--- a/packages/flutter_tools/lib/src/build_system/targets/common.dart
+++ b/packages/flutter_tools/lib/src/build_system/targets/common.dart
@@ -148,6 +148,7 @@ class KernelSnapshot extends Target {
Source.pattern(
'{FLUTTER_ROOT}/packages/flutter_tools/lib/src/build_system/targets/common.dart',
),
+ Source.pattern('{FLUTTER_ROOT}/$flutterShieldEntrypoint'),
Source.artifact(Artifact.platformKernelDill),
Source.artifact(Artifact.engineDartBinary),
Source.artifact(Artifact.engineDartAotRuntime),
@@ -175,6 +176,19 @@ class KernelSnapshot extends Target {
static const dillName = 'app.dill';
static const recordedUsesFileName = 'recorded_uses.json';
+ /// The FlutterShield tool entrypoint, relative to the flutter root.
+ ///
+ /// It is invoked with `dart run`, so it does not need to be compiled ahead
+ /// of time.
+ static const flutterShieldEntrypoint = '../bin/flutter_shield.dart';
+
+ /// The name FlutterShield writes to, before it replaces [dillName].
+ static const hardenedDillName = 'app.hardened.dill';
+
+ /// Dart defines with this prefix configure FlutterShield; it is skipped
+ /// entirely when a build passes none of them.
+ static const flutterShieldDefinePrefix = 'FS_';
+
/// The content of an empty recorded uses file.
///
/// We always write this file even if no resources are recorded to prevent
@@ -314,6 +328,70 @@ class KernelSnapshot extends Target {
if (output == null || output.errorCount != 0) {
throw Exception();
}
+
+ if (buildMode == BuildMode.release) {
+ await _runFlutterShield(environment, dillPath, dartDefines);
+ }
+ }
+
+ /// Runs the FlutterShield tool over the kernel snapshot at [dillPath],
+ /// replacing it with the processed one.
+ ///
+ /// The tool is invoked with the input dill as the first argument and the
+ /// destination of the processed dill as the second, and is expected to write
+ /// the latter. It is run here rather than as a separate [Target] so that
+ /// every consumer of the snapshot sees the processed dill, whichever AOT
+ /// target the platform uses.
+ ///
+ /// It is configured through the `FS_`-prefixed entries of [dartDefines],
+ /// which are handed to it as environment variables; dart defines themselves
+ /// only reach the compiled program, not the processes of the build.
+ Future<void> _runFlutterShield(
+ Environment environment,
+ String dillPath,
+ List<String> dartDefines,
+ ) async {
+ final shieldEnvironment = <String, String>{};
+ for (final String define in dartDefines) {
+ if (!define.startsWith(flutterShieldDefinePrefix)) {
+ continue;
+ }
+ final int separator = define.indexOf('=');
+ if (separator > 0) {
+ shieldEnvironment[define.substring(0, separator)] = define.substring(separator + 1);
+ }
+ }
+ if (shieldEnvironment.isEmpty) {
+ return;
+ }
+
+ final File inputDill = environment.fileSystem.file(dillPath);
+ final File hardenedDill = environment.buildDir.childFile(hardenedDillName);
+
+ final command = <String>[
+ environment.artifacts.getArtifactPath(Artifact.engineDartBinary),
+ 'run',
+ environment.fileSystem.path.normalize(
+ environment.fileSystem.path.join(environment.flutterRootDir.path, flutterShieldEntrypoint),
+ ),
+ inputDill.path,
+ hardenedDill.path,
+ ];
+ environment.logger.printTrace('Running FlutterShield: ${command.join(' ')}');
+
+ final ProcessResult result = await environment.processManager.run(
+ command,
+ environment: shieldEnvironment,
+ );
+ environment.logger.printTrace(result.stdout.toString().trim());
+ if (result.exitCode != 0) {
+ throw Exception('FlutterShield exited with code ${result.exitCode}.\n${result.stderr}');
+ }
+ if (!hardenedDill.existsSync()) {
+ throw Exception('FlutterShield did not write the processed dill to ${hardenedDill.path}.');
+ }
+
+ hardenedDill.renameSync(inputDill.path);
}
Future<void> _addFlavorToDartDefines(