Skip to content

Commit 56707dc

Browse files
committed
added multi executable support
1 parent 84b9f37 commit 56707dc

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

lib.rs

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,8 @@ impl WindowsResource {
614614
self
615615
}
616616

617-
fn compile_with_toolkit_gnu<'a>(&self, input: &'a str, output_dir: &'a str) -> io::Result<()> {
618-
let output = PathBuf::from(output_dir).join("resource.o");
617+
fn compile_with_toolkit_gnu<'a>(&self, input: &'a str, output_dir: &'a str, binary: Option<&'a str>) -> io::Result<()> {
618+
let output = PathBuf::from(output_dir).join(format!("{}.o", binary.unwrap_or("resource")));
619619
let input = PathBuf::from(input);
620620
let status = process::Command::new(&self.windres_path)
621621
.current_dir(&self.toolkit_path)
@@ -647,26 +647,52 @@ impl WindowsResource {
647647
println!("cargo:rustc-link-search=native={}", output_dir);
648648

649649
if version_check::is_min_version("1.61.0").unwrap_or(true) {
650-
println!("cargo:rustc-link-lib=static:+whole-archive=resource");
650+
match binary {
651+
None => {
652+
println!("cargo:rustc-link-lib=static:+whole-archive=resource");
653+
}
654+
Some(binary) => {
655+
println!("cargo:rustc-link-arg-bin={}=--whole-archive", binary);
656+
println!("cargo:rustc-link-arg-bin={}={}.o", binary, binary);
657+
}
658+
}
651659
} else {
652-
println!("cargo:rustc-link-lib=static=resource");
660+
match binary {
661+
None => {
662+
println!("cargo:rustc-link-lib=static=resource");
663+
}
664+
Some(binary) => {
665+
println!("cargo:rustc-link-arg-bin={}={}.o", binary, binary);
666+
}
667+
}
653668
}
654669

655670
Ok(())
656671
}
657672

658-
/// Run the resource compiler
673+
/// `cargo:rustc-link-lib=` and `cargo:rustc-link-search` on the console,
674+
/// so that the cargo build script can link the compiled resource file.
675+
pub fn compile(&self) -> io::Result<()> {
676+
self.compile_internal(None)
677+
}
678+
679+
/// Run the resource compiler for a specific binary.
659680
///
660681
/// This function generates a resource file from the settings or
661682
/// uses an existing resource file and passes it to the resource compiler
662683
/// of your toolkit.
663684
///
664-
/// Further more we will print the correct statements for
665-
/// `cargo:rustc-link-lib=` and `cargo:rustc-link-search` on the console,
666-
/// so that the cargo build script can link the compiled resource file.
667-
pub fn compile(&self) -> io::Result<()> {
685+
/// Furthermore we will print the correct statements for
686+
/// `cargo:rustc-link-arg-bin=` and `cargo:rustc-link-search` on the console,
687+
/// so that the cargo build script can link the compiled resource file for the desired binary.
688+
pub fn compile_for(&self, binary: &str) -> io::Result<()> {
689+
self.compile_internal(Some(binary))
690+
}
691+
692+
fn compile_internal(&self, binary: Option<&str>) -> io::Result<()> {
668693
let output = PathBuf::from(&self.output_directory);
669694
let rc = output.join("resource.rc");
695+
let rc = output.join(format!("{}.rc", binary.unwrap_or("resource")));
670696
if self.rc_file.is_none() {
671697
self.write_resource_file(&rc)?;
672698
}
@@ -678,16 +704,16 @@ impl WindowsResource {
678704

679705
let target_env = std::env::var("CARGO_CFG_TARGET_ENV").unwrap();
680706
match target_env.as_str() {
681-
"gnu" => self.compile_with_toolkit_gnu(rc.as_str(), &self.output_directory),
682-
"msvc" => self.compile_with_toolkit_msvc(rc.as_str(), &self.output_directory),
707+
"gnu" => self.compile_with_toolkit_gnu(rc.as_str(), &self.output_directory, binary),
708+
"msvc" => self.compile_with_toolkit_msvc(rc.as_str(), &self.output_directory, binary),
683709
_ => Err(io::Error::new(
684710
io::ErrorKind::Other,
685711
"Can only compile resource file when target_env is \"gnu\" or \"msvc\"",
686712
)),
687713
}
688714
}
689715

690-
fn compile_with_toolkit_msvc<'a>(&self, input: &'a str, output_dir: &'a str) -> io::Result<()> {
716+
fn compile_with_toolkit_msvc<'a>(&self, input: &'a str, output_dir: &'a str, binary: Option<&str>) -> io::Result<()> {
691717
let rc_exe = PathBuf::from(&self.toolkit_path).join("rc.exe");
692718
let rc_exe = if !rc_exe.exists() {
693719
if cfg!(target_arch = "x86_64") {
@@ -699,7 +725,7 @@ impl WindowsResource {
699725
rc_exe
700726
};
701727
println!("Selected RC path: '{}'", rc_exe.display());
702-
let output = PathBuf::from(output_dir).join("resource.lib");
728+
let output = PathBuf::from(output_dir).join(format!("{}.lib", binary.unwrap_or("resource")));
703729
let input = PathBuf::from(input);
704730
let mut command = process::Command::new(&rc_exe);
705731
let command = command.arg(format!("/I{}", env::var("CARGO_MANIFEST_DIR").unwrap()));
@@ -732,7 +758,14 @@ impl WindowsResource {
732758
}
733759

734760
println!("cargo:rustc-link-search=native={}", output_dir);
735-
println!("cargo:rustc-link-lib=dylib=resource");
761+
match binary {
762+
None => {
763+
println!("cargo:rustc-link-lib=dylib=resource");
764+
}
765+
Some(binary) => {
766+
println!("cargo:rustc-link-arg-bin={}={}.lib", binary, binary);
767+
}
768+
}
736769
Ok(())
737770
}
738771
}

0 commit comments

Comments
 (0)