-
Notifications
You must be signed in to change notification settings - Fork 72
/
CargoBuildTask.kt
237 lines (209 loc) · 11.5 KB
/
CargoBuildTask.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.nishtahir;
import com.android.build.gradle.*
import org.apache.tools.ant.taskdefs.condition.Os
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.logging.LogLevel
import org.gradle.api.tasks.TaskAction
import java.io.ByteArrayOutputStream
import java.io.File
open class CargoBuildTask : DefaultTask() {
var toolchain: Toolchain? = null
@Suppress("unused")
@TaskAction
fun build() = with(project) {
extensions[CargoExtension::class].apply {
// Need to capture the value to dereference smoothly.
val toolchain = toolchain
if (toolchain == null) {
throw GradleException("toolchain cannot be null")
}
project.plugins.all {
when (it) {
is AppPlugin -> buildProjectForTarget<AppExtension>(project, toolchain, this)
is LibraryPlugin -> buildProjectForTarget<LibraryExtension>(project, toolchain, this)
}
}
// CARGO_TARGET_DIR can be used to force the use of a global, shared target directory
// across all rust projects on a machine. Use it if it's set, otherwise use the
// configured `targetDirectory` value, and fall back to `${module}/target`.
//
// We also allow this to be specified in `local.properties`, not because this is
// something you should ever need to do currently, but we don't want it to ruin anyone's
// day if it turns out we're wrong about that.
val targetDirectory =
getProperty("rust.cargoTargetDir", "CARGO_TARGET_DIR")
?: targetDirectory
?: "${module!!}/target"
val defaultTargetTriple = getDefaultTargetTriple(project, rustcCommand)
val cargoOutputDir = if (toolchain.target == defaultTargetTriple) {
"${targetDirectory}/${profile}"
} else {
"${targetDirectory}/${toolchain.target}/${profile}"
}
copy { spec ->
spec.from(File(project.projectDir, cargoOutputDir))
spec.into(File(buildDir, "rustJniLibs/${toolchain.folder}"))
// Need to capture the value to dereference smoothly.
val targetIncludes = targetIncludes
if (targetIncludes != null) {
spec.include(targetIncludes.asIterable())
} else {
// It's safe to unwrap, since we bailed at configuration time if this is unset.
val libname = libname!!
spec.include("lib${libname}.so")
spec.include("lib${libname}.dylib")
spec.include("${libname}.dll")
}
}
}
}
inline fun <reified T : BaseExtension> buildProjectForTarget(project: Project, toolchain: Toolchain, cargoExtension: CargoExtension) {
val app = project.extensions[T::class]
val apiLevel = cargoExtension.apiLevel ?: app.defaultConfig.minSdkVersion.apiLevel
val defaultTargetTriple = getDefaultTargetTriple(project, cargoExtension.rustcCommand)
project.exec { spec ->
with(spec) {
standardOutput = System.out
workingDir = File(project.project.projectDir, cargoExtension.module!!)
val theCommandLine = mutableListOf(cargoExtension.cargoCommand, "build");
// Respect `verbose` if it is set; otherwise, log if asked to
// with `--info` or `--debug` from the command line.
if (cargoExtension.verbose ?: project.logger.isEnabled(LogLevel.INFO)) {
theCommandLine.add("--verbose")
}
val features = cargoExtension.featureSpec.features
// We just pass this along to cargo as something space separated... AFAICT
// you're allowed to have featureSpec with spaces in them, but I don't think
// there's a way to specify them in the cargo command line -- rustc accepts
// them if passed in directly with `--cfg`, and cargo will pass them to rustc
// if you use them as default featureSpec.
when (features) {
is Features.All -> {
theCommandLine.add("--all-features")
}
is Features.DefaultAnd -> {
if (!features.featureSet.isEmpty()) {
theCommandLine.add("--features")
theCommandLine.add(features.featureSet.joinToString(" "))
}
}
is Features.NoDefaultBut -> {
theCommandLine.add("--no-default-features")
if (!features.featureSet.isEmpty()) {
theCommandLine.add("--features")
theCommandLine.add(features.featureSet.joinToString(" "))
}
}
}
if (cargoExtension.profile != "debug") {
// Cargo is rigid: it accepts "--release" for release (and
// nothing for dev). This is a cheap way of allowing only
// two values.
theCommandLine.add("--${cargoExtension.profile}")
}
if (toolchain.target != defaultTargetTriple) {
// Only providing --target for the non-default targets means desktop builds
// can share the build cache with `cargo build`/`cargo test`/etc invocations,
// instead of requiring a large amount of redundant work.
theCommandLine.add("--target=${toolchain.target}")
}
// Target-specific environment configuration, passed through to
// the underlying `cargo build` invocation.
val toolchain_target = toolchain.target.toUpperCase().replace('-', '_')
val prefix = "RUST_ANDROID_GRADLE_TARGET_${toolchain_target}_"
// For ORG_GRADLE_PROJECT_RUST_ANDROID_GRADLE_TARGET_x_KEY=VALUE, set KEY=VALUE.
project.logger.info("Passing through project properties with prefix '${prefix}' (environment variables with prefix 'ORG_GRADLE_PROJECT_${prefix}'")
project.properties.forEach { (key, value) ->
if (key.startsWith(prefix)) {
val realKey = key.substring(prefix.length)
project.logger.debug("Passing through environment variable '${key}' as '${realKey}=${value}'")
environment(realKey, value)
}
}
// Cross-compiling to Android requires toolchain massaging.
if (toolchain.type != ToolchainType.DESKTOP) {
val toolchainDirectory = if (toolchain.type == ToolchainType.ANDROID_PREBUILT) {
val ndkPath = app.ndkDirectory
val hostTag = if (Os.isFamily(Os.FAMILY_WINDOWS)) {
if (Os.isArch("x86_64") || Os.isArch("amd64")) {
"windows-x86_64"
} else {
"windows"
}
} else if (Os.isFamily(Os.FAMILY_MAC)) {
"darwin-x86_64"
} else {
"linux-x86_64"
}
File("$ndkPath/toolchains/llvm/prebuilt", hostTag)
} else {
cargoExtension.toolchainDirectory
}
// Be aware that RUSTFLAGS can have problems with embedded
// spaces, but that shouldn't be a problem here.
val cc = File(toolchainDirectory, "${toolchain.cc(apiLevel)}").path;
val cxx = File(toolchainDirectory, "${toolchain.cxx(apiLevel)}").path;
val ar = File(toolchainDirectory, "${toolchain.ar(apiLevel)}").path;
// For cargo: like "CARGO_TARGET_I686_LINUX_ANDROID_CC". This is really weakly
// documented; see https://github.com/rust-lang/cargo/issues/5690 and follow
// links from there.
environment("CARGO_TARGET_${toolchain_target}_CC", cc)
environment("CARGO_TARGET_${toolchain_target}_AR", ar)
val linker_wrapper =
if (System.getProperty("os.name").startsWith("Windows")) {
File(project.rootProject.buildDir, "linker-wrapper/linker-wrapper.bat")
} else {
File(project.rootProject.buildDir, "linker-wrapper/linker-wrapper.sh")
}
environment("CARGO_TARGET_${toolchain_target}_LINKER", linker_wrapper.path)
// For build.rs in `cc` consumers: like "CC_i686-linux-android". See
// https://github.com/alexcrichton/cc-rs#external-configuration-via-environment-variables.
environment("CC_${toolchain.target}", cc)
environment("CXX_${toolchain.target}", cxx)
environment("AR_${toolchain.target}", ar)
// Configure our linker wrapper.
environment("RUST_ANDROID_GRADLE_PYTHON_COMMAND", cargoExtension.pythonCommand)
environment("RUST_ANDROID_GRADLE_LINKER_WRAPPER_PY",
File(project.rootProject.buildDir, "linker-wrapper/linker-wrapper.py").path)
environment("RUST_ANDROID_GRADLE_CC", cc)
environment("RUST_ANDROID_GRADLE_CC_LINK_ARG", "-Wl,-soname,lib${cargoExtension.libname!!}.so")
}
cargoExtension.extraCargoBuildArguments?.let {
theCommandLine.addAll(it)
}
commandLine = theCommandLine
}
if (cargoExtension.exec != null) {
(cargoExtension.exec!!)(spec, toolchain)
}
}.assertNormalExitValue()
}
}
// This can't be private/internal as it's called from `buildProjectForTarget`.
fun getDefaultTargetTriple(project: Project, rustc: String): String? {
val stdout = ByteArrayOutputStream()
val result = project.exec { spec ->
spec.standardOutput = stdout
spec.commandLine = listOf(rustc, "--version", "--verbose")
}
if (result.exitValue != 0) {
project.logger.warn(
"Failed to get default target triple from rustc (exit code: ${result.exitValue})")
return null
}
val output = stdout.toString()
// The `rustc --version --verbose` output contains a number of lines like `key: value`.
// We're only interested in `host: `, which corresponds to the default target triple.
val triplePrefix = "host: "
val triple = output.split("\n")
.find { it.startsWith(triplePrefix) }
?.let { it.substring(triplePrefix.length).trim() }
if (triple == null) {
project.logger.warn("Failed to parse `rustc -Vv` output! (Please report a rust-android-gradle bug)")
} else {
project.logger.info("Default rust target triple: $triple")
}
return triple
}