[embedder] Bump SDK to latest.

Workflow changes:

- Switch to `scripts/bootstrap.sh` instead of installing Bazel manually.
- Use tools/bazel when building examples.
- Remove flutter_toolchain stuff, it's removed from the SDK and
  we're grabbing flutter as a git submodule right now anyways.

Tested: `scripts/build_and_run_example hello_flutter` with new SDK.
Change-Id: Ia46c4b972756b4f1445ddf62c8f9a189920e8f7d
diff --git a/.gitignore b/.gitignore
index 3b683b7..a9f0212 100644
--- a/.gitignore
+++ b/.gitignore
@@ -27,3 +27,13 @@
 
 # Files created by CCLS (C++ language server).
 .ccls-cache
+
+# CIPD files.
+.cipd
+
+# Files generated by bootstrap.sh.
+tools/.versions/
+.cipd_client
+
+# Tools that are automatically generated.
+tools/bazel
diff --git a/README.md b/README.md
index 4186e7a..d11be1b 100644
--- a/README.md
+++ b/README.md
@@ -10,9 +10,8 @@
 
 ## Requirements
 
-1. Install a Bazel version no older than the one defined in the
-   [WORKSPACE.bazel](WORKSPACE.bazel#23) file, by following the instructions at https://bazel.build/install.
-   In some Linux distributions, `sudo apt-get install bazel` should be enough.
+1. Set `$FUCHSIA_EMBEDDER_DIR` to your flutter-embedder.git checkout location,
+   for example `~/flutter-embedder`.
 
 2. Make sure this repository has the required submodules:
 
@@ -20,7 +19,9 @@
    git submodule update --recursive --init
    ```
 
-3. Ensure that there are Fuchsia SSH keys in your host machine. You will need
+3. Run `$FUCHSIA_EMBEDDER_DIR/scripts/bootstrap.sh`.
+
+4. Ensure that there are Fuchsia SSH keys in your host machine. You will need
    them for running the Fuchsia emulator.
 
    ```sh
@@ -29,33 +30,40 @@
     [[ -f "${HOME}/.ssh/fuchsia_authorized_keys" ]] || ssh-keygen -y -f "${HOME}/.ssh/fuchsia_ed25519" > "${HOME}/.ssh/fuchsia_authorized_keys"
    ```
 
-4. Set `$FUCHSIA_EMBEDDER_DIR` to your flutter-embedder.git checkout location,
-   for example `~/flutter-embedder`.
-
 ## Build and package the sample
 
 Now the repository is ready to build the sample.
 
-1. Fetch an emulator image and start an emulator:
+1. Fetch an emulator image:
 
    ```sh
-   tools/ffx product-bundle get workstation.qemu-x64
-   # If running in a graphical environment:
-   tools/ffx emu start workstation.qemu-x64
-   # If running in a terminal environment:
-   tools/ffx emu start --headless workstation.qemu-x64
+   $FUCHSIA_EMBEDDER_DIR/tools/ffx product-bundle get workstation_eng.qemu-x64
    ```
 
-2. (optional) Watch the device log in a separate window:
+2. Start the emulator.
+
+   If running in a graphical environment:
 
    ```sh
-   tools/ffx log
+   $FUCHSIA_EMBEDDER_DIR/tools/ffx emu start workstation_eng.qemu-x64
    ```
 
-3. Run an example app:
+   If running in a terminal environment:
 
    ```sh
-   scripts/build_and_run_example.sh hello_flutter
+   $FUCHSIA_EMBEDDER_DIR/tools/ffx emu start --headless workstation_eng.qemu-x64
+   
+   ```
+3. (optional) Watch the device log in a separate window:
+
+   ```sh
+   $FUCHSIA_EMBEDDER_DIR/tools/ffx log
+   ```
+
+4. Run an example app:
+
+   ```sh
+   $FUCHSIA_EMBEDDER_DIR/scripts/build_and_run_example.sh hello_flutter
    ```
 
 **TODO(akbiggs): The app occasionally gets stuck on a loading screen
@@ -89,12 +97,12 @@
 that revision.
 
 ```sh
-scripts/sync_engine_artifacts_to_revision.sh <ENGINE_COMMIT_SHA>
+$FUCHSIA_EMBEDDER_DIR/scripts/sync_engine_artifacts_to_revision.sh <ENGINE_COMMIT_SHA>
 ```
 
 A common workflow is to sync your Engine commit to the Flutter tool in this repository,
 for example when updating the Flutter tool to a new version. To do this:
 
 ```sh
-scripts/sync_engine_artifacts_to_revision.sh $(cat $FUCHSIA_EMBEDDER_DIR/third_party/dart-pkg/internal/flutter/flutter/bin/internal/engine.version)
+$FUCHSIA_EMBEDDER_DIR/scripts/sync_engine_artifacts_to_revision.sh $(cat $FUCHSIA_EMBEDDER_DIR/third_party/dart-pkg/internal/flutter/flutter/bin/internal/engine.version)
 ```
diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel
index a889f19..4827e23 100644
--- a/WORKSPACE.bazel
+++ b/WORKSPACE.bazel
@@ -3,6 +3,25 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
+workspace(name = "embedder")
+
+load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
+
+http_archive(
+       name = "bazel_skylib",
+          urls = [
+                     "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.2.1/bazel-skylib-1.2.1.tar.gz",
+                            "https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.1/bazel-skylib-1.2.1.tar.gz",
+                        ],
+          sha256 = "f7be3474d42aae265405a592bb7da8e171919d74c16f082a5457840f06054728",
+       )
+
+load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
+bazel_skylib_workspace()
+
+load("@bazel_skylib//lib:versions.bzl", "versions")
+versions.check(minimum_bazel_version = "5.1.0")
+
 local_repository(
     name = "rules_fuchsia",
     path = "third_party/sdk-integration/bazel_rules_fuchsia",
@@ -17,32 +36,23 @@
 
 rules_fuchsia_deps()
 
-load(
-    "@rules_fuchsia//fuchsia:workspace_setup.bzl",
-    "rules_fuchsia_workspace_setup",
-)
-
-rules_fuchsia_workspace_setup()
-
+# To upgrade the Fuchsia SDK:
+# 1. `cd $FUCHSIA_EMBEDDER_DIR/third_party/sdk-integration && git fetch && git pull origin main`
+# 2. Run `bazel build --config=fuchsia_x64 //src/embedder` or any other Bazel command to
+#    fetch the latest SDK, which will upgrade `tools/ffx`.
+#
+# After upgrading the SDK, you should restart any running emulators and package servers
+# and update your product bundles (`ffx product-bundle get workstation_eng.qemu-x64`), as
+# old images may be incompatible with the upgraded SDK.
 fuchsia_sdk_repository(
     name = "fuchsia_sdk",
-    cipd_tag = "version:8.20220520.1.1",
-    sha256 = {
-        "linux": "1700f5e03daeb34e950912f225cacf737f8ebcfef24dfe9051444a3c99e7e935",
-        "mac": "1503e6b4578073df6afcd4c7a9e3a58d747cd7a5b0ebb00f7602745f70d96eee",
-    },
 )
 
 register_toolchains("@fuchsia_sdk//:fuchsia_toolchain_sdk")
 
 fuchsia_clang_repository(
     name = "fuchsia_clang",
-    cipd_tag = "git_revision:80bebbc7cb77979ef9d229450b7ea84e3e9c6a5a",
     sdk_root_label = "@fuchsia_sdk",
-    sha256 = {
-        "linux": "3e1b7cacf697943aefc512f7cf4a4476e40a9dad937f268222ec5afd386d17a4",
-        "mac": "76d64ec3daaeb72c01061817affdce3bdac37a5ec47d931a8cefdc807d0fad90",
-    },
 )
 
 # gTest.
@@ -50,14 +60,3 @@
     name = "com_google_googletest",
     path = "third_party/googletest",
 )
