Merge "Install bazel as a prereq through apt"
diff --git a/kgdb.sh b/kgdb.sh
index 39a95f1..b3a745a 100755
--- a/kgdb.sh
+++ b/kgdb.sh
@@ -18,22 +18,75 @@
 KATA_OUT=out/kata/${TARGET}/debug
 MATCHA_OUT=out/matcha/riscv32imc-unknown-none-elf/debug
 
+USE_SEL4_EXTENSIONS="true"
+USE_SEL4_SYMBOL_AUTOSWITCHING="false"
+
 export SOURCE_DIR=${ROOTDIR}/kata
 export BUILD_DIR=$KATA_OUT
 
-# NB: -q suppresses the banner to workaround the banner msg triggering the pager
-# NB: auto-start cpu0 & cpu1 but leave cpu2 (VC) halted
-exec "${GDB}" -q -cd "${ROOTDIR}" \
-  -ex "set pagination off" \
-  -ex "directory sw/tock" \
-  -ex "file ${PROGRAM}" \
-  -ex "set confirm off" \
-  -ex "add-symbol-file ${PROGRAM}" \
-  -ex "add-symbol-file ${MATCHA_OUT}/matcha_platform" \
-  -ex "add-symbol-file ${MATCHA_OUT}/matcha_app" \
-  -ex "set pagination on" \
-  -ex "target remote ${REMOTE}" \
-  -ex "monitor cpu0 IsHalted false" \
-  -ex "monitor cpu1 CreateSeL4 0xffffffef" \
-  -ex "source sim/renode/tools/sel4_extensions/gdbscript.py" \
-  -ex "sel4 symbol-autoswitching false"
+function parseargv {
+    local usage="Usage: kgdb.sh [-h|--help] [-S|--no-sel4-extensions] [-a|--sel4-symbol-autoswitching]"
+    local args=$(getopt -o hSa --long no-sel4-extensions,symbol-autoswitching,help -n kgdb.sh -- "$@")
+
+    set -- $args
+
+    for i; do
+        case "$1" in
+            -S|--no-sel4-extensions)
+                echo "*** Disabling sel4 extensions"
+                USE_SEL4_EXTENSIONS="false"
+                shift
+                ;;
+
+            -a|--symbol-autoswitching)
+                echo "*** Enabling sel4 symbol autoswitching"
+                echo "*** Warning: this can cause unexpected behaviors."
+                USE_SEL4_EXTENSIONS="true"
+                USE_SEL4_SYMBOL_AUTOSWITCHING="true"
+                shift
+                ;;
+
+            --)
+                shift
+                break
+                ;;
+
+            -h|--help|*)
+                echo "$usage" >/dev/stderr
+                exit 1
+                ;;
+        esac
+    done
+}
+
+function main {
+    local -a gdbargs=(
+         -ex "set pagination off"
+         -ex "directory sw/tock"
+         -ex "file ${PROGRAM}"
+         -ex "set confirm off"
+         -ex "add-symbol-file ${PROGRAM}"
+         -ex "add-symbol-file ${MATCHA_OUT}/matcha_platform"
+         -ex "add-symbol-file ${MATCHA_OUT}/matcha_app"
+         -ex "set pagination on"
+         -ex "target remote ${REMOTE}"
+         -ex "monitor cpu0 IsHalted false"
+    )
+
+    parseargv "$@"
+
+    if [[ "${USE_SEL4_EXTENSIONS}" == "true" ]]; then
+        gdbargs+=(
+            -ex "monitor cpu1 CreateSeL4 0xffffffee"
+            -ex "source sim/renode/tools/sel4_extensions/gdbscript.py"
+            -ex "sel4 symbol-autoswitching ${USE_SEL4_SYMBOL_AUTOSWITCHING}"
+        )
+    fi
+
+
+    # NB: -q suppresses the banner to workaround the banner msg triggering the pager
+    # NB: auto-start cpu0 & cpu1 but leave cpu2 (VC) halted
+    exec "${GDB}" -q -cd "${ROOTDIR}" "${gdbargs[@]}"
+}
+
+main "$@"
diff --git a/preupload-hooks/rustfmt.py b/preupload-hooks/rustfmt.py
new file mode 100755
index 0000000..e153c36
--- /dev/null
+++ b/preupload-hooks/rustfmt.py
@@ -0,0 +1,73 @@
+#!/usr/bin/env python3
+# Copyright 2022 Google LLC
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""Wrapper to run  rustfmt for repo preupload."""
+
+import argparse
+import os
+import subprocess
+import sys
+
+
+def get_parser():
+    """Return a command line parser."""
+    parser = argparse.ArgumentParser(description=__doc__)
+    parser.add_argument("--rustfmt_path",
+                        default="rustfmt",
+                        help="The path to the rustfmt binary.")
+    parser.add_argument('files',
+                        type=str,
+                        nargs='*',
+                        help='If specified, only consider rustfmt in '
+                        'these files.')
+    return parser
+
+
+def main(argv):
+    """The main entry."""
+    parser = get_parser()
+    opts = parser.parse_args(argv)
+
+    # Check and set rustfmt path in case `source build/setup.sh` is not run in
+    # the shell session. In repo preupload, the path should be set in
+    # PREUPLOAD.cfg.
+    if opts.rustfmt_path != "rustfmt":
+        # Add rustfmt path to system PATH and set up RUSTUP_HOME at one level up
+        # rustfmt has to have both variables set up to work properly.
+        path = os.path.realpath(opts.rustfmt_path + "/..")
+        os.environ["PATH"] = path + ":" + os.getenv("PATH")
+        os.environ["RUSTUP_HOME"] = path + "/.."
+
+    # Only process .rs files
+    file_list = [f for f in opts.files if f.endswith("rs")]
+    if not file_list:
+        sys.exit(0)
+
+    cmd = [opts.rustfmt_path, "--check", "--color", "never"]
+
+    for f in file_list:
+        cmd.append(f)
+
+    # Run rustfmt on all the .rs files in the file list. `--check` flag
+    # prints out the formatting error and return with exit(1).
+    try:
+        subprocess.run(cmd, check=True)
+    except subprocess.CalledProcessError as e:
+        print(f"rustfmt check failed\ncmd: {cmd}\nexit code {e.returncode}")
+        sys.exit(e.returncode)
+    else:
+        sys.exit(0)
+
+
+if __name__ == '__main__':
+    main(sys.argv[1:])
diff --git a/python-requirements.txt b/python-requirements.txt
index 94de1ba..c0e0ae9 100644
--- a/python-requirements.txt
+++ b/python-requirements.txt
@@ -14,6 +14,7 @@
 pyyaml
 requests
 robotframework==4.0.1
+scipy
 sel4-deps
 setuptools
 tempita