team-ljyzipelo

공개
111줄3.8 KB
1use std::{env, ffi::OsString, path::PathBuf, process::Command};
2
3fn msvc_compiler() -> Option<OsString> {
4 if let Some(compiler) = env::var_os("RC") {
5 return Some(compiler);
6 }
7 let mut candidates = Vec::new();
8 for root in ["ProgramFiles(x86)", "ProgramFiles"] {
9 let Some(root) = env::var_os(root) else {
10 continue;
11 };
12 let bin = PathBuf::from(root).join("Windows Kits/10/bin");
13 let Ok(entries) = std::fs::read_dir(bin) else {
14 continue;
15 };
16 for entry in entries.flatten() {
17 let version = entry
18 .file_name()
19 .to_string_lossy()
20 .split('.')
21 .map(str::parse::<u32>)
22 .collect::<Result<Vec<_>, _>>();
23 let compiler = entry.path().join("x64/rc.exe");
24 if let Ok(version) = version {
25 if compiler.is_file() {
26 candidates.push((version, compiler));
27 }
28 }
29 }
30 }
31 candidates
32 .into_iter()
33 .max_by(|a, b| a.0.cmp(&b.0))
34 .map(|(_, path)| path.into_os_string())
35 .or_else(|| {
36 env::var_os("PATH").and_then(|paths| {
37 env::split_paths(&paths)
38 .map(|path| path.join("rc.exe"))
39 .find(|path| path.is_file())
40 .map(PathBuf::into_os_string)
41 })
42 })
43}
44
45fn main() {
46 println!("cargo:rerun-if-changed=src/assets/icons/zipelo.ico");
47 for variable in ["RC", "ProgramFiles(x86)", "ProgramFiles", "PATH"] {
48 println!("cargo:rerun-if-env-changed={variable}");
49 }
50 println!("cargo:rerun-if-env-changed=CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER");
51 if env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("windows")
52 || env::var_os("CARGO_FEATURE_UI").is_none()
53 {
54 return;
55 }
56 let msvc = env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc");
57 let compiler = if msvc {
58 let Some(compiler) = msvc_compiler() else {
59 println!("cargo:warning=아이콘 없이 빌드: MSVC 리소스 컴파일러를 찾지 못함");
60 return;
61 };
62 compiler
63 } else {
64 env::var_os("RC").unwrap_or_else(|| "x86_64-w64-mingw32-windres".into())
65 };
66 let output = PathBuf::from(env::var_os("OUT_DIR").unwrap());
67 let icon = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap())
68 .join("src/assets/icons/zipelo.ico");
69 let resource = output.join("zipelo.rc");
70 std::fs::write(
71 &resource,
72 format!(
73 "1 ICON \"{}\"\n",
74 icon.display().to_string().replace('\\', "/")
75 ),
76 )
77 .unwrap();
78 let compiled = output.join(if msvc { "zipelo.res" } else { "zipelo.o" });
79 let mut command = Command::new(compiler);
80 if msvc {
81 command
82 .arg("/nologo")
83 .arg(format!("/fo{}", compiled.display()))
84 .arg(&resource);
85 } else {
86 if let Ok(preprocessor) = env::var("CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER") {
87 command.arg(format!("--preprocessor={preprocessor}")).args([
88 "--preprocessor-arg=-E",
89 "--preprocessor-arg=-xc",
90 "--preprocessor-arg=-DRC_INVOKED",
91 ]);
92 }
93 command
94 .arg("-i")
95 .arg(&resource)
96 .arg("-o")
97 .arg(&compiled)
98 .arg("-O")
99 .arg("coff");
100 }
101 let status = command.status();
102 if msvc && !status.as_ref().is_ok_and(|status| status.success()) {
103 println!("cargo:warning=아이콘 없이 빌드: MSVC 리소스 컴파일러 실행 실패");
104 return;
105 }
106 assert!(status.expect("Windows 리소스 컴파일러가 필요함").success());
107 println!(
108 "cargo:rustc-link-arg-bin=zipelo-ui-memory-app={}",
109 compiled.display()
110 );
111}