Merge "bundling: Remove the shell script that created CPIO bundles"
diff --git a/build-sparrow.sh b/build-sparrow.sh
index 449a07e..3ccd25f 100755
--- a/build-sparrow.sh
+++ b/build-sparrow.sh
@@ -79,6 +79,7 @@
 #   awkward; maybe add fallback/defaults in the build glue
 
 export ROOTDIR="$(pwd)"
+export KATA_RUST_VERSION="nightly-2021-11-05"
 export SEL4_DIR="${ROOTDIR}/kernel"
 export SEL4_OUT_DIR="${ROOTDIR}/${BUILD_DIR}/kernel"
 
@@ -88,27 +89,63 @@
 # tar xf gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu.tar.xz
 # PATH=~/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/bin:$PATH
 
-# NB: use an existing toolchain but make sure the necessary target is installed
-echo "If your rust toolchain is not setup use something like:"
-echo "rustup target add --toolchain nightly-2021-11-05-x86_64-unknown-linux-gnu ${RUST_TARGET}"
+rustup --version >/dev/null 2>&1 || {
+    cat <<EOF
+It appears you don't have the rust installer on this system; please check here:
+
+    https://www.rust-lang.org/tools/install
+
+for help getting started.
+EOF
+    exit 1
+}
+rustup toolchain list | grep -q "${KATA_RUST_VERSION}" || {
+    cat <<EOF
+It appears your rust toolchains do not include version ${KATA_RUST_VERSION}, this is
+required to build this software. Use something like:
+
+    rustup toolchain add ${KATA_RUST_VERSION}
+
+to add the necessary version.,
+EOF
+    exit 1
+}
+rustup "+${KATA_RUST_VERSION}" target list | grep installed | grep -q "${RUST_TARGET}" || {
+    cat <<EOF
+It appears your rust toolchain is not setup for ${RUST_TARGET}, use something like:
+
+    rustup target add --toolchain ${KATA_RUST_VERSION}-x86_64-unknown-linux-gnu ${RUST_TARGET}
+
+to add support for ${RUST_TARGET}.
+EOF
+    exit 1
+}
+
+# TODO(sleffler): this is slow, maybe cache result
+pip list 2>/dev/null | grep -q -i tempita || {
+    cat <<EOF
+It appears your python setup lacks the tempita module; this is necessary to build this software.
+Something like:
+
+    pip install tempita
+
+should install the necessary module.
+EOF
+}
 
 # Run cmake to build the ninja files
 test -f ${BUILD_DIR}/build.ninja || {
     mkdir -p ${BUILD_DIR}
-    pushd ${BUILD_DIR}
-    ../init-build.sh \
+    (cd ${BUILD_DIR} && ../init-build.sh \
         -DCROSS_COMPILER_PREFIX=${CROSS_COMPILER_PREFIX} \
         -DRUST_TARGET=${RUST_TARGET} \
         -DPLATFORM=${PLATFORM} \
         -DCAPDL_LOADER_APP=kata-os-rootserver \
         -DSIMULATION=TRUE \
-        ${EXTRA_INIT_ARGS}
-    popd # ${BUILD_DIR}
+        ${EXTRA_INIT_ARGS})
 }
 
 # Run ninja to do the actual build
-pushd ${BUILD_DIR}
-ninja -j$(nproc)
-popd # ${BUILD_DIR}
+ninja -C ${BUILD_DIR} -j$(nproc)
 
 echo "To run the simulator use: (cd ${BUILD_DIR} && ./simulate -M ${MACHINE})"
diff --git a/download_iree_compiler.py b/download_iree_compiler.py
index 43a6baf..3c45055 100755
--- a/download_iree_compiler.py
+++ b/download_iree_compiler.py
@@ -1,16 +1,16 @@
 #!/usr/bin/env python3
 """Download IREE host compiler from the snapshot release."""
 
+import errno
 import os
 import sys
 import tarfile
 import time
 import argparse
-import requests
 import urllib
-import wget
-
 from pathlib import Path
+import requests
+import wget
 
 
 def download_artifact(assets, keywords, out_dir):
@@ -24,10 +24,10 @@
             artifact_match = True
             break
     if not artifact_match:
-        print("%s is not found" % (keywords[0]))
+        print(f"{keywords[0]} is not found")
         sys.exit(1)
 
-    print("\nDownload %s from %s\n" % (artifact_name, download_url))
+    print(f"\nDownload {artifact_name} from {download_url}\n")
     if not os.path.isdir(out_dir):
         os.makedirs(out_dir)
     out_file = os.path.join(out_dir, artifact_name)
