···
{ lib, buildPlatform, stdenv, defaultCrateOverrides, fetchCrate, ncurses, rustc }:
9
-
let buildCrate = { crateName, crateVersion, crateAuthors, buildDependencies,
10
-
dependencies, completeDeps, completeBuildDeps,
11
-
crateFeatures, libName, build, release, libPath,
12
-
crateType, metadata, crateBin, finalBins,
15
-
let depsDir = lib.concatStringsSep " " dependencies;
16
-
completeDepsDir = lib.concatStringsSep " " completeDeps;
17
-
completeBuildDepsDir = lib.concatStringsSep " " completeBuildDeps;
18
-
makeDeps = dependencies:
19
-
(lib.concatMapStringsSep " " (dep:
20
-
let extern = lib.strings.replaceStrings ["-"] ["_"] dep.libName; in
21
-
(if dep.crateType == "lib" then
22
-
" --extern ${extern}=${dep.out}/lib/lib${extern}-${dep.metadata}.rlib"
24
-
" --extern ${extern}=${dep.out}/lib/lib${extern}-${dep.metadata}${buildPlatform.extensions.sharedLibrary}")
26
-
deps = makeDeps dependencies;
27
-
buildDeps = makeDeps buildDependencies;
28
-
optLevel = if release then 3 else 0;
29
-
rustcOpts = (if release then "-C opt-level=3" else "-C debuginfo=2");
30
-
rustcMeta = "-C metadata=${metadata} -C extra-filename=-${metadata}";
31
-
version_ = lib.splitString "-" crateVersion;
32
-
versionPre = if lib.tail version_ == [] then "" else builtins.elemAt version_ 1;
33
-
version = lib.splitString "." (lib.head version_);
34
-
authors = lib.concatStringsSep ":" crateAuthors;
40
-
if [[ "${colors}" -eq "always" ]]; then
41
-
norm="$(printf '\033[0m')" #returns to "normal"
42
-
bold="$(printf '\033[0;1m')" #set bold
43
-
green="$(printf '\033[0;32m')" #set green
44
-
boldgreen="$(printf '\033[0;1;32m')" #set bold, and set green.
9
+
let makeDeps = dependencies:
10
+
(lib.concatMapStringsSep " " (dep:
11
+
let extern = lib.strings.replaceStrings ["-"] ["_"] dep.libName; in
12
+
(if dep.crateType == "lib" then
13
+
" --extern ${extern}=${dep.out}/lib/lib${extern}-${dep.metadata}.rlib"
15
+
" --extern ${extern}=${dep.out}/lib/lib${extern}-${dep.metadata}${buildPlatform.extensions.sharedLibrary}")
18
+
echo_build_heading = colors: ''
···
echo "$start""Building $1 ($2)""$end"
33
+
noisily = colors: verbose: ''
···
50
+
{ crateName, crateVersion, crateAuthors, build, libName, crateFeatures, colors, libPath, release, buildDependencies, completeDeps, completeBuildDeps, verbose, dependencies }:
51
+
let version_ = lib.splitString "-" crateVersion;
52
+
versionPre = if lib.tail version_ == [] then "" else builtins.elemAt version_ 1;
53
+
version = lib.splitString "." (lib.head version_);
54
+
rustcOpts = (if release then "-C opt-level=3" else "-C debuginfo=2");
55
+
buildDeps = makeDeps buildDependencies;
56
+
authors = lib.concatStringsSep ":" crateAuthors;
57
+
optLevel = if release then 3 else 0;
58
+
completeDepsDir = lib.concatStringsSep " " completeDeps;
59
+
completeBuildDepsDir = lib.concatStringsSep " " completeBuildDeps;
61
+
runHook preConfigure
62
+
${echo_build_heading colors}
63
+
${noisily colors verbose}
76
-
# $1 is the nix-store path of a dependency
65
+
# $1 is the nix-store path of a dependency
66
+
# $2 is the target path
79
-
if [ ! -z $2 ]; then
80
-
if [ "$2" = "--buildDep" ]; then
81
-
dest=target/buildDeps
84
-
ln -s -f $i/lib/*.rlib $dest #*/
85
-
ln -s -f $i/lib/*.so $i/lib/*.dylib $dest #*/
68
+
ln -s -f $i/lib/*.rlib $2 #*/
69
+
ln -s -f $i/lib/*.so $i/lib/*.dylib $2 #*/
if [ -e "$i/lib/link" ]; then
cat $i/lib/link >> target/link
cat $i/lib/link >> target/link.final
···
97
-
echo_build_heading $lib_src ${libName}
99
-
noisily rustc --crate-name $CRATE_NAME $lib_src --crate-type ${crateType} \
100
-
${rustcOpts} ${rustcMeta} ${crateFeatures} --out-dir target/lib \
101
-
--emit=dep-info,link -L dependency=target/deps ${deps} --cap-lints allow \
102
-
$BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES --color ${colors}
104
-
EXTRA_LIB=" --extern $CRATE_NAME=target/lib/lib$CRATE_NAME-${metadata}.rlib"
105
-
if [ -e target/deps/lib$CRATE_NAME-${metadata}${buildPlatform.extensions.sharedLibrary} ]; then
106
-
EXTRA_LIB="$EXTRA_LIB --extern $CRATE_NAME=target/lib/lib$CRATE_NAME-${metadata}${buildPlatform.extensions.sharedLibrary}"
112
-
crate_name_=$(echo $crate_name | sed -e "s/-/_/g")
114
-
if [[ ! -z $2 ]]; then
117
-
echo_build_heading $@
118
-
noisily rustc --crate-name $crate_name_ $main_file --crate-type bin ${rustcOpts}\
119
-
${crateFeatures} --out-dir target/bin --emit=dep-info,link -L dependency=target/deps \
120
-
$LINK ${deps}$EXTRA_LIB --cap-lints allow \
121
-
$BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES --color ${colors}
122
-
if [ "$crate_name_" -ne "$crate_name" ]; then
123
-
mv target/bin/$crate_name_ target/bin/$crate_name
mkdir -p target/{deps,lib,build,buildDeps}
for i in ${completeDepsDir}; do
131
-
symlink_dependency $i
82
+
symlink_dependency $i target/deps
for i in ${completeBuildDepsDir}; do
134
-
symlink_dependency $i --buildDep
85
+
symlink_dependency $i target/buildDeps
136
-
if [ -e target/link ]; then
87
+
if [[ -e target/link ]]; then
sort -u target/link > target/link.sorted
mv target/link.sorted target/link
sort -u target/link.final > target/link.final.sorted
···
export CARGO_PKG_NAME=${crateName}
export CARGO_PKG_VERSION=${crateVersion}
export CARGO_PKG_AUTHORS="${authors}"
export CARGO_CFG_TARGET_ARCH=${buildPlatform.parsed.cpu.name}
export CARGO_CFG_TARGET_OS=${buildPlatform.parsed.kernel.name}
102
+
export CARGO_CFG_TARGET_FAMILY="unix"
103
+
export CARGO_CFG_UNIX=1
export CARGO_CFG_TARGET_ENV="gnu"
105
+
export CARGO_CFG_TARGET_ENDIAN=${if buildPlatform.parsed.cpu.significantByte.name == "littleEndian" then "little" else "big"}
106
+
export CARGO_CFG_TARGET_POINTER_WIDTH=${toString buildPlatform.parsed.cpu.bits}
107
+
export CARGO_CFG_TARGET_VENDOR=${buildPlatform.parsed.vendor.name}
export CARGO_MANIFEST_DIR="."
export DEBUG="${toString (!release)}"
export OPT_LEVEL="${toString optLevel}"
···
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}
162
-
if [ -n "${versionPre}" ]; then
119
+
if [[ -n "${versionPre}" ]]; then
export CARGO_PKG_VERSION_PRE="${versionPre}"
···
EXTRA_BUILD_FLAGS="$EXTRA_BUILD_FLAGS $(cat target/link.build)"
noisily rustc --crate-name build_script_build $BUILD --crate-type bin ${rustcOpts} \
183
-
${crateFeatures} --out-dir target/build/${crateName} --emit=dep-info,link \
184
-
-L dependency=target/buildDeps ${buildDeps} --cap-lints allow $EXTRA_BUILD_FLAGS --color ${colors}
140
+
${crateFeatures} --out-dir target/build/${crateName} --emit=dep-info,link \
141
+
-L dependency=target/buildDeps ${buildDeps} --cap-lints allow $EXTRA_BUILD_FLAGS --color ${colors}
mkdir -p target/build/${crateName}.out
···
| sed -e "s/cargo:\([^=]*\)=\(.*\)/export DEP_$(echo $CRATENAME)_\U\1\E=\2/" > target/env
201
-
if [ -n "$(ls target/build/${crateName}.out)" ]; then
158
+
if [[ -n "$(ls target/build/${crateName}.out)" ]]; then
203
-
if [ -e "${libPath}" ] ; then
160
+
if [[ -e "${libPath}" ]]; then
cp -r target/build/${crateName}.out/* $(dirname ${libPath}) #*/
cp -r target/build/${crateName}.out/* src #*/
167
+
runHook postConfigure
170
+
buildCrate = { crateName, crateVersion, crateAuthors,
171
+
dependencies, completeDeps, completeBuildDeps,
172
+
crateFeatures, libName, build, release, libPath,
173
+
crateType, metadata, crateBin, finalBins,
177
+
let depsDir = lib.concatStringsSep " " dependencies;
178
+
completeDepsDir = lib.concatStringsSep " " completeDeps;
179
+
completeBuildDepsDir = lib.concatStringsSep " " completeBuildDeps;
180
+
deps = makeDeps dependencies;
181
+
optLevel = if release then 3 else 0;
183
+
lib.lists.foldl' (opts: opt: opts + " " + opt)
184
+
(if release then "-C opt-level=3" else "-C debuginfo=2")
186
+
rustcMeta = "-C metadata=${metadata} -C extra-filename=-${metadata}";
187
+
version_ = lib.splitString "-" crateVersion;
188
+
versionPre = if lib.tail version_ == [] then "" else builtins.elemAt version_ 1;
189
+
version = lib.splitString "." (lib.head version_);
190
+
authors = lib.concatStringsSep ":" crateAuthors;
197
+
if [[ "${colors}" -eq "always" ]]; then
198
+
norm="$(printf '\033[0m')" #returns to "normal"
199
+
bold="$(printf '\033[0;1m')" #set bold
200
+
green="$(printf '\033[0;32m')" #set green
201
+
boldgreen="$(printf '\033[0;1;32m')" #set bold, and set green.
203
+
${echo_build_heading colors}
204
+
${noisily colors verbose}
208
+
echo_build_heading $lib_src ${libName}
210
+
noisily rustc --crate-name $CRATE_NAME $lib_src --crate-type ${crateType} \
211
+
${rustcOpts} ${rustcMeta} ${crateFeatures} --out-dir target/lib \
212
+
--emit=dep-info,link -L dependency=target/deps ${deps} --cap-lints allow \
213
+
$BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES --color ${colors}
215
+
EXTRA_LIB=" --extern $CRATE_NAME=target/lib/lib$CRATE_NAME-${metadata}.rlib"
216
+
if [ -e target/deps/lib$CRATE_NAME-${metadata}${buildPlatform.extensions.sharedLibrary} ]; then
217
+
EXTRA_LIB="$EXTRA_LIB --extern $CRATE_NAME=target/lib/lib$CRATE_NAME-${metadata}${buildPlatform.extensions.sharedLibrary}"
223
+
crate_name_=$(echo $crate_name | sed -e "s/-/_/g")
225
+
if [[ ! -z $2 ]]; then
228
+
echo_build_heading $@
229
+
noisily rustc --crate-name $crate_name_ $main_file --crate-type bin ${rustcOpts}\
230
+
${crateFeatures} --out-dir target/bin --emit=dep-info,link -L dependency=target/deps \
231
+
$LINK ${deps}$EXTRA_LIB --cap-lints allow \
232
+
$BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES --color ${colors}
233
+
if [ "$crate_name_" -ne "$crate_name" ]; then
234
+
mv target/bin/$crate_name_ target/bin/$crate_name
CRATE_NAME=$(echo ${libName} | sed -e "s/-/_/g")
214
-
if [ -e target/link_ ]; then
242
+
if [[ -e target/link_ ]]; then
EXTRA_BUILD="$(cat target/link_) $EXTRA_BUILD"
218
-
if [ -e "${libPath}" ] ; then
246
+
if [[ -e "${libPath}" ]]; then
220
-
elif [ -e src/lib.rs ] ; then
248
+
elif [[ -e src/lib.rs ]]; then
222
-
elif [ -e src/${libName}.rs ] ; then
250
+
elif [[ -e src/${libName}.rs ]]; then
build_lib src/${libName}.rs
echo "$EXTRA_LINK_SEARCH" | while read i; do
227
-
if [ ! -z "$i" ]; then
255
+
if [[ ! -z "$i" ]]; then
echo "-L $lib" >> target/link
L=$(echo $lib | sed -e "s#$(pwd)/target/build#$out/lib#")
···
echo "$EXTRA_LINK" | while read i; do
236
-
if [ ! -z "$i" ]; then
264
+
if [[ ! -z "$i" ]]; then
echo "-l $lib" >> target/link
echo "-l $lib" >> target/link.final
···
244
-
if [ -e target/link ]; then
272
+
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
···
echo "${crateBin}" | sed -n 1'p' | tr ',' '\n' | while read BIN; do
256
-
if [ ! -z "$BIN" ]; then
284
+
if [[ ! -z "$BIN" ]]; then
···
# Remove object files to avoid "wrong ELF type"
find target -type f -name "*.o" -print0 | xargs -0 rm -f
298
+
'' + finalBins + ''
273
-
installCrate = crateName: ''
302
+
installCrate = crateName: metadata: ''
275
-
if [ -s target/env ]; then
305
+
if [[ -s target/env ]]; then
278
-
if [ -s target/link.final ]; then
308
+
if [[ -s target/link.final ]]; then
cp target/link.final $out/lib/link
282
-
if [ "$(ls -A target/lib)" ]; then
312
+
if [[ "$(ls -A target/lib)" ]]; then
cp target/lib/* $out/lib #*/
315
+
for lib in $out/lib/*.so $out/lib/*.dylib; do #*/
316
+
ln -s $lib $(echo $lib | sed -e "s/-${metadata}//")
286
-
if [ "$(ls -A target/build)" ]; then # */
319
+
if [[ "$(ls -A target/build)" ]]; then # */
cp -r target/build/* $out/lib # */
290
-
if [ "$(ls -A target/bin)" ]; then
323
+
if [[ "$(ls -A target/bin)" ]]; then
cp -P target/bin/* $out/bin # */
···
298
-
crate_: lib.makeOverridable ({ rust, release, verbose, features, buildInputs, crateOverrides }:
331
+
crate_: lib.makeOverridable ({ rust, release, verbose, features, buildInputs, crateOverrides,
332
+
dependencies, buildDependencies,
334
+
preUnpack, postUnpack, prePatch, patches, postPatch,
335
+
preConfigure, postConfigure, preBuild, postBuild, preInstall, postInstall }:
let crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: {}) crateOverrides crate_);
338
+
release_ = release;
339
+
dependencies_ = dependencies;
340
+
buildDependencies_ = buildDependencies;
"src" "buildInputs" "crateBin" "crateLib" "libName" "libPath"
"buildDependencies" "dependencies" "features"
···
stdenv.mkDerivation (rec {
inherit (crate) crateName;
352
+
inherit preUnpack postUnpack prePatch patches postPatch preConfigure postConfigure preBuild postBuild preInstall postInstall;
src = if lib.hasAttr "src" crate then
···
(dep: dep.override { rust = rust; release = release; verbose = verbose; crateOverrides = crateOverrides; })
322
-
(crate.dependencies or []);
(dep: dep.override { rust = rust; release = release; verbose = verbose; crateOverrides = crateOverrides; })
327
-
(crate.buildDependencies or []);
368
+
buildDependencies_;
completeDeps = lib.lists.unique (dependencies ++ lib.lists.concatMap (dep: dep.completeDeps) dependencies);
completeBuildDeps = lib.lists.unique (
···
libName = if crate ? libName then crate.libName else crate.crateName;
libPath = if crate ? libPath then crate.libPath else "";
342
-
metadata = builtins.substring 0 10 (builtins.hashString "sha256" (crateName + "-" + crateVersion));
383
+
depsMetadata = builtins.foldl' (str: dep: str + dep.metadata) "" (dependencies ++ buildDependencies);
384
+
metadata = builtins.substring 0 10 (builtins.hashString "sha256" (crateName + "-" + crateVersion + "___" + toString crateFeatures + "___" + depsMetadata ));
crateBin = if crate ? crateBin then
builtins.foldl' (bins: bin:
···
crateAuthors = if crate ? authors && lib.isList crate.authors then crate.authors else [];
if lib.attrByPath ["procMacro"] false crate then "proc-macro" else
373
-
if lib.attrByPath ["plugin"] false crate then "dylib" else "lib";
415
+
if lib.attrByPath ["plugin"] false crate then "dylib" else
416
+
if crate ? type then crate.type else "lib";
colors = lib.attrByPath [ "colors" ] "always" crate;
375
-
buildPhase = buildCrate {
418
+
configurePhase = configureCrate {
inherit crateName dependencies buildDependencies completeDeps completeBuildDeps
420
+
crateFeatures libName build release libPath crateVersion
421
+
crateAuthors verbose colors;
423
+
extraRustcOpts = if crate ? extraRustcOpts then crate.extraRustcOpts else [];
424
+
buildPhase = buildCrate {
425
+
inherit crateName dependencies completeDeps completeBuildDeps
crateFeatures libName build release libPath crateType crateVersion
378
-
crateAuthors metadata crateBin finalBins verbose colors;
427
+
crateAuthors metadata crateBin finalBins verbose colors extraRustcOpts;
380
-
installPhase = installCrate crateName;
429
+
installPhase = installCrate crateName metadata;
382
-
} // extraDerivationAttrs)) {
431
+
} // extraDerivationAttrs
434
+
release = crate_.release or true;
435
+
verbose = crate_.verbose or true;
436
+
extraRustcOpts = [];
crateOverrides = defaultCrateOverrides;
440
+
preUnpack = crate_.preUnpack or "";
441
+
postUnpack = crate_.postUnpack or "";
442
+
prePatch = crate_.prePatch or "";
443
+
patches = crate_.patches or [];
444
+
postPatch = crate_.postPatch or "";
445
+
preConfigure = crate_.preConfigure or "";
446
+
postConfigure = crate_.postConfigure or "";
447
+
preBuild = crate_.preBuild or "";
448
+
postBuild = crate_.postBuild or "";
449
+
preInstall = crate_.preInstall or "";
450
+
postInstall = crate_.postInstall or "";
451
+
dependencies = crate_.dependencies or [];
452
+
buildDependencies = crate_.buildDependencies or [];