Skip to content

Commit e74945c

Browse files
linabutlerncalexan
authored andcommitted
Always use the NDK version from source.properties.
Previously, `RustAndroidPlugin` would read the NDK version from `source.properties` in the NDK path, while `CargoBuildTask` would try to infer the NDK version from the pathname. Inferring the NDK version from the pathname would fail if the pathname didn't contain the version (e.g., Mach currently installs the NDK used to build Firefox for Android into `~/.mozbuild/android-ndk-r26c`), causing `Toolchain` to return the wrong path to `ar` for new NDK versions.
1 parent 109ce8a commit e74945c

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

plugin/src/main/kotlin/com/nishtahir/CargoBuildTask.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ open class CargoBuildTask : DefaultTask() {
1515
@Input
1616
var toolchain: Toolchain? = null
1717

18+
@Input
19+
var ndk: Ndk? = null
20+
1821
@Suppress("unused")
1922
@TaskAction
2023
fun build() = with(project) {
@@ -25,10 +28,12 @@ open class CargoBuildTask : DefaultTask() {
2528
throw GradleException("toolchain cannot be null")
2629
}
2730

31+
val ndk = ndk ?: throw GradleException("ndk cannot be null")
32+
2833
project.plugins.all {
2934
when (it) {
30-
is AppPlugin -> buildProjectForTarget<AppExtension>(project, toolchain, this)
31-
is LibraryPlugin -> buildProjectForTarget<LibraryExtension>(project, toolchain, this)
35+
is AppPlugin -> buildProjectForTarget<AppExtension>(project, toolchain, ndk, this)
36+
is LibraryPlugin -> buildProjectForTarget<LibraryExtension>(project, toolchain, ndk, this)
3237
}
3338
}
3439
// CARGO_TARGET_DIR can be used to force the use of a global, shared target directory
@@ -77,8 +82,7 @@ open class CargoBuildTask : DefaultTask() {
7782
}
7883
}
7984

80-
inline fun <reified T : BaseExtension> buildProjectForTarget(project: Project, toolchain: Toolchain, cargoExtension: CargoExtension) {
81-
val app = project.extensions[T::class]
85+
inline fun <reified T : BaseExtension> buildProjectForTarget(project: Project, toolchain: Toolchain, ndk: Ndk, cargoExtension: CargoExtension) {
8286
val apiLevel = cargoExtension.apiLevels[toolchain.platform]!!
8387
val defaultTargetTriple = getDefaultTargetTriple(project, cargoExtension.rustcCommand)
8488

@@ -165,13 +169,8 @@ open class CargoBuildTask : DefaultTask() {
165169

166170
// Cross-compiling to Android requires toolchain massaging.
167171
if (toolchain.type != ToolchainType.DESKTOP) {
168-
val ndkPath = app.ndkDirectory
169-
val ndkVersion = ndkPath.name
170-
val ndkVersionMajor = try {
171-
ndkVersion.split(".").first().toInt()
172-
} catch (ex: NumberFormatException) {
173-
0 // Falls back to generic behaviour.
174-
}
172+
val ndkPath = ndk.path
173+
val ndkVersionMajor = ndk.versionMajor
175174

176175
val toolchainDirectory = if (toolchain.type == ToolchainType.ANDROID_PREBUILT) {
177176
environment("CARGO_NDK_MAJOR_VERSION", ndkVersionMajor)

plugin/src/main/kotlin/com/nishtahir/RustAndroidPlugin.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ val toolchains = listOf(
107107
"android/x86_64")
108108
)
109109

110+
data class Ndk(val path: File, val version: String) {
111+
val versionMajor: Int
112+
get() = version.split(".").first().toInt()
113+
}
114+
110115
data class Toolchain(val platform: String,
111116
val type: ToolchainType,
112117
val target: String,
@@ -227,21 +232,23 @@ open class RustAndroidPlugin : Plugin<Project> {
227232
}
228233

229234
// Determine the NDK version, if present
230-
val ndkSourceProperties = Properties()
231-
val ndkSourcePropertiesFile = File(extensions[T::class].ndkDirectory, "source.properties")
232-
if (ndkSourcePropertiesFile.exists()) {
233-
ndkSourceProperties.load(ndkSourcePropertiesFile.inputStream())
235+
val ndk = extensions[T::class].ndkDirectory.let {
236+
val ndkSourceProperties = Properties()
237+
val ndkSourcePropertiesFile = File(it, "source.properties")
238+
if (ndkSourcePropertiesFile.exists()) {
239+
ndkSourceProperties.load(ndkSourcePropertiesFile.inputStream())
240+
}
241+
val ndkVersion = ndkSourceProperties.getProperty("Pkg.Revision", "0.0")
242+
Ndk(path = it, version = ndkVersion)
234243
}
235-
val ndkVersion = ndkSourceProperties.getProperty("Pkg.Revision", "0.0")
236-
val ndkVersionMajor = ndkVersion.split(".").first().toInt()
237244

238245
// Determine whether to use prebuilt or generated toolchains
239246
val usePrebuilt =
240247
cargoExtension.localProperties.getProperty("rust.prebuiltToolchains")?.equals("true") ?:
241248
cargoExtension.prebuiltToolchains ?:
242-
(ndkVersionMajor >= 19);
249+
(ndk.versionMajor >= 19);
243250

244-
if (usePrebuilt && ndkVersionMajor < 19) {
251+
if (usePrebuilt && ndk.versionMajor < 19) {
245252
throw GradleException("usePrebuilt = true requires NDK version 19+")
246253
}
247254

@@ -299,6 +306,7 @@ open class RustAndroidPlugin : Plugin<Project> {
299306
group = RUST_TASK_GROUP
300307
description = "Build library ($target)"
301308
toolchain = theToolchain
309+
this.ndk = ndk
302310
}
303311

304312
if (!usePrebuilt) {

0 commit comments

Comments
 (0)