Skip to content

Commit abdafc7

Browse files
bkonyisuryaKommana2662
authored andcommitted
[flutter_tools] Define ExtensionAssembleTarget and AssembleTarget in extension protocol (flutter#193984)
Defines `ExtensionAssembleTarget` and `AssembleTarget` in the extension protocol, separating hermetic `flutter assemble` DAG targets from top-level `flutter build` subcommand targets. ### Stack 1. **flutter#193984 (this PR)** 2. flutter#194563 ([diff](bkonyi/flutter@ft-ext-step07b-build-remaining-slice...ft-ext-step07c-assemble-host-integration)) 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 8150ebe commit abdafc7

11 files changed

Lines changed: 2318 additions & 227 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_tools_core/flutter_tools_core.dart' hide BuildMode;
6+
7+
import '../../artifacts.dart';
8+
import '../../base/common.dart';
9+
import '../../base/file_system.dart';
10+
import '../../build_info.dart';
11+
12+
final _placeholderRegex = RegExp(r'\{[A-Z_]+\}');
13+
14+
const _supportedOutputDirPlaceholders = <String>{
15+
BuiltInPlaceholders.outputDir,
16+
BuiltInPlaceholders.projectDir,
17+
BuiltInPlaceholders.buildMode,
18+
BuiltInPlaceholders.targetPlatform,
19+
};
20+
21+
/// Resolves the output directory for [target] by substituting placeholders
22+
/// (`{OUTPUT_DIR}`, `{PROJECT_DIR}`, `{BUILD_MODE}`, and `{TARGET_PLATFORM}`).
23+
///
24+
/// Relative paths are resolved relative to [projectDir].
25+
/// Throws a [ToolExit] if [ExtensionBuildTarget.outputDir] contains an
26+
/// unsupported `{...}` placeholder.
27+
Directory resolveExtensionOutputDir({
28+
required BuildMode buildMode,
29+
required Directory buildOutputDir,
30+
required FileSystem fileSystem,
31+
required Directory projectDir,
32+
required ExtensionBuildTarget target,
33+
}) {
34+
for (final RegExpMatch match in _placeholderRegex.allMatches(target.outputDir)) {
35+
final String? placeholder = match[0];
36+
if (!_supportedOutputDirPlaceholders.contains(placeholder)) {
37+
throwToolExit(
38+
'Unsupported placeholder "$placeholder" in outputDir "${target.outputDir}" for build target "${target.name}".',
39+
);
40+
}
41+
}
42+
String outputPath = target.outputDir
43+
.replaceAll(BuiltInPlaceholders.outputDir, buildOutputDir.path)
44+
.replaceAll(BuiltInPlaceholders.projectDir, projectDir.path)
45+
.replaceAll(BuiltInPlaceholders.buildMode, buildMode.cliName)
46+
.replaceAll(BuiltInPlaceholders.targetPlatform, target.targetPlatform);
47+
if (fileSystem.path.isRelative(outputPath)) {
48+
outputPath = fileSystem.path.join(projectDir.path, outputPath);
49+
}
50+
return fileSystem.directory(fileSystem.path.normalize(outputPath));
51+
}
52+
53+
/// Resolves all engine and host artifacts declared on [target] using
54+
/// [artifacts] and [buildMode].
55+
///
56+
/// Throws a [ToolExit] if any requested artifact name is invalid or unsupported.
57+
// TODO(bkonyi): Download required artifacts on demand if not already cached.
58+
Map<String, String> resolveExtensionBuildTargetArtifacts({
59+
required Artifacts artifacts,
60+
required BuildMode buildMode,
61+
required ExtensionBuildTarget target,
62+
}) {
63+
final TargetPlatform? targetPlatform = _tryResolveTargetPlatform(target.targetPlatform);
64+
final resolvedArtifacts = <String, String>{};
65+
for (final ExtensionArtifact artifact in target.artifacts) {
66+
resolvedArtifacts[artifact.name] = artifacts.getArtifactPath(
67+
_resolveArtifact(artifact),
68+
platform: targetPlatform,
69+
mode: buildMode,
70+
);
71+
}
72+
for (final ExtensionHostArtifact artifact in target.hostArtifacts) {
73+
final HostArtifact hostArtifact = _resolveHostArtifact(artifact);
74+
resolvedArtifacts[artifact.name] = artifacts.getHostArtifact(hostArtifact).path;
75+
}
76+
return resolvedArtifacts;
77+
}
78+
79+
TargetPlatform? _tryResolveTargetPlatform(String platformName) {
80+
if (platformName.isEmpty) {
81+
return null;
82+
}
83+
try {
84+
return TargetPlatform.fromName(platformName);
85+
} on Exception {
86+
// Custom extension target platforms do not map to a built-in TargetPlatform.
87+
return null;
88+
}
89+
}
90+
91+
Artifact _resolveArtifact(ExtensionArtifact artifact) => switch (artifact.name) {
92+
'flutterPatchedSdkPath' => Artifact.flutterPatchedSdkPath,
93+
'genSnapshot' => Artifact.genSnapshot,
94+
'icuData' => Artifact.icuData,
95+
'linuxDesktopPath' => Artifact.linuxDesktopPath,
96+
'linuxHeaders' => Artifact.linuxHeaders,
97+
final String name => throwToolExit('Unsupported artifact "$name" requested by a tool extension.'),
98+
};
99+
100+
HostArtifact _resolveHostArtifact(ExtensionHostArtifact artifact) => switch (artifact.name) {
101+
'impellerc' => HostArtifact.impellerc,
102+
'libtessellator' => HostArtifact.libtessellator,
103+
final String name => throwToolExit(
104+
'Unsupported host artifact "$name" requested by a tool extension.',
105+
),
106+
};

‎packages/flutter_tools/lib/src/commands/build.dart‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import '../android/android_builder.dart';
1010
import '../android/gradle.dart';
1111
import '../artifacts.dart';
1212
import '../base/common.dart' show throwToolExit;
13+
import '../base/config.dart';
1314
import '../base/file_system.dart';
1415
import '../base/logger.dart';
1516
import '../base/platform.dart';
1617
import '../base/template.dart';
1718
import '../build_info.dart';
1819
import '../build_system/build_system.dart';
20+
import '../build_system/targets/extension.dart';
1921
import '../cache.dart';
2022
import '../context/android_context.dart';
2123
import '../context/apple_context.dart';
@@ -305,12 +307,29 @@ class ExtensionBuildSubCommand extends BuildSubCommand {
305307

306308
@override
307309
Future<FlutterCommandResult> runCommand() async {
310+
final ToolContext(:Artifacts artifacts, :Config config, :FileSystem fs) = toolContext;
311+
final Directory projectDir = fs.currentDirectory;
308312
final BuildInfo buildInfo = await getBuildInfo();
313+
final Directory outputDir = resolveExtensionOutputDir(
314+
buildMode: buildInfo.mode,
315+
buildOutputDir: projectDir.childDirectory(getBuildDirectory(config, fs)),
316+
fileSystem: fs,
317+
projectDir: projectDir,
318+
target: target,
319+
);
320+
final Map<String, String> resolvedArtifacts = resolveExtensionBuildTargetArtifacts(
321+
artifacts: artifacts,
322+
buildMode: buildInfo.mode,
323+
target: target,
324+
);
309325

310326
final ExtensionBuildResult result = await _buildManager.build(
311327
buildMode: buildInfo.mode,
328+
flutterRoot: fs.directory(Cache.flutterRoot).uri,
312329
mainPath: targetFile,
313-
projectRoot: toolContext.fs.currentDirectory.uri,
330+
outputDir: outputDir.uri,
331+
projectRoot: projectDir.uri,
332+
resolvedArtifacts: resolvedArtifacts,
314333
targetName: target.name,
315334
);
316335

‎packages/flutter_tools/lib/src/experimental/extension_build_manager.dart‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ base class ExtensionBuildManager {
6262
);
6363
return (connection, ExtensionBuildTarget.listFromJson(result));
6464
} on Object catch (e) {
65-
_logger.printTrace('Failed to query build targets from extension: $e');
65+
_logger.printWarning('Failed to query build targets from extension: $e');
6666
return (connection, const <ExtensionBuildTarget>[]);
6767
}
6868
}),
@@ -88,16 +88,25 @@ base class ExtensionBuildManager {
8888
return targets;
8989
}
9090

