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

Rust build-support: fixing a compilation error in some crates #50452

Merged
merged 3 commits into from Dec 3, 2018
Merged

Rust build-support: fixing a compilation error in some crates #50452

merged 3 commits into from Dec 3, 2018

Conversation

P-E-Meunier
Copy link
Contributor

BuildRustCrate fails on some crates (such as proc-macro2), because some environment variables were not set. This fixes it.

Additionally, an error message related to the install script is fixed.

Motivation for this change
Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS)
  • 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 nox --run "nox-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)
  • Fits CONTRIBUTING.md.

@dtzWill
Copy link
Member

dtzWill commented Nov 16, 2018

What's the motivation for NUM_JOBS=1? Does that impact parallel building?

@andir
Copy link
Member

andir commented Nov 16, 2018

This is the definition of NUM_CPUS:

* `NUM_JOBS` - the parallelism specified as the top-level parallelism. This can
               be useful to pass a `-j` parameter to a system like `make`. Note
               that care should be taken when interpreting this environment
               variable. For historical purposes this is still provided but
               recent versions of Cargo, for example, do not need to run `make
               -j` as it'll automatically happen. Cargo implements its own
               [jobserver] and will allow build scripts to inherit this
               information, so programs compatible with GNU make jobservers will
               already have appropriately configured parallelism.

And the RUSTC case:

* `RUSTC`, `RUSTDOC` - the compiler and documentation generator that Cargo has
                       resolved to use, passed to the build script so it might
                       use it as well.

So I think the addition is warranted. We might as well go for the full list of them as it can be found in the cargo search at src/doc/src/reference/environment-variables.md.

Should we also add test cases so that we can ensure we are properly compiling something that actually depends on those?

Copy link
Member

@andir andir left a comment

Choose a reason for hiding this comment

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

See the inline comments and #50452 (comment).

@@ -71,6 +71,8 @@ in ''
export CARGO_PKG_VERSION_MAJOR=${builtins.elemAt version 0}
export CARGO_PKG_VERSION_MINOR=${builtins.elemAt version 1}
export CARGO_PKG_VERSION_PATCH=${builtins.elemAt version 2}
export NUM_JOBS=1
export RUSTC="rustc"
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we set this to the absolute path maybe? I am not sure what the exactly expectation is in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it matters, as I believe this is meant to be argv[0] when rustc is called, and since we call rustc directly everywhere else, I believe this is what is expected in most build scripts.
Since we have rustc in the builtInputs, rustc is in the path, I think we're fine.

Copy link
Member

Choose a reason for hiding this comment

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

I remember rustc to do a realpath on /proc/self/exe at some point, when trying to cheat with our rustup patch.

@@ -71,6 +71,8 @@ in ''
export CARGO_PKG_VERSION_MAJOR=${builtins.elemAt version 0}
export CARGO_PKG_VERSION_MINOR=${builtins.elemAt version 1}
export CARGO_PKG_VERSION_PATCH=${builtins.elemAt version 2}
export NUM_JOBS=1
Copy link
Member

Choose a reason for hiding this comment

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

According to the docs this should not be required in the future. Does this imply that newer code will just ignore it an use the proper jobserver implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know, it's still in the docs. I was just trying to include as many environment variables from the docs as possible.

@P-E-Meunier
Copy link
Contributor Author

What's the motivation for NUM_JOBS=1? Does that impact parallel building?

Rustc is already called with -j1 by buildRustCrate, so this environment variable will not change anything.
When used with cargo, this environment variable means cargo will compile multiple modules of the same crate in parallel. Nix adopts a different strategy, by parallelising only at the crate level (doing otherwise doesn't work).

@dtzWill
Copy link
Member

dtzWill commented Nov 16, 2018 via email

@P-E-Meunier
Copy link
Contributor Author

So I think the addition is warranted. We might as well go for the full list of them as it can be found in the cargo search at src/doc/src/reference/environment-variables.md.

Right.

Should we also add test cases so that we can ensure we are properly compiling something that actually depends on those?

I don't know how to add test cases in Nix, but I'd be happy to learn.

@thoughtpolice
Copy link
Member

I just ran into this failure myself when using proc_macro2 as well; here was my patch, after I reverse-engineered the failure back to RUSTC:

diff --git a/pkgs/build-support/rust/build-rust-crate/configure-crate.nix b/pkgs/build-support/rust/build-rust-crate/configure-crate.nix
index 37fef2abd77..0be77fb4c0a 100644
--- a/pkgs/build-support/rust/build-rust-crate/configure-crate.nix
+++ b/pkgs/build-support/rust/build-rust-crate/configure-crate.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, echo_build_heading, noisily, makeDeps }:
+{ lib, stdenv, which, echo_build_heading, noisily, makeDeps }:
 { build, buildDependencies, colors, completeBuildDeps, completeDeps, crateAuthors, crateFeatures, crateName, crateVersion, extraLinkFlags, libName, libPath, release, target_os, verbose, workspace_member }:
 let version_ = lib.splitString "-" crateVersion;
     versionPre = if lib.tail version_ == [] then "" else builtins.elemAt version_ 1;
