Skip to content

Commit 6a433dc

Browse files
author
matrixdev
committed
updated public interface
1 parent e488565 commit 6a433dc

File tree

9 files changed

+72
-53
lines changed

9 files changed

+72
-53
lines changed

README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,21 @@ androidRust {
4646
profile = "release"
4747

4848
// default abi targets
49-
targets = setOf(Abi.Arm, Abi.Arm64)
49+
targets = setOf("arm", "arm64")
5050

5151
// MSRV, plugin will update rust if installed version is lower than requested
52-
minimumSupportedRustVersion = SemanticVersion("1.62.1")
52+
minimumSupportedRustVersion = "1.62.1"
5353

5454
// "debug" build type specific configuration
5555
buildType("debug") {
5656
// use "dev" profile in rust
57-
it.profile = "dev"
58-
59-
// build only Arm64 version
60-
it.targets = setOf(Abi.Arm64)
57+
profile = "dev"
6158
}
6259

6360
// "release" build type specific configuration
6461
buildType("release") {
6562
// build all supported abi versions
66-
it.targets = setOf(Abi.Arm, Abi.Arm64, Abi.X86, Abi.X86_64)
63+
targets = setOf("arm", "arm64", "x86", "x86_64")
6764
}
6865
}
6966
```
Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package dev.matrix.agp.rust
22

33
import dev.matrix.agp.rust.utils.Abi
4-
import dev.matrix.agp.rust.utils.SemanticVersion
54
import java.io.File
65

7-
@Suppress("MemberVisibilityCanBePrivate", "unused")
6+
@DslMarker
7+
annotation class AndroidRustDslMarker
8+
9+
@AndroidRustDslMarker
10+
@Suppress("unused")
811
open class AndroidRustExtension {
912
lateinit var path: File
1013

1114
var profile = ""
12-
var targets = Abi.values().toSet()
13-
var minimumSupportedRustVersion: SemanticVersion? = null
14-
15-
val buildTypes = hashMapOf(
15+
var targets = Abi.values().asSequence().map { it.androidName }.toSet()
16+
var minimumSupportedRustVersion = ""
17+
var buildTypes = hashMapOf(
1618
"debug" to AndroidRustBuildType().also {
1719
it.profile = "dev"
1820
},
@@ -21,21 +23,14 @@ open class AndroidRustExtension {
2123
},
2224
)
2325

24-
fun buildType(name: String, configure: (AndroidRustBuildType) -> Unit) {
26+
fun buildType(name: String, configure: AndroidRustBuildType.() -> Unit) {
2527
configure(buildTypes.getOrPut(name, ::AndroidRustBuildType))
2628
}
27-
28-
fun debugBuildType(configure: (AndroidRustBuildType) -> Unit) {
29-
buildType("debug", configure)
30-
}
31-
32-
fun releaseBuildType(configure: (AndroidRustBuildType) -> Unit) {
33-
buildType("release", configure)
34-
}
3529
}
3630

31+
@AndroidRustDslMarker
3732
@Suppress("unused")
3833
open class AndroidRustBuildType {
3934
var profile = ""
40-
var targets: List<Abi>? = null
35+
var targets = emptySet<String>()
4136
}

plugin/src/main/java/dev/matrix/agp/rust/AndroidRustPlugin.kt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import java.util.*
1515
// TODO: migrate to variant API with artifacts when JNI will be supported
1616
// https://developer.android.com/studio/build/extend-agp#access-modify-artifacts
1717
//
18+
@Suppress("unused")
1819
class AndroidRustPlugin : Plugin<Project> {
1920
override fun apply(project: Project) {
2021
val extension = project.extensions.create("androidRust", AndroidRustExtension::class.java)
2122
val androidExtension = project.getAndroidExtension()
2223
val androidComponents = project.getAndroidComponentsExtension()
2324
val tasksByBuildType = HashMap<String, ArrayList<TaskProvider<RustBuildTask>>>()
25+
val minimumSupportedRustVersion = SemanticVersion(extension.minimumSupportedRustVersion)
2426

2527
androidComponents.finalizeDsl { dsl ->
2628
val allRustAbiSet = HashSet<Abi>()
@@ -35,7 +37,7 @@ class AndroidRustPlugin : Plugin<Project> {
3537
val variantJniLibsDirectory = File(variantBuildDirectory, "jniLibs")
3638

3739
val rustBuildType = extension.buildTypes[buildType.name]
38-
val rustAbiSet = resolveAbiList(project, rustBuildType?.targets ?: extension.targets)
40+
val rustAbiSet = resolveAbiList(project, extension.targets, rustBuildType?.targets)
3941
allRustAbiSet.addAll(rustAbiSet)
4042

4143
val cleanTaskName = "cleanRust${buildTypeName}"
@@ -65,7 +67,7 @@ class AndroidRustPlugin : Plugin<Project> {
6567
}
6668
}
6769

68-
installRustComponentsIfNeeded(project, extension.minimumSupportedRustVersion, allRustAbiSet)
70+
installRustComponentsIfNeeded(project, minimumSupportedRustVersion, allRustAbiSet)
6971
}
7072

7173
androidComponents.onVariants { variant ->
@@ -81,15 +83,24 @@ class AndroidRustPlugin : Plugin<Project> {
8183
}
8284
}
8385

84-
private fun resolveAbiList(project: Project, requested: Collection<Abi>): Collection<Abi> {
85-
val injectedBuildAbi = Abi.fromInjectedBuildAbi(project)
86-
if (injectedBuildAbi.isEmpty()) {
86+
private fun resolveAbiList(
87+
project: Project,
88+
extension: Collection<String>,
89+
buildType: Collection<String>?,
90+
): Collection<Abi> {
91+
val requested = when (buildType != null && buildType.isNotEmpty()) {
92+
true -> Abi.fromRustNames(buildType)
93+
else -> Abi.fromRustNames(extension)
94+
}
95+
96+
val injected = Abi.fromInjectedBuildAbi(project)
97+
if (injected.isEmpty()) {
8798
return requested
8899
}
89100

90-
val intersection = requested.intersect(injectedBuildAbi)
101+
val intersection = requested.intersect(injected)
91102
check(intersection.isNotEmpty()) {
92-
"ABIs requested by IDE ($injectedBuildAbi) are not supported by the build config ($requested)"
103+
"ABIs requested by IDE ($injected) are not supported by the build config ($requested)"
93104
}
94105

95106
return when {

plugin/src/main/java/dev/matrix/agp/rust/utils/Abi.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dev.matrix.agp.rust.utils
22

33
import org.gradle.api.Project
44

5-
enum class Abi(
5+
internal enum class Abi(
66
val rustName: String,
77
val androidName: String,
88
val compilerTriple: String,
@@ -31,7 +31,7 @@ enum class Abi(
3131
rustTargetTriple = "armv7-linux-androideabi",
3232
),
3333
Arm64(
34-
rustName = "aarch64",
34+
rustName = "arm64",
3535
androidName = "arm64-v8a",
3636
compilerTriple = "aarch64-linux-android",
3737
binUtilsTriple = "aarch64-linux-android",
@@ -40,13 +40,19 @@ enum class Abi(
4040

4141
@Suppress("unused", "MemberVisibilityCanBePrivate")
4242
companion object {
43-
fun fromRustName(value: String) = values().find { it.rustName == value }
44-
fun fromAndroidName(value: String) = values().find { it.androidName == value }
43+
fun fromRustName(value: String) = values().find { it.rustName.equals(value, ignoreCase = true) }
44+
fun fromAndroidName(value: String) = values().find { it.androidName.equals(value, ignoreCase = true) }
4545

4646
fun fromInjectedBuildAbi(project: Project): Set<Abi> {
4747
val values = project.properties["android.injected.build.abi"] ?: return emptySet()
4848
return values.toString().split(",").mapNotNullTo(HashSet()) { fromAndroidName(it.trim()) }
4949
}
50+
51+
fun fromRustNames(names: Collection<String>): Set<Abi> {
52+
return names.asSequence().map {
53+
requireNotNull(fromRustName(it)) { "unsupported abi version string: ${it}" }
54+
}.toSet()
55+
}
5056
}
5157

5258
fun cc(apiLevel: Int) = when (Os.isWindows) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package dev.matrix.agp.rust.utils
22

3-
fun log(message: String) {
3+
internal fun log(message: String) {
44
println("AndroidRust: $message")
55
}

plugin/src/main/java/dev/matrix/agp/rust/utils/NullOutputStream.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ package dev.matrix.agp.rust.utils
22

33
import java.io.OutputStream
44

5-
object NullOutputStream : OutputStream() {
5+
internal object NullOutputStream : OutputStream() {
66
override fun write(value: Int) = Unit
77
}

plugin/src/main/java/dev/matrix/agp/rust/utils/Os.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dev.matrix.agp.rust.utils
22

3-
object Os {
3+
internal object Os {
44
val isWindows: Boolean
55
get() {
66
return System.getProperty("os.name").orEmpty().startsWith("windows", ignoreCase = true)

plugin/src/main/java/dev/matrix/agp/rust/utils/ProjectExt.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@ import com.android.build.gradle.LibraryExtension
88
import com.android.build.gradle.LibraryPlugin
99
import org.gradle.api.Project
1010

11-
fun Project.findAndroidPlugin() = plugins.asSequence()
11+
internal fun Project.findAndroidPlugin() = plugins.asSequence()
1212
.mapNotNull { it as? AppPlugin ?: it as? LibraryPlugin }
1313
.firstOrNull()
1414

15-
fun Project.getAndroidPlugin() = checkNotNull(findAndroidPlugin()) {
15+
internal fun Project.getAndroidPlugin() = checkNotNull(findAndroidPlugin()) {
1616
"couldn't find android AppPlugin or LibraryPlugin"
1717
}
1818

19-
fun Project.findAndroidExtension() = extensions.findByType(AppExtension::class.java)
19+
internal fun Project.findAndroidExtension() = extensions.findByType(AppExtension::class.java)
2020
?: extensions.findByType(LibraryExtension::class.java)
2121

22-
fun Project.getAndroidExtension() = checkNotNull(findAndroidExtension()) {
22+
internal fun Project.getAndroidExtension() = checkNotNull(findAndroidExtension()) {
2323
"couldn't find android AppExtension or LibraryExtension"
2424
}
2525

26-
fun Project.findAndroidComponentsExtension() = when (val it = project.extensions.getByName("androidComponents")) {
26+
internal fun Project.findAndroidComponentsExtension() = when (val it = project.extensions.getByName("androidComponents")) {
2727
is LibraryAndroidComponentsExtension -> it
2828
is ApplicationAndroidComponentsExtension -> it
2929
else -> null
3030
}
3131

32-
fun Project.getAndroidComponentsExtension() = checkNotNull(findAndroidComponentsExtension()) {
32+
internal fun Project.getAndroidComponentsExtension() = checkNotNull(findAndroidComponentsExtension()) {
3333
"couldn't find android components extension"
3434
}

plugin/src/main/java/dev/matrix/agp/rust/utils/SemanticVersion.kt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dev.matrix.agp.rust.utils
22

33
import java.io.Serializable
44

5-
data class SemanticVersion(
5+
internal data class SemanticVersion(
66
val major: Int,
77
val minor: Int,
88
val patch: Int,
@@ -24,15 +24,25 @@ data class SemanticVersion(
2424
}
2525
}
2626

27-
fun SemanticVersion(version: String?): SemanticVersion {
28-
val parts = version.orEmpty().split(".")
27+
internal fun SemanticVersion(version: String?): SemanticVersion {
28+
if (version.isNullOrEmpty()) {
29+
return SemanticVersion(0, 0, 0)
30+
}
31+
32+
val parts = version.split(".")
33+
val major = requireNotNull(parts.getOrNull(0)?.toIntOrNull()) {
34+
"failed to parse 'major' part of the version from '$version'"
35+
}
36+
val minor = requireNotNull(parts.getOrNull(1)?.toIntOrNull()) {
37+
"failed to parse 'minor' part of the version from '$version'"
38+
}
39+
val patch = requireNotNull(parts.getOrNull(2)?.toIntOrNull()) {
40+
"failed to parse 'patch' part of the version from '$version'"
41+
}
42+
2943
return SemanticVersion(
30-
major = parts.getOrNull(0)?.toIntOrNull() ?: 0,
31-
minor = parts.getOrNull(1)?.toIntOrNull() ?: 0,
32-
patch = parts.getOrNull(2)?.toIntOrNull() ?: 0,
44+
major = major,
45+
minor = minor,
46+
patch = patch,
3347
)
3448
}
35-
36-
fun SemanticVersion?.orEmpty(): SemanticVersion {
37-
return this ?: SemanticVersion(0, 0, 0)
38-
}

0 commit comments

Comments
 (0)