-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.rs
More file actions
277 lines (235 loc) · 9.64 KB
/
build.rs
File metadata and controls
277 lines (235 loc) · 9.64 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use std::{env, fs, path::PathBuf};
fn main() {
// Set CMAKE_GENERATOR to Ninja for SDL3 build
println!("cargo:rustc-env=CMAKE_GENERATOR=Ninja");
generate_viiper_metadata();
// On Linux, VIIPER must be used with elevated permissions,
// so we do not bundle and tell users to install VIIPER separately
#[cfg(not(target_os = "linux"))]
fetch_viiper_binary();
#[cfg(windows)]
{
let mut res = winres::WindowsResource::new();
res.set_icon("assets/icon.ico");
let version = option_env!("SISR_VERSION")
.or(option_env!("CARGO_PKG_VERSION"))
.unwrap_or("0.0.1");
let version_clean = version.strip_prefix('v').unwrap_or(version);
let version_parts: Vec<&str> = version_clean
.split('-')
.next()
.unwrap_or(version_clean)
.split('.')
.collect();
let major = version_parts
.get(0)
.and_then(|s| s.parse::<u64>().ok())
.unwrap_or(0);
let minor = version_parts
.get(1)
.and_then(|s| s.parse::<u64>().ok())
.unwrap_or(0);
let patch = version_parts
.get(2)
.and_then(|s| s.parse::<u64>().ok())
.unwrap_or(0);
res.set_version_info(
winres::VersionInfo::PRODUCTVERSION,
(major << 48) | (minor << 32) | (patch << 16),
);
res.set_version_info(
winres::VersionInfo::FILEVERSION,
(major << 48) | (minor << 32) | (patch << 16),
);
res.set("FileVersion", &format!("{}.{}.{}.0", major, minor, patch));
res.set("ProductVersion", version_clean);
res.set("ProductName", "SISR");
res.set("FileDescription", "SISR - Steam Input System Redirector");
res.set("CompanyName", "Peter Repukat");
res.set(
"LegalCopyright",
"Copyright (C) 2025 Peter Repukat - GPL-3.0",
);
res.set("OriginalFilename", "SISR.exe");
res.set("InternalName", "sisr");
let manifest = format!(
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="{}.{}.{}.0"
publicKeyToken="0000000000000000"
name="SISR"
type="win32"
/>
<description>SISR - Steam Input System Redirector</description>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
</windowsSettings>
</application>
</assembly>
"#,
major, minor, patch
);
res.set_manifest(&manifest);
if let Err(e) = res.compile() {
eprintln!("Warning: Failed to compile Windows resources: {}", e);
}
}
println!("cargo:rerun-if-env-changed=SISR_VERSION");
println!("cargo:rerun-if-changed=Cargo.toml");
println!("cargo:rerun-if-changed=assets/icon.ico");
}
fn generate_viiper_metadata() {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
let cargo_toml_path = manifest_dir.join("Cargo.toml");
let cargo_toml = fs::read_to_string(&cargo_toml_path)
.unwrap_or_else(|e| panic!("Failed to read {}: {e}", cargo_toml_path.display()));
let doc: toml::Table = cargo_toml
.parse()
.unwrap_or_else(|e| panic!("Failed to parse {} as TOML: {e}", cargo_toml_path.display()));
let viiper = doc
.get("package")
.and_then(|v| v.get("metadata"))
.and_then(|v| v.get("viiper"));
let min_version = viiper
.and_then(|v| v.get("viiper-version"))
.and_then(|v| v.as_str())
.unwrap_or("0.0.0");
let allow_dev = viiper
.and_then(|v| v.get("allow_dev"))
.and_then(|v| v.as_bool())
.unwrap_or(false);
let fetch_prelease = viiper
.and_then(|v| v.get("fetch_prelease"))
.and_then(|v| v.as_bool())
.unwrap_or(false);
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR"));
let out_path = out_dir.join("viiper_metadata.rs");
let generated = format!(
"// @generated by build.rs; do not edit by hand.\n\n\
pub const VIIPER_MIN_VERSION: &str = {min_version:?};\n\
pub const VIIPER_ALLOW_DEV: bool = {allow_dev};\n\
pub const VIIPER_FETCH_PRELEASE: bool = {fetch_prelease};\n",
);
fs::write(&out_path, generated)
.unwrap_or_else(|e| panic!("Failed to write {}: {e}", out_path.display()));
println!("cargo:rerun-if-changed={}", cargo_toml_path.display());
}
fn fetch_viiper_binary() {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
let cargo_toml_path = manifest_dir.join("Cargo.toml");
let cargo_toml = fs::read_to_string(&cargo_toml_path)
.unwrap_or_else(|e| panic!("Failed to read {}: {e}", cargo_toml_path.display()));
let doc: toml::Table = cargo_toml
.parse()
.unwrap_or_else(|e| panic!("Failed to parse {} as TOML: {e}", cargo_toml_path.display()));
let viiper = doc
.get("package")
.and_then(|v| v.get("metadata"))
.and_then(|v| v.get("viiper"));
let fetch_prerelease = viiper
.and_then(|v| v.get("fetch_prelease"))
.and_then(|v| v.as_bool())
.unwrap_or(false);
let min_version = viiper
.and_then(|v| v.get("viiper-version"))
.and_then(|v| v.as_str())
.unwrap_or_else(|| panic!("package.metadata.viiper.viiper-version must be set"))
.trim();
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_else(|_| "unknown".into());
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_else(|_| "unknown".into());
let target_triple = env::var("TARGET").unwrap_or_else(|_| "unknown".into());
let is_windows = target_os == "windows";
let asset_name = match (target_os.as_str(), target_arch.as_str()) {
("windows", "x86_64") => "viiper-windows-amd64.exe",
("windows", "aarch64") => "viiper-windows-arm64.exe",
("linux", "x86_64") => "viiper-linux-amd64",
("linux", "aarch64") => "viiper-linux-arm64",
_ => {
eprintln!(
"Warning: VIIPER fetch_prelease enabled but unsupported target: os={target_os} arch={target_arch}"
);
return;
}
};
let tag_name = if fetch_prerelease {
"dev-snapshot".to_string()
} else if min_version.starts_with('v') {
min_version.to_string()
} else {
format!("v{min_version}")
};
let url = format!(
"https://github.com/Alia5/VIIPER/releases/download/{tag}/{asset}",
tag = tag_name,
asset = asset_name
);
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR"));
let cache_dir = out_dir.join("viiper");
let _ = fs::create_dir_all(&cache_dir);
let downloaded_path = cache_dir.join(asset_name);
let version_marker = cache_dir.join(format!("{}.marker", sanitize_filename(&tag_name)));
if downloaded_path.exists() && version_marker.exists() {
// Cached.
} else {
for entry in fs::read_dir(&cache_dir)
.unwrap_or_else(|_| fs::read_dir(&out_dir).unwrap())
.flatten()
{
let p = entry.path();
if p.extension().and_then(|e| e.to_str()) == Some("marker") {
let _ = fs::remove_file(p);
}
}
download_to_file(&url, &downloaded_path);
let _ = fs::write(&version_marker, &tag_name);
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut perms = fs::metadata(&downloaded_path)
.expect("downloaded file metadata")
.permissions();
perms.set_mode(0o755);
let _ = fs::set_permissions(&downloaded_path, perms);
}
}
let profile = env::var("PROFILE").unwrap_or_else(|_| "debug".into());
let target_dir = env::var("CARGO_TARGET_DIR")
.map(PathBuf::from)
.unwrap_or_else(|_| manifest_dir.join("target"));
let bin_dir = target_dir.join(&target_triple).join(&profile);
let _ = fs::create_dir_all(&bin_dir);
let dest_path = bin_dir.join(if is_windows { "viiper.exe" } else { "viiper" });
fs::copy(&downloaded_path, &dest_path)
.unwrap_or_else(|e| panic!("Failed to copy VIIPER to {}: {e}", dest_path.display()));
println!(
"cargo:warning=VIIPER fetched: tag={} asset={} -> {}",
tag_name,
asset_name,
dest_path.display()
);
}
fn download_to_file(url: &str, path: &PathBuf) {
let mut resp = ureq::get(url)
.header("User-Agent", "SISR-build-script")
.call()
.unwrap_or_else(|e| panic!("Failed to download {url}: {e}"));
let mut reader = resp.body_mut().as_reader();
let mut out = fs::File::create(path)
.unwrap_or_else(|e| panic!("Failed to create {}: {e}", path.display()));
std::io::copy(&mut reader, &mut out)
.unwrap_or_else(|e| panic!("Failed to write {}: {e}", path.display()));
}
fn sanitize_filename(s: &str) -> String {
s.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
c
} else {
'_'
}
})
.collect()
}