2017-12-12 11:55:15 +01:00
|
|
|
# Code for buildRustCrate, a Nix function that builds Rust code, just
|
|
|
|
# like Cargo, but using Nix instead.
|
|
|
|
#
|
|
|
|
# This can be useful for deploying packages with NixOps, and to share
|
|
|
|
# binary dependencies between projects.
|
|
|
|
|
2018-08-20 20:43:41 +02:00
|
|
|
{ lib, stdenv, defaultCrateOverrides, fetchCrate, ncurses, rustc }:
|
2017-12-12 11:55:15 +01:00
|
|
|
|
2018-02-20 08:55:04 +01:00
|
|
|
let makeDeps = dependencies:
|
|
|
|
(lib.concatMapStringsSep " " (dep:
|
|
|
|
let extern = lib.strings.replaceStrings ["-"] ["_"] dep.libName; in
|
|
|
|
(if dep.crateType == "lib" then
|
|
|
|
" --extern ${extern}=${dep.out}/lib/lib${extern}-${dep.metadata}.rlib"
|
|
|
|
else
|
2018-08-20 20:43:41 +02:00
|
|
|
" --extern ${extern}=${dep.out}/lib/lib${extern}-${dep.metadata}${stdenv.hostPlatform.extensions.sharedLibrary}")
|
2018-02-20 08:55:04 +01:00
|
|
|
) dependencies);
|
|
|
|
|
2018-07-02 17:22:47 +02:00
|
|
|
# This doesn't appear to be officially documented anywhere yet.
|
|
|
|
# See https://github.com/rust-lang-nursery/rust-forge/issues/101.
|
2018-08-20 20:43:41 +02:00
|
|
|
target_os = if stdenv.hostPlatform.isDarwin
|
2018-07-02 17:22:47 +02:00
|
|
|
then "macos"
|
2018-08-20 20:43:41 +02:00
|
|
|
else stdenv.hostPlatform.parsed.kernel.name;
|
2018-07-02 17:22:47 +02:00
|
|
|
|
2018-02-20 08:55:04 +01:00
|
|
|
echo_build_heading = colors: ''
|
2017-12-12 11:55:15 +01:00
|
|
|
echo_build_heading() {
|
|
|
|
start=""
|
|
|
|
end=""
|
2018-03-28 10:24:22 +02:00
|
|
|
if [[ "${colors}" == "always" ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
start="$(printf '\033[0;1;32m')" #set bold, and set green.
|
|
|
|
end="$(printf '\033[0m')" #returns to "normal"
|
|
|
|
fi
|
|
|
|
if (( $# == 1 )); then
|
|
|
|
echo "$start""Building $1""$end"
|
|
|
|
else
|
|
|
|
echo "$start""Building $1 ($2)""$end"
|
|
|
|
fi
|
|
|
|
}
|
2018-02-20 08:55:04 +01:00
|
|
|
'';
|
|
|
|
noisily = colors: verbose: ''
|
2017-12-12 11:55:15 +01:00
|
|
|
noisily() {
|
|
|
|
start=""
|
|
|
|
end=""
|
2018-03-28 10:24:22 +02:00
|
|
|
if [[ "${colors}" == "always" ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
start="$(printf '\033[0;1;32m')" #set bold, and set green.
|
|
|
|
end="$(printf '\033[0m')" #returns to "normal"
|
|
|
|
fi
|
|
|
|
${lib.optionalString verbose ''
|
|
|
|
echo -n "$start"Running "$end"
|
|
|
|
echo $@
|
|
|
|
''}
|
|
|
|
$@
|
|
|
|
}
|
2018-02-20 08:55:04 +01:00
|
|
|
'';
|
2017-12-12 11:55:15 +01:00
|
|
|
|
2018-02-20 08:55:04 +01:00
|
|
|
configureCrate =
|
2018-07-21 02:44:44 +02:00
|
|
|
{ crateName, crateVersion, crateAuthors, build, libName, crateFeatures, colors, libPath, release, buildDependencies, completeDeps, completeBuildDeps, verbose, workspace_member, extraLinkFlags }:
|
2018-02-20 08:55:04 +01:00
|
|
|
let version_ = lib.splitString "-" crateVersion;
|
|
|
|
versionPre = if lib.tail version_ == [] then "" else builtins.elemAt version_ 1;
|
|
|
|
version = lib.splitString "." (lib.head version_);
|
|
|
|
rustcOpts = (if release then "-C opt-level=3" else "-C debuginfo=2");
|
|
|
|
buildDeps = makeDeps buildDependencies;
|
|
|
|
authors = lib.concatStringsSep ":" crateAuthors;
|
|
|
|
optLevel = if release then 3 else 0;
|
|
|
|
completeDepsDir = lib.concatStringsSep " " completeDeps;
|
|
|
|
completeBuildDepsDir = lib.concatStringsSep " " completeBuildDeps;
|
|
|
|
in ''
|
2018-04-16 14:11:25 +02:00
|
|
|
cd ${workspace_member}
|
2018-02-20 08:55:04 +01:00
|
|
|
runHook preConfigure
|
|
|
|
${echo_build_heading colors}
|
|
|
|
${noisily colors verbose}
|
2017-12-12 11:55:15 +01:00
|
|
|
symlink_dependency() {
|
2018-02-20 08:55:04 +01:00
|
|
|
# $1 is the nix-store path of a dependency
|
|
|
|
# $2 is the target path
|
2017-12-12 11:55:15 +01:00
|
|
|
i=$1
|
2018-02-20 08:55:04 +01:00
|
|
|
ln -s -f $i/lib/*.rlib $2 #*/
|
|
|
|
ln -s -f $i/lib/*.so $i/lib/*.dylib $2 #*/
|
2017-12-12 11:55:15 +01:00
|
|
|
if [ -e "$i/lib/link" ]; then
|
|
|
|
cat $i/lib/link >> target/link
|
|
|
|
cat $i/lib/link >> target/link.final
|
|
|
|
fi
|
|
|
|
if [ -e $i/env ]; then
|
|
|
|
source $i/env
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir -p target/{deps,lib,build,buildDeps}
|
|
|
|
chmod uga+w target -R
|
2018-05-20 12:28:19 +02:00
|
|
|
echo ${extraLinkFlags} > target/link
|
|
|
|
echo ${extraLinkFlags} > target/link.final
|
2017-12-12 11:55:15 +01:00
|
|
|
for i in ${completeDepsDir}; do
|
2018-02-20 08:55:04 +01:00
|
|
|
symlink_dependency $i target/deps
|
2017-12-12 11:55:15 +01:00
|
|
|
done
|
|
|
|
for i in ${completeBuildDepsDir}; do
|
2018-02-20 08:55:04 +01:00
|
|
|
symlink_dependency $i target/buildDeps
|
2017-12-12 11:55:15 +01:00
|
|
|
done
|
2018-02-20 08:55:04 +01:00
|
|
|
if [[ -e target/link ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
sort -u target/link > target/link.sorted
|
|
|
|
mv target/link.sorted target/link
|
|
|
|
sort -u target/link.final > target/link.final.sorted
|
|
|
|
mv target/link.final.sorted target/link.final
|
|
|
|
tr '\n' ' ' < target/link > target/link_
|
|
|
|
fi
|
|
|
|
EXTRA_BUILD=""
|
|
|
|
BUILD_OUT_DIR=""
|
|
|
|
export CARGO_PKG_NAME=${crateName}
|
|
|
|
export CARGO_PKG_VERSION=${crateVersion}
|
|
|
|
export CARGO_PKG_AUTHORS="${authors}"
|
2018-02-20 08:55:04 +01:00
|
|
|
|
2018-08-20 20:43:41 +02:00
|
|
|
export CARGO_CFG_TARGET_ARCH=${stdenv.hostPlatform.parsed.cpu.name}
|
2018-07-02 17:22:47 +02:00
|
|
|
export CARGO_CFG_TARGET_OS=${target_os}
|
2018-02-20 08:55:04 +01:00
|
|
|
export CARGO_CFG_TARGET_FAMILY="unix"
|
|
|
|
export CARGO_CFG_UNIX=1
|
2017-12-12 11:55:15 +01:00
|
|
|
export CARGO_CFG_TARGET_ENV="gnu"
|
2018-08-20 20:43:41 +02:00
|
|
|
export CARGO_CFG_TARGET_ENDIAN=${if stdenv.hostPlatform.parsed.cpu.significantByte.name == "littleEndian" then "little" else "big"}
|
|
|
|
export CARGO_CFG_TARGET_POINTER_WIDTH=${toString stdenv.hostPlatform.parsed.cpu.bits}
|
|
|
|
export CARGO_CFG_TARGET_VENDOR=${stdenv.hostPlatform.parsed.vendor.name}
|
2018-02-20 08:55:04 +01:00
|
|
|
|
2017-12-12 11:55:15 +01:00
|
|
|
export CARGO_MANIFEST_DIR="."
|
|
|
|
export DEBUG="${toString (!release)}"
|
|
|
|
export OPT_LEVEL="${toString optLevel}"
|
2018-08-20 20:43:41 +02:00
|
|
|
export TARGET="${stdenv.hostPlatform.config}"
|
|
|
|
export HOST="${stdenv.hostPlatform.config}"
|
2017-12-12 11:55:15 +01:00
|
|
|
export PROFILE=${if release then "release" else "debug"}
|
|
|
|
export OUT_DIR=$(pwd)/target/build/${crateName}.out
|
|
|
|
export CARGO_PKG_VERSION_MAJOR=${builtins.elemAt version 0}
|
|
|
|
export CARGO_PKG_VERSION_MINOR=${builtins.elemAt version 1}
|
|
|
|
export CARGO_PKG_VERSION_PATCH=${builtins.elemAt version 2}
|
2018-02-20 08:55:04 +01:00
|
|
|
if [[ -n "${versionPre}" ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
export CARGO_PKG_VERSION_PRE="${versionPre}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
BUILD=""
|
|
|
|
if [[ ! -z "${build}" ]] ; then
|
|
|
|
BUILD=${build}
|
|
|
|
elif [[ -e "build.rs" ]]; then
|
|
|
|
BUILD="build.rs"
|
|
|
|
fi
|
|
|
|
if [[ ! -z "$BUILD" ]] ; then
|
|
|
|
echo_build_heading "$BUILD" ${libName}
|
|
|
|
mkdir -p target/build/${crateName}
|
|
|
|
EXTRA_BUILD_FLAGS=""
|
|
|
|
if [ -e target/link_ ]; then
|
|
|
|
EXTRA_BUILD_FLAGS=$(cat target/link_)
|
|
|
|
fi
|
|
|
|
if [ -e target/link.build ]; then
|
|
|
|
EXTRA_BUILD_FLAGS="$EXTRA_BUILD_FLAGS $(cat target/link.build)"
|
|
|
|
fi
|
|
|
|
noisily rustc --crate-name build_script_build $BUILD --crate-type bin ${rustcOpts} \
|
2018-02-20 08:55:04 +01:00
|
|
|
${crateFeatures} --out-dir target/build/${crateName} --emit=dep-info,link \
|
|
|
|
-L dependency=target/buildDeps ${buildDeps} --cap-lints allow $EXTRA_BUILD_FLAGS --color ${colors}
|
2017-12-12 11:55:15 +01:00
|
|
|
|
|
|
|
mkdir -p target/build/${crateName}.out
|
|
|
|
export RUST_BACKTRACE=1
|
|
|
|
BUILD_OUT_DIR="-L $OUT_DIR"
|
|
|
|
mkdir -p $OUT_DIR
|
|
|
|
target/build/${crateName}/build_script_build > target/build/${crateName}.opt
|
|
|
|
set +e
|
2018-04-16 14:11:25 +02:00
|
|
|
EXTRA_BUILD=$(sed -n "s/^cargo:rustc-flags=\(.*\)/\1/p" target/build/${crateName}.opt | tr '\n' ' ' | sort -u)
|
2017-12-12 11:55:15 +01:00
|
|
|
EXTRA_FEATURES=$(sed -n "s/^cargo:rustc-cfg=\(.*\)/--cfg \1/p" target/build/${crateName}.opt | tr '\n' ' ')
|
2018-04-16 14:11:25 +02:00
|
|
|
EXTRA_LINK=$(sed -n "s/^cargo:rustc-link-lib=\(.*\)/\1/p" target/build/${crateName}.opt | tr '\n' ' ' | sort -u)
|
|
|
|
EXTRA_LINK_SEARCH=$(sed -n "s/^cargo:rustc-link-search=\(.*\)/\1/p" target/build/${crateName}.opt | tr '\n' ' ' | sort -u)
|
|
|
|
|
|
|
|
for env in $(sed -n "s/^cargo:rustc-env=\(.*\)/\1/p" target/build/${crateName}.opt); do
|
|
|
|
export $env
|
|
|
|
done
|
|
|
|
|
2017-12-12 11:55:15 +01:00
|
|
|
CRATENAME=$(echo ${crateName} | sed -e "s/\(.*\)-sys$/\U\1/")
|
|
|
|
grep -P "^cargo:(?!(rustc-|warning=|rerun-if-changed=|rerun-if-env-changed))" target/build/${crateName}.opt \
|
|
|
|
| sed -e "s/cargo:\([^=]*\)=\(.*\)/export DEP_$(echo $CRATENAME)_\U\1\E=\2/" > target/env
|
|
|
|
|
|
|
|
set -e
|
2018-02-20 08:55:04 +01:00
|
|
|
if [[ -n "$(ls target/build/${crateName}.out)" ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
|
2018-02-20 08:55:04 +01:00
|
|
|
if [[ -e "${libPath}" ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
cp -r target/build/${crateName}.out/* $(dirname ${libPath}) #*/
|
|
|
|
else
|
|
|
|
cp -r target/build/${crateName}.out/* src #*/
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2018-02-20 08:55:04 +01:00
|
|
|
runHook postConfigure
|
|
|
|
'';
|
|
|
|
|
2018-07-21 02:44:44 +02:00
|
|
|
buildCrate = { crateName,
|
|
|
|
dependencies,
|
|
|
|
crateFeatures, libName, release, libPath,
|
2018-02-20 08:55:04 +01:00
|
|
|
crateType, metadata, crateBin, finalBins,
|
2018-04-16 14:11:25 +02:00
|
|
|
extraRustcOpts, verbose, colors }:
|
2018-02-20 08:55:04 +01:00
|
|
|
|
2018-07-20 19:56:59 +02:00
|
|
|
let deps = makeDeps dependencies;
|
2018-02-20 08:55:04 +01:00
|
|
|
rustcOpts =
|
|
|
|
lib.lists.foldl' (opts: opt: opts + " " + opt)
|
|
|
|
(if release then "-C opt-level=3" else "-C debuginfo=2")
|
2018-04-16 16:16:28 +02:00
|
|
|
(["-C codegen-units=1"] ++ extraRustcOpts);
|
2018-02-20 08:55:04 +01:00
|
|
|
rustcMeta = "-C metadata=${metadata} -C extra-filename=-${metadata}";
|
|
|
|
in ''
|
|
|
|
runHook preBuild
|
|
|
|
norm=""
|
|
|
|
bold=""
|
|
|
|
green=""
|
|
|
|
boldgreen=""
|
2018-03-28 10:24:22 +02:00
|
|
|
if [[ "${colors}" == "always" ]]; then
|
2018-02-20 08:55:04 +01:00
|
|
|
norm="$(printf '\033[0m')" #returns to "normal"
|
|
|
|
bold="$(printf '\033[0;1m')" #set bold
|
|
|
|
green="$(printf '\033[0;32m')" #set green
|
|
|
|
boldgreen="$(printf '\033[0;1;32m')" #set bold, and set green.
|
|
|
|
fi
|
|
|
|
${echo_build_heading colors}
|
|
|
|
${noisily colors verbose}
|
|
|
|
|
|
|
|
build_lib() {
|
|
|
|
lib_src=$1
|
|
|
|
echo_build_heading $lib_src ${libName}
|
|
|
|
|
|
|
|
noisily rustc --crate-name $CRATE_NAME $lib_src --crate-type ${crateType} \
|
|
|
|
${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"
|
2018-08-20 20:43:41 +02:00
|
|
|
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}"
|
2018-02-20 08:55:04 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
build_bin() {
|
|
|
|
crate_name=$1
|
|
|
|
crate_name_=$(echo $crate_name | sed -e "s/-/_/g")
|
2018-04-16 14:11:25 +02:00
|
|
|
main_file=""
|
|
|
|
if [[ ! -z $2 ]]; then
|
2018-02-20 08:55:04 +01:00
|
|
|
main_file=$2
|
2018-04-16 14:11:25 +02:00
|
|
|
fi
|
|
|
|
echo_build_heading $@
|
|
|
|
noisily rustc --crate-name $crate_name_ $main_file --crate-type bin ${rustcOpts}\
|
2018-02-20 08:55:04 +01:00
|
|
|
${crateFeatures} --out-dir target/bin --emit=dep-info,link -L dependency=target/deps \
|
|
|
|
$LINK ${deps}$EXTRA_LIB --cap-lints allow \
|
|
|
|
$BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES --color ${colors}
|
2018-03-27 21:08:48 +02:00
|
|
|
if [ "$crate_name_" != "$crate_name" ]; then
|
2018-02-20 08:55:04 +01:00
|
|
|
mv target/bin/$crate_name_ target/bin/$crate_name
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-12-12 11:55:15 +01:00
|
|
|
|
|
|
|
EXTRA_LIB=""
|
|
|
|
CRATE_NAME=$(echo ${libName} | sed -e "s/-/_/g")
|
|
|
|
|
2018-02-20 08:55:04 +01:00
|
|
|
if [[ -e target/link_ ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
EXTRA_BUILD="$(cat target/link_) $EXTRA_BUILD"
|
|
|
|
fi
|
|
|
|
|
2018-02-20 08:55:04 +01:00
|
|
|
if [[ -e "${libPath}" ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
build_lib ${libPath}
|
2018-02-20 08:55:04 +01:00
|
|
|
elif [[ -e src/lib.rs ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
build_lib src/lib.rs
|
2018-02-20 08:55:04 +01:00
|
|
|
elif [[ -e src/${libName}.rs ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
build_lib src/${libName}.rs
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$EXTRA_LINK_SEARCH" | while read i; do
|
2018-02-20 08:55:04 +01:00
|
|
|
if [[ ! -z "$i" ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
for lib in $i; do
|
|
|
|
echo "-L $lib" >> target/link
|
|
|
|
L=$(echo $lib | sed -e "s#$(pwd)/target/build#$out/lib#")
|
|
|
|
echo "-L $L" >> target/link.final
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo "$EXTRA_LINK" | while read i; do
|
2018-02-20 08:55:04 +01:00
|
|
|
if [[ ! -z "$i" ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
for lib in $i; do
|
|
|
|
echo "-l $lib" >> target/link
|
|
|
|
echo "-l $lib" >> target/link.final
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2018-02-20 08:55:04 +01:00
|
|
|
if [[ -e target/link ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
mkdir -p target/bin
|
|
|
|
echo "${crateBin}" | sed -n 1'p' | tr ',' '\n' | while read BIN; do
|
2018-02-20 08:55:04 +01:00
|
|
|
if [[ ! -z "$BIN" ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
build_bin $BIN
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
${lib.optionalString (crateBin == "") ''
|
|
|
|
if [[ -e src/main.rs ]]; then
|
|
|
|
build_bin ${crateName} src/main.rs
|
|
|
|
fi
|
|
|
|
for i in src/bin/*.rs; do #*/
|
|
|
|
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
|
2018-02-20 08:55:04 +01:00
|
|
|
'' + finalBins + ''
|
2017-12-12 11:55:15 +01:00
|
|
|
runHook postBuild
|
2018-02-20 08:55:04 +01:00
|
|
|
'';
|
2017-12-12 11:55:15 +01:00
|
|
|
|
2018-02-20 08:55:04 +01:00
|
|
|
installCrate = crateName: metadata: ''
|
|
|
|
runHook preInstall
|
2017-12-12 11:55:15 +01:00
|
|
|
mkdir -p $out
|
2018-02-20 08:55:04 +01:00
|
|
|
if [[ -s target/env ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
cp target/env $out/env
|
|
|
|
fi
|
2018-02-20 08:55:04 +01:00
|
|
|
if [[ -s target/link.final ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
mkdir -p $out/lib
|
|
|
|
cp target/link.final $out/lib/link
|
|
|
|
fi
|
2018-02-20 08:55:04 +01:00
|
|
|
if [[ "$(ls -A target/lib)" ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
mkdir -p $out/lib
|
|
|
|
cp target/lib/* $out/lib #*/
|
2018-02-20 08:55:04 +01:00
|
|
|
for lib in $out/lib/*.so $out/lib/*.dylib; do #*/
|
|
|
|
ln -s $lib $(echo $lib | sed -e "s/-${metadata}//")
|
|
|
|
done
|
2017-12-12 11:55:15 +01:00
|
|
|
fi
|
2018-02-20 08:55:04 +01:00
|
|
|
if [[ "$(ls -A target/build)" ]]; then # */
|
2017-12-12 11:55:15 +01:00
|
|
|
mkdir -p $out/lib
|
|
|
|
cp -r target/build/* $out/lib # */
|
|
|
|
fi
|
2018-02-20 08:55:04 +01:00
|
|
|
if [[ "$(ls -A target/bin)" ]]; then
|
2017-12-12 11:55:15 +01:00
|
|
|
mkdir -p $out/bin
|
|
|
|
cp -P target/bin/* $out/bin # */
|
|
|
|
fi
|
2018-02-13 17:28:32 +01:00
|
|
|
runHook postInstall
|
2017-12-12 11:55:15 +01:00
|
|
|
'';
|
|
|
|
in
|
|
|
|
|
2018-02-20 08:55:04 +01:00
|
|
|
crate_: lib.makeOverridable ({ rust, release, verbose, features, buildInputs, crateOverrides,
|
|
|
|
dependencies, buildDependencies,
|
|
|
|
extraRustcOpts,
|
|
|
|
preUnpack, postUnpack, prePatch, patches, postPatch,
|
|
|
|
preConfigure, postConfigure, preBuild, postBuild, preInstall, postInstall }:
|
2017-12-12 11:55:15 +01:00
|
|
|
|
|
|
|
let crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: {}) crateOverrides crate_);
|
2018-02-20 08:55:04 +01:00
|
|
|
dependencies_ = dependencies;
|
|
|
|
buildDependencies_ = buildDependencies;
|
2018-01-17 16:00:20 +01:00
|
|
|
processedAttrs = [
|
|
|
|
"src" "buildInputs" "crateBin" "crateLib" "libName" "libPath"
|
|
|
|
"buildDependencies" "dependencies" "features"
|
|
|
|
"crateName" "version" "build" "authors" "colors"
|
|
|
|
];
|
|
|
|
extraDerivationAttrs = lib.filterAttrs (n: v: ! lib.elem n processedAttrs) crate;
|
2017-12-12 11:55:15 +01:00
|
|
|
buildInputs_ = buildInputs;
|
|
|
|
in
|
2018-01-17 16:00:20 +01:00
|
|
|
stdenv.mkDerivation (rec {
|
2017-12-12 11:55:15 +01:00
|
|
|
|
|
|
|
inherit (crate) crateName;
|
2018-02-20 08:55:04 +01:00
|
|
|
inherit preUnpack postUnpack prePatch patches postPatch preConfigure postConfigure preBuild postBuild preInstall postInstall;
|
2017-12-12 11:55:15 +01:00
|
|
|
|
|
|
|
src = if lib.hasAttr "src" crate then
|
|
|
|
crate.src
|
|
|
|
else
|
|
|
|
fetchCrate { inherit (crate) crateName version sha256; };
|
|
|
|
name = "rust_${crate.crateName}-${crate.version}";
|
|
|
|
buildInputs = [ rust ncurses ] ++ (crate.buildInputs or []) ++ buildInputs_;
|
|
|
|
dependencies =
|
|
|
|
builtins.map
|
|
|
|
(dep: dep.override { rust = rust; release = release; verbose = verbose; crateOverrides = crateOverrides; })
|
2018-02-20 08:55:04 +01:00
|
|
|
dependencies_;
|
2017-12-12 11:55:15 +01:00
|
|
|
|
|
|
|
buildDependencies =
|
|
|
|
builtins.map
|
|
|
|
(dep: dep.override { rust = rust; release = release; verbose = verbose; crateOverrides = crateOverrides; })
|
2018-02-20 08:55:04 +01:00
|
|
|
buildDependencies_;
|
2017-12-12 11:55:15 +01:00
|
|
|
|
|
|
|
completeDeps = lib.lists.unique (dependencies ++ lib.lists.concatMap (dep: dep.completeDeps) dependencies);
|
|
|
|
completeBuildDeps = lib.lists.unique (
|
|
|
|
buildDependencies
|
|
|
|
++ lib.lists.concatMap (dep: dep.completeBuildDeps ++ dep.completeDeps) buildDependencies
|
|
|
|
);
|
|
|
|
|
|
|
|
crateFeatures = if crate ? features then
|
2018-04-16 14:11:25 +02:00
|
|
|
lib.concatMapStringsSep " " (f: "--cfg feature=\\\"${f}\\\"") (crate.features ++ features) #"
|
2017-12-12 11:55:15 +01:00
|
|
|
else "";
|
|
|
|
|
|
|
|
libName = if crate ? libName then crate.libName else crate.crateName;
|
|
|
|
libPath = if crate ? libPath then crate.libPath else "";
|
|
|
|
|
2018-02-20 08:55:04 +01:00
|
|
|
depsMetadata = builtins.foldl' (str: dep: str + dep.metadata) "" (dependencies ++ buildDependencies);
|
|
|
|
metadata = builtins.substring 0 10 (builtins.hashString "sha256" (crateName + "-" + crateVersion + "___" + toString crateFeatures + "___" + depsMetadata ));
|
2017-12-12 11:55:15 +01:00
|
|
|
|
|
|
|
crateBin = if crate ? crateBin then
|
|
|
|
builtins.foldl' (bins: bin:
|
|
|
|
let name =
|
|
|
|
lib.strings.replaceStrings ["-"] ["_"]
|
|
|
|
(if bin ? name then bin.name else crateName);
|
|
|
|
path = if bin ? path then bin.path else "src/main.rs";
|
|
|
|
in
|
|
|
|
bins + (if bin == "" then "" else ",") + "${name} ${path}"
|
|
|
|
|
|
|
|
) "" crate.crateBin
|
|
|
|
else "";
|
|
|
|
|
|
|
|
finalBins = if crate ? crateBin then
|
|
|
|
builtins.foldl' (bins: bin:
|
|
|
|
let name = lib.strings.replaceStrings ["-"] ["_"]
|
|
|
|
(if bin ? name then bin.name else crateName);
|
|
|
|
new_name = if bin ? name then bin.name else crateName;
|
|
|
|
in
|
|
|
|
if name == new_name then bins else
|
|
|
|
(bins + "mv target/bin/${name} target/bin/${new_name};")
|
|
|
|
|
|
|
|
) "" crate.crateBin
|
|
|
|
else "";
|
|
|
|
|
2018-04-16 14:11:25 +02:00
|
|
|
build = crate.build or "";
|
|
|
|
workspace_member = crate.workspace_member or ".";
|
2017-12-12 11:55:15 +01:00
|
|
|
crateVersion = crate.version;
|
|
|
|
crateAuthors = if crate ? authors && lib.isList crate.authors then crate.authors else [];
|
|
|
|
crateType =
|
|
|
|
if lib.attrByPath ["procMacro"] false crate then "proc-macro" else
|
2018-02-20 08:55:04 +01:00
|
|
|
if lib.attrByPath ["plugin"] false crate then "dylib" else
|
2018-04-16 14:11:25 +02:00
|
|
|
(crate.type or "lib");
|
2017-12-12 11:55:15 +01:00
|
|
|
colors = lib.attrByPath [ "colors" ] "always" crate;
|
2018-05-20 12:28:19 +02:00
|
|
|
extraLinkFlags = builtins.concatStringsSep " " (crate.extraLinkFlags or []);
|
2018-02-20 08:55:04 +01:00
|
|
|
configurePhase = configureCrate {
|
2018-07-21 02:44:44 +02:00
|
|
|
inherit crateName buildDependencies completeDeps completeBuildDeps
|
2018-04-16 14:11:25 +02:00
|
|
|
crateFeatures libName build workspace_member release libPath crateVersion
|
2018-05-20 12:28:19 +02:00
|
|
|
extraLinkFlags
|
2018-02-20 08:55:04 +01:00
|
|
|
crateAuthors verbose colors;
|
|
|
|
};
|
|
|
|
extraRustcOpts = if crate ? extraRustcOpts then crate.extraRustcOpts else [];
|
|
|
|
buildPhase = buildCrate {
|
2018-07-21 02:44:44 +02:00
|
|
|
inherit crateName dependencies
|
|
|
|
crateFeatures libName release libPath crateType
|
|
|
|
metadata crateBin finalBins verbose colors
|
2018-04-16 14:11:25 +02:00
|
|
|
extraRustcOpts;
|
2017-12-12 11:55:15 +01:00
|
|
|
};
|
2018-02-20 08:55:04 +01:00
|
|
|
installPhase = installCrate crateName metadata;
|
2017-12-12 11:55:15 +01:00
|
|
|
|
2018-02-20 08:55:04 +01:00
|
|
|
} // extraDerivationAttrs
|
|
|
|
)) {
|
2017-12-12 11:55:15 +01:00
|
|
|
rust = rustc;
|
2018-02-20 08:55:04 +01:00
|
|
|
release = crate_.release or true;
|
|
|
|
verbose = crate_.verbose or true;
|
|
|
|
extraRustcOpts = [];
|
2017-12-12 11:55:15 +01:00
|
|
|
features = [];
|
|
|
|
buildInputs = [];
|
|
|
|
crateOverrides = defaultCrateOverrides;
|
2018-02-20 08:55:04 +01:00
|
|
|
preUnpack = crate_.preUnpack or "";
|
|
|
|
postUnpack = crate_.postUnpack or "";
|
|
|
|
prePatch = crate_.prePatch or "";
|
|
|
|
patches = crate_.patches or [];
|
|
|
|
postPatch = crate_.postPatch or "";
|
|
|
|
preConfigure = crate_.preConfigure or "";
|
|
|
|
postConfigure = crate_.postConfigure or "";
|
|
|
|
preBuild = crate_.preBuild or "";
|
|
|
|
postBuild = crate_.postBuild or "";
|
|
|
|
preInstall = crate_.preInstall or "";
|
|
|
|
postInstall = crate_.postInstall or "";
|
|
|
|
dependencies = crate_.dependencies or [];
|
|
|
|
buildDependencies = crate_.buildDependencies or [];
|
2017-12-12 11:55:15 +01:00
|
|
|
}
|