-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathCargoExtension.kt
142 lines (121 loc) · 4.61 KB
/
CargoExtension.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.nishtahir
import org.gradle.api.Action
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.process.ExecSpec
import java.io.File
import java.util.*
sealed class Features {
class All() : Features()
data class DefaultAnd(val featureSet: Set<String>) : Features()
data class NoDefaultBut(val featureSet: Set<String>) : Features()
}
data class FeatureSpec(var features: Features? = null) {
fun all() {
this.features = Features.All()
}
fun defaultAnd(featureSet: Array<String>) {
this.features = Features.DefaultAnd(featureSet.toSet())
}
fun noDefaultBut(featureSet: Array<String>) {
this.features = Features.NoDefaultBut(featureSet.toSet())
}
}
// `CargoExtension` is documented in README.md.
open class CargoExtension {
lateinit var localProperties: Properties
var module: String? = null
var libname: String? = null
var targets: List<String>? = null
var prebuiltToolchains: Boolean? = null
var profile: String = "debug"
var verbose: Boolean? = null
var targetDirectory: String? = null
var targetIncludes: Array<String>? = null
var apiLevel: Int? = null
var apiLevels: Map<String, Int> = mapOf()
var extraCargoBuildArguments: List<String>? = null
var generateBuildId: Boolean = false
// It would be nice to use a receiver here, but there are problems interoperating with Groovy
// and Kotlin that are just not worth working out. Another JVM language, yet another dynamic
// invoke solution :(
var exec: ((ExecSpec, Toolchain) -> Unit)? = null
var featureSpec: FeatureSpec = FeatureSpec()
fun features(action: Action<FeatureSpec>) {
action.execute(featureSpec)
}
val toolchainDirectory: File
get() {
// Share a single toolchain directory, if one is configured. Prefer "local.properties"
// to "ANDROID_NDK_TOOLCHAIN_DIR" to "$TMP/rust-android-ndk-toolchains".
val local: String? = localProperties.getProperty("rust.androidNdkToolchainDir")
if (local != null) {
return File(local).absoluteFile
}
val globalDir: String? = System.getenv("ANDROID_NDK_TOOLCHAIN_DIR")
if (globalDir != null) {
return File(globalDir).absoluteFile
}
var defaultDir = File(System.getProperty("java.io.tmpdir"), "rust-android-ndk-toolchains")
return defaultDir.absoluteFile
}
var cargoCommand: String = ""
get() {
return if (!field.isEmpty()) {
field
} else {
getProperty("rust.cargoCommand", "RUST_ANDROID_GRADLE_CARGO_COMMAND") ?: "cargo"
}
}
var rustupChannel: String = ""
get() {
return if (!field.isEmpty()) {
field
} else {
getProperty("rust.rustupChannel", "RUST_ANDROID_GRADLE_RUSTUP_CHANNEL") ?: ""
}
}
var pythonCommand: String = ""
get() {
return if (!field.isEmpty()) {
field
} else {
getProperty("rust.pythonCommand", "RUST_ANDROID_GRADLE_PYTHON_COMMAND") ?: "python"
}
}
// Required so that we can parse the default triple out of `rustc --version --verbose`. Sadly,
// there seems to be no way to get this information out of cargo directly. Failure to locate
// this isn't fatal, however.
var rustcCommand: String = ""
get() {
return if (!field.isEmpty()) {
field
} else {
getProperty("rust.rustcCommand", "RUST_ANDROID_GRADLE_RUSTC_COMMAND") ?: "rustc"
}
}
fun getFlagProperty(camelCaseName: String, snakeCaseName: String, ifUnset: Boolean): Boolean {
val propVal = getProperty(camelCaseName, snakeCaseName)
if (propVal == "1" || propVal == "true") {
return true
}
if (propVal == "0" || propVal == "false") {
return false
}
if (propVal == null || propVal == "") {
return ifUnset
}
throw GradleException("Illegal value for property \"$camelCaseName\" / \"$snakeCaseName\". Must be 0/1/true/false if set")
}
internal fun getProperty(camelCaseName: String, snakeCaseName: String): String? {
val local: String? = localProperties.getProperty(camelCaseName)
if (local != null) {
return local
}
val global: String? = System.getenv(snakeCaseName)
if (global != null) {
return global
}
return null
}
}