Skip to content

Instantly share code, notes, and snippets.

@SteveGilham
Created July 4, 2016 18:46
Show Gist options
  • Save SteveGilham/60b4d3dac30cf67a59caefb80d53b033 to your computer and use it in GitHub Desktop.
Save SteveGilham/60b4d3dac30cf67a59caefb80d53b033 to your computer and use it in GitHub Desktop.
A hacky resource builder script for Rust-MSVC
use std::process::Command;
use std::env;
use std::path::Path;
fn main() {
let out_dir = env::var("OUT_DIR").ok().expect("can't find out_dir");
Command::new("C:\\Program Files (x86)\\Windows Kits\\8.1\\bin\\x64\\rc.exe")
.args(&["/v", "/fo", "hello_rc.lib"]) // HACK HACK HACK
.args(&["..\\..\\..\\..\\..\\src\\hello.rc"])
.current_dir(&Path::new(&out_dir))
.status()
.unwrap();
println!("cargo:rustc-link-search=native={}", out_dir);
println!("cargo:rustc-link-lib=static=hello_rc");
}
@SteveGilham
Copy link
Author

It's hacky, since it builds the .rc file to a .res file that actually calls itself a .lib, so that the cargo tool will add the extensionless name as a .lib to the linker, and the linker, fortunately, ignores the file extension and just looks at the file content. It's also hacky in that it hard-codes the path to a local rc.exe, and works around the fact that that tool does not offer an output directory parameter by tree-walking from the directory where we want to place the output to where the source resides.

I'm sure there must be a better way, but this works. Apart from the fact that a VERSIONINFO compiles OK into the resulting .exe (as can be seen by opening it in Visual Studio), but doesn't show in the file properties. Application icons work just fine, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment