Skip to content

A high-performance Android storage analyzer that bypasses MTP using a native C++ daemon, a Rust TCP bridge, and a React Treemap UI.

Rust

Honor Android NDK environment paths in local builds (#84)

Vishnu Srivatsavac37f84c24645
diff --git a/README.md b/README.md
index 14d9bce..d5b5ac2 100644
--- a/README.md
+++ b/README.md
@@ -239,13 +239,17 @@ sequenceDiagram
### 1. Compile the C++ Daemon
Cross-compile the daemon for `aarch64-linux-android`:
```bash
-# Set your NDK path
-export NDK=/path/to/your/android-ndk-r26d
+# Optional: uncomment and replace to override a configured or auto-detected NDK
+# export NDK="/path/to/your/android-ndk-r26d"
# Build the daemon
cd engine
bash ./build.sh
```
+The script prefers `NDK`, then `ANDROID_NDK_LATEST_HOME`, then `ANDROID_NDK_HOME`.
+If none is set, it searches the default Linux/macOS SDK directories. An invalid
+configured path fails explicitly rather than selecting a different toolchain.
+
*This generates the stripped `daemon` binary in the `engine/` directory and
copies it into `src-tauri/bin/daemon`, which Tauri bundles. Rebuild after changing
the daemon so local development uses the current protocol (v3).*
diff --git a/engine/build.sh b/engine/build.sh
index b86d71a..9faf3d7 100644
--- a/engine/build.sh
+++ b/engine/build.sh
@@ -4,6 +4,9 @@
# ============================================================================
set -euo pipefail
+# Match Unix CI discovery while retaining an explicit NDK override.
+NDK="${NDK:-${ANDROID_NDK_LATEST_HOME:-${ANDROID_NDK_HOME:-}}}"
+
if [ -z "${NDK:-}" ]; then
for candidate in \
"$HOME/Android/Sdk/ndk/"* \
@@ -16,7 +19,7 @@ if [ -z "${NDK:-}" ]; then
fi
if [ -z "${NDK:-}" ]; then
- echo "ERROR: Android NDK not found. Set the NDK environment variable."
+ echo "ERROR: Android NDK not found. Set NDK, ANDROID_NDK_LATEST_HOME, or ANDROID_NDK_HOME."
exit 1
fi
diff --git a/tests/test_engine_build.py b/tests/test_engine_build.py
new file mode 100644
index 0000000..f475a6c
--- /dev/null
+++ b/tests/test_engine_build.py
@@ -0,0 +1,90 @@
+"""Build-script fixtures use disposable resources and fake NDK compilers."""
+import json
+import os
+from pathlib import Path
+import shutil
+import subprocess
+import sys
+import tempfile
+import unittest
+
+
[email protected](sys.platform in ("darwin", "linux") and shutil.which("bash"),
+ "The existing Bash build entry point supports macOS/Linux")
+class EngineBuildTests(unittest.TestCase):
+ def setUp(self):
+ self.temp = tempfile.TemporaryDirectory(prefix="socketsweep build fixture ")
+ self.addCleanup(self.temp.cleanup)
+ self.root = Path(self.temp.name)
+ self.engine = self.root / "repository with spaces/engine"
+ self.engine.mkdir(parents=True)
+ script = Path(__file__).resolve().parents[1] / "engine/build.sh"
+ shutil.copyfile(script, self.engine / "build.sh")
+ # Fail closed if a broken discovery path ever selects a real compiler.
+ (self.engine / "daemon.cpp").write_text('#error "Use the fake compiler fixture"\n')
+ self.bundled = self.engine.parent / "src-tauri/bin/daemon"
+ self.bundled.parent.mkdir(parents=True)
+ self.bundled.write_bytes(b"existing bundle")
+ (self.engine / "daemon").write_bytes(b"existing engine")
+ self.environment = {key: value for key, value in os.environ.items()
+ if key not in ("NDK", "ANDROID_NDK_LATEST_HOME", "ANDROID_NDK_HOME")}
+ self.ndks = {label: self.fake_ndk(label) for label in ("explicit", "latest", "home")}
+
+ def fake_ndk(self, label):
+ ndk = self.root / f"NDK {label} with spaces"
+ host = "darwin-x86_64" if sys.platform == "darwin" else "linux-x86_64"
+ compiler = ndk / f"toolchains/llvm/prebuilt/{host}/bin/aarch64-linux-android31-clang++"
+ compiler.parent.mkdir(parents=True)
+ compiler.write_text("#!/usr/bin/env python3\n"
+ "import json, sys\nfrom pathlib import Path\n"
+ f"Path({str(ndk / 'calls.json')!r}).write_text(json.dumps(sys.argv[1:]))\n"
+ f"Path(sys.argv[sys.argv.index('-o') + 1]).write_bytes({label.encode()!r})\n")
+ compiler.chmod(0o700)
+ return ndk
+
+ def build(self, **variables):
+ return subprocess.run(["bash", str(self.engine / "build.sh")], cwd=self.root,
+ env={**self.environment, **{key: str(value) for key, value in variables.items()}},
+ capture_output=True, text=True, timeout=15)
+
+ def assert_selected(self, label, result):
+ self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
+ self.assertEqual((self.engine / "daemon").read_bytes(), label.encode())
+ self.assertEqual(self.bundled.read_bytes(), label.encode())
+ args = json.loads((self.ndks[label] / "calls.json").read_text())
+ self.assertEqual(args, ["-std=c++17", "-O2", "-pthread", "-Wall", "-Wextra", "-Wpedantic",
+ "-fno-exceptions", "-fno-rtti", "-DNDEBUG", "-static-libstdc++", "-s",
+ "-o", str(self.engine / "daemon"), str(self.engine / "daemon.cpp")])
+ for other in self.ndks:
+ if other != label:
+ self.assertFalse((self.ndks[other] / "calls.json").exists())
+
+ def test_explicit_ndk_remains_highest_priority(self):
+ self.assert_selected("explicit", self.build(NDK=self.ndks["explicit"],
+ ANDROID_NDK_LATEST_HOME=self.ndks["latest"], ANDROID_NDK_HOME=self.ndks["home"]))
+
+ def test_latest_home_is_used_before_home_when_ndk_is_unset_or_empty(self):
+ for explicit in ({}, {"NDK": ""}):
+ with self.subTest(explicit=explicit):
+ self.assert_selected("latest", self.build(**explicit,
+ ANDROID_NDK_LATEST_HOME=self.ndks["latest"], ANDROID_NDK_HOME=self.ndks["home"]))
+
+ def test_home_is_used_when_latest_is_unset_or_empty(self):
+ for latest in ({}, {"ANDROID_NDK_LATEST_HOME": ""}):
+ with self.subTest(latest=latest):
+ self.assert_selected("home", self.build(**latest, ANDROID_NDK_HOME=self.ndks["home"]))
+
+ def test_invalid_selected_path_does_not_fall_back_or_replace_resources(self):
+ missing = self.root / "missing NDK"
+ for variables in [
+ {"NDK": missing, "ANDROID_NDK_LATEST_HOME": self.ndks["latest"], "ANDROID_NDK_HOME": self.ndks["home"]},
+ {"ANDROID_NDK_LATEST_HOME": missing, "ANDROID_NDK_HOME": self.ndks["home"]},
+ {"ANDROID_NDK_HOME": missing},
+ ]:
+ with self.subTest(variables=variables):
+ result = self.build(**variables)
+ self.assertNotEqual(result.returncode, 0)
+ self.assertIn(str(missing), result.stdout + result.stderr)
+ self.assertEqual(self.bundled.read_bytes(), b"existing bundle")
+ self.assertEqual((self.engine / "daemon").read_bytes(), b"existing engine")
+ self.assertFalse(any((ndk / "calls.json").exists() for ndk in self.ndks.values()))