Skip to content

Commit

Permalink
Merged PR 931676: Compile Windows message resource files at build time
Browse files Browse the repository at this point in the history
Compile Windows message resource files at build time
  • Loading branch information
avranju committed Jun 29, 2018
1 parent 0a949fb commit aa38367
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 8 deletions.
17 changes: 17 additions & 0 deletions edgelet/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions edgelet/iotedged/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ win-logger = { path = "../win-logger" }

[dev_dependencies]
tempdir = "0.3.7"

edgelet-test-utils = { path = "../edgelet-test-utils" }

[target.'cfg(windows)'.build-dependencies]
winreg = "0.5.1"
version-compare = "0.0.6"
88 changes: 82 additions & 6 deletions edgelet/iotedged/build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,87 @@
// Copyright (c) Microsoft. All rights reserved.
#[cfg(windows)]
extern crate version_compare;
#[cfg(windows)]
extern crate winreg;

fn main() {
#[cfg(windows)]
{
let current_dir = ::std::env::var("CARGO_MANIFEST_DIR").unwrap();
println!(
"cargo:rustc-link-search=all={}\\src\\resources",
current_dir,
);
windows::main()
}

#[cfg(windows)]
mod windows {
use std::cmp::Ordering;
use std::env;
use std::fs;
use std::io::Result;
use std::path::{Path, PathBuf};
use std::process::Command;

use version_compare::comp_op::CompOp;
use version_compare::VersionCompare;
use winreg::enums::*;
use winreg::RegKey;

pub fn main() {
// on windows we only support x64 at this time
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
if target_arch != "x86_64" {
panic!("Only x64 architecture is supported on Windows at this time.");
}

let sdk_bin_root = get_sdk_bin_root().expect("Could not get Windows SDK root bin path");
let mc_path = sdk_bin_root.join("mc.exe");
let rc_path = sdk_bin_root.join("rc.exe");
let out_dir = env::var("OUT_DIR").unwrap();

Command::new(mc_path.to_str().unwrap())
.args(&[
"-r",
&out_dir,
"-h",
&out_dir,
"src\\resources\\event_messages.mc",
])
.status()
.expect("Message compilation failed");

let rc_source_path = Path::new(&out_dir).join("event_messages.rc");
Command::new(rc_path.to_str().unwrap())
.args(&[rc_source_path.to_str().unwrap()])
.status()
.expect("Resource compilation failed");

fs::rename(
Path::new(&out_dir).join("event_messages.res"),
Path::new(&out_dir).join("event_messages.res.lib"),
).expect("Rename of event_messages.res failed");

println!("cargo:rustc-link-search=all={}", out_dir);
println!("cargo:rustc-link-lib=event_messages.res");
}

fn get_sdk_bin_root() -> Result<PathBuf> {
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
let installed_roots =
hklm.open_subkey("SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots")?;
let max_version = installed_roots
.enum_keys()
.map(|v| v.unwrap())
.max_by(|v1, v2| comp_op_to_ordering(VersionCompare::compare(v1, v2).unwrap()))
.unwrap();

let kits_root: String = installed_roots.get_value("KitsRoot10").unwrap();
let install_root = Path::new(&kits_root);

Ok(install_root.join("bin").join(max_version).join("x64"))
}

fn comp_op_to_ordering(op: CompOp) -> Ordering {
match op {
CompOp::Eq | CompOp::Le | CompOp::Ge => Ordering::Equal,
CompOp::Lt | CompOp::Ne => Ordering::Less,
CompOp::Gt => Ordering::Greater,
}
}
}
Binary file removed edgelet/iotedged/src/resources/MSG00001.bin
Binary file not shown.
2 changes: 0 additions & 2 deletions edgelet/iotedged/src/resources/event_messages.rc

This file was deleted.

Binary file removed edgelet/iotedged/src/resources/event_messages.res.lib
Binary file not shown.

0 comments on commit aa38367

Please sign in to comment.