git.iko.soy/typeable/nixpkgs
on master help sign in
buildRustCrate: add heuristic to picking the right source files

Cargo has a few odd (old) ways of picking source files if the `bin.path` attribute isn't given in the Cargo.toml. This commit adds support for some of those. The previous behaviour always defaulted to `src/main.rs` which was not always the right choice. Since there is look-ahead into the unpacked sources before running the actual builder the path selection logic has to be embedded within the build script. `buildRustCrate` currently supports two ways of running building binaries when processing a crate: - Explicit definition of all the binaries (& optionally the paths to their respective `main.rs`) and, - if not binary was explictly configured all files matching the patterns `src/main.rs`, `src/bin/*.rs`. When the explicit list is given without path information paths are now being picked from a list of candidates. The first match wins. The order is the same as within the cargo compatibility code. If the crate does not provide any libraries the path `src/{bin_name}.rs` is also considered. All underscores within the binary names are translated into dashes (`-`) before the lookups are made. This seems to be a common convention.

commit0c50140da5bcc55c588ac64bfcedac4699a43711 authorAndreas Rammhold <andreas@rammhold.de> dateThu 6 Sep 2018, 15:03 (8 years ago) parentee21f64
2 files changed+202−10
pkgs/build-support/rust/build-rust-crate.nix+43−10 pkgs/build-support/rust/build-rust-crate/build-crate.nix+159
pkgs/build-support/rust/build-rust-crate.nix+43−10
@@ -282,14 +282,48 @@ let makeDeps = dependencies:
282282
283283 tr '\n' ' ' < target/link > target/link_
284284 LINK=$(cat target/link_)
285− fi
285+ fi
286286
287287 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
288+ printf "%s\n" "${crateBin}" | head -n1 | tr -s ',' '\n' | while read -r BIN_NAME BIN_PATH; do
289+ # filter empty entries / empty "lines"
290+ if [[ -z "$BIN_NAME" ]]; then
291+ continue
292+ fi
293+
294+ if [[ -z "$BIN_PATH" ]]; then
295+ # heuristic to "guess" the correct source file as found in cargo:
296+ # https://github.com/rust-lang/cargo/blob/90fc9f620190d5fa3c80b0c8c65a1e1361e6b8ae/src/cargo/util/toml/targets.rs#L308-L325
297+
298+ # the first two cases are the "new" default IIRC
299+ BIN_NAME_=$(echo $BIN_NAME | sed -e 's/-/_/g')
300+ FILES="src/bin/$BIN_NAME_.rs src/bin/$BIN_NAME_/main.rs src/bin/main.rs src/main.rs"
301+
302+ if ! [ -e "${libPath}" -o -e src/lib.rs -o -e "src/${libName}.rs" ]; then
303+ # if this is not a library the following path is also valid
304+ FILES="src/$BIN_NAME_.rs $FILES"
305+ fi
306+
307+ echo $FILES
308+ for file in $FILES;
309+ do
310+ echo "checking file $file"
311+ # first file that exists wins
312+ if [[ -e "$file" ]]; then
313+ BIN_PATH="$file"
314+ break
315+ fi
316+ done
317+
318+ if [[ -z "$BIN_PATH" ]]; then
319+ echo "failed to find file for binary target: $BIN_NAME" >&2
320+ exit 1
321+ fi
322+ fi
323+ build_bin $BIN_NAME $BIN_PATH
292324 done
325+
326+
293327 ${lib.optionalString (crateBin == "") ''
294328 if [[ -e src/main.rs ]]; then
295329 build_bin ${crateName} src/main.rs
@@ -388,11 +422,10 @@ stdenv.mkDerivation (rec {
388422 metadata = builtins.substring 0 10 (builtins.hashString "sha256" (crateName + "-" + crateVersion + "___" + toString crateFeatures + "___" + depsMetadata ));
389423
390424 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";
425+ builtins.foldl' (bins: bin: let
426+ _name = (if bin ? name then bin.name else crateName);
427+ name = lib.strings.replaceStrings ["-"] ["_"] _name;
428+ path = if bin ? path then bin.path else "";
396429 in
397430 bins + (if bin == "" then "" else ",") + "${name} ${path}"
398431
pkgs/build-support/rust/build-rust-crate/build-crate.nixnew file+159
@@ -0,0 +1,159 @@
1+{ lib, stdenv, echo_build_heading, noisily, makeDeps }:
2+{ crateName,
3+ dependencies,
4+ crateFeatures, libName, release, libPath,
5+ crateType, metadata, crateBin, finalBins,
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/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 $FILES"
125+ fi
126+
127+ echo $FILES
128+ for file in $FILES;
129+ do
130+ echo "checking file $file"
131+ # first file that exists wins
132+ if [[ -e "$file" ]]; then
133+ BIN_PATH="$file"
134+ break
135+ fi
136+ done
137+
138+ if [[ -z "$BIN_PATH" ]]; then
139+ echo "failed to find file for binary target: $BIN_NAME" >&2
140+ exit 1
141+ fi
142+ fi
143+ build_bin $BIN_NAME $BIN_PATH
144+ done
145+
146+
147+ ${lib.optionalString (crateBin == "") ''
148+ if [[ -e src/main.rs ]]; then
149+ build_bin ${crateName} src/main.rs
150+ fi
151+ for i in src/bin/*.rs; do #*/
152+ build_bin "$(basename $i .rs)" "$i"
153+ done
154+ ''}
155+ # Remove object files to avoid "wrong ELF type"
156+ find target -type f -name "*.o" -print0 | xargs -0 rm -f
157+ '' + finalBins + ''
158+ runHook postBuild
159+ ''