rust-lang / rust-lang/cargo

No way to pass environment information to compiler flags defined in Cargo configuration

Open
#7,352 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-configuration A-environment-variables A-rustflags C-bug S-needs-design
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

  1. Create a new Cargo binary project:
cargo new --bin test-case
  1. Insert into .cargo/config:
[build]
rustflags = ["-C", "link-arg=-Wl,--defsym=FOO=${BAR}"]
  1. Export BAR:
export BAR=0
  1. Build the binary:
cargo build
  1. 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:

  1. Expand rustflags before passing them to the compiler.
  2. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.