-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
206 lines (174 loc) · 6.31 KB
/
build.rs
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
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
fn main() {
rerun_if_changed(&["build.rs"]);
set_linker_args(&[
"/NOLOGO",
"/ALLOWBIND:NO",
"/INFERASANLIBS:NO",
"/CGTHREADS:1",
"/VERBOSE",
"/WX",
]);
if let Some(mt_path) = find_exe_in_path("mt.exe") {
if let Some(rc_path) = find_exe_in_path("rc.exe") {
let authors = env!("CARGO_PKG_AUTHORS").replace(':', ", ");
let crate_toml_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let version = env!("CARGO_PKG_VERSION");
let version_with_commas = version.replace('.', ",");
let pkg_name = env!("CARGO_PKG_NAME");
let pkg_description = env!("CARGO_PKG_DESCRIPTION");
let manifest = make_manifest(pkg_name, version, pkg_description);
let manifest_fullname = &format!("{pkg_name}.exe.manifest");
write_file(&crate_toml_path, manifest_fullname, &manifest);
run(
&mt_path,
&[
"/nologo",
"/validate_manifest",
//"/validate_file_hashes",
"/check_for_duplicates",
"/manifest",
manifest_fullname,
],
);
set_linker_args(&[
"/MANIFEST:EMBED", // requires for '/MANIFESTINPUT'
&format!("/MANIFESTINPUT:{pkg_name}.exe.manifest"), // set default 'requestedExecutionLevel' to tell UAC doesn't emulate paths like for the old application
"/MANIFESTUAC:level='requireAdministrator'", // set custom 'requestedExecutionLevel' in manifest
]);
let rc_data = make_rcdata(
&authors,
pkg_description,
pkg_name,
&version_with_commas,
version,
);
let rc_fullname = "res.rc";
write_file(&crate_toml_path, rc_fullname, &rc_data);
run(&rc_path, &["/nologo", "/v", rc_fullname]);
let mut res_path = crate_toml_path.clone();
res_path.push("res.res");
println!(r#"cargo:rustc-link-arg={}"#, res_path.to_str().unwrap());
} else {
sdk_not_set_warn()
}
} else {
sdk_not_set_warn()
}
}
fn sdk_not_set_warn() {
println!(r"cargo:warning='C:\Program Files (x86)\Windows Kits\10\bin\**ver**\x64' not installed or not specified in PATH.")
}
fn rerun_if_changed(list: &[&str]) {
for item in list {
println!("cargo:rerun-if-changed={item}");
}
}
fn set_linker_args(args: &[&str]) {
for arg in args {
println!("cargo:rustc-link-arg={arg}");
}
}
fn write_file<P: AsRef<Path>>(path_to_dir: P, file_fullname: &str, data: &str) {
let mut file_path = PathBuf::from(path_to_dir.as_ref());
file_path.push(file_fullname);
let mut file = File::options()
.write(true)
.truncate(true)
.create(true)
.open(file_path)
.unwrap();
file.write_all(data.as_bytes()).unwrap();
}
fn find_exe_in_path<P: AsRef<Path>>(exe_name: P) -> Option<PathBuf>
{
env::var_os("PATH").and_then(|paths| {
env::split_paths(&paths).filter_map(|dir| {
let full_path = dir.join(&exe_name);
if full_path.is_file() {
Some(full_path)
} else {
None
}
}).next()
})
}
fn run<P: AsRef<Path>>(exe_path: P, args: &[&str]) {
let rc_proc = Command::new(exe_path.as_ref()).args(args).spawn().unwrap();
let result = rc_proc.wait_with_output().unwrap();
if !result.status.success() {
panic!("{}", String::from_utf8_lossy(result.stdout.as_slice()))
}
}
fn make_manifest(pkg_name: &str, version: &str, pkg_description: &str) -> String {
format!(
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
<assemblyIdentity
name="{pkg_name}.exe"
version="{version}.0"
type="win32"
processorArchitecture="amd64"
/>
<description>{pkg_description}</description>
</assembly>"#
)
// <!-- <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
// <security>
// <requestedPrivileges> -->
// <!-- 'requestedExecutionLevel' conflict with '/MANIFESTINPUT' and requires same '/MANIFESTUAC' or remove 'requestedExecutionLevel' block -->
// <!-- <requestedExecutionLevel
// level="requireAdministrator"
// uiAccess="false"
// />
// </requestedPrivileges>
// </security>
// </trustInfo> -->
}
fn make_rcdata(
authors: &str,
pkg_description: &str,
pkg_name: &str,
version_with_commas: &str,
version: &str,
) -> String {
format!(
r#"#define VER_COMPANYNAME_STR "{authors}\0"
#define VER_FILEDESCRIPTION_STR "{pkg_description}\0"
#define VER_INTERNALNAME_STR "{pkg_name}\0"
#define VER_ORIGINALFILENAME_STR "{pkg_name}.exe\0"
#define VER_PRODUCTNAME_STR "Server Switcher\0"
#define VERSION {version_with_commas}
#define VERSION_STR "{version}\0"
1 ICON "CodeCreator.ico"
1 VERSIONINFO
FILEVERSION VERSION
PRODUCTVERSION VERSION
BEGIN
BLOCK "StringFileInfo"
BEGIN
// https://learn.microsoft.com/en-us/windows/win32/menurc/versioninfo-resource
BLOCK "041904B0" // Ru
BEGIN
VALUE "CompanyName", VER_COMPANYNAME_STR
VALUE "FileDescription", VER_FILEDESCRIPTION_STR
VALUE "FileVersion", VERSION_STR
VALUE "InternalName", VER_INTERNALNAME_STR
VALUE "OriginalFilename", VER_ORIGINALFILENAME_STR
VALUE "ProductName", VER_PRODUCTNAME_STR
VALUE "ProductVersion", VERSION_STR
END
END
BLOCK "VarFileInfo"
BEGIN
// https://learn.microsoft.com/en-us/windows/win32/menurc/versioninfo-resource
// VALUE "Translation", 0x409, 1200 // En
VALUE "Translation", 0x419, 1200 // Ru
END
END"#
)
}