| 1 |
//! The `embed-manifest` crate provides a straightforward way to embed |
| 2 |
//! a Windows manifest in an executable, whatever the build environment |
| 3 |
//! and even when cross-compiling, without dependencies on external |
| 4 |
//! tools from LLVM or MinGW. |
| 5 |
//! |
| 6 |
//! This should be called from a [build script][1], as shown below. |
| 7 |
//! |
| 8 |
//! [1]: https://doc.rust-lang.org/cargo/reference/build-scripts.html |
| 9 |
//! |
| 10 |
//! On MSVC targets, the manifest file is embedded in the executable by |
| 11 |
//! instructing Cargo to pass `/MANIFEST` options to `LINK.EXE`. This |
| 12 |
//! requires Cargo from Rust 1.56. |
| 13 |
//! |
| 14 |
//! On GNU targets, the manifest file is added as a resource in a COFF |
| 15 |
//! object file, and Cargo is instructed to link this file into the |
| 16 |
//! executable, also using functionality from Rust 1.56. |
| 17 |
//! |
| 18 |
//! # Usage |
| 19 |
//! |
| 20 |
//! This crate should be added to the `[build-dependencies]` section in |
| 21 |
//! your executable’s `Cargo.toml`: |
| 22 |
//! |
| 23 |
//! ```toml |
| 24 |
//! [build-dependencies] |
| 25 |
//! embed-manifest = "1.3.1" |
| 26 |
//! ``` |
| 27 |
//! |
| 28 |
//! In the same directory, create a `build.rs` file to call this crate’s |
| 29 |
//! code when building for Windows, and to only run once: |
| 30 |
//! |
| 31 |
//! ``` |
| 32 |
//! use embed_manifest::{embed_manifest, new_manifest}; |
| 33 |
//! |
| 34 |
//! fn main() { |
| 35 |
//! # let tempdir = tempfile::tempdir().unwrap(); |
| 36 |
//! # std::env::set_var("OUT_DIR", tempdir.path()); |
| 37 |
//! # std::env::set_var("TARGET", "x86_64-pc-windows-gnu"); |
| 38 |
//! # std::env::set_var("CARGO_CFG_WINDOWS", ""); |
| 39 |
//! if std::env::var_os("CARGO_CFG_WINDOWS").is_some() { |
| 40 |
//! embed_manifest(new_manifest("Contoso.Sample")).expect("unable to embed manifest file"); |
| 41 |
//! } |
| 42 |
//! println!("cargo:rerun-if-changed=build.rs"); |
| 43 |
//! } |
| 44 |
//! ``` |
| 45 |
//! |
| 46 |
//! To customise the application manifest, use the methods on it to change things like |
| 47 |
//! enabling the segment heap: |
| 48 |
//! |
| 49 |
//! ``` |
| 50 |
//! use embed_manifest::{embed_manifest, new_manifest, manifest::HeapType}; |
| 51 |
//! |
| 52 |
//! fn main() { |
| 53 |
//! # let tempdir = tempfile::tempdir().unwrap(); |
| 54 |
//! # std::env::set_var("OUT_DIR", tempdir.path()); |
| 55 |
//! # std::env::set_var("TARGET", "x86_64-pc-windows-gnu"); |
| 56 |
//! # std::env::set_var("CARGO_CFG_WINDOWS", ""); |
| 57 |
//! if std::env::var_os("CARGO_CFG_WINDOWS").is_some() { |
| 58 |
//! embed_manifest(new_manifest("Contoso.Sample").heap_type(HeapType::SegmentHeap)) |
| 59 |
//! .expect("unable to embed manifest file"); |
| 60 |
//! } |
| 61 |
//! println!("cargo:rerun-if-changed=build.rs"); |
| 62 |
//! } |
| 63 |
//! ``` |
| 64 |
//! |
| 65 |
//! or making it always use legacy single-byte API encoding and only declaring compatibility |
| 66 |
//! up to Windows 8.1, without checking whether this is a Windows build: |
| 67 |
//! |
| 68 |
//! ``` |
| 69 |
//! use embed_manifest::{embed_manifest, new_manifest}; |
| 70 |
//! use embed_manifest::manifest::{ActiveCodePage::Legacy, SupportedOS::*}; |
| 71 |
//! |
| 72 |
//! fn main() { |
| 73 |
//! # let tempdir = tempfile::tempdir().unwrap(); |
| 74 |
//! # std::env::set_var("OUT_DIR", tempdir.path()); |
| 75 |
//! # std::env::set_var("TARGET", "x86_64-pc-windows-gnu"); |
| 76 |
//! let manifest = new_manifest("Contoso.Sample") |
| 77 |
//! .active_code_page(Legacy) |
| 78 |
//! .supported_os(Windows7..=Windows81); |
| 79 |
//! embed_manifest(manifest).expect("unable to embed manifest file"); |
| 80 |
//! println!("cargo:rerun-if-changed=build.rs"); |
| 81 |
//! } |
| 82 |
//! ``` |
| 83 |
|
| 84 |
#![allow(clippy::needless_doctest_main)] |
| 85 |
|
| 86 |
pub use embed::error::Error; |
| 87 |
pub use embed::{embed_manifest, embed_manifest_file}; |
| 88 |
|
| 89 |
use crate::manifest::ManifestBuilder; |
| 90 |
|
| 91 |
mod embed; |
| 92 |
pub mod manifest; |
| 93 |
|
| 94 |
/// Creates a new [`ManifestBuilder`] with sensible defaults, allowing customisation |
| 95 |
/// before the Windows application manifest XML is generated. |
| 96 |
/// |
| 97 |
/// The initial values used by the manifest are: |
| 98 |
/// - Version number from the `CARGO_PKG_VERSION_MAJOR`, `CARGO_PKG_VERSION_MINOR` and |
| 99 |
/// `CARGO_PKG_VERSION_PATCH` environment variables. This can then be changed with |
| 100 |
/// [`version()`][ManifestBuilder::version]. |
| 101 |
/// - A dependency on version 6 of the Common Controls so that message boxes and dialogs |
| 102 |
/// will use the latest design, and have the best available support for high DPI displays. |
| 103 |
/// This can be removed with |
| 104 |
/// [`remove_dependency`][ManifestBuilder::remove_dependency]. |
| 105 |
/// - [Compatible with Windows from 7 to 11][ManifestBuilder::supported_os], |
| 106 |
/// matching the Rust compiler [tier 1 targets][tier1]. |
| 107 |
/// - A “[maximum version tested][ManifestBuilder::max_version_tested]” of Windows 10 |
| 108 |
/// version 1903. |
| 109 |
/// - An [active code page][ManifestBuilder::active_code_page] of UTF-8, so that |
| 110 |
/// single-byte Windows APIs will generally interpret Rust strings correctly, starting |
| 111 |
/// from Windows 10 version 1903. |
| 112 |
/// - [Version 2 of per-monitor high DPI awareness][manifest::DpiAwareness::PerMonitorV2Only], |
| 113 |
/// so that user interface elements can be scaled correctly by the application and |
| 114 |
/// Common Controls from Windows 10 version 1703. |
| 115 |
/// - [Long path awareness][ManifestBuilder::long_path_aware] from Windows 10 version |
| 116 |
/// 1607 [when separately enabled][longpaths]. |
| 117 |
/// - [Printer driver isolation][ManifestBuilder::printer_driver_isolation] enabled |
| 118 |
/// to improve reliability and security. |
| 119 |
/// - An [execution level][ManifestBuilder::requested_execution_level] of “as invoker” |
| 120 |
/// so that the UAC elevation dialog will never be displayed, regardless of the name |
| 121 |
/// of the program, and [UAC Virtualisation][uac] is disabled in 32-bit programs. |
| 122 |
/// |
| 123 |
/// [tier1]: https://doc.rust-lang.org/nightly/rustc/platform-support.html |
| 124 |
/// [longpaths]: https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd#enable-long-paths-in-windows-10-version-1607-and-later |
| 125 |
/// [uac]: https://docs.microsoft.com/en-us/windows/security/identity-protection/user-account-control/how-user-account-control-works#virtualization |
| 126 |
pub fn new_manifest(name: &str) -> ManifestBuilder { |
| 127 |
ManifestBuilder::new(name) |
| 128 |
} |
| 129 |
|
| 130 |
/// Creates a new [`ManifestBuilder`] without any settings, allowing creation of |
| 131 |
/// a manifest with only desired content. |
| 132 |
pub fn empty_manifest() -> ManifestBuilder { |
| 133 |
ManifestBuilder::empty() |
| 134 |
} |