mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

This PR introduces a `NativeAssetsManifest.json` next to the `AssetManifest.bin` and `FontManifest.json`. This removes the need for embedding the native assets mapping inside the kernel file and enables decoupling native assets building and bundling from the kernel compilation in flutter tools. This means `flutter run` no longer does a dry run of `hook/build.dart` hooks. (It also means all isolate groups will have the same native assets. However, since Flutter does not support `Isolate.spawnUri` from kernel files, this is not a regression.) Implementation details: * g3 is still using kernel embedding. https://github.com/flutter/flutter/pull/142016 introduced an argument to embed a `native_assets.yaml` inside `flutter attach` and `flutter run` (the outer flutter process), but it is not used in `flutter assemble` (the inner process when doing `flutter run`). So, those arguments need to still be respected. However, all other logic related to embedding a yaml encoding in the kernel file has been removed. * All dry-run logic has been removed. 🎉 * The `KernelSnapshot` target no longer depends on the `InstallCodeAssets` target. Instead, the various OS-specific "BundleAsset" targets now depend on the `InstallCodeAssets` target. The `InstallCodeAssets` invokes the build hooks and produces the `NativeAssetsManifest.json`. The various "BundleAsset" commands synchronize the `NativeAssetsManifest.json` to the app bundle. * `InstallCodeAssets` produces a `native_assets.json`, which is renamed to `NativeAssetsManifest.json` in the various "Bundle" targets. This means that all unit tests of the "Bundle" targets now need to create this file. (Similar to how `app.dill` is expected to exist because `KernelSnapshot` is a dependency of the "Bundle" targets.) * Because dynamic libraries need to be code signed (at least on iOS and MacOS), the bundling of the dylibs is _not_ migrated to reuse `_updateDevFS` (which is used for ordinary assets). Only the 2nd and 3rd invocation of `flutter assemble` from `xcodebuild` has access to the code signing identity. Relevant tests: * test/integration.shard/isolated/native_assets_test.dart - runs `flutter run` with native assets including hot restart and hot reload. TODO: * Undo engine-roll in this PR after engine has rolled in. Issue: * https://github.com/flutter/flutter/issues/154425 Related PRs: * https://dart-review.googlesource.com/c/sdk/+/388161 * https://github.com/flutter/engine/pull/56727 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [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 [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
246 lines
10 KiB
Bash
Executable File
246 lines
10 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Copyright 2014 The Flutter Authors. All rights reserved.
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
# found in the LICENSE file.
|
|
|
|
# TODO(zanderso): refactor this and xcode_backend.sh into one script
|
|
# once iOS is using 'assemble'.
|
|
RunCommand() {
|
|
if [[ -n "$VERBOSE_SCRIPT_LOGGING" ]]; then
|
|
echo "♦ $*"
|
|
fi
|
|
"$@"
|
|
return $?
|
|
}
|
|
|
|
EchoError() {
|
|
echo "$@" 1>&2
|
|
}
|
|
|
|
ParseFlutterBuildMode() {
|
|
# Use FLUTTER_BUILD_MODE if it's set, otherwise use the Xcode build configuration name
|
|
# This means that if someone wants to use an Xcode build config other than Debug/Profile/Release,
|
|
# they _must_ set FLUTTER_BUILD_MODE so we know what type of artifact to build.
|
|
local build_mode="$(echo "${FLUTTER_BUILD_MODE:-${CONFIGURATION}}" | tr "[:upper:]" "[:lower:]")"
|
|
|
|
case "$build_mode" in
|
|
*release*) build_mode="release";;
|
|
*profile*) build_mode="profile";;
|
|
*debug*) build_mode="debug";;
|
|
*)
|
|
EchoError "========================================================================"
|
|
EchoError "ERROR: Unknown FLUTTER_BUILD_MODE: ${build_mode}."
|
|
EchoError "Valid values are 'Debug', 'Profile', or 'Release' (case insensitive)."
|
|
EchoError "This is controlled by the FLUTTER_BUILD_MODE environment variable."
|
|
EchoError "If that is not set, the CONFIGURATION environment variable is used."
|
|
EchoError ""
|
|
EchoError "You can fix this by either adding an appropriately named build"
|
|
EchoError "configuration, or adding an appropriate value for FLUTTER_BUILD_MODE to the"
|
|
EchoError ".xcconfig file for the current build configuration (${CONFIGURATION})."
|
|
EchoError "========================================================================"
|
|
exit -1;;
|
|
esac
|
|
echo "${build_mode}"
|
|
}
|
|
|
|
BuildApp() {
|
|
# Set the working directory to the project root
|
|
local project_path="${SOURCE_ROOT}/.."
|
|
RunCommand pushd "${project_path}" > /dev/null
|
|
|
|
# Set the target file.
|
|
local target_path="lib/main.dart"
|
|
if [[ -n "$FLUTTER_TARGET" ]]; then
|
|
target_path="${FLUTTER_TARGET}"
|
|
fi
|
|
|
|
# Use FLUTTER_BUILD_MODE if it's set, otherwise use the Xcode build configuration name
|
|
# This means that if someone wants to use an Xcode build config other than Debug/Profile/Release,
|
|
# they _must_ set FLUTTER_BUILD_MODE so we know what type of artifact to build.
|
|
local build_mode="$(ParseFlutterBuildMode)"
|
|
|
|
if [[ -n "$LOCAL_ENGINE" ]]; then
|
|
if [[ $(echo "$LOCAL_ENGINE" | tr "[:upper:]" "[:lower:]") != *"$build_mode"* ]]; then
|
|
EchoError "========================================================================"
|
|
EchoError "ERROR: Requested build with Flutter local engine at '${LOCAL_ENGINE}'"
|
|
EchoError "This engine is not compatible with FLUTTER_BUILD_MODE: '${build_mode}'."
|
|
EchoError "You can fix this by updating the LOCAL_ENGINE environment variable, or"
|
|
EchoError "by running:"
|
|
EchoError " flutter build macos --local-engine=host_${build_mode} --local-engine-host=host_${build_mode}"
|
|
EchoError "or"
|
|
EchoError " flutter build macos --local-engine=host_${build_mode}_unopt --local-engine-host=host_${build_mode}_unopt"
|
|
EchoError "========================================================================"
|
|
exit -1
|
|
fi
|
|
fi
|
|
if [[ -n "$LOCAL_ENGINE_HOST" ]]; then
|
|
if [[ $(echo "$LOCAL_ENGINE_HOST" | tr "[:upper:]" "[:lower:]") != *"$build_mode"* ]]; then
|
|
EchoError "========================================================================"
|
|
EchoError "ERROR: Requested build with Flutter local engine at '${LOCAL_ENGINE_HOST}'"
|
|
EchoError "This engine is not compatible with FLUTTER_BUILD_MODE: '${build_mode}'."
|
|
EchoError "You can fix this by updating the LOCAL_ENGINE_HOST environment variable, or"
|
|
EchoError "by running:"
|
|
EchoError " flutter build macos --local-engine=host_${build_mode} --local-engine-host=host_${build_mode}"
|
|
EchoError "or"
|
|
EchoError " flutter build macos --local-engine=host_${build_mode}_unopt --local-engine-host=host_${build_mode}_unopt"
|
|
EchoError "========================================================================"
|
|
exit -1
|
|
fi
|
|
fi
|
|
|
|
# The path where the input/output xcfilelists are stored. These are used by xcode
|
|
# to conditionally skip this script phase if neither have changed.
|
|
local ephemeral_dir="${SOURCE_ROOT}/Flutter/ephemeral"
|
|
local build_inputs_path="${ephemeral_dir}/FlutterInputs.xcfilelist"
|
|
local build_outputs_path="${ephemeral_dir}/FlutterOutputs.xcfilelist"
|
|
|
|
# Construct the "flutter assemble" argument array. Arguments should be added
|
|
# as quoted string elements of the flutter_args array, otherwise an argument
|
|
# (like a path) with spaces in it might be interpreted as two separate
|
|
# arguments.
|
|
local flutter_args=("${FLUTTER_ROOT}/bin/flutter")
|
|
if [[ -n "$VERBOSE_SCRIPT_LOGGING" ]]; then
|
|
flutter_args+=('--verbose')
|
|
fi
|
|
if [[ -n "$FLUTTER_ENGINE" ]]; then
|
|
flutter_args+=("--local-engine-src-path=${FLUTTER_ENGINE}")
|
|
fi
|
|
if [[ -n "$LOCAL_ENGINE" ]]; then
|
|
flutter_args+=("--local-engine=${LOCAL_ENGINE}")
|
|
fi
|
|
if [[ -n "$LOCAL_ENGINE_HOST" ]]; then
|
|
flutter_args+=("--local-engine-host=${LOCAL_ENGINE_HOST}")
|
|
fi
|
|
|
|
local architectures="${ARCHS}"
|
|
if [[ -n "$1" && "$1" == "prepare" ]]; then
|
|
# The "prepare" command runs in a pre-action script, which doesn't always
|
|
# filter the "ARCHS" build setting to only the active arch. To workaround,
|
|
# if "ONLY_ACTIVE_ARCH" is true and the "NATIVE_ARCH" is arm, assume the
|
|
# active arch is also arm to improve caching. If this assumption is
|
|
# incorrect, it will later be corrected by the "build" command.
|
|
if [[ -n "$ONLY_ACTIVE_ARCH" && "$ONLY_ACTIVE_ARCH" == "YES" && -n "$NATIVE_ARCH" ]]; then
|
|
if [[ "$NATIVE_ARCH" == *"arm"* ]]; then
|
|
architectures="arm64"
|
|
else
|
|
architectures="x86_64"
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
flutter_args+=(
|
|
"assemble"
|
|
"--no-version-check"
|
|
"-dTargetPlatform=darwin"
|
|
"-dDarwinArchs=${architectures}"
|
|
"-dTargetFile=${target_path}"
|
|
"-dBuildMode=${build_mode}"
|
|
"-dTreeShakeIcons=${TREE_SHAKE_ICONS}"
|
|
"-dDartObfuscation=${DART_OBFUSCATION}"
|
|
"-dSplitDebugInfo=${SPLIT_DEBUG_INFO}"
|
|
"-dTrackWidgetCreation=${TRACK_WIDGET_CREATION}"
|
|
"-dAction=${ACTION}"
|
|
"-dFrontendServerStarterPath=${FRONTEND_SERVER_STARTER_PATH}"
|
|
"--DartDefines=${DART_DEFINES}"
|
|
"--ExtraGenSnapshotOptions=${EXTRA_GEN_SNAPSHOT_OPTIONS}"
|
|
"--ExtraFrontEndOptions=${EXTRA_FRONT_END_OPTIONS}"
|
|
"--build-inputs=${build_inputs_path}"
|
|
"--build-outputs=${build_outputs_path}"
|
|
"--output=${BUILT_PRODUCTS_DIR}"
|
|
)
|
|
|
|
local target="${build_mode}_macos_bundle_flutter_assets";
|
|
if [[ -n "$1" && "$1" == "prepare" ]]; then
|
|
# The "prepare" command only targets the UnpackMacOS target, which copies the
|
|
# FlutterMacOS framework to the BUILT_PRODUCTS_DIR.
|
|
target="${build_mode}_unpack_macos"
|
|
|
|
# Use the PreBuildAction define flag to force the tool to use a different
|
|
# filecache file for the "prepare" command. This will make the environment
|
|
# buildPrefix for the "prepare" command unique from the "build" command.
|
|
# This will improve caching since the "build" command has more target dependencies.
|
|
flutter_args+=("-dPreBuildAction=PrepareFramework")
|
|
fi
|
|
|
|
if [[ -n "$FLAVOR" ]]; then
|
|
flutter_args+=("-dFlavor=${FLAVOR}")
|
|
fi
|
|
if [[ -n "$PERFORMANCE_MEASUREMENT_FILE" ]]; then
|
|
flutter_args+=("--performance-measurement-file=${PERFORMANCE_MEASUREMENT_FILE}")
|
|
fi
|
|
if [[ -n "$BUNDLE_SKSL_PATH" ]]; then
|
|
flutter_args+=("-dBundleSkSLPath=${BUNDLE_SKSL_PATH}")
|
|
fi
|
|
if [[ -n "$CODE_SIZE_DIRECTORY" ]]; then
|
|
flutter_args+=("-dCodeSizeDirectory=${CODE_SIZE_DIRECTORY}")
|
|
fi
|
|
|
|
flutter_args+=("${target}")
|
|
|
|
RunCommand "${flutter_args[@]}"
|
|
}
|
|
|
|
PrepareFramework() {
|
|
# The "prepare" command runs in a pre-action script, which also runs when
|
|
# using the Xcode/xcodebuild clean command. Skip if cleaning.
|
|
if [[ $ACTION == "clean" ]]; then
|
|
exit 0
|
|
fi
|
|
BuildApp "prepare"
|
|
}
|
|
|
|
# Adds the App.framework as an embedded binary, the flutter_assets as
|
|
# resources, and the native assets.
|
|
EmbedFrameworks() {
|
|
# Embed App.framework from Flutter into the app (after creating the Frameworks directory
|
|
# if it doesn't already exist).
|
|
local xcode_frameworks_dir="${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}"
|
|
RunCommand mkdir -p -- "${xcode_frameworks_dir}"
|
|
RunCommand rsync -av --delete --filter "- .DS_Store" "${BUILT_PRODUCTS_DIR}/App.framework" "${xcode_frameworks_dir}"
|
|
|
|
# Embed the actual FlutterMacOS.framework that the Flutter app expects to run against,
|
|
# which could be a local build or an arch/type specific build.
|
|
|
|
# Copy Xcode behavior and don't copy over headers or modules.
|
|
RunCommand rsync -av --delete --filter "- .DS_Store" --filter "- Headers" --filter "- Modules" "${BUILT_PRODUCTS_DIR}/FlutterMacOS.framework" "${xcode_frameworks_dir}/"
|
|
|
|
# Sign the binaries we moved.
|
|
if [[ -n "${EXPANDED_CODE_SIGN_IDENTITY:-}" ]]; then
|
|
RunCommand codesign --force --verbose --sign "${EXPANDED_CODE_SIGN_IDENTITY}" -- "${xcode_frameworks_dir}/App.framework/App"
|
|
RunCommand codesign --force --verbose --sign "${EXPANDED_CODE_SIGN_IDENTITY}" -- "${xcode_frameworks_dir}/FlutterMacOS.framework/FlutterMacOS"
|
|
fi
|
|
|
|
# Copy the native assets. These do not have to be codesigned here because,
|
|
# they are already codesigned in buildNativeAssetsMacOS.
|
|
local project_path="${SOURCE_ROOT}/.."
|
|
if [[ -n "$FLUTTER_APPLICATION_PATH" ]]; then
|
|
project_path="${FLUTTER_APPLICATION_PATH}"
|
|
fi
|
|
local native_assets_path="${project_path}/${FLUTTER_BUILD_DIR}/native_assets/macos/"
|
|
if [[ -d "$native_assets_path" ]]; then
|
|
RunCommand rsync -av --filter "- .DS_Store" --filter "- native_assets.yaml" --filter "- native_assets.json" "${native_assets_path}" "${xcode_frameworks_dir}"
|
|
|
|
# Iterate through all .frameworks in native assets directory.
|
|
for native_asset in "${native_assets_path}"*.framework; do
|
|
[ -e "$native_asset" ] || continue # Skip when there are no matches.
|
|
# Codesign the framework inside the app bundle.
|
|
RunCommand codesign --force --verbose --sign "${EXPANDED_CODE_SIGN_IDENTITY}" -- "${xcode_frameworks_dir}/$(basename "$native_asset")"
|
|
done
|
|
fi
|
|
}
|
|
|
|
# Main entry point.
|
|
if [[ $# == 0 ]]; then
|
|
# Unnamed entry point defaults to build.
|
|
BuildApp
|
|
else
|
|
case $1 in
|
|
"build")
|
|
BuildApp ;;
|
|
"embed")
|
|
EmbedFrameworks ;;
|
|
"prepare")
|
|
PrepareFramework ;;
|
|
esac
|
|
fi
|