-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathbuild.rs
More file actions
125 lines (114 loc) · 5.16 KB
/
Copy pathbuild.rs
File metadata and controls
125 lines (114 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
fn main() {
// Detect `cargo packager` builds using the `CARGO_PACKAGER_FORMAT` env var.
println!("cargo::rustc-check-cfg=cfg(packaging_build)");
println!("cargo:rerun-if-env-changed=CARGO_PACKAGER_FORMAT");
if std::env::var_os("CARGO_PACKAGER_FORMAT").is_some() {
println!("cargo:rustc-cfg=packaging_build");
}
// Note: `#[cfg(windows)]` checks the *host* OS, not the *target*.
// We must check the target env at runtime to avoid running this
// when cross-compiling (e.g., building for Android on a Windows CI runner).
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
if target_os == "windows" {
#[cfg(windows)]
{
let mut res = winresource::WindowsResource::new();
res.set_icon("resources/icon.ico");
// Explicit VERSIONINFO fields. Without these, Windows shows
// "Unknown publisher" in the UAC/SmartScreen install prompt
// (CompanyName/LegalCopyright are empty by default), and the
// ProductName/FileDescription fall back to the lowercase crate
// name "robrix" instead of the product name.
res.set("CompanyName", "GOSIM Foundation");
res.set("ProductName", "Robrix");
res.set("FileDescription", "Robrix - Matrix chat client");
res.set("LegalCopyright", "Copyright - 2023-2026 Project Robius");
res.compile().expect("Failed to compile Windows resources");
}
}
// Get version info about Robrix, the matrix SDK, and testflight.
println!("cargo:rerun-if-changed=Cargo.lock");
let (sdk_version, sdk_git_rev, sdk_url) = read_matrix_sdk_info();
println!("cargo:rustc-env=MATRIX_SDK_VERSION={sdk_version}");
println!("cargo:rustc-env=MATRIX_SDK_GIT_REV={sdk_git_rev}");
println!("cargo:rustc-env=MATRIX_SDK_URL={sdk_url}");
let (robrix_git_rev, robrix_url) = read_robrix_git_info();
println!("cargo:rustc-env=ROBRIX_GIT_COMMIT_HASH={robrix_git_rev}");
println!("cargo:rustc-env=ROBRIX_GIT_COMMIT_URL={robrix_url}");
println!("cargo:rerun-if-env-changed=TESTFLIGHT_BUILD_NUMBER");
let testflight_build = std::env::var("TESTFLIGHT_BUILD_NUMBER").unwrap_or_default();
println!("cargo:rustc-env=TESTFLIGHT_BUILD_NUMBER={testflight_build}");
}
/// Returns Robrix's own current git commit info as a commit hash and a permalink.
fn read_robrix_git_info() -> (String, String) {
// Tell cargo to re-run when the git-tracked HEAD changes.
println!("cargo:rerun-if-changed=.git/HEAD");
if let Ok(head) = std::fs::read_to_string(".git/HEAD") {
if let Some(branch_ref) = head.trim().strip_prefix("ref: ") {
println!("cargo:rerun-if-changed=.git/{branch_ref}");
}
}
let Ok(output) = std::process::Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
else {
return (String::new(), String::new());
};
if !output.status.success() {
return (String::new(), String::new());
}
let full_sha = String::from_utf8_lossy(&output.stdout).trim().to_string();
if full_sha.len() < 8 {
return (String::new(), String::new());
}
let short_rev: String = full_sha.chars().take(8).collect();
let url = format!("https://github.com/project-robius/robrix/tree/{full_sha}");
(short_rev, url)
}
/// Parses Cargo.lock to find the resolved version of `matrix-sdk`.
///
/// Returns `(version, short_git_rev, url)`.
fn read_matrix_sdk_info() -> (String, String, String) {
let Ok(lockfile_text) = std::fs::read_to_string("Cargo.lock") else {
return (String::new(), String::new(), String::new());
};
let Ok(lockfile) = toml::from_str::<toml::Value>(&lockfile_text) else {
return (String::new(), String::new(), String::new());
};
let Some(pkg) = lockfile
.get("package")
.and_then(|p| p.as_array())
.and_then(|pkgs| {
pkgs.iter().find(|p| {
p.get("name").and_then(|n| n.as_str()) == Some("matrix-sdk")
})
})
else {
return (String::new(), String::new(), String::new());
};
let version = pkg
.get("version")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
let source = pkg.get("source").and_then(|s| s.as_str()).unwrap_or("");
// Git sources look like `git+<repo-url>?<query>#<full-commit>`.
// The repo URL is the prefix before `?` or `#`; the commit is after `#`.
let (git_rev, url) = if let Some(rest) = source.strip_prefix("git+") {
let (left, full_commit) = rest.rsplit_once('#').unwrap_or((rest, ""));
let base = left.split_once('?').map_or(left, |(b, _)| b);
let short_rev: String = full_commit.chars().take(8).collect();
let url = if full_commit.is_empty() {
base.to_string()
} else {
format!("{base}/tree/{full_commit}")
};
(short_rev, url)
} else if !version.is_empty() {
// Registry/path/other sources: fall back to the crates.io URL.
(String::new(), format!("https://crates.io/crates/matrix-sdk/{version}"))
} else {
(String::new(), String::new())
};
(version, git_rev, url)
}