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

Alternative to RUSTFLAGS that allows spaces within arguments #3686

Closed
Jascha-N opened this issue Feb 11, 2017 · 5 comments
Closed

Alternative to RUSTFLAGS that allows spaces within arguments #3686

Jascha-N opened this issue Feb 11, 2017 · 5 comments
Labels
A-build-execution Area: anything dealing with executing the compiler A-configuration Area: cargo config files and env vars A-environment-variables Area: environment variables A-rustflags Area: rustflags C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Z-advanced-env Nightly: advanced-env

Comments

@Jascha-N
Copy link

Jascha-N commented Feb 11, 2017

I'm working on a build tool for setting up a cargo environment to build projects that depend on Arduino cores and libraries. It detects the target architecture, compiler locations and arguments, linker location and arguments and a bunch of other stuff that needs to be passed to rustc based on a given Arduino board specification. Currently I pass these arguments using the RUSTFLAGS environment variable, Things like the linker path and linker script can point to arbitrary files on the file system and can thus contains spaces. Neither RUSTFLAGS nor CARGO_BUILD_RUSTFLAGS support spaces in arguments and generating a .cargo/config file that could possibly overwrite a user specified config file is also not an option. What are my options?

And another question. Does the CARGO_BUILD_TARGET variable not work or could this be a problem on xargo's end?

Thanks!

@alexcrichton
Copy link
Member

If you're not willing to use .cargo/config, another option would be to change the compiler entirely to a wrapper of some form you provide around the real compiler, although that's not guaranteed to work in all situations.

Also I'm not so sure about xargo :(

@Jascha-N
Copy link
Author

Jascha-N commented Feb 12, 2017

Thanks.

If you're not willing to use .cargo/config, another option would be to change the compiler entirely to a wrapper of some form you provide around the real compiler, although that's not guaranteed to work in all situations.

That seems a little bit excessive, but I guess it's possible.

If parsing quoted RUSTFLAGS arguments is out of the question it would at least be nice to be able to pass a custom config file location to cargo, either through an environment variable or a command line option. Another possibility is to allow CARGO_ config overrides for lists/tables. For example: CARGO_BUILD_RUSTFLAGS.0, CARGO_BUILD_RUSTFLAGS.1, etc. or use JSON serialization for config overrides with composite data types.

If I'm not mistaken one of the goals for Cargo in 2017 is better integration into other build systems. Not having any (non-destructive) way to pass custom arguments with spaces to the compiler seems pretty limiting in this respect. No straightforward way to obtain the paths of generated artefacts is another.

For now, I'll go with your solution of creating a wrapper around the compiler.

Also I'm not so sure about xargo :(

Looking through the source code, xargo does not seem to interpret CARGO_BUILD_TARGET or any other config overrides, unfortunately. :(

I'm also wondering. Is there any difference between RUSTC and CARGO_BUILD_RUSTC or 'RUSTDOC' and CARGO_BUILD_RUSTDOC, etc.?

@alexcrichton
Copy link
Member

Yeah I'd be totally open to alternatives here in how to define env vars, and something like FOO.n where n is an index in an array sounds plausible at least!

Also for now RUSTC == CARGO_BUILD_RUSTC, we just added the former first and the latter fell out of blanket "read config from env vars as well"

@carols10cents carols10cents added A-build-execution Area: anything dealing with executing the compiler A-configuration Area: cargo config files and env vars A-environment-variables Area: environment variables C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` labels Sep 29, 2017
@ehuss ehuss added the Z-advanced-env Nightly: advanced-env label Oct 4, 2019
@ehuss ehuss added the A-rustflags Area: rustflags label Apr 6, 2020
@jyn514
Copy link
Member

jyn514 commented Dec 30, 2020

You can do this today with -Z unstable-options --config build.rustflags="$RUSTFLAGS", the same way that docs.rs does: https://github.com/rust-lang/docs.rs/blob/bcc1a2c9f63a62907abbe68dcbe3f73632a9a4de/crates/metadata/lib.rs#L255-L263

@ehuss
Copy link
Contributor

ehuss commented Nov 22, 2022

Closing as this is mostly resolved now. There are several mechanisms for setting flags that have spaces in them:

  • CARGO_ENCODED_RUSTFLAGS uses the \x1f character to separate arguments.
  • The --config CLI option can pass arbitrary TOML.
  • Flags can be set in cargo.toml with build.rustflags and other variants.
  • (nightly only, undocumented) -Z advanced-env with CARGO_BUILD_RUSTFLAGS supports TOML values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-build-execution Area: anything dealing with executing the compiler A-configuration Area: cargo config files and env vars A-environment-variables Area: environment variables A-rustflags Area: rustflags C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Z-advanced-env Nightly: advanced-env
Projects
None yet
Development

No branches or pull requests

5 participants