| 1 |
use std::env; |
| 2 |
use std::fs::{self, File}; |
| 3 |
use std::io::{self, stdout, BufWriter, Cursor, Write}; |
| 4 |
use std::path::{Path, PathBuf}; |
| 5 |
|
| 6 |
use crate::manifest::ManifestBuilder; |
| 7 |
|
| 8 |
use self::coff::{resource_data_entry, resource_directory_id_entry, resource_directory_table, CoffWriter, MachineType}; |
| 9 |
use self::error::Error; |
| 10 |
|
| 11 |
mod coff; |
| 12 |
pub mod error; |
| 13 |
|
| 14 |
#[cfg(test)] |
| 15 |
mod test; |
| 16 |
|
| 17 |
/// Embeds the manifest described by `manifest` by converting it to XML, |
| 18 |
/// then saving it to a file and passing the correct options to the linker |
| 19 |
/// on MSVC targets, or by building a static library and instructing Cargo |
| 20 |
/// to link the executable against it on GNU targets. |
| 21 |
pub fn embed_manifest(manifest: ManifestBuilder) -> Result<(), Error> { |
| 22 |
let out_dir = get_out_dir()?; |
| 23 |
let target = get_target()?; |
| 24 |
if matches!(target.os, TargetOs::WindowsMsvc) { |
| 25 |
let manifest_file = out_dir.join("manifest.xml"); |
| 26 |
write!(BufWriter::new(File::create(&manifest_file)?), "{}", manifest)?; |
| 27 |
link_manifest_msvc(&manifest_file, &mut stdout().lock()) |
| 28 |
} else { |
| 29 |
let manifest_data = manifest.to_string(); |
| 30 |
link_manifest_gnu(manifest_data.as_bytes(), &out_dir, target.arch, &mut stdout().lock()) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/// Directly embeds the manifest in the provided `file` by passing the correct |
| 35 |
/// options to the linker on MSVC targets, or by building a static library |
| 36 |
/// and instructing Cargo to link the executable against it on GNU targets. |
| 37 |
pub fn embed_manifest_file<P: AsRef<Path>>(file: P) -> Result<(), io::Error> { |
| 38 |
let out_dir = get_out_dir()?; |
| 39 |
let target = get_target()?; |
| 40 |
if matches!(target.os, TargetOs::WindowsMsvc) { |
| 41 |
Ok(link_manifest_msvc(file.as_ref(), &mut stdout().lock())?) |
| 42 |
} else { |
| 43 |
let manifest = fs::read(file.as_ref())?; |
| 44 |
Ok(link_manifest_gnu(&manifest, &out_dir, target.arch, &mut stdout().lock())?) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
fn get_out_dir() -> Result<PathBuf, io::Error> { |
| 49 |
match env::var_os("OUT_DIR") { |
| 50 |
Some(dir) => Ok(PathBuf::from(dir)), |
| 51 |
None => env::current_dir(), |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
enum TargetOs { |
| 56 |
WindowsGnu, |
| 57 |
WindowsMsvc, |
| 58 |
} |
| 59 |
|
| 60 |
struct Target { |
| 61 |
arch: MachineType, |
| 62 |
os: TargetOs, |
| 63 |
} |
| 64 |
|
| 65 |
fn get_target() -> Result<Target, Error> { |
| 66 |
match env::var("TARGET") { |
| 67 |
Ok(target) => parse_target(&target), |
| 68 |
_ => Err(Error::unknown_target()), |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
fn parse_target(target: &str) -> Result<Target, Error> { |
| 73 |
let mut iter = target.splitn(3, '-'); |
| 74 |
let arch = match iter.next() { |
| 75 |
Some("i686") => MachineType::I386, |
| 76 |
Some("aarch64") => MachineType::Aarch64, |
| 77 |
Some("x86_64") => MachineType::X86_64, |
| 78 |
_ => return Err(Error::unknown_target()), |
| 79 |
}; |
| 80 |
if iter.next() != Some("pc") { |
| 81 |
return Err(Error::unknown_target()); |
| 82 |
} |
| 83 |
let os = match iter.next() { |
| 84 |
Some("windows-gnu") => TargetOs::WindowsGnu, |
| 85 |
Some("windows-gnullvm") => TargetOs::WindowsGnu, |
| 86 |
Some("windows-msvc") => TargetOs::WindowsMsvc, |
| 87 |
_ => return Err(Error::unknown_target()), |
| 88 |
}; |
| 89 |
Ok(Target { arch, os }) |
| 90 |
} |
| 91 |
|
| 92 |
fn link_manifest_msvc<W: Write>(manifest_path: &Path, out: &mut W) -> Result<(), Error> { |
| 93 |
writeln!(out, "cargo:rustc-link-arg-bins=/MANIFEST:EMBED")?; |
| 94 |
writeln!( |
| 95 |
out, |
| 96 |
"cargo:rustc-link-arg-bins=/MANIFESTINPUT:{}", |
| 97 |
manifest_path.canonicalize()?.display() |
| 98 |
)?; |
| 99 |
writeln!(out, "cargo:rustc-link-arg-bins=/MANIFESTUAC:NO")?; |
| 100 |
Ok(()) |
| 101 |
} |
| 102 |
|
| 103 |
fn link_manifest_gnu<W: Write>(manifest: &[u8], out_dir: &Path, arch: MachineType, out: &mut W) -> Result<(), Error> { |
| 104 |
// Generate a COFF object file containing the manifest in a .rsrc section. |
| 105 |
let object_data = create_object_file(manifest, arch)?; |
| 106 |
let path = out_dir.join("embed-manifest.o"); |
| 107 |
fs::create_dir_all(out_dir)?; |
| 108 |
fs::write(&path, object_data)?; |
| 109 |
|
| 110 |
// Link the manifest with the executable. |
| 111 |
writeln!(out, "cargo:rustc-link-arg-bins={}", path.display())?; |
| 112 |
Ok(()) |
| 113 |
} |
| 114 |
|
| 115 |
fn create_object_file(manifest: &[u8], arch: MachineType) -> Result<Vec<u8>, io::Error> { |
| 116 |
// Start object file with .rsrc section. |
| 117 |
let mut obj = CoffWriter::new(Cursor::new(Vec::with_capacity(4096)), arch)?; |
| 118 |
|
| 119 |
// Create resource directories for type ID 24, name ID 1, language ID 1033. |
| 120 |
obj.add_data(&resource_directory_table(1))?; |
| 121 |
obj.add_data(&resource_directory_id_entry(24, 24, true))?; |
| 122 |
obj.add_data(&resource_directory_table(1))?; |
| 123 |
obj.add_data(&resource_directory_id_entry(1, 48, true))?; |
| 124 |
obj.add_data(&resource_directory_table(1))?; |
| 125 |
obj.add_data(&resource_directory_id_entry(1033, 72, false))?; |
| 126 |
|
| 127 |
// Add resource data entry with relocated address. |
| 128 |
let address = obj.add_data(&resource_data_entry(88, manifest.len() as u32))?; |
| 129 |
|
| 130 |
// Add the manifest data. |
| 131 |
obj.add_data(manifest)?; |
| 132 |
obj.align_to(8)?; |
| 133 |
|
| 134 |
// Write manifest data relocation at the end of the section. |
| 135 |
obj.add_relocation(address)?; |
| 136 |
|
| 137 |
// Finish writing file and return the populated object data. |
| 138 |
Ok(obj.finish()?.into_inner()) |
| 139 |
} |