Unverified Commit 402a5455 authored by Daco Harkes's avatar Daco Harkes Committed by GitHub

Don't build native assets in `flutter build bundle` (#136641)

Closes: https://github.com/flutter/flutter/issues/136547
parent 4b7b4d41
...@@ -967,6 +967,20 @@ const String kSdkRoot = 'SdkRoot'; ...@@ -967,6 +967,20 @@ const String kSdkRoot = 'SdkRoot';
/// Whether to enable Dart obfuscation and where to save the symbol map. /// Whether to enable Dart obfuscation and where to save the symbol map.
const String kDartObfuscation = 'DartObfuscation'; const String kDartObfuscation = 'DartObfuscation';
/// Whether to enable Native Assets.
///
/// If true, native assets are built and the mapping for native assets lookup
/// at runtime is embedded in the kernel file.
///
/// If false, native assets are not built, and an empty mapping is embedded in
/// the kernel file. Used for targets that trigger kernel builds but
/// are not OS/architecture specific.
///
/// Supported values are 'true' and 'false'.
///
/// Defaults to 'true'.
const String kNativeAssets = 'NativeAssets';
/// An output directory where one or more code-size measurements may be written. /// An output directory where one or more code-size measurements may be written.
const String kCodeSizeDirectory = 'CodeSizeDirectory'; const String kCodeSizeDirectory = 'CodeSizeDirectory';
......
...@@ -48,157 +48,163 @@ class NativeAssets extends Target { ...@@ -48,157 +48,163 @@ class NativeAssets extends Target {
@override @override
Future<void> build(Environment environment) async { Future<void> build(Environment environment) async {
final String? targetPlatformEnvironment = environment.defines[kTargetPlatform]; final String? nativeAssetsEnvironment = environment.defines[kNativeAssets];
if (targetPlatformEnvironment == null) { final List<Uri> dependencies;
throw MissingDefineException(kTargetPlatform, name);
}
final TargetPlatform targetPlatform = getTargetPlatformForName(targetPlatformEnvironment);
final Uri projectUri = environment.projectDir.uri;
final FileSystem fileSystem = environment.fileSystem; final FileSystem fileSystem = environment.fileSystem;
final File packagesFile = fileSystem final File nativeAssetsFile = environment.buildDir.childFile('native_assets.yaml');
.directory(projectUri) if (nativeAssetsEnvironment == 'false') {
.childDirectory('.dart_tool') dependencies = <Uri>[];
.childFile('package_config.json'); await writeNativeAssetsYaml(<Asset>[], environment.buildDir.uri, fileSystem);
final PackageConfig packageConfig = await loadPackageConfigWithLogging( } else {
packagesFile, final String? targetPlatformEnvironment = environment.defines[kTargetPlatform];
logger: environment.logger, if (targetPlatformEnvironment == null) {
); throw MissingDefineException(kTargetPlatform, name);
final NativeAssetsBuildRunner buildRunner = _buildRunner ?? }
NativeAssetsBuildRunnerImpl( final TargetPlatform targetPlatform = getTargetPlatformForName(targetPlatformEnvironment);
projectUri, final Uri projectUri = environment.projectDir.uri;
packageConfig, final File packagesFile = fileSystem
fileSystem, .directory(projectUri)
environment.logger, .childDirectory('.dart_tool')
); .childFile('package_config.json');
final PackageConfig packageConfig = await loadPackageConfigWithLogging(
packagesFile,
logger: environment.logger,
);
final NativeAssetsBuildRunner buildRunner = _buildRunner ??
NativeAssetsBuildRunnerImpl(
projectUri,
packageConfig,
fileSystem,
environment.logger,
);
final List<Uri> dependencies;
switch (targetPlatform) { switch (targetPlatform) {
case TargetPlatform.ios: case TargetPlatform.ios:
final String? iosArchsEnvironment = environment.defines[kIosArchs]; final String? iosArchsEnvironment = environment.defines[kIosArchs];
if (iosArchsEnvironment == null) { if (iosArchsEnvironment == null) {
throw MissingDefineException(kIosArchs, name); throw MissingDefineException(kIosArchs, name);
} }
final List<DarwinArch> iosArchs = iosArchsEnvironment.split(' ').map(getDarwinArchForName).toList(); final List<DarwinArch> iosArchs = iosArchsEnvironment.split(' ').map(getDarwinArchForName).toList();
final String? environmentBuildMode = environment.defines[kBuildMode]; final String? environmentBuildMode = environment.defines[kBuildMode];
if (environmentBuildMode == null) { if (environmentBuildMode == null) {
throw MissingDefineException(kBuildMode, name); throw MissingDefineException(kBuildMode, name);
} }
final BuildMode buildMode = BuildMode.fromCliName(environmentBuildMode); final BuildMode buildMode = BuildMode.fromCliName(environmentBuildMode);
final String? sdkRoot = environment.defines[kSdkRoot]; final String? sdkRoot = environment.defines[kSdkRoot];
if (sdkRoot == null) { if (sdkRoot == null) {
throw MissingDefineException(kSdkRoot, name); throw MissingDefineException(kSdkRoot, name);
} }
final EnvironmentType environmentType = environmentTypeFromSdkroot(sdkRoot, environment.fileSystem)!; final EnvironmentType environmentType = environmentTypeFromSdkroot(sdkRoot, environment.fileSystem)!;
dependencies = await buildNativeAssetsIOS( dependencies = await buildNativeAssetsIOS(
environmentType: environmentType, environmentType: environmentType,
darwinArchs: iosArchs, darwinArchs: iosArchs,
buildMode: buildMode, buildMode: buildMode,
projectUri: projectUri, projectUri: projectUri,
codesignIdentity: environment.defines[kCodesignIdentity], codesignIdentity: environment.defines[kCodesignIdentity],
fileSystem: fileSystem, fileSystem: fileSystem,
buildRunner: buildRunner, buildRunner: buildRunner,
yamlParentDirectory: environment.buildDir.uri, yamlParentDirectory: environment.buildDir.uri,
); );
case TargetPlatform.darwin: case TargetPlatform.darwin:
final String? darwinArchsEnvironment = environment.defines[kDarwinArchs]; final String? darwinArchsEnvironment = environment.defines[kDarwinArchs];
if (darwinArchsEnvironment == null) { if (darwinArchsEnvironment == null) {
throw MissingDefineException(kDarwinArchs, name); throw MissingDefineException(kDarwinArchs, name);
} }
final List<DarwinArch> darwinArchs = darwinArchsEnvironment.split(' ').map(getDarwinArchForName).toList(); final List<DarwinArch> darwinArchs = darwinArchsEnvironment.split(' ').map(getDarwinArchForName).toList();
final String? environmentBuildMode = environment.defines[kBuildMode]; final String? environmentBuildMode = environment.defines[kBuildMode];
if (environmentBuildMode == null) { if (environmentBuildMode == null) {
throw MissingDefineException(kBuildMode, name); throw MissingDefineException(kBuildMode, name);
} }
final BuildMode buildMode = BuildMode.fromCliName(environmentBuildMode); final BuildMode buildMode = BuildMode.fromCliName(environmentBuildMode);
(_, dependencies) = await buildNativeAssetsMacOS(
darwinArchs: darwinArchs,
buildMode: buildMode,
projectUri: projectUri,
codesignIdentity: environment.defines[kCodesignIdentity],
yamlParentDirectory: environment.buildDir.uri,
fileSystem: fileSystem,
buildRunner: buildRunner,
);
case TargetPlatform.linux_arm64:
case TargetPlatform.linux_x64:
final String? environmentBuildMode = environment.defines[kBuildMode];
if (environmentBuildMode == null) {
throw MissingDefineException(kBuildMode, name);
}
final BuildMode buildMode = BuildMode.fromCliName(environmentBuildMode);
(_, dependencies) = await buildNativeAssetsLinux(
targetPlatform: targetPlatform,
buildMode: buildMode,
projectUri: projectUri,
yamlParentDirectory: environment.buildDir.uri,
fileSystem: fileSystem,
buildRunner: buildRunner,
);
case TargetPlatform.windows_x64:
final String? environmentBuildMode = environment.defines[kBuildMode];
if (environmentBuildMode == null) {
throw MissingDefineException(kBuildMode, name);
}
final BuildMode buildMode = BuildMode.fromCliName(environmentBuildMode);
(_, dependencies) = await buildNativeAssetsWindows(
targetPlatform: targetPlatform,
buildMode: buildMode,
projectUri: projectUri,
yamlParentDirectory: environment.buildDir.uri,
fileSystem: fileSystem,
buildRunner: buildRunner,
);
case TargetPlatform.tester:
if (const LocalPlatform().isMacOS) {
(_, dependencies) = await buildNativeAssetsMacOS( (_, dependencies) = await buildNativeAssetsMacOS(
buildMode: BuildMode.debug, darwinArchs: darwinArchs,
buildMode: buildMode,
projectUri: projectUri, projectUri: projectUri,
codesignIdentity: environment.defines[kCodesignIdentity], codesignIdentity: environment.defines[kCodesignIdentity],
yamlParentDirectory: environment.buildDir.uri, yamlParentDirectory: environment.buildDir.uri,
fileSystem: fileSystem, fileSystem: fileSystem,
buildRunner: buildRunner, buildRunner: buildRunner,
flutterTester: true,
); );
} else if (const LocalPlatform().isLinux) { case TargetPlatform.linux_arm64:
case TargetPlatform.linux_x64:
final String? environmentBuildMode = environment.defines[kBuildMode];
if (environmentBuildMode == null) {
throw MissingDefineException(kBuildMode, name);
}
final BuildMode buildMode = BuildMode.fromCliName(environmentBuildMode);
(_, dependencies) = await buildNativeAssetsLinux( (_, dependencies) = await buildNativeAssetsLinux(
buildMode: BuildMode.debug, targetPlatform: targetPlatform,
buildMode: buildMode,
projectUri: projectUri, projectUri: projectUri,
yamlParentDirectory: environment.buildDir.uri, yamlParentDirectory: environment.buildDir.uri,
fileSystem: fileSystem, fileSystem: fileSystem,
buildRunner: buildRunner, buildRunner: buildRunner,
flutterTester: true,
); );
} else if (const LocalPlatform().isWindows) { case TargetPlatform.windows_x64:
final String? environmentBuildMode = environment.defines[kBuildMode];
if (environmentBuildMode == null) {
throw MissingDefineException(kBuildMode, name);
}
final BuildMode buildMode = BuildMode.fromCliName(environmentBuildMode);
(_, dependencies) = await buildNativeAssetsWindows( (_, dependencies) = await buildNativeAssetsWindows(
buildMode: BuildMode.debug, targetPlatform: targetPlatform,
buildMode: buildMode,
projectUri: projectUri, projectUri: projectUri,
yamlParentDirectory: environment.buildDir.uri, yamlParentDirectory: environment.buildDir.uri,
fileSystem: fileSystem, fileSystem: fileSystem,
buildRunner: buildRunner, buildRunner: buildRunner,
flutterTester: true,
); );
} else { case TargetPlatform.tester:
if (const LocalPlatform().isMacOS) {
(_, dependencies) = await buildNativeAssetsMacOS(
buildMode: BuildMode.debug,
projectUri: projectUri,
codesignIdentity: environment.defines[kCodesignIdentity],
yamlParentDirectory: environment.buildDir.uri,
fileSystem: fileSystem,
buildRunner: buildRunner,
flutterTester: true,
);
} else if (const LocalPlatform().isLinux) {
(_, dependencies) = await buildNativeAssetsLinux(
buildMode: BuildMode.debug,
projectUri: projectUri,
yamlParentDirectory: environment.buildDir.uri,
fileSystem: fileSystem,
buildRunner: buildRunner,
flutterTester: true,
);
} else if (const LocalPlatform().isWindows) {
(_, dependencies) = await buildNativeAssetsWindows(
buildMode: BuildMode.debug,
projectUri: projectUri,
yamlParentDirectory: environment.buildDir.uri,
fileSystem: fileSystem,
buildRunner: buildRunner,
flutterTester: true,
);
} 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);
dependencies = <Uri>[];
}
case TargetPlatform.android_arm:
case TargetPlatform.android_arm64:
case TargetPlatform.android_x64:
case TargetPlatform.android_x86:
case TargetPlatform.android:
case TargetPlatform.fuchsia_arm64:
case TargetPlatform.fuchsia_x64:
case TargetPlatform.web_javascript:
// TODO(dacoharkes): Implement other OSes. https://github.com/flutter/flutter/issues/129757 // TODO(dacoharkes): Implement other OSes. https://github.com/flutter/flutter/issues/129757
// Write the file we claim to have in the [outputs]. // Write the file we claim to have in the [outputs].
await writeNativeAssetsYaml(<Asset>[], environment.buildDir.uri, fileSystem); await writeNativeAssetsYaml(<Asset>[], environment.buildDir.uri, fileSystem);
dependencies = <Uri>[]; dependencies = <Uri>[];
} }
case TargetPlatform.android_arm:
case TargetPlatform.android_arm64:
case TargetPlatform.android_x64:
case TargetPlatform.android_x86:
case TargetPlatform.android:
case TargetPlatform.fuchsia_arm64:
case TargetPlatform.fuchsia_x64:
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);
dependencies = <Uri>[];
} }
final File nativeAssetsFile = environment.buildDir.childFile('native_assets.yaml');
final Depfile depfile = Depfile( final Depfile depfile = Depfile(
<File>[ <File>[
for (final Uri dependency in dependencies) fileSystem.file(dependency), for (final Uri dependency in dependencies) fileSystem.file(dependency),
......
...@@ -26,7 +26,11 @@ class BundleBuilder { ...@@ -26,7 +26,11 @@ class BundleBuilder {
/// Builds the bundle for the given target platform. /// Builds the bundle for the given target platform.
/// ///
/// The default `mainPath` is `lib/main.dart`. /// The default `mainPath` is `lib/main.dart`.
/// The default `manifestPath` is `pubspec.yaml` /// The default `manifestPath` is `pubspec.yaml`.
///
/// If [buildNativeAssets], native assets are built and the mapping for native
/// assets lookup at runtime is embedded in the kernel file, otherwise an
/// empty native assets mapping is embedded in the kernel file.
Future<void> build({ Future<void> build({
required TargetPlatform platform, required TargetPlatform platform,
required BuildInfo buildInfo, required BuildInfo buildInfo,
...@@ -36,6 +40,7 @@ class BundleBuilder { ...@@ -36,6 +40,7 @@ class BundleBuilder {
String? applicationKernelFilePath, String? applicationKernelFilePath,
String? depfilePath, String? depfilePath,
String? assetDirPath, String? assetDirPath,
bool buildNativeAssets = true,
@visibleForTesting BuildSystem? buildSystem, @visibleForTesting BuildSystem? buildSystem,
}) async { }) async {
project ??= FlutterProject.current(); project ??= FlutterProject.current();
...@@ -60,6 +65,7 @@ class BundleBuilder { ...@@ -60,6 +65,7 @@ class BundleBuilder {
kTargetFile: mainPath, kTargetFile: mainPath,
kDeferredComponents: 'false', kDeferredComponents: 'false',
...buildInfo.toBuildSystemEnvironment(), ...buildInfo.toBuildSystemEnvironment(),
if (!buildNativeAssets) kNativeAssets: 'false'
}, },
artifacts: globals.artifacts!, artifacts: globals.artifacts!,
fileSystem: globals.fs, fileSystem: globals.fs,
......
...@@ -132,6 +132,7 @@ class BuildBundleCommand extends BuildSubCommand { ...@@ -132,6 +132,7 @@ class BuildBundleCommand extends BuildSubCommand {
mainPath: targetFile, mainPath: targetFile,
depfilePath: stringArg('depfile'), depfilePath: stringArg('depfile'),
assetDirPath: stringArg('asset-dir'), assetDirPath: stringArg('asset-dir'),
buildNativeAssets: false,
); );
return FlutterCommandResult.success(); return FlutterCommandResult.success();
} }
......
...@@ -252,6 +252,7 @@ void main() { ...@@ -252,6 +252,7 @@ void main() {
kIconTreeShakerFlag: 'false', kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false', kDeferredComponents: 'false',
kDartObfuscation: 'false', kDartObfuscation: 'false',
kNativeAssets: 'false',
}); });
}), }),
FileSystem: fsFactory, FileSystem: fsFactory,
...@@ -285,6 +286,7 @@ void main() { ...@@ -285,6 +286,7 @@ void main() {
kIconTreeShakerFlag: 'false', kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false', kDeferredComponents: 'false',
kDartObfuscation: 'false', kDartObfuscation: 'false',
kNativeAssets: 'false',
}); });
}), }),
FileSystem: fsFactory, FileSystem: fsFactory,
...@@ -317,6 +319,7 @@ void main() { ...@@ -317,6 +319,7 @@ void main() {
kIconTreeShakerFlag: 'false', kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false', kDeferredComponents: 'false',
kDartObfuscation: 'false', kDartObfuscation: 'false',
kNativeAssets: 'false',
}); });
}), }),
FileSystem: fsFactory, FileSystem: fsFactory,
...@@ -350,6 +353,7 @@ void main() { ...@@ -350,6 +353,7 @@ void main() {
kIconTreeShakerFlag: 'false', kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false', kDeferredComponents: 'false',
kDartObfuscation: 'false', kDartObfuscation: 'false',
kNativeAssets: 'false',
}); });
}), }),
FileSystem: fsFactory, FileSystem: fsFactory,
...@@ -383,6 +387,7 @@ void main() { ...@@ -383,6 +387,7 @@ void main() {
kIconTreeShakerFlag: 'false', kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false', kDeferredComponents: 'false',
kDartObfuscation: 'false', kDartObfuscation: 'false',
kNativeAssets: 'false',
}); });
}), }),
FileSystem: fsFactory, FileSystem: fsFactory,
...@@ -416,6 +421,7 @@ void main() { ...@@ -416,6 +421,7 @@ void main() {
kIconTreeShakerFlag: 'false', kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false', kDeferredComponents: 'false',
kDartObfuscation: 'false', kDartObfuscation: 'false',
kNativeAssets: 'false',
}); });
}), }),
FileSystem: fsFactory, FileSystem: fsFactory,
...@@ -457,6 +463,7 @@ void main() { ...@@ -457,6 +463,7 @@ void main() {
kIconTreeShakerFlag: 'false', kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false', kDeferredComponents: 'false',
kDartObfuscation: 'false', kDartObfuscation: 'false',
kNativeAssets: 'false',
}); });
}), }),
FileSystem: fsFactory, FileSystem: fsFactory,
...@@ -498,6 +505,7 @@ void main() { ...@@ -498,6 +505,7 @@ void main() {
kIconTreeShakerFlag: 'false', kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false', kDeferredComponents: 'false',
kDartObfuscation: 'false', kDartObfuscation: 'false',
kNativeAssets: 'false',
}); });
}), }),
FileSystem: fsFactory, FileSystem: fsFactory,
...@@ -516,7 +524,7 @@ class FakeBundleBuilder extends Fake implements BundleBuilder { ...@@ -516,7 +524,7 @@ class FakeBundleBuilder extends Fake implements BundleBuilder {
String? applicationKernelFilePath, String? applicationKernelFilePath,
String? depfilePath, String? depfilePath,
String? assetDirPath, String? assetDirPath,
Uri? nativeAssets, bool buildNativeAssets = true,
@visibleForTesting BuildSystem? buildSystem, @visibleForTesting BuildSystem? buildSystem,
}) async {} }) async {}
} }
...@@ -645,6 +645,7 @@ class FakeBundleBuilder extends Fake implements BundleBuilder { ...@@ -645,6 +645,7 @@ class FakeBundleBuilder extends Fake implements BundleBuilder {
String? depfilePath, String? depfilePath,
String? assetDirPath, String? assetDirPath,
Uri? nativeAssets, Uri? nativeAssets,
bool buildNativeAssets = true,
@visibleForTesting BuildSystem? buildSystem @visibleForTesting BuildSystem? buildSystem
}) async {} }) async {}
} }
...@@ -196,7 +196,7 @@ class FakeBundleBuilder extends Fake implements BundleBuilder { ...@@ -196,7 +196,7 @@ class FakeBundleBuilder extends Fake implements BundleBuilder {
String? applicationKernelFilePath, String? applicationKernelFilePath,
String? depfilePath, String? depfilePath,
String? assetDirPath, String? assetDirPath,
Uri? nativeAssets, bool buildNativeAssets = true,
@visibleForTesting BuildSystem? buildSystem @visibleForTesting BuildSystem? buildSystem
}) async { }) async {
final Directory assetDirectory = fileSystem final Directory assetDirectory = fileSystem
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment