[embedder] Add fidlcat support to workflow.

Getting started with it is annoying if you
don't already have a build in $FUCHSIA_DIR, but
it's a start.

Change-Id: Ib1987ca3c6f55adbe73c74367a2cd9d8f386b4c9
Reviewed-on: https://fuchsia-review.googlesource.com/c/flutter-embedder/+/738133
Reviewed-by: Ben Bergkamp <benbergkamp@google.com>
Reviewed-by: Naud Ghebre <naudzghebre@google.com>
diff --git a/README.md b/README.md
index 45016b0..cf71c32 100644
--- a/README.md
+++ b/README.md
@@ -47,6 +47,10 @@
    $FUCHSIA_EMBEDDER_DIR/tools/ffx emu start --headless workstation_eng.qemu-x64
    ```
 
+   The workflow below also supports
+   [Fuchsia builds from source](https://fuchsia.dev/fuchsia-src/get-started/get_fuchsia_source)
+   that are within Fuchsia's SDK compatibility window (six weeks).
+
 2. Set the default target in ffx to be `fuchsia-emulator`:
 
    ```sh
@@ -67,6 +71,25 @@
 
      If you want to watch all logs, run `$FUCHSIA_EMBEDDER_DIR/tools/ffx log` in a separate terminal.
 
+   - To watch FIDL calls for the example component, run:
+
+     ```sh
+     $FUCHSIA_EMBEDDER_DIR/scripts/build_and_run_example.sh hello_flutter --fidl
+     ```
+
+     Caveats:
+
+      - `--fidl` currently requires setting `$FUCHSIA_DIR` to a
+      [Fuchsia source checkout](https://fuchsia.dev/fuchsia-src/get-started/get_fuchsia_source)
+      with a build in `$FUCHSIA_DIR/out/default`. This directory is only
+      used to read IR files for FIDL, you can still use the
+      `workstation_eng.qemu-x64` product bundle with `--fidl`.
+
+      - `--fidl` attaches after the component has been run, so it does not pick up
+      FIDL calls made when the component starts.
+
+     **TODO(akbiggs): Work with Bazel SDK team to get proper support for `--fidl`.**
+
 **TODO(akbiggs): The app occasionally gets stuck on a loading screen
 instead of rendering. Re-running the app usually fixes it. We need to
 fix this.**
diff --git a/scripts/build_and_run_example.sh b/scripts/build_and_run_example.sh
index 4c97d1b..d1a8580 100755
--- a/scripts/build_and_run_example.sh
+++ b/scripts/build_and_run_example.sh
@@ -10,6 +10,15 @@
 #   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.
+#
 # TODO(akbiggs): Port this workflow to Bazel.
 
 set -e # Fail on any error.
@@ -23,6 +32,7 @@
 # Parse arguments.
 headless=0
 log=0
+fidl=0
 app_name="hello_flutter"
 while [[ $# -gt 0 ]]; do
   case $1 in
@@ -35,6 +45,25 @@
       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
+      ;;
     *)
       app_name="$1"
       shift # past value
@@ -42,6 +71,12 @@
   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}" -ne 0 ]]
@@ -74,3 +109,10 @@
   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