2020-06-12 16:09:45 +01:00
|
|
|
{ lib, stdenv, mkRustcDepArgs, mkRustcFeatureArgs, 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,
|
2019-12-12 12:55:04 +00:00
|
|
|
extraRustcOpts, verbose, colors,
|
2019-12-17 20:15:53 +00:00
|
|
|
buildTests
|
2019-12-12 12:55:04 +00:00
|
|
|
}:
|
2018-09-06 14:03:13 +01:00
|
|
|
|
|
|
|
let
|
2019-12-12 12:55:04 +00:00
|
|
|
baseRustcOpts =
|
|
|
|
[(if release then "-C opt-level=3" else "-C debuginfo=2")]
|
|
|
|
++ ["-C codegen-units=$NIX_BUILD_CORES"]
|
2020-02-05 23:59:40 +00:00
|
|
|
++ ["--remap-path-prefix=$NIX_BUILD_TOP=/" ]
|
2019-12-12 12:55:04 +00:00
|
|
|
++ [(mkRustcDepArgs dependencies crateRenames)]
|
2020-06-12 16:09:45 +01:00
|
|
|
++ [(mkRustcFeatureArgs crateFeatures)]
|
2019-12-12 12:55:04 +00:00
|
|
|
++ extraRustcOpts
|
2020-10-14 04:37:29 +01:00
|
|
|
++ lib.optional (stdenv.hostPlatform != stdenv.buildPlatform) "--target ${rust.toRustTargetSpec stdenv.hostPlatform} -C linker=${stdenv.hostPlatform.config}-gcc"
|
2020-03-17 15:33:13 +00:00
|
|
|
# since rustc 1.42 the "proc_macro" crate is part of the default crate prelude
|
|
|
|
# https://github.com/rust-lang/cargo/commit/4d64eb99a4#diff-7f98585dbf9d30aa100c8318e2c77e79R1021-R1022
|
|
|
|
++ lib.optional (lib.elem "proc-macro" crateType) "--extern proc_macro"
|
2019-12-12 12:55:04 +00:00
|
|
|
;
|
2019-02-17 23:10:36 +00:00
|
|
|
rustcMeta = "-C metadata=${metadata} -C extra-filename=-${metadata}";
|
2018-09-06 14:03:13 +01:00
|
|
|
|
|
|
|
|
2019-12-12 12:55:04 +00:00
|
|
|
# build the final rustc arguments that can be different between different
|
|
|
|
# crates
|
|
|
|
libRustcOpts = lib.concatStringsSep " " (
|
|
|
|
baseRustcOpts
|
|
|
|
++ [rustcMeta]
|
|
|
|
++ (map (x: "--crate-type ${x}") crateType)
|
|
|
|
);
|
2018-09-06 14:03:13 +01:00
|
|
|
|
2019-12-12 12:55:04 +00:00
|
|
|
binRustcOpts = lib.concatStringsSep " " (
|
|
|
|
baseRustcOpts
|
|
|
|
);
|
2018-09-06 14:03:13 +01:00
|
|
|
|
2019-12-17 20:15:53 +00:00
|
|
|
build_bin = if buildTests then "build_bin_test" else "build_bin";
|
2019-12-12 12:55:04 +00:00
|
|
|
in ''
|
|
|
|
runHook preBuild
|
2020-08-09 08:47:12 +01:00
|
|
|
|
2019-12-12 12:55:04 +00:00
|
|
|
# configure & source common build functions
|
|
|
|
LIB_RUSTC_OPTS="${libRustcOpts}"
|
|
|
|
BIN_RUSTC_OPTS="${binRustcOpts}"
|
|
|
|
LIB_EXT="${stdenv.hostPlatform.extensions.sharedLibrary}"
|
|
|
|
LIB_PATH="${libPath}"
|
|
|
|
LIB_NAME="${libName}"
|
2018-09-06 14:03:13 +01:00
|
|
|
|
2019-12-11 23:06:22 +00:00
|
|
|
CRATE_NAME='${lib.replaceStrings ["-"] ["_"] libName}'
|
2018-09-06 14:03:13 +01:00
|
|
|
|
2019-12-12 12:55:04 +00:00
|
|
|
setup_link_paths
|
2018-09-06 14:03:13 +01:00
|
|
|
|
2019-12-12 12:55:04 +00:00
|
|
|
if [[ -e "$LIB_PATH" ]]; then
|
2019-12-17 20:15:53 +00:00
|
|
|
build_lib "$LIB_PATH"
|
|
|
|
${lib.optionalString buildTests ''build_lib_test "$LIB_PATH"''}
|
2018-09-06 14:03:13 +01:00
|
|
|
elif [[ -e src/lib.rs ]]; then
|
|
|
|
build_lib src/lib.rs
|
2019-12-17 20:15:53 +00:00
|
|
|
${lib.optionalString buildTests "build_lib_test src/lib.rs"}
|
2018-09-06 14:03:13 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2019-12-17 20:15:53 +00:00
|
|
|
|
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 ''
|
2019-12-12 12:55:04 +00:00
|
|
|
BIN_PATH=""
|
|
|
|
search_for_bin_path "$BIN_NAME"
|
2019-12-11 23:06:22 +00:00
|
|
|
'' else ''
|
|
|
|
BIN_PATH='${bin.path}'
|
|
|
|
''}
|
2019-12-17 20:15:53 +00: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-17 20:15:53 +00:00
|
|
|
${lib.optionalString buildTests ''
|
|
|
|
# When tests are enabled build all the files in the `tests` directory as
|
|
|
|
# test binaries.
|
|
|
|
if [ -d tests ]; then
|
|
|
|
# find all the .rs files (or symlinks to those) in the tests directory, no subdirectories
|
|
|
|
find tests -maxdepth 1 \( -type f -o -type l \) -a -name '*.rs' -print0 | while IFS= read -r -d ''' file; do
|
|
|
|
mkdir -p target/bin
|
|
|
|
build_bin_test_file "$file"
|
|
|
|
done
|
|
|
|
|
|
|
|
# find all the subdirectories of tests/ that contain a main.rs file as
|
|
|
|
# that is also a test according to cargo
|
|
|
|
find tests/ -mindepth 1 -maxdepth 2 \( -type f -o -type l \) -a -name 'main.rs' -print0 | while IFS= read -r -d ''' file; do
|
|
|
|
mkdir -p target/bin
|
|
|
|
build_bin_test_file "$file"
|
|
|
|
done
|
|
|
|
|
|
|
|
fi
|
|
|
|
''}
|
|
|
|
|
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
|
2019-12-17 20:15:53 +00:00
|
|
|
${build_bin} ${crateName} src/main.rs
|
2018-09-06 14:03:13 +01:00
|
|
|
fi
|
|
|
|
for i in src/bin/*.rs; do #*/
|
2018-10-28 00:06:29 +01:00
|
|
|
mkdir -p target/bin
|
2019-12-17 20:15:53 +00:00
|
|
|
${build_bin} "$(basename $i .rs)" "$i"
|
2018-09-06 14:03:13 +01:00
|
|
|
done
|
|
|
|
''}
|
|
|
|
# Remove object files to avoid "wrong ELF type"
|
|
|
|
find target -type f -name "*.o" -print0 | xargs -0 rm -f
|
|
|
|
runHook postBuild
|
|
|
|
''
|