about summary refs log tree commit diff
path: root/nixpkgs/pkgs/build-support/rust/default.nix
blob: cf8042ba255d9f3a0d722de70bb7d66ae9ac60e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
{ stdenv, cacert, git, cargo, rustc, fetchcargo, buildPackages, windows }:

let
  buildRustPackage =
    { name ? "${args.pname}-${args.version}"
    , cargoSha256 ? "unset"
    , src ? null
    , srcs ? null
    , cargoPatches ? []
    , patches ? []
    , sourceRoot ? null
    , logLevel ? ""
    , buildInputs ? []
    , nativeBuildInputs ? []
    , cargoUpdateHook ? ""
    , cargoDepsHook ? ""
    , cargoBuildFlags ? []
    , buildType ? "release"
    , meta ? {}

    , cargoVendorDir ? null
    , ... } @ args:

    assert cargoVendorDir == null -> cargoSha256 != "unset";
    assert buildType == "release" || buildType == "debug";

    let
      cargoDeps = if cargoVendorDir == null
        then fetchcargo {
            inherit name src srcs sourceRoot cargoUpdateHook;
            patches = cargoPatches;
            sha256 = cargoSha256;
          }
        else null;

      setupVendorDir = if cargoVendorDir == null
        then ''
          unpackFile "$cargoDeps"
          cargoDepsCopy=$(stripHash $(basename $cargoDeps))
          chmod -R +w "$cargoDepsCopy"
        ''
        else ''
          cargoDepsCopy="$sourceRoot/${cargoVendorDir}"
        '';

      hostConfig = stdenv.hostPlatform.config;

      rustHostConfig = {
        "x86_64-pc-mingw32" = "x86_64-pc-windows-gnu";
      }."${hostConfig}" or hostConfig;

      ccForBuild="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}cc";
      cxxForBuild="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}c++";
      ccForHost="${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
      cxxForHost="${stdenv.cc}/bin/${stdenv.cc.targetPrefix}c++";
      releaseDir = "target/${rustHostConfig}/${buildType}";
    in

    stdenv.mkDerivation (args // {
      inherit cargoDeps;

      patchRegistryDeps = ./patch-registry-deps;

      nativeBuildInputs = nativeBuildInputs ++ [ cacert git cargo rustc ];
      buildInputs = buildInputs ++ stdenv.lib.optional stdenv.hostPlatform.isMinGW windows.pthreads;

      patches = cargoPatches ++ patches;

      PKG_CONFIG_ALLOW_CROSS =
        if stdenv.buildPlatform != stdenv.hostPlatform then 1 else 0;

      postUnpack = ''
        eval "$cargoDepsHook"

        ${setupVendorDir}

        mkdir .cargo
        config="$(pwd)/$cargoDepsCopy/.cargo/config";
        if [[ ! -e $config ]]; then
          config=${./fetchcargo-default-config.toml};
        fi;
        substitute $config .cargo/config \
          --subst-var-by vendor "$(pwd)/$cargoDepsCopy"

        cat >> .cargo/config <<'EOF'
        [target."${stdenv.buildPlatform.config}"]
        "linker" = "${ccForBuild}"
        ${stdenv.lib.optionalString (stdenv.buildPlatform.config != stdenv.hostPlatform.config) ''
        [target."${rustHostConfig}"]
        "linker" = "${ccForHost}"
        ''}
        EOF

        unset cargoDepsCopy
        export RUST_LOG=${logLevel}
      '' + (args.postUnpack or "");

      configurePhase = args.configurePhase or ''
        runHook preConfigure
        runHook postConfigure
      '';

      buildPhase = with builtins; args.buildPhase or ''
        runHook preBuild

        (
        set -x
        env \
          "CC_${stdenv.buildPlatform.config}"="${ccForBuild}" \
          "CXX_${stdenv.buildPlatform.config}"="${cxxForBuild}" \
          "CC_${stdenv.hostPlatform.config}"="${ccForHost}" \
          "CXX_${stdenv.hostPlatform.config}"="${cxxForHost}" \
          cargo build \
            ${stdenv.lib.optionalString (buildType == "release") "--release"} \
            --target ${rustHostConfig} \
            --frozen ${concatStringsSep " " cargoBuildFlags}
        )

        # rename the output dir to a architecture independent one
        mapfile -t targets < <(find "$NIX_BUILD_TOP" -type d | grep '${releaseDir}$')
        for target in "''${targets[@]}"; do
          rm -rf "$target/../../${buildType}"
          ln -srf "$target" "$target/../../"
        done

        runHook postBuild
      '';

      checkPhase = args.checkPhase or ''
        runHook preCheck
        echo "Running cargo test"
        cargo test
        runHook postCheck
      '';

      doCheck = args.doCheck or true;

      inherit releaseDir;

      installPhase = args.installPhase or ''
        runHook preInstall
        mkdir -p $out/bin $out/lib

        find $releaseDir \
          -maxdepth 1 \
          -type f \
          -executable ! \( -regex ".*\.\(so.[0-9.]+\|so\|a\|dylib\)" \) \
          -print0 | xargs -r -0 cp -t $out/bin
        find $releaseDir \
          -maxdepth 1 \
          -regex ".*\.\(so.[0-9.]+\|so\|a\|dylib\)" \
          -print0 | xargs -r -0 cp -t $out/lib
        rmdir --ignore-fail-on-non-empty $out/lib $out/bin
        runHook postInstall
      '';

      passthru = { inherit cargoDeps; } // (args.passthru or {});

      meta = {
        # default to Rust's platforms
        platforms = rustc.meta.platforms;
      } // meta;
    }) // {
      overrideRustAttrs = f: buildRustPackage (args // (f args));
    };
in buildRustPackage