1 use std::path::Path;
2 use std::process::Command;
3
4 use anyhow::{bail, ensure, Result};
5
6 const LLVM_ANDROID_REL_PATH: &str = "toolchain/llvm_android";
7
8 /// Return the Android checkout's current llvm version.
9 ///
10 /// This uses android_version.get_svn_revision_number, a python function
11 /// that can't be executed directly. We spawn a Python3 program
12 /// to run it and get the result from that.
get_android_llvm_version(android_checkout: &Path) -> Result<String>13 pub fn get_android_llvm_version(android_checkout: &Path) -> Result<String> {
14 let mut command = new_android_cmd(android_checkout, "python3")?;
15 command.args([
16 "-c",
17 "import android_version; print(android_version.get_svn_revision_number(), end='')",
18 ]);
19 let output = command.output()?;
20 if !output.status.success() {
21 bail!(
22 "could not get android llvm version: {}",
23 String::from_utf8_lossy(&output.stderr)
24 );
25 }
26 let out_string = String::from_utf8(output.stdout)?.trim().to_string();
27 Ok(out_string)
28 }
29
30 /// Sort the Android patches using the cherrypick_cl.py Android utility.
31 ///
32 /// This assumes that:
33 /// 1. There exists a python script called cherrypick_cl.py
34 /// 2. That calling it with the given arguments sorts the PATCHES.json file.
35 /// 3. Calling it does nothing besides sorting the PATCHES.json file.
36 ///
37 /// We aren't doing our own sorting because we shouldn't have to update patch_sync along
38 /// with cherrypick_cl.py any time they change the __lt__ implementation.
sort_android_patches(android_checkout: &Path) -> Result<()>39 pub fn sort_android_patches(android_checkout: &Path) -> Result<()> {
40 let mut command = new_android_cmd(android_checkout, "python3")?;
41 command.args(["cherrypick_cl.py", "--reason", "patch_sync sorting"]);
42 let output = command.output()?;
43 if !output.status.success() {
44 bail!(
45 "could not sort: {}",
46 String::from_utf8_lossy(&output.stderr)
47 );
48 }
49 Ok(())
50 }
51
new_android_cmd(android_checkout: &Path, cmd: &str) -> Result<Command>52 fn new_android_cmd(android_checkout: &Path, cmd: &str) -> Result<Command> {
53 let mut command = Command::new(cmd);
54 let llvm_android_dir = android_checkout.join(LLVM_ANDROID_REL_PATH);
55 ensure!(
56 llvm_android_dir.is_dir(),
57 "can't make android command; {} is not a directory",
58 llvm_android_dir.display()
59 );
60 command.current_dir(llvm_android_dir);
61 Ok(command)
62 }
63