Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi executable support #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/.idea/
/target/
/example/target
/multi_binary_example/target
Cargo.lock
*~
*.bk
Expand Down
61 changes: 47 additions & 14 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,8 @@ impl WindowsResource {
self
}

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

if version_check::is_min_version("1.61.0").unwrap_or(true) {
println!("cargo:rustc-link-lib=static:+whole-archive=resource");
match binary {
None => {
println!("cargo:rustc-link-lib=static:+whole-archive=resource");
}
Some(binary) => {
println!("cargo:rustc-link-arg-bin={}=--whole-archive", binary);
println!("cargo:rustc-link-arg-bin={}={}.o", binary, binary);
}
}
} else {
println!("cargo:rustc-link-lib=static=resource");
match binary {
None => {
println!("cargo:rustc-link-lib=static=resource");
}
Some(binary) => {
println!("cargo:rustc-link-arg-bin={}={}.o", binary, binary);
}
}
}

Ok(())
}

/// Run the resource compiler
/// `cargo:rustc-link-lib=` and `cargo:rustc-link-search` on the console,
/// so that the cargo build script can link the compiled resource file.
pub fn compile(&self) -> io::Result<()> {
self.compile_internal(None)
}

/// Run the resource compiler for a specific binary.
///
/// This function generates a resource file from the settings or
/// uses an existing resource file and passes it to the resource compiler
/// of your toolkit.
///
/// Further more we will print the correct statements for
/// `cargo:rustc-link-lib=` and `cargo:rustc-link-search` on the console,
/// so that the cargo build script can link the compiled resource file.
pub fn compile(&self) -> io::Result<()> {
/// Furthermore we will print the correct statements for
/// `cargo:rustc-link-arg-bin=` and `cargo:rustc-link-search` on the console,
/// so that the cargo build script can link the compiled resource file for the desired binary.
pub fn compile_for(&self, binary: &str) -> io::Result<()> {
self.compile_internal(Some(binary))
}

fn compile_internal(&self, binary: Option<&str>) -> io::Result<()> {
let output = PathBuf::from(&self.output_directory);
let rc = output.join("resource.rc");
let rc = output.join(format!("{}.rc", binary.unwrap_or("resource")));
if self.rc_file.is_none() {
self.write_resource_file(&rc)?;
}
Expand All @@ -678,16 +704,16 @@ impl WindowsResource {

let target_env = std::env::var("CARGO_CFG_TARGET_ENV").unwrap();
match target_env.as_str() {
"gnu" => self.compile_with_toolkit_gnu(rc.as_str(), &self.output_directory),
"msvc" => self.compile_with_toolkit_msvc(rc.as_str(), &self.output_directory),
"gnu" => self.compile_with_toolkit_gnu(rc.as_str(), &self.output_directory, binary),
"msvc" => self.compile_with_toolkit_msvc(rc.as_str(), &self.output_directory, binary),
_ => Err(io::Error::new(
io::ErrorKind::Other,
"Can only compile resource file when target_env is \"gnu\" or \"msvc\"",
)),
}
}

fn compile_with_toolkit_msvc<'a>(&self, input: &'a str, output_dir: &'a str) -> io::Result<()> {
fn compile_with_toolkit_msvc<'a>(&self, input: &'a str, output_dir: &'a str, binary: Option<&str>) -> io::Result<()> {
let rc_exe = PathBuf::from(&self.toolkit_path).join("rc.exe");
let rc_exe = if !rc_exe.exists() {
if cfg!(target_arch = "x86_64") {
Expand All @@ -699,7 +725,7 @@ impl WindowsResource {
rc_exe
};
println!("Selected RC path: '{}'", rc_exe.display());
let output = PathBuf::from(output_dir).join("resource.lib");
let output = PathBuf::from(output_dir).join(format!("{}.lib", binary.unwrap_or("resource")));
let input = PathBuf::from(input);
let mut command = process::Command::new(&rc_exe);
let command = command.arg(format!("/I{}", env::var("CARGO_MANIFEST_DIR").unwrap()));
Expand Down Expand Up @@ -732,7 +758,14 @@ impl WindowsResource {
}

println!("cargo:rustc-link-search=native={}", output_dir);
println!("cargo:rustc-link-lib=dylib=resource");
match binary {
None => {
println!("cargo:rustc-link-lib=dylib=resource");
}
Some(binary) => {
println!("cargo:rustc-link-arg-bin={}={}.lib", binary, binary);
}
}
Ok(())
}
}
Expand Down
19 changes: 19 additions & 0 deletions multi_binary_example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "multi_binary_example"
description = "Winres - example script for a library with 2 binaries"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["rlib"]

[build-dependencies]
winresource = { path = ".." }

[[bin]]
name = "a"
path = "bin/a.rs"

[[bin]]
name = "example-b"
path = "bin/b.rs"
3 changes: 3 additions & 0 deletions multi_binary_example/bin/a.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("2 + 2 is {}", multi_binary_example::add(2, 2));
}
3 changes: 3 additions & 0 deletions multi_binary_example/bin/b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Example B");
}
23 changes: 23 additions & 0 deletions multi_binary_example/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
// Do not do this
// This will cause the binaries to fail building with the below error:
// "fatal error CVT1100: duplicate resource"

// let lib_res = tauri_winres::WindowsResource::new();
// lib_res.compile()?;

// Binary target with name "a"
let mut a_res = winresource::WindowsResource::new();
a_res.set("FileDescription", "multi_binary_example - example A");
a_res.compile_for("a")?;

// Binary target with name "example-b"
let mut b_res = winresource::WindowsResource::new();
b_res.set("FileDescription", "multi_binary_example - example B");
b_res.compile_for("example-b")?;
}
Ok(())
}
3 changes: 3 additions & 0 deletions multi_binary_example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}