Skip to content

Commit

Permalink
Roll native_assets_builder to 0.5.0 (#143472)
Browse files Browse the repository at this point in the history
Roll of dart-lang/native#964, which separates the `KernelAsset`s (the asset information embedded in the Dart kernel snapshot) from `Asset`s (the assets in the `build.dart` protocol). See the linked issue for why they ought to be different instead of shared.

This PR does not change any functionality in Flutter.

(Now that flutter/flutter#143055 has landed, we can land breaking changes.)

For reference, the same roll in the Dart SDK: https://dart-review.googlesource.com/c/sdk/+/352642
  • Loading branch information
dcharkes authored Feb 14, 2024
1 parent 5b005e4 commit db83bc6
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// found in the LICENSE file.

import 'package:meta/meta.dart';
import 'package:native_assets_cli/native_assets_cli_internal.dart' show Asset;
import 'package:native_assets_builder/native_assets_builder.dart' hide NativeAssetsBuildRunner;
import 'package:package_config/package_config_types.dart';

import '../../android/gradle_utils.dart';
Expand Down Expand Up @@ -56,7 +56,7 @@ class NativeAssets extends Target {
final File nativeAssetsFile = environment.buildDir.childFile('native_assets.yaml');
if (nativeAssetsEnvironment == 'false') {
dependencies = <Uri>[];
await writeNativeAssetsYaml(<Asset>[], environment.buildDir.uri, fileSystem);
await writeNativeAssetsYaml(KernelAssets(), environment.buildDir.uri, fileSystem);
} else {
final String? targetPlatformEnvironment = environment.defines[kTargetPlatform];
if (targetPlatformEnvironment == null) {
Expand Down Expand Up @@ -145,7 +145,7 @@ class NativeAssets extends Target {
} else {
// TODO(dacoharkes): Implement other OSes. https://github.com/flutter/flutter/issues/129757
// Write the file we claim to have in the [outputs].
await writeNativeAssetsYaml(<Asset>[], environment.buildDir.uri, fileSystem);
await writeNativeAssetsYaml(KernelAssets(), environment.buildDir.uri, fileSystem);
dependencies = <Uri>[];
}
case TargetPlatform.android_arm:
Expand All @@ -165,7 +165,7 @@ class NativeAssets extends Target {
case TargetPlatform.web_javascript:
// TODO(dacoharkes): Implement other OSes. https://github.com/flutter/flutter/issues/129757
// Write the file we claim to have in the [outputs].
await writeNativeAssetsYaml(<Asset>[], environment.buildDir.uri, fileSystem);
await writeNativeAssetsYaml(KernelAssets(), environment.buildDir.uri, fileSystem);
dependencies = <Uri>[];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// found in the LICENSE file.

import 'package:native_assets_builder/native_assets_builder.dart'
show BuildResult, DryRunResult;
hide NativeAssetsBuildRunner;
import 'package:native_assets_cli/native_assets_cli_internal.dart'
as native_assets_cli;
import 'package:native_assets_cli/native_assets_cli_internal.dart'
Expand Down Expand Up @@ -31,21 +31,21 @@ Future<Uri?> dryRunNativeAssetsAndroid({
}

final Uri buildUri_ = nativeAssetsBuildUri(projectUri, OS.android);
final Iterable<Asset> nativeAssetPaths =
final Iterable<KernelAsset> nativeAssetPaths =
await dryRunNativeAssetsAndroidInternal(
fileSystem,
projectUri,
buildRunner,
);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(
nativeAssetPaths,
KernelAssets(nativeAssetPaths),
buildUri_,
fileSystem,
);
return nativeAssetsUri;
}

Future<Iterable<Asset>> dryRunNativeAssetsAndroidInternal(
Future<Iterable<KernelAsset>> dryRunNativeAssetsAndroidInternal(
FileSystem fileSystem,
Uri projectUri,
NativeAssetsBuildRunner buildRunner,
Expand All @@ -63,10 +63,9 @@ Future<Iterable<Asset>> dryRunNativeAssetsAndroidInternal(
final List<Asset> nativeAssets = dryRunResult.assets;
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Dry running native assets for $targetOS done.');
final Map<Asset, Asset> assetTargetLocations =
final Map<Asset, KernelAsset> assetTargetLocations =
_assetTargetLocations(nativeAssets);
final Iterable<Asset> nativeAssetPaths = assetTargetLocations.values;
return nativeAssetPaths;
return assetTargetLocations.values;
}

/// Builds native assets.
Expand All @@ -85,7 +84,7 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)>
final Uri buildUri_ = nativeAssetsBuildUri(projectUri, targetOS);
if (!await nativeBuildRequired(buildRunner)) {
final Uri nativeAssetsYaml = await writeNativeAssetsYaml(
<Asset>[],
KernelAssets(),
yamlParentDirectory ?? buildUri_,
fileSystem,
);
Expand Down Expand Up @@ -116,19 +115,19 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)>
}
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Building native assets for $targets done.');
final Map<Asset, Asset> assetTargetLocations =
final Map<Asset, KernelAsset> assetTargetLocations =
_assetTargetLocations(nativeAssets);
await _copyNativeAssetsAndroid(buildUri_, assetTargetLocations, fileSystem);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(
assetTargetLocations.values,
KernelAssets(assetTargetLocations.values),
yamlParentDirectory ?? buildUri_,
fileSystem);
return (nativeAssetsUri, dependencies.toList());
}

Future<void> _copyNativeAssetsAndroid(
Uri buildUri,
Map<Asset, Asset> assetTargetLocations,
Map<Asset, KernelAsset> assetTargetLocations,
FileSystem fileSystem,
) async {
if (assetTargetLocations.isNotEmpty) {
Expand All @@ -142,10 +141,10 @@ Future<void> _copyNativeAssetsAndroid(
final Uri archUri = buildUri.resolve('jniLibs/lib/$jniArchDir/');
await fileSystem.directory(archUri).create(recursive: true);
}
for (final MapEntry<Asset, Asset> assetMapping
for (final MapEntry<Asset, KernelAsset> assetMapping
in assetTargetLocations.entries) {
final Uri source = (assetMapping.key.path as AssetAbsolutePath).uri;
final Uri target = (assetMapping.value.path as AssetAbsolutePath).uri;
final Uri target = (assetMapping.value.path as KernelAssetAbsolutePath).uri;
final AndroidArch androidArch =
_getAndroidArch(assetMapping.value.target);
final String jniArchDir = androidArch.archName;
Expand Down Expand Up @@ -190,28 +189,37 @@ AndroidArch _getAndroidArch(Target target) {
}
}

Map<Asset, Asset> _assetTargetLocations(List<Asset> nativeAssets) {
return <Asset, Asset>{
Map<Asset, KernelAsset> _assetTargetLocations(List<Asset> nativeAssets) {
return <Asset, KernelAsset>{
for (final Asset asset in nativeAssets)
asset: _targetLocationAndroid(asset),
};
}

/// Converts the `path` of [asset] as output from a `build.dart` invocation to
/// the path used inside the Flutter app bundle.
Asset _targetLocationAndroid(Asset asset) {
KernelAsset _targetLocationAndroid(Asset asset) {
final AssetPath path = asset.path;
final KernelAssetPath kernelAssetPath;
switch (path) {
case AssetSystemPath _:
kernelAssetPath = KernelAssetSystemPath(path.uri);
case AssetInExecutable _:
kernelAssetPath = KernelAssetInExecutable();
case AssetInProcess _:
return asset;
kernelAssetPath = KernelAssetInProcess();
case AssetAbsolutePath _:
final String fileName = path.uri.pathSegments.last;
return asset.copyWith(path: AssetAbsolutePath(Uri(path: fileName)));
kernelAssetPath = KernelAssetAbsolutePath(Uri(path: fileName));
default:
throw Exception(
'Unsupported asset path type ${path.runtimeType} in asset $asset',
);
}
throw Exception(
'Unsupported asset path type ${path.runtimeType} in asset $asset',
return KernelAsset(
id: asset.id,
target: asset.target,
path: kernelAssetPath,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// found in the LICENSE file.

import 'package:native_assets_builder/native_assets_builder.dart'
show BuildResult, DryRunResult;
hide NativeAssetsBuildRunner;
import 'package:native_assets_cli/native_assets_cli_internal.dart'
hide BuildMode;
import 'package:native_assets_cli/native_assets_cli_internal.dart'
Expand Down Expand Up @@ -31,20 +31,20 @@ Future<Uri?> dryRunNativeAssetsIOS({
}

final Uri buildUri = nativeAssetsBuildUri(projectUri, OS.iOS);
final Iterable<Asset> assetTargetLocations = await dryRunNativeAssetsIOSInternal(
final Iterable<KernelAsset> assetTargetLocations = await dryRunNativeAssetsIOSInternal(
fileSystem,
projectUri,
buildRunner,
);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(
assetTargetLocations,
KernelAssets(assetTargetLocations),
buildUri,
fileSystem,
);
return nativeAssetsUri;
}

Future<Iterable<Asset>> dryRunNativeAssetsIOSInternal(
Future<Iterable<KernelAsset>> dryRunNativeAssetsIOSInternal(
FileSystem fileSystem,
Uri projectUri,
NativeAssetsBuildRunner buildRunner,
Expand All @@ -61,8 +61,7 @@ Future<Iterable<Asset>> dryRunNativeAssetsIOSInternal(
final List<Asset> nativeAssets = dryRunResult.assets;
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Dry running native assets for $targetOS done.');
final Iterable<Asset> assetTargetLocations = _assetTargetLocations(nativeAssets).values;
return assetTargetLocations;
return _assetTargetLocations(nativeAssets).values;
}

/// Builds native assets.
Expand All @@ -77,7 +76,7 @@ Future<List<Uri>> buildNativeAssetsIOS({
required FileSystem fileSystem,
}) async {
if (!await nativeBuildRequired(buildRunner)) {
await writeNativeAssetsYaml(<Asset>[], yamlParentDirectory, fileSystem);
await writeNativeAssetsYaml(KernelAssets(), yamlParentDirectory, fileSystem);
return <Uri>[];
}

Expand Down Expand Up @@ -107,7 +106,7 @@ Future<List<Uri>> buildNativeAssetsIOS({
}
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Building native assets for $targets done.');
final Map<AssetPath, List<Asset>> fatAssetTargetLocations = _fatAssetTargetLocations(nativeAssets);
final Map<KernelAssetPath, List<Asset>> fatAssetTargetLocations = _fatAssetTargetLocations(nativeAssets);
await _copyNativeAssetsIOS(
buildUri,
fatAssetTargetLocations,
Expand All @@ -116,9 +115,9 @@ Future<List<Uri>> buildNativeAssetsIOS({
fileSystem,
);

final Map<Asset, Asset> assetTargetLocations = _assetTargetLocations(nativeAssets);
final Map<Asset, KernelAsset> assetTargetLocations = _assetTargetLocations(nativeAssets);
await writeNativeAssetsYaml(
assetTargetLocations.values,
KernelAssets(assetTargetLocations.values),
yamlParentDirectory,
fileSystem,
);
Expand Down Expand Up @@ -146,40 +145,51 @@ Target _getNativeTarget(DarwinArch darwinArch) {
}
}

Map<AssetPath, List<Asset>> _fatAssetTargetLocations(List<Asset> nativeAssets) {
Map<KernelAssetPath, List<Asset>> _fatAssetTargetLocations(List<Asset> nativeAssets) {
final Set<String> alreadyTakenNames = <String>{};
final Map<AssetPath, List<Asset>> result = <AssetPath, List<Asset>>{};
final Map<KernelAssetPath, List<Asset>> result = <KernelAssetPath, List<Asset>>{};
for (final Asset asset in nativeAssets) {
final AssetPath path = _targetLocationIOS(asset, alreadyTakenNames).path;
final KernelAssetPath path = _targetLocationIOS(asset, alreadyTakenNames).path;
result[path] ??= <Asset>[];
result[path]!.add(asset);
}
return result;
}

Map<Asset, Asset> _assetTargetLocations(List<Asset> nativeAssets) {
Map<Asset, KernelAsset> _assetTargetLocations(List<Asset> nativeAssets) {
final Set<String> alreadyTakenNames = <String>{};
return <Asset, Asset>{
return <Asset, KernelAsset>{
for (final Asset asset in nativeAssets)
asset: _targetLocationIOS(asset, alreadyTakenNames),
};
}

Asset _targetLocationIOS(Asset asset, Set<String> alreadyTakenNames) {
KernelAsset _targetLocationIOS(Asset asset, Set<String> alreadyTakenNames) {
final AssetPath path = asset.path;
final KernelAssetPath kernelAssetPath;
switch (path) {
case AssetSystemPath _:
kernelAssetPath = KernelAssetSystemPath(path.uri);
case AssetInExecutable _:
kernelAssetPath = KernelAssetInExecutable();
case AssetInProcess _:
return asset;
kernelAssetPath = KernelAssetInProcess();
case AssetAbsolutePath _:
final String fileName = path.uri.pathSegments.last;
return asset.copyWith(
path: AssetAbsolutePath(frameworkUri(fileName, alreadyTakenNames)),
kernelAssetPath = KernelAssetAbsolutePath(frameworkUri(
fileName,
alreadyTakenNames,
));
default:
throw Exception(
'Unsupported asset path type ${path.runtimeType} in asset $asset',
);
}
throw Exception(
'Unsupported asset path type ${path.runtimeType} in asset $asset');
return KernelAsset(
id: asset.id,
target: asset.target,
path: kernelAssetPath,
);
}

/// Copies native assets into a framework per dynamic library.
Expand All @@ -194,17 +204,17 @@ Asset _targetLocationIOS(Asset asset, Set<String> alreadyTakenNames) {
/// in xcode_backend.dart.
Future<void> _copyNativeAssetsIOS(
Uri buildUri,
Map<AssetPath, List<Asset>> assetTargetLocations,
Map<KernelAssetPath, List<Asset>> assetTargetLocations,
String? codesignIdentity,
BuildMode buildMode,
FileSystem fileSystem,
) async {
if (assetTargetLocations.isNotEmpty) {
globals.logger
.printTrace('Copying native assets to ${buildUri.toFilePath()}.');
for (final MapEntry<AssetPath, List<Asset>> assetMapping
for (final MapEntry<KernelAssetPath, List<Asset>> assetMapping
in assetTargetLocations.entries) {
final Uri target = (assetMapping.key as AssetAbsolutePath).uri;
final Uri target = (assetMapping.key as KernelAssetAbsolutePath).uri;
final List<Uri> sources = <Uri>[
for (final Asset source in assetMapping.value)
(source.path as AssetAbsolutePath).uri
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:native_assets_builder/native_assets_builder.dart'
hide NativeAssetsBuildRunner;
import 'package:native_assets_cli/native_assets_cli_internal.dart'
hide BuildMode;

Expand Down Expand Up @@ -31,7 +33,7 @@ Future<Uri?> dryRunNativeAssetsLinux({
);
}

Future<Iterable<Asset>> dryRunNativeAssetsLinuxInternal(
Future<Iterable<KernelAsset>> dryRunNativeAssetsLinuxInternal(
FileSystem fileSystem,
Uri projectUri,
bool flutterTester,
Expand Down
Loading

0 comments on commit db83bc6

Please sign in to comment.