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

Wrong post-build if .cargo/config contains build target #16

Closed
MathiasKoch opened this issue Jan 13, 2021 · 6 comments
Closed

Wrong post-build if .cargo/config contains build target #16

MathiasKoch opened this issue Jan 13, 2021 · 6 comments

Comments

@MathiasKoch
Copy link

MathiasKoch commented Jan 13, 2021

If .config/cargo contains a build target where std is not available, post-build will fail, as it tries to compile post-build for said target.

.cargo/config:

[build]
target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)

post_build.rs:

use std::{path::PathBuf, env};

fn main() {
    let current_dir = env::current_dir().unwrap();
    let current_parent = current_dir.parent().unwrap();
    println!("current_dir: {:?}", current_dir);
    println!("current_parent: {:?}", current_parent);
    println!("ok");
}

cargo post build -p factbird-rs:

Finished dev [optimized + debuginfo] target(s) in 0.03s
Running Post Build Script at /home/mathias/git/blackbird/factbird-mini/factbird-rs/post_build.rs
   Compiling post-build-script v0.1.0 (/home/mathias/git/blackbird/factbird-mini/target/post_build_script_manifest)
error[E0463]: can't find crate for `std`
  |
  = note: the `thumbv7em-none-eabihf` target may not be installed

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error: could not compile `post-build-script`

To learn more, run the command again with --verbose.

I can't quite figure out if this is actually the same as #2 ?

As i see it, the post_build script should always be run using the host target, and never be influenced by .config/cargo target?

@phil-opp
Copy link
Owner

Thanks for reporting! The issue is that .cargo/config files apply to all cargo invocations in that directory. Unfortunately, there is currently no way (e.g. a command line flag) to unset a default target for a single invocation.

I also encountered this underlying problem many times, e.g. as described here: rust-lang/cargo#8757 (comment). I now think the best solution for this is https://internals.rust-lang.org/t/proposal-move-some-cargo-config-settings-to-cargo-toml/13336, which would allow to specify the default target as a package-specific key in the Cargo.toml. There is already an implementation for this in rust-lang/cargo#9030, let's hope it gets merged soon.

@MathiasKoch
Copy link
Author

Hmm, okay.
So what i hear you say, is that there is not even a work-around currently?

Lets hope for rust-lang/cargo#9030 soon :)

@phil-opp
Copy link
Owner

The only workaround that I'm aware of is to not use a default target and instead pass it as --target argument on the command line. Alternatively, you can create a custom alias for this for easier typing (e.g. cargo arm-build or cargo xbuild).

@MathiasKoch
Copy link
Author

It seems to work if i remove the default target, and builds with cargo post build --target ... --release, but the custom alias to make this easier, does not seem to work as expected, due to

cargo-post/src/main.rs

Lines 42 to 53 in ef1b4d0

let build_script_call = match args.peek().map(Deref::deref) {
Some(cmd) => match cmd {
"b" | "build" | "xbuild" => BuildScriptCall::AfterCommand,
"c" | "check" | "clean" | "doc" | "new" | "init" | "update" | "search"
| "uninstall" => BuildScriptCall::NoCall,
cmd if ["run", "test", "bench", "publish", "install"].contains(&cmd) => {
panic!("`cargo post {}` is not supported yet", cmd)
}
cmd => panic!("unknown cargo command `cargo {}`", cmd),
},
None => BuildScriptCall::NoCall,
};

An alias like

[alias]
br = "build --release --target thumbv7em-none-eabihf"

Will always result in unknown cargo command 'cargo br'

@phil-opp
Copy link
Owner

Have you tried including the post in the alias, i.e. br = "post build --release --target thumbv7em-none-eabihf"?

@MathiasKoch
Copy link
Author

MathiasKoch commented Jan 15, 2021

Ahh, no haven't tried that. Will give it a shot

EDIT
Confirmed, it works to include post

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