diff --git a/README.md b/README.md index 5f7a0f5..327fae7 100644 --- a/README.md +++ b/README.md @@ -389,7 +389,7 @@ The plugin invokes Python, Cargo and Rustc. In order of preference, the plugin 1. the value of `cargo { pythonCommand = "..." }`, if non-empty 1. `rust.pythonCommand` in `${rootDir}/local.properties` 1. the environment variable `RUST_ANDROID_GRADLE_PYTHON_COMMAND` -1. the default, `python` +1. the default: `python3` if it is on the `PATH`, otherwise `python` (always `python` on Windows) In order of preference, the plugin determines what command to invoke for Cargo by: diff --git a/plugin/src/main/kotlin/com/nishtahir/CargoExtension.kt b/plugin/src/main/kotlin/com/nishtahir/CargoExtension.kt index 0d57390..37e5f59 100644 --- a/plugin/src/main/kotlin/com/nishtahir/CargoExtension.kt +++ b/plugin/src/main/kotlin/com/nishtahir/CargoExtension.kt @@ -97,10 +97,19 @@ open class CargoExtension { return if (!field.isEmpty()) { field } else { - getProperty("rust.pythonCommand", "RUST_ANDROID_GRADLE_PYTHON_COMMAND") ?: "python" + getProperty("rust.pythonCommand", "RUST_ANDROID_GRADLE_PYTHON_COMMAND") ?: defaultPythonCommand() } } + private fun defaultPythonCommand(): String { + if (System.getProperty("os.name").startsWith("Windows")) { + return "python" + } + val path = System.getenv("PATH") ?: return "python" + val hasPython3 = path.split(File.pathSeparator).any { File(it, "python3").canExecute() } + return if (hasPython3) "python3" else "python" + } + // Required so that we can parse the default triple out of `rustc --version --verbose`. Sadly, // there seems to be no way to get this information out of cargo directly. Failure to locate // this isn't fatal, however.