@@ -68,11 +68,12 @@
 
     snapshot = None
     if args.tag_name:
-        r = requests.get(("%s/tags/%s" % (args.release_url, args.tag_name)),
+        r = requests.get((f"{args.release_url}/tags/{args.tag_name}"),
                          auth=('user', 'pass'))
         if r.status_code != 200:
-            print("!!!!!IREE snapshot can't be found with tag %s, please try a "
-                  "different tag!!!!!" % args.tag_name)
+            print(
+                f"!!!!!IREE snapshot can't be found with tag {args.tag_name}, "
+                "please try a different tag!!!!!")
             sys.exit(1)
         snapshot = r.json()
     else:
@@ -86,14 +87,14 @@
     tag_name = snapshot["tag_name"]
     commit_sha = snapshot["target_commitish"]
 
-    print("Snapshot: %s" % tag_name)
+    print(f"Snapshot: {tag_name}")
 
     tag_file = iree_compiler_dir / "tag"
 
     # Check the tag of the existing download.
     tag_match = False
     if os.path.isfile(tag_file):
-        with open(tag_file, 'r') as f:
+        with open(tag_file, 'r', encoding="utf-8") as f:
             for line in f:
                 if tag_name == line.replace("\n", ""):
                     tag_match = True
@@ -111,7 +112,8 @@
         snapshot["assets"], ["linux-x86_64.tar"], tmp_dir)
 
     # Install IREE TFLite tool
-    cmd = ("pip3 install %s --no-cache-dir" % whl_file)
+    cmd = (f"pip3 install --target={iree_compiler_dir} {whl_file} "
+           "--upgrade --no-cache-dir")
     os.system(cmd)
 
     # Extract the tarball to ${iree_compiler_dir}/install
@@ -119,17 +121,25 @@
     if not install_dir:
         os.makedirs(install_dir)
 
-    tar = tarfile.open(tar_file)
-    tar.extractall(path=install_dir)
-    tar.close()
+    with tarfile.open(tar_file) as tar:
+        tar.extractall(path=install_dir)
+
+    try:
+        os.symlink(f"{iree_compiler_dir}/bin/iree-import-tflite",
+                f"{install_dir}/bin/iree-import-tflite")
+    except OSError as e:
+        if e.errno == errno.EEXIST:
+            os.remove(f"{install_dir}/bin/iree-import-tflite")
+            os.symlink(f"{iree_compiler_dir}/bin/iree-import-tflite",
+                f"{install_dir}/bin/iree-import-tflite")
 
     os.remove(tar_file)
     os.remove(whl_file)
     print("\nIREE compiler is installed")
 
     # Add tag file for future checks
-    with open(tag_file, "w") as f:
-        f.write("%s\ncommit_sha: %s\n" % (tag_name, commit_sha))
+    with open(tag_file, "w", encoding="utf-8") as f:
+        f.write(f"{tag_name}\ncommit_sha: {commit_sha}\n")
 
 
 if __name__ == "__main__":
diff --git a/install-prereqs.sh b/install-prereqs.sh
index 858d98e..68d5ec0 100755
--- a/install-prereqs.sh
+++ b/install-prereqs.sh
@@ -58,7 +58,7 @@
     libglib2.0-dev-bin
     libgtk2.0-0
     libpixman-1-dev
-    libpython3.9
+    libpython3-dev
     libsqlite3-dev
     libssl-dev
     libtinfo-dev
@@ -77,8 +77,8 @@
     pv
     python-is-python3
     python3-protobuf
-    python3.9
-    python3.9-dev
+    python3
+    python3-dev
     python3-pip
     rsync
     srecord
diff --git a/kcargo.sh b/kcargo.sh
index 8664a72..b058374 100755
--- a/kcargo.sh
+++ b/kcargo.sh
@@ -24,7 +24,7 @@
 
 # HACK: sel4-config needs a path to the kernel build which could be
 #   in debug or release
-export SEL4_OUT_DIR="$ROOTDIR/out/kata/riscv32-unknown-elf/debug/kernel/"
+export SEL4_OUT_DIR=${SEL4_OUT_DIR:-"$ROOTDIR/out/kata/riscv32-unknown-elf/debug/kernel/"}
 if [[ ! -d "${SEL4_OUT_DIR}/gen_config" ]]; then
     echo "No kernel build found at ${SEL4_OUT_DIR}; build a kernel first"
     exit 2