forked from cargo2nix/cargo2nix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkcrate.nix
331 lines (297 loc) · 12 KB
/
mkcrate.nix
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
{
rustToolchain,
lib,
pkgs,
buildPackages,
rustLib,
stdenv,
}:
{
release, # Compiling in release mode?
name,
version,
registry,
src,
features ? [ ],
dependencies ? { },
devDependencies ? { },
buildDependencies ? { },
compileMode ? "build",
profile,
profileOpts ? null,
codegenOpts ? null,
meta ? { },
cargoUnstableFlags ? [ ],
rustcLinkFlags ? [ ],
rustcBuildFlags ? [ ],
target ? null,
hostPlatformCpu ? null,
hostPlatformFeatures ? [],
NIX_DEBUG ? 0,
}:
with builtins; with lib;
let
inherit (rustLib) rustTriple decideProfile;
wrapper = rustpkg: pkgs.writeScriptBin rustpkg ''
#!${stdenv.shell}
. ${./mkcrate-utils.sh}
isBuildScript=
args=("$@")
for i in "''${!args[@]}"; do
if [ "xmetadata=" = "x''${args[$i]::9}" ]; then
args[$i]=metadata=$NIX_RUST_METADATA
elif [ "x--crate-name" = "x''${args[$i]}" ] && [ "xbuild_script_" = "x''${args[$i+1]::13}" ]; then
isBuildScript=1
fi
done
if [ "$isBuildScript" ]; then
args+=($NIX_RUST_BUILD_FLAGS)
else
args+=($NIX_RUST_LINK_FLAGS)
fi
touch invoke.log
echo "''${args[@]}" >>invoke.log
exec ${rustToolchain}/bin/${rustpkg} "''${args[@]}"
'';
ccForBuild="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}cc";
cxxForBuild="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}c++";
targetPrefix = stdenv.cc.targetPrefix;
cc = stdenv.cc;
ccForHost="${cc}/bin/${targetPrefix}cc";
cxxForHost="${cc}/bin/${targetPrefix}c++";
rustBuildTriple = rustTriple stdenv.buildPlatform;
rustHostTriple = if (target != null) then target else rustTriple stdenv.hostPlatform;
depMapToList = deps:
flatten
(sort (a: b: elemAt a 0 < elemAt b 0)
(mapAttrsToList (name: value: [ name "${value}" ]) deps));
buildCmd =
let
hasDefaultFeature = elem "default" features;
featuresWithoutDefault = if hasDefaultFeature
then filter (feature: feature != "default") features
else features;
buildMode = {
"test" = "--tests";
"bench" = "--benches";
}.${compileMode} or "";
featuresArg = if featuresWithoutDefault == [ ]
then ""
else "--features ${concatStringsSep "," featuresWithoutDefault}";
in
if compileMode != "doctest" then ''
${rustToolchain}/bin/cargo build $CARGO_VERBOSE ${optionalString release "--release"} --target ${rustHostTriple} ${buildMode} \
${featuresArg} ${optionalString (!hasDefaultFeature) "--no-default-features"} \
${optionalString (builtins.length cargoUnstableFlags > 0) "-Z ${lib.strings.concatStringsSep "," cargoUnstableFlags}"} \
--message-format json-diagnostic-rendered-ansi | tee .cargo-build-output \
1> >(jq 'select(.message != null) .message.rendered' -r)
''
# Note: Doctest doesn't yet support no-run https://github.com/rust-lang/rust/pull/83857
# So instead of persiting the binaries with
# RUSTDOCFLAGS="-Zunstable-options --persist-doctests $(pwd)/target/rustdoctest -o $(pwd)/target/rustdoctest" cargo test --doc | tee .cargo-doctest-output
# we just introduce a new compile mode
#
# We also filter -l linkage flags, as rustdoc doesn't support them
#
# And _also_ detect if there are no lib crates, in which case skip, because thats an error for rustdoc
#
# This does not abort on failure. The output should be inspected for failures
else ''
echo "Performing Doctests"
export NIX_RUST_LINK_FLAGS=$(echo "$NIX_RUST_LINK_FLAGS" | sed 's/ \-l \w*//g')
${rustToolchain}/bin/cargo read-manifest | jq -e '.targets | .[] | select(.crate_types[] | contains ("lib")) | any' >/dev/null && \
( ${rustToolchain}/bin/cargo test --doc --no-fail-fast \
${featuresArg} ${optionalString (!hasDefaultFeature) "--no-default-features"} \
-- -Z unstable-options --format json \
| tee results.json \
|| true) \
|| echo "No lib crate detected"
'';
inherit
(({ right, wrong }: { runtimeDependencies = right; buildtimeDependencies = wrong; })
(partition (drv: drv.stdenv.hostPlatform == stdenv.hostPlatform)
(concatLists [
(attrValues dependencies)
(optionals (compileMode != "build") (attrValues devDependencies))
(attrValues buildDependencies)
])))
runtimeDependencies buildtimeDependencies;
drvAttrs = {
inherit src version meta NIX_DEBUG;
name = "crate-${name}-${version}${optionalString (compileMode != "build") "-${compileMode}"}";
# Adding libiconv is a convenience hack. It really isn't needed by every
# derivation and should instead be added / propagated where appropriate, but
# until someone decides to investigate the actual dependencies, it remains
# here instead of in overrides.
buildInputs = runtimeDependencies ++ lib.optionals stdenv.hostPlatform.isDarwin [ pkgs.libiconv ];
propagatedBuildInputs = lib.unique (concatMap (drv: drv.propagatedBuildInputs) runtimeDependencies);
nativeBuildInputs = [ rustToolchain ] ++ buildtimeDependencies;
depsBuildBuild =
let inherit (buildPackages.buildPackages) stdenv jq remarshal;
in [ stdenv.cc jq remarshal ];
# Running the default `strip -S` command on Darwin corrupts the
# .rlib files in "lib/".
#
# See https://github.com/NixOS/nixpkgs/pull/34227
stripDebugList = if stdenv.isDarwin then [ "bin" ] else null;
passthru = {
inherit
name
version
registry
dependencies
devDependencies
buildDependencies
features;
shell = pkgs.mkShell (removeAttrs drvAttrs ["src"]);
};
dependencies = depMapToList dependencies;
buildDependencies = depMapToList buildDependencies;
devDependencies = depMapToList (optionalAttrs (compileMode != "build") devDependencies);
extraRustcLinkFlags =
optionals (hostPlatformCpu != null) ([("-Ctarget-cpu=" + hostPlatformCpu)]) ++
optionals (hostPlatformFeatures != []) [("-Ctarget-feature=" + (concatMapStringsSep "," (feature: "+" + feature) hostPlatformFeatures))] ++
rustcLinkFlags;
extraRustcBuildFlags = rustcBuildFlags;
configureCargo = ''
mkdir -p .cargo
cat > .cargo/config <<'EOF'
[target."${rustBuildTriple}"]
linker = "${ccForBuild}"
'' + optionalString (codegenOpts != null && codegenOpts ? "${rustBuildTriple}") (''
rustflags = [
'' + concatStringsSep ", " (concatMap (opt: [''"-C"'' ''"${opt}"'']) codegenOpts."${rustBuildTriple}") + "\n" + ''
]
# HACK: 2019-08-01: wasm32-wasi always uses `wasm-ld`
# HACK: 2021-12-29: x86_64-fortanix-unknown-sgx always use `ld`
'') + optionalString (rustBuildTriple != rustHostTriple && rustHostTriple != "wasm32-wasi" && rustHostTriple != "wasm32-unknown-unknown" && rustHostTriple != "x86_64-fortanix-unknown-sgx") (''
[target."${rustHostTriple}"]
linker = "${ccForHost}"
''+ optionalString (codegenOpts != null && codegenOpts ? "${rustHostTriple}") (''
rustflags = [
'' + concatStringsSep ", " (concatMap (opt: [''"-C"'' ''"${opt}"'']) codegenOpts."${rustHostTriple}") + "\n" + ''
]
'')) + optionalString (profileOpts != null && profileOpts."${decideProfile compileMode release}" != null) (''
[profile.${decideProfile compileMode release}]
'' + concatStringsSep "\n" (mapAttrsToList (n: a: "${n} = " + (
if isInt a then "${toString a}"
else (if isBool a then (if a then "true" else "false")
else ''"${a}"'')))
(profileOpts."${decideProfile compileMode release}"))
) + "\n" + ''
EOF
'';
configurePhase = ''
runHook preConfigure
runHook configureCargo
runHook postConfigure
'';
manifestPatch = toJSON {
features = genAttrs features (_: [ ]);
profile.${ decideProfile compileMode release } = profile;
};
overrideCargoManifest = ''
. ${./mkcrate-utils.sh}
# Synthesize a lock file
echo "[[package]]" > Cargo.lock
echo name = \"${name}\" >> Cargo.lock
echo version = \"${version}\" >> Cargo.lock
registry="${registry}"
if [ "$registry" != "unknown" ]; then
echo source = \"registry+''${registry}\" >> Cargo.lock
fi
manifest_path=$(cargoRelativeManifest ${name})
manifest_dir=''${manifest_path%Cargo.toml}
# Rewrite the crate's toml
if [ -n "$manifest_dir" ]; then pushd $manifest_dir; fi
mv Cargo.toml Cargo.original.toml
sanitizeTomlForRemarshal Cargo.original.toml
reducePackageToml Cargo.original.toml Cargo.toml "$manifestPatch"
if [ -n "$manifest_dir" ]; then popd; fi
# If the crate is a workspace, reduce it to a crate of just a workspace of a single crate
if [ $manifest_path != "Cargo.toml" ]; then
mv Cargo.toml Cargo.workspace.toml
sanitizeTomlForRemarshal Cargo.workspace.toml
reduceWorkspaceToml Cargo.workspace.toml Cargo.toml "$manifest_dir"
fi
'';
setBuildEnv = ''
CARGO_BUILD_INCREMENTAL=0 #builds inside nix sandbox use nix caching, not cargo target caching
MINOR_RUSTC_VERSION="$(${rustToolchain}/bin/rustc --version | cut -d . -f 2)"
if (( MINOR_RUSTC_VERSION < 41 )); then
isProcMacro="$(
remarshal -if toml -of json "''${manifest_dir}Cargo.original.toml" \
| jq -r 'if .lib."proc-macro" or .lib."proc_macro" then "1" else "" end' \
)"
fi
crateName="$(
remarshal -if toml -of json "''${manifest_dir}Cargo.original.toml" \
| jq -r 'if .lib."name" then .lib."name" else "${replaceStrings ["-"] ["_"] name}" end' \
)"
. ${./mkcrate-utils.sh}
export CARGO_VERBOSE=`cargoVerbosityLevel $NIX_DEBUG`
export NIX_RUST_METADATA=`extractHash $out`
export CARGO_HOME=`pwd`/.cargo
mkdir -p deps build_deps
linkFlags=(`makeExternCrateFlags $dependencies $devDependencies | sort -u`)
buildLinkFlags=(`makeExternCrateFlags $buildDependencies | sort -u`)
linkExternCrateToDeps `realpath deps` $dependencies $devDependencies
linkExternCrateToDeps `realpath build_deps` $buildDependencies
export NIX_RUST_LINK_FLAGS="''${linkFlags[@]} -L dependency=$(realpath deps) $extraRustcLinkFlags"
export NIX_RUST_BUILD_FLAGS="''${buildLinkFlags[@]} -L dependency=$(realpath build_deps) $extraRustcBuildFlags"
export RUSTC=${wrapper "rustc"}/bin/rustc
export CLIPPY_DRIVER=${wrapper "clippy-driver"}/bin/clippy-driver
export RUSTDOC=${wrapper "rustdoc"}/bin/rustdoc
readarray -t depKeys <<< `loadDepKeys $dependencies`
if (( NIX_DEBUG >= 1 )); then
echo $NIX_RUST_LINK_FLAGS
echo $NIX_RUST_BUILD_FLAGS
for key in ''${depKeys[@]}; do
echo $key
done
fi
'';
runCargo = ''
(
set -euo pipefail
if (( NIX_DEBUG >= 1 )); then
set -x
fi
env \
"CC_${stdenv.buildPlatform.config}"="${ccForBuild}" \
"CXX_${stdenv.buildPlatform.config}"="${cxxForBuild}" \
"CC_${rustHostTriple}"="${ccForHost}" \
"CXX_${rustHostTriple}"="${cxxForHost}" \
''${depKeys:+"''${depKeys[@]}"} \
${buildCmd}
)
'';
buildPhase = ''
runHook preBuild
runHook overrideCargoManifest
runHook setBuildEnv
runHook runCargo
runHook postBuild
'';
outputs = ["bin" "out"];
installPhase = ''
runHook preInstall
'' + (if compileMode != "doctest" then ''
mkdir -p $out/lib
cargo_links="$(remarshal -if toml -of json "''${manifest_dir}Cargo.original.toml" | jq -r '.package.links | select(. != null)')"
if (( MINOR_RUSTC_VERSION < 41 )); then
install_crate ${rustHostTriple} ${if release then "release" else "debug"}
else
install_crate2 ${rustHostTriple}
fi
'' else ''
mkdir -p $out/share
mkdir -p $bin
touch results.json
mv results.json $out/share/
'') + ''
runHook postInstall
'';
};
in
stdenv.mkDerivation drvAttrs