mxre / winres

Create and set windows icons and metadata for executables with a rust build script
MIT License
297 stars 42 forks source link

Adding an icon issues when building from Linux for Windows #33

Closed Joe23232 closed 3 years ago

Joe23232 commented 3 years ago

Hi,, I am trying to build an application from (Arch) Linux to Windows.

I use this command cargo build --release --target x86_64-pc-windows-gnu and it works just fine. I can execute my program just fine.

Now I want to include an icon to my executable.

I followed this post (as shown in the screenshot)

https://stackoverflow.com/questions/30291757/attaching-an-icon-resource-to-a-rust-application

image

And this works fine if I am compiling on Windows. However on Linux when building for Windows, it fails to add an icon. I have made the following modification:

build.rs

use std::io;
use winres::WindowsResource;

fn main() -> io::Result<()> {
        WindowsResource::new()
            // This path can be absolute, or relative to your crate root.
            .set_toolkit_path("/usr/bin")
            .set_windres_path("x86_64-w64-mingw32-windres")
            .set_icon("./assets/icon.ico")
            .compile()?;
    Ok(())
}

Cargo.toml

[package]
name = "testing"
version = "0.1.0"
edition = "2018"

[build-dependencies]
winres = "0.1"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

However, with the following modification I get this error message:

   Compiling testing v0.1.0 (/home/test/Desktop/programming/testing)
error: failed to run custom build command for `testing v0.1.0 (/home/test/Desktop/programming/testing)`

Caused by:
  process didn't exit successfully: `/home/test/Desktop/programming/testing/target/release/build/testing-eda5b7592106f7de/build-script-build` (exit status: 1)
  --- stdout
  package.metadata does not exist

  --- stderr
  Error: Os { code: 2, kind: NotFound, message: "No such file or directory" }

Does anyone know why this is happening?

mxre commented 3 years ago

Crosscompiling is still unsupported and I am working on it. As for your error, "package.metadata does not exist" its nor an error but an informative message because your Cargo.toml has no [package.metadata.winres] section. The NotFound error is probably because GNU tools like windres need the ar tool too, try specifying the appropriate tool with set_ar_path("") (after new, before compile)

Joe23232 commented 3 years ago

As for your error, "package.metadata does not exist" its nor an error but an informative message because your Cargo.toml has no [package.metadata.winres] section. The NotFound error is probably because GNU tools like windres need the ar tool too, try specifying the appropriate tool with set_ar_path("") (after new, before compile)

If I fixed this the icon will still not appear as you said Crosscompiling is still unsupported and I am working on it., am I correct?

mxre commented 3 years ago

I uploaded a new version (0.1.12) to crates.io, Please look at #24 and the example in the repository.

Joe23232 commented 3 years ago

Thanks mate

On Wed, Sep 29, 2021 at 6:00 AM Max Resch @.***> wrote:

I uploaded a new version (0.1.12) to crates.io, Please look at #24 https://github.com/mxre/winres/pull/24 and the example in the repository.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mxre/winres/issues/33#issuecomment-929579869, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIKO7IP5OTAYCEFI2RLDURDUEINE7ANCNFSM5BPY6G7A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

getreu commented 3 years ago

I uploaded a new version (0.1.12) to crates.io, Please look at #24 and the example in the repository.

Both together works for me.

AshfordN commented 3 years ago

I'm not fully clear on this. Is cross-compilation supported or not? I'm attempting to cross compile for Windows on Linux, and the icon still isn't being set after following the above suggestions. Below is the build script I'm using:

use std::{env, io};
use winres::WindowsResource;

fn main() -> io::Result<()> {
    let target_family = env::var("CARGO_CFG_TARGET_FAMILY").unwrap();
    if target_family == "windows" {
        WindowsResource::new()
            .set_toolkit_path("/usr/bin")
            .set_windres_path("x86_64-w64-mingw32-windres")
            .set_ar_path("x86_64-w64-mingw32-ar")
            .set("ProductVersion", "1.1.0")
            .set_icon("icon.ico")
            .compile()?;
    }

    Ok(())
}

I'm also using winres 0.1.12

mxre commented 3 years ago

@AshfordN I can't recreate your missing icon with the build.rs you provided. Please look at the output in target/x86_64-pc-windows-gnu/{release,debug}/build/<cratename>/output should look something like this:

cargo:rustc-link-search=native=/git/winres/example/target/x86_64-pc-windows-gnu/release/build/winres-example-7f6f80bd1c122442/output
cargo:rustc-link-lib=static=resource

the resource file (.rc) itself is in target/x86_64-pc-windows-gnu/{release,debug}/build/<createname>/out together with its compiled (.o) form and its library form (.a) (because cargo only allows adding static libraries via build script but not single object files)

Also the icon file path is relative to the create and not the build script.

AshfordN commented 3 years ago

@mxre I did some further testing with a basic project and, using the same build script above, I was able to successfully set the icon. However, I found that if I added a library crate to the package, things stopped working. Since I'm still new to rust, I'm not quite sure if this is an obvious side effect, but I would appreciated any feedback on the matter. Below is a summary of the sample project I'm using:

Directory structure

.
├─src
│  ├─lib.rs
│  └─main.rs
├─build.rs
├─cargo.toml
└─icon.ico

libs.rs

pub mod foo {
    pub fn bar() {
        println!("this is a test");
    }
}

main.rs

use sample::foo;

fn main() {
    foo::bar();
}

build.rs

same as above

cargo.toml

[package]
name = "sample-project"
version = "0.1.0"
edition = "2018"
build = "build.rs"

[lib]
name = "sample"
path = "src/lib.rs"

[build-dependencies]
winres = "0.1"

icon.ico

I'm not able to attached the exact .ico file, but I guess any should work in this case

AshfordN commented 3 years ago

Nevermind, I see now that my issue is a dupplicate of #32

mxre commented 3 years ago

Not exactly a duplicate. But certainly unrelated to crosscompiling.