Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb4c264

Browse files
committed
Remove dependency on python
1 parent e02e8a0 commit fb4c264

8 files changed

Lines changed: 106 additions & 72 deletions

File tree

README.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -384,16 +384,9 @@ rust.targets.library=linux-x86-64
384384
rust.targets=arm,linux-x86-64,darwin
385385
```
386386

387-
## Specifying paths to sub-commands (Python, Cargo, and Rustc)
387+
## Specifying paths to sub-commands (Cargo, and Rustc)
388388

389-
The plugin invokes Python, Cargo and Rustc. In order of preference, the plugin determines what command to invoke for Python by:
390-
391-
1. the value of `cargo { pythonCommand = "..." }`, if non-empty
392-
1. `rust.pythonCommand` in `${rootDir}/local.properties`
393-
1. the environment variable `RUST_ANDROID_GRADLE_PYTHON_COMMAND`
394-
1. the default, `python`
395-
396-
In order of preference, the plugin determines what command to invoke for Cargo by:
389+
The plugin invokes Cargo and Rustc. In order of preference, the plugin determines what command to invoke for Cargo by:
397390

398391
1. the value of `cargo { cargoCommand = "..." }`, if non-empty
399392
1. `rust.cargoCommand` in `${rootDir}/local.properties`
@@ -412,7 +405,7 @@ In order of preference, the plugin determines what command to invoke for `rustc`
412405
Paths must be host operating system specific. For example, on Windows:
413406

414407
```properties
415-
rust.pythonCommand=c:\Python27\bin\python
408+
rust.cargoCommand=c:\cargo\bin\cargo
416409
```
417410

418411
On Linux,

plugin/src/main/kotlin/net/mullvad/androidrust/CargoBuildTask.kt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ abstract class CargoBuildTask : DefaultTask() {
4343

4444
@Input @Optional val verbose = property<Boolean>()
4545

46-
@Input val pythonCommand = property<String>()
47-
4846
@Input val featureSpec = property<FeatureSpec>()
4947

5048
@Input val toolchainDirectory = property<File>()
@@ -209,9 +207,9 @@ abstract class CargoBuildTask : DefaultTask() {
209207

210208
val linkerWrapper =
211209
if (System.getProperty("os.name").startsWith("Windows")) {
212-
File(buildDir, "linker-wrapper/linker-wrapper.bat")
210+
File(buildDir, "linker-wrapper/linker-wrapper.exe")
213211
} else {
214-
File(buildDir, "linker-wrapper/linker-wrapper.sh")
212+
File(buildDir, "linker-wrapper/linker-wrapper")
215213
}
216214
environment("CARGO_TARGET_${toolchainTarget}_LINKER", linkerWrapper.path)
217215

@@ -238,11 +236,6 @@ abstract class CargoBuildTask : DefaultTask() {
238236
}
239237

240238
// Configure our linker wrapper.
241-
environment("RUST_ANDROID_GRADLE_PYTHON_COMMAND", pythonCommand.get())
242-
environment(
243-
"RUST_ANDROID_GRADLE_LINKER_WRAPPER_PY",
244-
File(buildDir, "linker-wrapper/linker-wrapper.py").path,
245-
)
246239
environment("RUST_ANDROID_GRADLE_CC", cc)
247240
environment(
248241
"RUST_ANDROID_GRADLE_CC_LINK_ARG",

plugin/src/main/kotlin/net/mullvad/androidrust/CargoExtension.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ open class CargoExtension {
3838
var apiLevels: Map<String, Int> = mapOf()
3939
var extraCargoBuildArguments: List<String>? = null
4040
var generateBuildId: Boolean = false
41-
var pythonCommand: String = ""
42-
get() {
43-
return field.ifEmpty {
44-
getProperty("rust.pythonCommand", "RUST_ANDROID_GRADLE_PYTHON_COMMAND") ?: "python"
45-
}
46-
}
4741

4842
var environmentalOverrides = mutableMapOf<String, String>()
4943

plugin/src/main/kotlin/net/mullvad/androidrust/RustAndroidPlugin.kt

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import org.gradle.api.GradleException
1313
import org.gradle.api.Plugin
1414
import org.gradle.api.Project
1515
import org.gradle.api.file.DuplicatesStrategy
16+
import org.gradle.api.tasks.Exec
1617
import org.gradle.api.tasks.Sync
1718

1819
const val RUST_TASK_GROUP = "rust"
@@ -278,14 +279,40 @@ open class RustAndroidPlugin : Plugin<Project> {
278279
.path
279280
)
280281
)
281-
include("**/linker-wrapper*")
282+
include("**/linker-wrapper.rs")
282283
into(File(rootBuildDir, "linker-wrapper"))
283284
eachFile { it.path = it.path.replaceFirst("net/mullvad/androidrust", "") }
284285
filePermissions { it.unix("755") }
285286
includeEmptyDirs = false
286287
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
287288
}
288289

290+
val compileLinkerWrapper =
291+
tasks.maybeCreate("compileLinkerWrapper", Exec::class.java).apply {
292+
group = RUST_TASK_GROUP
293+
description = "Compile shared linker wrapper binary"
294+
dependsOn(generateLinkerWrapper)
295+
296+
val linkerWrapperRs = File(rootBuildDir, "linker-wrapper/linker-wrapper.rs")
297+
val linkerWrapperBin =
298+
if (System.getProperty("os.name").startsWith("Windows")) {
299+
File(rootBuildDir, "linker-wrapper/linker-wrapper.exe")
300+
} else {
301+
File(rootBuildDir, "linker-wrapper/linker-wrapper")
302+
}
303+
304+
inputs.file(linkerWrapperRs)
305+
outputs.file(linkerWrapperBin)
306+
307+
workingDir(File(rootBuildDir, "linker-wrapper"))
308+
commandLine(
309+
cargoExtension.rustcCommand,
310+
linkerWrapperRs.path,
311+
"-o",
312+
linkerWrapperBin.path,
313+
)
314+
}
315+
289316
val buildTask =
290317
tasks.maybeCreate("cargoBuild", DefaultTask::class.java).apply {
291318
group = RUST_TASK_GROUP
@@ -354,7 +381,6 @@ open class RustAndroidPlugin : Plugin<Project> {
354381
toolchainDirectory.set(cargoExtension.toolchainDirectory)
355382
generateBuildId.set(cargoExtension.generateBuildId)
356383
extraCargoBuildArguments.set(cargoExtension.extraCargoBuildArguments)
357-
pythonCommand.set(cargoExtension.pythonCommand)
358384
autoConfigureClangSys.set(cargoExtension.autoConfigureClangSys)
359385

360386
this.apiLevel.set(cargoExtension.apiLevels[theToolchain.platform]!!)
@@ -365,7 +391,7 @@ open class RustAndroidPlugin : Plugin<Project> {
365391

366392
buildTask.dependsOn(targetBuildTask)
367393

368-
targetBuildTask.dependsOn(generateLinkerWrapper)
394+
targetBuildTask.dependsOn(compileLinkerWrapper)
369395
}
370396
}
371397
}

plugin/src/main/resources/net/mullvad/androidrust/linker-wrapper.bat

Lines changed: 0 additions & 1 deletion
This file was deleted.

plugin/src/main/resources/net/mullvad/androidrust/linker-wrapper.py

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use std::env;
2+
use std::fs;
3+
use std::process::Command;
4+
5+
fn update_args(args: &mut Vec<String>, ndk_major_version: i32) {
6+
if ndk_major_version >= 23 {
7+
for arg in args.iter_mut() {
8+
if arg.starts_with("-lgcc") {
9+
*arg = format!("-lunwind{}", &arg[5..]);
10+
}
11+
}
12+
}
13+
}
14+
15+
fn main() {
16+
let ndk_major_version = env::var("CARGO_NDK_MAJOR_VERSION")
17+
.unwrap_or_default()
18+
.parse::<i32>()
19+
.unwrap_or(0);
20+
21+
let cc = env::var("RUST_ANDROID_GRADLE_CC").expect("RUST_ANDROID_GRADLE_CC not set");
22+
let cc_link_arg = env::var("RUST_ANDROID_GRADLE_CC_LINK_ARG").expect("RUST_ANDROID_GRADLE_CC_LINK_ARG not set");
23+
24+
let mut args: Vec<String> = env::args().skip(1).collect();
25+
26+
// Update main arguments
27+
update_args(&mut args, ndk_major_version);
28+
29+
// Update response files if any
30+
for arg in &args {
31+
if arg.starts_with("@") {
32+
let path = &arg[1..];
33+
if let Ok(content) = fs::read_to_string(path) {
34+
// Split into lines while preserving line endings as much as possible
35+
// The python code used splitlines(keepends=True)
36+
// We'll simulate this by looking for line boundaries.
37+
let mut lines = Vec::new();
38+
let mut start = 0;
39+
for (i, c) in content.char_indices() {
40+
if c == '\n' {
41+
lines.push(content[start..=i].to_string());
42+
start = i + 1;
43+
}
44+
}
45+
if start < content.len() {
46+
lines.push(content[start..].to_string());
47+
}
48+
49+
let mut modified = false;
50+
for line in lines.iter_mut() {
51+
if ndk_major_version >= 23 && line.starts_with("-lgcc") {
52+
*line = format!("-lunwind{}", &line[5..]);
53+
modified = true;
54+
}
55+
}
56+
57+
if modified {
58+
let new_content = lines.concat();
59+
let _ = fs::write(path, new_content);
60+
}
61+
}
62+
}
63+
}
64+
65+
let status = Command::new(&cc)
66+
.arg(cc_link_arg)
67+
.args(&args)
68+
.status()
69+
.expect("Failed to execute linker");
70+
71+
std::process::exit(status.code().unwrap_or(1));
72+
}

plugin/src/main/resources/net/mullvad/androidrust/linker-wrapper.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)