@@ -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+ ''