* Rewrote nix-build in Perl, since sh is just too limited (turns out

that arrays are a bash extension, so it didn't work on FreeBSD).
  Also fixes NIX-8 (readlink(1) dependency).
This commit is contained in:
Eelco Dolstra 2006-03-03 14:15:02 +00:00
parent 78d84f5631
commit 2d54312f87

View file

@ -1,78 +1,87 @@
#! @shell@ -e
#! @perl@ -w -I@libexecdir@/nix
nixExpr=$1
extraArgs=
extraInstArgs=
addDrvLink=0
addOutLink=1
use strict;
trap 'rm -f ./.nix-build-tmp-*' EXIT
my $addDrvLink = 0;
my $addOutLink = 1;
my @instArgs = ();
my @buildArgs = ();
my @exprs = ();
# Process the arguments.
args=("$@")
for ((i = 0; i < ${#args[*]}; )); do
arg=${args[$i]}
i=$((i + 1))
case "$arg" in
END {
foreach my $fn (glob ".nix-build-tmp-*") {
unlink $fn;
}
}
--help)
echo "syntax: $0 [NIX-EXPR...]" >&2
exit 0
;;
sub intHandler {
exit 1;
}
--add-drv-link)
addDrvLink=1
;;
$SIG{'INT'} = 'intHandler';
--no-link)
addOutLink=0
;;
--attr|-A)
arg2=${args[$i]}
i=$((i + 1))
extraInstArgs="$extraInstArgs $arg $arg2"
;;
for (my $n = 0; $n < scalar @ARGV; $n++) {
my $arg = $ARGV[$n];
-*)
extraArgs="$extraArgs $arg"
;;
if ($arg eq "--help") {
print STDERR "Usage: nix-build [OPTION]... [FILE]...\n";
exit 0;
}
*)
exprs="$exprs $arg"
;;
esac
done
elsif ($arg eq "--add-drv-link") {
$addDrvLink = 1;
}
if test -z "$exprs"; then
exprs="./default.nix"
fi
elsif ($arg eq "--no-out-link" or $arg eq "--no-link") {
$addOutLink = 1;
}
# Process the specified Nix expressions.
for i in $exprs; do
elsif ($arg eq "--attr" or $arg eq "-A") {
$n++;
die "$0: `--attr' requires an argument\n" unless $n < scalar @ARGV;
push @instArgs, ("--attr", $ARGV[$n]);
}
# Instantiate the Nix expression.
prefix=
if test "$addDrvLink" = 0; then prefix=.nix-build-tmp-; fi
storeExprs=$(@bindir@/nix-instantiate \
--add-root ./${prefix}derivation --indirect \
$extraInstArgs "$i")
elsif (substr($arg, 0, 1) eq "-") {
push @buildArgs, $arg;
}
for j in $storeExprs; do
echo "store expression is $(readlink "$j")" >&2
done
else {
push @exprs, $arg;
}
}
# Build the resulting store derivation.
prefix=
if test "$addOutLink" = 0; then prefix=.nix-build-tmp-; fi
outPaths=$(@bindir@/nix-store \
--add-root ./${prefix}result --indirect \
-rv $extraArgs $storeExprs)
@exprs = ("./default.nix") if scalar @exprs == 0;
for j in $outPaths; do
echo "$(readlink "$j")"
done
done
foreach my $expr (@exprs) {
# Instantiate.
my $prefix = "";
$prefix = ".nix-build-tmp-" if !$addDrvLink;
my $drvPaths = `@bindir@/nix-instantiate --add-root ./${prefix}derivation --indirect @instArgs "$expr"`;
my @drvPaths = split ' ', $drvPaths;
foreach my $drvPath (@drvPaths) {
my $target = readlink $drvPath;
print STDERR "store derivation is $target\n";
}
# Build.
$prefix = "";
$prefix = ".nix-build-tmp-" if !$addOutLink;
my $outPaths = `@bindir@/nix-store --add-root ./${prefix}result --indirect -rv @buildArgs @drvPaths`;
my @outPaths = split ' ', $outPaths;
foreach my $outPath (@outPaths) {
my $target = readlink $outPath;
print "$target\n";
}
}