-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Set the CARGO_BIN_NAME environment variable also when building (binary) examples #11689
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I think it should be feasible to make it happen here. fn fill_rustc_tool_env(mut cmd: ProcessBuilder, unit: &Unit) -> ProcessBuilder {
- if unit.target.is_bin() {
+ if unit.target.is_executable() { |
Let's do a quick poll whether to accept this feature: “Set the @rfcbot poll T-cargo "feature accept?" |
Team member @weihanglo has asked teams: T-cargo, for consensus on:
|
@weihanglo: This was the line change that I stumbled over, too. From a quick check with a custom-built cargo it works. |
Go ahead when it gets accepted! Also recommend adding some tests and updating docs if needed. |
Hi... I have an issue that is related to this.. I need to know when the build target is a bin.
So, I need to find a way to determine if the build is a lib/rlib/exe so I can do something like : #[cfg(windows)]
pub fn build_resource () {
#[cfg(bin)]
if Ok("release".to_owned()) == std::env::var("PROFILE") {
let mut res = winres::WindowsResource::new();
res.set_icon("../libs/myicon.ico")
;
if let Err(e) = res.compile() {
eprintln!("{}", e);
std::process::exit(1);
}
}
} Here is the error I get:
Or is that possible some other way ? Thanks |
Hi @JRAndreassen. This feels like a separate issue to me, and probably more like a user issue. I don't clearly understand the problem, but you may want to move resource generation to Alternatively, you can also create a new issue explaining the expectation with a reproducer and possible solutions and workarounds. Thank you. |
Hi @weihanglo, Well, it may be... But it seems like the same issue to me... The goal is to be able to detect build target type at compile-time. My cargo config has these entries: [lib]
name = "velocloud_sensor"
path = "src/lib.rs"
[lib]
crate-type = ["cdylib"]
name = "velocloud_sensor"
path = "src/lib.rs"
[[bin]]
name = "prtgc_velocloud"
path = "src/main.rs"
build = "build.rs" In order to determine if I need to build resources for the target, Such that: #[cfg(windows)]
pub fn build_resource () {
#[cfg(bin or cdylib))]
if Ok("release".to_owned()) == std::env::var("PROFILE") {
// Build resources only when the target is an Windows DLL or EXE
let mut res = winres::WindowsResource::new();
res.set_icon("../libs/myicon.ico")
;
if let Err(e) = res.compile() {
eprintln!("{}", e);
std::process::exit(1);
}
}
#[cfg(cdylib)]
{ // build other things for a library
}
#[cfg(rlib)]
{ // build other things for a rlibrary
}
} Thanks |
@t-rapp, since the majority of the Cargo team voted for accepting this, we can move forward implementing this. Thank you! |
Set CARGO_BIN_NAME environment variable also for binary examples ### What does this PR try to resolve? The existing CARGO_BIN_NAME environment variable is useful when building command-line programs. It lets the command know its binary name, e.g. for printing out simple usage instructions. This PR extends the CARGO_BIN_NAME environment variable to be set when building binary example targets, additional to "normal" binary targets. Fixes #11689. ### How should we test and review this PR? Create a new binary project with `cargo new hello-env`. Add a new file "examples/foo-bar.rs" with the following lines: ``` fn main() { let program = env!("CARGO_BIN_NAME"); println!("{program}"); } ``` Building and running the example with `cargo run --example foo-bar` should print a line with the string "foo-bar". Note that this PR extends the existing testcase for the CARGO_BIN_NAME environment variable with an example target.
I would also like to have a |
For target-dir for For accessing artifacts in tests, we'd instead want something like how we expose bin paths. |
Hadn't noticed this is a closed issue. If there isn't an open issue for that need, I'd recommend creating one |
Problem
The CARGO_BIN_NAME environment variable is useful when building command-line programs (see #8251). It lets the command know its binary name for printing out simple usage instructions.
Currently CARGO_BIN_NAME is only set when building a
[[bin]]
target, though. In my projects a lot of the examples are wrappers around functions of a library crate to allow testing with custom data, similar to small command-line programs. In those example binaries CARGO_BIN_NAME is not available during compilation.Proposed Solution
It would be useful to also set the CARGO_BIN_NAME environment variable for (binary) examples.
Notes
No response
The text was updated successfully, but these errors were encountered: