#!/usr/bin/env bash # Static verification without building or installing: # ./gradlew ::detekt ::lint ::ktlintCheck [...] + cargo check/clippy/fmt/doc (rust modules only) # # Usage: # ./check [module...] # detekt (detekt.yml) + lint + ktlintCheck + cargo check/clippy/fmt/doc # ./check games/voxels maps # multi-module, one gradlew invocation # ./check voxels # shorthand -> games:voxels (same as ./install) # ./check all # every app + library module # ./check structure # only the structure ratchets (below) # # Structure ratchets (docs/structure/README.md): tools/structure_check.py # checks Wear protocol-pair drift, the generated-source manifest, and the # exception-marker inventory. They run on every ./check invocation (cheap, # no build) and their failure fails this script. # # Module notation mirrors ./install: slash == colon, games:/personal: shorthand, # 'all' keyword, deduped. Tasks run with --continue so one broken module does not # block the rest; exit code is nonzero if anything failed. # # Rust: a module with src/main/rust/Cargo.toml is checked with `cargo check # --locked`: workspace members together via `-p` from the repo root, standalone # crates (own [workspace]) individually in their own dir (host target: fast smoke # check; the real aarch64 cross-build runs inside the Gradle build via # cargoNdkBuild). Lint-sweep Phase 4 adds, per in-scope crate: `cargo clippy # --locked --all-targets` (blocking on deny/error lints; warnings never fail), # `cargo fmt --check` (blocking: this script fails on rustfmt drift; fix # with `./format `), and `cargo doc --locked --no-deps` (blocking on # rustdoc errors). Kotlin formatting is enforced the same way: every module # runs `:ktlintCheck` next to detekt/lint; fix with `./format `. # No rust modules -> cargo steps skipped. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT="$SCRIPT_DIR" print_help() { sed -n '2,/^set -euo/p' "$SCRIPT_DIR/check" | sed 's/^# \{0,1\}//' | head -n -1 } normalize_module() { local m="$1" m="$(echo "$m" | xargs)" m="$(echo "$m" | sed -e 's|^[/:]*||' -e 's|[/:]*$||')" m="$(echo "$m" | tr '/' ':')" while echo "$m" | grep -q "::"; do m="$(echo "$m" | sed 's/::/:/g')"; done echo "$m" | tr '[:upper:]' '[:lower:]' } # all = app + library convention modules (both apply dev.detekt and # org.jlleitschuh.gradle.ktlint; pure-JVM # modules like youpipe:extractor do not, so they are excluded here but still # accepted when named explicitly and left for Gradle to report on). all_module_dirs() { grep -E 'include\(":[^"]+"\)' "$ROOT/settings.gradle.kts" 2>/dev/null \ | sed -E 's/.*include\(":([^"]+)"\).*/\1/' \ | tr ':' '/' \ | while IFS= read -r p; do case "$p" in "" ) continue;; esac if [[ -f "$ROOT/$p/build.gradle.kts" ]] \ && grep -qE 'common-conventions-(app|library)' "$ROOT/$p/build.gradle.kts" 2>/dev/null; then echo "$p" fi done | sort -u } if [[ $# -eq 0 ]]; then print_help; echo ""; echo "Error: no modules specified."; exit 1; fi for arg in "$@"; do case "$arg" in --help|-h|help) print_help; exit 0;; esac; done for arg in "$@"; do if [[ "$arg" == "structure" ]]; then exec python3 "$ROOT/tools/structure_check.py" all fi done MODS=(); ALL_EXP=0 for raw in "$@"; do norm="$(normalize_module "$raw")" [[ -z "$norm" ]] && continue if [[ "$norm" == "all" ]]; then if [[ $ALL_EXP -eq 0 ]]; then while IFS= read -r sm; do case "$sm" in "" ) continue;; esac MODS+=("$sm") done < <(all_module_dirs) ALL_EXP=1 fi continue fi if [[ "$norm" != *":"* ]]; then for pfx in games personal; do [[ -f "$ROOT/$pfx/$norm/build.gradle.kts" ]] && { norm="$pfx:$norm"; break; } done fi fs="$(echo "$norm" | tr ':' '/')" if [[ ! -f "$ROOT/$fs/build.gradle.kts" ]]; then echo "Error: unknown module '$raw' (-> $fs/, no build.gradle.kts)." >&2; exit 1 fi dup=0; for m in ${MODS[@]:-}; do [[ "$m" == "$fs" ]] && dup=1; done [[ $dup -eq 0 ]] && MODS+=("$fs") done [[ ${#MODS[@]} -gt 0 ]] || { echo "Error: no valid modules." >&2; exit 1; } TASKS=() for mod in "${MODS[@]}"; do gmod="$(echo "$mod" | tr '/' ':')" TASKS+=(":$gmod:detekt" ":$gmod:lint" ":$gmod:ktlintCheck") # cargo include_bytes!s the SPIR-V this task writes to build/spirv. [[ -d "$ROOT/$mod/src/main/rust/shaders" ]] && TASKS+=(":$gmod:compileShaders") done # Guards issue #884 (no library may depend on a permission-declaring library). # Root task, so add once however many modules are checked. TASKS+=(":verifyLibraryBoundaries") echo "Modules (${#MODS[@]}): ${MODS[*]}" echo "Gradle tasks: ${TASKS[*]}" echo "" STRUCTURE_RC=0 if command -v python3 >/dev/null 2>&1; then python3 "$ROOT/tools/structure_check.py" all || STRUCTURE_RC=$? else echo "Error: python3 not found; structure checks (tools/structure_check.py) cannot run." >&2 STRUCTURE_RC=1 fi [[ $STRUCTURE_RC -ne 0 ]] && echo "Warning: structure checks reported failures (exit $STRUCTURE_RC)." echo "" DETEKT_RC=0 "$SCRIPT_DIR/gradlew" "${TASKS[@]}" --continue --console=plain || DETEKT_RC=$? echo "" [[ $DETEKT_RC -ne 0 ]] && echo "Warning: detekt/lint reported failures (exit $DETEKT_RC)." # --- cargo check (rust modules only) --- # Workspace members share one root `cargo check -p ...` invocation; standalone # crates (own [workspace] table + lockfile, e.g. games:voxels, code) are checked # individually with cwd set to the crate dir. ROOT_CRATES=() STANDALONE_DIRS=() for mod in "${MODS[@]}"; do rdir="$ROOT/$mod/src/main/rust" ct="$rdir/Cargo.toml" if [[ -f "$ct" ]]; then if grep -qE '^\[workspace\][[:space:]]*$' "$ct"; then STANDALONE_DIRS+=("$rdir") echo "Rust: $mod -> standalone crate ($rdir)" else name="$(sed -nE 's/^name *= *"([^"]+)".*/\1/p' "$ct" | head -1)" [[ -n "$name" ]] && ROOT_CRATES+=("$name") && echo "Rust: $mod -> workspace crate $name" fi fi done CARGO_RC=0 if command -v cargo >/dev/null 2>&1; then CARGO_BIN="cargo" else CARGO_BIN="${CARGO_HOME:-$HOME/.cargo}/bin/cargo" [[ -x "$CARGO_BIN" ]] || { echo "Error: cargo not found ($CARGO_BIN)." >&2; exit 1; } fi if [[ ${#ROOT_CRATES[@]} -gt 0 ]]; then echo "" CARGO_ARGS=() for c in "${ROOT_CRATES[@]}"; do CARGO_ARGS+=(-p "$c"); done echo "Running: cargo check --locked ${CARGO_ARGS[*]}" ( cd "$ROOT" && "$CARGO_BIN" check --locked "${CARGO_ARGS[@]}" ) || CARGO_RC=$? [[ $CARGO_RC -ne 0 ]] && echo "Warning: cargo check (workspace) reported failures (exit $CARGO_RC)." fi for rdir in ${STANDALONE_DIRS[@]:-}; do echo "" echo "Running: cargo check --locked (in $rdir)" ( cd "$rdir" && "$CARGO_BIN" check --locked ) || CARGO_RC=$? [[ $CARGO_RC -ne 0 ]] && echo "Warning: cargo check ($rdir) reported failures (exit $CARGO_RC)." done # --- cargo clippy (rust modules only, lint-sweep Phase 4) --- # Same scope as cargo check. Each crate's own `[lints]` table governs severity, # so warnings never fail this step; only deny/error lints do. Runs with # --all-targets so tests/examples/benches are covered too. CLIPPY_RC=0 if [[ ${#ROOT_CRATES[@]} -gt 0 ]]; then echo "" CLIPPY_ARGS=() for c in "${ROOT_CRATES[@]}"; do CLIPPY_ARGS+=(-p "$c"); done echo "Running: cargo clippy --locked ${CLIPPY_ARGS[*]} --all-targets" ( cd "$ROOT" && "$CARGO_BIN" clippy --locked "${CLIPPY_ARGS[@]}" --all-targets ) || CLIPPY_RC=$? [[ $CLIPPY_RC -ne 0 ]] && echo "Warning: cargo clippy (workspace) reported failures (exit $CLIPPY_RC)." fi for rdir in ${STANDALONE_DIRS[@]:-}; do echo "" echo "Running: cargo clippy --locked --all-targets (in $rdir)" ( cd "$rdir" && "$CARGO_BIN" clippy --locked --all-targets ) || CLIPPY_RC=$? [[ $CLIPPY_RC -ne 0 ]] && echo "Warning: cargo clippy ($rdir) reported failures (exit $CLIPPY_RC)." done # --- cargo fmt (blocking) --- # `cargo fmt --check` fails this script on drift. Fix with # `./format ` (or `cargo fmt -p `); style is rustfmt.toml. # With `all`, the workspace-wide `--all` run also covers workspace crates # with no Gradle module (scripts/, third_party/, library/tilecodec, # library/jni-http, library/vision, communicate-dev/src/main/rust-mls), # and any standalone crate that is not a workspace member (a Cargo.toml # outside target/build/personal and outside the workspace member set) # is checked in its own dir. standalone_crate_dirs() { python3 - "$ROOT" "$CARGO_BIN" <<'PYEOF' import json, os, pathlib, subprocess, sys root = pathlib.Path(sys.argv[1]); cargo = sys.argv[2] try: meta = json.loads(subprocess.check_output([cargo, "metadata", "--format-version", "1", "--no-deps"], cwd=root, text=True, stderr=subprocess.DEVNULL)) members = {str(pathlib.Path(p["manifest_path"]).parent) for p in meta["packages"]} except Exception: members = set() skip = {"target", "build", ".git", ".gradle", "personal"} for dirpath, dirnames, filenames in os.walk(root): dirnames[:] = sorted(d for d in dirnames if d not in skip) if "Cargo.toml" in filenames and dirpath != str(root) and dirpath not in members: print(dirpath) PYEOF } FMT_RC=0 if [[ $ALL_EXP -eq 1 ]]; then echo "" echo "Running: cargo fmt --check --all" FMT_OUT="$(cd "$ROOT" && "$CARGO_BIN" fmt --check --all 2>&1)" || FMT_RC=$? if [[ $FMT_RC -ne 0 ]]; then FMT_COUNT="$(echo "$FMT_OUT" | grep -c '^Diff in' || true)" echo "Error: rustfmt drift: $FMT_COUNT hunks in workspace (run ./format all to fix)." else echo "rustfmt: clean (workspace)." fi elif [[ ${#ROOT_CRATES[@]} -gt 0 ]]; then echo "" FMT_ARGS=() for c in "${ROOT_CRATES[@]}"; do FMT_ARGS+=(-p "$c"); done echo "Running: cargo fmt --check ${FMT_ARGS[*]}" FMT_OUT="$(cd "$ROOT" && "$CARGO_BIN" fmt --check "${FMT_ARGS[@]}" 2>&1)" || FMT_RC=$? if [[ $FMT_RC -ne 0 ]]; then FMT_COUNT="$(echo "$FMT_OUT" | grep -c '^Diff in' || true)" echo "Error: rustfmt drift: $FMT_COUNT hunks in scope (run ./format to fix)." else echo "rustfmt: clean." fi fi for rdir in ${STANDALONE_DIRS[@]:-}; do echo "" echo "Running: cargo fmt --check (in $rdir)" FMT_OUT="$(cd "$rdir" && "$CARGO_BIN" fmt --check 2>&1)" || FMT_RC=$? if [[ $FMT_RC -ne 0 ]]; then FMT_COUNT="$(echo "$FMT_OUT" | grep -c '^Diff in' || true)" echo "Error: rustfmt drift: $FMT_COUNT hunks in $rdir (run ./format to fix)." else echo "rustfmt: clean ($rdir)." fi done if [[ $ALL_EXP -eq 1 ]]; then while IFS= read -r rdir; do [[ -z "$rdir" ]] && continue dup=0; for m in ${STANDALONE_DIRS[@]:-}; do [[ "$m" == "$rdir" ]] && dup=1; done [[ $dup -eq 1 ]] && continue echo "" echo "Running: cargo fmt --check (standalone in $rdir)" FMT_OUT="$(cd "$rdir" && "$CARGO_BIN" fmt --check 2>&1)" || FMT_RC=$? if [[ $FMT_RC -ne 0 ]]; then FMT_COUNT="$(echo "$FMT_OUT" | grep -c '^Diff in' || true)" echo "Error: rustfmt drift: $FMT_COUNT hunks in $rdir (run ./format all to fix)." else echo "rustfmt: clean ($rdir)." fi done < <(standalone_crate_dirs) fi # --- cargo doc (rust modules only, lint-sweep Phase 4) --- # Catches rustdoc::broken_intra_doc_links / invalid_html_tags. Both land as # warnings first (warn-then-error), so this fails only on rustdoc errors. DOC_RC=0 if [[ ${#ROOT_CRATES[@]} -gt 0 ]]; then echo "" DOC_ARGS=() for c in "${ROOT_CRATES[@]}"; do DOC_ARGS+=(-p "$c"); done echo "Running: cargo doc --locked --no-deps ${DOC_ARGS[*]}" ( cd "$ROOT" && "$CARGO_BIN" doc --locked --no-deps "${DOC_ARGS[@]}" ) || DOC_RC=$? [[ $DOC_RC -ne 0 ]] && echo "Warning: cargo doc (workspace) reported failures (exit $DOC_RC)." fi for rdir in ${STANDALONE_DIRS[@]:-}; do echo "" echo "Running: cargo doc --locked --no-deps (in $rdir)" ( cd "$rdir" && "$CARGO_BIN" doc --locked --no-deps ) || DOC_RC=$? [[ $DOC_RC -ne 0 ]] && echo "Warning: cargo doc ($rdir) reported failures (exit $DOC_RC)." done if [[ ${#ROOT_CRATES[@]} -eq 0 && ${#STANDALONE_DIRS[@]} -eq 0 ]]; then echo "No Rust crates in scope; cargo steps skipped." fi if [[ $STRUCTURE_RC -ne 0 || $DETEKT_RC -ne 0 || $CARGO_RC -ne 0 || $CLIPPY_RC -ne 0 || $FMT_RC -ne 0 || $DOC_RC -ne 0 ]]; then exit 1; fi exit 0