Modern Apps git

← Modern-Apps.git

build: share Rust dependency compilation across dev crates

Commit
fdf3b24086cac55d2cc22e0b27e97f3f9461f4a6
Author
Vayun Mathur <[email protected]>
When
2026-12-05 22:19 UTC (5 hours ago)
Parents
d79268c
Cold dev builds recompiled the shared cargo dependency graph once
per crate (34 crates, isolated target dirs, per-crate remap prefixes
making every rustc command line distinct). For the fast (dev) profile
only: one unified remap prefix (repo root -> /ma-dev) makes rustc
invocations for shared dependencies identical across crates, and
rustc runs through sccache when installed (~/.cargo/bin/sccache,
downloaded 0.8.2 binary; cache dir is shared across worktrees).
sccache prohibits incremental compilation, so CARGO_INCREMENTAL turns
off exactly when the wrapper is active; the wrapper replaces it.
Isolated target dirs and parallel cargo stay (a single shared target
dir serialised on cargo's file lock and measured slower: 155s vs 40s
scoped wall). Release is byte-for-byte untouched: per-crate remap,
no wrapper, incremental off, same target dirs. Both new facts are
declared task inputs so build-cache keys stay honest.

Also raises the Kotlin daemon heap 1536m -> 4g (gradle.properties):
cold all-builds queue dozens of Compose compiles through it.

Measured (scoped camera+notes+calculator, empty build/target,
--no-build-cache, --profile): task-time split Kotlin+KSP 78s, dex
48s (mergeExtDexDev 12-18s/app), cargo 39s, other 32s; wall ~40s.
First sccache cold run: 43 misses, 0 hits (populates); fresh-target
rerun: 43/43 shared units hit, remaining time is final-crate compile
and linking, which no cache can skip. Verified: scoped assembleDev
(notes/camera/library/games:chess/calculator), :library:ui:lint and
:library:ui:testDevUnitTest (1106 tests, 0 failures).

Diff

