2019-12-11 22:21:38 +00:00
|
|
|
{ lib, stdenv, echo_build_heading, noisily, mkRustcDepArgs, rust }:
|
2018-09-06 14:03:13 +01:00
|
|
|
{ crateName,
|
|
|
|
dependencies,
|
2019-09-08 07:18:09 +01:00
|
|
|
crateFeatures, crateRenames, libName, release, libPath,
|
2018-10-28 00:06:29 +01:00
|
|
|
crateType, metadata, crateBin, hasCrateBin,
|
2018-09-06 14:03:13 +01:00
|
|
|
extraRustcOpts, verbose, colors }:
|
|
|
|
|
|
|
|
let
|
2019-12-11 22:21:38 +00:00
|
|
|
deps = mkRustcDepArgs dependencies crateRenames;
|
2019-02-17 23:10:36 +00:00
|
|
|
rustcOpts =
|
2019-12-11 20:53:42 +00:00
|
|
|
lib.foldl' (opts: opt: opts + " " + opt)
|
2019-02-17 23:10:36 +00:00
|
|
|
(if release then "-C opt-level=3" else "-C debuginfo=2")
|
2019-03-20 01:19:50 +00:00
|
|
|
(["-C codegen-units=$NIX_BUILD_CORES"] ++ extraRustcOpts);
|
2019-02-17 23:10:36 +00:00
|
|
|
rustcMeta = "-C metadata=${metadata} -C extra-filename=-${metadata}";
|
2018-09-06 14:03:13 +01:00
|
|
|
in ''
|
|
|
|
runHook preBuild
|
|
|
|
${echo_build_heading colors}
|
|
|
|
${noisily colors verbose}
|
|
|
|
|
|
|
|
build_lib() {
|
|
|
|
lib_src=$1
|
|
|
|
echo_build_heading $lib_src ${libName}
|
|
|
|
|
2018-10-28 00:06:29 +01:00
|
|
|
noisily rustc --crate-name $CRATE_NAME $lib_src \
|
|
|
|
${lib.strings.concatStrings (map (x: " --crate-type ${x}") crateType)} \
|
2018-09-06 14:03:13 +01:00
|
|
|
${rustcOpts} ${rustcMeta} ${crateFeatures} --out-dir target/lib \
|
|
|
|
--emit=dep-info,link -L dependency=target/deps ${deps} --cap-lints allow \
|
|
|
|
$BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES --color ${colors}
|
|
|
|
|
|
|
|
EXTRA_LIB=" --extern $CRATE_NAME=target/lib/lib$CRATE_NAME-${metadata}.rlib"
|
|
|
|
if [ -e target/deps/lib$CRATE_NAME-${metadata}${stdenv.hostPlatform.extensions.sharedLibrary} ]; then
|
|
|
|
EXTRA_LIB="$EXTRA_LIB --extern $CRATE_NAME=target/lib/lib$CRATE_NAME-${metadata}${stdenv.hostPlatform.extensions.sharedLibrary}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
build_bin() {
|
|
|
|
crate_name=$1
|
2019-12-11 21:40:19 +00:00
|
|
|
crate_name_=$(echo $crate_name | tr '-' '_')
|
2018-09-06 14:03:13 +01:00
|
|
|
main_file=""
|
|
|
|
if [[ ! -z $2 ]]; then
|
|
|
|
main_file=$2
|
|
|
|
fi
|
|
|
|
echo_build_heading $@
|
|
|
|
noisily rustc --crate-name $crate_name_ $main_file --crate-type bin ${rustcOpts}\
|
|
|
|
${crateFeatures} --out-dir target/bin --emit=dep-info,link -L dependency=target/deps \
|
|
|
|
$LINK ${deps}$EXTRA_LIB --cap-lints allow \
|
2018-10-28 00:06:29 +01:00
|
|
|
$BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES --color ${colors} \
|
2018-12-21 00:28:09 +00:00
|
|
|
${if stdenv.hostPlatform != stdenv.buildPlatform then "--target ${rust.toRustTarget stdenv.hostPlatform} -C linker=${stdenv.hostPlatform.config}-gcc" else ""}
|
2018-09-06 14:03:13 +01:00
|
|
|
if [ "$crate_name_" != "$crate_name" ]; then
|
|
|
|
mv target/bin/$crate_name_ target/bin/$crate_name
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EXTRA_LIB=""
|
2019-12-11 23:06:22 +00:00
|
|
|
CRATE_NAME='${lib.replaceStrings ["-"] ["_"] libName}'
|
2018-09-06 14:03:13 +01:00
|
|
|
|
|
|
|
if [[ -e target/link_ ]]; then
|
|
|
|
EXTRA_BUILD="$(cat target/link_) $EXTRA_BUILD"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -e "${libPath}" ]]; then
|
|
|
|
build_lib ${libPath}
|
|
|
|
elif [[ -e src/lib.rs ]]; then
|
|
|
|
build_lib src/lib.rs
|
|
|
|
elif [[ -e src/${libName}.rs ]]; then
|
|
|
|
build_lib src/${libName}.rs
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$EXTRA_LINK_SEARCH" | while read i; do
|
|
|
|
if [[ ! -z "$i" ]]; then
|
2019-11-20 06:30:06 +00:00
|
|
|
for library in $i; do
|
|
|
|
echo "-L $library" >> target/link
|
|
|
|
L=$(echo $library | sed -e "s#$(pwd)/target/build#$lib/lib#")
|
2018-09-06 14:03:13 +01:00
|
|
|
echo "-L $L" >> target/link.final
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo "$EXTRA_LINK" | while read i; do
|
|
|
|
if [[ ! -z "$i" ]]; then
|
2019-11-20 06:30:06 +00:00
|
|
|
for library in $i; do
|
|
|
|
echo "-l $library" >> target/link
|
|
|
|
echo "-l $library" >> target/link.final
|
2018-09-06 14:03:13 +01:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ -e target/link ]]; then
|
|
|
|
sort -u target/link.final > target/link.final.sorted
|
|
|
|
mv target/link.final.sorted target/link.final
|
|
|
|
sort -u target/link > target/link.sorted
|
|
|
|
mv target/link.sorted target/link
|
|
|
|
|
|
|
|
tr '\n' ' ' < target/link > target/link_
|
|
|
|
LINK=$(cat target/link_)
|
|
|
|
fi
|
|
|
|
|
2019-12-11 23:06:22 +00:00
|
|
|
${lib.optionalString (lib.length crateBin > 0) (lib.concatMapStringsSep "\n" (bin: ''
|
|
|
|
mkdir -p target/bin
|
|
|
|
BIN_NAME='${bin.name or crateName}'
|
|
|
|
${if !bin ? path then ''
|
2018-09-06 14:03:13 +01:00
|
|
|
# heuristic to "guess" the correct source file as found in cargo:
|
|
|
|
# https://github.com/rust-lang/cargo/blob/90fc9f620190d5fa3c80b0c8c65a1e1361e6b8ae/src/cargo/util/toml/targets.rs#L308-L325
|
|
|
|
|
|
|
|
# the first two cases are the "new" default IIRC
|
2019-12-11 23:06:22 +00:00
|
|
|
BIN_NAME_='${lib.replaceStrings ["-"] ["_"] bin.name}'
|
2018-09-13 20:12:14 +01:00
|
|
|
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" )
|
2018-09-06 14:03:13 +01:00
|
|
|
|
|
|
|
if ! [ -e "${libPath}" -o -e src/lib.rs -o -e "src/${libName}.rs" ]; then
|
|
|
|
# if this is not a library the following path is also valid
|
2018-09-13 20:12:14 +01:00
|
|
|
FILES=( "src/$BIN_NAME.rs" "src/$BIN_NAME_.rs" "''${FILES[@]}" )
|
2018-09-06 14:03:13 +01:00
|
|
|
fi
|
|
|
|
|
2018-09-08 22:02:06 +01:00
|
|
|
for file in "''${FILES[@]}";
|
2018-09-06 14:03:13 +01:00
|
|
|
do
|
|
|
|
echo "checking file $file"
|
|
|
|
# first file that exists wins
|
|
|
|
if [[ -e "$file" ]]; then
|
|
|
|
BIN_PATH="$file"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ -z "$BIN_PATH" ]]; then
|
|
|
|
echo "failed to find file for binary target: $BIN_NAME" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-12-11 23:06:22 +00:00
|
|
|
'' else ''
|
|
|
|
BIN_PATH='${bin.path}'
|
|
|
|
''}
|
2018-09-08 22:02:06 +01:00
|
|
|
build_bin "$BIN_NAME" "$BIN_PATH"
|
2019-12-11 23:06:22 +00:00
|
|
|
'') crateBin)}
|
2018-09-06 14:03:13 +01:00
|
|
|
|
2019-12-11 23:06:22 +00:00
|
|
|
# If crateBin is empty and hasCrateBin is not set then we must try to
|
|
|
|
# detect some kind of bin target based on some files that might exist.
|
|
|
|
${lib.optionalString (lib.length crateBin == 0 && !hasCrateBin) ''
|
2018-09-06 14:03:13 +01:00
|
|
|
if [[ -e src/main.rs ]]; then
|
2018-10-28 00:06:29 +01:00
|
|
|
mkdir -p target/bin
|
2018-09-06 14:03:13 +01:00
|
|
|
build_bin ${crateName} src/main.rs
|
|
|
|
fi
|
|
|
|
for i in src/bin/*.rs; do #*/
|
2018-10-28 00:06:29 +01:00
|
|
|
mkdir -p target/bin
|
2018-09-06 14:03:13 +01:00
|
|
|
build_bin "$(basename $i .rs)" "$i"
|
|
|
|
done
|
|
|
|
''}
|
|
|
|
# Remove object files to avoid "wrong ELF type"
|
|
|
|
find target -type f -name "*.o" -print0 | xargs -0 rm -f
|
|
|
|
runHook postBuild
|
|
|
|
''
|