mozilla / rust-android-gradle

Apache License 2.0
1.03k stars 67 forks source link

Empty rustJniLibs/ folder when CARGO_TARGET_DIR is defined #125

Open chtimi59 opened 1 year ago

chtimi59 commented 1 year ago

Version 0.9.3

I've try to change CARGO_TARGET_DIR per target like so:

cargo {
        ...        
        exec { spec, toolchain ->
                spec.environment("CARGO_TARGET_DIR", rustOutputFolder(toolchain.platform))
        }
    }

As the result, even if, the build folder build/rustJniLibs/.. is created and rust build succeed,
the folder stay empty !

chtimi59 commented 1 year ago

After some investigation I've found this:

https://github.com/mozilla/rust-android-gradle/blob/master/plugin/src/main/kotlin/com/nishtahir/CargoBuildTask.kt#L42:

// 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 target =
    getProperty("rust.cargoTargetDir", "CARGO_TARGET_DIR")
    ?: targetDirectory
    ?: "${module!!}/target"

And I've realized that getProperty() is defined as follow:

https://github.com/mozilla/rust-android-gradle/blob/master/plugin/src/main/kotlin/com/nishtahir/CargoExtension.kt#L130

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
}

I guess it makes sens then:

spec.environment("CARGO_TARGET_DIR", rustOutputFolder(toolchain.platform))

won't populate:

System.getenv("CARGO_TARGET_DIR")
chtimi59 commented 1 year ago

possible little hack (?)

 exec { spec, toolchain ->
        spec.environment("CARGO_TARGET_DIR", rustOutputFolder(toolchain.platform))
        localProperties.setProperty("rust.cargoTargetDir", "${rustOutputFolder(toolchain.platform)}")
}