91-
/// Triggers a custom build for the given [targetName] by routing to the active extension.
91+
/// Triggers a custom `flutter build` target for the given [targetName] by routing to the active extension.
9292
Future<ExtensionBuildResult> build({
9393
required BuildMode buildMode,
94+
required Uri flutterRoot,
9495
required String mainPath,
96+
required Uri outputDir,
9597
required Uri projectRoot,
98+
required Map<String, String> resolvedArtifacts,
9699
required String targetName,
97100
}) async {
98101
if (!projectRoot.hasAbsolutePath) {
99102
throw ArgumentError.value(projectRoot, 'projectRoot', 'Must be an absolute path.');
100103
}
104+
if (!outputDir.hasAbsolutePath) {
105+
throw ArgumentError.value(outputDir, 'outputDir', 'Must be an absolute path.');
106+
}
107+
if (!flutterRoot.hasAbsolutePath) {
108+
throw ArgumentError.value(flutterRoot, 'flutterRoot', 'Must be an absolute path.');
109+
}
101110
if (!_featureFlags.isToolExtensionsEnabled) {
102111
return const ExtensionBuildResult(
103112
success: false,
@@ -132,8 +141,11 @@ base class ExtensionBuildManager {
132141
BuildService.buildMethod,
133142
params: <String, Object?>{
134143
BuildService.buildModeParam: buildMode.cliName,
144+
BuildService.flutterRootParam: flutterRoot.toString(),
135145
BuildService.mainPathParam: mainPath,
146+
BuildService.outputDirParam: outputDir.toString(),
136147
BuildService.projectRootParam: projectRoot.toString(),
148+
BuildService.resolvedArtifactsParam: resolvedArtifacts,
137149
BuildService.targetNameParam: targetName,
138150
},
139151
// TODO(bkonyi): Support heartbeat / liveness monitoring for long-running RPCs,

0 commit comments

Comments
 (0)