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';
/// Whether to enable Dart obfuscation and where to save the symbol map.
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.
const String kCodeSizeDirectory = 'CodeSizeDirectory';
......
......@@ -48,157 +48,163 @@ class NativeAssets extends Target {
@override
Future<void> build(Environment environment) async {
final String? targetPlatformEnvironment = environment.defines[kTargetPlatform];
if (targetPlatformEnvironment == null) {
throw MissingDefineException(kTargetPlatform, name);
}
final TargetPlatform targetPlatform = getTargetPlatformForName(targetPlatformEnvironment);
final Uri projectUri = environment.projectDir.uri;
final String? nativeAssetsEnvironment = environment.defines[kNativeAssets];
final List<Uri> dependencies;
final FileSystem fileSystem = environment.fileSystem;
final File packagesFile = fileSystem
.directory(projectUri)
.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 File nativeAssetsFile = environment.buildDir.childFile('native_assets.yaml');
if (nativeAssetsEnvironment == 'false') {
dependencies = <Uri>[];
await writeNativeAssetsYaml(<Asset>[], environment.buildDir.uri, fileSystem);
} else {
final String? targetPlatformEnvironment = environment.defines[kTargetPlatform];
if (targetPlatformEnvironment == null) {
throw MissingDefineException(kTargetPlatform, name);
}
final TargetPlatform targetPlatform = getTargetPlatformForName(targetPlatformEnvironment);
final Uri projectUri = environment.projectDir.uri;
final File packagesFile = fileSystem
.directory(projectUri)
.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) {
case TargetPlatform.ios:
final String? iosArchsEnvironment = environment.defines[kIosArchs];
if (iosArchsEnvironment == null) {
throw MissingDefineException(kIosArchs, name);
}
final List<DarwinArch> iosArchs = iosArchsEnvironment.split(' ').map(getDarwinArchForName).toList();
final String? environmentBuildMode = environment.defines[kBuildMode];
if (environmentBuildMode == null) {
throw MissingDefineException(kBuildMode, name);
}
final BuildMode buildMode = BuildMode.fromCliName(environmentBuildMode);
final String? sdkRoot = environment.defines[kSdkRoot];
if (sdkRoot == null) {
throw MissingDefineException(kSdkRoot, name);
}
final EnvironmentType environmentType = environmentTypeFromSdkroot(sdkRoot, environment.fileSystem)!;
dependencies = await buildNativeAssetsIOS(
environmentType: environmentType,
darwinArchs: iosArchs,
buildMode: buildMode,
projectUri: projectUri,
codesignIdentity: environment.defines[kCodesignIdentity],
fileSystem: fileSystem,
buildRunner: buildRunner,
yamlParentDirectory: environment.buildDir.uri,
);
case TargetPlatform.darwin:
final String? darwinArchsEnvironment = environment.defines[kDarwinArchs];
if (darwinArchsEnvironment == null) {
throw MissingDefineException(kDarwinArchs, name);
}
final List<DarwinArch> darwinArchs = darwinArchsEnvironment.split(' ').map(getDarwinArchForName).toList();
final String? environmentBuildMode = environment.defines[kBuildMode];
if (environmentBuildMode == null) {
throw MissingDefineException(kBuildMode, name);
}
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) {
switch (targetPlatform) {
case TargetPlatform.ios:
final String? iosArchsEnvironment = environment.defines[kIosArchs];
if (iosArchsEnvironment == null) {
throw MissingDefineException(kIosArchs, name);
}
final List<DarwinArch> iosArchs = iosArchsEnvironment.split(' ').map(getDarwinArchForName).toList();
final String? environmentBuildMode = environment.defines[kBuildMode];
if (environmentBuildMode == null) {
throw MissingDefineException(kBuildMode, name);
}
final BuildMode buildMode = BuildMode.fromCliName(environmentBuildMode);
final String? sdkRoot = environment.defines[kSdkRoot];
if (sdkRoot == null) {
throw MissingDefineException(kSdkRoot, name);
}
final EnvironmentType environmentType = environmentTypeFromSdkroot(sdkRoot, environment.fileSystem)!;
dependencies = await buildNativeAssetsIOS(
environmentType: environmentType,
darwinArchs: iosArchs,
buildMode: buildMode,
projectUri: projectUri,
codesignIdentity: environment.defines[kCodesignIdentity],
fileSystem: fileSystem,
buildRunner: buildRunner,
yamlParentDirectory: environment.buildDir.uri,
);
case TargetPlatform.darwin:
final String? darwinArchsEnvironment = environment.defines[kDarwinArchs];
if (darwinArchsEnvironment == null) {
throw MissingDefineException(kDarwinArchs, name);
}
final List<DarwinArch> darwinArchs = darwinArchsEnvironment.split(' ').map(getDarwinArchForName).toList();
final String? environmentBuildMode = environment.defines[kBuildMode];
if (environmentBuildMode == null) {
throw MissingDefineException(kBuildMode, name);
}
final BuildMode buildMode = BuildMode.fromCliName(environmentBuildMode);
(_, dependencies) = await buildNativeAssetsMacOS(
buildMode: BuildMode.debug,
darwinArchs: darwinArchs,
buildMode: buildMode,
projectUri: projectUri,
codesignIdentity: environment.defines[kCodesignIdentity],
yamlParentDirectory: environment.buildDir.uri,
fileSystem: fileSystem,
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(
buildMode: BuildMode.debug,
targetPlatform: targetPlatform,
buildMode: buildMode,
projectUri: projectUri,
yamlParentDirectory: environment.buildDir.uri,
fileSystem: fileSystem,
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(
buildMode: BuildMode.debug,
targetPlatform: targetPlatform,
buildMode: buildMode,
projectUri: projectUri,
yamlParentDirectory: environment.buildDir.uri,
fileSystem: fileSystem,
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
// 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
// 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(
<File>[
for (final Uri dependency in dependencies) fileSystem.file(dependency),
......
......@@ -26,7 +26,11 @@ class BundleBuilder {
/// Builds the bundle for the given target platform.
///
/// 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({
required TargetPlatform platform,
required BuildInfo buildInfo,
......@@ -36,6 +40,7 @@ class BundleBuilder {
String? applicationKernelFilePath,
String? depfilePath,
String? assetDirPath,
bool buildNativeAssets = true,
@visibleForTesting BuildSystem? buildSystem,
}) async {
project ??= FlutterProject.current();
......@@ -60,6 +65,7 @@ class BundleBuilder {
kTargetFile: mainPath,
kDeferredComponents: 'false',
...buildInfo.toBuildSystemEnvironment(),
if (!buildNativeAssets) kNativeAssets: 'false'
},
artifacts: globals.artifacts!,
fileSystem: globals.fs,
......
......@@ -132,6 +132,7 @@ class BuildBundleCommand extends BuildSubCommand {
mainPath: targetFile,
depfilePath: stringArg('depfile'),
assetDirPath: stringArg('asset-dir'),
buildNativeAssets: false,
);
return FlutterCommandResult.success();
}
......
......@@ -252,6 +252,7 @@ void main() {
kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false',
kDartObfuscation: 'false',
kNativeAssets: 'false',
});
}),
FileSystem: fsFactory,
......@@ -285,6 +286,7 @@ void main() {
kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false',
kDartObfuscation: 'false',
kNativeAssets: 'false',
});
}),
FileSystem: fsFactory,
......@@ -317,6 +319,7 @@ void main() {
kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false',
kDartObfuscation: 'false',
kNativeAssets: 'false',
});
}),
FileSystem: fsFactory,
......@@ -350,6 +353,7 @@ void main() {
kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false',
kDartObfuscation: 'false',
kNativeAssets: 'false',
});
}),
FileSystem: fsFactory,
......@@ -383,6 +387,7 @@ void main() {
kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false',
kDartObfuscation: 'false',
kNativeAssets: 'false',
});
}),
FileSystem: fsFactory,
......@@ -416,6 +421,7 @@ void main() {
kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false',
kDartObfuscation: 'false',
kNativeAssets: 'false',
});
}),
FileSystem: fsFactory,
......@@ -457,6 +463,7 @@ void main() {
kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false',
kDartObfuscation: 'false',
kNativeAssets: 'false',
});
}),
FileSystem: fsFactory,
......@@ -498,6 +505,7 @@ void main() {
kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false',
kDartObfuscation: 'false',
kNativeAssets: 'false',
});
}),
FileSystem: fsFactory,
......@@ -516,7 +524,7 @@ class FakeBundleBuilder extends Fake implements BundleBuilder {
String? applicationKernelFilePath,
String? depfilePath,
String? assetDirPath,
Uri? nativeAssets,
bool buildNativeAssets = true,
@visibleForTesting BuildSystem? buildSystem,
}) async {}
}
......@@ -645,6 +645,7 @@ class FakeBundleBuilder extends Fake implements BundleBuilder {
String? depfilePath,
String? assetDirPath,
Uri? nativeAssets,
bool buildNativeAssets = true,
@visibleForTesting BuildSystem? buildSystem
}) async {}
}
......@@ -196,7 +196,7 @@ class FakeBundleBuilder extends Fake implements BundleBuilder {
String? applicationKernelFilePath,
String? depfilePath,
String? assetDirPath,
Uri? nativeAssets,
bool buildNativeAssets = true,
@visibleForTesting BuildSystem? buildSystem
}) async {
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