|
| 1 | +//! A build script dependency to create a Windows resource file for the compiler |
| 2 | +//! |
| 3 | +//! Uses values from the `CFG_VERSION` and `CFG_RELEASE` environment variables |
| 4 | +//! to set the product and file version information in the Windows resource file. |
| 5 | +use std::{env, ffi, fs, path, process}; |
| 6 | + |
| 7 | +use cc::windows_registry; |
| 8 | + |
| 9 | +/// The template for the Windows resource file. |
| 10 | +const RESOURCE_TEMPLATE: &str = include_str!("../rustc.rc.in"); |
| 11 | + |
| 12 | +const VFT_APP: u32 = 0x00000001; // VFT_APP |
| 13 | +const VFT_DLL: u32 = 0x00000002; // VFT_DLL |
| 14 | + |
| 15 | +#[derive(Debug, Clone, Copy)] |
| 16 | +#[repr(u32)] |
| 17 | +pub enum VersionInfoFileType { |
| 18 | + App = VFT_APP, |
| 19 | + Dll = VFT_DLL, |
| 20 | +} |
| 21 | + |
| 22 | +/// Create and compile a Windows resource file with the product and file version information for the rust compiler. |
| 23 | +/// |
| 24 | +/// Returns the path to the compiled resource file |
| 25 | +/// |
| 26 | +/// Does not emit any cargo directives, the caller is responsible for that. |
| 27 | +pub fn compile_windows_resource_file( |
| 28 | + file_stem: &path::Path, |
| 29 | + file_description: &str, |
| 30 | + filetype: VersionInfoFileType, |
| 31 | +) -> path::PathBuf { |
| 32 | + let mut resources_dir = path::PathBuf::from(env::var_os("OUT_DIR").unwrap()); |
| 33 | + resources_dir.push("resources"); |
| 34 | + fs::create_dir_all(&resources_dir).unwrap(); |
| 35 | + |
| 36 | + let resource_compiler = find_resource_compiler(&env::var("CARGO_CFG_TARGET_ARCH").unwrap()) |
| 37 | + .expect("rc.exe not found"); |
| 38 | + |
| 39 | + let mut resource_script = RESOURCE_TEMPLATE.to_string(); |
| 40 | + |
| 41 | + // Set the string product version to the same thing as `rustc --version` |
| 42 | + let product_version = env::var("CFG_VERSION").unwrap_or("unknown".to_string()); |
| 43 | + let file_version = &product_version; |
| 44 | + |
| 45 | + // This is just "major.minor.patch" and a "-dev", "-nightly" or similar suffix |
| 46 | + let rel_version = env::var("CFG_RELEASE").unwrap(); |
| 47 | + let product_name = format!("Rust {}", &rel_version); |
| 48 | + |
| 49 | + // remove the suffix, if present and parse into [`ResourceVersion`] |
| 50 | + let version = parse_version(rel_version.split("-").next().unwrap_or("0.0.0")) |
| 51 | + .expect("could not parse CFG_RELEASE version"); |
| 52 | + |
| 53 | + resource_script = resource_script |
| 54 | + .replace("@RUSTC_FILEDESCRIPTION_STR@", file_description) |
| 55 | + .replace("@RUSTC_FILETYPE@", &format!("{}", filetype as u32)) |
| 56 | + .replace("@RUSTC_FILEVERSION_QUAD@", &version.to_quad_string()) |
| 57 | + .replace("@RUSTC_FILEVERSION_STR@", file_version) |
| 58 | + .replace("@RUSTC_PRODUCTNAME_STR@", &product_name) |
| 59 | + .replace("@RUSTC_PRODUCTVERSION_QUAD@", &version.to_quad_string()) |
| 60 | + .replace("@RUSTC_PRODUCTVERSION_STR@", &product_version); |
| 61 | + |
| 62 | + let rc_path = resources_dir.join(file_stem.with_extension("rc")); |
| 63 | + fs::write(&rc_path, resource_script) |
| 64 | + .unwrap_or_else(|_| panic!("failed to write resource file {}", rc_path.display())); |
| 65 | + |
| 66 | + let res_path = resources_dir.join(file_stem.with_extension("res")); |
| 67 | + |
| 68 | + let status = process::Command::new(resource_compiler) |
| 69 | + .arg("/fo") |
| 70 | + .arg(&res_path) |
| 71 | + .arg(&rc_path) |
| 72 | + .status() |
| 73 | + .expect("failed to execute rc.exe"); |
| 74 | + assert!(status.success(), "rc.exe failed with status {}", status); |
| 75 | + assert!( |
| 76 | + res_path.try_exists().unwrap_or(false), |
| 77 | + "resource file {} was not created", |
| 78 | + res_path.display() |
| 79 | + ); |
| 80 | + res_path |
| 81 | +} |
| 82 | + |
| 83 | +/// Windows resources store versions as four 16-bit integers. |
| 84 | +struct ResourceVersion { |
| 85 | + major: u16, |
| 86 | + minor: u16, |
| 87 | + patch: u16, |
| 88 | + build: u16, |
| 89 | +} |
| 90 | + |
| 91 | +impl ResourceVersion { |
| 92 | + /// Format the version as a comma-separated string of four integers |
| 93 | + /// as expected by Windows resource scripts for the `FILEVERSION` and `PRODUCTVERSION` fields. |
| 94 | + fn to_quad_string(&self) -> String { |
| 95 | + format!("{},{},{},{}", self.major, self.minor, self.patch, self.build) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/// Parse a version string in the format "major.minor.patch" into a form suitable for |
| 100 | +/// [`winres::VersionInfo::PRODUCTVERSION`]. |
| 101 | +/// Returns `None` if the version string is not in the expected format. |
| 102 | +fn parse_version(version: &str) -> Option<ResourceVersion> { |
| 103 | + let mut parts = version.split('.'); |
| 104 | + let major = parts.next()?.parse::<u16>().ok()?; |
| 105 | + let minor = parts.next()?.parse::<u16>().ok()?; |
| 106 | + let patch = parts.next()?.parse::<u16>().ok()?; |
| 107 | + if parts.next().is_some() { |
| 108 | + None |
| 109 | + } else { |
| 110 | + Some(ResourceVersion { major, minor, patch, build: 0 }) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/// Find the Windows SDK resource compiler `rc.exe` for the given architecture or target triple. |
| 115 | +/// Returns `None` if the tool could not be found. |
| 116 | +fn find_resource_compiler(arch_or_target: &str) -> Option<path::PathBuf> { |
| 117 | + find_windows_sdk_tool(arch_or_target, "rc.exe") |
| 118 | +} |
| 119 | + |
| 120 | +/// Find a Windows SDK tool for the given architecture or target triple. |
| 121 | +/// Returns `None` if the tool could not be found. |
| 122 | +fn find_windows_sdk_tool(arch_or_target: &str, tool_name: &str) -> Option<path::PathBuf> { |
| 123 | + // windows_registry::find_tool can only find MSVC tools, not Windows SDK tools, but |
| 124 | + // cc does include the Windows SDK tools in the PATH environment of MSVC tools. |
| 125 | + |
| 126 | + let msvc_linker = windows_registry::find_tool(arch_or_target, "link.exe")?; |
| 127 | + let path = &msvc_linker.env().iter().find(|(k, _)| k == "PATH")?.1; |
| 128 | + find_tool_in_path(tool_name, path) |
| 129 | +} |
| 130 | + |
| 131 | +/// Find a tool in the directories in a given PATH-like string. |
| 132 | +fn find_tool_in_path<P: AsRef<ffi::OsStr>>(tool_name: &str, path: P) -> Option<path::PathBuf> { |
| 133 | + env::split_paths(path.as_ref()).find_map(|p| { |
| 134 | + let tool_path = p.join(tool_name); |
| 135 | + if tool_path.try_exists().unwrap_or(false) { Some(tool_path) } else { None } |
| 136 | + }) |
| 137 | +} |
0 commit comments