blob: 562c22f28307b4ec8d4b1a1ee083dcc352210f3c [file] [log] [blame]
#!/bin/bash
# Copyright 2022 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Usage:
# build_and_run_example.sh <name_of_dir_in_examples>
#
# Requirements:
# 1. You are currently running Workstation (`ffx emu start --headless workstation_eng.qemu-x64`).
# 2. The example folder must contain a fuchsia_package target named `<example>_pkg`.
#
# Arguments:
# --headless: WIP. Runs outside the session for non-graphical examples.
# Does not work yet because there's no appropriate collection for this
# with JIT support.
# --log: Follows logs containing the word "embedder" after running the example.
# --fidl: Follows FIDL calls made by the example process.
# We monitor after starting the example so FIDL calls on startup will not appear.
# TODO(akbiggs): Show FIDL calls from startup.
# --with-engine: Also builds the Flutter Engine alongside the example.
# See https://fuchsia.googlesource.com/flutter-embedder/+/refs/heads/main/docs/making_engine_changes.md for setup.
# --goma: Uses GOMA when building the Flutter Engine to accelerate the build.
# For Googlers only. Sorry. :(
# --no-prebuilt-dart-sdk: Builds the Dart SDK for the Flutter Engine from source
# in third_party/engine/src/third_party/dart instead of
# using a prebuilt Dart SDK.
# --with-fuchsia: Also builds a local Fuchsia SDK alongside the example.
# See https://fuchsia.googlesource.com/flutter-embedder/+/refs/heads/main/docs/testing_fuchsia_sdk_changes.md for setup.
#
# TODO(akbiggs): Port this workflow to Bazel.
set -e # Fail on any error.
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/helpers.sh || exit $?
ensure-embedder-dir
far_debug_dir=/tmp/"${app_name}"_far_contents
far_tool="${FUCHSIA_EMBEDDER_DIR}"/bazel-flutter-embedder/external/fuchsia_sdk/tools/x64/far
# Parse arguments.
headless=0
log=0
fidl=0
with_engine=0
with_fuchsia=0
goma_flags=""
no_prebuilt_dart_sdk_flags=""
app_name="hello_flutter"
cpu="x64"
while [[ $# -gt 0 ]]; do
case $1 in
--headless)
echo-warning "--headless apps will not work yet because there's no collection to run JIT components in."
headless=1
shift # past argument
;;
--log)
log=1
shift # past argument
;;
--fidl)
ensure-local-fuchsia-platform-build
fidl=1
shift # past argument
;;
--with-engine)
with_engine=1
shift # past argument
;;
--with-fuchsia)
ensure-local-fuchsia-platform-build
with_fuchsia=1
shift # past argument
;;
--goma)
goma_flags="--goma"
shift # past argument
;;
--no-prebuilt-dart-sdk)
no_prebuilt_dart_sdk_flags="--no-prebuilt-dart-sdk"
shift # past argument
;;
--cpu)
shift # past argument
cpu="$1"
if [[ "${cpu}" != "x64" && "${cpu}" != "arm64" ]]
then
echo-error "Unknown value for cpu: $cpu. Valid values: x64, arm64"
fi
shift # past value
;;
*)
app_name="$1"
shift # past value
;;
esac
done
if [[ "${with_engine}" == 0 && "${no_prebuilt_dart_sdk_flags}" != "" ]]
then
echo-error "--no-prebuilt-dart-sdk requires --with-engine."
exit 1
fi
if [[ "${with_engine}" == 0 && "${goma_flags}" != "" ]]
then
echo-error "--goma requires --with-engine."
exit 1
fi
if [[ "${log}" != 0 && "${fidl}" != 0 ]]
then
echo-error "Both --log and --fidl require taking over stdout, so they can't both be specified."
exit 1
fi
session_args="-- --session"
release_args=
if [[ "${headless}" != 0 ]]
then
session_args=
release_args="--release"
fi
if [[ "${with_engine}" != 0 ]]
then
"${FUCHSIA_EMBEDDER_DIR}"/scripts/build_and_copy_engine_artifacts.sh ${goma_flags} ${no_prebuilt_dart_sdk_flags} --cpu "${cpu}"
fi
if [[ "${with_fuchsia}" != 0 ]]
then
echo-info "Building Fuchsia SDK from $LOCAL_FUCHSIA_PLATFORM_BUILD..."
# All 'fx' commands must be run in the Fuchsia directory.
pushd "${LOCAL_FUCHSIA_PLATFORM_BUILD}"
"${LOCAL_FUCHSIA_PLATFORM_BUILD}"/../../.jiri_root/bin/fx build sdk
popd # "${LOCAL_FUCHSIA_PLATFORM_BUILD}"
echo-info "Syncing flutter-embedder's Bazel SDK to $LOCAL_FUCHSIA_PLATFORM_BUILD..."
# All bazel commands must be run from our workspace.
pushd "${FUCHSIA_EMBEDDER_DIR}"
"${FUCHSIA_EMBEDDER_DIR}"/tools/bazel sync --configure
popd # "${FUCHSIA_EMBEDDER_DIR}"
fi
echo-info "Building Flutter sample app bundle."
pushd "${FUCHSIA_EMBEDDER_DIR}"/src/examples/"${app_name}"
"${FUCHSIA_EMBEDDER_DIR}"/tools/flutter build bundle
popd # "${FUCHSIA_EMBEDDER_DIR}"/src/examples/"${app_name}"
# All bazel commands must be run from our workspace.
pushd "${FUCHSIA_EMBEDDER_DIR}"
echo-info "Running Flutter sample app."
"${FUCHSIA_EMBEDDER_DIR}"/tools/ffx repository server start
"${FUCHSIA_EMBEDDER_DIR}"/tools/bazel run --config=fuchsia_"${cpu}" //src/examples/"${app_name}":"${app_name}"_pkg.component ${session_args}
echo-info "Package contents for debugging:"
"${FUCHSIA_EMBEDDER_DIR}"/tools/bazel build --config=fuchsia_"${cpu}" //src/examples/"${app_name}":"${app_name}"_pkg
"${far_tool}" extract --archive="$(find -L ${FUCHSIA_EMBEDDER_DIR}/bazel-out -name "${app_name}".far | head -n 1)" --output="${far_debug_dir}"
"${far_tool}" extract --archive="${far_debug_dir}"/meta.far --output="${far_debug_dir}"
cat "${far_debug_dir}"/meta/contents
rm -r "${far_debug_dir}"
popd # $FUCHSIA_EMBEDDER_DIR
if [[ "${log}" != 0 ]]
then
echo-info "Showing ffx log --filter embedder --filter Embedder ... (ctrl+C to exit)"
"${FUCHSIA_EMBEDDER_DIR}"/tools/ffx log --filter embedder --filter Embedder
fi
if [[ "${fidl}" != 0 ]]
then
echo-info 'Showing FIDL logs... (ctrl+C to exit)'
echo-info "(This does not currently catch FIDL calls from app startup)"
"${FUCHSIA_EMBEDDER_DIR}"/tools/ffx debug fidl --remote-name embedder.cm --fidl-ir-path "${LOCAL_FUCHSIA_PLATFORM_BUILD}"
fi