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

Add option to pass environment variables to rustc with a flag #80792

Open
dcbaker opened this issue Jan 7, 2021 · 6 comments
Open

Add option to pass environment variables to rustc with a flag #80792

dcbaker opened this issue Jan 7, 2021 · 6 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dcbaker
Copy link

dcbaker commented Jan 7, 2021

See #78913 for more background.

I'm a meson developer, working to make rust work better with meson. I know that cargo is the official build system for rust, but for me and the projects I work on cargo is unacceptable, we have a massive C/C++ codebase already using meson, with interest in migrating some (but not all) of our code to rust. Mostly things work fine, but one problem we do have is including generated code.

Cargo handles generated code by setting an OUT_DIR environment variable, this works for cargo because it has a number of unique design decisions compared to most build systems. For Cmake and meson and particular this is problematic because we generate a declarative build system which the user invokes, visual-studio, ninja, make, etc; so there are separate "configure" and "build" stages. For ninja in particular environment variables are problematic because it's made an explicit design choice not to support them. As such, we have no way to set environment variables at build time without resorting to something ugly like wrapping rustc in a script to proxy the variables.

What would be nice is the ability to pass environment variables to rustc directly, much like --cfg, something like --env "OUT_DIR=/some/dir". This would allow us to make include() work like cargo, but without needing to resort to wrapping rustc and making things slower than they need to be.

@jyn514
Copy link
Member

jyn514 commented Jan 7, 2021

As such, we have no way to set environment variables at build time without resorting to something ugly like wrapping rustc in a script to proxy the variables.

For what it's worth, this is exactly what x.py does: https://github.com/rust-lang/rust/blob/master/src/bootstrap/bin/rustc.rs

What would be nice is the ability to pass environment variables to rustc directly, much like --cfg, something like --env "OUT_DIR=/some/dir".

To be clear, you only need this to support env!, right, not actual environment variables?

@jyn514 jyn514 added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jan 7, 2021
@dcbaker
Copy link
Author

dcbaker commented Jan 7, 2021

Yes exactly, just env!.

@jyn514
Copy link
Member

jyn514 commented Jan 7, 2021

Just so you know, I doubt anyone will pick this up as-is. Normally the way small things like this get implemented is by making a PR implementing it, which then goes through FCP. If you don't know how to implement it, you could make an MCP and hope someone picks it up instead.

@dcbaker
Copy link
Author

dcbaker commented Jan 7, 2021

Thanks, I'm new in these parts and don't know all of the process yet :)

@camelid camelid added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 7, 2021
@GuillaumeGomez
Copy link
Member

Opened an MCP about it: rust-lang/compiler-team#653

bors added a commit to rust-lang-ci/rust that referenced this issue Dec 10, 2023
Implement `--env` compiler flag (without `tracked_env` support)

Part of rust-lang#80792.
Implementation of rust-lang/compiler-team#653.
Not an implementation of rust-lang/rfcs#2794.

It adds the `--env` compiler flag option which allows to set environment values used by `env!` and `option_env!`.

Important to note: When trying to retrieve an environment variable value, it will first look into the ones defined with `--env`, and if there isn't one, then only it will look into the environment variables. So if you use `--env PATH=a`, then `env!("PATH")` will return `"a"` and not the actual `PATH` value.

As mentioned in the title, `tracked_env` support is not added here. I'll do it in a follow-up PR.

r? rust-lang/compiler
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Dec 12, 2023
Implement `--env` compiler flag (without `tracked_env` support)

Part of rust-lang/rust#80792.
Implementation of rust-lang/compiler-team#653.
Not an implementation of rust-lang/rfcs#2794.

It adds the `--env` compiler flag option which allows to set environment values used by `env!` and `option_env!`.

Important to note: When trying to retrieve an environment variable value, it will first look into the ones defined with `--env`, and if there isn't one, then only it will look into the environment variables. So if you use `--env PATH=a`, then `env!("PATH")` will return `"a"` and not the actual `PATH` value.

As mentioned in the title, `tracked_env` support is not added here. I'll do it in a follow-up PR.

r? rust-lang/compiler
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Dec 16, 2023
…Nilstrieb

Add support for `--env` on `tracked_env::var`

