build-logic: fast cargo profile for dev builds
- Commit
263b96c847c27e27a6264ece5a66be9b5e5434e7- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-04 23:22 UTC (1 month ago)
- Parents
bd884be
Root Cargo.toml gains a [profile.fast] section inheriting release (so strip and panic=abort still match the shipped profile) but with opt-level 2, no LTO, and 16 codegen units instead of fat LTO with a single codegen unit. Release stays the default for anything shipped. RustNative.kt picks the cargo profile once per build at configuration time as a plain String (configuration-cache compatible). An explicit -Pmodernapps.rustProfile=fast|release wins and anything else fails the build with a clear error. Without the property the rule fails safe: fast is used only when the requested task list is non-empty and every task's last path segment is a dev task (Dev|Debug before a capital or end) or a cargo task; assembleRelease, preReleaseBuild, build, assemble, lint, and empty task lists all use release, so an ambiguous build can never ship a fast-profile .so in a release APK. The cargo command line now passes --profile <profile> instead of --release, and the .so lookup paths track target/<triple>/<profile>/. Alternatives rejected: per-variant jniLibs wiring would touch all 25 native modules for the same effect; a separate dev-only crate feature would fork dependency resolution instead of reusing the release graph. Verified in this worktree with real builds of :euicc:cargoBuild_euicc_arm64_v8a (small crate over library/jni-http): direct invocation selects --profile fast and copies target/aarch64-linux-android/fast/libeuicc.so; :euicc:preReleaseBuild selects --profile release; -Pmodernapps.rustProfile=release forces release on the direct task and an invalid value fails with the expected-value error. Cold cargo times: fast finished in 8.14s vs release 8.89s on the same machine; incremental crate-only rebuilds measured ~2.7s vs ~2.2s. The euicc crate is small so absolute deltas are modest; larger crates with heavier LTO should show a wider gap. AGENTS.md section 3 notes the profile rule and the override flag.
Diff
AGENTS.md
diff --git a/AGENTS.md b/AGENTS.mdindex 657f9380f8..7e413917dd 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -78,6 +78,7 @@ The backend (relay server for findfamily/everysync/office/voxels) lives outside ./gradlew :<module>:checkMetadata ``` Gradle defaults to small heap (`-Xmx8g`, 2 workers). Don't raise it. New build logic must be configuration-cache compatible. New/changed UI previews: `./gradlew :<module>:metadata`.+Rust builds use the fast cargo profile for dev tasks and the release profile for release/ambiguous tasks, overridable with `-Pmodernapps.rustProfile=release|fast`. ## 4. Tools — use these, not raw gradle/adb Work in repo dir. Experiments/logs go in `analysis/` (gitignored).
Cargo.toml
diff --git a/Cargo.toml b/Cargo.tomlindex 20bb730a14..03cf551fff 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -330,3 +330,10 @@ lto = true codegen-units = 1 strip = true panic = "abort"++# Fast profile for dev/agent builds: skips fat LTO for quicker iteration.+[profile.fast]+inherits = "release"+opt-level = 2+lto = false+codegen-units = 16
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 5f02c499b6..317682ac7e 100644--- a/build-logic/src/main/kotlin/RustNative.kt+++ b/build-logic/src/main/kotlin/RustNative.kt@@ -211,6 +211,27 @@ fun Project.rustNativeLib( val cargoFeatureArgs: List<String> = if (features.isNotEmpty()) listOf("--features", features.joinToString(",")) else emptyList() + // Cargo profile chosen once at configuration time (plain String, config-cache+ // safe). Explicit `-Pmodernapps.rustProfile=fast|release` wins; otherwise fail+ // safe: `fast` only when every requested task is a dev/cargo task, else+ // `release` so an ambiguous build never ships a fast-profile .so.+ val devTaskPattern = Regex("(Dev|Debug)(?=[A-Z]|$)")+ val profile: String = providers.gradleProperty("modernapps.rustProfile").orNull?.let { requested ->+ if (requested != "fast" && requested != "release") {+ throw GradleException(+ "Invalid -Pmodernapps.rustProfile=$requested: expected 'fast' or 'release'."+ )+ }+ requested+ } ?: run {+ val taskNames = gradle.startParameter.taskNames+ if (taskNames.isNotEmpty() && taskNames.all { name ->+ val lastSegment = name.substringAfterLast(':')+ lastSegment.startsWith("cargo") || devTaskPattern.containsMatchIn(lastSegment)+ }+ ) "fast" else "release"+ }+ val compileShaders = if (shaders.isEmpty()) null else { require(srcDir == "src/main/rust") { "rustNativeLib($crate): shaders need the default srcDir" } val glslcExe = if (OperatingSystem.current().isWindows) "glslc.exe" else "glslc"@@ -240,8 +261,8 @@ fun Project.rustNativeLib( val linkerVar = "CARGO_TARGET_${triple.uppercase().replace('-', '_')}_LINKER" // Per-crate target (may exist from older isolated builds) and workspace root target // (current scheme since Cargo.toml workspace unified to root).- val perCrateSoOut = file("$srcDir/target/$triple/release/lib$crate.so")- val workspaceSoOut = rootProject.file("target/$triple/release/lib$crate.so")+ val perCrateSoOut = file("$srcDir/target/$triple/$profile/lib$crate.so")+ val workspaceSoOut = rootProject.file("target/$triple/$profile/lib$crate.so") val destSo = layout.buildDirectory.file("rustJniLibs/$abiDir/lib$crate.so").get().asFile inputs.dir("$srcDir/src")@@ -327,7 +348,8 @@ fun Project.rustNativeLib( "$cargoBin/cargo$exeExt", "build", "--locked",- "--release",+ "--profile",+ profile, "--target", triple, *cargoFeatureArgs.toTypedArray(),