@@ -8,6 +8,147 @@ import '../../artifacts.dart';
88import '../../base/common.dart' ;
99import '../../base/file_system.dart' ;
1010import '../../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
12153final _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.
58208Map <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+
79315TargetPlatform ? _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+
91363Artifact _resolveArtifact (ExtensionArtifact artifact) => switch (artifact.name) {
92364 'flutterPatchedSdkPath' => Artifact .flutterPatchedSdkPath,
93365 'genSnapshot' => Artifact .genSnapshot,
0 commit comments