Skip to content

Commit 319ffb4

Browse files
authored
[flutter_tools] Add flutter assemble support for tool extension assemble targets (flutter#194563)
Integrates tool extension assemble targets (`ExtensionAssembleTarget`) into `flutter assemble`. Part of flutter#190755 Part of flutter#190752 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent f250e93 commit 319ffb4

8 files changed

Lines changed: 1847 additions & 194 deletions

File tree

‎packages/flutter_tools/lib/executable.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ List<FlutterCommand> generateCommands({
256256
),
257257
AssembleCommand(
258258
buildSystem: toolDependencies.buildSystem,
259+
extensionBuildManager: toolDependencies.extensionBuildManager,
259260
featureFlags: toolDependencies.featureFlags,
260261
toolContext: toolContext,
261262
verboseHelp: verboseHelp,

‎packages/flutter_tools/lib/src/build_system/exceptions.dart‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,22 @@ class MissingDefineException implements Exception {
9494
'but it was not provided';
9595
}
9696
}
97+
98+
/// An exception thrown when an extension-defined assemble target fails.
99+
class ExtensionAssembleException implements Exception {
100+
const ExtensionAssembleException(this.target, {this.errorMessage});
101+
102+
/// The name of the assemble target that failed.
103+
final String target;
104+
105+
/// The optional failure message returned by the tool extension.
106+
final String? errorMessage;
107+
108+
@override
109+
String toString() {
110+
final String? message = errorMessage;
111+
return message != null && message.isNotEmpty
112+
? 'Extension assemble target "$target" failed: $message'
113+
: 'Extension assemble target "$target" failed.';
114+
}
115+
}

‎packages/flutter_tools/lib/src/build_system/targets/extension.dart‎

Lines changed: 275 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,147 @@ import '../../artifacts.dart';
88
import '../../base/common.dart';
99
import '../../base/file_system.dart';
1010
import '../../build_info.dart';
11+
import '../../experimental/extension_build_manager.dart';
12+
import '../build_system.dart';
13+
import '../exceptions.dart';
14+
15+
/// Resolves a build target dependency by its [dependency] name.
16+
typedef DependencyResolverCallback = Target Function(String dependency);
17+
18+
/// A `flutter assemble` target that delegates to a tool extension.
19+
class ExtensionTarget extends Target {
20+
ExtensionTarget({
21+
required this.assembleTarget,
22+
required this._buildManager,
23+
required this._dependencyResolver,
24+
});
25+
26+
/// The extension-defined assemble target.
27+
final ExtensionAssembleTarget assembleTarget;
28+
29+
final ExtensionBuildManager _buildManager;
30+
final DependencyResolverCallback _dependencyResolver;
31+
32+
@override
33+
String get name => assembleTarget.name;
34+
35+
@override
36+
late final List<Target> dependencies = assembleTarget.dependencies
37+
.map(_dependencyResolver)
38+
.toList();
39+
40+
late final TargetPlatform? _defaultTargetPlatform = _tryResolveTargetPlatform(
41+
assembleTarget.targetPlatform,
42+
);
43+
44+
/// Static input sources for this target.
45+
///
46+
/// [resolveInputs] resolves the concrete input files at build time by
47+
/// supplying [BuildMode] from the build [Environment] to mode-dependent
48+
/// artifacts.
49+
@override
50+
late final List<Source> inputs = assembleTarget.inputs
51+
.map(
52+
(ExtensionSource s) => _toHostSource(
53+
s,
54+
defaultTargetPlatform: _defaultTargetPlatform,
55+
targetPlatformName: assembleTarget.targetPlatform,
56+
),
57+
)
58+
.toList();
59+
60+
/// Static output sources for this target.
61+
///
62+
/// [resolveOutputs] resolves the concrete output files at build time by
63+
/// supplying [BuildMode] from the build [Environment] to mode-dependent
64+
/// artifacts.
65+
@override
66+
late final List<Source> outputs = assembleTarget.outputs
67+
.map(
68+
(ExtensionSource s) => _toHostSource(
69+
s,
70+
defaultTargetPlatform: _defaultTargetPlatform,
71+
targetPlatformName: assembleTarget.targetPlatform,
72+
),
73+
)
74+
.toList();
75+
76+
BuildMode? _resolveEnvironmentBuildMode(Environment environment) {
77+
final String? buildMode = environment.defines[kBuildMode];
78+
return buildMode != null ? BuildMode.fromCliName(buildMode) : null;
79+
}
80+
81+
@override
82+
ResolvedFiles resolveInputs(Environment environment) =>
83+
_resolveSources(assembleTarget.inputs, environment, inputs: true);
84+
85+
@override
86+
ResolvedFiles resolveOutputs(Environment environment) =>
87+
_resolveSources(assembleTarget.outputs, environment, inputs: false);
88+
89+
ResolvedFiles _resolveSources(
90+
List<ExtensionSource> sources,
91+
Environment environment, {
92+
required bool inputs,
93+
}) {
94+
final BuildMode? mode = _resolveEnvironmentBuildMode(environment);
95+
final List<Source> resolvedSources = sources
96+
.map(
97+
(ExtensionSource s) => _toHostSource(
98+
s,
99+
defaultBuildMode: mode,
100+
defaultTargetPlatform: _defaultTargetPlatform,
101+
targetPlatformName: assembleTarget.targetPlatform,
102+
),
103+
)
104+
.toList();
105+
final collector = SourceVisitor(environment, inputs);
106+
for (final source in resolvedSources) {
107+
source.accept(collector);
108+
}
109+
depfiles.forEach(collector.visitDepfile);
110+
return collector;
111+
}
112+
113+
@override
114+
Future<void> build(Environment environment) async {
115+
final Environment(
116+
:Artifacts artifacts,
117+
:Directory buildDir,
118+
:Map<String, String> defines,
119+
:FileSystem fileSystem,
120+
:Directory outputDir,
121+
:Directory projectDir,
122+
) = environment;
123+
final String mainPath = defines[kTargetFile] ?? fileSystem.path.join('lib', 'main.dart');
124+
final String? buildMode = defines[kBuildMode];
125+
if (buildMode == null) {
126+
throw MissingDefineException(kBuildMode, name);
127+
}
128+
final mode = BuildMode.fromCliName(buildMode);
129+
130+
final Map<String, String> resolvedArtifacts = resolveExtensionAssembleTargetArtifacts(
131+
artifacts: artifacts,
132+
buildMode: mode,
133+
target: assembleTarget,
134+
);
135+
136+
final ExtensionBuildResult result = await _buildManager.assemble(
137+
buildDir: buildDir.uri,
138+
buildMode: mode,
139+
flutterRoot: environment.flutterRootDir.uri,
140+
mainPath: mainPath,
141+
outputDir: outputDir.uri,
142+
projectRoot: projectDir.uri,
143+
resolvedArtifacts: resolvedArtifacts,
144+
targetName: assembleTarget.name,
145+
);
146+
147+
if (!result.success) {
148+
throw ExtensionAssembleException(assembleTarget.name, errorMessage: result.errorMessage);
149+
}
150+
}
151+
}
11152

12153
final _placeholderRegex = RegExp(r'\{[A-Z_]+\}');
13154

@@ -50,10 +191,19 @@ Directory resolveExtensionOutputDir({
50191
return fileSystem.directory(fileSystem.path.normalize(outputPath));
51192
}
52193

194+
/// Engine artifacts whose cached path does not depend on the target platform,
195+
/// so they can be resolved for out-of-tree target platforms.
196+
const _targetPlatformIndependentArtifacts = <ExtensionArtifact>{
197+
BuiltInArtifacts.flutterPatchedSdkPath,
198+
BuiltInArtifacts.icuData,
199+
};
200+
53201
/// Resolves all engine and host artifacts declared on [target] using
54202
/// [artifacts] and [buildMode].
55203
///
56-
/// Throws a [ToolExit] if any requested artifact name is invalid or unsupported.
204+
/// Throws a [ToolExit] if any requested artifact name is invalid or unsupported,
205+
/// or if a target-platform-specific artifact is requested on an out-of-tree
206+
/// target platform.
57207
// TODO(bkonyi): Download required artifacts on demand if not already cached.
58208
Map<String, String> resolveExtensionBuildTargetArtifacts({
59209
required Artifacts artifacts,
@@ -63,8 +213,15 @@ Map<String, String> resolveExtensionBuildTargetArtifacts({
63213
final TargetPlatform? targetPlatform = _tryResolveTargetPlatform(target.targetPlatform);
64214
final resolvedArtifacts = <String, String>{};
65215
for (final ExtensionArtifact artifact in target.artifacts) {
216+
final Artifact resolvedArtifact = _resolveArtifact(artifact);
217+
if (targetPlatform == null && !_targetPlatformIndependentArtifacts.contains(artifact)) {
218+
throwToolExit(
219+
'Artifact "${artifact.name}" requires a built-in target platform, '
220+
'but build target "${target.name}" targets out-of-tree platform "${target.targetPlatform}".',
221+
);
222+
}
66223
resolvedArtifacts[artifact.name] = artifacts.getArtifactPath(
67-
_resolveArtifact(artifact),
224+
resolvedArtifact,
68225
platform: targetPlatform,
69226
mode: buildMode,
70227
);
@@ -76,18 +233,133 @@ Map<String, String> resolveExtensionBuildTargetArtifacts({
76233
return resolvedArtifacts;
77234
}
78235

236+
/// Resolves all engine and host artifact inputs declared on [target] using
237+
/// [artifacts] and [buildMode].
238+
///
239+
/// Throws a [ToolExit] if any requested artifact name or explicit artifact
240+
/// target platform is invalid or unsupported, if a target-platform-specific
241+
/// artifact resolves to a `null` target platform, or if the same artifact
242+
/// resolves to different paths across multiple target platforms or build modes.
243+
// TODO(bkonyi): Download required artifacts on demand if not already cached.
244+
Map<String, String> resolveExtensionAssembleTargetArtifacts({
245+
required Artifacts artifacts,
246+
required BuildMode buildMode,
247+
required ExtensionAssembleTarget target,
248+
}) {
249+
final TargetPlatform? defaultTargetPlatform = _tryResolveTargetPlatform(target.targetPlatform);
250+
final resolvedArtifacts = <String, String>{};
251+
for (final ExtensionSource input in target.inputs) {
252+
switch (input) {
253+
case ExtensionPatternSource():
254+
continue;
255+
case ExtensionArtifactSource(
256+
:final ExtensionArtifact artifact,
257+
:final String? platform,
258+
:final BuildMode? mode,
259+
):
260+
final Artifact resolvedArtifact = _resolveArtifact(artifact);
261+
final TargetPlatform? resolvedPlatform = _resolveArtifactTargetPlatform(
262+
artifact: artifact,
263+
defaultTargetPlatform: defaultTargetPlatform,
264+
sourcePlatform: platform,
265+
targetPlatformName: target.targetPlatform,
266+
);
267+
final String path = artifacts.getArtifactPath(
268+
resolvedArtifact,
269+
platform: resolvedPlatform,
270+
mode: mode ?? buildMode,
271+
);
272+
final String? existingPath = resolvedArtifacts[artifact.name];
273+
if (existingPath != null && existingPath != path) {
274+
throwToolExit(
275+
'Assemble target "${target.name}" requested artifact "${artifact.name}" '
276+
'for multiple target platforms or build modes.',
277+
);
278+
}
279+
resolvedArtifacts[artifact.name] = path;
280+
case ExtensionHostArtifactSource(:final ExtensionHostArtifact artifact):
281+
final HostArtifact hostArtifact = _resolveHostArtifact(artifact);
282+
resolvedArtifacts[artifact.name] = artifacts.getHostArtifact(hostArtifact).path;
283+
}
284+
}
285+
return resolvedArtifacts;
286+
}
287+
288+
TargetPlatform? _resolveArtifactTargetPlatform({
289+
required ExtensionArtifact artifact,
290+
required TargetPlatform? defaultTargetPlatform,
291+
required String? sourcePlatform,
292+
required String targetPlatformName,
293+
}) {
294+
final TargetPlatform? resolvedPlatform;
295+
if (sourcePlatform != null) {
296+
resolvedPlatform = _tryResolveTargetPlatform(sourcePlatform);
297+
if (resolvedPlatform == null) {
298+
throwToolExit(
299+
'Unsupported target platform "$sourcePlatform" for artifact "${artifact.name}".',
300+
);
301+
}
302+
} else {
303+
resolvedPlatform = defaultTargetPlatform;
304+
}
305+
if (resolvedPlatform == null && !_targetPlatformIndependentArtifacts.contains(artifact)) {
306+
throwToolExit(
307+
'Artifact "${artifact.name}" requires a built-in target platform; specify a built-in '
308+
'"platform" on the artifact source when targeting out-of-tree platform '
309+
'"$targetPlatformName".',
310+
);
311+
}
312+
return resolvedPlatform;
313+
}
314+
79315
TargetPlatform? _tryResolveTargetPlatform(String platformName) {
80316
if (platformName.isEmpty) {
81317
return null;
82318
}
83319
try {
84320
return TargetPlatform.fromName(platformName);
85321
} on Exception {
86-
// Custom extension target platforms do not map to a built-in TargetPlatform.
322+
// Out-of-tree extension target platforms don't map to a built-in
323+
// TargetPlatform, so target-independent artifacts resolve with
324+
// platform: null. Target-specific engine artifacts such as genSnapshot
325+
// require an explicit built-in platform on the artifact source until
326+
// extensions can provide their own artifacts.
327+
// TODO(bkonyi): Support custom extension-provided artifact resolution for
328+
// out-of-tree target platforms via ExtensionArtifactManager.
87329
return null;
88330
}
89331
}
90332

333+
Source _toHostSource(
334+
ExtensionSource source, {
335+
required TargetPlatform? defaultTargetPlatform,
336+
required String targetPlatformName,
337+
BuildMode? defaultBuildMode,
338+
}) => switch (source) {
339+
ExtensionPatternSource(:final String value, :final bool optional) => Source.pattern(
340+
value,
341+
optional: optional,
342+
),
343+
ExtensionArtifactSource(
344+
:final ExtensionArtifact artifact,
345+
:final String? platform,
346+
:final BuildMode? mode,
347+
) =>
348+
Source.artifact(
349+
_resolveArtifact(artifact),
350+
platform: _resolveArtifactTargetPlatform(
351+
artifact: artifact,
352+
defaultTargetPlatform: defaultTargetPlatform,
353+
sourcePlatform: platform,
354+
targetPlatformName: targetPlatformName,
355+
),
356+
mode: mode ?? defaultBuildMode,
357+
),
358+
ExtensionHostArtifactSource(:final ExtensionHostArtifact artifact) => Source.hostArtifact(
359+
_resolveHostArtifact(artifact),
360+
),
361+
};
362+
91363
Artifact _resolveArtifact(ExtensionArtifact artifact) => switch (artifact.name) {
92364
'flutterPatchedSdkPath' => Artifact.flutterPatchedSdkPath,
93365
'genSnapshot' => Artifact.genSnapshot,

0 commit comments

Comments
 (0)