Merge pull request #268619 from tweag/lib-descriptions
[NixPkgs.git] / pkgs / development / compilers / rust / cargo.nix
blob8eb628aa536b7a0dd375855c1afadc2644fd6347
1 { lib, stdenv, pkgsBuildHost, pkgsHostHost
2 , file, curl, pkg-config, python3, openssl, cmake, zlib
3 , installShellFiles, makeWrapper, rustPlatform, rustc
4 , CoreFoundation, Security
5 , auditable ? !cargo-auditable.meta.broken
6 , cargo-auditable
7 , pkgsBuildBuild
8 }:
10 rustPlatform.buildRustPackage.override {
11   cargo-auditable = cargo-auditable.bootstrap;
12 } ({
13   pname = "cargo";
14   inherit (rustc) version src;
16   # the rust source tarball already has all the dependencies vendored, no need to fetch them again
17   cargoVendorDir = "vendor";
18   buildAndTestSubdir = "src/tools/cargo";
20   inherit auditable;
22   passthru = {
23     rustc = rustc;
24     inherit (rustc) tests;
25   };
27   # Upstream rustc still assumes that musl = static[1].  The fix for
28   # this is to disable crt-static by default for non-static musl
29   # targets.
30   #
31   # For every package apart from Cargo, we can fix this by just
32   # patching rustc to not have crt-static by default.  But Cargo is
33   # built with the upstream bootstrap binary for rustc, which we can't
34   # easily patch.  This means we need to find another way to make sure
35   # crt-static is not used during the build of pkgsMusl.cargo.
36   #
37   # By default, Cargo doesn't apply RUSTFLAGS when building build.rs
38   # if --target is passed, so the only good way to set -crt-static for
39   # build.rs files used in the Cargo build is to use the unstable
40   # -Zhost-config Cargo feature.  This allows us to specify flags that
41   # should be passed to rustc when building for the build platform.
42   # We also need to use -Ztarget-applies-to-host, because using
43   # -Zhost-config requires it.
44   #
45   # When doing this, we also have to specify the linker, or cargo
46   # won't pass a -C linker= argument to rustc.  This will make rustc
47   # try to use its default value of "cc", which won't be available
48   # when cross-compiling.
49   #
50   # [1]: https://github.com/rust-lang/compiler-team/issues/422
51   postPatch = lib.optionalString (with stdenv.buildPlatform; isMusl && !isStatic) ''
52     mkdir -p .cargo
53     cat <<EOF >> .cargo/config
54     [host]
55     rustflags = "-C target-feature=-crt-static"
56     linker = "${pkgsBuildHost.stdenv.cc}/bin/${pkgsBuildHost.stdenv.cc.targetPrefix}cc"
57     [unstable]
58     host-config = true
59     target-applies-to-host = true
60     EOF
61   '';
63   # changes hash of vendor directory otherwise
64   dontUpdateAutotoolsGnuConfigScripts = true;
66   nativeBuildInputs = [
67     pkg-config cmake installShellFiles makeWrapper
68     (lib.getDev pkgsHostHost.curl)
69     zlib
70   ];
71   buildInputs = [ file curl python3 openssl zlib ]
72     ++ lib.optionals stdenv.isDarwin [ CoreFoundation Security ];
74   # cargo uses git-rs which is made for a version of libgit2 from recent master that
75   # is not compatible with the current version in nixpkgs.
76   #LIBGIT2_SYS_USE_PKG_CONFIG = 1;
78   # fixes: the cargo feature `edition` requires a nightly version of Cargo, but this is the `stable` channel
79   RUSTC_BOOTSTRAP = 1;
81   postInstall = ''
82     wrapProgram "$out/bin/cargo" --suffix PATH : "${rustc}/bin"
84     installManPage src/tools/cargo/src/etc/man/*
86     installShellCompletion --bash --name cargo \
87       src/tools/cargo/src/etc/cargo.bashcomp.sh
89     installShellCompletion --zsh src/tools/cargo/src/etc/_cargo
90   '';
92   checkPhase = ''
93     # Disable cross compilation tests
94     export CFG_DISABLE_CROSS_TESTS=1
95     cargo test
96   '';
98   # Disable check phase as there are failures (4 tests fail)
99   doCheck = false;
101   doInstallCheck = !stdenv.hostPlatform.isStatic &&
102     stdenv.hostPlatform.parsed.kernel.execFormat == lib.systems.parse.execFormats.elf;
103   installCheckPhase = ''
104     runHook preInstallCheck
105     readelf -a $out/bin/.cargo-wrapped | grep -F 'Shared library: [libcurl.so'
106     runHook postInstallCheck
107   '';
109   meta = with lib; {
110     homepage = "https://crates.io";
111     description = "Downloads your Rust project's dependencies and builds your project";
112     maintainers = teams.rust.members;
113     license = [ licenses.mit licenses.asl20 ];
114     platforms = platforms.unix;
115     # https://github.com/alexcrichton/nghttp2-rs/issues/2
116     broken = stdenv.hostPlatform.isx86 && stdenv.buildPlatform != stdenv.hostPlatform;
117   };
119 // lib.optionalAttrs (stdenv.buildPlatform.rust.rustcTarget != stdenv.hostPlatform.rust.rustcTarget) {
120   HOST_PKG_CONFIG_PATH="${pkgsBuildBuild.pkg-config}/bin/pkg-config";