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

buildRustCrate: set CARGO_FEATURE_* when running the build script #90193

Merged
merged 1 commit into from Jun 16, 2020

Conversation

danieldk
Copy link
Contributor

Motivation for this change

Cargo sets CARGO_FEATURE_* for all features when running a build
script:

https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts

Some crates have build scripts (e.g. openblas-src) that rely on the
feature variables being properly set.

Since we now need several representations of features, this change
also updates createFeatures to be a list of features, rather than
rustc feature arguments. configureCrate and buildCrate then
build the required representations as-needed.

Fixes #68978

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.

Cargo sets `CARGO_FEATURE_*` for all features when running a build
script:

https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts

Some crates have build scripts (e.g. openblas-src) that rely on the
feature variables being properly set.

Since we now need several representations of features, this change
also updates `createFeatures` to be a list of features, rather than
`rustc` feature arguments. `configureCrate` and `buildCrate` then
build the required representations as-needed.

Fixes NixOS#68978
# Features should be set as environment variable for build scripts:
# https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
for feature in ${envFeatures}; do
export CARGO_FEATURE_$feature=1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are features allowed to contain whitespaces? Is this the sanest way to set them?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there other possible values?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are features allowed to contain whitespaces?

Yes, they are, though I don’t believe anybody actually uses features with spaces in them. Still a worthwhile corner-case to handle.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@danieldk danieldk Jun 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they are, though I don’t believe anybody actually uses features with spaces in them. Still a worthwhile corner-case to handle.

I didn't treat any other cases, since Cargo does not do anything besides uppercasing and replacing dashes either:

https://github.com/rust-lang/cargo/blob/ee417cbc2fea5c63bb32f91af53e64a3ccbb381e/src/cargo/core/compiler/mod.rs#L1098

(Unless the features are pre-processed somewhere else.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, Cargo will set the environment variable with a space in it. So I guess we'll have to do that too.

Copy link
Contributor Author

@danieldk danieldk Jun 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently features with spaces seem to fail in other places. Once a feature contains a space, it is also incorrectly passed to rustc and already fails when compiling the build script.

I guess supporting features with spaces would be better to handle in a separate PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree. Is anyone going to do that? ;)

Copy link
Contributor Author

@danieldk danieldk Jun 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have created an issue #90605 . I'll try to have a look at it one of the next few days.

@andir
Copy link
Member

andir commented Jun 16, 2020

@ofborg build buildRustCrateTests

@andir andir merged commit d282a7e into NixOS:master Jun 16, 2020
@danieldk danieldk deleted the build-rs-feature-env branch August 17, 2020 06:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

buildRustCrate does note set CARGO_FEATURE_<name>
3 participants