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

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

Open
CJKay opened this issue Sep 11, 2019 · 2 comments
Labels
A-configuration Area: cargo config files and env vars A-environment-variables Area: environment variables A-rustflags Area: rustflags C-bug Category: bug S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

Comments

@CJKay
Copy link

CJKay commented Sep 11, 2019

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 (fe0e5a4 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).

@CJKay CJKay added the C-bug Category: bug label Sep 11, 2019
@ehuss ehuss added A-configuration Area: cargo config files and env vars A-environment-variables Area: environment variables labels Sep 21, 2019
@Luminoth
Copy link

Luminoth commented Mar 6, 2020

It would be amazing if either this or #5376 happened. For our project we have to pass a dynamic sysroot through RUSTFLAGS ('-C link-arg=--sysroot=$SYSROOT') which means we have to pass the entire RUSTFLAGS through our build system rather than just the one argument (or having it set an env var to be expanded by Cargo).

@epage
Copy link
Contributor

epage commented Nov 2, 2023

Side note: #12193 requests other ways of modifying RUSTFLAGS

@epage epage added the S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted. label Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-configuration Area: cargo config files and env vars A-environment-variables Area: environment variables A-rustflags Area: rustflags C-bug Category: bug S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.
Projects
None yet
Development

No branches or pull requests

4 participants