-
-# Flutter.
-load(
-    "@rules_fuchsia//flutter:deps.bzl",
-    "flutter_register_toolchains",
-)
-
-flutter_register_toolchains(
-    dart_tag = "git_revision:e59c04ff550d5343f626a0fca4006e4b1e6c09da",
-    flutter_tag = "git_revision:f9c6208687e0d1796710d93862b170292c3e5ee1",
-)
diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh
new file mode 120000
index 0000000..925a6b1
--- /dev/null
+++ b/scripts/bootstrap.sh
@@ -0,0 +1 @@
+../third_party/sdk-integration/bazel_rules_fuchsia/bootstrap/bootstrap.sh
\ No newline at end of file
diff --git a/scripts/build_and_copy_engine_artifacts.sh b/scripts/build_and_copy_engine_artifacts.sh
index 29b2b88..e99f074 100755
--- a/scripts/build_and_copy_engine_artifacts.sh
+++ b/scripts/build_and_copy_engine_artifacts.sh
@@ -34,7 +34,7 @@
 echo-info "Building the debug Flutter Engine embedding for Fuchsia (libflutter_engine.so)..."
 "${ENGINE_DIR}"/flutter/tools/gn --fuchsia --embedder-for-target --unopt
 debug_out_dir="${ENGINE_DIR}"/out/fuchsia_debug_unopt_x64
