Skip to content

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

@CJKay

Description

@CJKay

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-configurationArea: cargo config files and env varsA-environment-variablesArea: environment variablesA-rustflagsArea: rustflagsC-bugCategory: bugS-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions