2022-08-12 12:06:08 +00:00
|
|
|
{ lib, stdenv
|
|
|
|
, mkRustcDepArgs, mkRustcFeatureArgs, needUnstableCLI
|
|
|
|
, rust
|
|
|
|
}:
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
{ crateName,
|
|
|
|
dependencies,
|
|
|
|
crateFeatures, crateRenames, libName, release, libPath,
|
|
|
|
crateType, metadata, crateBin, hasCrateBin,
|
|
|
|
extraRustcOpts, verbose, colors,
|
2022-04-27 09:35:20 +00:00
|
|
|
buildTests,
|
|
|
|
codegenUnits
|
2020-04-24 23:36:52 +00:00
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
|
|
|
baseRustcOpts =
|
2021-04-08 16:26:57 +00:00
|
|
|
[
|
|
|
|
(if release then "-C opt-level=3" else "-C debuginfo=2")
|
2022-04-27 09:35:20 +00:00
|
|
|
"-C codegen-units=${toString codegenUnits}"
|
2021-04-08 16:26:57 +00:00
|
|
|
"--remap-path-prefix=$NIX_BUILD_TOP=/"
|
|
|
|
(mkRustcDepArgs dependencies crateRenames)
|
|
|
|
(mkRustcFeatureArgs crateFeatures)
|
2021-10-07 14:46:35 +00:00
|
|
|
] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
|
|
|
|
"--target" (rust.toRustTargetSpec stdenv.hostPlatform)
|
2022-08-12 12:06:08 +00:00
|
|
|
] ++ lib.optionals (needUnstableCLI dependencies) [
|
|
|
|
"-Z" "unstable-options"
|
2021-04-08 16:26:57 +00:00
|
|
|
] ++ extraRustcOpts
|
2020-04-24 23:36:52 +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"
|
|
|
|
;
|
|
|
|
rustcMeta = "-C metadata=${metadata} -C extra-filename=-${metadata}";
|
|
|
|
|
|
|
|
|
|
|
|
# build the final rustc arguments that can be different between different
|
|
|
|
# crates
|
|
|
|
libRustcOpts = lib.concatStringsSep " " (
|
|
|
|
baseRustcOpts
|
|
|
|
++ [rustcMeta]
|
|
|
|
++ (map (x: "--crate-type ${x}") crateType)
|
|
|
|
);
|
|
|
|
|
|
|
|
binRustcOpts = lib.concatStringsSep " " (
|
|
|
|
baseRustcOpts
|
|
|
|
);
|
|
|
|
|
|
|
|
build_bin = if buildTests then "build_bin_test" else "build_bin";
|
|
|
|
in ''
|
|
|
|
runHook preBuild
|
2020-08-20 17:08:02 +00:00
|
|
|
|
2020-04-24 23:36:52 +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}"
|
|
|
|
|
|
|
|
CRATE_NAME='${lib.replaceStrings ["-"] ["_"] libName}'
|
|
|
|
|
|
|
|
setup_link_paths
|
|
|
|
|
|
|
|
if [[ -e "$LIB_PATH" ]]; then
|
|
|
|
build_lib "$LIB_PATH"
|
|
|
|
${lib.optionalString buildTests ''build_lib_test "$LIB_PATH"''}
|
|
|
|
elif [[ -e src/lib.rs ]]; then
|
|
|
|
build_lib src/lib.rs
|
|
|
|
${lib.optionalString buildTests "build_lib_test src/lib.rs"}
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
${lib.optionalString (lib.length crateBin > 0) (lib.concatMapStringsSep "\n" (bin:
|
|
|
|
let
|
|
|
|
haveRequiredFeature = if bin ? requiredFeatures then
|
|
|
|
# Check that all element in requiredFeatures are also present in crateFeatures
|
|
|
|
lib.intersectLists bin.requiredFeatures crateFeatures == bin.requiredFeatures
|
|
|
|
else
|
|
|
|
true;
|
|
|
|
in
|
|
|
|
if haveRequiredFeature then ''
|
2020-04-24 23:36:52 +00:00
|
|
|
mkdir -p target/bin
|
|
|
|
BIN_NAME='${bin.name or crateName}'
|
|
|
|
${if !bin ? path then ''
|
|
|
|
BIN_PATH=""
|
|
|
|
search_for_bin_path "$BIN_NAME"
|
|
|
|
'' else ''
|
|
|
|
BIN_PATH='${bin.path}'
|
|
|
|
''}
|
|
|
|
${build_bin} "$BIN_NAME" "$BIN_PATH"
|
2022-09-09 14:08:57 +00:00
|
|
|
'' else ''
|
|
|
|
echo Binary ${bin.name or crateName} not compiled due to not having all of the required features -- ${lib.escapeShellArg (builtins.toJSON bin.requiredFeatures)} -- enabled.
|
2020-04-24 23:36:52 +00:00
|
|
|
'') crateBin)}
|
|
|
|
|
|
|
|
${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
|
|
|
|
''}
|
|
|
|
|
|
|
|
# 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) ''
|
|
|
|
if [[ -e src/main.rs ]]; then
|
|
|
|
mkdir -p target/bin
|
|
|
|
${build_bin} ${crateName} src/main.rs
|
|
|
|
fi
|
|
|
|
for i in src/bin/*.rs; do #*/
|
|
|
|
mkdir -p target/bin
|
|
|
|
${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
|
|
|
|
''
|