Skip to content

Commit 196528e

Browse files
committed
Allow module and targetDirectory to be absolute paths.
1 parent 75093fe commit 196528e

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,12 @@ to `debug`!*
120120

121121
### module
122122

123-
The path to the Rust library to build with Cargo; required. `module` is interpreted as a relative
124-
path to the Gradle `projectDir`.
123+
The path to the Rust library to build with Cargo; required. `module` can be absolute; if it is not,
124+
it is interpreted as a path relative to the Gradle `projectDir`.
125125

126126
```groovy
127127
cargo {
128+
// Note: path is either absolute, or relative to the gradle project's `projectDir`.
128129
module = '../rust'
129130
}
130131
```
@@ -256,8 +257,8 @@ The target directory into which Cargo writes built outputs. You will likely need
256257
if you are using a [cargo virtual workspace](https://doc.rust-lang.org/book/ch14-03-cargo-workspaces.html),
257258
as our default will likely fail to locate the correct target directory.
258259

259-
Defaults to `${module}/target`. `targetDirectory` is interpreted as a relative path to the Gradle
260-
`projectDir`.
260+
Defaults to `${module}/target`. `targetDirectory` can be absolute; if it is not, it is interpreted
261+
as a path relative to the Gradle `projectDir`.
261262

262263
Note that if `CARGO_TARGET_DIR` (see https://doc.rust-lang.org/cargo/reference/environment-variables.html)
263264
is specified in the environment, it takes precedence over `targetDirectory`, as cargo will output
@@ -270,7 +271,7 @@ library on a per-machine basis.
270271

271272
```groovy
272273
cargo {
273-
// Note: path is relative to the gradle project's `projectDir`
274+
// Note: path is either absolute, or relative to the gradle project's `projectDir`.
274275
targetDirectory = 'path/to/workspace/root/target'
275276
}
276277
```

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

+21-7
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,29 @@ open class CargoBuildTask : DefaultTask() {
3737
// We also allow this to be specified in `local.properties`, not because this is
3838
// something you should ever need to do currently, but we don't want it to ruin anyone's
3939
// day if it turns out we're wrong about that.
40-
val targetDirectory =
40+
val target =
4141
getProperty("rust.cargoTargetDir", "CARGO_TARGET_DIR")
4242
?: targetDirectory
4343
?: "${module!!}/target"
4444

4545
val defaultTargetTriple = getDefaultTargetTriple(project, rustcCommand)
4646

47-
val cargoOutputDir = if (toolchain.target == defaultTargetTriple) {
48-
"${targetDirectory}/${profile}"
47+
var cargoOutputDir = File(if (toolchain.target == defaultTargetTriple) {
48+
"${target}/${profile}"
4949
} else {
50-
"${targetDirectory}/${toolchain.target}/${profile}"
50+
"${target}/${toolchain.target}/${profile}"
51+
})
52+
if (!cargoOutputDir.isAbsolute) {
53+
cargoOutputDir = File(project.project.projectDir, cargoOutputDir.path)
5154
}
55+
cargoOutputDir = cargoOutputDir.canonicalFile
56+
57+
val intoDir = File(buildDir, "rustJniLibs/${toolchain.folder}")
58+
intoDir.mkdirs()
59+
5260
copy { spec ->
53-
spec.from(File(project.projectDir, cargoOutputDir))
54-
spec.into(File(buildDir, "rustJniLibs/${toolchain.folder}"))
61+
spec.from(cargoOutputDir)
62+
spec.into(intoDir)
5563

5664
// Need to capture the value to dereference smoothly.
5765
val targetIncludes = targetIncludes
@@ -76,7 +84,13 @@ open class CargoBuildTask : DefaultTask() {
7684
project.exec { spec ->
7785
with(spec) {
7886
standardOutput = System.out
79-
workingDir = File(project.project.projectDir, cargoExtension.module!!)
87+
val module = File(cargoExtension.module!!)
88+
if (module.isAbsolute) {
89+
workingDir = module
90+
} else {
91+
workingDir = File(project.project.projectDir, module.path)
92+
}
93+
workingDir = workingDir.canonicalFile
8094

8195
val theCommandLine = mutableListOf(cargoExtension.cargoCommand)
8296

0 commit comments

Comments
 (0)