@@ -61,6 +61,8 @@ in ''
   export CARGO_CFG_TARGET_POINTER_WIDTH=${toString stdenv.hostPlatform.parsed.cpu.bits}
   export CARGO_CFG_TARGET_VENDOR=${stdenv.hostPlatform.parsed.vendor.name}

+  export RUSTC=$(${which}/bin/which rustc)
+  export RUSTDOC=$(${which}/bin/which rustdoc)
   export CARGO_MANIFEST_DIR="."
   export DEBUG="${toString (!release)}"
   export OPT_LEVEL="${toString optLevel}"
diff --git a/pkgs/build-support/rust/build-rust-crate/default.nix b/pkgs/build-support/rust/build-rust-crate/default.nix
index ec11472bbae..ac790b0b62f 100644
--- a/pkgs/build-support/rust/build-rust-crate/default.nix
+++ b/pkgs/build-support/rust/build-rust-crate/default.nix
@@ -4,7 +4,7 @@
 # This can be useful for deploying packages with NixOps, and to share
 # binary dependencies between projects.

-{ lib, stdenv, defaultCrateOverrides, fetchCrate, rustc }:
+{ lib, stdenv, which, defaultCrateOverrides, fetchCrate, rustc }:

 let
     # This doesn't appear to be officially documented anywhere yet.
@@ -53,7 +53,7 @@ let
       }
     '';

-    configureCrate = import ./configure-crate.nix { inherit lib stdenv echo_build_heading noisily makeDeps; };
+    configureCrate = import ./configure-crate.nix { inherit lib stdenv which echo_build_heading noisily makeDeps; };
     buildCrate = import ./build-crate.nix { inherit lib stdenv echo_build_heading noisily makeDeps; };
     installCrate = import ./install-crate.nix;

i.e. I set the absolute path.

I think not setting the absolute path is also just fine.

In general a test case would be nice, though, but I don't think we should hold this up for that? proc-macro2 is used quite widely through some indirect dependencies, so I think this is generally OK, and overall this change is relatively minor. I'd like to see this land soon (so I can eliminate some in-tree patches)

@andir
Copy link
Member

andir commented Nov 18, 2018

@P-E-Meunier The tests are currently in pkgs/build-support/rust/build-rust-crate/test.

Other then that we can go ahead and merge it.

@grahamc
Copy link
Member

grahamc commented Dec 1, 2018

Looks like a compilation error in some crates might include thread 'main' panicked at 'Failed to get rustc version', libcore/option.rs:1000:5 in libc.

@Ralith
Copy link
Contributor

Ralith commented Dec 1, 2018

Shouldn't this also set RUSTDOC?

@P-E-Meunier
Copy link
Contributor Author

I'm currently tweaking this to try and compile Rust to WASM. This would be very cool, but doesn't work yet. When called with Cargo, Rustc generates perfect WASM. When called with Nix, Rustc does generate WASM, but it is not usable.

@grahamc
Copy link
Member

grahamc commented Dec 1, 2018 via email

@thoughtpolice
Copy link
Member

I think this patch should work nicely and honestly I'm willing to go ahead with this, without a test, since it's been waiting for a little bit.

I think RUSTDOC should also be set, too, as it was in my original patch (otherwise this will just happen again)

@P-E-Meunier
Copy link
Contributor Author

No problem! Since I've used this commit to deploy stuff in production, I sort of messed up my branches locally to make this feasible, and merging would spare me a morning of rebasing, so yes please!

@grahamc grahamc merged commit fc459de into NixOS:master Dec 3, 2018
thoughtpolice added a commit to thoughtpolice/nixpkgs that referenced this pull request Dec 5, 2018
See NixOS#50452 (comment)

Signed-off-by: Austin Seipp <aseipp@pobox.com>
thoughtpolice added a commit that referenced this pull request Dec 5, 2018
See #50452 (comment)

Signed-off-by: Austin Seipp <aseipp@pobox.com>
danieldk added a commit to danieldk/nixpkgs that referenced this pull request Dec 3, 2020
Bofore this change, NUM_JOBS was set to 1. Some crates for building
C/C++ code (e.g. the cc and cmake crates), rely on this variable to
set the number of jobs. As a consequence, we were compiling embedded
libraries serially. Change this to NIX_BUILD_CORES to permit parallel
builds.

Prior discussion:

NixOS#50452 (comment)
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.

None yet

8 participants