| #!/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): Improve on this behavior. |
| # --with-engine: Also builds the Flutter Engine alongside the example. |
| # --goma: Uses GOMA when building the Flutter Engine to accelerate the build. |
| # For Googlers only. Sorry. :( |
| # |
| # 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 |
| goma_flags="" |
| app_name="hello_flutter" |
| 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) |
| if ! [ -d "${FUCHSIA_DIR}"/out/default ] |
| then |
| echo-error '--fidl currently relies on:' |
| echo-error ' 1. $FUCHSIA_DIR being set to your Fuchsia source checkout. https://fuchsia.dev/fuchsia-src/get-started/get_fuchsia_source' |
| echo-error ' 2. "$FUCHSIA_DIR/out/default" having a Fuchsia build.' |
| echo-error '... so that the FIDL IR can be read. You can still use a product bundle instead of a build from source.' |
| echo-error |
| echo-error 'If you already have a Fuchsia source checkout at $FUCHSIA_DIR, you can run:' |
| echo 'cd $FUCHSIA_DIR' |
| echo 'fx set workstation_eng.qemu-x64' |
| echo 'fx build' |
| echo-error "... and then try again." |
| exit 1 |
| fi |
| |
| fidl=1 |
| shift # past argument |
| ;; |
| --with-engine) |
| with_engine=1 |
| shift # past argument |
| ;; |
| --goma) |
| goma_flags="--goma" |
| shift # past argument |
| ;; |
| *) |
| app_name="$1" |
| shift # past value |
| ;; |
| esac |
| done |
| |
| 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} |
| 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}" |
| |
| echo-info "Running Flutter sample app." |
| pushd "${FUCHSIA_EMBEDDER_DIR}" |
| "${FUCHSIA_EMBEDDER_DIR}"/tools/ffx repository server start |
| "${FUCHSIA_EMBEDDER_DIR}"/tools/bazel run --config=fuchsia_x64 //src/examples/"${app_name}":"${app_name}"_pkg.component ${session_args} |
| |
| echo-info "Package contents for debugging:" |
| "${FUCHSIA_EMBEDDER_DIR}"/tools/bazel build --config=fuchsia_x64 //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}" -ne 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}" -ne 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 "${FUCHSIA_DIR}"/out/default |
| fi |