No way to pass environment information to compiler flags defined in Cargo configuration
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 15.5k
- Forks
- 3k
- Avg merge
- 23h 30m
- Merged PRs (30d)
- 51
Description
The .cargo/config file, through its build.rustflags key, allows you to add arbitrary compiler flags, which are appended to those passed by Cargo at build-time. These flags, however, are not parsed or evaluated in any way, which means that no environment information can be used in these flags.
Steps
- Create a new Cargo binary project:
cargo new --bin test-case
- Insert into .cargo/config:
[build]
rustflags = ["-C", "link-arg=-Wl,--defsym=FOO=${BAR}"]
- Export BAR:
export BAR=0
- Build the binary:
cargo build
- Note the output of the error, which shows that the
${BAR}variable was not expanded:
= note: /usr/bin/ld:--defsym:1: ignoring invalid character `{' in expression
/usr/bin/ld:--defsym:1: syntax error
collect2: error: ld returned 1 exit status
Possible Solution(s)
There are two obvious solutions to this:
- Expand
rustflagsbefore passing them to the compiler. - Allow build.rs to override arbitrary compiler flags.
Notes
Output of cargo version: cargo 1.39.0-nightly (fe0e5a48b 2019-09-04)
The original issue that prompted this is that there is no way to pass the Cargo output directory as a part of the -Map linker flag, which means that the map file is always generated in the source directory (highly undesirable).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with Cargo's handling of the .cargo/config build.rustflags key and reproduce the reported cargo build case using BAR and ${BAR}. Compare the proposed expansion and build.rs approaches, then define and test behavior for passing environment-derived values to compiler flags without breaking existing configuration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- build-system
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100