-"${DEPOT_TOOLS}"/ninja -C "${debug_out_dir}" flutter/shell/platform/fuchsia
+"${DEPOT_TOOLS}"/ninja -C "${debug_out_dir}"
 
 echo-info "Copying debug Flutter Engine artifacts to ${FUCHSIA_EMBEDDER_DIR}/src/embedder/engine/debug_x64..."
 cp "${debug_out_dir}"/libflutter_engine.so "${FUCHSIA_EMBEDDER_DIR}"/src/embedder/engine/debug_x64/libflutter_engine.so
diff --git a/scripts/build_and_run_example.sh b/scripts/build_and_run_example.sh
index 523bdcf..4270816 100755
--- a/scripts/build_and_run_example.sh
+++ b/scripts/build_and_run_example.sh
@@ -5,7 +5,7 @@
 #
 # Prerequisites:
 #   1. $FUCHSIA_EMBEDDER_DIR is set to your flutter-embedder.git checkout directory.
-#   2. You are currently running Workstation (`ffx emu start --headless workstation.qemu-x64`).
+#   2. You are currently running Workstation (`ffx emu start --headless workstation_eng.qemu-x64`).
 #
 # TODO(akbiggs): Port this workflow to Bazel.
 
@@ -60,7 +60,7 @@
 
 echo-info "Running Flutter sample app."
 cd "${FUCHSIA_EMBEDDER_DIR}"
-bazel run --config=fuchsia_x64 //src/examples/"${app_name}":"${app_name}"_pkg.component ${session_args}
+"${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:"
 "${far_tool}" extract --archive="${FUCHSIA_EMBEDDER_DIR}"/bazel-bin/src/examples/"${app_name}"/"${app_name}".far --output="${far_debug_dir}"
diff --git a/src/examples/README.md b/src/examples/README.md
index 1b2d666..9a6a58a 100644
--- a/src/examples/README.md
+++ b/src/examples/README.md
@@ -3,8 +3,8 @@
 These are example apps that can be used to manually
 test the behavior of the Flutter embedder.
 
-TODO(akbiggs): Write integration tests that automatically
-verify the behavior of these example apps on each commit.
+**TODO(akbiggs): Write integration tests that automatically
+verify the behavior of these example apps on each commit.**
 
 ## How to run an example
 
@@ -36,8 +36,8 @@
 example being called `<example_folder_name>_pkg`.
 
 **TODO(akbiggs): Migrate this workflow to Bazel and remove
-`build_and_run_example.sh`. The ideal workflow is just
-`blaze run //src/examples/my_example/my_example_pkg` because
+`build_and_run_example.sh`. The ideal workflow is
+`$FUCHSIA_EMBEDDER_DIR/tools/bazel run //src/examples/my_example/my_example_pkg` because
 it's consistent with other platforms and discoverable.**
 
 **TODO(akbiggs): Headless examples will not work yet because
diff --git a/third_party/sdk-integration b/third_party/sdk-integration
index eba8db7..bf0ce28 160000
--- a/third_party/sdk-integration
+++ b/third_party/sdk-integration
@@ -1 +1 @@
-Subproject commit eba8db745bddc4fefeb4692ba837caa85d387a08
+Subproject commit bf0ce2880eebb8d321ff88c28060cda61c2255b5
diff --git a/tools/ffx b/tools/ffx
index 1952e07..ade000e 120000
--- a/tools/ffx
+++ b/tools/ffx
@@ -1 +1 @@
-../bazel-flutter-embedder/external/fuchsia_sdk/tools/x64/ffx
\ No newline at end of file
+../third_party/sdk-integration/bazel_rules_fuchsia/tools/run_sdk_tool.sh
\ No newline at end of file