git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #46210 from andir/build-rust-crate-changes

buildRustCrate: add heuristic to picking the right binary source files

commitf83a26291fcef25609796110f0cdd76b7d02942a authorJörg Thalheim <Mic92@users.noreply.github.com> dateSat 15 Sep 2018, 08:31 (8 years ago) parent0bfdab2fc5e595
8 files changed+688−462
pkgs/build-support/rust/build-rust-crate.nix−461 pkgs/build-support/rust/build-rust-crate/build-crate.nix+157 pkgs/build-support/rust/build-rust-crate/configure-crate.nix+129 pkgs/build-support/rust/build-rust-crate/default.nix+173 pkgs/build-support/rust/build-rust-crate/install-crate.nix+28 pkgs/build-support/rust/build-rust-crate/test/brotli-crates.nix+95 pkgs/build-support/rust/build-rust-crate/test/default.nix+104 pkgs/top-level/all-packages.nix+2−1
pkgs/build-support/rust/build-rust-crate.nixdeleted−461
@@ -1,461 +0,0 @@
1−# Code for buildRustCrate, a Nix function that builds Rust code, just
2−# like Cargo, but using Nix instead.
3−#
4−# This can be useful for deploying packages with NixOps, and to share
5−# binary dependencies between projects.
6−
7−{ lib, stdenv, defaultCrateOverrides, fetchCrate, ncurses, rustc }:
8−
9−let makeDeps = dependencies:
10− (lib.concatMapStringsSep " " (dep:
11− let extern = lib.strings.replaceStrings ["-"] ["_"] dep.libName; in
12− (if dep.crateType == "lib" then
13− " --extern ${extern}=${dep.out}/lib/lib${extern}-${dep.metadata}.rlib"
14− else
15− " --extern ${extern}=${dep.out}/lib/lib${extern}-${dep.metadata}${stdenv.hostPlatform.extensions.sharedLibrary}")
16− ) dependencies);
17−
18− # This doesn't appear to be officially documented anywhere yet.
19− # See https://github.com/rust-lang-nursery/rust-forge/issues/101.
20− target_os = if stdenv.hostPlatform.isDarwin
21− then "macos"
22− else stdenv.hostPlatform.parsed.kernel.name;
23−
24− echo_build_heading = colors: ''
25− echo_build_heading() {
26− start=""
27− end=""
28− if [[ "${colors}" == "always" ]]; then
29− start="$(printf '\033[0;1;32m')" #set bold, and set green.
30− end="$(printf '\033[0m')" #returns to "normal"
31− fi
32− if (( $# == 1 )); then
33− echo "$start""Building $1""$end"
34− else
35− echo "$start""Building $1 ($2)""$end"
36− fi
37− }
38− '';
39− noisily = colors: verbose: ''
40− noisily() {
41− start=""
42− end=""
43− if [[ "${colors}" == "always" ]]; then
44− start="$(printf '\033[0;1;32m')" #set bold, and set green.
45− end="$(printf '\033[0m')" #returns to "normal"
46− fi
47− ${lib.optionalString verbose ''
48− echo -n "$start"Running "$end"
49− echo $@
50− ''}
51− $@
52− }
53− '';
54−
55− configureCrate =
56− { crateName, crateVersion, crateAuthors, build, libName, crateFeatures, colors, libPath, release, buildDependencies, completeDeps, completeBuildDeps, verbose, workspace_member, extraLinkFlags }:
57− let version_ = lib.splitString "-" crateVersion;
58− versionPre = if lib.tail version_ == [] then "" else builtins.elemAt version_ 1;
59− version = lib.splitString "." (lib.head version_);
60− rustcOpts = (if release then "-C opt-level=3" else "-C debuginfo=2");
61− buildDeps = makeDeps buildDependencies;
62− authors = lib.concatStringsSep ":" crateAuthors;
63− optLevel = if release then 3 else 0;
64− completeDepsDir = lib.concatStringsSep " " completeDeps;
65− completeBuildDepsDir = lib.concatStringsSep " " completeBuildDeps;
66− in ''
67− cd ${workspace_member}
68− runHook preConfigure
69− ${echo_build_heading colors}
70− ${noisily colors verbose}
71− symlink_dependency() {
72− # $1 is the nix-store path of a dependency
73− # $2 is the target path
74− i=$1
75− ln -s -f $i/lib/*.rlib $2 #*/
76− ln -s -f $i/lib/*.so $i/lib/*.dylib $2 #*/
77− if [ -e "$i/lib/link" ]; then
78− cat $i/lib/link >> target/link
79− cat $i/lib/link >> target/link.final
80− fi
81− if [ -e $i/env ]; then
82− source $i/env
83− fi
84− }
85−
86− mkdir -p target/{deps,lib,build,buildDeps}
87− chmod uga+w target -R
88− echo ${extraLinkFlags} > target/link
89− echo ${extraLinkFlags} > target/link.final
90− for i in ${completeDepsDir}; do
91− symlink_dependency $i target/deps
92− done
93− for i in ${completeBuildDepsDir}; do
94− symlink_dependency $i target/buildDeps
95− done
96− if [[ -e target/link ]]; then
97− sort -u target/link > target/link.sorted
98− mv target/link.sorted target/link
99− sort -u target/link.final > target/link.final.sorted
100− mv target/link.final.sorted target/link.final
101− tr '\n' ' ' < target/link > target/link_
102− fi
103− EXTRA_BUILD=""
104− BUILD_OUT_DIR=""
105− export CARGO_PKG_NAME=${crateName}
106− export CARGO_PKG_VERSION=${crateVersion}
107− export CARGO_PKG_AUTHORS="${authors}"
108−
109− export CARGO_CFG_TARGET_ARCH=${stdenv.hostPlatform.parsed.cpu.name}
110− export CARGO_CFG_TARGET_OS=${target_os}
111− export CARGO_CFG_TARGET_FAMILY="unix"
112− export CARGO_CFG_UNIX=1
113− export CARGO_CFG_TARGET_ENV="gnu"
114− export CARGO_CFG_TARGET_ENDIAN=${if stdenv.hostPlatform.parsed.cpu.significantByte.name == "littleEndian" then "little" else "big"}
115− export CARGO_CFG_TARGET_POINTER_WIDTH=${toString stdenv.hostPlatform.parsed.cpu.bits}
116− export CARGO_CFG_TARGET_VENDOR=${stdenv.hostPlatform.parsed.vendor.name}
117−
118− export CARGO_MANIFEST_DIR="."
119− export DEBUG="${toString (!release)}"
120− export OPT_LEVEL="${toString optLevel}"
121− export TARGET="${stdenv.hostPlatform.config}"
122− export HOST="${stdenv.hostPlatform.config}"
123− export PROFILE=${if release then "release" else "debug"}
124− export OUT_DIR=$(pwd)/target/build/${crateName}.out
125− export CARGO_PKG_VERSION_MAJOR=${builtins.elemAt version 0}
126− export CARGO_PKG_VERSION_MINOR=${builtins.elemAt version 1}
127− export CARGO_PKG_VERSION_PATCH=${builtins.elemAt version 2}
128− if [[ -n "${versionPre}" ]]; then
129− export CARGO_PKG_VERSION_PRE="${versionPre}"
130− fi
131−
132− BUILD=""
133− if [[ ! -z "${build}" ]] ; then
134− BUILD=${build}
135− elif [[ -e "build.rs" ]]; then
136− BUILD="build.rs"
137− fi
138− if [[ ! -z "$BUILD" ]] ; then
139− echo_build_heading "$BUILD" ${libName}
140− mkdir -p target/build/${crateName}
141− EXTRA_BUILD_FLAGS=""
142− if [ -e target/link_ ]; then
143− EXTRA_BUILD_FLAGS=$(cat target/link_)
144− fi
145− if [ -e target/link.build ]; then
146− EXTRA_BUILD_FLAGS="$EXTRA_BUILD_FLAGS $(cat target/link.build)"
147− fi
148− noisily rustc --crate-name build_script_build $BUILD --crate-type bin ${rustcOpts} \
149− ${crateFeatures} --out-dir target/build/${crateName} --emit=dep-info,link \
150− -L dependency=target/buildDeps ${buildDeps} --cap-lints allow $EXTRA_BUILD_FLAGS --color ${colors}
151−
152− mkdir -p target/build/${crateName}.out
153− export RUST_BACKTRACE=1
154− BUILD_OUT_DIR="-L $OUT_DIR"
155− mkdir -p $OUT_DIR
156− target/build/${crateName}/build_script_build > target/build/${crateName}.opt
157− set +e
158− EXTRA_BUILD=$(sed -n "s/^cargo:rustc-flags=\(.*\)/\1/p" target/build/${crateName}.opt | tr '\n' ' ' | sort -u)
159− EXTRA_FEATURES=$(sed -n "s/^cargo:rustc-cfg=\(.*\)/--cfg \1/p" target/build/${crateName}.opt | tr '\n' ' ')
160− EXTRA_LINK=$(sed -n "s/^cargo:rustc-link-lib=\(.*\)/\1/p" target/build/${crateName}.opt | tr '\n' ' ' | sort -u)
161− EXTRA_LINK_SEARCH=$(sed -n "s/^cargo:rustc-link-search=\(.*\)/\1/p" target/build/${crateName}.opt | tr '\n' ' ' | sort -u)
162−
163− for env in $(sed -n "s/^cargo:rustc-env=\(.*\)/\1/p" target/build/${crateName}.opt); do
164− export $env
165− done
166−
167− CRATENAME=$(echo ${crateName} | sed -e "s/\(.*\)-sys$/\U\1/")
168− grep -P "^cargo:(?!(rustc-|warning=|rerun-if-changed=|rerun-if-env-changed))" target/build/${crateName}.opt \
169− | sed -e "s/cargo:\([^=]*\)=\(.*\)/export DEP_$(echo $CRATENAME)_\U\1\E=\2/" > target/env
170−
171− set -e
172− if [[ -n "$(ls target/build/${crateName}.out)" ]]; then
173−
174− if [[ -e "${libPath}" ]]; then
175− cp -r target/build/${crateName}.out/* $(dirname ${libPath}) #*/
176− else
177− cp -r target/build/${crateName}.out/* src #*/
178− fi
179− fi
180− fi
181− runHook postConfigure
182− '';
183−
184− buildCrate = { crateName,
185− dependencies,
186− crateFeatures, libName, release, libPath,
187− crateType, metadata, crateBin, finalBins,
188− extraRustcOpts, verbose, colors }:
189−
190− let deps = makeDeps dependencies;
191− rustcOpts =
192− lib.lists.foldl' (opts: opt: opts + " " + opt)
193− (if release then "-C opt-level=3" else "-C debuginfo=2")
194− (["-C codegen-units=1"] ++ extraRustcOpts);
195− rustcMeta = "-C metadata=${metadata} -C extra-filename=-${metadata}";
196− in ''
197− runHook preBuild
198− norm=""
199− bold=""
200− green=""
201− boldgreen=""
202− if [[ "${colors}" == "always" ]]; then
203− norm="$(printf '\033[0m')" #returns to "normal"
204− bold="$(printf '\033[0;1m')" #set bold
205− green="$(printf '\033[0;32m')" #set green
206− boldgreen="$(printf '\033[0;1;32m')" #set bold, and set green.
207− fi
208− ${echo_build_heading colors}
209− ${noisily colors verbose}
210−
211− build_lib() {
212− lib_src=$1
213− echo_build_heading $lib_src ${libName}
214−
215− noisily rustc --crate-name $CRATE_NAME $lib_src --crate-type ${crateType} \
216− ${rustcOpts} ${rustcMeta} ${crateFeatures} --out-dir target/lib \
217− --emit=dep-info,link -L dependency=target/deps ${deps} --cap-lints allow \
218− $BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES --color ${colors}
219−
220− EXTRA_LIB=" --extern $CRATE_NAME=target/lib/lib$CRATE_NAME-${metadata}.rlib"
221− if [ -e target/deps/lib$CRATE_NAME-${metadata}${stdenv.hostPlatform.extensions.sharedLibrary} ]; then
222− EXTRA_LIB="$EXTRA_LIB --extern $CRATE_NAME=target/lib/lib$CRATE_NAME-${metadata}${stdenv.hostPlatform.extensions.sharedLibrary}"
223− fi
224− }
225−
226− build_bin() {
227− crate_name=$1
228− crate_name_=$(echo $crate_name | sed -e "s/-/_/g")
229− main_file=""
230− if [[ ! -z $2 ]]; then
231− main_file=$2
232− fi
233− echo_build_heading $@
234− noisily rustc --crate-name $crate_name_ $main_file --crate-type bin ${rustcOpts}\
235− ${crateFeatures} --out-dir target/bin --emit=dep-info,link -L dependency=target/deps \
236− $LINK ${deps}$EXTRA_LIB --cap-lints allow \
237− $BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES --color ${colors}
238− if [ "$crate_name_" != "$crate_name" ]; then
239− mv target/bin/$crate_name_ target/bin/$crate_name
240− fi
241− }
242−
243−
244− EXTRA_LIB=""
245− CRATE_NAME=$(echo ${libName} | sed -e "s/-/_/g")
246−
247− if [[ -e target/link_ ]]; then
248− EXTRA_BUILD="$(cat target/link_) $EXTRA_BUILD"
249− fi
250−
251− if [[ -e "${libPath}" ]]; then
252− build_lib ${libPath}
253− elif [[ -e src/lib.rs ]]; then
254− build_lib src/lib.rs
255− elif [[ -e src/${libName}.rs ]]; then
256− build_lib src/${libName}.rs
257− fi
258−
259− echo "$EXTRA_LINK_SEARCH" | while read i; do
260− if [[ ! -z "$i" ]]; then
261− for lib in $i; do
262− echo "-L $lib" >> target/link
263− L=$(echo $lib | sed -e "s#$(pwd)/target/build#$out/lib#")
264− echo "-L $L" >> target/link.final
265− done
266− fi
267− done
268− echo "$EXTRA_LINK" | while read i; do
269− if [[ ! -z "$i" ]]; then
270− for lib in $i; do
271− echo "-l $lib" >> target/link
272− echo "-l $lib" >> target/link.final
273− done
274− fi
275− done
276−
277− if [[ -e target/link ]]; then
278− sort -u target/link.final > target/link.final.sorted
279− mv target/link.final.sorted target/link.final
280− sort -u target/link > target/link.sorted
281− mv target/link.sorted target/link
282−
283− tr '\n' ' ' < target/link > target/link_
284− LINK=$(cat target/link_)
285− fi
286−
287− mkdir -p target/bin
288− echo "${crateBin}" | sed -n 1'p' | tr ',' '\n' | while read BIN; do
289− if [[ ! -z "$BIN" ]]; then
290− build_bin $BIN
291− fi
292− done
293− ${lib.optionalString (crateBin == "") ''
294− if [[ -e src/main.rs ]]; then
295− build_bin ${crateName} src/main.rs
296− fi
297− for i in src/bin/*.rs; do #*/
298− build_bin "$(basename $i .rs)" "$i"
299− done
300− ''}
301− # Remove object files to avoid "wrong ELF type"
302− find target -type f -name "*.o" -print0 | xargs -0 rm -f
303− '' + finalBins + ''
304− runHook postBuild
305− '';
306−
307− installCrate = crateName: metadata: ''
308− runHook preInstall
309− mkdir -p $out
310− if [[ -s target/env ]]; then
311− cp target/env $out/env
312− fi
313− if [[ -s target/link.final ]]; then
314− mkdir -p $out/lib
315− cp target/link.final $out/lib/link
316− fi
317− if [[ "$(ls -A target/lib)" ]]; then
318− mkdir -p $out/lib
319− cp target/lib/* $out/lib #*/
320− for lib in $out/lib/*.so $out/lib/*.dylib; do #*/
321− ln -s $lib $(echo $lib | sed -e "s/-${metadata}//")
322− done
323− fi
324− if [[ "$(ls -A target/build)" ]]; then # */
325− mkdir -p $out/lib
326− cp -r target/build/* $out/lib # */
327− fi
328− if [[ "$(ls -A target/bin)" ]]; then
329− mkdir -p $out/bin
330− cp -P target/bin/* $out/bin # */
331− fi
332− runHook postInstall
333− '';
334−in
335−
336−crate_: lib.makeOverridable ({ rust, release, verbose, features, buildInputs, crateOverrides,
337− dependencies, buildDependencies,
338− extraRustcOpts,
339− preUnpack, postUnpack, prePatch, patches, postPatch,
340− preConfigure, postConfigure, preBuild, postBuild, preInstall, postInstall }:
341−
342−let crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: {}) crateOverrides crate_);
343− dependencies_ = dependencies;
344− buildDependencies_ = buildDependencies;
345− processedAttrs = [
346− "src" "buildInputs" "crateBin" "crateLib" "libName" "libPath"
347− "buildDependencies" "dependencies" "features"
348− "crateName" "version" "build" "authors" "colors"
349− ];
350− extraDerivationAttrs = lib.filterAttrs (n: v: ! lib.elem n processedAttrs) crate;
351− buildInputs_ = buildInputs;
352−in
353−stdenv.mkDerivation (rec {
354−
355− inherit (crate) crateName;
356− inherit preUnpack postUnpack prePatch patches postPatch preConfigure postConfigure preBuild postBuild preInstall postInstall;
357−
358− src = if lib.hasAttr "src" crate then
359− crate.src
360− else
361− fetchCrate { inherit (crate) crateName version sha256; };
362− name = "rust_${crate.crateName}-${crate.version}";
363− buildInputs = [ rust ncurses ] ++ (crate.buildInputs or []) ++ buildInputs_;
364− dependencies =
365− builtins.map
366− (dep: dep.override { rust = rust; release = release; verbose = verbose; crateOverrides = crateOverrides; })
367− dependencies_;
368−
369− buildDependencies =
370− builtins.map
371− (dep: dep.override { rust = rust; release = release; verbose = verbose; crateOverrides = crateOverrides; })
372− buildDependencies_;
373−
374− completeDeps = lib.lists.unique (dependencies ++ lib.lists.concatMap (dep: dep.completeDeps) dependencies);
375− completeBuildDeps = lib.lists.unique (
376− buildDependencies
377− ++ lib.lists.concatMap (dep: dep.completeBuildDeps ++ dep.completeDeps) buildDependencies
378− );
379−
380− crateFeatures = if crate ? features then
381− lib.concatMapStringsSep " " (f: "--cfg feature=\\\"${f}\\\"") (crate.features ++ features) #"
382− else "";
383−
384− libName = if crate ? libName then crate.libName else crate.crateName;
385− libPath = if crate ? libPath then crate.libPath else "";
386−
387− depsMetadata = builtins.foldl' (str: dep: str + dep.metadata) "" (dependencies ++ buildDependencies);
388− metadata = builtins.substring 0 10 (builtins.hashString "sha256" (crateName + "-" + crateVersion + "___" + toString crateFeatures + "___" + depsMetadata ));
389−
390− crateBin = if crate ? crateBin then
391− builtins.foldl' (bins: bin:
392− let name =
393− lib.strings.replaceStrings ["-"] ["_"]
394− (if bin ? name then bin.name else crateName);
395− path = if bin ? path then bin.path else "src/main.rs";
396− in
397− bins + (if bin == "" then "" else ",") + "${name} ${path}"
398−
399− ) "" crate.crateBin
400− else "";
401−
402− finalBins = if crate ? crateBin then
403− builtins.foldl' (bins: bin:
404− let name = lib.strings.replaceStrings ["-"] ["_"]
405− (if bin ? name then bin.name else crateName);
406− new_name = if bin ? name then bin.name else crateName;
407− in
408− if name == new_name then bins else
409− (bins + "mv target/bin/${name} target/bin/${new_name};")
410−
411− ) "" crate.crateBin
412− else "";
413−
414− build = crate.build or "";
415− workspace_member = crate.workspace_member or ".";
416− crateVersion = crate.version;
417− crateAuthors = if crate ? authors && lib.isList crate.authors then crate.authors else [];
418− crateType =
419− if lib.attrByPath ["procMacro"] false crate then "proc-macro" else
420− if lib.attrByPath ["plugin"] false crate then "dylib" else
421− (crate.type or "lib");
422− colors = lib.attrByPath [ "colors" ] "always" crate;
423− extraLinkFlags = builtins.concatStringsSep " " (crate.extraLinkFlags or []);
424− configurePhase = configureCrate {
425− inherit crateName buildDependencies completeDeps completeBuildDeps
426− crateFeatures libName build workspace_member release libPath crateVersion
427− extraLinkFlags
428− crateAuthors verbose colors;
429− };
430− extraRustcOpts = if crate ? extraRustcOpts then crate.extraRustcOpts else [];
431− buildPhase = buildCrate {
432− inherit crateName dependencies
433− crateFeatures libName release libPath crateType
434− metadata crateBin finalBins verbose colors
435− extraRustcOpts;
436− };
437− installPhase = installCrate crateName metadata;
438−
439−} // extraDerivationAttrs
440−)) {
441− rust = rustc;
442− release = crate_.release or true;
443− verbose = crate_.verbose or true;
444− extraRustcOpts = [];
445− features = [];
446− buildInputs = [];
447− crateOverrides = defaultCrateOverrides;
448− preUnpack = crate_.preUnpack or "";
449− postUnpack = crate_.postUnpack or "";
450− prePatch = crate_.prePatch or "";
451− patches = crate_.patches or [];
452− postPatch = crate_.postPatch or "";
453− preConfigure = crate_.preConfigure or "";
454− postConfigure = crate_.postConfigure or "";
455− preBuild = crate_.preBuild or "";
456− postBuild = crate_.postBuild or "";
457− preInstall = crate_.preInstall or "";
458− postInstall = crate_.postInstall or "";
459− dependencies = crate_.dependencies or [];
460− buildDependencies = crate_.buildDependencies or [];
461−}
pkgs/build-support/rust/build-rust-crate/build-crate.nixnew file+157
@@ -0,0 +1,157 @@
1+{ lib, stdenv, echo_build_heading, noisily, makeDeps }:
2+{ crateName,
3+ dependencies,
4+ crateFeatures, libName, release, libPath,
5+ crateType, metadata, crateBin,
6+ extraRustcOpts, verbose, colors }:
7+
8+ let
9+
10+ deps = makeDeps dependencies;
11+ rustcOpts =
12+ lib.lists.foldl' (opts: opt: opts + " " + opt)
13+ (if release then "-C opt-level=3" else "-C debuginfo=2")
14+ (["-C codegen-units=1"] ++ extraRustcOpts);
15+ rustcMeta = "-C metadata=${metadata} -C extra-filename=-${metadata}";
16+ in ''
17+ runHook preBuild
18+ norm=""
19+ bold=""
20+ green=""
21+ boldgreen=""
22+ if [[ "${colors}" == "always" ]]; then
23+ norm="$(printf '\033[0m')" #returns to "normal"
24+ bold="$(printf '\033[0;1m')" #set bold
25+ green="$(printf '\033[0;32m')" #set green
26+ boldgreen="$(printf '\033[0;1;32m')" #set bold, and set green.
27+ fi
28+ ${echo_build_heading colors}
29+ ${noisily colors verbose}
30+
31+ build_lib() {
32+ lib_src=$1
33+ echo_build_heading $lib_src ${libName}
34+
35+ noisily rustc --crate-name $CRATE_NAME $lib_src --crate-type ${crateType} \
36+ ${rustcOpts} ${rustcMeta} ${crateFeatures} --out-dir target/lib \
37+ --emit=dep-info,link -L dependency=target/deps ${deps} --cap-lints allow \
38+ $BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES --color ${colors}
39+
40+ EXTRA_LIB=" --extern $CRATE_NAME=target/lib/lib$CRATE_NAME-${metadata}.rlib"
41+ if [ -e target/deps/lib$CRATE_NAME-${metadata}${stdenv.hostPlatform.extensions.sharedLibrary} ]; then
42+ EXTRA_LIB="$EXTRA_LIB --extern $CRATE_NAME=target/lib/lib$CRATE_NAME-${metadata}${stdenv.hostPlatform.extensions.sharedLibrary}"
43+ fi
44+ }
45+
46+ build_bin() {
47+ crate_name=$1
48+ crate_name_=$(echo $crate_name | sed -e "s/-/_/g")
49+ main_file=""
50+ if [[ ! -z $2 ]]; then
51+ main_file=$2
52+ fi
53+ echo_build_heading $@
54+ noisily rustc --crate-name $crate_name_ $main_file --crate-type bin ${rustcOpts}\
55+ ${crateFeatures} --out-dir target/bin --emit=dep-info,link -L dependency=target/deps \
56+ $LINK ${deps}$EXTRA_LIB --cap-lints allow \
57+ $BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES --color ${colors}
58+ if [ "$crate_name_" != "$crate_name" ]; then
59+ mv target/bin/$crate_name_ target/bin/$crate_name
60+ fi
61+ }
62+
63+
64+ EXTRA_LIB=""
65+ CRATE_NAME=$(echo ${libName} | sed -e "s/-/_/g")
66+
67+ if [[ -e target/link_ ]]; then
68+ EXTRA_BUILD="$(cat target/link_) $EXTRA_BUILD"
69+ fi
70+
71+ if [[ -e "${libPath}" ]]; then
72+ build_lib ${libPath}
73+ elif [[ -e src/lib.rs ]]; then
74+ build_lib src/lib.rs
75+ elif [[ -e src/${libName}.rs ]]; then
76+ build_lib src/${libName}.rs
77+ fi
78+
79+ echo "$EXTRA_LINK_SEARCH" | while read i; do
80+ if [[ ! -z "$i" ]]; then
81+ for lib in $i; do
82+ echo "-L $lib" >> target/link
83+ L=$(echo $lib | sed -e "s#$(pwd)/target/build#$out/lib#")
84+ echo "-L $L" >> target/link.final
85+ done
86+ fi
87+ done
88+ echo "$EXTRA_LINK" | while read i; do
89+ if [[ ! -z "$i" ]]; then
90+ for lib in $i; do
91+ echo "-l $lib" >> target/link
92+ echo "-l $lib" >> target/link.final
93+ done
94+ fi
95+ done
96+
97+ if [[ -e target/link ]]; then
98+ sort -u target/link.final > target/link.final.sorted
99+ mv target/link.final.sorted target/link.final
100+ sort -u target/link > target/link.sorted
101+ mv target/link.sorted target/link
102+
103+ tr '\n' ' ' < target/link > target/link_
104+ LINK=$(cat target/link_)
105+ fi
106+
107+ mkdir -p target/bin
108+ printf "%s\n" "${crateBin}" | head -n1 | tr -s ',' '\n' | while read -r BIN_NAME BIN_PATH; do
109+ # filter empty entries / empty "lines"
110+ if [[ -z "$BIN_NAME" ]]; then
111+ continue
112+ fi
113+
114+ if [[ -z "$BIN_PATH" ]]; then
115+ # heuristic to "guess" the correct source file as found in cargo:
116+ # https://github.com/rust-lang/cargo/blob/90fc9f620190d5fa3c80b0c8c65a1e1361e6b8ae/src/cargo/util/toml/targets.rs#L308-L325
117+
118+ # the first two cases are the "new" default IIRC
119+ BIN_NAME_=$(echo $BIN_NAME | sed -e 's/-/_/g')
120+ FILES=( "src/bin/$BIN_NAME.rs" "src/bin/$BIN_NAME/main.rs" "src/bin/$BIN_NAME_.rs" "src/bin/$BIN_NAME_/main.rs" "src/bin/main.rs" "src/main.rs" )
121+
122+ if ! [ -e "${libPath}" -o -e src/lib.rs -o -e "src/${libName}.rs" ]; then
123+ # if this is not a library the following path is also valid
124+ FILES=( "src/$BIN_NAME.rs" "src/$BIN_NAME_.rs" "''${FILES[@]}" )
125+ fi
126+
127+ for file in "''${FILES[@]}";
128+ do
129+ echo "checking file $file"
130+ # first file that exists wins
131+ if [[ -e "$file" ]]; then
132+ BIN_PATH="$file"
133+ break
134+ fi
135+ done
136+
137+ if [[ -z "$BIN_PATH" ]]; then
138+ echo "failed to find file for binary target: $BIN_NAME" >&2
139+ exit 1
140+ fi
141+ fi
142+ build_bin "$BIN_NAME" "$BIN_PATH"
143+ done
144+
145+
146+ ${lib.optionalString (crateBin == "") ''
147+ if [[ -e src/main.rs ]]; then
148+ build_bin ${crateName} src/main.rs
149+ fi
150+ for i in src/bin/*.rs; do #*/
151+ build_bin "$(basename $i .rs)" "$i"
152+ done
153+ ''}
154+ # Remove object files to avoid "wrong ELF type"
155+ find target -type f -name "*.o" -print0 | xargs -0 rm -f
156+ runHook postBuild
157+ ''
pkgs/build-support/rust/build-rust-crate/configure-crate.nixnew file+129
@@ -0,0 +1,129 @@
1+{ lib, stdenv, echo_build_heading, noisily, makeDeps }:
2+{ build, buildDependencies, colors, completeBuildDeps, completeDeps, crateAuthors, crateFeatures, crateName, crateVersion, extraLinkFlags, libName, libPath, release, target_os, verbose, workspace_member }:
3+let version_ = lib.splitString "-" crateVersion;
4+ versionPre = if lib.tail version_ == [] then "" else builtins.elemAt version_ 1;
5+ version = lib.splitString "." (lib.head version_);
6+ rustcOpts = (if release then "-C opt-level=3" else "-C debuginfo=2");
7+ buildDeps = makeDeps buildDependencies;
8+ authors = lib.concatStringsSep ":" crateAuthors;
9+ optLevel = if release then 3 else 0;
10+ completeDepsDir = lib.concatStringsSep " " completeDeps;
11+ completeBuildDepsDir = lib.concatStringsSep " " completeBuildDeps;
12+in ''
13+ cd ${workspace_member}
14+ runHook preConfigure
15+ ${echo_build_heading colors}
16+ ${noisily colors verbose}
17+ symlink_dependency() {
18+ # $1 is the nix-store path of a dependency
19+ # $2 is the target path
20+ i=$1
21+ ln -s -f $i/lib/*.rlib $2 #*/
22+ ln -s -f $i/lib/*.so $i/lib/*.dylib $2 #*/
23+ if [ -e "$i/lib/link" ]; then
24+ cat $i/lib/link >> target/link
25+ cat $i/lib/link >> target/link.final
26+ fi
27+ if [ -e $i/env ]; then
28+ source $i/env
29+ fi
30+ }
31+
32+ mkdir -p target/{deps,lib,build,buildDeps}
33+ chmod uga+w target -R
34+ echo ${extraLinkFlags} > target/link
35+ echo ${extraLinkFlags} > target/link.final
36+ for i in ${completeDepsDir}; do
37+ symlink_dependency $i target/deps
38+ done
39+ for i in ${completeBuildDepsDir}; do
40+ symlink_dependency $i target/buildDeps
41+ done
42+ if [[ -e target/link ]]; then
43+ sort -u target/link > target/link.sorted
44+ mv target/link.sorted target/link
45+ sort -u target/link.final > target/link.final.sorted
46+ mv target/link.final.sorted target/link.final
47+ tr '\n' ' ' < target/link > target/link_
48+ fi
49+ EXTRA_BUILD=""
50+ BUILD_OUT_DIR=""
51+ export CARGO_PKG_NAME=${crateName}
52+ export CARGO_PKG_VERSION=${crateVersion}
53+ export CARGO_PKG_AUTHORS="${authors}"
54+
55+ export CARGO_CFG_TARGET_ARCH=${stdenv.hostPlatform.parsed.cpu.name}
56+ export CARGO_CFG_TARGET_OS=${target_os}
57+ export CARGO_CFG_TARGET_FAMILY="unix"
58+ export CARGO_CFG_UNIX=1
59+ export CARGO_CFG_TARGET_ENV="gnu"
60+ export CARGO_CFG_TARGET_ENDIAN=${if stdenv.hostPlatform.parsed.cpu.significantByte.name == "littleEndian" then "little" else "big"}
61+ export CARGO_CFG_TARGET_POINTER_WIDTH=${toString stdenv.hostPlatform.parsed.cpu.bits}
62+ export CARGO_CFG_TARGET_VENDOR=${stdenv.hostPlatform.parsed.vendor.name}
63+
64+ export CARGO_MANIFEST_DIR="."
65+ export DEBUG="${toString (!release)}"
66+ export OPT_LEVEL="${toString optLevel}"
67+ export TARGET="${stdenv.hostPlatform.config}"
68+ export HOST="${stdenv.hostPlatform.config}"
69+ export PROFILE=${if release then "release" else "debug"}
70+ export OUT_DIR=$(pwd)/target/build/${crateName}.out
71+ export CARGO_PKG_VERSION_MAJOR=${builtins.elemAt version 0}
72+ export CARGO_PKG_VERSION_MINOR=${builtins.elemAt version 1}
73+ export CARGO_PKG_VERSION_PATCH=${builtins.elemAt version 2}
74+ if [[ -n "${versionPre}" ]]; then
75+ export CARGO_PKG_VERSION_PRE="${versionPre}"
76+ fi
77+
78+ BUILD=""
79+ if [[ ! -z "${build}" ]] ; then
80+ BUILD=${build}
81+ elif [[ -e "build.rs" ]]; then
82+ BUILD="build.rs"
83+ fi
84+ if [[ ! -z "$BUILD" ]] ; then
85+ echo_build_heading "$BUILD" ${libName}
86+ mkdir -p target/build/${crateName}
87+ EXTRA_BUILD_FLAGS=""
88+ if [ -e target/link_ ]; then
89+ EXTRA_BUILD_FLAGS=$(cat target/link_)
90+ fi
91+ if [ -e target/link.build ]; then
92+ EXTRA_BUILD_FLAGS="$EXTRA_BUILD_FLAGS $(cat target/link.build)"
93+ fi
94+ noisily rustc --crate-name build_script_build $BUILD --crate-type bin ${rustcOpts} \
95+ ${crateFeatures} --out-dir target/build/${crateName} --emit=dep-info,link \
96+ -L dependency=target/buildDeps ${buildDeps} --cap-lints allow $EXTRA_BUILD_FLAGS --color ${colors}
97+
98+ mkdir -p target/build/${crateName}.out
99+ export RUST_BACKTRACE=1
100+ BUILD_OUT_DIR="-L $OUT_DIR"
101+ mkdir -p $OUT_DIR
102+ target/build/${crateName}/build_script_build > target/build/${crateName}.opt
103+ set +e
104+ EXTRA_BUILD=$(sed -n "s/^cargo:rustc-flags=\(.*\)/\1/p" target/build/${crateName}.opt | tr '\n' ' ' | sort -u)
105+ EXTRA_FEATURES=$(sed -n "s/^cargo:rustc-cfg=\(.*\)/--cfg \1/p" target/build/${crateName}.opt | tr '\n' ' ')
106+ EXTRA_LINK=$(sed -n "s/^cargo:rustc-link-lib=\(.*\)/\1/p" target/build/${crateName}.opt | tr '\n' ' ' | sort -u)
107+ EXTRA_LINK_SEARCH=$(sed -n "s/^cargo:rustc-link-search=\(.*\)/\1/p" target/build/${crateName}.opt | tr '\n' ' ' | sort -u)
108+
109+ for env in $(sed -n "s/^cargo:rustc-env=\(.*\)/\1/p" target/build/${crateName}.opt); do
110+ export $env
111+ done
112+
113+ CRATENAME=$(echo ${crateName} | sed -e "s/\(.*\)-sys$/\U\1/")
114+ grep -P "^cargo:(?!(rustc-|warning=|rerun-if-changed=|rerun-if-env-changed))" target/build/${crateName}.opt \
115+ | sed -e "s/cargo:\([^=]*\)=\(.*\)/export DEP_$(echo $CRATENAME)_\U\1\E=\2/" > target/env
116+
117+ set -e
118+ if [[ -n "$(ls target/build/${crateName}.out)" ]]; then
119+
120+ if [[ -e "${libPath}" ]]; then
121+ cp -r target/build/${crateName}.out/* $(dirname ${libPath}) #*/
122+ else
123+ cp -r target/build/${crateName}.out/* src #*/
124+ fi
125+ fi
126+ fi
127+ runHook postConfigure
128+''
129+
pkgs/build-support/rust/build-rust-crate/default.nixnew file+173
@@ -0,0 +1,173 @@
1+# Code for buildRustCrate, a Nix function that builds Rust code, just
2+# like Cargo, but using Nix instead.
3+#
4+# This can be useful for deploying packages with NixOps, and to share
5+# binary dependencies between projects.
6+
7+{ lib, stdenv, defaultCrateOverrides, fetchCrate, ncurses, rustc }:
8+
9+let
10+ # This doesn't appear to be officially documented anywhere yet.
11+ # See https://github.com/rust-lang-nursery/rust-forge/issues/101.
12+ target_os = if stdenv.hostPlatform.isDarwin
13+ then "macos"
14+ else stdenv.hostPlatform.parsed.kernel.name;
15+
16+ makeDeps = dependencies:
17+ (lib.concatMapStringsSep " " (dep:
18+ let extern = lib.strings.replaceStrings ["-"] ["_"] dep.libName; in
19+ (if dep.crateType == "lib" then
20+ " --extern ${extern}=${dep.out}/lib/lib${extern}-${dep.metadata}.rlib"
21+ else
22+ " --extern ${extern}=${dep.out}/lib/lib${extern}-${dep.metadata}${stdenv.hostPlatform.extensions.sharedLibrary}")
23+ ) dependencies);
24+
25+ echo_build_heading = colors: ''
26+ echo_build_heading() {
27+ start=""
28+ end=""
29+ if [[ "${colors}" == "always" ]]; then
30+ start="$(printf '\033[0;1;32m')" #set bold, and set green.
31+ end="$(printf '\033[0m')" #returns to "normal"
32+ fi
33+ if (( $# == 1 )); then
34+ echo "$start""Building $1""$end"
35+ else
36+ echo "$start""Building $1 ($2)""$end"
37+ fi
38+ }
39+ '';
40+ noisily = colors: verbose: ''
41+ noisily() {
42+ start=""
43+ end=""
44+ if [[ "${colors}" == "always" ]]; then
45+ start="$(printf '\033[0;1;32m')" #set bold, and set green.
46+ end="$(printf '\033[0m')" #returns to "normal"
47+ fi
48+ ${lib.optionalString verbose ''
49+ echo -n "$start"Running "$end"
50+ echo $@
51+ ''}
52+ $@
53+ }
54+ '';
55+
56+ configureCrate = import ./configure-crate.nix { inherit lib stdenv echo_build_heading noisily makeDeps; };
57+ buildCrate = import ./build-crate.nix { inherit lib stdenv echo_build_heading noisily makeDeps; };
58+ installCrate = import ./install-crate.nix;
59+
60+ in
61+
62+crate_: lib.makeOverridable ({ rust, release, verbose, features, buildInputs, crateOverrides,
63+ dependencies, buildDependencies,
64+ extraRustcOpts,
65+ preUnpack, postUnpack, prePatch, patches, postPatch,
66+ preConfigure, postConfigure, preBuild, postBuild, preInstall, postInstall }:
67+
68+let crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: {}) crateOverrides crate_);
69+ dependencies_ = dependencies;
70+ buildDependencies_ = buildDependencies;
71+ processedAttrs = [
72+ "src" "buildInputs" "crateBin" "crateLib" "libName" "libPath"
73+ "buildDependencies" "dependencies" "features"
74+ "crateName" "version" "build" "authors" "colors"
75+ ];
76+ extraDerivationAttrs = lib.filterAttrs (n: v: ! lib.elem n processedAttrs) crate;
77+ buildInputs_ = buildInputs;
78+in
79+stdenv.mkDerivation (rec {
80+
81+ inherit (crate) crateName;
82+ inherit preUnpack postUnpack prePatch patches postPatch preConfigure postConfigure preBuild postBuild preInstall postInstall;
83+
84+ src = if lib.hasAttr "src" crate then
85+ crate.src
86+ else
87+ fetchCrate { inherit (crate) crateName version sha256; };
88+ name = "rust_${crate.crateName}-${crate.version}";
89+ buildInputs = [ rust ncurses ] ++ (crate.buildInputs or []) ++ buildInputs_;
90+ dependencies =
91+ builtins.map
92+ (dep: dep.override { rust = rust; release = release; verbose = verbose; crateOverrides = crateOverrides; })
93+ dependencies_;
94+
95+ buildDependencies =
96+ builtins.map
97+ (dep: dep.override { rust = rust; release = release; verbose = verbose; crateOverrides = crateOverrides; })
98+ buildDependencies_;
99+
100+ completeDeps = lib.lists.unique (dependencies ++ lib.lists.concatMap (dep: dep.completeDeps) dependencies);
101+ completeBuildDeps = lib.lists.unique (
102+ buildDependencies
103+ ++ lib.lists.concatMap (dep: dep.completeBuildDeps ++ dep.completeDeps) buildDependencies
104+ );
105+
106+ crateFeatures = if crate ? features then
107+ lib.concatMapStringsSep " " (f: "--cfg feature=\\\"${f}\\\"") (crate.features ++ features) #"
108+ else "";
109+
110+ libName = if crate ? libName then crate.libName else crate.crateName;
111+ libPath = if crate ? libPath then crate.libPath else "";
112+
113+ depsMetadata = builtins.foldl' (str: dep: str + dep.metadata) "" (dependencies ++ buildDependencies);
114+ metadata = builtins.substring 0 10 (builtins.hashString "sha256" (crateName + "-" + crateVersion + "___" + toString crateFeatures + "___" + depsMetadata ));
115+
116+ crateBin = if crate ? crateBin then
117+ builtins.foldl' (bins: bin: let
118+ name = (if bin ? name then bin.name else crateName);
119+ path = if bin ? path then bin.path else "";
120+ in
121+ bins + (if bin == "" then "" else ",") + "${name} ${path}"
122+
123+ ) "" crate.crateBin
124+ else "";
125+
126+ build = crate.build or "";
127+ workspace_member = crate.workspace_member or ".";
128+ crateVersion = crate.version;
129+ crateAuthors = if crate ? authors && lib.isList crate.authors then crate.authors else [];
130+ crateType =
131+ if lib.attrByPath ["procMacro"] false crate then "proc-macro" else
132+ if lib.attrByPath ["plugin"] false crate then "dylib" else
133+ (crate.type or "lib");
134+ colors = lib.attrByPath [ "colors" ] "always" crate;
135+ extraLinkFlags = builtins.concatStringsSep " " (crate.extraLinkFlags or []);
136+ configurePhase = configureCrate {
137+ inherit crateName buildDependencies completeDeps completeBuildDeps
138+ crateFeatures libName build workspace_member release libPath crateVersion
139+ extraLinkFlags
140+ crateAuthors verbose colors target_os;
141+ };
142+ extraRustcOpts = if crate ? extraRustcOpts then crate.extraRustcOpts else [];
143+ buildPhase = buildCrate {
144+ inherit crateName dependencies
145+ crateFeatures libName release libPath crateType
146+ metadata crateBin verbose colors
147+ extraRustcOpts;
148+ };
149+ installPhase = installCrate crateName metadata;
150+
151+} // extraDerivationAttrs
152+)) {
153+ rust = rustc;
154+ release = crate_.release or true;
155+ verbose = crate_.verbose or true;
156+ extraRustcOpts = [];
157+ features = [];
158+ buildInputs = [];
159+ crateOverrides = defaultCrateOverrides;
160+ preUnpack = crate_.preUnpack or "";
161+ postUnpack = crate_.postUnpack or "";
162+ prePatch = crate_.prePatch or "";
163+ patches = crate_.patches or [];
164+ postPatch = crate_.postPatch or "";
165+ preConfigure = crate_.preConfigure or "";
166+ postConfigure = crate_.postConfigure or "";
167+ preBuild = crate_.preBuild or "";
168+ postBuild = crate_.postBuild or "";
169+ preInstall = crate_.preInstall or "";
170+ postInstall = crate_.postInstall or "";
171+ dependencies = crate_.dependencies or [];
172+ buildDependencies = crate_.buildDependencies or [];
173+}
pkgs/build-support/rust/build-rust-crate/install-crate.nixnew file+28
@@ -0,0 +1,28 @@
1+crateName: metadata:
2+''
3+ runHook preInstall
4+ mkdir -p $out
5+ if [[ -s target/env ]]; then
6+ cp target/env $out/env
7+ fi
8+ if [[ -s target/link.final ]]; then
9+ mkdir -p $out/lib
10+ cp target/link.final $out/lib/link
11+ fi
12+ if [[ "$(ls -A target/lib)" ]]; then
13+ mkdir -p $out/lib
14+ cp target/lib/* $out/lib #*/
15+ for lib in $out/lib/*.so $out/lib/*.dylib; do #*/
16+ ln -s $lib $(echo $lib | sed -e "s/-${metadata}//")
17+ done
18+ fi
19+ if [[ "$(ls -A target/build)" ]]; then # */
20+ mkdir -p $out/lib
21+ cp -r target/build/* $out/lib # */
22+ fi
23+ if [[ "$(ls -A target/bin)" ]]; then
24+ mkdir -p $out/bin
25+ cp -P target/bin/* $out/bin # */
26+ fi
27+ runHook postInstall
28+''
pkgs/build-support/rust/build-rust-crate/test/brotli-crates.nixnew file+95
@@ -0,0 +1,95 @@
1+{ lib, buildPlatform, buildRustCrate, fetchgit }:
2+let kernel = buildPlatform.parsed.kernel.name;
3+ abi = buildPlatform.parsed.abi.name;
4+ include = includedFiles: src: builtins.filterSource (path: type:
5+ lib.lists.any (f:
6+ let p = toString (src + ("/" + f)); in
7+ (path == p) || (type == "directory" && lib.strings.hasPrefix path p)
8+ ) includedFiles
9+ ) src;
10+ updateFeatures = f: up: functions: builtins.deepSeq f (lib.lists.foldl' (features: fun: fun features) (lib.attrsets.recursiveUpdate f up) functions);
11+ mapFeatures = features: map (fun: fun { features = features; });
12+ mkFeatures = feat: lib.lists.foldl (features: featureName:
13+ if feat.${featureName} or false then
14+ [ featureName ] ++ features
15+ else
16+ features
17+ ) [] (builtins.attrNames feat);
18+in
19+rec {
20+ alloc_no_stdlib_1_3_0_ = { dependencies?[], buildDependencies?[], features?[] }: buildRustCrate {
21+ crateName = "alloc-no-stdlib";
22+ version = "1.3.0";
23+ authors = [ "Daniel Reiter Horn <danielrh@dropbox.com>" ];
24+ sha256 = "1jcp27pzmqdszgp80y484g4kwbjbg7x8a589drcwbxg0i8xwkir9";
25+ crateBin = [ { name = "example"; } ];
26+ inherit dependencies buildDependencies features;
27+ };
28+ brotli_2_5_0_ = { dependencies?[], buildDependencies?[], features?[] }: buildRustCrate {
29+ crateName = "brotli";
30+ version = "2.5.0";
31+ authors = [ "Daniel Reiter Horn <danielrh@dropbox.com>" "The Brotli Authors" ];
32+ sha256 = "1ynw4hkdwnp0kj30p86ls44ahv4s99258s019bqrq4mya8hlsb5b";
33+ crateBin = [ { name = "brotli"; } ];
34+ inherit dependencies buildDependencies features;
35+ };
36+ brotli_decompressor_1_3_1_ = { dependencies?[], buildDependencies?[], features?[] }: buildRustCrate {
37+ crateName = "brotli-decompressor";
38+ version = "1.3.1";
39+ authors = [ "Daniel Reiter Horn <danielrh@dropbox.com>" "The Brotli Authors" ];
40+ sha256 = "022g69q1xzwdj0130qm3fa4qwpn4q1jx3lc8yz0v0v201p7bm8fb";
41+ crateBin = [ { name = "brotli-decompressor"; } ];
42+ inherit dependencies buildDependencies features;
43+ };
44+ alloc_no_stdlib_1_3_0 = { features?(alloc_no_stdlib_1_3_0_features {}) }: alloc_no_stdlib_1_3_0_ {
45+ features = mkFeatures (features.alloc_no_stdlib_1_3_0 or {});
46+ };
47+ alloc_no_stdlib_1_3_0_features = f: updateFeatures f (rec {
48+ alloc_no_stdlib_1_3_0.default = (f.alloc_no_stdlib_1_3_0.default or true);
49+ }) [];
50+ brotli_2_5_0 = { features?(brotli_2_5_0_features {}) }: brotli_2_5_0_ {
51+ dependencies = mapFeatures features ([ alloc_no_stdlib_1_3_0 brotli_decompressor_1_3_1 ]);
52+ features = mkFeatures (features.brotli_2_5_0 or {});
53+ };
54+ brotli_2_5_0_features = f: updateFeatures f (rec {
55+ alloc_no_stdlib_1_3_0."no-stdlib" =
56+ (f.alloc_no_stdlib_1_3_0."no-stdlib" or false) ||
57+ (brotli_2_5_0."no-stdlib" or false) ||
58+ (f.brotli_2_5_0."no-stdlib" or false);
59+ alloc_no_stdlib_1_3_0.default = true;
60+ brotli_2_5_0.default = (f.brotli_2_5_0.default or true);
61+ brotli_decompressor_1_3_1."disable-timer" =
62+ (f.brotli_decompressor_1_3_1."disable-timer" or false) ||
63+ (brotli_2_5_0."disable-timer" or false) ||
64+ (f.brotli_2_5_0."disable-timer" or false);
65+ brotli_decompressor_1_3_1."no-stdlib" =
66+ (f.brotli_decompressor_1_3_1."no-stdlib" or false) ||
67+ (brotli_2_5_0."no-stdlib" or false) ||
68+ (f.brotli_2_5_0."no-stdlib" or false);
69+ brotli_decompressor_1_3_1.benchmark =
70+ (f.brotli_decompressor_1_3_1.benchmark or false) ||
71+ (brotli_2_5_0.benchmark or false) ||
72+ (f.brotli_2_5_0.benchmark or false);
73+ brotli_decompressor_1_3_1.default = true;
74+ brotli_decompressor_1_3_1.seccomp =
75+ (f.brotli_decompressor_1_3_1.seccomp or false) ||
76+ (brotli_2_5_0.seccomp or false) ||
77+ (f.brotli_2_5_0.seccomp or false);
78+ }) [ alloc_no_stdlib_1_3_0_features brotli_decompressor_1_3_1_features ];
79+ brotli_decompressor_1_3_1 = { features?(brotli_decompressor_1_3_1_features {}) }: brotli_decompressor_1_3_1_ {
80+ dependencies = mapFeatures features ([ alloc_no_stdlib_1_3_0 ]);
81+ features = mkFeatures (features.brotli_decompressor_1_3_1 or {});
82+ };
83+ brotli_decompressor_1_3_1_features = f: updateFeatures f (rec {
84+ alloc_no_stdlib_1_3_0."no-stdlib" =
85+ (f.alloc_no_stdlib_1_3_0."no-stdlib" or false) ||
86+ (brotli_decompressor_1_3_1."no-stdlib" or false) ||
87+ (f.brotli_decompressor_1_3_1."no-stdlib" or false);
88+ alloc_no_stdlib_1_3_0.default = true;
89+ alloc_no_stdlib_1_3_0.unsafe =
90+ (f.alloc_no_stdlib_1_3_0.unsafe or false) ||
91+ (brotli_decompressor_1_3_1.unsafe or false) ||
92+ (f.brotli_decompressor_1_3_1.unsafe or false);
93+ brotli_decompressor_1_3_1.default = (f.brotli_decompressor_1_3_1.default or true);
94+ }) [ alloc_no_stdlib_1_3_0_features ];
95+}
pkgs/build-support/rust/build-rust-crate/test/default.nixnew file+104
@@ -0,0 +1,104 @@
1+{ lib, stdenv, buildRustCrate, runCommand, writeTextFile, symlinkJoin, callPackage }:
2+let
3+ mkCrate = args: let
4+ p = {
5+ crateName = "nixtestcrate";
6+ version = "0.1.0";
7+ authors = [ "Test <test@example.com>" ];
8+ } // args;
9+ in buildRustCrate p;
10+
11+ mkFile = destination: text: writeTextFile {
12+ name = "src";
13+ destination = "/${destination}";
14+ inherit text;
15+ };
16+
17+ mkBin = name: mkFile name ''
18+ use std::env;
19+ fn main() {
20+ let name: String = env::args().nth(0).unwrap();
21+ println!("executed {}", name);
22+ }
23+ '';
24+
25+ mkLib = name: mkFile name "pub fn test() -> i32 { return 23; }";
26+
27+ mkTest = crateArgs: let
28+ crate = mkCrate crateArgs;
29+ binaries = map (v: ''"${v.name}"'') (crateArgs.crateBin or []);
30+ isLib = crateArgs ? libName || crateArgs ? libPath;
31+ crateName = crateArgs.crateName or "nixtestcrate";
32+ libName = crateArgs.libName or crateName;
33+
34+ libTestBinary = if !isLib then null else mkCrate {
35+ crateName = "run-test-${crateName}";
36+ dependencies = [ crate ];
37+ src = mkFile "src/main.rs" ''
38+ extern crate ${libName};
39+ fn main() {
40+ assert_eq!(${libName}::test(), 23);
41+ }
42+ '';
43+ };
44+
45+ in runCommand "run-buildRustCrate-${crateName}-test" {
46+ nativeBuildInputs = [ crate ];
47+ } ''
48+ ${lib.concatStringsSep "\n" binaries}
49+ ${lib.optionalString isLib ''
50+ test -e ${crate}/lib/*.rlib || exit 1
51+ ${libTestBinary}/bin/run-test-${crateName}
52+ ''}
53+ touch $out
54+ '';
55+ in rec {
56+
57+ tests = let
58+ cases = {
59+ libPath = { libPath = "src/my_lib.rs"; src = mkLib "src/my_lib.rs"; };
60+ srcLib = { src = mkLib "src/lib.rs"; };
61+ customLibName = { libName = "test_lib"; src = mkLib "src/test_lib.rs"; };
62+ customLibNameAndLibPath = { libName = "test_lib"; libPath = "src/best-lib.rs"; src = mkLib "src/best-lib.rs"; };
63+ crateBinWithPath = { crateBin = [{ name = "test_binary1"; path = "src/foobar.rs"; }]; src = mkBin "src/foobar.rs"; };
64+ crateBinNoPath1 = { crateBin = [{ name = "my-binary2"; }]; src = mkBin "src/my_binary2.rs"; };
65+ crateBinNoPath2 = {
66+ crateBin = [{ name = "my-binary3"; } { name = "my-binary4"; }];
67+ src = symlinkJoin {
68+ name = "buildRustCrateMultipleBinariesCase";
69+ paths = [ (mkBin "src/bin/my_binary3.rs") (mkBin "src/bin/my_binary4.rs") ];
70+ };
71+ };
72+ crateBinNoPath3 = { crateBin = [{ name = "my-binary5"; }]; src = mkBin "src/bin/main.rs"; };
73+ crateBinNoPath4 = { crateBin = [{ name = "my-binary6"; }]; src = mkBin "src/main.rs";};
74+ };
75+ brotliCrates = (callPackage ./brotli-crates.nix {});
76+ in lib.mapAttrs (key: value: mkTest (value // lib.optionalAttrs (!value?crateName) { crateName = key; })) cases // {
77+ brotliTest = let
78+ pkg = brotliCrates.brotli_2_5_0 {};
79+ in runCommand "run-brotli-test-cmd" {
80+ nativeBuildInputs = [ pkg ];
81+ } ''
82+ ${pkg}/bin/brotli -c ${pkg}/bin/brotli > /dev/null && touch $out
83+ '';
84+ allocNoStdLibTest = let
85+ pkg = brotliCrates.alloc_no_stdlib_1_3_0 {};
86+ in runCommand "run-alloc-no-stdlib-test-cmd" {
87+ nativeBuildInputs = [ pkg ];
88+ } ''
89+ test -e ${pkg}/bin/example && touch $out
90+ '';
91+ brotliDecompressorTest = let
92+ pkg = brotliCrates.brotli_decompressor_1_3_1 {};
93+ in runCommand "run-brotli-decompressor-test-cmd" {
94+ nativeBuildInputs = [ pkg ];
95+ } ''
96+ test -e ${pkg}/bin/brotli-decompressor && touch $out
97+ '';
98+ };
99+ test = runCommand "run-buildRustCrate-tests" {
100+ nativeBuildInputs = builtins.attrValues tests;
101+ } "
102+ touch $out
103+ ";
104+}
pkgs/top-level/all-packages.nix+2−1
@@ -7179,7 +7179,8 @@ with pkgs;
71797179 });
71807180 inherit (rust) cargo rustc;
71817181
7182− buildRustCrate = callPackage ../build-support/rust/build-rust-crate.nix { };
7182+ buildRustCrate = callPackage ../build-support/rust/build-rust-crate { };
7183+ buildRustCrateTests = recurseIntoAttrs (callPackage ../build-support/rust/build-rust-crate/test { }).tests;
71837184
71847185 cargo-vendor = callPackage ../build-support/rust/cargo-vendor { };
71857186