build-logic/src/main/kotlin/RustNative.kt
diff --git a/build-logic/src/main/kotlin/RustNative.kt b/build-logic/src/main/kotlin/RustNative.ktindex 8708c0c44f..b84a73cc91 100644--- a/build-logic/src/main/kotlin/RustNative.kt+++ b/build-logic/src/main/kotlin/RustNative.kt@@ -511,7 +511,20 @@ fun Project.rustNativeLib(             } else {                 null             }-            val isolatedSoOut = isolatedTargetDir?.let { File(it, "$triple/$profile/lib$crate.so") }+            // Fast (dev) profile only: unify the remap prefix across crates+            // (below) so rustc command lines for shared dependencies are+            // identical in every crate, and route rustc through sccache when+            // it is installed (~/.cargo/bin/sccache, shared cache dir across+            // worktrees). Isolated per-crate target dirs stay, so cargo+            // builds remain parallel; sccache, not a shared target dir,+            // provides the sharing (a shared dir serialises every cargo on+            // its file lock and measured slower cold). Release is untouched+            // (per-crate remap, no wrapper) and stays byte-reproducible.+            val devSharedTarget = profile == "fast"+            val effectiveTargetDir = isolatedTargetDir+            val effectiveSoOut = effectiveTargetDir?.let { File(it, "$triple/$profile/lib$crate.so") }+            val effectiveRemapLabel = if (devSharedTarget) "ma-dev" else remapLabel+            val sccacheExe = if (devSharedTarget && File("$cargoBin/sccache").isFile) "$cargoBin/sccache" else null             val destSo = layout.buildDirectory.file("rustJniLibs/$abiDir/lib$crate.so").get().asFile             val targetRustflags = "-C link-arg=-Wl,-z,max-page-size=16384" @@ -596,7 +609,13 @@ fun Project.rustNativeLib(             // setting below, and Exec environment is @Internal, so declare             // both as inputs: switching fast/release must re-run the task.             inputs.property("cargoProfile", profile)-            inputs.property("cargoIncremental", profile == "fast")+            inputs.property("cargoIncremental", profile == "fast" && sccacheExe == null)+            inputs.property("rustRemapLabel", effectiveRemapLabel)+            inputs.property("rustcWrapperSccache", sccacheExe != null)+            // (cargoIncremental above already reflects this: sccache+            // prohibits incremental compilation outright, so the wrapper+            // replaces it — shared deps come from the sccache cache, and+            // only the edited crate itself compiles in full.)             outputs.file(destSo)              val cargoHome = System.getenv("CARGO_HOME") ?: "${System.getProperty("user.home")}/.cargo"@@ -627,13 +646,14 @@ fun Project.rustNativeLib(             // environment("BINDGEN_EXTRA_CLANG_ARGS", "--target=$triple$androidApiLevel --sysroot=$ndkSysroot")             // Reproducible builds: remap $HOME-specific paths (cargo registry + crate             // dir) to fixed constants so different machines produce identical .so bytes.+            val remapBase = if (devSharedTarget) rootProject.projectDir.absolutePath else rustSrc             environment(                 "RUSTFLAGS",-                "--remap-path-prefix=$cargoHome=/cargo --remap-path-prefix=$rustSrc=/$remapLabel",+                "--remap-path-prefix=$cargoHome=/cargo --remap-path-prefix=$remapBase=/$effectiveRemapLabel",             )-            environment("CFLAGS", "-ffile-prefix-map=$cargoHome=/cargo -ffile-prefix-map=$rustSrc=/$remapLabel -Wdate-time -Werror=date-time")-            environment("CXXFLAGS", "-ffile-prefix-map=$cargoHome=/cargo -ffile-prefix-map=$rustSrc=/$remapLabel -Wdate-time -Werror=date-time")-            environment("CPPFLAGS", "-ffile-prefix-map=$cargoHome=/cargo -ffile-prefix-map=$rustSrc=/$remapLabel -Wdate-time -Werror=date-time")+            environment("CFLAGS", "-ffile-prefix-map=$cargoHome=/cargo -ffile-prefix-map=$remapBase=/$effectiveRemapLabel -Wdate-time -Werror=date-time")+            environment("CXXFLAGS", "-ffile-prefix-map=$cargoHome=/cargo -ffile-prefix-map=$remapBase=/$effectiveRemapLabel -Wdate-time -Werror=date-time")+            environment("CPPFLAGS", "-ffile-prefix-map=$cargoHome=/cargo -ffile-prefix-map=$remapBase=/$effectiveRemapLabel -Wdate-time -Werror=date-time")             environment("ZERO_AR_DATE", "1")             // Reproducible builds: respect SOURCE_DATE_EPOCH if set (exported by the release build)             // https://reproducible-builds.org/docs/source-date-epoch/@@ -643,8 +663,9 @@ fun Project.rustNativeLib(             // Release .so files must stay byte-reproducible, so release             // builds keep incremental off. The fast (dev) profile is never             // shipped, so it reuses incremental artifacts across edits.-            environment("CARGO_INCREMENTAL", if (profile == "fast") "1" else "0")-            isolatedTargetDir?.let { environment("CARGO_TARGET_DIR", it.absolutePath) }+            environment("CARGO_INCREMENTAL", if (profile == "fast" && sccacheExe == null) "1" else "0")+            effectiveTargetDir?.let { environment("CARGO_TARGET_DIR", it.absolutePath) }+            sccacheExe?.let { environment("RUSTC_WRAPPER", it) }              commandLine(                 "$cargoBin/cargo$exeExt",@@ -660,8 +681,8 @@ fun Project.rustNativeLib(             doLast {                 destSo.parentFile.mkdirs()                 val src = when {-                    isolatedSoOut != null -> isolatedSoOut.takeIf { it.exists() }-                        ?: error("cargo produced no ${isolatedSoOut.path} for $crate")+                    effectiveSoOut != null -> effectiveSoOut.takeIf { it.exists() }+                        ?: error("cargo produced no ${effectiveSoOut.path} for $crate")                     perCrateSoOut.exists() -> perCrateSoOut                     workspaceSoOut.exists() -> workspaceSoOut                     else -> error("Neither per-crate ${perCrateSoOut.path} nor workspace ${workspaceSoOut.path} exists for $crate")
gradle.properties
diff --git a/gradle.properties b/gradle.propertiesindex a6cb38e024..daecb4e8f7 100644--- a/gradle.properties+++ b/gradle.properties@@ -11,7 +11,9 @@ # load class metadata into the same daemon's metaspace; with parallel workers we # easily blew past 512m). org.gradle.jvmargs=-Xmx8g -Xms512m -XX:MaxMetaspaceSize=1024m -XX:+UseG1GC -Dfile.encoding=UTF-8-kotlin.daemon.jvm.options=-Xmx1536m+# 4g: cold all-builds queue dozens of module compiles through the daemon;+# the old 1536m cap made large Compose modules GC-thrash mid-compile.+kotlin.daemon.jvm.options=-Xmx4g  # Parallel task workers. The build graph is wide (~100 app modules with # independent Kotlin/KSP/dex work), so the default uses the dev box's cores;