Follow-up of rust-lang#118368.
Part of Part of rust-lang#80792.

It adds support of the `--env` option for proc-macros through `tracked_env::var`.

r? `@Nilstrieb`
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Dec 16, 2023
…Nilstrieb

Add support for `--env` on `tracked_env::var`

Follow-up of rust-lang#118368.
Part of Part of rust-lang#80792.

It adds support of the `--env` option for proc-macros through `tracked_env::var`.

r? ``@Nilstrieb``
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 17, 2023
…lstrieb

Add support for `--env` on `tracked_env::var`

Follow-up of rust-lang#118368.
Part of Part of rust-lang#80792.

It adds support of the `--env` option for proc-macros through `tracked_env::var`.

r? `@Nilstrieb`
lnicola pushed a commit to lnicola/rust-analyzer that referenced this issue Dec 18, 2023
Add support for `--env` on `tracked_env::var`

Follow-up of rust-lang/rust#118368.
Part of Part of rust-lang/rust#80792.

It adds support of the `--env` option for proc-macros through `tracked_env::var`.

r? `@Nilstrieb`
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Dec 19, 2023
Add support for `--env` on `tracked_env::var`

Follow-up of rust-lang/rust#118368.
Part of Part of rust-lang/rust#80792.

It adds support of the `--env` option for proc-macros through `tracked_env::var`.

r? `@Nilstrieb`
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Dec 20, 2023
Update documentation for `--env` compilation flag

Part of rust-lang#80792.
As mentioned in rust-lang#118830.

It adds a mention to `tracked_env::var` and also clarifies what triggers a new compilation.

r? `@Nilstrieb`
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 20, 2023
Rollup merge of rust-lang#119115 - GuillaumeGomez:env-docs, r=Nilstrieb

Update documentation for `--env` compilation flag

Part of rust-lang#80792.
As mentioned in rust-lang#118830.

It adds a mention to `tracked_env::var` and also clarifies what triggers a new compilation.

r? `@Nilstrieb`
@GuillaumeGomez
Copy link
Member

The option is now available in nightly.

lnicola pushed a commit to lnicola/rust-analyzer that referenced this issue Apr 7, 2024
Implement `--env` compiler flag (without `tracked_env` support)

Part of rust-lang/rust#80792.
Implementation of rust-lang/compiler-team#653.
Not an implementation of rust-lang/rfcs#2794.

It adds the `--env` compiler flag option which allows to set environment values used by `env!` and `option_env!`.

Important to note: When trying to retrieve an environment variable value, it will first look into the ones defined with `--env`, and if there isn't one, then only it will look into the environment variables. So if you use `--env PATH=a`, then `env!("PATH")` will return `"a"` and not the actual `PATH` value.

As mentioned in the title, `tracked_env` support is not added here. I'll do it in a follow-up PR.

r? rust-lang/compiler
lnicola pushed a commit to lnicola/rust-analyzer that referenced this issue Apr 7, 2024
Add support for `--env` on `tracked_env::var`

Follow-up of rust-lang/rust#118368.
Part of Part of rust-lang/rust#80792.

It adds support of the `--env` option for proc-macros through `tracked_env::var`.

r? `@Nilstrieb`
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this issue Apr 27, 2024
Implement `--env` compiler flag (without `tracked_env` support)

Part of rust-lang/rust#80792.
Implementation of rust-lang/compiler-team#653.
Not an implementation of rust-lang/rfcs#2794.

It adds the `--env` compiler flag option which allows to set environment values used by `env!` and `option_env!`.

Important to note: When trying to retrieve an environment variable value, it will first look into the ones defined with `--env`, and if there isn't one, then only it will look into the environment variables. So if you use `--env PATH=a`, then `env!("PATH")` will return `"a"` and not the actual `PATH` value.

As mentioned in the title, `tracked_env` support is not added here. I'll do it in a follow-up PR.

r? rust-lang/compiler
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this issue Apr 27, 2024
Add support for `--env` on `tracked_env::var`

Follow-up of rust-lang/rust#118368.
Part of Part of rust-lang/rust#80792.

It adds support of the `--env` option for proc-macros through `tracked_env::var`.

r? `@Nilstrieb`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
4 participants