Skip to content

Commit 3a83e43

Browse files
authored
Make flutter_tools use newest package:{native_assets_builder,native_assets_cli,native_toolchain_c} (#158214)
Almost all of the code is just adopting to changes to the APIs of `package:native_assets_builder`, `package:native_assets_cli` and `package:native_toolchain_c` There's only two semantic changes * Removes a test that checks for a verification error if a build hook produces a static library if the preferred linking mode is dynamic: => The test is written in a very hacky way. By monkey patching the build config.json that flutter build actually made. This monkey patching relies on package:cli_config which is now no longer used. => The actual code that checks for this mismatch lives in dart-lang/native repository and is tested there. So there's really no need to duplicate that. * The `package:native_assets_builder` no longer knows about code assets. This is something a user of that package (e.g. flutter tools) adds. Now the dry-run functionality will invoke build hooks who produce code assets without an architecture. => The `package:native_assets_builder` used to expand such a code asset to N different code assets (one for each supported architecture) => This logic was now moved to flutter tools. => In the near future we're going to this dry-run complexity, which will then also get rid of this uglyness (of expanding to all archs of an OS).
1 parent 22a7afd commit 3a83e43

24 files changed

Lines changed: 519 additions & 449 deletions

dev/integration_tests/link_hook/hook/build.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
// found in the LICENSE file.
44

55
import 'package:logging/logging.dart';
6-
import 'package:native_assets_cli/native_assets_cli.dart';
6+
import 'package:native_assets_cli/code_assets.dart';
7+
import 'package:native_assets_cli/code_assets_builder.dart';
78
import 'package:native_toolchain_c/native_toolchain_c.dart';
89

9-
1010
void main(List<String> args) async {
11-
await build(args, (BuildConfig config, BuildOutput output) async {
11+
await build(args, (BuildConfig config, BuildOutputBuilder output) async {
1212
final String assetName;
1313
if (config.linkingEnabled) {
1414
// The link hook will be run. So emit an asset with a name that is
@@ -30,18 +30,19 @@ void main(List<String> args) async {
3030
],
3131
dartBuildFiles: <String>['hook/build.dart'],
3232
);
33-
final BuildOutput outputCatcher = BuildOutput();
33+
final BuildOutputBuilder outputCatcher = BuildOutputBuilder();
3434
await cbuilder.run(
3535
config: config,
3636
output: outputCatcher,
3737
logger: Logger('')
3838
..level = Level.ALL
3939
..onRecord.listen((LogRecord record) => print(record.message)),
4040
);
41-
output.addDependencies(outputCatcher.dependencies);
41+
final BuildOutput catchedOutput = BuildOutput(outputCatcher.json);
42+
output.addDependencies(catchedOutput.dependencies);
4243
// Send the asset to hook/link.dart or immediately for bundling.
43-
output.addAsset(
44-
outputCatcher.assets.single,
44+
output.codeAssets.add(
45+
catchedOutput.codeAssets.single,
4546
linkInPackage: config.linkingEnabled ? 'link_hook' : null,
4647
);
4748
});

dev/integration_tests/link_hook/hook/link.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'package:native_assets_cli/native_assets_cli.dart';
5+
import 'package:native_assets_cli/code_assets.dart';
66

77
void main(List<String> args) async {
8-
await link(args, (LinkConfig config, LinkOutput output) async {
9-
final NativeCodeAsset asset = config.assets.single as NativeCodeAsset;
8+
await link(args, (LinkConfig config, LinkOutputBuilder output) async {
9+
final CodeAsset asset = config.codeAssets.single;
1010
final String packageName = config.packageName;
11-
output.addAsset(NativeCodeAsset(
11+
output.codeAssets.add(CodeAsset(
1212
package: packageName,
1313
// Change the asset id to something that is used.
1414
name: '${packageName}_bindings_generated.dart',

dev/integration_tests/link_hook/pubspec.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ environment:
66
sdk: '>=3.5.0-154.0.dev <4.0.0'
77

88
dependencies:
9-
cli_config: 0.2.0
109
logging: 1.2.0
11-
native_assets_cli: 0.8.0
12-
native_toolchain_c: 0.5.4
10+
native_assets_cli: 0.9.0
11+
native_toolchain_c: 0.6.0
1312

1413
_fe_analyzer_shared: 76.0.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
1514
analyzer: 6.11.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
@@ -68,4 +67,4 @@ dev_dependencies:
6867
quiver: 3.2.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
6968
yaml_edit: 2.2.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
7069

71-
# PUBSPEC CHECKSUM: ddf6
70+
# PUBSPEC CHECKSUM: a89a

packages/flutter_tools/lib/src/isolated/native_assets/android/native_assets.dart

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:native_assets_builder/native_assets_builder.dart';
6-
import 'package:native_assets_cli/native_assets_cli.dart';
7-
import 'package:native_assets_cli/native_assets_cli_internal.dart';
6+
import 'package:native_assets_cli/code_assets_builder.dart';
87

98
import '../../../android/android_sdk.dart';
109
import '../../../android/gradle_utils.dart';
@@ -19,7 +18,7 @@ int targetAndroidNdkApi(Map<String, String> environmentDefines) {
1918

2019
Future<void> copyNativeCodeAssetsAndroid(
2120
Uri buildUri,
22-
Map<NativeCodeAssetImpl, KernelAsset> assetTargetLocations,
21+
Map<CodeAsset, KernelAsset> assetTargetLocations,
2322
FileSystem fileSystem,
2423
) async {
2524
if (assetTargetLocations.isNotEmpty) {
@@ -33,7 +32,7 @@ Future<void> copyNativeCodeAssetsAndroid(
3332
final Uri archUri = buildUri.resolve('jniLibs/lib/$jniArchDir/');
3433
await fileSystem.directory(archUri).create(recursive: true);
3534
}
36-
for (final MapEntry<NativeCodeAssetImpl, KernelAsset> assetMapping
35+
for (final MapEntry<CodeAsset, KernelAsset> assetMapping
3736
in assetTargetLocations.entries) {
3837
final Uri source = assetMapping.key.file!;
3938
final Uri target = (assetMapping.value.path as KernelAssetAbsolutePath).uri;
@@ -71,17 +70,17 @@ AndroidArch _getAndroidArch(Target target) {
7170
};
7271
}
7372

74-
Map<NativeCodeAssetImpl, KernelAsset> assetTargetLocationsAndroid(
75-
List<NativeCodeAssetImpl> nativeAssets) {
76-
return <NativeCodeAssetImpl, KernelAsset>{
77-
for (final NativeCodeAssetImpl asset in nativeAssets)
73+
Map<CodeAsset, KernelAsset> assetTargetLocationsAndroid(
74+
List<CodeAsset> nativeAssets) {
75+
return <CodeAsset, KernelAsset>{
76+
for (final CodeAsset asset in nativeAssets)
7877
asset: _targetLocationAndroid(asset),
7978
};
8079
}
8180

8281
/// Converts the `path` of [asset] as output from a `build.dart` invocation to
8382
/// the path used inside the Flutter app bundle.
84-
KernelAsset _targetLocationAndroid(NativeCodeAssetImpl asset) {
83+
KernelAsset _targetLocationAndroid(CodeAsset asset) {
8584
final LinkMode linkMode = asset.linkMode;
8685
final KernelAssetPath kernelAssetPath;
8786
switch (linkMode) {
@@ -113,12 +112,12 @@ KernelAsset _targetLocationAndroid(NativeCodeAssetImpl asset) {
113112
/// Should only be invoked if a native assets build is performed. If the native
114113
/// assets feature is disabled, or none of the packages have native assets, a
115114
/// missing NDK is okay.
116-
Future<CCompilerConfigImpl> cCompilerConfigAndroid() async {
115+
Future<CCompilerConfig> cCompilerConfigAndroid() async {
117116
final AndroidSdk? androidSdk = AndroidSdk.locateAndroidSdk();
118117
if (androidSdk == null) {
119118
throwToolExit('Android SDK could not be found.');
120119
}
121-
final CCompilerConfigImpl result = CCompilerConfigImpl(
120+
final CCompilerConfig result = CCompilerConfig(
122121
compiler: _toOptionalFileUri(androidSdk.getNdkClangPath()),
123122
archiver: _toOptionalFileUri(androidSdk.getNdkArPath()),
124123
linker: _toOptionalFileUri(androidSdk.getNdkLdPath()),

packages/flutter_tools/lib/src/isolated/native_assets/ios/native_assets.dart

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:native_assets_builder/native_assets_builder.dart';
6-
import 'package:native_assets_cli/native_assets_cli.dart';
7-
import 'package:native_assets_cli/native_assets_cli_internal.dart';
6+
import 'package:native_assets_cli/code_assets_builder.dart';
87

98
import '../../../base/file_system.dart';
109
import '../../../build_info.dart' hide BuildMode;
@@ -15,10 +14,10 @@ import '../macos/native_assets_host.dart';
1514
// TODO(dcharkes): Fetch minimum iOS version from somewhere. https://github.com/flutter/flutter/issues/145104
1615
const int targetIOSVersion = 12;
1716

18-
IOSSdkImpl getIOSSdk(EnvironmentType environmentType) {
17+
IOSSdk getIOSSdk(EnvironmentType environmentType) {
1918
return switch (environmentType) {
20-
EnvironmentType.physical => IOSSdkImpl.iPhoneOS,
21-
EnvironmentType.simulator => IOSSdkImpl.iPhoneSimulator,
19+
EnvironmentType.physical => IOSSdk.iPhoneOS,
20+
EnvironmentType.simulator => IOSSdk.iPhoneSimulator,
2221
};
2322
}
2423

@@ -31,33 +30,32 @@ Target getNativeIOSTarget(DarwinArch darwinArch) {
3130
};
3231
}
3332

34-
Map<KernelAssetPath, List<NativeCodeAssetImpl>> fatAssetTargetLocationsIOS(
35-
List<NativeCodeAssetImpl> nativeAssets) {
33+
Map<KernelAssetPath, List<CodeAsset>> fatAssetTargetLocationsIOS(
34+
List<CodeAsset> nativeAssets) {
3635
final Set<String> alreadyTakenNames = <String>{};
37-
final Map<KernelAssetPath, List<NativeCodeAssetImpl>> result =
38-
<KernelAssetPath, List<NativeCodeAssetImpl>>{};
36+
final Map<KernelAssetPath, List<CodeAsset>> result =
37+
<KernelAssetPath, List<CodeAsset>>{};
3938
final Map<String, KernelAssetPath> idToPath = <String, KernelAssetPath>{};
40-
for (final NativeCodeAssetImpl asset in nativeAssets) {
39+
for (final CodeAsset asset in nativeAssets) {
4140
// Use same target path for all assets with the same id.
4241
final KernelAssetPath path = idToPath[asset.id] ??
4342
_targetLocationIOS(
4443
asset,
4544
alreadyTakenNames,
4645
).path;
4746
idToPath[asset.id] = path;
48-
result[path] ??= <NativeCodeAssetImpl>[];
47+
result[path] ??= <CodeAsset>[];
4948
result[path]!.add(asset);
5049
}
5150
return result;
5251
}
5352

54-
Map<NativeCodeAssetImpl, KernelAsset> assetTargetLocationsIOS(
55-
List<NativeCodeAssetImpl> nativeAssets) {
53+
Map<CodeAsset, KernelAsset> assetTargetLocationsIOS(
54+
List<CodeAsset> nativeAssets) {
5655
final Set<String> alreadyTakenNames = <String>{};
5756
final Map<String, KernelAssetPath> idToPath = <String, KernelAssetPath>{};
58-
final Map<NativeCodeAssetImpl, KernelAsset> result =
59-
<NativeCodeAssetImpl, KernelAsset>{};
60-
for (final NativeCodeAssetImpl asset in nativeAssets) {
57+
final Map<CodeAsset, KernelAsset> result = <CodeAsset, KernelAsset>{};
58+
for (final CodeAsset asset in nativeAssets) {
6159
final KernelAssetPath path =
6260
idToPath[asset.id] ?? _targetLocationIOS(asset, alreadyTakenNames).path;
6361
idToPath[asset.id] = path;
@@ -70,8 +68,7 @@ Map<NativeCodeAssetImpl, KernelAsset> assetTargetLocationsIOS(
7068
return result;
7169
}
7270

73-
KernelAsset _targetLocationIOS(
74-
NativeCodeAssetImpl asset, Set<String> alreadyTakenNames) {
71+
KernelAsset _targetLocationIOS(CodeAsset asset, Set<String> alreadyTakenNames) {
7572
final LinkMode linkMode = asset.linkMode;
7673
final KernelAssetPath kernelAssetPath;
7774
switch (linkMode) {
@@ -113,7 +110,7 @@ KernelAsset _targetLocationIOS(
113110
/// in xcode_backend.dart.
114111
Future<void> copyNativeCodeAssetsIOS(
115112
Uri buildUri,
116-
Map<KernelAssetPath, List<NativeCodeAssetImpl>> assetTargetLocations,
113+
Map<KernelAssetPath, List<CodeAsset>> assetTargetLocations,
117114
String? codesignIdentity,
118115
build_info.BuildMode buildMode,
119116
FileSystem fileSystem,
@@ -125,11 +122,11 @@ Future<void> copyNativeCodeAssetsIOS(
125122
final Map<String, String> oldToNewInstallNames = <String, String>{};
126123
final List<(File, String, Directory)> dylibs = <(File, String, Directory)>[];
127124

128-
for (final MapEntry<KernelAssetPath, List<NativeCodeAssetImpl>> assetMapping
125+
for (final MapEntry<KernelAssetPath, List<CodeAsset>> assetMapping
129126
in assetTargetLocations.entries) {
130127
final Uri target = (assetMapping.key as KernelAssetAbsolutePath).uri;
131128
final List<File> sources = <File>[
132-
for (final NativeCodeAssetImpl source in assetMapping.value)
129+
for (final CodeAsset source in assetMapping.value)
133130
fileSystem.file(source.file)
134131
];
135132
final Uri targetUri = buildUri.resolveUri(target);

packages/flutter_tools/lib/src/isolated/native_assets/linux/native_assets.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'package:native_assets_cli/native_assets_cli_internal.dart';
5+
import 'package:native_assets_cli/code_assets_builder.dart';
66

77
import '../../../base/common.dart';
88
import '../../../base/file_system.dart';
@@ -12,7 +12,7 @@ import '../../../globals.dart' as globals;
1212
/// Flutter expects `clang++` to be on the path on Linux hosts.
1313
///
1414
/// Search for the accompanying `clang`, `ar`, and `ld`.
15-
Future<CCompilerConfigImpl> cCompilerConfigLinux() async {
15+
Future<CCompilerConfig> cCompilerConfigLinux() async {
1616
const String kClangPlusPlusBinary = 'clang++';
1717
const String kClangBinary = 'clang';
1818
const String kArBinary = 'llvm-ar';
@@ -34,7 +34,7 @@ Future<CCompilerConfigImpl> cCompilerConfigLinux() async {
3434
}
3535
binaryPaths[binary] = binaryFile.uri;
3636
}
37-
return CCompilerConfigImpl(
37+
return CCompilerConfig(
3838
archiver: binaryPaths[kArBinary],
3939
compiler: binaryPaths[kClangBinary],
4040
linker: binaryPaths[kLdBinary],

packages/flutter_tools/lib/src/isolated/native_assets/macos/native_assets.dart

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:native_assets_builder/native_assets_builder.dart';
6-
import 'package:native_assets_cli/native_assets_cli.dart';
7-
import 'package:native_assets_cli/native_assets_cli_internal.dart';
6+
import 'package:native_assets_cli/code_assets_builder.dart';
87

98
import '../../../base/file_system.dart';
109
import '../../../build_info.dart' hide BuildMode;
@@ -24,15 +23,15 @@ Target getNativeMacOSTarget(DarwinArch darwinArch) {
2423
};
2524
}
2625

27-
Map<KernelAssetPath, List<NativeCodeAssetImpl>> fatAssetTargetLocationsMacOS(
28-
List<NativeCodeAssetImpl> nativeAssets,
26+
Map<KernelAssetPath, List<CodeAsset>> fatAssetTargetLocationsMacOS(
27+
List<CodeAsset> nativeAssets,
2928
Uri? absolutePath,
3029
) {
3130
final Set<String> alreadyTakenNames = <String>{};
32-
final Map<KernelAssetPath, List<NativeCodeAssetImpl>> result =
33-
<KernelAssetPath, List<NativeCodeAssetImpl>>{};
31+
final Map<KernelAssetPath, List<CodeAsset>> result =
32+
<KernelAssetPath, List<CodeAsset>>{};
3433
final Map<String, KernelAssetPath> idToPath = <String, KernelAssetPath>{};
35-
for (final NativeCodeAssetImpl asset in nativeAssets) {
34+
for (final CodeAsset asset in nativeAssets) {
3635
// Use same target path for all assets with the same id.
3736
final KernelAssetPath path = idToPath[asset.id] ??
3837
_targetLocationMacOS(
@@ -41,20 +40,20 @@ Map<KernelAssetPath, List<NativeCodeAssetImpl>> fatAssetTargetLocationsMacOS(
4140
alreadyTakenNames,
4241
).path;
4342
idToPath[asset.id] = path;
44-
result[path] ??= <NativeCodeAssetImpl>[];
43+
result[path] ??= <CodeAsset>[];
4544
result[path]!.add(asset);
4645
}
4746
return result;
4847
}
4948

50-
Map<NativeCodeAssetImpl, KernelAsset> assetTargetLocationsMacOS(
51-
List<NativeCodeAssetImpl> nativeAssets,
49+
Map<CodeAsset, KernelAsset> assetTargetLocationsMacOS(
50+
List<CodeAsset> nativeAssets,
5251
Uri? absolutePath,
5352
) {
5453
final Set<String> alreadyTakenNames = <String>{};
5554
final Map<String, KernelAssetPath> idToPath = <String, KernelAssetPath>{};
56-
final Map<NativeCodeAssetImpl, KernelAsset> result = <NativeCodeAssetImpl, KernelAsset>{};
57-
for (final NativeCodeAssetImpl asset in nativeAssets) {
55+
final Map<CodeAsset, KernelAsset> result = <CodeAsset, KernelAsset>{};
56+
for (final CodeAsset asset in nativeAssets) {
5857
final KernelAssetPath path = idToPath[asset.id] ??
5958
_targetLocationMacOS(asset, absolutePath, alreadyTakenNames).path;
6059
idToPath[asset.id] = path;
@@ -68,7 +67,7 @@ Map<NativeCodeAssetImpl, KernelAsset> assetTargetLocationsMacOS(
6867
}
6968

7069
KernelAsset _targetLocationMacOS(
71-
NativeCodeAssetImpl asset,
70+
CodeAsset asset,
7271
Uri? absolutePath,
7372
Set<String> alreadyTakenNames,
7473
) {
@@ -123,7 +122,7 @@ KernelAsset _targetLocationMacOS(
123122
/// in macos_assemble.sh.
124123
Future<void> copyNativeCodeAssetsMacOS(
125124
Uri buildUri,
126-
Map<KernelAssetPath, List<NativeCodeAssetImpl>> assetTargetLocations,
125+
Map<KernelAssetPath, List<CodeAsset>> assetTargetLocations,
127126
String? codesignIdentity,
128127
build_info.BuildMode buildMode,
129128
FileSystem fileSystem,
@@ -136,11 +135,11 @@ Future<void> copyNativeCodeAssetsMacOS(
136135
final Map<String, String> oldToNewInstallNames = <String, String>{};
137136
final List<(File, String, Directory)> dylibs = <(File, String, Directory)>[];
138137

139-
for (final MapEntry<KernelAssetPath, List<NativeCodeAssetImpl>> assetMapping
138+
for (final MapEntry<KernelAssetPath, List<CodeAsset>> assetMapping
140139
in assetTargetLocations.entries) {
141140
final Uri target = (assetMapping.key as KernelAssetAbsolutePath).uri;
142141
final List<File> sources = <File>[
143-
for (final NativeCodeAssetImpl source in assetMapping.value) fileSystem.file(source.file),
142+
for (final CodeAsset source in assetMapping.value) fileSystem.file(source.file),
144143
];
145144
final Uri targetUri = buildUri.resolveUri(target);
146145
final String name = targetUri.pathSegments.last;
@@ -217,7 +216,7 @@ Future<void> copyNativeCodeAssetsMacOS(
217216
/// Code signing is also done here.
218217
Future<void> copyNativeCodeAssetsMacOSFlutterTester(
219218
Uri buildUri,
220-
Map<KernelAssetPath, List<NativeCodeAssetImpl>> assetTargetLocations,
219+
Map<KernelAssetPath, List<CodeAsset>> assetTargetLocations,
221220
String? codesignIdentity,
222221
build_info.BuildMode buildMode,
223222
FileSystem fileSystem,
@@ -230,11 +229,11 @@ Future<void> copyNativeCodeAssetsMacOSFlutterTester(
230229
final Map<String, String> oldToNewInstallNames = <String, String>{};
231230
final List<(File, String)> dylibs = <(File, String)>[];
232231

233-
for (final MapEntry<KernelAssetPath, List<NativeCodeAssetImpl>> assetMapping
232+
for (final MapEntry<KernelAssetPath, List<CodeAsset>> assetMapping
234233
in assetTargetLocations.entries) {
235234
final Uri target = (assetMapping.key as KernelAssetAbsolutePath).uri;
236235
final List<File> sources = <File>[
237-
for (final NativeCodeAssetImpl source in assetMapping.value) fileSystem.file(source.file),
236+
for (final CodeAsset source in assetMapping.value) fileSystem.file(source.file),
238237
];
239238
final Uri targetUri = buildUri.resolveUri(target);
240239
final File dylibFile = fileSystem.file(targetUri);

0 commit comments

Comments
 (0)