Skip to content

Commit

Permalink
Respect the CARGO_TARGET_DIR environment variable. Fixes mozilla#9
Browse files Browse the repository at this point in the history
  • Loading branch information
Thom Chiovoloni committed Aug 19, 2019
1 parent fcea238 commit 056ba06
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,21 @@ cargo {

### targetDirectory

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

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

Note that if `CARGO_TARGET_DIR` (see https://doc.rust-lang.org/cargo/reference/environment-variables.html)
is specified in the environment, it takes precedence over `targetDirectory`, as cargo will output
all build artifacts to it, regardless of what is being built, or where it was invoked.

```groovy
cargo {
targetDirectory = 'release'
// Note: path is relative to the gradle project root.
targetDirectory = 'path/to/workspace/root/target'
}
```

Expand Down
8 changes: 6 additions & 2 deletions plugin/src/main/kotlin/com/nishtahir/CargoBuildTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ open class CargoBuildTask : DefaultTask() {
is LibraryPlugin -> buildProjectForTarget<LibraryExtension>(project, toolchain, this)
}
}

val targetDirectory = targetDirectory ?: "${module!!}/target"
// 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`.
val targetDirectory = System.getenv("CARGO_TARGET_DIR")
?: targetDirectory
?: "${module!!}/target"

copy { spec ->
spec.from(File(project.projectDir, "${targetDirectory}/${toolchain.target}/${profile}"))
Expand Down

0 comments on commit 056ba06

Please sign in to comment.