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

cfg may not work in build script #37

Open
zu1k opened this issue Oct 23, 2021 · 2 comments
Open

cfg may not work in build script #37

zu1k opened this issue Oct 23, 2021 · 2 comments

Comments

@zu1k
Copy link

zu1k commented Oct 23, 2021

I'm cross compiling from linux to windows, but I found it not work.

I checked the history issue and found that the problem is still unresolved.

However, in the course of my continuous testing, I found that the cfg conditional judgment in the build script does not work.

My Test Demo

// build.rs

#[cfg(target_os="windows")]
fn main() {
    panic!("target_os = windows")
}

#[cfg(windows)]
fn main() {
    panic!("windows")
}

#[cfg(unix)]
fn main() {
    panic!("unix")
}

host system

$ uname -a
Linux alex 5.13.19-2-MANJARO #1 SMP PREEMPT Sun Sep 19 21:31:53 UTC 2021 x86_64 GNU/Linux

target: x86_64-unknown-linux-gnu (local)

$ cargo build 
   Compiling target v0.1.0 (/data/projects/target)
error: failed to run custom build command for `target v0.1.0 (/data/projects/target)`

Caused by:
  process didn't exit successfully: `/data/projects/target/target/debug/build/target-fff5ba352ad7afdb/build-script-build` (exit status: 101)
  --- stderr
  thread 'main' panicked at 'unix', build.rs:13:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

That's fine, however when cross compile...

target: x86_64-pc-windows-gnu (cross compile)

$ cargo build --target x86_64-pc-windows-gnu 
   Compiling target v0.1.0 (/data/projects/target)
error: failed to run custom build command for `target v0.1.0 (/data/projects/target)`

Caused by:
  process didn't exit successfully: `/data/projects/target/target/debug/build/target-fff5ba352ad7afdb/build-script-build` (exit status: 101)
  --- stderr
  thread 'main' panicked at 'unix', build.rs:13:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I expect it to output window or target_os = windows, but it panic at unix

My solution

I think it may be because some features were not determined when build.rs was compiled and the features not been passed to rustc.

I have found some environment variables from the documentation and they can be used to determine target and host.

use std::env;

fn main() {
    if env::var("PROFILE").unwrap() == "release" {
        if let Ok(_) = env::var("CARGO_CFG_WINDOWS") {
            let mut res = winres::WindowsResource::new();
            if let Ok(host) = env::var("HOST") {
                if host.contains("linux") {
                    res.set_toolkit_path("/usr/bin")
                        .set_windres_path("x86_64-w64-mingw32-windres");
                }
            }
            res.set_icon("res/copy-translator.ico").set_language(0x04);
            res.compile().unwrap();
        }
    }
}

Icons are still not set successfully when cross compile fron linux to windows

@zu1k
Copy link
Author

zu1k commented Oct 24, 2021

Icons are still not set successfully when cross compile fron linux to windows

Solved by remove lib.rs

@BenjaminRi
Copy link

Cross-compilation should now work out of the box in v0.1.13 of my fork. I updated the README as well, it had some buggy instructions (target_os is the host OS when you compile build.rs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants