2020-09-29 06:15:06 +01:00
|
|
|
{ lib, stdenv, rust, echo_colored, noisily, mkRustcDepArgs, mkRustcFeatureArgs }:
|
2020-06-12 16:09:45 +01:00
|
|
|
{
|
|
|
|
build
|
2019-02-17 23:10:36 +00:00
|
|
|
, buildDependencies
|
|
|
|
, colors
|
|
|
|
, completeBuildDeps
|
|
|
|
, completeDeps
|
|
|
|
, crateAuthors
|
2019-02-25 15:22:03 +00:00
|
|
|
, crateDescription
|
2019-04-23 22:40:29 +01:00
|
|
|
, crateHomepage
|
2019-02-17 23:10:36 +00:00
|
|
|
, crateFeatures
|
|
|
|
, crateName
|
2019-09-08 07:18:09 +01:00
|
|
|
, crateRenames
|
2019-02-17 23:10:36 +00:00
|
|
|
, crateVersion
|
|
|
|
, extraLinkFlags
|
2021-06-30 16:50:45 +01:00
|
|
|
, extraRustcOptsForBuildRs
|
2019-02-17 23:10:36 +00:00
|
|
|
, libName
|
|
|
|
, libPath
|
|
|
|
, release
|
|
|
|
, verbose
|
|
|
|
, workspace_member }:
|
2018-09-07 14:08:06 +01:00
|
|
|
let version_ = lib.splitString "-" crateVersion;
|
2019-12-11 20:53:42 +00:00
|
|
|
versionPre = if lib.tail version_ == [] then "" else lib.elemAt version_ 1;
|
2019-09-24 10:24:13 +01:00
|
|
|
version = lib.splitVersion (lib.head version_);
|
2019-12-11 20:53:42 +00:00
|
|
|
rustcOpts = 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")
|
2021-06-30 16:50:45 +01:00
|
|
|
(["-C codegen-units=$NIX_BUILD_CORES"] ++ extraRustcOptsForBuildRs);
|
2019-12-11 22:21:38 +00:00
|
|
|
buildDeps = mkRustcDepArgs buildDependencies crateRenames;
|
2018-09-07 14:08:06 +01:00
|
|
|
authors = lib.concatStringsSep ":" crateAuthors;
|
|
|
|
optLevel = if release then 3 else 0;
|
|
|
|
completeDepsDir = lib.concatStringsSep " " completeDeps;
|
|
|
|
completeBuildDepsDir = lib.concatStringsSep " " completeBuildDeps;
|
2020-06-12 16:09:45 +01:00
|
|
|
envFeatures = lib.concatStringsSep " " (
|
|
|
|
map (f: lib.replaceChars ["-"] ["_"] (lib.toUpper f)) crateFeatures
|
|
|
|
);
|
2018-09-07 14:08:06 +01:00
|
|
|
in ''
|
2020-03-01 11:58:26 +00:00
|
|
|
${echo_colored colors}
|
2018-09-07 14:08:06 +01:00
|
|
|
${noisily colors verbose}
|
2020-03-01 11:58:26 +00:00
|
|
|
source ${./lib.sh}
|
|
|
|
|
2020-03-01 12:34:36 +00:00
|
|
|
${lib.optionalString (workspace_member != null) ''
|
|
|
|
noisily cd "${workspace_member}"
|
|
|
|
''}
|
|
|
|
${lib.optionalString (workspace_member == null) ''
|
2020-08-09 08:47:12 +01:00
|
|
|
echo_colored "Searching for matching Cargo.toml (${crateName})"
|
2020-03-01 12:34:36 +00:00
|
|
|
local cargo_toml_dir=$(matching_cargo_toml_dir "${crateName}")
|
|
|
|
if [ -z "$cargo_toml_dir" ]; then
|
|
|
|
echo_error "ERROR configuring ${crateName}: No matching Cargo.toml in $(pwd) found." >&2
|
|
|
|
exit 23
|
|
|
|
fi
|
|
|
|
noisily cd "$cargo_toml_dir"
|
|
|
|
''}
|
|
|
|
|
2020-03-01 11:58:26 +00:00
|
|
|
runHook preConfigure
|
2020-08-09 08:47:12 +01:00
|
|
|
|
2018-09-07 14:08:06 +01:00
|
|
|
symlink_dependency() {
|
|
|
|
# $1 is the nix-store path of a dependency
|
|
|
|
# $2 is the target path
|
|
|
|
i=$1
|
|
|
|
ln -s -f $i/lib/*.rlib $2 #*/
|
|
|
|
ln -s -f $i/lib/*.so $i/lib/*.dylib $2 #*/
|
|
|
|
if [ -e $i/env ]; then
|
|
|
|
source $i/env
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-03-12 11:29:31 +00:00
|
|
|
# The following steps set up the dependencies of the crate. Two
|
|
|
|
# kinds of dependencies are distinguished: build dependencies
|
|
|
|
# (used by the build script) and crate dependencies. For each
|
|
|
|
# dependency we have to:
|
|
|
|
#
|
|
|
|
# - Make its Rust library available to rustc. This is done by
|
|
|
|
# symlinking all library dependencies into a directory that
|
|
|
|
# can be provided to rustc.
|
|
|
|
# - Accumulate linking flags. These flags are largely used for
|
|
|
|
# linking native libraries.
|
|
|
|
#
|
|
|
|
# The crate link flags are added to the `link` and `link.final`
|
|
|
|
# files. The `link` file is used for linkage in the current
|
|
|
|
# crate. The `link.final` file will be copied to the output and can
|
|
|
|
# be used by downstream crates to get the linker flags of this
|
|
|
|
# crate.
|
|
|
|
|
2018-09-07 14:08:06 +01:00
|
|
|
mkdir -p target/{deps,lib,build,buildDeps}
|
|
|
|
chmod uga+w target -R
|
|
|
|
echo ${extraLinkFlags} > target/link
|
|
|
|
echo ${extraLinkFlags} > target/link.final
|
2020-03-12 11:29:31 +00:00
|
|
|
|
|
|
|
# Prepare crate dependencies
|
2018-09-07 14:08:06 +01:00
|
|
|
for i in ${completeDepsDir}; do
|
|
|
|
symlink_dependency $i target/deps
|
2020-03-12 11:29:31 +00:00
|
|
|
if [ -e "$i/lib/link" ]; then
|
|
|
|
cat $i/lib/link >> target/link
|
|
|
|
cat $i/lib/link >> target/link.final
|
|
|
|
fi
|
2018-09-07 14:08:06 +01:00
|
|
|
done
|
2020-03-12 11:29:31 +00:00
|
|
|
|
|
|
|
# Prepare crate build dependencies that are used for the build script.
|
2018-09-07 14:08:06 +01:00
|
|
|
for i in ${completeBuildDepsDir}; do
|
2020-03-12 11:29:31 +00:00
|
|
|
symlink_dependency $i target/buildDeps
|
|
|
|
if [ -e "$i/lib/link" ]; then
|
|
|
|
cat $i/lib/link >> target/link.build
|
|
|
|
fi
|
2018-09-07 14:08:06 +01:00
|
|
|
done
|
2020-03-12 11:29:31 +00:00
|
|
|
|
|
|
|
# Remove duplicate linker flags from the build dependencies.
|
|
|
|
if [[ -e target/link.build ]]; then
|
2020-03-13 10:21:07 +00:00
|
|
|
sort -uo target/link.build target/link.build
|
2018-09-07 14:08:06 +01:00
|
|
|
fi
|
2020-03-12 11:29:31 +00:00
|
|
|
|
|
|
|
# Remove duplicate linker flags from the dependencies.
|
2020-03-13 10:21:07 +00:00
|
|
|
sort -uo target/link target/link
|
2020-03-12 11:29:31 +00:00
|
|
|
tr '\n' ' ' < target/link > target/link_
|
|
|
|
|
|
|
|
# Remove duplicate linker flags from the that are written
|
|
|
|
# to the derivation's output.
|
2020-03-13 10:21:07 +00:00
|
|
|
sort -uo target/link.final target/link.final
|
2020-03-12 11:29:31 +00:00
|
|
|
|
2018-09-07 14:08:06 +01:00
|
|
|
EXTRA_BUILD=""
|
|
|
|
BUILD_OUT_DIR=""
|
|
|
|
export CARGO_PKG_NAME=${crateName}
|
|
|
|
export CARGO_PKG_VERSION=${crateVersion}
|
|
|
|
export CARGO_PKG_AUTHORS="${authors}"
|
2019-02-25 15:22:03 +00:00
|
|
|
export CARGO_PKG_DESCRIPTION="${crateDescription}"
|
2018-09-07 14:08:06 +01:00
|
|
|
|
2020-09-29 06:15:06 +01:00
|
|
|
export CARGO_CFG_TARGET_ARCH=${rust.toTargetArch stdenv.hostPlatform}
|
|
|
|
export CARGO_CFG_TARGET_OS=${rust.toTargetOs stdenv.hostPlatform}
|
2018-09-07 14:08:06 +01:00
|
|
|
export CARGO_CFG_TARGET_FAMILY="unix"
|
|
|
|
export CARGO_CFG_UNIX=1
|
|
|
|
export CARGO_CFG_TARGET_ENV="gnu"
|
|
|
|
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}
|
|
|
|
|
2019-03-03 11:02:26 +00:00
|
|
|
export CARGO_MANIFEST_DIR=$(pwd)
|
2018-09-07 14:08:06 +01:00
|
|
|
export DEBUG="${toString (!release)}"
|
|
|
|
export OPT_LEVEL="${toString optLevel}"
|
2020-10-14 04:37:29 +01:00
|
|
|
export TARGET="${rust.toRustTargetSpec stdenv.hostPlatform}"
|
|
|
|
export HOST="${rust.toRustTargetSpec stdenv.buildPlatform}"
|
2018-09-07 14:08:06 +01:00
|
|
|
export PROFILE=${if release then "release" else "debug"}
|
|
|
|
export OUT_DIR=$(pwd)/target/build/${crateName}.out
|
2019-12-11 20:53:42 +00:00
|
|
|
export CARGO_PKG_VERSION_MAJOR=${lib.elemAt version 0}
|
|
|
|
export CARGO_PKG_VERSION_MINOR=${lib.elemAt version 1}
|
|
|
|
export CARGO_PKG_VERSION_PATCH=${lib.elemAt version 2}
|
2019-04-23 22:40:29 +01:00
|
|
|
export CARGO_PKG_VERSION_PRE="${versionPre}"
|
|
|
|
export CARGO_PKG_HOMEPAGE="${crateHomepage}"
|
2020-12-03 11:44:12 +00:00
|
|
|
export NUM_JOBS=$NIX_BUILD_CORES
|
2018-11-16 12:12:59 +00:00
|
|
|
export RUSTC="rustc"
|
2018-12-05 01:21:36 +00:00
|
|
|
export RUSTDOC="rustdoc"
|
2018-09-07 14:08:06 +01:00
|
|
|
|
|
|
|
BUILD=""
|
|
|
|
if [[ ! -z "${build}" ]] ; then
|
|
|
|
BUILD=${build}
|
|
|
|
elif [[ -e "build.rs" ]]; then
|
|
|
|
BUILD="build.rs"
|
|
|
|
fi
|
2020-03-12 11:29:31 +00:00
|
|
|
|
|
|
|
# Compile and run the build script, when available.
|
2018-09-07 14:08:06 +01:00
|
|
|
if [[ ! -z "$BUILD" ]] ; then
|
|
|
|
echo_build_heading "$BUILD" ${libName}
|
|
|
|
mkdir -p target/build/${crateName}
|
|
|
|
EXTRA_BUILD_FLAGS=""
|
|
|
|
if [ -e target/link.build ]; then
|
2020-03-12 11:29:31 +00:00
|
|
|
EXTRA_BUILD_FLAGS="$EXTRA_BUILD_FLAGS $(tr '\n' ' ' < target/link.build)"
|
2018-09-07 14:08:06 +01:00
|
|
|
fi
|
|
|
|
noisily rustc --crate-name build_script_build $BUILD --crate-type bin ${rustcOpts} \
|
2020-06-12 16:09:45 +01:00
|
|
|
${mkRustcFeatureArgs crateFeatures} --out-dir target/build/${crateName} --emit=dep-info,link \
|
2018-09-07 14:08:06 +01:00
|
|
|
-L dependency=target/buildDeps ${buildDeps} --cap-lints allow $EXTRA_BUILD_FLAGS --color ${colors}
|
|
|
|
|
|
|
|
mkdir -p target/build/${crateName}.out
|
|
|
|
export RUST_BACKTRACE=1
|
|
|
|
BUILD_OUT_DIR="-L $OUT_DIR"
|
|
|
|
mkdir -p $OUT_DIR
|
2020-06-12 16:09:45 +01:00
|
|
|
|
|
|
|
(
|
|
|
|
# Features should be set as environment variable for build scripts:
|
|
|
|
# https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
|
|
|
|
for feature in ${envFeatures}; do
|
|
|
|
export CARGO_FEATURE_$feature=1
|
|
|
|
done
|
|
|
|
|
|
|
|
target/build/${crateName}/build_script_build > target/build/${crateName}.opt
|
|
|
|
)
|
|
|
|
|
2018-09-07 14:08:06 +01:00
|
|
|
set +e
|
|
|
|
EXTRA_BUILD=$(sed -n "s/^cargo:rustc-flags=\(.*\)/\1/p" target/build/${crateName}.opt | tr '\n' ' ' | sort -u)
|
|
|
|
EXTRA_FEATURES=$(sed -n "s/^cargo:rustc-cfg=\(.*\)/--cfg \1/p" target/build/${crateName}.opt | tr '\n' ' ')
|
2020-03-25 12:00:00 +00:00
|
|
|
EXTRA_LINK=$(sed -n "s/^cargo:rustc-link-lib=\(.*\)/\1/p" target/build/${crateName}.opt | tr '\n' ' ')
|
2018-09-07 14:08:06 +01:00
|
|
|
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
|
|
|
|
|
2020-05-26 19:52:18 +01:00
|
|
|
CRATENAME=$(echo ${crateName} | sed -e "s/\(.*\)-sys$/\U\1/" -e "s/-/_/g")
|
2018-09-07 14:08:06 +01:00
|
|
|
grep -P "^cargo:(?!(rustc-|warning=|rerun-if-changed=|rerun-if-env-changed))" target/build/${crateName}.opt \
|
2020-05-26 19:52:18 +01:00
|
|
|
| awk -F= "/^cargo:/ { sub(/^cargo:/, \"\", \$1); gsub(/-/, \"_\", \$1); print \"export \" toupper(\"DEP_$(echo $CRATENAME)_\" \$1) \"=\" \$2 }" > target/env
|
2018-09-07 14:08:06 +01:00
|
|
|
set -e
|
|
|
|
fi
|
|
|
|
runHook postConfigure
|
|
|
|
''
|