embed_resource/lib.rs
1//! A [`Cargo` build script](http://doc.crates.io/build-script.html) library to handle compilation and inclusion of Windows
2//! resources in the most resilient fashion imaginable
3//!
4//! # Background
5//!
6//! Including Windows resources seems very easy at first, despite the build scripts' abhorrent documentation:
7//! [compile with `windres`, then make linkable with
8//! `ar`](https://github.com/nabijaczleweli/cargo-update/commit/ef4346c#diff-a7b0a2dee0126cddf994326e705a91ea).
9//!
10//! I was very happy with that solution until it was brought to my attention, that [MSVC uses something
11//! different](https://github.com/nabijaczleweli/cargo-update/commit/f57e9c3#diff-a7b0a2dee0126cddf994326e705a91ea),
12//! and now either `windres`-`ar` combo or `RC.EXE` would be used, which was OK.
13//!
14//! Later it transpired, that [MSVC is even more incompatible with everything
15//! else](https://github.com/nabijaczleweli/cargo-update/commit/39fa758#diff-a7b0a2dee0126cddf994326e705a91ea)
16//! by way of not having `RC.EXE` in `$PATH` (because it would only be reasonable to do so),
17//! so another MSVC artisan made the script [find the most likely places for `RC.EXE` to
18//! be](https://github.com/nabijaczleweli/cargo-update/pull/22), and the script grew yet again,
19//! now standing at 100 lines and 3.2 kB.
20//!
21//! After [copying the build script in its
22//! entirety](https://github.com/thecoshman/http/commit/98205a4#diff-a7b0a2dee0126cddf994326e705a91ea)
23//! and realising how error-prone that was, then being [nudged by
24//! Shepmaster](https://chat.stackoverflow.com/transcript/message/35378953#35378953)
25//! to extract it to a crate, here we are.
26//!
27//! # Usage
28//!
29//! For the purposes of the demonstration we will assume that the resource file's name
30//! is "checksums.rc", but it can be any name relative to the crate root.
31//!
32//! In `Cargo.toml`:
33//!
34//! ```toml
35//! # The general section with crate name, license, etc.
36//! build = "build.rs"
37//!
38//! [build-dependencies]
39//! embed-resource = "3.0"
40//! ```
41//!
42//! In `build.rs`:
43//!
44//! ```rust,no_run
45//! extern crate embed_resource;
46//!
47//! fn main() {
48//! embed_resource::compile("checksums.rc", embed_resource::NONE).manifest_optional().unwrap();
49//! // or
50//! embed_resource::compile("checksums.rc", &["VERSION=000901"]).manifest_required().unwrap();
51//! }
52//! ```
53//!
54//! Use `.manifest_optional().unwrap()` if the manifest is cosmetic (like an icon).<br />
55//! Use `.manifest_required().unwrap()` if the manifest is required (security, entry point, &c.).
56//!
57//! ## Errata
58//!
59//! If no `cargo:rerun-if-changed` annotations are generated, Cargo scans the entire build root by default.
60//! Because the first step in building a manifest is an unspecified C preprocessor step with-out the ability to generate the
61//! equivalent of `cc -MD`, we do *not* output said annotation.
62//!
63//! If scanning is prohibitively expensive, or you have something else that generates the annotations, you may want to spec the
64//! full non-system dependency list for your manifest manually, so:
65//! ```rust,no_run
66//! println!("cargo:rerun-if-changed=app-name-manifest.rc");
67//! embed_resource::compile("app-name-manifest.rc", embed_resource::NONE);
68//! ```
69//! for the above example (cf, [#41](https://github.com/nabijaczleweli/rust-embed-resource/issues/41)).
70//!
71//! # Cross-compilation
72//!
73//! It is possible to embed resources in Windows executables built on non-Windows hosts. There are two ways to do this:
74//!
75//! When targetting `*-pc-windows-gnu`, `*-w64-mingw32-windres` is attempted by default, for `*-pc-windows-msvc` it's `llvm-rc`,
76//! this can be overriden by setting `RC_$TARGET`, `RC_${TARGET//-/_}`, or `RC` environment variables.
77//!
78//! When compiling with LLVM-RC, an external C compiler is used to preprocess the resource,
79//! preloaded with configuration from
80//! [`cc`](https://github.com/alexcrichton/cc-rs#external-configuration-via-environment-variables).
81//!
82//! ## Migration
83//! ### 2.x
84//!
85//! Add `embed_resource::NONE` as the last argument to `embed_resource::compile()` and `embed_resource::compile_for()`.
86//!
87//! ### 3.x
88//!
89//! Add `.manifest_optional().unwrap()` or `.manifest_required().unwrap()` to all [`compile()`] and `compile_for*()` calls.
90//! `CompilationResult` is `#[must_use]` so should be highlighted automatically.
91//!
92//! Embed-resource <3.x always behaves like `.manifest_optional().unwrap()`.
93//!
94//! # Credit
95//!
96//! In chronological order:
97//!
98//! [@liigo](https://github.com/liigo) -- persistency in pestering me and investigating problems where I have failed
99//!
100//! [@mzji](https://github.com/mzji) -- MSVC lab rat
101//!
102//! [@TheCatPlusPlus](https://github.com/TheCatPlusPlus) -- knowledge and providing first iteration of manifest-embedding code
103//!
104//! [@azyobuzin](https://github.com/azyobuzin) -- providing code for finding places where RC.EXE could hide
105//!
106//! [@retep998](https://github.com/retep998) -- fixing MSVC support
107//!
108//! [@SonnyX](https://github.com/SonnyX) -- Windows cross-compilation support and testing
109//!
110//! [@MSxDOS](https://github.com/MSxDOS) -- finding and supplying RC.EXE its esoteric header include paths
111//!
112//! [@roblabla](https://github.com/roblabla) -- cross-compilation to Windows MSVC via LLVM-RC
113//!
114//! # Special thanks
115//!
116//! To all who support further development on [Patreon](https://patreon.com/nabijaczleweli), in particular:
117//!
118//! * ThePhD
119//! * Embark Studios
120//! * Lars Strojny
121//! * EvModder
122
123
124#[cfg(any(not(target_os = "windows"), all(target_os = "windows", target_env = "msvc")))]
125extern crate cc;
126#[cfg(not(target_os = "windows"))]
127extern crate memchr;
128#[cfg(all(target_os = "windows", target_env = "msvc"))]
129extern crate vswhom;
130#[cfg(all(target_os = "windows", target_env = "msvc"))]
131extern crate winreg;
132extern crate rustc_version;
133extern crate toml;
134
135#[cfg(not(target_os = "windows"))]
136mod non_windows;
137#[cfg(all(target_os = "windows", target_env = "msvc"))]
138mod windows_msvc;
139#[cfg(all(target_os = "windows", not(target_env = "msvc")))]
140mod windows_not_msvc;
141
142#[cfg(not(target_os = "windows"))]
143use self::non_windows::*;
144#[cfg(all(target_os = "windows", target_env = "msvc"))]
145use self::windows_msvc::*;
146#[cfg(all(target_os = "windows", not(target_env = "msvc")))]
147use self::windows_not_msvc::*;
148
149use std::{env, fs};
150use std::ffi::OsStr;
151use std::borrow::Cow;
152use std::process::Command;
153use toml::Value as TomlValue;
154use std::fmt::{self, Display};
155use toml::map::Map as TomlMap;
156use std::path::{Path, PathBuf};
157
158
159/// Empty slice, properly-typed for [`compile()`] and `compile_for*()`'s macro list.
160///
161/// Rust helpfully forbids default type parameters on functions, so just passing `[]` doesn't work :)
162pub const NONE: &[&OsStr] = &[];
163
164
165/// Result of [`compile()`] and `compile_for*()`
166///
167/// Turn this into a `Result` with `manifest_optional()` if the manifest is nice, but isn't required, like when embedding an
168/// icon or some other cosmetic.
169///
170/// Turn this into a `Result` with `manifest_required()` if the manifest is mandatory, like when configuring entry points or
171/// security.
172#[must_use]
173#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
174pub enum CompilationResult {
175 /// not building for windows
176 NotWindows,
177 /// built, linked
178 Ok,
179 /// building for windows, but the environment can't compile a resource (most likely due to a missing compiler)
180 NotAttempted(Cow<'static, str>),
181 /// environment can compile a resource, but has failed to do so
182 Failed(Cow<'static, str>),
183}
184impl CompilationResult {
185 /// `Ok(())` if `NotWindows`, `Ok`, or `NotAttempted`; `Err(self)` if `Failed`
186 pub fn manifest_optional(self) -> Result<(), CompilationResult> {
187 match self {
188 CompilationResult::NotWindows |
189 CompilationResult::Ok |
190 CompilationResult::NotAttempted(..) => Ok(()),
191 err @ CompilationResult::Failed(..) => Err(err),
192 }
193 }
194
195 /// `Ok(())` if `NotWindows`, `Ok`; `Err(self)` if `NotAttempted` or `Failed`
196 pub fn manifest_required(self) -> Result<(), CompilationResult> {
197 match self {
198 CompilationResult::NotWindows |
199 CompilationResult::Ok => Ok(()),
200 err @ CompilationResult::NotAttempted(..) |
201 err @ CompilationResult::Failed(..) => Err(err),
202 }
203 }
204}
205impl Display for CompilationResult {
206 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
207 f.write_str("embed-resource: ")?;
208 match self {
209 CompilationResult::NotWindows => f.write_str("not building for windows"),
210 CompilationResult::Ok => f.write_str("OK"),
211 CompilationResult::NotAttempted(why) => {
212 f.write_str("compilation not attempted: ")?;
213 if !why.contains(' ') {
214 f.write_str("missing compiler: ")?;
215 }
216 f.write_str(why)
217 }
218 CompilationResult::Failed(err) => f.write_str(err),
219 }
220 }
221}
222impl std::error::Error for CompilationResult {}
223
224macro_rules! try_compile_impl {
225 ($expr:expr) => {
226 match $expr {
227 Result::Ok(val) => val,
228 Result::Err(err) => return err,
229 }
230 };
231}
232
233
234/// Compile the Windows resource file and update the cargo search path if building for Windows.
235///
236/// On non-Windows non-Windows-cross-compile-target this does nothing, on non-MSVC Windows and Windows cross-compile targets,
237/// this chains `windres` with `ar`,
238/// but on MSVC Windows, this will try its hardest to find `RC.EXE` in Windows Kits and/or SDK directories,
239/// falling back to [Jon Blow's VS discovery script](https://pastebin.com/3YvWQa5c),
240/// and on Windows 10 `%INCLUDE%` will be updated to help `RC.EXE` find `windows.h` and friends.
241///
242/// `$OUT_DIR` is added to the include search path.
243///
244/// Note that this does *nothing* if building with rustc before 1.50.0 and there's a library in the crate,
245/// since the resource is linked to the library, if any, instead of the binaries.
246///
247/// Since rustc 1.50.0, the resource is linked only to the binaries
248/// (unless there are none, in which case it's also linked to the library).
249///
250/// `macros` are a list of macros to define, in standard `NAME`/`NAME=VALUE` format.
251///
252/// # Examples
253///
254/// In your build script, assuming the crate's name is "checksums":
255///
256/// ```rust,no_run
257/// extern crate embed_resource;
258///
259/// fn main() {
260/// // Compile and link checksums.rc
261/// embed_resource::compile("checksums.rc", embed_resource::NONE);
262/// }
263/// ```
264pub fn compile<T: AsRef<Path>, Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>>(resource_file: T, macros: Mi) -> CompilationResult {
265 let (prefix, out_dir, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), macros));
266 let hasbins = fs::read_to_string("Cargo.toml")
267 .unwrap_or_else(|err| {
268 eprintln!("Couldn't read Cargo.toml: {}; assuming src/main.rs or S_ISDIR(src/bin/)", err);
269 String::new()
270 })
271 .parse::<TomlValue>()
272 .unwrap_or_else(|err| {
273 eprintln!("Couldn't parse Cargo.toml: {}; assuming src/main.rs or S_ISDIR(src/bin/)", err);
274 TomlValue::Table(TomlMap::new())
275 })
276 .as_table()
277 .map(|t| t.contains_key("bin"))
278 .unwrap_or(false) || (Path::new("src/main.rs").exists() || Path::new("src/bin").is_dir());
279 eprintln!("Final verdict: crate has binaries: {}", hasbins);
280
281 if hasbins && rustc_version::version().expect("couldn't get rustc version") >= rustc_version::Version::new(1, 50, 0) {
282 println!("cargo:rustc-link-arg-bins={}", out_file);
283 } else {
284 // Cargo pre-0.51.0 (rustc pre-1.50.0) compat
285 // Only links to the calling crate's library
286 println!("cargo:rustc-link-search=native={}", out_dir);
287 println!("cargo:rustc-link-lib=dylib={}", prefix);
288 }
289 CompilationResult::Ok
290}
291
292/// Likewise, but only for select binaries.
293///
294/// Only available since rustc 1.55.0, does nothing before.
295///
296/// # Examples
297///
298/// ```rust,no_run
299/// extern crate embed_resource;
300///
301/// fn main() {
302/// embed_resource::compile_for("assets/poke-a-mango.rc", &["poke-a-mango", "poke-a-mango-installer"],
303/// &["VERSION=\"0.5.0\""]);
304/// embed_resource::compile_for("assets/uninstaller.rc", &["unins001"], embed_resource::NONE);
305/// }
306/// ```
307pub fn compile_for<T: AsRef<Path>, J: Display, I: IntoIterator<Item = J>, Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>>(resource_file: T, for_bins: I,
308 macros: Mi)
309 -> CompilationResult {
310 let (_, _, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), macros));
311 for bin in for_bins {
312 println!("cargo:rustc-link-arg-bin={}={}", bin, out_file);
313 }
314 CompilationResult::Ok
315}
316
317/// Likewise, but only link the resource to test binaries (select types only. unclear which (and likely to change). you may
318/// prefer [`compile_for_everything()`]).
319///
320/// Only available since rustc 1.60.0, does nothing before.
321pub fn compile_for_tests<T: AsRef<Path>, Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>>(resource_file: T, macros: Mi) -> CompilationResult {
322 let (_, _, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), macros));
323 println!("cargo:rustc-link-arg-tests={}", out_file);
324 CompilationResult::Ok
325}
326
327/// Likewise, but only link the resource to benchmarks.
328///
329/// Only available since rustc 1.60.0, does nothing before.
330pub fn compile_for_benchmarks<T: AsRef<Path>, Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>>(resource_file: T, macros: Mi) -> CompilationResult {
331 let (_, _, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), macros));
332 println!("cargo:rustc-link-arg-benches={}", out_file);
333 CompilationResult::Ok
334}
335
336/// Likewise, but only link the resource to examples.
337///
338/// Only available since rustc 1.60.0, does nothing before.
339pub fn compile_for_examples<T: AsRef<Path>, Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>>(resource_file: T, macros: Mi) -> CompilationResult {
340 let (_, _, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), macros));
341 println!("cargo:rustc-link-arg-examples={}", out_file);
342 CompilationResult::Ok
343}
344
345/// Likewise, but link the resource into *every* artifact: binaries, cdylibs, examples, tests (`[[test]]`/`#[test]`/doctest),
346/// benchmarks, &c.
347///
348/// Only available since rustc 1.50.0, does nothing before.
349pub fn compile_for_everything<T: AsRef<Path>, Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>>(resource_file: T, macros: Mi) -> CompilationResult {
350 let (_, _, out_file) = try_compile_impl!(compile_impl(resource_file.as_ref(), macros));
351 println!("cargo:rustc-link-arg={}", out_file);
352 CompilationResult::Ok
353}
354
355fn compile_impl<Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>>(resource_file: &Path, macros: Mi) -> Result<(&str, String, String), CompilationResult> {
356 let mut comp = ResourceCompiler::new();
357 if let Some(missing) = comp.is_supported() {
358 if missing.is_empty() {
359 Err(CompilationResult::NotWindows)
360 } else {
361 Err(CompilationResult::NotAttempted(missing))
362 }
363 } else {
364 let prefix = &resource_file.file_stem().expect("resource_file has no stem").to_str().expect("resource_file's stem not UTF-8");
365 let out_dir = env::var("OUT_DIR").expect("No OUT_DIR env var");
366
367 let out_file = comp.compile_resource(&out_dir, &prefix, resource_file.to_str().expect("resource_file not UTF-8"), macros)
368 .map_err(CompilationResult::Failed)?;
369 Ok((prefix, out_dir, out_file))
370 }
371}
372
373fn apply_macros<'t, Ms: AsRef<OsStr>, Mi: IntoIterator<Item = Ms>>(to: &'t mut Command, pref: &str, macros: Mi) -> &'t mut Command {
374 for m in macros {
375 to.arg(pref).arg(m);
376 }
377 to
378}
379
380
381/// Find MSVC build tools other than the compiler and linker
382///
383/// On Windows + MSVC this can be used try to find tools such as `MIDL.EXE` in Windows Kits and/or SDK directories.
384///
385/// The compilers and linkers can be better found with the `cc` or `vswhom` crates.
386/// This always returns `None` on non-MSVC targets.
387///
388/// # Examples
389///
390/// In your build script, find `midl.exe` and use it to compile an IDL file:
391///
392/// ```rust,no_run
393/// # #[cfg(all(target_os = "windows", target_env = "msvc"))]
394/// # {
395/// extern crate embed_resource;
396/// extern crate vswhom;
397/// # use std::env;
398/// # use std::process::Command;
399///
400/// let midl = embed_resource::find_windows_sdk_tool("midl.exe").unwrap();
401///
402/// // midl.exe uses cl.exe as a preprocessor, so it needs to be in PATH
403/// let vs_locations = vswhom::VsFindResult::search().unwrap();
404/// let output = Command::new(midl)
405/// .env("PATH", vs_locations.vs_exe_path.unwrap())
406/// .args(&["/out", &env::var("OUT_DIR").unwrap()])
407/// .arg("haka.pfx.idl").output().unwrap();
408///
409/// assert!(output.status.success());
410/// # }
411/// ```
412pub fn find_windows_sdk_tool<T: AsRef<str>>(tool: T) -> Option<PathBuf> {
413 find_windows_sdk_tool_impl(tool.as_ref())
414}