Skip to content

Commit e30bbc0

Browse files
author
Thom Chiovoloni
committed
Respect the CARGO_TARGET_DIR environment variable. Fixes mozilla#9
1 parent 44c4214 commit e30bbc0

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,21 @@ cargo {
215215

216216
### targetDirectory
217217

218-
The target directory into which Cargo writes built outputs.
218+
The target directory into which Cargo writes built outputs. You will likely need to specify this
219+
if you are using a [cargo virtual workspace](https://doc.rust-lang.org/book/ch14-03-cargo-workspaces.html),
220+
as our default will likely fail to locate the correct target directory.
219221

220222
Defaults to `${module}/target`. `targetDirectory` is interpreted as a relative path to the Gradle
221223
`projectDir`.
222224

225+
Note that if `CARGO_TARGET_DIR` (see https://doc.rust-lang.org/cargo/reference/environment-variables.html)
226+
is specified in the environment, it takes precedence over `targetDirectory`, as cargo will output
227+
all build artifacts to it, regardless of what is being built, or where it was invoked.
228+
223229
```groovy
224230
cargo {
225-
targetDirectory = 'release'
231+
// Note: path is relative to the gradle project root.
232+
targetDirectory = 'path/to/workspace/root/target'
226233
}
227234
```
228235

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ open class CargoBuildTask : DefaultTask() {
3131
is LibraryPlugin -> buildProjectForTarget<LibraryExtension>(project, toolchain, this)
3232
}
3333
}
34-
35-
val targetDirectory = targetDirectory ?: "${module!!}/target"
34+
// CARGO_TARGET_DIR can be used to force the use of a global, shared target directory
35+
// across all rust projects on a machine. Use it if it's set, otherwise use the
36+
// configured `targetDirectory` value, and fall back to `${module}/target`.
37+
val targetDirectory = System.getenv("CARGO_TARGET_DIR")
38+
?: targetDirectory
39+
?: "${module!!}/target"
3640

3741
val cargoOutputDir = if (toolchain.target == defaultTargetTriple) {
3842
"${targetDirectory}/${profile}"

0 commit comments

Comments
 (0)