Compare commits

..
3 Commits
Author SHA1 Message Date
Eelco Dolstra 29fd60131b * Add a primop to return the context of a string. 2010-01-31 20:54:44 +00:00
Eelco Dolstra 6d1abdc6d9 * In string contexts containing derivations
(e.g. "${somePkg}/bin/foo"), store the original derivation
  expression in the context.
2010-01-31 20:08:57 +00:00
Eelco Dolstra 25fc95d56d 2010-01-31 20:06:58 +00:00
199 changed files with 6106 additions and 7202 deletions

No files matched your search

-262
View File
@@ -1,262 +0,0 @@
# START "git svn show-ignore"
# /
/Makefile
/Makefile.in
/aclocal.m4
/autom4te.cache
/config.*
/configure
/nix.spec
/stamp-h1
/svn-revision
/NEWS
/libtool
# /config/
/config/config.guess
/config/config.sub
/config/depcomp
/config/install-sh
/config/missing
/config/mkinstalldirs
/config/ltmain.sh
# /corepkgs/
/corepkgs/Makefile
/corepkgs/Makefile.in
# /corepkgs/buildenv/
/corepkgs/buildenv/Makefile.in
/corepkgs/buildenv/Makefile
/corepkgs/buildenv/builder.pl
# /corepkgs/channels/
/corepkgs/channels/Makefile.in
/corepkgs/channels/Makefile
/corepkgs/channels/unpack.sh
# /corepkgs/nar/
/corepkgs/nar/Makefile
/corepkgs/nar/Makefile.in
/corepkgs/nar/nar.sh
/corepkgs/nar/unnar.sh
# /doc/
/doc/Makefile
/doc/Makefile.in
# /doc/manual/
/doc/manual/Makefile
/doc/manual/Makefile.in
/doc/manual/manual.html
/doc/manual/manual.is-valid
/doc/manual/*.1
/doc/manual/*.8
/doc/manual/images
/doc/manual/version.txt
/doc/manual/NEWS.html
/doc/manual/NEWS.txt
# /externals/
/externals/Makefile
/externals/Makefile.in
/externals/aterm-*
/externals/have-aterm
/externals/build-aterm
/externals/inst-aterm
/externals/bzip2-*
/externals/have-bzip2
/externals/build-bzip2
/externals/inst-bzip2
# /make/examples/aterm/
/make/examples/aterm/result*
# /make/examples/aterm/aterm/
/make/examples/aterm/aterm/*
# /make/examples/aterm/test/
/make/examples/aterm/test/*
# /misc/
/misc/Makefile.in
/misc/Makefile
# /misc/emacs/
/misc/emacs/Makefile.in
/misc/emacs/Makefile
# /scripts/
/scripts/Makefile
/scripts/Makefile.in
/scripts/nix-profile.sh
/scripts/nix-pull
/scripts/nix-push
/scripts/nix-switch
/scripts/nix-collect-garbage
/scripts/nix-prefetch-url
/scripts/nix-install-package
/scripts/nix-channel
/scripts/nix-build
/scripts/nix-copy-closure
/scripts/readmanifest.pm
/scripts/readconfig.pm
/scripts/download-using-manifests.pl
/scripts/copy-from-other-stores.pl
/scripts/generate-patches.pl
/scripts/find-runtime-roots.pl
/scripts/build-remote.pl
# /src/
/src/Makefile
/src/Makefile.in
# /src/bin2c/
/src/bin2c/Makefile.in
/src/bin2c/Makefile
/src/bin2c/bin2c
/src/bin2c/.deps
/src/bin2c/.libs
# /src/boost/
/src/boost/Makefile
/src/boost/Makefile.in
# /src/boost/format/
/src/boost/format/Makefile
/src/boost/format/Makefile.in
/src/boost/format/.deps
/src/boost/format/libformat.a
/src/boost/format/.libs
# /src/bsdiff-4.3/
/src/bsdiff-4.3/Makefile
/src/bsdiff-4.3/Makefile.in
/src/bsdiff-4.3/bsdiff
/src/bsdiff-4.3/bspatch
/src/bsdiff-4.3/.deps
/src/bsdiff-4.3/.libs
# /src/libexpr/
/src/libexpr/Makefile
/src/libexpr/Makefile.in
/src/libexpr/.deps
/src/libexpr/libexpr.a
/src/libexpr/lexer-tab.cc
/src/libexpr/lexer-tab.hh
/src/libexpr/parser-tab.cc
/src/libexpr/parser-tab.hh
/src/libexpr/parser-tab.output
/src/libexpr/nixexpr-ast.hh
/src/libexpr/nixexpr-ast.cc
/src/libexpr/.libs
/src/libexpr/nix.tbl
# /src/libmain/
/src/libmain/Makefile
/src/libmain/Makefile.in
/src/libmain/.deps
/src/libmain/libmain.a
/src/libmain/.libs
# /src/libstore/
/src/libstore/Makefile
/src/libstore/Makefile.in
/src/libstore/.deps
/src/libstore/libstore.a
/src/libstore/derivations-ast.cc
/src/libstore/derivations-ast.hh
/src/libstore/.libs
# /src/libutil/
/src/libutil/Makefile
/src/libutil/Makefile.in
/src/libutil/.deps
/src/libutil/libutil.a
/src/libutil/.libs
# /src/nix-env/
/src/nix-env/Makefile.in
/src/nix-env/Makefile
/src/nix-env/.deps
/src/nix-env/nix-env
/src/nix-env/help.txt.hh
/src/nix-env/.libs
# /src/nix-hash/
/src/nix-hash/Makefile
/src/nix-hash/Makefile.in
/src/nix-hash/.deps
/src/nix-hash/.libs
/src/nix-hash/nix-hash
/src/nix-hash/help.txt.hh
# /src/nix-instantiate/
/src/nix-instantiate/Makefile.in
/src/nix-instantiate/Makefile
/src/nix-instantiate/.deps
/src/nix-instantiate/nix-instantiate
/src/nix-instantiate/help.txt.hh
/src/nix-instantiate/.libs
# /src/nix-log2xml/
/src/nix-log2xml/Makefile.in
/src/nix-log2xml/Makefile
/src/nix-log2xml/.deps
/src/nix-log2xml/nix-log2xml
/src/nix-log2xml/test*.*
/src/nix-log2xml/.libs
/src/nix-log2xml/*.log
/src/nix-log2xml/*.xml
/src/nix-log2xml/*.html
# /src/nix-setuid-helper/
/src/nix-setuid-helper/Makefile.in
/src/nix-setuid-helper/Makefile
/src/nix-setuid-helper/.deps
/src/nix-setuid-helper/nix-setuid-helper
/src/nix-setuid-helper/help.txt.hh
/src/nix-setuid-helper/.libs
# /src/nix-store/
/src/nix-store/Makefile
/src/nix-store/Makefile.in
/src/nix-store/.deps
/src/nix-store/help.txt.hh
/src/nix-store/nix-store
/src/nix-store/.libs
# /src/nix-worker/
/src/nix-worker/Makefile.in
/src/nix-worker/Makefile
/src/nix-worker/.deps
/src/nix-worker/nix-worker
/src/nix-worker/help.txt.hh
/src/nix-worker/.libs
# /tests/
/tests/Makefile
/tests/Makefile.in
/tests/test-tmp
/tests/config.nix
/tests/common.sh
/tests/dummy
# /tests/lang/
/tests/lang/*.out
/tests/lang/*.out.xml
/tests/lang/*.ast
# END "git svn show-ignore"
*.lo
*.la
*.o
*~
# GNU Global
GPATH
GRTAGS
GSYMS
GTAGS
-2
View File
@@ -2,8 +2,6 @@ SUBDIRS = externals src scripts corepkgs doc misc tests
EXTRA_DIST = substitute.mk nix.spec nix.spec.in bootstrap.sh \ EXTRA_DIST = substitute.mk nix.spec nix.spec.in bootstrap.sh \
nix.conf.example NEWS version nix.conf.example NEWS version
pkginclude_HEADERS = config.h
include ./substitute.mk include ./substitute.mk
nix.spec: nix.spec.in nix.spec: nix.spec.in
-32
View File
@@ -115,35 +115,3 @@
fun:* fun:*
fun:AT_collect fun:AT_collect
} }
{
ATerm library conservatively scans for GC roots
Memcheck:Value4
fun:*
fun:*
fun:mark_phase
}
{
ATerm library conservatively scans for GC roots
Memcheck:Cond
fun:*
fun:*
fun:mark_phase
}
{
ATerm library conservatively scans for GC roots
Memcheck:Value4
fun:*
fun:*
fun:mark_phase_young
}
{
ATerm library conservatively scans for GC roots
Memcheck:Cond
fun:*
fun:*
fun:mark_phase_young
}
-1
View File
@@ -1,5 +1,4 @@
#! /bin/sh -e #! /bin/sh -e
rm -f aclocal.m4
mkdir -p config mkdir -p config
libtoolize --copy libtoolize --copy
aclocal aclocal
+54 -71
View File
@@ -10,8 +10,8 @@ AC_CANONICAL_HOST
# Construct a Nix system name (like "i686-linux"). # Construct a Nix system name (like "i686-linux").
AC_MSG_CHECKING([for the canonical Nix system name]) AC_MSG_CHECKING([for the canonical Nix system name])
cpu_name=$(uname -p | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ' 'abcdefghijklmnopqrstuvwxyz_') cpu_name=$(uname -p | tr 'A-Z ' 'a-z_')
machine_name=$(uname -m | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ' 'abcdefghijklmnopqrstuvwxyz_') machine_name=$(uname -m | tr 'A-Z ' 'a-z_')
case $machine_name in case $machine_name in
i*86) i*86)
@@ -30,7 +30,7 @@ case $machine_name in
;; ;;
esac esac
sys_name=$(uname -s | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ' 'abcdefghijklmnopqrstuvwxyz_') sys_name=$(uname -s | tr 'A-Z ' 'a-z_')
case $sys_name in case $sys_name in
cygwin*) cygwin*)
@@ -50,49 +50,48 @@ AC_DEFINE_UNQUOTED(SYSTEM, ["$system"], [platform identifier (`cpu-os')])
test "$localstatedir" = '${prefix}/var' && localstatedir=/nix/var test "$localstatedir" = '${prefix}/var' && localstatedir=/nix/var
# Windows-specific stuff. On Cygwin, dynamically linking against the # Whether to produce a statically linked binary. On Cygwin, this is
# ATerm DLL works, except that it requires the ATerm "lib" directory # the default: dynamically linking against the ATerm DLL does work,
# to be in $PATH, as Windows doesn't have anything like an RPATH # except that it requires the ATerm "lib" directory to be in $PATH, as
# embedded in executable. Since this is kind of annoying, we use # Windows doesn't have anything like an RPATH embedded in executable.
# static libraries for now. # Since this is kind of annoying, we use static libraries for now.
if test "$sys_name" = "cygwin"; then
AC_ARG_ENABLE(static-nix, AC_HELP_STRING([--enable-static-nix],
[produce statically linked binaries]),
static_nix=$enableval, static_nix=no)
if test "$sys_name" = cygwin; then
static_nix=yes
fi
if test "$static_nix" = yes; then
AC_DISABLE_SHARED AC_DISABLE_SHARED
AC_ENABLE_STATIC AC_ENABLE_STATIC
fi fi
# Windows-specific stuff.
if test "$sys_name" = "cygwin"; then
# We cannot delete open files.
AC_DEFINE(CANNOT_DELETE_OPEN_FILES, 1, [Whether it is impossible to delete open files.])
fi
# Solaris-specific stuff. # Solaris-specific stuff.
if test "$sys_name" = "sunos"; then if test "$sys_name" = "sunos"; then
# Solaris requires -lsocket -lnsl for network functions # Solaris requires -lsocket -lnsl for network functions
LIBS="-lsocket -lnsl $LIBS" ADDITIONAL_NETWORK_LIBS="-lsocket -lnsl"
AC_SUBST(ADDITIONAL_NETWORK_LIBS)
fi fi
AC_PROG_CC AC_PROG_CC
AC_PROG_CXX AC_PROG_CXX
# To build programs to be run in the build machine
if test "$CC_FOR_BUILD" = ""; then
if test "$cross_compiling" = "yes"; then
AC_CHECK_PROGS(CC_FOR_BUILD, gcc cc)
else
CC_FOR_BUILD="$CC"
fi
fi
AC_SUBST([CC_FOR_BUILD])
# We are going to use libtool. # We are going to use libtool.
AC_DISABLE_STATIC AC_DISABLE_STATIC
AC_ENABLE_SHARED AC_ENABLE_SHARED
AC_PROG_LIBTOOL AC_PROG_LIBTOOL
if test "$enable_shared" = yes; then
SUB_CONFIGURE_FLAGS="--enable-shared --disable-static"
else
SUB_CONFIGURE_FLAGS="--enable-static --disable-shared"
fi
AC_SUBST(SUB_CONFIGURE_FLAGS)
# Use 64-bit file system calls so that we can support files > 2 GiB. # Use 64-bit file system calls so that we can support files > 2 GiB.
AC_SYS_LARGEFILE AC_SYS_LARGEFILE
@@ -128,22 +127,11 @@ AC_CHECK_HEADERS([locale], [], [], [])
AC_LANG_POP(C++) AC_LANG_POP(C++)
# Check for <err.h>.
AC_CHECK_HEADER([err.h], [], [bsddiff_compat_include="-Icompat-include"])
AC_SUBST([bsddiff_compat_include])
# Check whether we have the personality() syscall, which allows us to # Check whether we have the personality() syscall, which allows us to
# do i686-linux builds on x86_64-linux machines. # do i686-linux builds on x86_64-linux machines.
AC_CHECK_HEADERS([sys/personality.h]) AC_CHECK_HEADERS([sys/personality.h])
# Check for tr1/unordered_set.
AC_LANG_PUSH(C++)
AC_CHECK_HEADERS([tr1/unordered_set], [], [], [])
AC_LANG_POP(C++)
AC_DEFUN([NEED_PROG], AC_DEFUN([NEED_PROG],
[ [
AC_PATH_PROG($1, $2) AC_PATH_PROG($1, $2)
@@ -161,7 +149,6 @@ AC_PATH_PROG(w3m, w3m, false)
AC_PATH_PROG(flex, flex, false) AC_PATH_PROG(flex, flex, false)
AC_PATH_PROG(bison, bison, false) AC_PATH_PROG(bison, bison, false)
NEED_PROG(perl, perl) NEED_PROG(perl, perl)
NEED_PROG(sed, sed)
NEED_PROG(tar, tar) NEED_PROG(tar, tar)
AC_PATH_PROG(dot, dot) AC_PATH_PROG(dot, dot)
AC_PATH_PROG(dblatex, dblatex) AC_PATH_PROG(dblatex, dblatex)
@@ -206,6 +193,23 @@ AC_ARG_WITH(store-dir, AC_HELP_STRING([--with-store-dir=PATH],
storedir=$withval, storedir='/nix/store') storedir=$withval, storedir='/nix/store')
AC_SUBST(storedir) AC_SUBST(storedir)
AC_ARG_WITH(aterm, AC_HELP_STRING([--with-aterm=PATH],
[prefix of CWI ATerm library]),
aterm=$withval, aterm=)
AM_CONDITIONAL(HAVE_ATERM, test -n "$aterm")
if test -z "$aterm"; then
aterm_lib='-L${top_builddir}/externals/inst-aterm/lib -lATerm'
aterm_include='-I${top_builddir}/externals/inst-aterm/include'
aterm_bin='${top_builddir}/externals/inst-aterm/bin'
else
aterm_lib="-L$aterm/lib -lATerm"
aterm_include="-I$aterm/include"
aterm_bin="$aterm/bin"
fi
AC_SUBST(aterm_lib)
AC_SUBST(aterm_include)
AC_SUBST(aterm_bin)
AC_ARG_WITH(openssl, AC_HELP_STRING([--with-openssl=PATH], AC_ARG_WITH(openssl, AC_HELP_STRING([--with-openssl=PATH],
[prefix of the OpenSSL library]), [prefix of the OpenSSL library]),
openssl=$withval, openssl=) openssl=$withval, openssl=)
@@ -221,8 +225,6 @@ AC_ARG_WITH(bzip2, AC_HELP_STRING([--with-bzip2=PATH],
[prefix of bzip2]), [prefix of bzip2]),
bzip2=$withval, bzip2=) bzip2=$withval, bzip2=)
AM_CONDITIONAL(HAVE_BZIP2, test -n "$bzip2") AM_CONDITIONAL(HAVE_BZIP2, test -n "$bzip2")
ATERM_VERSION=2.5
AC_SUBST(ATERM_VERSION)
if test -z "$bzip2"; then if test -z "$bzip2"; then
# Headers and libraries will be used from the temporary installation # Headers and libraries will be used from the temporary installation
# in externals/inst-bzip2. # in externals/inst-bzip2.
@@ -243,36 +245,8 @@ AC_SUBST(bzip2_include)
AC_SUBST(bzip2_bin) AC_SUBST(bzip2_bin)
AC_SUBST(bzip2_bin_test) AC_SUBST(bzip2_bin_test)
AC_ARG_WITH(sqlite, AC_HELP_STRING([--with-sqlite=PATH],
[prefix of SQLite]),
sqlite=$withval, sqlite=)
AM_CONDITIONAL(HAVE_SQLITE, test -n "$sqlite")
SQLITE_VERSION=3070500
AC_SUBST(SQLITE_VERSION)
if test -z "$sqlite"; then
sqlite_lib='${top_builddir}/externals/sqlite-autoconf-$(SQLITE_VERSION)/libsqlite3.la'
sqlite_include='-I${top_builddir}/externals/sqlite-autoconf-$(SQLITE_VERSION)'
sqlite_bin='${top_builddir}/externals/sqlite-autoconf-$(SQLITE_VERSION)'
else
sqlite_lib="-L$sqlite/lib -lsqlite3"
sqlite_include="-I$sqlite/include"
sqlite_bin="$sqlite/bin"
fi
AC_SUBST(sqlite_lib)
AC_SUBST(sqlite_include)
AC_SUBST(sqlite_bin)
# Whether to use the Boehm garbage collector. AC_CHECK_LIB(pthread, pthread_mutex_init)
AC_ARG_ENABLE(gc, AC_HELP_STRING([--enable-gc],
[enable garbage collection in the Nix expression evaluator (requires Boehm GC)]),
gc=$enableval, gc=)
if test -n "$gc"; then
PKG_CHECK_MODULES([BDW_GC], [bdw-gc])
boehmgc_lib="-L$boehmgc/lib -lgc"
CXXFLAGS="$BDW_GC_CFLAGS $CXXFLAGS"
AC_DEFINE(HAVE_BOEHMGC, 1, [Whether to use the Boehm garbage collector.])
fi
AC_SUBST(boehmgc_lib)
AC_ARG_ENABLE(init-state, AC_HELP_STRING([--disable-init-state], AC_ARG_ENABLE(init-state, AC_HELP_STRING([--disable-init-state],
@@ -286,7 +260,8 @@ AC_CHECK_FUNCS([setresuid setreuid lchown])
# Nice to have, but not essential. # Nice to have, but not essential.
AC_CHECK_FUNCS([strsignal posix_fallocate nanosleep]) AC_CHECK_FUNCS([strsignal])
AC_CHECK_FUNCS([posix_fallocate])
# This is needed if ATerm or bzip2 are static libraries, # This is needed if ATerm or bzip2 are static libraries,
@@ -296,6 +271,14 @@ if test "$(uname)" = "Darwin"; then
fi fi
if test "$static_nix" = yes; then
# `-all-static' has to be added at the end of configure, because
# the C compiler doesn't know about -all-static (it's filtered out
# by libtool, but configure doesn't use libtool).
LDFLAGS="-all-static $LDFLAGS"
fi
AM_CONFIG_HEADER([config.h]) AM_CONFIG_HEADER([config.h])
AC_CONFIG_FILES([Makefile AC_CONFIG_FILES([Makefile
externals/Makefile externals/Makefile
+2 -10
View File
@@ -29,18 +29,10 @@ sub createLinks {
$baseName =~ s/^.*\///g; # strip directory $baseName =~ s/^.*\///g; # strip directory
my $dstFile = "$dstDir/$baseName"; my $dstFile = "$dstDir/$baseName";
# The files below are special-cased so that they don't show up
# in user profiles, either because they are useless, or
# because they would cause pointless collisions (e.g., each
# Python package brings its own
# `$out/lib/pythonX.Y/site-packages/easy-install.pth'.)
# Urgh, hacky... # Urgh, hacky...
if ($srcFile =~ /\/propagated-build-inputs$/ || if ($srcFile =~ /\/propagated-build-inputs$/ ||
$srcFile =~ /\/nix-support$/ || $srcFile =~ /\/nix-support$/ ||
$srcFile =~ /\/perllocal.pod$/ || $srcFile =~ /\/perllocal.pod$/ ||
$srcFile =~ /\/easy-install.pth$/ ||
$srcFile =~ /\/site.py$/ ||
$srcFile =~ /\/site.pyc$/ ||
$srcFile =~ /\/info\/dir$/ || $srcFile =~ /\/info\/dir$/ ||
$srcFile =~ /\/log$/) $srcFile =~ /\/log$/)
{ {
@@ -168,4 +160,4 @@ while (scalar(keys %postponed) > 0) {
print STDERR "created $symlinks symlinks in user environment\n"; print STDERR "created $symlinks symlinks in user environment\n";
symlink($ENV{"manifest"}, "$out/manifest.nix") or die "cannot create manifest"; symlink($ENV{"manifest"}, "$out/manifest") or die "cannot create manifest";
-4
View File
@@ -11,8 +11,4 @@ derivation {
paths = derivations; paths = derivations;
active = map (x: if x ? meta && x.meta ? active then x.meta.active else "true") derivations; active = map (x: if x ? meta && x.meta ? active then x.meta.active else "true") derivations;
priority = map (x: if x ? meta && x.meta ? priority then x.meta.priority else "5") derivations; priority = map (x: if x ? meta && x.meta ? priority then x.meta.priority else "5") derivations;
# Building user environments remotely just causes huge amounts of
# network traffic, so don't do that.
preferLocalBuild = true;
} }
-3
View File
@@ -1,8 +1,5 @@
#! @shell@ -e #! @shell@ -e
# Cygwin compatibility hack: bunzip2 expects cygwin.dll in $PATH.
export PATH=@coreutils@
@coreutils@/mkdir $out @coreutils@/mkdir $out
@coreutils@/mkdir $out/tmp @coreutils@/mkdir $out/tmp
cd $out/tmp cd $out/tmp
+2
View File
@@ -7,6 +7,8 @@ dst=$out/tmp.nar.bz2
@bzip2@ < tmp > $dst @bzip2@ < tmp > $dst
@bindir@/nix-hash -vvvvv --flat --type $hashAlgo --base32 tmp > $out/nar-hash
@bindir@/nix-hash --flat --type $hashAlgo --base32 $dst > $out/narbz2-hash @bindir@/nix-hash --flat --type $hashAlgo --base32 $dst > $out/narbz2-hash
@coreutils@/mv $out/tmp.nar.bz2 $out/$(@coreutils@/cat $out/narbz2-hash).nar.bz2 @coreutils@/mv $out/tmp.nar.bz2 $out/$(@coreutils@/cat $out/narbz2-hash).nar.bz2
+1 -20
View File
@@ -97,25 +97,6 @@ env-keep-derivations = false
</varlistentry> </varlistentry>
<varlistentry xml:id="conf-build-cores"><term><literal>build-cores</literal></term>
<listitem><para>Sets the value of the
<envar>NIX_BUILD_CORES</envar> environment variable in the
invocation of builders. Builders can use this variable at their
discretion to control the maximum amount of parallelism. For
instance, in Nixpkgs, if the derivation attribute
<varname>enableParallelBuilding</varname> is set to
<literal>true</literal>, the builder passes the
<option>-j<replaceable>N</replaceable></option> flag to GNU Make.
It can be overriden using the <option
linkend='opt-cores'>--cores</option> command line switch and
defaults to <literal>1</literal>. The value <literal>0</literal>
means that the builder should use all available CPU cores in the
system.</para></listitem>
</varlistentry>
<varlistentry xml:id="conf-build-max-silent-time"><term><literal>build-max-silent-time</literal></term> <varlistentry xml:id="conf-build-max-silent-time"><term><literal>build-max-silent-time</literal></term>
<listitem> <listitem>
@@ -260,7 +241,7 @@ build-use-chroot = /dev /proc /bin</programlisting>
Nix store metadata (in <filename>/nix/var/nix/db</filename>) are Nix store metadata (in <filename>/nix/var/nix/db</filename>) are
synchronously flushed to disk. This improves robustness in case synchronously flushed to disk. This improves robustness in case
of system crashes, but reduces performance. The default is of system crashes, but reduces performance. The default is
<literal>true</literal>.</para></listitem> <literal>false</literal>.</para></listitem>
</varlistentry> </varlistentry>
-11
View File
@@ -271,17 +271,6 @@ $ mount -o bind /mnt/otherdisk/nix /nix</screen>
</varlistentry> </varlistentry>
<varlistentry><term><envar>GC_INITIAL_HEAP_SIZE</envar></term>
<listitem><para>If Nix has been configured to use the Boehm garbage
collector, this variable sets the initial size of the heap in bytes.
It defaults to 384 MiB. Setting it to a low value reduces memory
consumption, but will increase runtime due to the overhead of
garbage collection.</para></listitem>
</varlistentry>
</variablelist> </variablelist>
+9 -14
View File
@@ -96,22 +96,17 @@ ubiquitous 2.5.4a won't. Note that these are only required if you
modify the parser or when you are building from the Subversion modify the parser or when you are building from the Subversion
repository.</para> repository.</para>
<para>Nix uses the bzip2 compressor (including the bzip2 library). It <para>Nix uses CWI's ATerm library and the bzip2 compressor (including
is included in the Nix source distribution. If you build from the the bzip2 library). These are included in the Nix source
Subversion repository, you must download it yourself and place it in distribution. If you build from the Subversion repository, you must
the <filename>externals/</filename> directory. See download them yourself and place them in the
<filename>externals/</filename> directory. See
<filename>externals/Makefile.am</filename> for the precise URLs of <filename>externals/Makefile.am</filename> for the precise URLs of
this packages. Alternatively, if you already have it installed, you these packages. Alternatively, if you already have them installed,
can use <command>configure</command>'s <option>--with-bzip2</option> you can use <command>configure</command>'s
<option>--with-aterm</option> and <option>--with-bzip2</option>
options to point to their respective locations.</para> options to point to their respective locations.</para>
<para>Nix can optionally use the <link
xlink:href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/">Boehm
garbage collector</link> to reduce the evaluator’s memory consumption.
To enable it, install <literal>pkgconfig</literal> and the Boehm
garbage collector, and pass the flag <option>--enable-gc</option> to
<command>configure</command>.</para>
</section> </section>
@@ -131,7 +126,7 @@ $ make install</screen>
preceded by the command: preceded by the command:
<screen> <screen>
$ ./bootstrap.sh</screen> $ ./bootstrap</screen>
</para> </para>
+1 -1
View File
@@ -113,7 +113,7 @@ $ nix-env --rollback
<simplesect><title>Garbage collection</title> <simplesect><title>Garbage collection</title>
<para>When you uninstall a package like this… <para>When you install a package like this…
<screen> <screen>
$ nix-env --uninstall firefox $ nix-env --uninstall firefox
+1 -2
View File
@@ -27,11 +27,10 @@
<year>2007</year> <year>2007</year>
<year>2008</year> <year>2008</year>
<year>2009</year> <year>2009</year>
<year>2010</year>
<holder>Eelco Dolstra</holder> <holder>Eelco Dolstra</holder>
</copyright> </copyright>
<date>August 2010</date> <date>September 2009</date>
</info> </info>
+1 -1
View File
@@ -120,7 +120,7 @@ dependencies used in the build, such as compilers).</para>
dependencies, we can do: dependencies, we can do:
<screen> <screen>
$ nix-push <replaceable>urls</replaceable> $(nix-store -r $(nix-instantiate foo.nix))</screen> $ nix-push <replaceable>urls</replaceable> $(nix-instantiate $(nix-store -r foo.nix))</screen>
</para> </para>
+4 -17
View File
@@ -342,7 +342,7 @@ $ nix-store --gc --max-freed $((100 * 1024 * 1024))</screen>
<cmdsynopsis> <cmdsynopsis>
<command>nix-store</command> <command>nix-store</command>
<arg choice='plain'><option>--delete</option></arg> <arg choice='plain'><option>--gc</option></arg>
<arg><option>--ignore-liveness</option></arg> <arg><option>--ignore-liveness</option></arg>
<arg choice='plain' rep='repeat'><replaceable>paths</replaceable></arg> <arg choice='plain' rep='repeat'><replaceable>paths</replaceable></arg>
</cmdsynopsis> </cmdsynopsis>
@@ -404,7 +404,6 @@ error: cannot delete path `/nix/store/zq0h41l75vlb4z45kzgjjmsjxvcv1qk7-mesa-6.4'
<arg choice='plain'><option>--tree</option></arg> <arg choice='plain'><option>--tree</option></arg>
<arg choice='plain'><option>--binding</option> <replaceable>name</replaceable></arg> <arg choice='plain'><option>--binding</option> <replaceable>name</replaceable></arg>
<arg choice='plain'><option>--hash</option></arg> <arg choice='plain'><option>--hash</option></arg>
<arg choice='plain'><option>--size</option></arg>
<arg choice='plain'><option>--roots</option></arg> <arg choice='plain'><option>--roots</option></arg>
</group> </group>
<arg><option>--use-output</option></arg> <arg><option>--use-output</option></arg>
@@ -588,21 +587,9 @@ query is applied to the target of the symlink.</para>
<varlistentry><term><option>--hash</option></term> <varlistentry><term><option>--hash</option></term>
<listitem><para>Prints the SHA-256 hash of the contents of the <listitem><para>Prints the SHA-256 hash of the contents of the
store paths <replaceable>paths</replaceable> (that is, the hash of store paths <replaceable>paths</replaceable>. Since the hash is
the output of <command>nix-store --dump</command> on the given stored in the Nix database, this is a fast
paths). Since the hash is stored in the Nix database, this is a operation.</para></listitem>
fast operation.</para></listitem>
</varlistentry>
<varlistentry><term><option>--size</option></term>
<listitem><para>Prints the size in bytes of the contents of the
store paths <replaceable>paths</replaceable> — to be precise, the
size of the output of <command>nix-store --dump</command> on the
given paths. Note that the actual disk space required by the
store paths may be higher, especially on filesystems with large
cluster sizes.</para></listitem>
</varlistentry> </varlistentry>
-4
View File
@@ -13,10 +13,6 @@
</group> </group>
<replaceable>number</replaceable> <replaceable>number</replaceable>
</arg> </arg>
<arg>
<arg><option>--cores</option></arg>
<replaceable>number</replaceable>
</arg>
<arg> <arg>
<arg><option>--max-silent-time</option></arg> <arg><option>--max-silent-time</option></arg>
<replaceable>number</replaceable> <replaceable>number</replaceable>
+1 -19
View File
@@ -98,25 +98,7 @@
linkend='conf-build-max-jobs'><literal>build-max-jobs</literal></link> linkend='conf-build-max-jobs'><literal>build-max-jobs</literal></link>
configuration setting, which itself defaults to configuration setting, which itself defaults to
<literal>1</literal>. A higher value is useful on SMP systems or to <literal>1</literal>. A higher value is useful on SMP systems or to
exploit I/O latency.</para></listitem> exploit I/O latency. </para></listitem>
</varlistentry>
<varlistentry xml:id="opt-cores"><term><option>--cores</option></term>
<listitem><para>Sets the value of the <envar>NIX_BUILD_CORES</envar>
environment variable in the invocation of builders. Builders can
use this variable at their discretion to control the maximum amount
of parallelism. For instance, in Nixpkgs, if the derivation
attribute <varname>enableParallelBuilding</varname> is set to
<literal>true</literal>, the builder passes the
<option>-j<replaceable>N</replaceable></option> flag to GNU Make.
It defaults to the value of the <link
linkend='conf-build-cores'><literal>build-cores</literal></link>
configuration setting, if set, or <literal>1</literal> otherwise.
The value <literal>0</literal> means that the builder should use all
available CPU cores in the system.</para></listitem>
</varlistentry> </varlistentry>
+2 -2
View File
@@ -38,7 +38,7 @@ to end-user applications like Mozilla Firefox. (Nix is however not
tied to the Nix Package collection; you could write your own Nix tied to the Nix Package collection; you could write your own Nix
expressions based on it, or completely new ones.) You can download expressions based on it, or completely new ones.) You can download
the latest version from <link the latest version from <link
xlink:href='http://nixos.org/nixpkgs/download.html' />.</para> xlink:href='http://nixos.org/releases/full-index-nixpkgs.html' />.</para>
<para>Assuming that you have downloaded and unpacked a release of Nix <para>Assuming that you have downloaded and unpacked a release of Nix
Packages, you can view the set of available packages in the release: Packages, you can view the set of available packages in the release:
@@ -496,7 +496,7 @@ available in the subscribed channels.</para>
<para>Often, when you want to install a specific package (e.g., from <para>Often, when you want to install a specific package (e.g., from
the <link the <link
xlink:href="http://nixos.org/nixpkgs/">Nix xlink:href="http://nixos.org/releases/nixpkgs/nixpkgs-unstable/">Nix
Packages collection</link>), subscribing to a channel is a bit Packages collection</link>), subscribing to a channel is a bit
cumbersome. And channels don’t help you at all if you want to install cumbersome. And channels don’t help you at all if you want to install
an older version of a package than the one provided by the current an older version of a package than the one provided by the current
+1 -1
View File
@@ -60,7 +60,7 @@ available remotely.</para></listitem>
in the channel: in the channel:
<screen> <screen>
$ nix-env -qa \* $ nix-env -qa ’*’ <lineannotation>(mind the quotes!)</lineannotation>
docbook-xml-4.2 docbook-xml-4.2
firefox-1.0pre-PR-0.10.1 firefox-1.0pre-PR-0.10.1
hello-2.1.1 hello-2.1.1
+1 -91
View File
@@ -8,93 +8,7 @@
<!--==================================================================--> <!--==================================================================-->
<section xml:id="ssec-relnotes-1.0"><title>Release 1.0 (TBA)</title> <section xml:id="ssec-relnotes-0.14"><title>Release 0.14 (TBA)</title>
<para>This release has the following improvements:</para>
<itemizedlist>
<listitem>
<para>Nix can now optionally use the Boehm garbage collector.
This significantly reduces the Nix evaluator’s memory footprint,
especially when evaluating large NixOS system configurations. It
can be enabled using the <option>--enable-gc</option> configure
option.</para>
</listitem>
</itemizedlist>
</section>
<!--==================================================================-->
<section xml:id="ssec-relnotes-0.16"><title>Release 0.16 (August 17, 2010)</title>
<para>This release has the following improvements:</para>
<itemizedlist>
<listitem>
<para>The Nix expression evaluator is now much faster in most
cases: typically, <link
xlink:href="http://www.mail-archive.com/nix-dev@cs.uu.nl/msg04113.html">3
to 8 times compared to the old implementation</link>. It also
uses less memory. It no longer depends on the ATerm
library.</para>
</listitem>
<listitem>
<para>
Support for configurable parallelism inside builders. Build
scripts have always had the ability to perform multiple build
actions in parallel (for instance, by running <command>make -j
2</command>), but this was not desirable because the number of
actions to be performed in parallel was not configurable. Nix
now has an option <option>--cores
<replaceable>N</replaceable></option> as well as a configuration
setting <varname>build-cores =
<replaceable>N</replaceable></varname> that causes the
environment variable <envar>NIX_BUILD_CORES</envar> to be set to
<replaceable>N</replaceable> when the builder is invoked. The
builder can use this at its discretion to perform a parallel
build, e.g., by calling <command>make -j
<replaceable>N</replaceable></command>. In Nixpkgs, this can be
enabled on a per-package basis by setting the derivation
attribute <varname>enableParallelBuilding</varname> to
<literal>true</literal>.
</para>
</listitem>
<listitem>
<para><command>nix-store -q</command> now supports XML output
through the <option>--xml</option> flag.</para>
</listitem>
<listitem>
<para>Several bug fixes.</para>
</listitem>
</itemizedlist>
</section>
<!--==================================================================-->
<section xml:id="ssec-relnotes-0.15"><title>Release 0.15 (March 17, 2010)</title>
<para>This is a bug-fix release. Among other things, it fixes
building on Mac OS X (Snow Leopard), and improves the contents of
<filename>/etc/passwd</filename> and <filename>/etc/group</filename>
in <literal>chroot</literal> builds.</para>
</section>
<!--==================================================================-->
<section xml:id="ssec-relnotes-0.14"><title>Release 0.14 (February 4, 2010)</title>
<para>This release has the following improvements:</para> <para>This release has the following improvements:</para>
@@ -123,10 +37,6 @@ in <literal>chroot</literal> builds.</para>
not very useful in practice.</para> not very useful in practice.</para>
</listitem> </listitem>
<listitem>
<para>On Windows, Nix now requires Cygwin 1.7.x.</para>
</listitem>
<listitem> <listitem>
<para>A few bug fixes.</para> <para>A few bug fixes.</para>
</listitem> </listitem>
+47 -41
View File
@@ -1,3 +1,35 @@
# CWI ATerm
ATERM = aterm-2.4.2-fixes-r2
$(ATERM).tar.bz2:
@echo "Nix requires the CWI ATerm library to build."
@echo "Please download version 2.4.2-fixes-r2 from"
@echo " http://nixos.org/tarballs/aterm-2.4.2-fixes-r2.tar.bz2"
@echo "and place it in the externals/ directory."
false
$(ATERM): $(ATERM).tar.bz2
bunzip2 < $(srcdir)/$(ATERM).tar.bz2 | tar xvf -
have-aterm:
$(MAKE) $(ATERM)
touch have-aterm
if HAVE_ATERM
build-aterm:
else
build-aterm: have-aterm
(pfx=`pwd` && \
cd $(ATERM) && \
CC="$(CC)" ./configure --prefix=$$pfx/inst-aterm \
--disable-shared --enable-static && \
$(MAKE) && \
$(MAKE) install)
touch build-aterm
endif
# bzip2 # bzip2
BZIP2 = bzip2-1.0.5 BZIP2 = bzip2-1.0.5
@@ -12,56 +44,30 @@ $(BZIP2).tar.gz:
$(BZIP2): $(BZIP2).tar.gz $(BZIP2): $(BZIP2).tar.gz
gunzip < $(srcdir)/$(BZIP2).tar.gz | tar xvf - gunzip < $(srcdir)/$(BZIP2).tar.gz | tar xvf -
have-bzip2:
$(MAKE) $(BZIP2)
touch have-bzip2
if HAVE_BZIP2 if HAVE_BZIP2
build-bzip2: build-bzip2:
else else
build-bzip2: $(BZIP2) build-bzip2: have-bzip2
(cd $(BZIP2) && \ (pfx=`pwd` && \
$(MAKE) CC="$(CC)" && \ cd $(BZIP2) && \
$(MAKE) install PREFIX=$(abs_builddir)/inst-bzip2) $(MAKE) && \
$(MAKE) install PREFIX=$$pfx/inst-bzip2)
touch build-bzip2 touch build-bzip2
install-exec-local:: build-bzip2 install:
mkdir -p $(DESTDIR)${bzip2_bin} mkdir -p $(DESTDIR)${bzip2_bin}
$(INSTALL_PROGRAM) $(bzip2_bin_test)/bzip2 $(bzip2_bin_test)/bunzip2 $(DESTDIR)${bzip2_bin} $(INSTALL_PROGRAM) $(bzip2_bin_test)/bzip2 $(bzip2_bin_test)/bunzip2 $(DESTDIR)${bzip2_bin}
endif endif
# SQLite all: build-aterm build-bzip2
SQLITE = sqlite-autoconf-$(SQLITE_VERSION) EXTRA_DIST = $(ATERM).tar.bz2 $(BZIP2).tar.gz
SQLITE_TAR = sqlite-autoconf-$(SQLITE_VERSION).tar.gz
$(SQLITE_TAR): ext-clean:
@echo "Nix requires the SQLite library to build." $(RM) -f have-aterm build-aterm have-bzip2 build-bzip2
@echo "Please download version $(SQLITE_VERSION) from" $(RM) -rf $(ATERM) $(BZIP2)
@echo " http://www.sqlite.org/$(SQLITE_TAR)"
@echo "and place it in the externals/ directory."
false
$(SQLITE): $(SQLITE_TAR)
gzip -d < $(srcdir)/$(SQLITE_TAR) | tar xvf -
if HAVE_SQLITE
build-sqlite:
else
build-sqlite: $(SQLITE)
(cd $(SQLITE) && \
CC="$(CC)" CFLAGS="-DSQLITE_ENABLE_COLUMN_METADATA=1" ./configure --disable-static --prefix=$(pkglibdir)/dummy --libdir=${pkglibdir} $(SUB_CONFIGURE_FLAGS) && \
$(MAKE) )
touch build-sqlite
install-exec-local:: build-sqlite
cd $(SQLITE) && $(MAKE) install
rm -rf "$(DESTDIR)/$(pkglibdir)/dummy"
endif
all: build-bzip2 build-sqlite
EXTRA_DIST = $(BZIP2).tar.gz $(SQLITE_TAR)
clean:
$(RM) -f build-bzip2 build-sqlite
$(RM) -rf $(BZIP2) $(SQLITE)
$(RM) -rf inst-bzip2
-11
View File
@@ -59,17 +59,6 @@
#build-max-jobs = 1 #build-max-jobs = 1
### Option `build-cores'
#
# This option defines the number of CPU cores to utilize in parallel
# within a build job, i.e. by passing an appropriate `-jN' flag to GNU
# Make. The default is 1, meaning that parallel building within jobs
# is disabled. Passing the special value `0' causes Nix to try and
# auto-detect the number of available cores on the local host. This
# setting can be overridden using the `--cores' command line switch.
#build-cores = 1
### Option `build-max-silent-time' ### Option `build-max-silent-time'
# #
# This option defines the maximum number of seconds that a builder can # This option defines the maximum number of seconds that a builder can
+62 -30
View File
@@ -1,7 +1,4 @@
{ nixpkgs ? ../nixpkgs { nixpkgs ? ../nixpkgs }:
, nix ? { outPath = ./.; rev = 1234; }
, officialRelease ? false
}:
let let
@@ -9,6 +6,10 @@ let
tarball = tarball =
{ nix ? {outPath = ./.; rev = 1234;}
, officialRelease ? false
}:
with import nixpkgs {}; with import nixpkgs {};
releaseTools.sourceTarball { releaseTools.sourceTarball {
@@ -18,8 +19,8 @@ let
inherit officialRelease; inherit officialRelease;
buildInputs = buildInputs =
[ curl bison24 flex2535 perl libxml2 libxslt w3m bzip2 [ curl bison flex2533 perl libxml2 libxslt w3m bzip2
tetex dblatex nukeReferences pkgconfig tetex dblatex nukeReferences
]; ];
configureFlags = '' configureFlags = ''
@@ -28,14 +29,14 @@ let
--with-xml-flags=--nonet --with-xml-flags=--nonet
''; '';
# Include the Bzip2 tarball in the distribution. # Include the ATerm and Bzip2 tarballs in the distribution.
preConfigure = '' preConfigure = ''
stripHash ${aterm242fixes.src}
cp -pv ${aterm242fixes.src} externals/$strippedName
stripHash ${bzip2.src} stripHash ${bzip2.src}
cp -pv ${bzip2.src} externals/$strippedName cp -pv ${bzip2.src} externals/$strippedName
stripHash ${sqlite.src}
cp -pv ${sqlite.src} externals/$strippedName
# TeX needs a writable font cache. # TeX needs a writable font cache.
export VARTEXFONTS=$TMPDIR/texfonts export VARTEXFONTS=$TMPDIR/texfonts
''; '';
@@ -62,43 +63,70 @@ let
build = build =
{ system ? "i686-linux" }: { tarball ? jobs.tarball {}
, system ? "i686-linux"
}:
with import nixpkgs { inherit system; }; with import nixpkgs {inherit system;};
releaseTools.nixBuild { releaseTools.nixBuild {
name = "nix"; name = "nix";
src = tarball; src = tarball;
buildInputs = [ curl perl bzip2 openssl pkgconfig boehmgc ]; buildInputs = [curl perl bzip2 openssl];
configureFlags = '' configureFlags = ''
--disable-init-state --disable-init-state
--with-bzip2=${bzip2} --with-sqlite=${sqlite} --with-aterm=${aterm242fixes} --with-bzip2=${bzip2}
--enable-gc
''; '';
}; };
/*
static =
{ tarball ? jobs.tarball {}
, system ? "i686-linux"
}:
with import nixpkgs {inherit system;};
releaseTools.binaryTarball {
name = "nix-static-tarball";
src = tarball;
buildInputs = [curl perl bzip2];
configureFlags = ''
--disable-init-state
--with-aterm=${aterm242fixes} --with-bzip2=${bzip2}
--enable-static-nix
'';
};
*/
coverage = coverage =
with import nixpkgs { system = "x86_64-linux"; }; { tarball ? jobs.tarball {}
}:
with import nixpkgs {};
releaseTools.coverageAnalysis { releaseTools.coverageAnalysis {
name = "nix-build"; name = "nix-build";
src = tarball; src = tarball;
buildInputs = buildInputs = [
[ curl perl bzip2 openssl curl perl bzip2 openssl
# These are for "make check" only: # These are for "make check" only:
graphviz libxml2 libxslt graphviz libxml2 libxslt
]; ];
configureFlags = '' configureFlags = ''
--disable-init-state --disable-shared --disable-init-state --disable-shared
--with-bzip2=${bzip2} --with-sqlite=${sqlite} --with-aterm=${aterm242fixes} --with-bzip2=${bzip2}
''; '';
lcovFilter = [ "*/boost/*" "*-tab.*" ]; lcovFilter = ["*/boost/*" "*-tab.*"];
# We call `dot', and even though we just use it to # We call `dot', and even though we just use it to
# syntax-check generated dot files, it still requires some # syntax-check generated dot files, it still requires some
@@ -143,15 +171,17 @@ let
makeRPM = makeRPM =
system: diskImageFun: prio: system: diskImageFun: prio:
{ tarball ? jobs.tarball {}
}:
with import nixpkgs { inherit system; }; with import nixpkgs {inherit system;};
releaseTools.rpmBuild rec { releaseTools.rpmBuild rec {
name = "nix-rpm-${diskImage.name}"; name = "nix-rpm-${diskImage.name}";
src = jobs.tarball; src = tarball;
diskImage = diskImageFun vmTools.diskImages; diskImage = diskImageFun vmTools.diskImages;
memSize = 1024; memSize = 1024;
meta.schedulingPriority = prio; meta = { schedulingPriority = toString prio; };
}; };
@@ -160,17 +190,19 @@ let
makeDeb = makeDeb =
system: diskImageFun: prio: system: diskImageFun: prio:
{ tarball ? jobs.tarball {}
}:
with import nixpkgs { inherit system; }; with import nixpkgs {inherit system;};
releaseTools.debBuild { releaseTools.debBuild {
name = "nix-deb"; name = "nix-deb";
src = jobs.tarball; src = tarball;
diskImage = diskImageFun vmTools.diskImages; diskImage = diskImageFun vmTools.diskImages;
memSize = 1024; memSize = 1024;
meta.schedulingPriority = prio; meta = { schedulingPriority = toString prio; };
configureFlags = "--sysconfdir=/etc"; configureFlags = "--sysconfdir=/etc";
debRequires = [ "curl" ]; debRequires = ["curl"];
}; };
-334
View File
@@ -1,334 +0,0 @@
#! @perl@ -w -I@libexecdir@/nix
use strict;
use File::Temp qw(tempdir);
# Some patch generations options.
# Max size of NAR archives to generate patches for.
my $maxNarSize = $ENV{"NIX_MAX_NAR_SIZE"};
$maxNarSize = 160 * 1024 * 1024 if !defined $maxNarSize;
# If patch is bigger than this fraction of full archive, reject.
my $maxPatchFraction = $ENV{"NIX_PATCH_FRACTION"};
$maxPatchFraction = 0.60 if !defined $maxPatchFraction;
my $timeLimit = $ENV{"NIX_BSDIFF_TIME_LIMIT"};
$timeLimit = 180 if !defined $timeLimit;
my $hashAlgo = "sha256";
sub findOutputPaths {
my $narFiles = shift;
my %outPaths;
foreach my $p (keys %{$narFiles}) {
# Ignore derivations.
next if ($p =~ /\.drv$/);
# Ignore builders (too much ambiguity -- they're all called
# `builder.sh').
next if ($p =~ /\.sh$/);
next if ($p =~ /\.patch$/);
# Don't bother including tar files etc.
next if ($p =~ /\.tar$/ || $p =~ /\.tar\.(gz|bz2|Z|lzma|xz)$/ || $p =~ /\.zip$/ || $p =~ /\.bin$/ || $p =~ /\.tgz$/ || $p =~ /\.rpm$/ || $p =~ /cvs-export$/ || $p =~ /fetchhg$/);
$outPaths{$p} = 1;
}
return %outPaths;
}
sub getNameVersion {
my $p = shift;
$p =~ /\/[0-9a-z]+((?:-[a-zA-Z][^\/-]*)+)([^\/]*)$/;
my $name = $1;
my $version = $2;
return undef unless defined $name && defined $version;
$name =~ s/^-//;
$version =~ s/^-//;
return ($name, $version);
}
# A quick hack to get a measure of the `distance' between two
# versions: it's just the position of the first character that differs
# (or 999 if they are the same).
sub versionDiff {
my $s = shift;
my $t = shift;
my $i;
return 999 if $s eq $t;
for ($i = 0; $i < length $s; $i++) {
return $i if $i >= length $t or
substr($s, $i, 1) ne substr($t, $i, 1);
}
return $i;
}
sub getNarBz2 {
my $narPath = shift;
my $narFiles = shift;
my $storePath = shift;
my $narFileList = $$narFiles{$storePath};
die "missing path $storePath" unless defined $narFileList;
my $narFile = @{$narFileList}[0];
die unless defined $narFile;
$narFile->{url} =~ /\/([^\/]+)$/;
die unless defined $1;
return "$narPath/$1";
}
sub containsPatch {
my $patches = shift;
my $storePath = shift;
my $basePath = shift;
my $patchList = $$patches{$storePath};
return 0 if !defined $patchList;
my $found = 0;
foreach my $patch (@{$patchList}) {
# !!! baseHash might differ
return 1 if $patch->{basePath} eq $basePath;
}
return 0;
}
sub generatePatches {
my ($srcNarFiles, $dstNarFiles, $srcPatches, $dstPatches, $narPath, $patchesPath, $patchesURL, $tmpDir) = @_;
my %srcOutPaths = findOutputPaths $srcNarFiles;
my %dstOutPaths = findOutputPaths $dstNarFiles;
# For each output path in the destination, see if we need to / can
# create a patch.
print STDERR "creating patches...\n";
foreach my $p (keys %dstOutPaths) {
# If exactly the same path already exists in the source, skip it.
next if defined $srcOutPaths{$p};
print " $p\n";
# If not, then we should find the paths in the source that are
# `most' likely to be present on a system that wants to
# install this path.
(my $name, my $version) = getNameVersion $p;
next unless defined $name && defined $version;
my @closest = ();
my $closestVersion;
my $minDist = -1; # actually, larger means closer
# Find all source paths with the same name.
foreach my $q (keys %srcOutPaths) {
(my $name2, my $version2) = getNameVersion $q;
next unless defined $name2 && defined $version2;
if ($name eq $name2) {
my $srcSystem = @{$$dstNarFiles{$p}}[0]->{system};
my $dstSystem = @{$$srcNarFiles{$q}}[0]->{system};
if (defined $srcSystem && defined $dstSystem && $srcSystem ne $dstSystem) {
print " SKIPPING $q due to different systems ($srcSystem vs. $dstSystem)\n";
next;
}
# If the sizes differ too much, then skip. This
# disambiguates between, e.g., a real component and a
# wrapper component (cf. Firefox in Nixpkgs).
my $srcSize = @{$$srcNarFiles{$q}}[0]->{size};
my $dstSize = @{$$dstNarFiles{$p}}[0]->{size};
my $ratio = $srcSize / $dstSize;
$ratio = 1 / $ratio if $ratio < 1;
# print " SIZE $srcSize $dstSize $ratio $q\n";
if ($ratio >= 3) {
print " SKIPPING $q due to size ratio $ratio ($srcSize vs. $dstSize)\n";
next;
}
# If there are multiple matching names, include the
# ones with the closest version numbers.
my $dist = versionDiff $version, $version2;
if ($dist > $minDist) {
$minDist = $dist;
@closest = ($q);
$closestVersion = $version2;
} elsif ($dist == $minDist) {
push @closest, $q;
}
}
}
if (scalar(@closest) == 0) {
print " NO BASE: $p\n";
next;
}
foreach my $closest (@closest) {
# Generate a patch between $closest and $p.
print STDERR " $p <- $closest\n";
# If the patch already exists, skip it.
if (containsPatch($srcPatches, $p, $closest) ||
containsPatch($dstPatches, $p, $closest))
{
print " skipping, already exists\n";
next;
}
my $srcNarBz2 = getNarBz2 $narPath, $srcNarFiles, $closest;
my $dstNarBz2 = getNarBz2 $narPath, $dstNarFiles, $p;
if (! -f $srcNarBz2) {
warn "patch source archive $srcNarBz2 is missing\n";
next;
}
system("@bunzip2@ < $srcNarBz2 > $tmpDir/A") == 0
or die "cannot unpack $srcNarBz2";
if ((stat "$tmpDir/A")[7] >= $maxNarSize) {
print " skipping, source is too large\n";
next;
}
system("@bunzip2@ < $dstNarBz2 > $tmpDir/B") == 0
or die "cannot unpack $dstNarBz2";
if ((stat "$tmpDir/B")[7] >= $maxNarSize) {
print " skipping, destination is too large\n";
next;
}
my $time1 = time();
my $res = system("ulimit -t $timeLimit; @libexecdir@/bsdiff $tmpDir/A $tmpDir/B $tmpDir/DIFF");
my $time2 = time();
if ($res) {
warn "binary diff computation aborted after ", $time2 - $time1, " seconds\n";
next;
}
my $baseHash = `@bindir@/nix-hash --flat --type $hashAlgo --base32 $tmpDir/A` or die;
chomp $baseHash;
my $narHash = `@bindir@/nix-hash --flat --type $hashAlgo --base32 $tmpDir/B` or die;
chomp $narHash;
my $narDiffHash = `@bindir@/nix-hash --flat --type $hashAlgo --base32 $tmpDir/DIFF` or die;
chomp $narDiffHash;
my $narDiffSize = (stat "$tmpDir/DIFF")[7];
my $dstNarBz2Size = (stat $dstNarBz2)[7];
print " size $narDiffSize; full size $dstNarBz2Size; ", $time2 - $time1, " seconds\n";
if ($narDiffSize >= $dstNarBz2Size) {
print " rejecting; patch bigger than full archive\n";
next;
}
if ($narDiffSize / $dstNarBz2Size >= $maxPatchFraction) {
print " rejecting; patch too large relative to full archive\n";
next;
}
my $finalName = "$narDiffHash.nar-bsdiff";
if (-e "$patchesPath/$finalName") {
print " not copying, already exists\n";
}
else {
system("cp '$tmpDir/DIFF' '$patchesPath/$finalName.tmp'") == 0
or die "cannot copy diff";
rename("$patchesPath/$finalName.tmp", "$patchesPath/$finalName")
or die "cannot rename $patchesPath/$finalName.tmp";
}
# Add the patch to the manifest.
addPatch $dstPatches, $p,
{ url => "$patchesURL/$finalName", hash => "$hashAlgo:$narDiffHash"
, size => $narDiffSize, basePath => $closest, baseHash => "$hashAlgo:$baseHash"
, narHash => "$hashAlgo:$narHash", patchType => "nar-bsdiff"
};
}
}
}
# Propagate useful patches from $srcPatches to $dstPatches. A patch
# is useful if it produces either paths in the $dstNarFiles or paths
# that can be used as the base for other useful patches.
sub propagatePatches {
my ($srcPatches, $dstNarFiles, $dstPatches) = @_;
print STDERR "propagating patches...\n";
my $changed;
do {
# !!! we repeat this to reach the transitive closure; inefficient
$changed = 0;
print STDERR "loop\n";
my %dstBasePaths;
foreach my $q (keys %{$dstPatches}) {
foreach my $patch (@{$$dstPatches{$q}}) {
$dstBasePaths{$patch->{basePath}} = 1;
}
}
foreach my $p (keys %{$srcPatches}) {
my $patchList = $$srcPatches{$p};
my $include = 0;
# Is path $p included in the destination? If so, include
# patches that produce it.
$include = 1 if defined $$dstNarFiles{$p};
# Is path $p a path that serves as a base for paths in the
# destination? If so, include patches that produce it.
# !!! check baseHash
$include = 1 if defined $dstBasePaths{$p};
if ($include) {
foreach my $patch (@{$patchList}) {
$changed = 1 if addPatch $dstPatches, $p, $patch;
}
}
}
} while $changed;
}
# Add all new patches in $srcPatches to $dstPatches.
sub copyPatches {
my ($srcPatches, $dstPatches) = @_;
foreach my $p (keys %{$srcPatches}) {
addPatch $dstPatches, $p, $_ foreach @{$$srcPatches{$p}};
}
}
return 1;
+11 -14
View File
@@ -1,23 +1,22 @@
bin_SCRIPTS = nix-collect-garbage \ bin_SCRIPTS = nix-collect-garbage \
nix-pull nix-push nix-prefetch-url \ nix-pull nix-push nix-prefetch-url \
nix-install-package nix-channel nix-build \ nix-install-package nix-channel nix-build \
nix-copy-closure nix-generate-patches nix-copy-closure
noinst_SCRIPTS = nix-profile.sh GeneratePatches.pm \ noinst_SCRIPTS = nix-profile.sh generate-patches.pl \
find-runtime-roots.pl build-remote.pl nix-reduce-build \ find-runtime-roots.pl build-remote.pl nix-reduce-build \
copy-from-other-stores.pl nix-http-export.cgi copy-from-other-stores.pl nix-http-export.cgi
nix-pull nix-push: NixManifest.pm NixConfig.pm download-using-manifests.pl nix-pull nix-push: readmanifest.pm readconfig.pm download-using-manifests.pl
install-exec-local: NixManifest.pm GeneratePatches.pm download-using-manifests.pl copy-from-other-stores.pl find-runtime-roots.pl install-exec-local: readmanifest.pm download-using-manifests.pl copy-from-other-stores.pl find-runtime-roots.pl
$(INSTALL) -d $(DESTDIR)$(sysconfdir)/profile.d $(INSTALL) -d $(DESTDIR)$(sysconfdir)/profile.d
$(INSTALL_PROGRAM) nix-profile.sh $(DESTDIR)$(sysconfdir)/profile.d/nix.sh $(INSTALL_PROGRAM) nix-profile.sh $(DESTDIR)$(sysconfdir)/profile.d/nix.sh
$(INSTALL) -d $(DESTDIR)$(libexecdir)/nix $(INSTALL) -d $(DESTDIR)$(libexecdir)/nix
$(INSTALL_DATA) NixManifest.pm $(DESTDIR)$(libexecdir)/nix $(INSTALL_DATA) readmanifest.pm $(DESTDIR)$(libexecdir)/nix
$(INSTALL_DATA) NixConfig.pm $(DESTDIR)$(libexecdir)/nix $(INSTALL_DATA) readconfig.pm $(DESTDIR)$(libexecdir)/nix
$(INSTALL_DATA) SSH.pm $(DESTDIR)$(libexecdir)/nix
$(INSTALL_DATA) GeneratePatches.pm $(DESTDIR)$(libexecdir)/nix
$(INSTALL_PROGRAM) find-runtime-roots.pl $(DESTDIR)$(libexecdir)/nix $(INSTALL_PROGRAM) find-runtime-roots.pl $(DESTDIR)$(libexecdir)/nix
$(INSTALL_PROGRAM) generate-patches.pl $(DESTDIR)$(libexecdir)/nix
$(INSTALL_PROGRAM) build-remote.pl $(DESTDIR)$(libexecdir)/nix $(INSTALL_PROGRAM) build-remote.pl $(DESTDIR)$(libexecdir)/nix
$(INSTALL) -d $(DESTDIR)$(libexecdir)/nix/substituters $(INSTALL) -d $(DESTDIR)$(libexecdir)/nix/substituters
$(INSTALL_PROGRAM) download-using-manifests.pl $(DESTDIR)$(libexecdir)/nix/substituters $(INSTALL_PROGRAM) download-using-manifests.pl $(DESTDIR)$(libexecdir)/nix/substituters
@@ -30,16 +29,14 @@ EXTRA_DIST = nix-collect-garbage.in \
nix-pull.in nix-push.in nix-profile.sh.in \ nix-pull.in nix-push.in nix-profile.sh.in \
nix-prefetch-url.in nix-install-package.in \ nix-prefetch-url.in nix-install-package.in \
nix-channel.in \ nix-channel.in \
NixManifest.pm.in \ readmanifest.pm.in \
NixConfig.pm.in \ readconfig.pm.in \
SSH.pm \
GeneratePatches.pm.in \
nix-build.in \ nix-build.in \
download-using-manifests.pl.in \ download-using-manifests.pl.in \
copy-from-other-stores.pl.in \ copy-from-other-stores.pl.in \
generate-patches.pl.in \
nix-copy-closure.in \ nix-copy-closure.in \
find-runtime-roots.pl.in \ find-runtime-roots.pl.in \
build-remote.pl.in \ build-remote.pl.in \
nix-reduce-build.in \ nix-reduce-build.in \
nix-http-export.cgi.in \ nix-http-export.cgi.in
nix-generate-patches.in
-52
View File
@@ -1,52 +0,0 @@
use strict;
use File::Temp qw(tempdir);
our @sshOpts = split ' ', ($ENV{"NIX_SSHOPTS"} or "");
push @sshOpts, "-x";
my $sshStarted = 0;
my $sshHost;
# Open a master SSH connection to `host', unless there already is a
# running master connection (as determined by `-O check').
sub openSSHConnection {
my ($host) = @_;
die if $sshStarted;
$sshHost = $host;
return 1 if system("ssh $sshHost @sshOpts -O check 2> /dev/null") == 0;
my $tmpDir = tempdir("nix-ssh.XXXXXX", CLEANUP => 1, TMPDIR => 1)
or die "cannot create a temporary directory";
push @sshOpts, "-S", "$tmpDir/control";
# Start the master. We can't use the `-f' flag (fork into
# background after establishing the connection) because then the
# child continues to run if we are killed. So instead make SSH
# print "started" when it has established the connection, and wait
# until we see that.
open SSHPIPE, "ssh $sshHost @sshOpts -M -N -o LocalCommand='echo started' -o PermitLocalCommand=yes |" or die;
while (<SSHPIPE>) {
chomp;
if ($_ eq "started") {
$sshStarted = 1;
return 1;
}
}
return 0;
}
# Tell the master SSH client to exit.
sub closeSSHConnection {
if ($sshStarted) {
system("ssh $sshHost @sshOpts -O exit 2> /dev/null") == 0
or warn "unable to stop SSH master: $?";
}
}
END { my $saved = $?; closeSSHConnection; $? = $saved; }
return 1;
+181 -194
View File
@@ -1,11 +1,9 @@
#! @perl@ -w -I@libexecdir@/nix #! @perl@ -w
use strict;
use Fcntl ':flock'; use Fcntl ':flock';
use English '-no_match_vars'; use English '-no_match_vars';
use IO::Handle; use IO::Handle;
use SSH qw/sshOpts openSSHConnection/;
no warnings('once');
# General operation: # General operation:
# #
@@ -24,30 +22,56 @@ no warnings('once');
# The nice thing about this scheme is that if we die prematurely, the # The nice thing about this scheme is that if we die prematurely, the
# locks are released automatically. # locks are released automatically.
my $loadIncreased = 0;
# Make sure that we don't get any SSH passphrase or host key popups - my ($amWilling, $localSystem, $neededSystem, $drvPath, $maxSilentTime) = @ARGV;
# if there is any problem it should fail, not do something $maxSilentTime = 0 unless defined $maxSilentTime;
# interactive.
$ENV{"DISPLAY"} = "";
$ENV{"SSH_ASKPASS"} = "";
sub sendReply { sub sendReply {
my $reply = shift; my $reply = shift;
print STDERR "# $reply\n"; print STDERR "# $reply\n";
} }
sub all { $_ || return 0 for @_; 1 } sub decline {
sendReply "decline";
exit 0;
# Initialisation. }
my $loadIncreased = 0;
my ($localSystem, $maxSilentTime, $printBuildTrace) = @ARGV;
$maxSilentTime = 0 unless defined $maxSilentTime;
my $currentLoad = $ENV{"NIX_CURRENT_LOAD"}; my $currentLoad = $ENV{"NIX_CURRENT_LOAD"};
decline unless defined $currentLoad;
mkdir $currentLoad, 0777 or die unless -d $currentLoad;
my $conf = $ENV{"NIX_REMOTE_SYSTEMS"}; my $conf = $ENV{"NIX_REMOTE_SYSTEMS"};
decline if !defined $conf || ! -e $conf;
my $canBuildLocally = $amWilling && ($localSystem eq $neededSystem);
# Read the list of machines.
my @machines;
open CONF, "< $conf" or die;
while (<CONF>) {
chomp;
s/\#.*$//g;
next if /^\s*$/;
/^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\d+)(\s+([0-9\.]+))?\s*$/ or die;
push @machines,
{ hostName => $1
, systemTypes => [split(/,/, $2)]
, sshKeys => $3
, maxJobs => $4
, speedFactor => 1.0 * ($6 || 1)
};
}
close CONF;
# Acquire the exclusive lock on $currentLoad/main-lock.
my $mainLock = "$currentLoad/main-lock";
open MAINLOCK, ">>$mainLock" or die;
flock(MAINLOCK, LOCK_EX) or die;
sub openSlotLock { sub openSlotLock {
@@ -57,213 +81,176 @@ sub openSlotLock {
open $slotLock, ">>$slotLockFn" or die; open $slotLock, ">>$slotLockFn" or die;
return $slotLock; return $slotLock;
} }
# Read the list of machines.
my @machines;
if (defined $conf && -e $conf) {
open CONF, "< $conf" or die;
while (<CONF>) {
chomp;
s/\#.*$//g;
next if /^\s*$/;
my @tokens = split /\s/, $_;
push @machines,
{ hostName => $tokens[0]
, systemTypes => [ split(/,/, $tokens[1]) ]
, sshKeys => $tokens[2]
, maxJobs => int($tokens[3])
, speedFactor => 1.0 * (defined $tokens[4] ? int($tokens[4]) : 1)
, features => [ split(/,/, $tokens[5] || "") ]
, enabled => 1
};
}
close CONF;
}
# Wait for the calling process to ask us whether we can build some derivation.
my ($drvPath, $hostName, $slotLock);
REQ: while (1) {
$_ = <STDIN> || exit 0;
my ($amWilling, $neededSystem);
($amWilling, $neededSystem, $drvPath, $requiredFeatures) = split;
my @requiredFeatures = split /,/, $requiredFeatures;
my $canBuildLocally = $amWilling && ($localSystem eq $neededSystem);
if (!defined $currentLoad) {
sendReply "decline";
next;
}
# Acquire the exclusive lock on $currentLoad/main-lock.
mkdir $currentLoad, 0777 or die unless -d $currentLoad;
my $mainLock = "$currentLoad/main-lock";
open MAINLOCK, ">>$mainLock" or die;
flock(MAINLOCK, LOCK_EX) or die;
while (1) {
# Find all machine that can execute this build, i.e., that
# support builds for the given platform and features, and are
# not at their job limit.
my $rightType = 0;
my @available = ();
LOOP: foreach my $cur (@machines) {
if ($cur->{enabled}
&& (grep { $neededSystem eq $_ } @{$cur->{systemTypes}})
&& all(map { my $f = $_; 0 != grep { $f eq $_ } @{$cur->{features}} } @requiredFeatures))
{
$rightType = 1;
# We have a machine of the right type. Determine the load on # Find all machine that can execute this build, i.e., that support
# the machine. # builds for the given platform and are not at their job limit.
my $slot = 0; my $rightType = 0;
my $load = 0; my @available = ();
my $free; LOOP: foreach my $cur (@machines) {
while ($slot < $cur->{maxJobs}) { if (grep { $neededSystem eq $_ } @{$cur->{systemTypes}}) {
my $slotLock = openSlotLock($cur, $slot); $rightType = 1;
if (flock($slotLock, LOCK_EX | LOCK_NB)) {
$free = $slot unless defined $free;
flock($slotLock, LOCK_UN) or die;
} else {
$load++;
}
close $slotLock;
$slot++;
}
push @available, { machine => $cur, load => $load, free => $free }
if $load < $cur->{maxJobs};
}
}
if (defined $ENV{NIX_DEBUG_HOOK}) { # We have a machine of the right type. Determine the load on
print STDERR "load on " . $_->{machine}->{hostName} . " = " . $_->{load} . "\n" # the machine.
foreach @available; my $slot = 0;
} my $load = 0;
my $free;
while ($slot < $cur->{maxJobs}) {
# Didn't find any available machine? Then decline or postpone. my $slotLock = openSlotLock($cur, $slot);
if (scalar @available == 0) { if (flock($slotLock, LOCK_EX | LOCK_NB)) {
# Postpone if we have a machine of the right type, except $free = $slot unless defined $free;
# if the local system can and wants to do the build. flock($slotLock, LOCK_UN) or die;
if ($rightType && !$canBuildLocally) {
sendReply "postpone";
} else { } else {
sendReply "decline"; $load++;
} }
close MAINLOCK; close $slotLock;
next REQ; $slot++;
} }
push @available, { machine => $cur, load => $load, free => $free }
# Prioritise the available machines as follows: if $load < $cur->{maxJobs};
# - First by load divided by speed factor, rounded to the nearest
# integer. This causes fast machines to be preferred over slow
# machines with similar loads.
# - Then by speed factor.
# - Finally by load.
sub lf { my $x = shift; return int($x->{load} / $x->{machine}->{speedFactor} + 0.4999); }
@available = sort
{ lf($a) <=> lf($b)
|| $b->{machine}->{speedFactor} <=> $a->{machine}->{speedFactor}
|| $a->{load} <=> $b->{load}
} @available;
# Select the best available machine and lock a free slot.
my $selected = $available[0];
my $machine = $selected->{machine};
$slotLock = openSlotLock($machine, $selected->{free});
flock($slotLock, LOCK_EX | LOCK_NB) or die;
utime undef, undef, $slotLock;
close MAINLOCK;
# Connect to the selected machine.
@sshOpts = ("-i", $machine->{sshKeys}, "-x");
$hostName = $machine->{hostName};
last REQ if openSSHConnection $hostName;
warn "unable to open SSH connection to $hostName, trying other available machines...\n";
$machine->{enabled} = 0;
} }
} }
if (defined $ENV{NIX_DEBUG_HOOK}) {
print STDERR "load on " . $_->{machine}->{hostName} . " = " . $_->{load} . "\n"
foreach @available;
}
# Didn't find any available machine? Then decline or postpone.
if (scalar @available == 0) {
# Postpone if we have a machine of the right type, except if the
# local system can and wants to do the build.
if ($rightType && !$canBuildLocally) {
sendReply "postpone";
exit 0;
} else {
decline;
}
}
# Prioritise the available machines as follows:
# - First by load divided by speed factor, rounded to the nearest
# integer. This causes fast machines to be preferred over slow
# machines with similar loads.
# - Then by speed factor.
# - Finally by load.
sub lf { my $x = shift; return int($x->{load} / $x->{machine}->{speedFactor} + 0.4999); }
@available = sort
{ lf($a) <=> lf($b)
|| $b->{machine}->{speedFactor} <=> $a->{machine}->{speedFactor}
|| $a->{load} <=> $b->{load}
} @available;
# Select the best available machine and lock a free slot.
my $selected = $available[0];
my $machine = $selected->{machine};
my $slotLock = openSlotLock($machine, $selected->{free});
flock($slotLock, LOCK_EX | LOCK_NB) or die;
utime undef, undef, $slotLock;
close MAINLOCK;
# Tell Nix we've accepted the build. # Tell Nix we've accepted the build.
sendReply "accept"; sendReply "accept";
my @inputs = split /\s/, readline(STDIN); if (defined $ENV{NIX_DEBUG_HOOK}) {
my @outputs = split /\s/, readline(STDIN); my $hostName = $machine->{hostName};
my $sp = $machine->{speedFactor};
print STDERR "building `$drvPath' on `$hostName' - $sp - " . $selected->{free} . "\n";
sleep 10;
exit 0;
}
my $x = <STDIN>;
chomp $x;
if ($x ne "okay") {
exit 0;
}
# Do the actual build.
my $hostName = $machine->{hostName};
print STDERR "building `$drvPath' on `$hostName'\n"; print STDERR "building `$drvPath' on `$hostName'\n";
print STDERR "@ build-remote $drvPath $hostName\n" if $printBuildTrace;
# Make sure that we don't get any SSH passphrase or host key popups -
# if there is any problem it should fail, not do something
# interactive.
$ENV{"DISPLAY"} = "";
$ENV{"SSH_PASSWORD_FILE="} = "";
$ENV{"SSH_ASKPASS="} = "";
my $sshOpts = "-i " . $machine->{sshKeys} . " -x";
# Hack to support Cygwin: if we login without a password, we don't
# have exactly the same rights as when we do. This causes the
# Microsoft C compiler to fail with certain flags:
#
# http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=99676
#
# So as a workaround, we pass a verbatim password. ssh tries to makes
# this very hard; the trick is to make it call SSH_ASKPASS to get the
# password. (It only calls this command when there is no controlling
# terminal, but Nix ensures that is is the case. When doing this
# manually, use setsid(1).)
if ($machine->{sshKeys} =~ /^password:/) {
my $passwordFile = $machine->{sshKeys};
$passwordFile =~ s/^password://;
$sshOpts = "ssh -x";
$ENV{"SSH_PASSWORD_FILE"} = $passwordFile;
$ENV{"SSH_ASKPASS"} = "/tmp/writepass";
open WRITEPASS, ">/tmp/writepass" or die;
print WRITEPASS "#! /bin/sh\ncat \"\$SSH_PASSWORD_FILE\"";
close WRITEPASS;
chmod 0755, "/tmp/writepass" or die;
}
my $inputs = `cat inputs`; die if ($? != 0);
$inputs =~ s/\n/ /g;
my $outputs = `cat outputs`; die if ($? != 0);
$outputs =~ s/\n/ /g;
print "copying inputs...\n";
my $maybeSign = ""; my $maybeSign = "";
$maybeSign = "--sign" if -e "/nix/etc/nix/signing-key.sec"; $maybeSign = "--sign" if -e "/nix/etc/nix/signing-key.sec";
system("NIX_SSHOPTS=\"$sshOpts\" @bindir@/nix-copy-closure $hostName $maybeSign $drvPath $inputs") == 0
# Register the derivation as a temporary GC root. Note that $PPID is
# the PID of the remote SSH process, which, due to the use of a
# persistant SSH connection, should be the same across all remote
# command invocations for this session.
my $rootsDir = "@localstatedir@/nix/gcroots/tmp";
system("ssh $hostName @sshOpts 'mkdir -m 1777 -p $rootsDir; ln -sfn $drvPath $rootsDir/\$PPID.drv'");
sub removeRoots {
system("ssh $hostName @sshOpts 'rm -f $rootsDir/\$PPID.drv $rootsDir/\$PPID.out'");
}
# Copy the derivation and its dependencies to the build machine.
system("NIX_SSHOPTS=\"@sshOpts\" @bindir@/nix-copy-closure $hostName $maybeSign $drvPath @inputs") == 0
or die "cannot copy inputs to $hostName: $?"; or die "cannot copy inputs to $hostName: $?";
print "building...\n";
# Perform the build. my $buildFlags = "--max-silent-time $maxSilentTime";
my $buildFlags = "--max-silent-time $maxSilentTime --fallback --add-root $rootsDir/\$PPID.out --option verbosity 0";
# We let the remote side kill its process group when the connection is # `-tt' forces allocation of a pseudo-terminal. This is required to
# closed unexpectedly. This is necessary to ensure that no processes # make the remote nix-store process receive a signal when the
# are left running on the remote system if the local Nix process is # connection dies. Without it, the remote process might continue to
# killed. (SSH itself doesn't kill child processes if the connection # run indefinitely (that is, until it next tries to write to
# is interrupted unless the `-tt' flag is used to force a pseudo-tty, # stdout/stderr).
# in which case every child receives SIGHUP; however, `-tt' doesn't if (system("ssh -tt $sshOpts $hostName 'nix-store --realise $buildFlags $drvPath > /dev/null'") != 0) {
# work on some platforms when connection sharing is used.) # If we couldn't run ssh or there was an ssh problem (indicated by
pipe STDIN, DUMMY; # make sure we have a readable STDIN # exit code 255), then we return exit code 1; otherwise we assume
if (system("ssh $hostName @sshOpts '(read; kill -INT -\$\$) <&0 & nix-store -r $drvPath $buildFlags > /dev/null' 2>&4") != 0) { # that the builder failed, which we indicated to Nix using exit
# Note that if we get exit code 100 from `nix-store -r', it # code 100. It's important to distinguish between the two because
# denotes a permanent build failure (as opposed to an SSH problem # the first is a transient failure and the latter is permanent.
# or a temporary Nix problem). We propagate this to the caller to my $res = $? == -1 || ($? >> 8) == 255 ? 1 : 100;
# allow it to distinguish between transient and permanent print STDERR "build of `$drvPath' on `$hostName' failed with exit code $?\n";
# failures.
my $res = $? >> 8;
print STDERR "build of `$drvPath' on `$hostName' failed with exit code $res\n";
removeRoots;
exit $res; exit $res;
} }
#print "build of `$drvPath' on `$hostName' succeeded\n"; print "build of `$drvPath' on `$hostName' succeeded\n";
foreach my $output (split '\n', $outputs) {
# Copy the output from the build machine.
foreach my $output (@outputs) {
my $maybeSignRemote = ""; my $maybeSignRemote = "";
$maybeSignRemote = "--sign" if $UID != 0; $maybeSignRemote = "--sign" if $UID != 0;
system("ssh $hostName @sshOpts 'nix-store --export $maybeSignRemote $output'" . system("ssh $sshOpts $hostName 'nix-store --export $maybeSignRemote $output' | @bindir@/nix-store --import > /dev/null") == 0
"| NIX_HELD_LOCKS=$output @bindir@/nix-store --import > /dev/null") == 0
or die "cannot copy $output from $hostName: $?"; or die "cannot copy $output from $hostName: $?";
} }
# Get rid of the temporary GC roots.
removeRoots;
+34 -29
View File
@@ -17,19 +17,25 @@ foreach my $dir (@remoteStoresAll) {
} }
$ENV{"NIX_REMOTE"} = "";
sub findStorePath { sub findStorePath {
my $storePath = shift; my $storePath = shift;
my $storePathName = basename $storePath;
foreach my $store (@remoteStores) { foreach my $store (@remoteStores) {
my $sourcePath = "$store/store/" . basename $storePath; # Determine whether $storePath exists by looking for the
next unless -e $sourcePath || -l $sourcePath; # existence of the info file, and if so, get store path info
$ENV{"NIX_DB_DIR"} = "$store/var/nix/db"; # from that file. This rather breaks abstraction: we should
return ($store, $sourcePath) if # be using `nix-store' for that. But right now there is no
system("@bindir@/nix-store --check-validity $storePath") == 0; # good way to tell nix-store to access a store mounted under a
# different location (there's $NIX_STORE, but that only works
# if the remote store is mounted under its "real" location).
my $infoFile = "$store/var/nix/db/info/$storePathName";
my $storePath2 = "$store/store/$storePathName";
if (-f $infoFile && -e $storePath2) {
return ($infoFile, $storePath2);
}
} }
return undef;
} }
@@ -40,38 +46,37 @@ if ($ARGV[0] eq "--query") {
if ($cmd eq "have") { if ($cmd eq "have") {
my $storePath = <STDIN>; chomp $storePath; my $storePath = <STDIN>; chomp $storePath;
print STDOUT (defined findStorePath($storePath) ? "1\n" : "0\n"); (my $infoFile) = findStorePath $storePath;
print STDOUT ($infoFile ? "1\n" : "0\n");
} }
elsif ($cmd eq "info") { elsif ($cmd eq "info") {
my $storePath = <STDIN>; chomp $storePath; my $storePath = <STDIN>; chomp $storePath;
my ($store, $sourcePath) = findStorePath($storePath); (my $infoFile) = findStorePath $storePath;
if (!defined $store) { if (!$infoFile) {
print "0\n"; print "0\n";
next; # not an error next; # not an error
} }
print "1\n"; print "1\n";
$ENV{"NIX_DB_DIR"} = "$store/var/nix/db"; my $deriver = "";
my @references = ();
my $deriver = `@bindir@/nix-store --query --deriver $storePath`;
die "cannot query deriver of `$storePath'" if $? != 0;
chomp $deriver;
$deriver = "" if $deriver eq "unknown-deriver";
my @references = split "\n", open INFO, "<$infoFile" or die "cannot read info file $infoFile\n";
`@bindir@/nix-store --query --references $storePath`; while (<INFO>) {
die "cannot query references of `$storePath'" if $? != 0; chomp;
/^([\w-]+): (.*)$/ or die "bad info file";
my $narSize = `@bindir@/nix-store --query --size $storePath`; my $key = $1;
die "cannot query size of `$storePath'" if $? != 0; my $value = $2;
chomp $narSize; if ($key eq "Deriver") { $deriver = $value; }
elsif ($key eq "References") { @references = split ' ', $value; }
}
close INFO;
print "$deriver\n"; print "$deriver\n";
print scalar @references, "\n"; print scalar @references, "\n";
print "$_\n" foreach @references; print "$_\n" foreach @references;
print "$narSize\n"; print "0\n"; # !!! showing size not supported (yet)
print "$narSize\n";
} }
else { die "unknown command `$cmd'"; } else { die "unknown command `$cmd'"; }
@@ -82,8 +87,8 @@ if ($ARGV[0] eq "--query") {
elsif ($ARGV[0] eq "--substitute") { elsif ($ARGV[0] eq "--substitute") {
die unless scalar @ARGV == 2; die unless scalar @ARGV == 2;
my $storePath = $ARGV[1]; my $storePath = $ARGV[1];
my ($store, $sourcePath) = findStorePath $storePath; (my $infoFile, my $sourcePath) = findStorePath $storePath;
die unless $store; die unless $infoFile;
print "\n*** Copying `$storePath' from `$sourcePath'\n\n"; print "\n*** Copying `$storePath' from `$sourcePath'\n\n";
system("$binDir/nix-store --dump $sourcePath | $binDir/nix-store --restore $storePath") == 0 system("$binDir/nix-store --dump $sourcePath | $binDir/nix-store --restore $storePath") == 0
or die "cannot copy `$sourcePath' to `$storePath'"; or die "cannot copy `$sourcePath' to `$storePath'";
+148 -177
View File
@@ -1,7 +1,7 @@
#! @perl@ -w -I@libexecdir@/nix #! @perl@ -w -I@libexecdir@/nix
use strict; use strict;
use NixManifest; use readmanifest;
use POSIX qw(strftime); use POSIX qw(strftime);
use File::Temp qw(tempdir); use File::Temp qw(tempdir);
@@ -12,10 +12,6 @@ STDOUT->autoflush(1);
my $manifestDir = ($ENV{"NIX_MANIFESTS_DIR"} or "@localstatedir@/nix/manifests"); my $manifestDir = ($ENV{"NIX_MANIFESTS_DIR"} or "@localstatedir@/nix/manifests");
my $logFile = "@localstatedir@/log/nix/downloads"; my $logFile = "@localstatedir@/log/nix/downloads";
# For queries, skip expensive calls to nix-hash etc. We're just
# estimating the expected download size.
my $fast = 1;
# Load all manifests. # Load all manifests.
my %narFiles; my %narFiles;
@@ -35,151 +31,6 @@ for my $manifest (glob "$manifestDir/*.nixmanifest") {
} }
sub isValidPath {
my $p = shift;
if ($fast) {
return -e $p;
} else {
return system("$binDir/nix-store --check-validity '$p' 2> /dev/null") == 0;
}
}
sub parseHash {
my $hash = shift;
if ($hash =~ /^(.+):(.+)$/) {
return ($1, $2);
} else {
return ("md5", $hash);
}
}
# Compute the most efficient sequence of downloads to produce the
# given path.
sub computeSmallestDownload {
my $targetPath = shift;
# Build a graph of all store paths that might contribute to the
# construction of $targetPath, and the special node "start". The
# edges are either patch operations, or downloads of full NAR
# files. The latter edges only occur between "start" and a store
# path.
my %graph;
$graph{"start"} = {d => 0, pred => undef, edges => []};
my @queue = ();
my $queueFront = 0;
my %done;
sub addNode {
my $graph = shift;
my $u = shift;
$$graph{$u} = {d => 999999999999, pred => undef, edges => []}
unless defined $$graph{$u};
}
sub addEdge {
my $graph = shift;
my $u = shift;
my $v = shift;
my $w = shift;
my $type = shift;
my $info = shift;
addNode $graph, $u;
push @{$$graph{$u}->{edges}},
{weight => $w, start => $u, end => $v, type => $type, info => $info};
my $n = scalar @{$$graph{$u}->{edges}};
}
push @queue, $targetPath;
while ($queueFront < scalar @queue) {
my $u = $queue[$queueFront++];
next if defined $done{$u};
$done{$u} = 1;
addNode \%graph, $u;
# If the path already exists, it has distance 0 from the
# "start" node.
if (isValidPath($u)) {
addEdge \%graph, "start", $u, 0, "present", undef;
}
else {
# Add patch edges.
my $patchList = $patches{$u};
foreach my $patch (@{$patchList}) {
if (isValidPath($patch->{basePath})) {
# !!! this should be cached
my ($baseHashAlgo, $baseHash) = parseHash $patch->{baseHash};
my $format = "--base32";
$format = "" if $baseHashAlgo eq "md5";
my $hash = $fast && $baseHashAlgo eq "sha256"
? `$binDir/nix-store -q --hash "$patch->{basePath}"`
: `$binDir/nix-hash --type '$baseHashAlgo' $format "$patch->{basePath}"`;
chomp $hash;
$hash =~ s/.*://;
next if $hash ne $baseHash;
}
push @queue, $patch->{basePath};
addEdge \%graph, $patch->{basePath}, $u, $patch->{size}, "patch", $patch;
}
# Add NAR file edges to the start node.
my $narFileList = $narFiles{$u};
foreach my $narFile (@{$narFileList}) {
# !!! how to handle files whose size is not known in advance?
# For now, assume some arbitrary size (1 MB).
addEdge \%graph, "start", $u, ($narFile->{size} || 1000000), "narfile", $narFile;
}
}
}
# Run Dijkstra's shortest path algorithm to determine the shortest
# sequence of download and/or patch actions that will produce
# $targetPath.
my @todo = keys %graph;
while (scalar @todo > 0) {
# Remove the closest element from the todo list.
# !!! inefficient, use a priority queue
@todo = sort { -($graph{$a}->{d} <=> $graph{$b}->{d}) } @todo;
my $u = pop @todo;
my $u_ = $graph{$u};
foreach my $edge (@{$u_->{edges}}) {
my $v_ = $graph{$edge->{end}};
if ($v_->{d} > $u_->{d} + $edge->{weight}) {
$v_->{d} = $u_->{d} + $edge->{weight};
# Store the edge; to edge->start is actually the
# predecessor.
$v_->{pred} = $edge;
}
}
}
# Retrieve the shortest path from "start" to $targetPath.
my @path = ();
my $cur = $targetPath;
return () unless defined $graph{$targetPath}->{pred};
while ($cur ne "start") {
push @path, $graph{$cur}->{pred};
$cur = $graph{$cur}->{pred}->{start};
}
return @path;
}
# Parse the arguments. # Parse the arguments.
if ($ARGV[0] eq "--query") { if ($ARGV[0] eq "--query") {
@@ -195,7 +46,6 @@ if ($ARGV[0] eq "--query") {
elsif ($cmd eq "info") { elsif ($cmd eq "info") {
my $storePath = <STDIN>; chomp $storePath; my $storePath = <STDIN>; chomp $storePath;
my $info; my $info;
if (defined $narFiles{$storePath}) { if (defined $narFiles{$storePath}) {
$info = @{$narFiles{$storePath}}[0]; $info = @{$narFiles{$storePath}}[0];
@@ -207,32 +57,13 @@ if ($ARGV[0] eq "--query") {
print "0\n"; print "0\n";
next; # not an error next; # not an error
} }
print "1\n"; print "1\n";
print "$info->{deriver}\n"; print "$info->{deriver}\n";
my @references = split " ", $info->{references}; my @references = split " ", $info->{references};
print scalar @references, "\n"; print scalar @references, "\n";
print "$_\n" foreach @references; print "$_\n" foreach @references;
my $size = $info->{size} || 0;
my @path = computeSmallestDownload $storePath; print "$size\n";
my $downloadSize = 0;
while (scalar @path > 0) {
my $edge = pop @path;
my $u = $edge->{start};
my $v = $edge->{end};
if ($edge->{type} eq "patch") {
$downloadSize += $edge->{info}->{size} || 0;
}
elsif ($edge->{type} eq "narfile") {
$downloadSize += $edge->{info}->{size} || 0;
}
}
print "$downloadSize\n";
my $narSize = $info->{narSize} || 0;
print "$narSize\n";
} }
else { die "unknown command `$cmd'"; } else { die "unknown command `$cmd'"; }
@@ -242,19 +73,20 @@ if ($ARGV[0] eq "--query") {
} }
elsif ($ARGV[0] ne "--substitute") { elsif ($ARGV[0] ne "--substitute") {
die; die "syntax: $0 [--query-paths | --query-info PATHS... | --substitute PATH]\n";
} }
die unless scalar @ARGV == 2; die unless scalar @ARGV == 2;
my $targetPath = $ARGV[1]; my $targetPath = $ARGV[1];
$fast = 0;
# Create a temporary directory. # Create a temporary directory.
my $tmpDir = tempdir("nix-download.XXXXXX", CLEANUP => 1, TMPDIR => 1) my $tmpDir = tempdir("nix-download.XXXXXX", CLEANUP => 1, TMPDIR => 1)
or die "cannot create a temporary directory"; or die "cannot create a temporary directory";
chdir $tmpDir or die "cannot change to `$tmpDir': $!";
my $tmpNar = "$tmpDir/nar"; my $tmpNar = "$tmpDir/nar";
my $tmpNar2 = "$tmpDir/nar2"; my $tmpNar2 = "$tmpDir/nar2";
@@ -280,9 +112,148 @@ foreach my $localPath (@{$localPathList}) {
} }
# Compute the shortest path. # Build a graph of all store paths that might contribute to the
my @path = computeSmallestDownload $targetPath; # construction of $targetPath, and the special node "start". The
die "don't know how to produce $targetPath\n" if scalar @path == 0; # edges are either patch operations, or downloads of full NAR files.
# The latter edges only occur between "start" and a store path.
my %graph;
$graph{"start"} = {d => 0, pred => undef, edges => []};
my @queue = ();
my $queueFront = 0;
my %done;
sub addToQueue {
my $v = shift;
return if defined $done{$v};
$done{$v} = 1;
push @queue, $v;
}
sub addNode {
my $u = shift;
$graph{$u} = {d => 999999999999, pred => undef, edges => []}
unless defined $graph{$u};
}
sub addEdge {
my $u = shift;
my $v = shift;
my $w = shift;
my $type = shift;
my $info = shift;
addNode $u;
push @{$graph{$u}->{edges}},
{weight => $w, start => $u, end => $v, type => $type, info => $info};
my $n = scalar @{$graph{$u}->{edges}};
}
addToQueue $targetPath;
sub isValidPath {
my $p = shift;
return system("$binDir/nix-store --check-validity '$p' 2> /dev/null") == 0;
}
sub parseHash {
my $hash = shift;
if ($hash =~ /^(.+):(.+)$/) {
return ($1, $2);
} else {
return ("md5", $hash);
}
}
while ($queueFront < scalar @queue) {
my $u = $queue[$queueFront++];
# print "$u\n";
addNode $u;
# If the path already exists, it has distance 0 from the "start"
# node.
if (isValidPath($u)) {
addEdge "start", $u, 0, "present", undef;
}
else {
# Add patch edges.
my $patchList = $patches{$u};
foreach my $patch (@{$patchList}) {
if (isValidPath($patch->{basePath})) {
# !!! this should be cached
my ($baseHashAlgo, $baseHash) = parseHash $patch->{baseHash};
my $format = "--base32";
$format = "" if $baseHashAlgo eq "md5";
my $hash = `$binDir/nix-hash --type '$baseHashAlgo' $format "$patch->{basePath}"`;
chomp $hash;
if ($hash ne $baseHash) {
print LOGFILE "$$ rejecting $patch->{basePath}\n";
next;
}
}
addToQueue $patch->{basePath};
addEdge $patch->{basePath}, $u, $patch->{size}, "patch", $patch;
}
# Add NAR file edges to the start node.
my $narFileList = $narFiles{$u};
foreach my $narFile (@{$narFileList}) {
# !!! how to handle files whose size is not known in advance?
# For now, assume some arbitrary size (1 MB).
addEdge "start", $u, ($narFile->{size} || 1000000), "narfile", $narFile;
if ($u eq $targetPath) {
my $size = $narFile->{size} || -1;
print LOGFILE "$$ full-download-would-be $size\n";
}
}
}
}
# Run Dijkstra's shortest path algorithm to determine the shortest
# sequence of download and/or patch actions that will produce
# $targetPath.
sub byDistance { # sort by distance, reversed
return -($graph{$a}->{d} <=> $graph{$b}->{d});
}
my @todo = keys %graph;
while (scalar @todo > 0) {
# Remove the closest element from the todo list.
@todo = sort byDistance @todo;
my $u = pop @todo;
my $u_ = $graph{$u};
foreach my $edge (@{$u_->{edges}}) {
my $v_ = $graph{$edge->{end}};
if ($v_->{d} > $u_->{d} + $edge->{weight}) {
$v_->{d} = $u_->{d} + $edge->{weight};
# Store the edge; to edge->start is actually the
# predecessor.
$v_->{pred} = $edge;
}
}
}
# Retrieve the shortest path from "start" to $targetPath.
my @path = ();
my $cur = $targetPath;
die "don't know how to produce $targetPath\n"
unless defined $graph{$targetPath}->{pred};
while ($cur ne "start") {
push @path, $graph{$cur}->{pred};
$cur = $graph{$cur}->{pred}->{start};
}
# Traverse the shortest path, perform the actions described by the # Traverse the shortest path, perform the actions described by the
+408
View File
@@ -0,0 +1,408 @@
#! @perl@ -w -I@libexecdir@/nix
use strict;
use File::Temp qw(tempdir);
use readmanifest;
# Some patch generations options.
# Max size of NAR archives to generate patches for.
my $maxNarSize = $ENV{"NIX_MAX_NAR_SIZE"};
$maxNarSize = 100 * 1024 * 1024 if !defined $maxNarSize;
# If patch is bigger than this fraction of full archive, reject.
my $maxPatchFraction = $ENV{"NIX_PATCH_FRACTION"};
$maxPatchFraction = 0.60 if !defined $maxPatchFraction;
die unless scalar @ARGV == 5;
my $hashAlgo = "sha256";
my $cacheDir = $ARGV[0];
my $patchesDir = $ARGV[1];
my $patchesURL = $ARGV[2];
my $srcDir = $ARGV[3];
my $dstDir = $ARGV[4];
my $tmpDir = tempdir("nix-generate-patches.XXXXXX", CLEANUP => 1, TMPDIR => 1)
or die "cannot create a temporary directory";
print "TEMP = $tmpDir\n";
#END { rmdir $tmpDir; }
my %srcNarFiles;
my %srcLocalPaths;
my %srcPatches;
my %dstNarFiles;
my %dstLocalPaths;
my %dstPatches;
readManifest "$srcDir/MANIFEST",
\%srcNarFiles, \%srcLocalPaths, \%srcPatches;
readManifest "$dstDir/MANIFEST",
\%dstNarFiles, \%dstLocalPaths, \%dstPatches;
sub findOutputPaths {
my $narFiles = shift;
my %outPaths;
foreach my $p (keys %{$narFiles}) {
# Ignore store expressions.
next if ($p =~ /\.store$/);
next if ($p =~ /\.drv$/);
# Ignore builders (too much ambiguity -- they're all called
# `builder.sh').
next if ($p =~ /\.sh$/);
next if ($p =~ /\.patch$/);
# Don't bother including tar files etc.
next if ($p =~ /\.tar\.(gz|bz2)$/ || $p =~ /\.zip$/ || $p =~ /\.bin$/);
$outPaths{$p} = 1;
}
return %outPaths;
}
print "finding src output paths...\n";
my %srcOutPaths = findOutputPaths \%srcNarFiles;
print "finding dst output paths...\n";
my %dstOutPaths = findOutputPaths \%dstNarFiles;
sub getNameVersion {
my $p = shift;
$p =~ /\/[0-9a-z]+((?:-[a-zA-Z][^\/-]*)+)([^\/]*)$/;
my $name = $1;
my $version = $2;
$name =~ s/^-//;
$version =~ s/^-//;
return ($name, $version);
}
# A quick hack to get a measure of the `distance' between two
# versions: it's just the position of the first character that differs
# (or 999 if they are the same).
sub versionDiff {
my $s = shift;
my $t = shift;
my $i;
return 999 if $s eq $t;
for ($i = 0; $i < length $s; $i++) {
return $i if $i >= length $t or
substr($s, $i, 1) ne substr($t, $i, 1);
}
return $i;
}
sub getNarBz2 {
my $narFiles = shift;
my $storePath = shift;
my $narFileList = $$narFiles{$storePath};
die "missing store expression $storePath" unless defined $narFileList;
my $narFile = @{$narFileList}[0];
die unless defined $narFile;
$narFile->{url} =~ /\/([^\/]+)$/;
die unless defined $1;
return "$cacheDir/$1";
}
sub containsPatch {
my $patches = shift;
my $storePath = shift;
my $basePath = shift;
my $patchList = $$patches{$storePath};
return 0 if !defined $patchList;
my $found = 0;
foreach my $patch (@{$patchList}) {
# !!! baseHash might differ
return 1 if $patch->{basePath} eq $basePath;
}
return 0;
}
# Compute the "weighted" number of uses of a path in the build graph.
sub computeUses {
my $narFiles = shift;
my $path = shift;
# Find the deriver of $path.
return 1 unless defined $$narFiles{$path};
my $deriver = @{$$narFiles{$path}}[0]->{deriver};
return 1 unless defined $deriver && $deriver ne "";
# print " DERIVER $deriver\n";
# Optimisation: build the referrers graph from the references
# graph.
my %referrers;
foreach my $q (keys %{$narFiles}) {
my @refs = split " ", @{$$narFiles{$q}}[0]->{references};
foreach my $r (@refs) {
$referrers{$r} = [] unless defined $referrers{$r};
push @{$referrers{$r}}, $q;
}
}
# Determine the shortest path from $deriver to all other reachable
# paths in the `referrers' graph.
my %dist;
$dist{$deriver} = 0;
my @queue = ($deriver);
my $pos = 0;
while ($pos < scalar @queue) {
my $p = $queue[$pos];
$pos++;
foreach my $q (@{$referrers{$p}}) {
if (!defined $dist{$q}) {
$dist{$q} = $dist{$p} + 1;
# print " $q $dist{$q}\n";
push @queue, $q;
}
}
}
my $wuse = 1.0;
foreach my $user (keys %dist) {
next if $user eq $deriver;
# print " $user $dist{$user}\n";
$wuse += 1.0 / 2.0**$dist{$user};
}
# print " XXX $path $wuse\n";
return $wuse;
}
# For each output path in the destination, see if we need to / can
# create a patch.
print "creating patches...\n";
foreach my $p (keys %dstOutPaths) {
# If exactly the same path already exists in the source, skip it.
next if defined $srcOutPaths{$p};
print " $p\n";
# If not, then we should find the paths in the source that are
# `most' likely to be present on a system that wants to install
# this path.
(my $name, my $version) = getNameVersion $p;
my @closest = ();
my $closestVersion;
my $minDist = -1; # actually, larger means closer
# Find all source paths with the same name.
foreach my $q (keys %srcOutPaths) {
(my $name2, my $version2) = getNameVersion $q;
if ($name eq $name2) {
# If the sizes differ too much, then skip. This
# disambiguates between, e.g., a real component and a
# wrapper component (cf. Firefox in Nixpkgs).
my $srcSize = @{$srcNarFiles{$q}}[0]->{size};
my $dstSize = @{$dstNarFiles{$p}}[0]->{size};
my $ratio = $srcSize / $dstSize;
$ratio = 1 / $ratio if $ratio < 1;
# print " SIZE $srcSize $dstSize $ratio $q\n";
if ($ratio >= 3) {
print " SKIPPING $q due to size ratio $ratio ($srcSize $dstSize)\n";
next;
}
# If the numbers of weighted uses differ too much, then
# skip. This disambiguates between, e.g., the bootstrap
# GCC and the final GCC in Nixpkgs.
my $srcUses = computeUses \%srcNarFiles, $q;
my $dstUses = computeUses \%dstNarFiles, $p;
$ratio = $srcUses / $dstUses;
$ratio = 1 / $ratio if $ratio < 1;
print " USE $srcUses $dstUses $ratio $q\n";
# if ($ratio >= 2) {
# print " SKIPPING $q due to use ratio $ratio ($srcUses $dstUses)\n";
# next;
# }
# If there are multiple matching names, include the ones
# with the closest version numbers.
my $dist = versionDiff $version, $version2;
if ($dist > $minDist) {
$minDist = $dist;
@closest = ($q);
$closestVersion = $version2;
} elsif ($dist == $minDist) {
push @closest, $q;
}
}
}
if (scalar(@closest) == 0) {
print " NO BASE: $p\n";
next;
}
foreach my $closest (@closest) {
# Generate a patch between $closest and $p.
print " $p <- $closest\n";
# If the patch already exists, skip it.
if (containsPatch(\%srcPatches, $p, $closest) ||
containsPatch(\%dstPatches, $p, $closest))
{
print " skipping, already exists\n";
next;
}
# next;
my $srcNarBz2 = getNarBz2 \%srcNarFiles, $closest;
my $dstNarBz2 = getNarBz2 \%dstNarFiles, $p;
system("@bunzip2@ < $srcNarBz2 > $tmpDir/A") == 0
or die "cannot unpack $srcNarBz2";
if ((stat "$tmpDir/A")[7] >= $maxNarSize) {
print " skipping, source is too large\n";
next;
}
system("@bunzip2@ < $dstNarBz2 > $tmpDir/B") == 0
or die "cannot unpack $dstNarBz2";
if ((stat "$tmpDir/B")[7] >= $maxNarSize) {
print " skipping, destination is too large\n";
next;
}
system("@libexecdir@/bsdiff $tmpDir/A $tmpDir/B $tmpDir/DIFF") == 0
or die "cannot compute binary diff";
my $baseHash = `@bindir@/nix-hash --flat --type $hashAlgo --base32 $tmpDir/A` or die;
chomp $baseHash;
my $narHash = `@bindir@/nix-hash --flat --type $hashAlgo --base32 $tmpDir/B` or die;
chomp $narHash;
my $narDiffHash = `@bindir@/nix-hash --flat --type $hashAlgo --base32 $tmpDir/DIFF` or die;
chomp $narDiffHash;
my $narDiffSize = (stat "$tmpDir/DIFF")[7];
my $dstNarBz2Size = (stat $dstNarBz2)[7];
print " size $narDiffSize; full size $dstNarBz2Size\n";
if ($narDiffSize >= $dstNarBz2Size) {
print " rejecting; patch bigger than full archive\n";
next;
}
if ($narDiffSize / $dstNarBz2Size >= $maxPatchFraction) {
print " rejecting; patch too large relative to full archive\n";
next;
}
my $finalName =
"$narDiffHash.nar-bsdiff";
if (-e "$patchesDir/$finalName") {
print " not copying, already exists\n";
}
else {
system("cp '$tmpDir/DIFF' '$patchesDir/$finalName.tmp'") == 0
or die "cannot copy diff";
rename("$patchesDir/$finalName.tmp", "$patchesDir/$finalName")
or die "cannot rename $patchesDir/$finalName.tmp";
}
# Add the patch to the manifest.
addPatch \%dstPatches, $p,
{ url => "$patchesURL/$finalName", hash => "$hashAlgo:$narDiffHash"
, size => $narDiffSize, basePath => $closest, baseHash => "$hashAlgo:$baseHash"
, narHash => "$hashAlgo:$narHash", patchType => "nar-bsdiff"
}, 0;
}
}
# Add in any potentially useful patches in the source (namely, those
# patches that produce either paths in the destination or paths that
# can be used as the base for other useful patches).
print "propagating patches...\n";
my $changed;
do {
# !!! we repeat this to reach the transitive closure; inefficient
$changed = 0;
print "loop\n";
my %dstBasePaths;
foreach my $q (keys %dstPatches) {
foreach my $patch (@{$dstPatches{$q}}) {
$dstBasePaths{$patch->{basePath}} = 1;
}
}
foreach my $p (keys %srcPatches) {
my $patchList = $srcPatches{$p};
my $include = 0;
# Is path $p included in the destination? If so, include
# patches that produce it.
$include = 1 if defined $dstNarFiles{$p};
# Is path $p a path that serves as a base for paths in the
# destination? If so, include patches that produce it.
# !!! check baseHash
$include = 1 if defined $dstBasePaths{$p};
if ($include) {
foreach my $patch (@{$patchList}) {
$changed = 1 if addPatch \%dstPatches, $p, $patch;
}
}
}
} while $changed;
# Rewrite the manifest of the destination (with the new patches).
writeManifest "$dstDir/MANIFEST",
\%dstNarFiles, \%dstPatches;
+2 -7
View File
@@ -102,7 +102,7 @@ EOF
$n += 2; $n += 2;
} }
elsif ($arg eq "--max-jobs" or $arg eq "-j" or $arg eq "--max-silent-time" or $arg eq "--log-type" or $arg eq "--cores") { elsif ($arg eq "--max-jobs" or $arg eq "-j" or $arg eq "--max-silent-time" or $arg eq "--log-type") {
$n++; $n++;
die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV; die "$0: `$arg' requires an argument\n" unless $n < scalar @ARGV;
push @buildArgs, ($arg, $ARGV[$n]); push @buildArgs, ($arg, $ARGV[$n]);
@@ -123,11 +123,6 @@ EOF
$verbose = 1; $verbose = 1;
} }
elsif ($arg eq "--quiet") {
push @buildArgs, $arg;
push @instArgs, $arg;
}
elsif (substr($arg, 0, 1) eq "-") { elsif (substr($arg, 0, 1) eq "-") {
push @buildArgs, $arg; push @buildArgs, $arg;
} }
@@ -170,7 +165,7 @@ foreach my $expr (@exprs) {
# Build. # Build.
my @outPaths; my @outPaths;
$pid = open(OUTPATHS, "-|") || exec "$binDir/nix-store", "--add-root", $outLink, "--indirect", "-r", $pid = open(OUTPATHS, "-|") || exec "$binDir/nix-store", "--add-root", $outLink, "--indirect", "-rv",
@buildArgs, @drvPaths; @buildArgs, @drvPaths;
while (<OUTPATHS>) {chomp; push @outPaths, $_;} while (<OUTPATHS>) {chomp; push @outPaths, $_;}
if (!close OUTPATHS) { if (!close OUTPATHS) {
+15 -21
View File
@@ -1,6 +1,4 @@
#! @perl@ -w -I@libexecdir@/nix #! @perl@ -w
use SSH;
my $binDir = $ENV{"NIX_BIN_DIR"} || "@bindir@"; my $binDir = $ENV{"NIX_BIN_DIR"} || "@bindir@";
@@ -16,11 +14,12 @@ EOF
# Get the target host. # Get the target host.
my $sshHost; my $sshHost;
my @sshOpts = split ' ', ($ENV{"NIX_SSHOPTS"} or "");
my $sign = 0; my $sign = 0;
my $compressor = ""; my $compressor = "cat";
my $decompressor = ""; my $decompressor = "cat";
my $toMode = 1; my $toMode = 1;
@@ -35,8 +34,8 @@ while (@ARGV) {
$sign = 1; $sign = 1;
} }
elsif ($arg eq "--gzip") { elsif ($arg eq "--gzip") {
$compressor = "| gzip"; $compressor = "gzip";
$decompressor = "gunzip |"; $decompressor = "gunzip";
} }
elsif ($arg eq "--from") { elsif ($arg eq "--from") {
$toMode = 0; $toMode = 0;
@@ -53,15 +52,12 @@ while (@ARGV) {
} }
openSSHConnection $sshHost or die "$0: unable to start SSH\n";
if ($toMode) { # Copy TO the remote machine. if ($toMode) { # Copy TO the remote machine.
my @allStorePaths; my @allStorePaths;
# Get the closure of this path. # Get the closure of this path.
my $pid = open(READ, "set -f; $binDir/nix-store --query --requisites @storePaths|") or die; my $pid = open(READ, "$binDir/nix-store --query --requisites @storePaths|") or die;
while (<READ>) { while (<READ>) {
chomp; chomp;
@@ -73,10 +69,11 @@ if ($toMode) { # Copy TO the remote machine.
# Ask the remote host which paths are invalid. # Ask the remote host which paths are invalid.
open(READ, "set -f; ssh $sshHost @sshOpts nix-store --check-validity --print-invalid @allStorePaths|"); open(READ, "ssh @sshOpts $sshHost nix-store --check-validity --print-invalid @allStorePaths|");
my @missing = (); my @missing = ();
while (<READ>) { while (<READ>) {
chomp; chomp;
print STDERR "target machine needs $_\n";
push @missing, $_; push @missing, $_;
} }
close READ or die; close READ or die;
@@ -84,11 +81,9 @@ if ($toMode) { # Copy TO the remote machine.
# Export the store paths and import them on the remote machine. # Export the store paths and import them on the remote machine.
if (scalar @missing > 0) { if (scalar @missing > 0) {
print STDERR "copying these missing paths:\n";
print STDERR " $_\n" foreach @missing;
my $extraOpts = ""; my $extraOpts = "";
$extraOpts .= "--sign" if $sign == 1; $extraOpts .= "--sign" if $sign == 1;
system("set -f; nix-store --export $extraOpts @missing $compressor | ssh $sshHost @sshOpts '$decompressor nix-store --import'") == 0 system("nix-store --export $extraOpts @missing | $compressor | ssh @sshOpts $sshHost '$decompressor | nix-store --import'") == 0
or die "copying store paths to remote machine `$sshHost' failed: $?"; or die "copying store paths to remote machine `$sshHost' failed: $?";
} }
@@ -101,7 +96,7 @@ else { # Copy FROM the remote machine.
# machine. Paths are assumed to be store paths; there is no # machine. Paths are assumed to be store paths; there is no
# resolution (following of symlinks). # resolution (following of symlinks).
my $pid = open(READ, my $pid = open(READ,
"set -f; ssh @sshOpts $sshHost nix-store --query --requisites @storePaths|") or die; "ssh @sshOpts $sshHost nix-store --query --requisites @storePaths|") or die;
my @allStorePaths; my @allStorePaths;
@@ -115,10 +110,11 @@ else { # Copy FROM the remote machine.
# What paths are already valid locally? # What paths are already valid locally?
open(READ, "set -f; @bindir@/nix-store --check-validity --print-invalid @allStorePaths|"); open(READ, "@bindir@/nix-store --check-validity --print-invalid @allStorePaths|");
my @missing = (); my @missing = ();
while (<READ>) { while (<READ>) {
chomp; chomp;
print STDERR "local machine needs $_\n";
push @missing, $_; push @missing, $_;
} }
close READ or die; close READ or die;
@@ -126,12 +122,10 @@ else { # Copy FROM the remote machine.
# Export the store paths on the remote machine and import them on locally. # Export the store paths on the remote machine and import them on locally.
if (scalar @missing > 0) { if (scalar @missing > 0) {
print STDERR "copying these missing paths:\n";
print STDERR " $_\n" foreach @missing;
my $extraOpts = ""; my $extraOpts = "";
$extraOpts .= "--sign" if $sign == 1; $extraOpts .= "--sign" if $sign == 1;
system("set -f; ssh $sshHost @sshOpts 'nix-store --export $extraOpts @missing $compressor' | $decompressor @bindir@/nix-store --import") == 0 system("ssh @sshOpts $sshHost 'nix-store --export $extraOpts @missing | $compressor' | $decompressor | @bindir@/nix-store --import") == 0
or die "copying store paths from remote machine `$sshHost' failed: $?"; or die "copying store paths to remote machine `$sshHost' failed: $?";
} }
} }
-42
View File
@@ -1,42 +0,0 @@
#! @perl@ -w -I@libexecdir@/nix
use strict;
use File::Temp qw(tempdir);
use NixManifest;
use GeneratePatches;
if (scalar @ARGV != 5) {
print STDERR <<EOF;
Usage: nix-generate-patches NAR-DIR PATCH-DIR PATCH-URI OLD-MANIFEST NEW-MANIFEST
This command generates binary patches between NAR files listed in
OLD-MANIFEST and NEW-MANIFEST. The patches are written to the
directory PATCH-DIR, and the prefix PATCH-URI is used to generate URIs
for the patches. The patches are added to NEW-MANIFEST. All NARs are
required to exist in NAR-DIR. Patches are generated between
succeeding versions of packages with the same name.
EOF
exit 1;
}
my $narPath = $ARGV[0];
my $patchesPath = $ARGV[1];
my $patchesURL = $ARGV[2];
my $srcManifest = $ARGV[3];
my $dstManifest = $ARGV[4];
my (%srcNarFiles, %srcLocalPaths, %srcPatches);
readManifest $srcManifest, \%srcNarFiles, \%srcLocalPaths, \%srcPatches;
my (%dstNarFiles, %dstLocalPaths, %dstPatches);
readManifest $dstManifest, \%dstNarFiles, \%dstLocalPaths, \%dstPatches;
my $tmpDir = tempdir("nix-generate-patches.XXXXXX", CLEANUP => 1, TMPDIR => 1)
or die "cannot create a temporary directory";
generatePatches \%srcNarFiles, \%dstNarFiles, \%srcPatches, \%dstPatches,
$narPath, $patchesPath, $patchesURL, $tmpDir;
propagatePatches \%srcPatches, \%dstNarFiles, \%dstPatches;
writeManifest $dstManifest, \%dstNarFiles, \%dstPatches;
+1 -4
View File
@@ -21,10 +21,7 @@ if test -z "$url"; then
exit 1 exit 1
fi fi
# Handle escaped characters in the URI. `+', `=' and `?' are the only name=$(basename "$url")
# characters that are valid in Nix store path names but have a special
# meaning in URIs.
name=$(basename "$url" | @sed@ -e 's/%2b/+/g' -e 's/%3d/=/g' -e 's/%3f/\?/g')
if test -z "$name"; then echo "invalid url"; exit 1; fi if test -z "$name"; then echo "invalid url"; exit 1; fi
+1 -1
View File
@@ -2,7 +2,7 @@
use strict; use strict;
use File::Temp qw(tempdir); use File::Temp qw(tempdir);
use NixManifest; use readmanifest;
my $tmpDir = tempdir("nix-pull.XXXXXX", CLEANUP => 1, TMPDIR => 1) my $tmpDir = tempdir("nix-pull.XXXXXX", CLEANUP => 1, TMPDIR => 1)
or die "cannot create a temporary directory"; or die "cannot create a temporary directory";
+8 -11
View File
@@ -2,7 +2,7 @@
use strict; use strict;
use File::Temp qw(tempdir); use File::Temp qw(tempdir);
use NixManifest; use readmanifest;
my $hashAlgo = "sha256"; my $hashAlgo = "sha256";
@@ -172,6 +172,12 @@ for (my $n = 0; $n < scalar @storePaths; $n++) {
$narbz2Hash =~ /^[0-9a-z]+$/ or die "invalid hash"; $narbz2Hash =~ /^[0-9a-z]+$/ or die "invalid hash";
close HASH; close HASH;
open HASH, "$narDir/nar-hash" or die "cannot open nar-hash";
my $narHash = <HASH>;
chomp $narHash;
$narHash =~ /^[0-9a-z]+$/ or die "invalid hash";
close HASH;
my $narName = "$narbz2Hash.nar.bz2"; my $narName = "$narbz2Hash.nar.bz2";
my $narFile = "$narDir/$narName"; my $narFile = "$narDir/$narName";
@@ -189,14 +195,6 @@ for (my $n = 0; $n < scalar @storePaths; $n++) {
chomp $deriver; chomp $deriver;
$deriver = "" if $deriver eq "unknown-deriver"; $deriver = "" if $deriver eq "unknown-deriver";
my $narHash = `$binDir/nix-store --query --hash '$storePath'`;
die "cannot query hash for `$storePath'" if $? != 0;
chomp $narHash;
my $narSize = `$binDir/nix-store --query --size '$storePath'`;
die "cannot query size for `$storePath'" if $? != 0;
chomp $narSize;
my $url; my $url;
if ($localCopy) { if ($localCopy) {
$url = "$targetArchivesUrl/$narName"; $url = "$targetArchivesUrl/$narName";
@@ -207,8 +205,7 @@ for (my $n = 0; $n < scalar @storePaths; $n++) {
{ url => $url { url => $url
, hash => "$hashAlgo:$narbz2Hash" , hash => "$hashAlgo:$narbz2Hash"
, size => $narbz2Size , size => $narbz2Size
, narHash => "$narHash" , narHash => "$hashAlgo:$narHash"
, narSize => $narSize
, references => $references , references => $references
, deriver => $deriver , deriver => $deriver
} }
File renamed without changes.
@@ -33,8 +33,18 @@ sub readManifest {
my $manifestVersion = 2; my $manifestVersion = 2;
my ($storePath, $url, $hash, $size, $basePath, $baseHash, $patchType); my $storePath;
my ($narHash, $narSize, $references, $deriver, $hashAlgo, $copyFrom, $system); my $url;
my $hash;
my $size;
my $basePath;
my $baseHash;
my $patchType;
my $narHash;
my $references;
my $deriver;
my $hashAlgo;
my $copyFrom;
while (<MANIFEST>) { while (<MANIFEST>) {
chomp; chomp;
@@ -52,11 +62,9 @@ sub readManifest {
undef $hash; undef $hash;
undef $size; undef $size;
undef $narHash; undef $narHash;
undef $narSize;
undef $basePath; undef $basePath;
undef $baseHash; undef $baseHash;
undef $patchType; undef $patchType;
undef $system;
$references = ""; $references = "";
$deriver = ""; $deriver = "";
$hashAlgo = "md5"; $hashAlgo = "md5";
@@ -81,10 +89,8 @@ sub readManifest {
if (!$found) { if (!$found) {
push @{$narFileList}, push @{$narFileList},
{ url => $url, hash => $hash, size => $size { url => $url, hash => $hash, size => $size
, narHash => $narHash, narSize => $narSize , narHash => $narHash, references => $references
, references => $references
, deriver => $deriver, hashAlgo => $hashAlgo , deriver => $deriver, hashAlgo => $hashAlgo
, system => $system
}; };
} }
@@ -94,8 +100,8 @@ sub readManifest {
addPatch $patches, $storePath, addPatch $patches, $storePath,
{ url => $url, hash => $hash, size => $size { url => $url, hash => $hash, size => $size
, basePath => $basePath, baseHash => $baseHash , basePath => $basePath, baseHash => $baseHash
, narHash => $narHash, narSize => $narSize , narHash => $narHash, patchType => $patchType
, patchType => $patchType, hashAlgo => $hashAlgo , hashAlgo => $hashAlgo
}; };
} }
@@ -126,11 +132,9 @@ sub readManifest {
elsif (/^\s*BaseHash:\s*(\S+)\s*$/) { $baseHash = $1; } elsif (/^\s*BaseHash:\s*(\S+)\s*$/) { $baseHash = $1; }
elsif (/^\s*Type:\s*(\S+)\s*$/) { $patchType = $1; } elsif (/^\s*Type:\s*(\S+)\s*$/) { $patchType = $1; }
elsif (/^\s*NarHash:\s*(\S+)\s*$/) { $narHash = $1; } elsif (/^\s*NarHash:\s*(\S+)\s*$/) { $narHash = $1; }
elsif (/^\s*NarSize:\s*(\d+)\s*$/) { $narSize = $1; }
elsif (/^\s*References:\s*(.*)\s*$/) { $references = $1; } elsif (/^\s*References:\s*(.*)\s*$/) { $references = $1; }
elsif (/^\s*Deriver:\s*(\S+)\s*$/) { $deriver = $1; } elsif (/^\s*Deriver:\s*(\S+)\s*$/) { $deriver = $1; }
elsif (/^\s*ManifestVersion:\s*(\d+)\s*$/) { $manifestVersion = $1; } elsif (/^\s*ManifestVersion:\s*(\d+)\s*$/) { $manifestVersion = $1; }
elsif (/^\s*System:\s*(\S+)\s*$/) { $system = $1; }
# Compatibility; # Compatibility;
elsif (/^\s*NarURL:\s*(\S+)\s*$/) { $url = $1; } elsif (/^\s*NarURL:\s*(\S+)\s*$/) { $url = $1; }
@@ -146,7 +150,7 @@ sub readManifest {
sub writeManifest { sub writeManifest {
my ($manifest, $narFiles, $patches, $noCompress) = @_; my ($manifest, $narFiles, $patches) = @_;
open MANIFEST, ">$manifest.tmp"; # !!! check exclusive open MANIFEST, ">$manifest.tmp"; # !!! check exclusive
@@ -161,14 +165,12 @@ sub writeManifest {
print MANIFEST " StorePath: $storePath\n"; print MANIFEST " StorePath: $storePath\n";
print MANIFEST " NarURL: $narFile->{url}\n"; print MANIFEST " NarURL: $narFile->{url}\n";
print MANIFEST " Hash: $narFile->{hash}\n" if defined $narFile->{hash}; print MANIFEST " Hash: $narFile->{hash}\n" if defined $narFile->{hash};
print MANIFEST " Size: $narFile->{size}\n" if defined $narFile->{size};
print MANIFEST " NarHash: $narFile->{narHash}\n"; print MANIFEST " NarHash: $narFile->{narHash}\n";
print MANIFEST " NarSize: $narFile->{narSize}\n" if $narFile->{narSize}; print MANIFEST " Size: $narFile->{size}\n" if defined $narFile->{size};
print MANIFEST " References: $narFile->{references}\n" print MANIFEST " References: $narFile->{references}\n"
if defined $narFile->{references} && $narFile->{references} ne ""; if defined $narFile->{references} && $narFile->{references} ne "";
print MANIFEST " Deriver: $narFile->{deriver}\n" print MANIFEST " Deriver: $narFile->{deriver}\n"
if defined $narFile->{deriver} && $narFile->{deriver} ne ""; if defined $narFile->{deriver} && $narFile->{deriver} ne "";
print MANIFEST " System: $narFile->{system}\n" if defined $narFile->{system};
print MANIFEST "}\n"; print MANIFEST "}\n";
} }
} }
@@ -180,9 +182,8 @@ sub writeManifest {
print MANIFEST " StorePath: $storePath\n"; print MANIFEST " StorePath: $storePath\n";
print MANIFEST " NarURL: $patch->{url}\n"; print MANIFEST " NarURL: $patch->{url}\n";
print MANIFEST " Hash: $patch->{hash}\n"; print MANIFEST " Hash: $patch->{hash}\n";
print MANIFEST " Size: $patch->{size}\n";
print MANIFEST " NarHash: $patch->{narHash}\n"; print MANIFEST " NarHash: $patch->{narHash}\n";
print MANIFEST " NarSize: $patch->{narSize}\n" if $patch->{narSize}; print MANIFEST " Size: $patch->{size}\n";
print MANIFEST " BasePath: $patch->{basePath}\n"; print MANIFEST " BasePath: $patch->{basePath}\n";
print MANIFEST " BaseHash: $patch->{baseHash}\n"; print MANIFEST " BaseHash: $patch->{baseHash}\n";
print MANIFEST " Type: $patch->{patchType}\n"; print MANIFEST " Type: $patch->{patchType}\n";
@@ -198,13 +199,11 @@ sub writeManifest {
# Create a bzipped manifest. # Create a bzipped manifest.
unless (defined $noCompress) { system("@bzip2@ < $manifest > $manifest.bz2.tmp") == 0
system("@bzip2@ < $manifest > $manifest.bz2.tmp") == 0 or die "cannot compress manifest";
or die "cannot compress manifest";
rename("$manifest.bz2.tmp", "$manifest.bz2") rename("$manifest.bz2.tmp", "$manifest.bz2")
or die "cannot rename $manifest.bz2.tmp: $!"; or die "cannot rename $manifest.bz2.tmp: $!";
}
} }
+2
View File
@@ -1,3 +1,5 @@
SUBDIRS = bin2c boost libutil libstore libmain nix-store nix-hash \ SUBDIRS = bin2c boost libutil libstore libmain nix-store nix-hash \
libexpr nix-instantiate nix-env nix-worker nix-setuid-helper \ libexpr nix-instantiate nix-env nix-worker nix-setuid-helper \
nix-log2xml bsdiff-4.3 nix-log2xml bsdiff-4.3
EXTRA_DIST = aterm-helper.pl
+178
View File
@@ -0,0 +1,178 @@
#! /usr/bin/perl -w
# This program generates C/C++ code for efficiently manipulating
# ATerms. It generates functions to build and match ATerms according
# to a set of constructor definitions defined in a file read from
# standard input. A constructor is defined by a line with the
# following format:
#
# SYM | ARGS | TYPE | FUN?
#
# where SYM is the name of the constructor, ARGS is a
# whitespace-separated list of argument types, TYPE is the type of the
# resulting ATerm (which should be `ATerm' or a type synonym for
# `ATerm'), and the optional FUN is used to construct the names of the
# build and match functions (it defaults to SYM; overriding it is
# useful if there are overloaded constructors, e.g., with different
# arities). Note that SYM may be empty.
#
# A line of the form
#
# VAR = EXPR
#
# causes a ATerm variable to be generated that is initialised to the
# value EXPR.
#
# Finally, a line of the form
#
# init NAME
#
# causes the initialisation function to be called `NAME'. This
# function must be called before any of the build/match functions or
# the generated variables are used.
die if scalar @ARGV != 2;
my $syms = "";
my $init = "";
my $initFun = "init";
open HEADER, ">$ARGV[0]";
open IMPL, ">$ARGV[1]";
print HEADER "#include <aterm2.h>\n";
print HEADER "#ifdef __cplusplus\n";
print HEADER "namespace nix {\n";
print HEADER "#endif\n\n\n";
print IMPL "namespace nix {\n";
while (<STDIN>) {
s/\#.*//;
next if (/^\s*$/);
if (/^\s*(\w*)\s*\|([^\|]*)\|\s*(\w+)\s*\|\s*(\w+)?/) {
my $const = $1;
my @types = split ' ', $2;
my $result = $3;
my $funname = $4;
$funname = $const unless defined $funname;
my $formals = "";
my $formals2 = "";
my $args = "";
my $unpack = "";
my $n = 1;
foreach my $type (@types) {
my $realType = $type;
$args .= ", ";
if ($type eq "string") {
# $args .= "(ATerm) ATmakeAppl0(ATmakeAFun((char *) e$n, 0, ATtrue))";
# $type = "const char *";
$type = "ATerm";
$args .= "e$n";
# !!! in the matcher, we should check that the
# argument is a string (i.e., a nullary application).
} elsif ($type eq "int") {
$args .= "(ATerm) ATmakeInt(e$n)";
} elsif ($type eq "ATermList" || $type eq "ATermBlob") {
$args .= "(ATerm) e$n";
} else {
$args .= "e$n";
}
$formals .= ", " if $formals ne "";
$formals .= "$type e$n";
$formals2 .= ", ";
$formals2 .= "$type & e$n";
my $m = $n - 1;
# !!! more checks here
if ($type eq "int") {
$unpack .= " e$n = ATgetInt((ATermInt) ATgetArgument(e, $m));\n";
} elsif ($type eq "ATermList") {
$unpack .= " e$n = (ATermList) ATgetArgument(e, $m);\n";
} elsif ($type eq "ATermBlob") {
$unpack .= " e$n = (ATermBlob) ATgetArgument(e, $m);\n";
} elsif ($realType eq "string") {
$unpack .= " e$n = ATgetArgument(e, $m);\n";
$unpack .= " if (ATgetType(e$n) != AT_APPL) return false;\n";
} else {
$unpack .= " e$n = ATgetArgument(e, $m);\n";
}
$n++;
}
my $arity = scalar @types;
print HEADER "extern AFun sym$funname;\n\n";
print IMPL "AFun sym$funname = 0;\n";
if ($arity == 0) {
print HEADER "extern ATerm const$funname;\n\n";
print IMPL "ATerm const$funname = 0;\n";
}
print HEADER "static inline $result make$funname($formals) __attribute__ ((pure, nothrow));\n";
print HEADER "static inline $result make$funname($formals) {\n";
if ($arity == 0) {
print HEADER " return const$funname;\n";
}
elsif ($arity <= 6) {
print HEADER " return (ATerm) ATmakeAppl$arity(sym$funname$args);\n";
} else {
$args =~ s/^,//;
print HEADER " ATerm array[$arity] = {$args};\n";
print HEADER " return (ATerm) ATmakeApplArray(sym$funname, array);\n";
}
print HEADER "}\n\n";
print HEADER "#ifdef __cplusplus\n";
print HEADER "static inline bool match$funname(ATerm e$formals2) {\n";
print HEADER " if (ATgetType(e) != AT_APPL || (AFun) ATgetAFun(e) != sym$funname) return false;\n";
print HEADER "$unpack";
print HEADER " return true;\n";
print HEADER "}\n";
print HEADER "#endif\n\n\n";
$init .= " sym$funname = ATmakeAFun(\"$const\", $arity, ATfalse);\n";
$init .= " ATprotectAFun(sym$funname);\n";
if ($arity == 0) {
$init .= " const$funname = (ATerm) ATmakeAppl0(sym$funname);\n";
$init .= " ATprotect(&const$funname);\n";
}
}
elsif (/^\s*(\w+)\s*=\s*(.*)$/) {
my $name = $1;
my $value = $2;
print HEADER "extern ATerm $name;\n";
print IMPL "ATerm $name = 0;\n";
$init .= " $name = $value;\n";
}
elsif (/^\s*init\s+(\w+)\s*$/) {
$initFun = $1;
}
else {
die "bad line: `$_'";
}
}
print HEADER "void $initFun();\n\n";
print HEADER "static inline const char * aterm2String(ATerm t) {\n";
print HEADER " return (const char *) ATgetName(ATgetAFun(t));\n";
print HEADER "}\n\n";
print IMPL "\n";
print IMPL "void $initFun() {\n";
print IMPL "$init";
print IMPL "}\n";
print HEADER "#ifdef __cplusplus\n";
print HEADER "}\n";
print HEADER "#endif\n\n\n";
print IMPL "}\n";
close HEADER;
close IMPL;
-3
View File
@@ -1,6 +1,3 @@
noinst_PROGRAMS = bin2c noinst_PROGRAMS = bin2c
bin2c_SOURCES = bin2c.c bin2c_SOURCES = bin2c.c
bin2c$(EXEEXT): bin2c.c
$(CC_FOR_BUILD) $(CFLAGS_FOR_BUILD) -o bin2c bin2c.c
+2 -2
View File
@@ -14,10 +14,10 @@ int main(int argc, char * * argv)
{ {
int c; int c;
if (argc != 2) abort(); if (argc != 2) abort();
print("static unsigned char %s[] = { ", argv[1]); print("static unsigned char %s[] = {", argv[1]);
while ((c = getchar()) != EOF) { while ((c = getchar()) != EOF) {
print("0x%02x, ", (unsigned char) c); print("0x%02x, ", (unsigned char) c);
} }
print("0 };\n"); print("};\n");
return 0; return 0;
} }
+1 -3
View File
@@ -1,5 +1,3 @@
EXTRA_DIST = compat-include
libexec_PROGRAMS = bsdiff bspatch libexec_PROGRAMS = bsdiff bspatch
bsdiff_SOURCES = bsdiff.c bsdiff_SOURCES = bsdiff.c
@@ -10,4 +8,4 @@ bspatch_SOURCES = bspatch.c
bspatch_LDADD = ${bzip2_lib} bspatch_LDADD = ${bzip2_lib}
AM_CFLAGS = -O3 ${bzip2_include} ${bsddiff_compat_include} AM_CFLAGS = -O3 ${bzip2_include}
-1
View File
@@ -277,7 +277,6 @@ int main(int argc,char *argv[])
for(scsc=scan+=len;scan<newsize;scan++) { for(scsc=scan+=len;scan<newsize;scan++) {
len=search(I,old,oldsize,new+scan,newsize-scan, len=search(I,old,oldsize,new+scan,newsize-scan,
0,oldsize,&pos); 0,oldsize,&pos);
if (len > 64 * 1024) break;
for(;scsc<scan+len;scsc++) for(;scsc<scan+len;scsc++)
if((scsc+lastoffset<oldsize) && if((scsc+lastoffset<oldsize) &&
-12
View File
@@ -1,12 +0,0 @@
/* Simulate BSD's <err.h> functionality. */
#ifndef COMPAT_ERR_H_INCLUDED
#define COMPAT_ERR_H_INCLUDED 1
#include <stdio.h>
#include <stdlib.h>
#define err(rc,...) do { fprintf(stderr,__VA_ARGS__); exit(rc); } while(0)
#define errx(rc,...) do { fprintf(stderr,__VA_ARGS__); exit(rc); } while(0)
#endif
+18 -7
View File
@@ -2,25 +2,27 @@ pkglib_LTLIBRARIES = libexpr.la
libexpr_la_SOURCES = \ libexpr_la_SOURCES = \
nixexpr.cc eval.cc primops.cc lexer-tab.cc parser-tab.cc \ nixexpr.cc eval.cc primops.cc lexer-tab.cc parser-tab.cc \
get-drvs.cc attr-path.cc value-to-xml.cc common-opts.cc \ get-drvs.cc attr-path.cc expr-to-xml.cc common-opts.cc \
names.cc names.cc
pkginclude_HEADERS = \ pkginclude_HEADERS = \
nixexpr.hh eval.hh parser.hh lexer-tab.hh parser-tab.hh \ nixexpr.hh eval.hh parser.hh lexer-tab.hh parser-tab.hh \
get-drvs.hh attr-path.hh value-to-xml.hh common-opts.hh \ get-drvs.hh attr-path.hh expr-to-xml.hh common-opts.hh \
names.hh symbol-table.hh names.hh nixexpr-ast.hh
libexpr_la_LIBADD = ../libutil/libutil.la ../libstore/libstore.la \ libexpr_la_LIBADD = ../libutil/libutil.la ../libstore/libstore.la \
../boost/format/libformat.la @boehmgc_lib@ ../boost/format/libformat.la
BUILT_SOURCES = \ BUILT_SOURCES = nixexpr-ast.cc nixexpr-ast.hh \
parser-tab.hh lexer-tab.hh parser-tab.cc lexer-tab.cc parser-tab.hh lexer-tab.hh parser-tab.cc lexer-tab.cc
EXTRA_DIST = lexer.l parser.y EXTRA_DIST = lexer.l parser.y nixexpr-ast.def nixexpr-ast.cc
AM_CXXFLAGS = \ AM_CXXFLAGS = \
-I$(srcdir)/.. \ -I$(srcdir)/.. ${aterm_include} \
-I$(srcdir)/../libutil -I$(srcdir)/../libstore -I$(srcdir)/../libutil -I$(srcdir)/../libstore
AM_CFLAGS = \
${aterm_include}
# Parser generation. # Parser generation.
@@ -32,6 +34,15 @@ lexer-tab.cc lexer-tab.hh: lexer.l
$(flex) --outfile lexer-tab.cc --header-file=lexer-tab.hh $(srcdir)/lexer.l $(flex) --outfile lexer-tab.cc --header-file=lexer-tab.hh $(srcdir)/lexer.l
# ATerm helper function generation.
nixexpr-ast.cc nixexpr-ast.hh: ../aterm-helper.pl nixexpr-ast.def
$(perl) $(srcdir)/../aterm-helper.pl nixexpr-ast.hh nixexpr-ast.cc < $(srcdir)/nixexpr-ast.def
CLEANFILES =
# SDF stuff (not built by default). # SDF stuff (not built by default).
nix.tbl: nix.sdf nix.tbl: nix.sdf
sdf2table -m Nix -s -i nix.sdf -o nix.tbl sdf2table -m Nix -s -i nix.sdf -o nix.tbl
+32 -22
View File
@@ -1,13 +1,23 @@
#include "attr-path.hh" #include "attr-path.hh"
#include "nixexpr-ast.hh"
#include "util.hh" #include "util.hh"
namespace nix { namespace nix {
// !!! Shouldn't we return a pointer to a Value? bool isAttrs(EvalState & state, Expr e, ATermMap & attrs)
void findAlongAttrPath(EvalState & state, const string & attrPath, {
Bindings & autoArgs, Expr * e, Value & v) e = evalExpr(state, e);
ATermList dummy;
if (!matchAttrs(e, dummy)) return false;
queryAllAttrs(e, attrs, false);
return true;
}
Expr findAlongAttrPath(EvalState & state, const string & attrPath,
const ATermMap & autoArgs, Expr e)
{ {
Strings tokens = tokenizeString(attrPath, "."); Strings tokens = tokenizeString(attrPath, ".");
@@ -15,10 +25,8 @@ void findAlongAttrPath(EvalState & state, const string & attrPath,
Error(format("attribute selection path `%1%' does not match expression") % attrPath); Error(format("attribute selection path `%1%' does not match expression") % attrPath);
string curPath; string curPath;
state.mkThunk_(v, e);
foreach (Strings::iterator, i, tokens) { for (Strings::iterator i = tokens.begin(); i != tokens.end(); ++i) {
if (!curPath.empty()) curPath += "."; if (!curPath.empty()) curPath += ".";
curPath += *i; curPath += *i;
@@ -30,10 +38,7 @@ void findAlongAttrPath(EvalState & state, const string & attrPath,
if (string2Int(attr, attrIndex)) apType = apIndex; if (string2Int(attr, attrIndex)) apType = apIndex;
/* Evaluate the expression. */ /* Evaluate the expression. */
Value vTmp; e = evalExpr(state, autoCallFunction(evalExpr(state, e), autoArgs));
state.autoCallFunction(autoArgs, v, vTmp);
v = vTmp;
state.forceValue(v);
/* It should evaluate to either an attribute set or an /* It should evaluate to either an attribute set or an
expression, according to what is specified in the expression, according to what is specified in the
@@ -41,31 +46,36 @@ void findAlongAttrPath(EvalState & state, const string & attrPath,
if (apType == apAttr) { if (apType == apAttr) {
if (v.type != tAttrs) ATermMap attrs;
if (!isAttrs(state, e, attrs))
throw TypeError( throw TypeError(
format("the expression selected by the selection path `%1%' should be an attribute set but is %2%") format("the expression selected by the selection path `%1%' should be an attribute set but is %2%")
% curPath % showType(v)); % curPath % showType(e));
Bindings::iterator a = v.attrs->find(state.symbols.create(attr)); e = attrs.get(toATerm(attr));
if (a == v.attrs->end()) if (!e)
throw Error(format("attribute `%1%' in selection path `%2%' not found") % attr % curPath); throw Error(format("attribute `%1%' in selection path `%2%' not found") % attr % curPath);
v = *a->value;
} }
else if (apType == apIndex) { else if (apType == apIndex) {
if (v.type != tList) ATermList es;
if (!matchList(e, es))
throw TypeError( throw TypeError(
format("the expression selected by the selection path `%1%' should be a list but is %2%") format("the expression selected by the selection path `%1%' should be a list but is %2%")
% curPath % showType(v)); % curPath % showType(e));
if (attrIndex >= v.list.length) e = ATelementAt(es, attrIndex);
throw Error(format("list index %1% in selection path `%2%' is out of range") % attrIndex % curPath); if (!e)
throw Error(format("list index %1% in selection path `%2%' not found") % attrIndex % curPath);
v = *v.list.elems[attrIndex];
} }
} }
return e;
} }
+4 -4
View File
@@ -1,17 +1,17 @@
#ifndef __ATTR_PATH_H #ifndef __ATTR_PATH_H
#define __ATTR_PATH_H #define __ATTR_PATH_H
#include "eval.hh"
#include <string> #include <string>
#include <map> #include <map>
#include "eval.hh"
namespace nix { namespace nix {
void findAlongAttrPath(EvalState & state, const string & attrPath, Expr findAlongAttrPath(EvalState & state, const string & attrPath,
Bindings & autoArgs, Expr * e, Value & v); const ATermMap & autoArgs, Expr e);
} }
+7 -13
View File
@@ -9,7 +9,7 @@ namespace nix {
bool parseOptionArg(const string & arg, Strings::iterator & i, bool parseOptionArg(const string & arg, Strings::iterator & i,
const Strings::iterator & argsEnd, EvalState & state, const Strings::iterator & argsEnd, EvalState & state,
Bindings & autoArgs) ATermMap & autoArgs)
{ {
if (arg != "--arg" && arg != "--argstr") return false; if (arg != "--arg" && arg != "--argstr") return false;
@@ -19,18 +19,12 @@ bool parseOptionArg(const string & arg, Strings::iterator & i,
string name = *i++; string name = *i++;
if (i == argsEnd) throw error; if (i == argsEnd) throw error;
string value = *i++; string value = *i++;
/* !!! check for duplicates! */ Expr e = arg == "--arg"
Value * v = state.allocValue(); ? parseExprFromString(state, value, absPath("."))
autoArgs.push_back(Attr(state.symbols.create(name), v)); : makeStr(value);
autoArgs.set(toATerm(name), e);
if (arg == "--arg")
state.mkThunk_(*v, parseExprFromString(state, value, absPath(".")));
else
mkString(*v, value);
autoArgs.sort(); // !!! inefficient
return true; return true;
} }
+1 -1
View File
@@ -9,7 +9,7 @@ namespace nix {
/* Some common option parsing between nix-env and nix-instantiate. */ /* Some common option parsing between nix-env and nix-instantiate. */
bool parseOptionArg(const string & arg, Strings::iterator & i, bool parseOptionArg(const string & arg, Strings::iterator & i,
const Strings::iterator & argsEnd, EvalState & state, const Strings::iterator & argsEnd, EvalState & state,
Bindings & autoArgs); ATermMap & autoArgs);
} }
+749 -1046
View File
File diff suppressed because it is too large. Load diff
+58 -317
View File
@@ -1,193 +1,19 @@
#ifndef __EVAL_H #ifndef __EVAL_H
#define __EVAL_H #define __EVAL_H
#include "nixexpr.hh"
#include "symbol-table.hh"
#include "hash.hh"
#include <map> #include <map>
#if HAVE_BOEHMGC #include "aterm.hh"
#include <gc/gc_allocator.h> #include "nixexpr.hh"
#endif
namespace nix { namespace nix {
class EvalState; class Hash;
struct Env;
struct Value;
struct Attr;
/* Attribute sets are represented as a vector of attributes, sorted by
symbol (i.e. pointer to the attribute name in the symbol table). */
#if HAVE_BOEHMGC
typedef std::vector<Attr, gc_allocator<Attr> > BindingsBase;
#else
typedef std::vector<Attr> BindingsBase;
#endif
class Bindings : public BindingsBase
{
public:
iterator find(const Symbol & name);
void sort();
};
typedef enum {
tInt = 1,
tBool,
tString,
tPath,
tNull,
tAttrs,
tList,
tThunk,
tApp,
tLambda,
tBlackhole,
tPrimOp,
tPrimOpApp,
} ValueType;
typedef void (* PrimOpFun) (EvalState & state, Value * * args, Value & v);
struct PrimOp
{
PrimOpFun fun;
unsigned int arity;
Symbol name;
PrimOp(PrimOpFun fun, unsigned int arity, Symbol name)
: fun(fun), arity(arity), name(name) { }
};
struct Value
{
ValueType type;
union
{
int integer;
bool boolean;
/* Strings in the evaluator carry a so-called `context' (the
ATermList) which is a list of strings representing store
paths. This is to allow users to write things like
"--with-freetype2-library=" + freetype + "/lib"
where `freetype' is a derivation (or a source to be copied
to the store). If we just concatenated the strings without
keeping track of the referenced store paths, then if the
string is used as a derivation attribute, the derivation
will not have the correct dependencies in its inputDrvs and
inputSrcs.
The semantics of the context is as follows: when a string
with context C is used as a derivation attribute, then the
derivations in C will be added to the inputDrvs of the
derivation, and the other store paths in C will be added to
the inputSrcs of the derivations.
For canonicity, the store paths should be in sorted order. */
struct {
const char * s;
const char * * context; // must be in sorted order
} string;
const char * path;
Bindings * attrs;
struct {
unsigned int length;
Value * * elems;
} list;
struct {
Env * env;
Expr * expr;
} thunk;
struct {
Value * left, * right;
} app;
struct {
Env * env;
ExprLambda * fun;
} lambda;
PrimOp * primOp;
struct {
Value * left, * right;
} primOpApp;
};
};
struct Env
{
Env * up;
unsigned int prevWith; // nr of levels up to next `with' environment
Value * values[0];
};
struct Attr
{
Symbol name;
Value * value;
Pos * pos;
Attr(Symbol name, Value * value, Pos * pos = &noPos)
: name(name), value(value), pos(pos) { };
Attr() : pos(&noPos) { };
bool operator < (const Attr & a) const
{
return name < a.name;
}
};
/* After overwriting an app node, be sure to clear pointers in the
Value to ensure that the target isn't kept alive unnecessarily. */
static inline void clearValue(Value & v)
{
v.app.right = 0;
}
static inline void mkInt(Value & v, int n)
{
clearValue(v);
v.type = tInt;
v.integer = n;
}
static inline void mkBool(Value & v, bool b)
{
clearValue(v);
v.type = tBool;
v.boolean = b;
}
static inline void mkApp(Value & v, Value & left, Value & right)
{
v.type = tApp;
v.app.left = &left;
v.app.right = &right;
}
void mkString(Value & v, const char * s);
void mkString(Value & v, const string & s, const PathSet & context = PathSet());
void mkPath(Value & v, const char * s);
void copyContext(const Value & v, PathSet & context);
typedef std::map<Path, PathSet> DrvRoots;
typedef std::map<Path, Hash> DrvHashes; typedef std::map<Path, Hash> DrvHashes;
/* Cache for calls to addToStore(); maps source paths to the store /* Cache for calls to addToStore(); maps source paths to the store
@@ -196,160 +22,75 @@ typedef std::map<Path, Path> SrcToStore;
struct EvalState; struct EvalState;
/* Note: using a ATermVector is safe here, since when we call a primop
std::ostream & operator << (std::ostream & str, const Value & v); we also have an ATermList on the stack. */
typedef Expr (* PrimOp) (EvalState &, const ATermVector & args);
class EvalState struct EvalState
{ {
public: ATermMap normalForms;
ATermMap primOps;
DrvRoots drvRoots;
DrvHashes drvHashes; /* normalised derivation hashes */ DrvHashes drvHashes; /* normalised derivation hashes */
SymbolTable symbols;
const Symbol sWith, sOutPath, sDrvPath, sType, sMeta, sName,
sSystem, sOverrides;
private:
SrcToStore srcToStore; SrcToStore srcToStore;
unsigned int nrEvaluated;
unsigned int nrCached;
bool allowUnsafeEquality; bool allowUnsafeEquality;
std::map<Path, Expr *> parseTrees;
public:
EvalState(); EvalState();
~EvalState();
/* Evaluate an expression read from the given file to normal
form. */
void evalFile(const Path & path, Value & v);
/* Evaluate an expression to normal form, storing the result in
value `v'. */
void eval(Expr * e, Value & v);
void eval(Env & env, Expr * e, Value & v);
/* Evaluation the expression, then verify that it has the expected
type. */
bool evalBool(Env & env, Expr * e);
void evalAttrs(Env & env, Expr * e, Value & v);
/* If `v' is a thunk, enter it and overwrite `v' with the result
of the evaluation of the thunk. If `v' is a delayed function
application, call the function and overwrite `v' with the
result. Otherwise, this is a no-op. */
void forceValue(Value & v);
/* Force a value, then recursively force list elements and
attributes. */
void strictForceValue(Value & v);
/* Force `v', and then verify that it has the expected type. */
int forceInt(Value & v);
bool forceBool(Value & v);
void forceAttrs(Value & v);
void forceList(Value & v);
void forceFunction(Value & v); // either lambda or primop
string forceString(Value & v);
string forceString(Value & v, PathSet & context);
string forceStringNoCtx(Value & v);
/* Return true iff the value `v' denotes a derivation (i.e. a
set with attribute `type = "derivation"'). */
bool isDerivation(Value & v);
/* String coercion. Converts strings, paths and derivations to a
string. If `coerceMore' is set, also converts nulls, integers,
booleans and lists to a string. If `copyToStore' is set,
referenced paths are copied to the Nix store as a side effect.q */
string coerceToString(Value & v, PathSet & context,
bool coerceMore = false, bool copyToStore = true);
/* Path coercion. Converts strings, paths and derivations to a
path. The result is guaranteed to be a canonicalised, absolute
path. Nothing is copied to the store. */
Path coerceToPath(Value & v, PathSet & context);
private:
/* The base environment, containing the builtin functions and
values. */
Env & baseEnv;
unsigned int baseEnvDispl;
public:
/* The same, but used during parsing to resolve variables. */
StaticEnv staticBaseEnv; // !!! should be private
private:
void createBaseEnv();
void addConstant(const string & name, Value & v);
void addPrimOps();
void addPrimOp(const string & name, void addPrimOp(const string & name,
unsigned int arity, PrimOpFun primOp); unsigned int arity, PrimOp primOp);
Value * lookupVar(Env * env, const VarRef & var);
friend class ExprVar;
friend class ExprAttrs;
friend class ExprLet;
public:
/* Do a deep equality test between two values. That is, list
elements and attributes are compared recursively. */
bool eqValues(Value & v1, Value & v2);
void callFunction(Value & fun, Value & arg, Value & v);
/* Automatically call a function for which each argument has a
default value or has a binding in the `args' map. */
void autoCallFunction(Bindings & args, Value & fun, Value & res);
/* Allocation primitives. */
Value * allocValue();
Env & allocEnv(unsigned int size);
Value * allocAttr(Value & vAttrs, const Symbol & name);
void mkList(Value & v, unsigned int length);
void mkAttrs(Value & v, unsigned int expected);
void mkThunk_(Value & v, Expr * expr);
Value * maybeThunk(Env & env, Expr * expr);
/* Print statistics. */
void printStats();
private:
unsigned long nrEnvs;
unsigned long nrValuesInEnvs;
unsigned long nrValues;
unsigned long nrListElems;
unsigned long nrEvaluated;
unsigned long nrAttrsets;
unsigned long nrOpUpdates;
unsigned long nrOpUpdateValuesCopied;
unsigned int recursionDepth;
unsigned int maxRecursionDepth;
char * deepestStack; /* for measuring stack usage */
friend class RecursionCounter;
friend class ExprOpUpdate;
}; };
/* Return a string representing the type of the value `v'. */ /* Evaluate an expression to normal form. */
string showType(const Value & v); Expr evalExpr(EvalState & state, Expr e);
/* Evaluate an expression read from the given file to normal form. */
Expr evalFile(EvalState & state, const Path & path);
/* Evaluate an expression, and recursively evaluate list elements and
attributes. If `canonicalise' is true, we remove things like
position information and make sure that attribute sets are in
sorded order. */
Expr strictEvalExpr(EvalState & state, Expr e);
/* Specific results. */
string evalString(EvalState & state, Expr e, ATermList & context);
string evalStringNoCtx(EvalState & state, Expr e);
int evalInt(EvalState & state, Expr e);
bool evalBool(EvalState & state, Expr e);
ATermList evalList(EvalState & state, Expr e);
/* Flatten nested lists into a single list (or expand a singleton into
a list). */
ATermList flattenList(EvalState & state, Expr e);
/* String coercion. Converts strings, paths and derivations to a
string. If `coerceMore' is set, also converts nulls, integers,
booleans and lists to a string. */
string coerceToString(EvalState & state, Expr e, Context & context,
bool coerceMore = false, bool copyToStore = true);
/* Path coercion. Converts strings, paths and derivations to a path.
The result is guaranteed to be an canonicalised, absolute path.
Nothing is copied to the store. */
Path coerceToPath(EvalState & state, Expr e, Context & context);
/* Automatically call a function for which each argument has a default
value or has a binding in the `args' map. Note: result is a call,
not a normal form; it should be evaluated by calling evalExpr(). */
Expr autoCallFunction(Expr e, const ATermMap & args);
/* Print statistics. */
void printEvalStats(EvalState & state);
} }
+156
View File
@@ -0,0 +1,156 @@
#include "expr-to-xml.hh"
#include "xml-writer.hh"
#include "nixexpr-ast.hh"
#include "aterm.hh"
#include "util.hh"
#include <cstdlib>
namespace nix {
static XMLAttrs singletonAttrs(const string & name, const string & value)
{
XMLAttrs attrs;
attrs[name] = value;
return attrs;
}
/* set<Expr> is safe because all the expressions are also reachable
from the stack, therefore can't be garbage-collected. */
typedef set<Expr> ExprSet;
static void printTermAsXML(Expr e, XMLWriter & doc, Context & context,
ExprSet & drvsSeen);
static void showAttrs(const ATermMap & attrs, XMLWriter & doc,
Context & context, ExprSet & drvsSeen)
{
StringSet names;
for (ATermMap::const_iterator i = attrs.begin(); i != attrs.end(); ++i)
names.insert(aterm2String(i->key));
for (StringSet::iterator i = names.begin(); i != names.end(); ++i) {
XMLOpenElement _(doc, "attr", singletonAttrs("name", *i));
printTermAsXML(attrs.get(toATerm(*i)), doc, context, drvsSeen);
}
}
static void printPatternAsXML(Pattern pat, XMLWriter & doc)
{
ATerm name;
ATermList formals;
Pattern pat1, pat2;
ATermBool ellipsis;
if (matchVarPat(pat, name))
doc.writeEmptyElement("varpat", singletonAttrs("name", aterm2String(name)));
else if (matchAttrsPat(pat, formals, ellipsis)) {
XMLOpenElement _(doc, "attrspat");
for (ATermIterator i(formals); i; ++i) {
Expr name; ATerm dummy;
if (!matchFormal(*i, name, dummy)) abort();
doc.writeEmptyElement("attr", singletonAttrs("name", aterm2String(name)));
}
if (ellipsis == eTrue) doc.writeEmptyElement("ellipsis");
}
else if (matchAtPat(pat, pat1, pat2)) {
XMLOpenElement _(doc, "at");
printPatternAsXML(pat1, doc);
printPatternAsXML(pat2, doc);
}
}
static void printTermAsXML(Expr e, XMLWriter & doc, Context & context,
ExprSet & drvsSeen)
{
XMLAttrs attrs;
string s;
ATerm s2;
int i;
ATermList as, es;
ATerm pat, body, pos;
checkInterrupt();
if (matchStr(e, s, context)) /* !!! show the context? */
doc.writeEmptyElement("string", singletonAttrs("value", s));
else if (matchPath(e, s2))
doc.writeEmptyElement("path", singletonAttrs("value", aterm2String(s2)));
else if (matchNull(e))
doc.writeEmptyElement("null");
else if (matchInt(e, i))
doc.writeEmptyElement("int", singletonAttrs("value", (format("%1%") % i).str()));
else if (e == eTrue)
doc.writeEmptyElement("bool", singletonAttrs("value", "true"));
else if (e == eFalse)
doc.writeEmptyElement("bool", singletonAttrs("value", "false"));
else if (matchAttrs(e, as)) {
ATermMap attrs;
queryAllAttrs(e, attrs);
Expr a = attrs.get(toATerm("type"));
if (a && matchStr(a, s, context) && s == "derivation") {
XMLAttrs xmlAttrs;
Path outPath, drvPath;
a = attrs.get(toATerm("drvPath"));
if (matchStr(a, drvPath, context))
xmlAttrs["drvPath"] = drvPath;
a = attrs.get(toATerm("outPath"));
if (matchStr(a, outPath, context))
xmlAttrs["outPath"] = outPath;
XMLOpenElement _(doc, "derivation", xmlAttrs);
if (drvsSeen.find(e) == drvsSeen.end()) {
drvsSeen.insert(e);
showAttrs(attrs, doc, context, drvsSeen);
} else
doc.writeEmptyElement("repeated");
}
else {
XMLOpenElement _(doc, "attrs");
showAttrs(attrs, doc, context, drvsSeen);
}
}
else if (matchList(e, es)) {
XMLOpenElement _(doc, "list");
for (ATermIterator i(es); i; ++i)
printTermAsXML(*i, doc, context, drvsSeen);
}
else if (matchFunction(e, pat, body, pos)) {
XMLOpenElement _(doc, "function");
printPatternAsXML(pat, doc);
}
else
doc.writeEmptyElement("unevaluated");
}
void printTermAsXML(Expr e, std::ostream & out, Context & context)
{
XMLWriter doc(true, out);
XMLOpenElement root(doc, "expr");
ExprSet drvsSeen;
printTermAsXML(e, doc, context, drvsSeen);
}
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef __EXPR_TO_XML_H
#define __EXPR_TO_XML_H
#include <string>
#include <map>
#include "nixexpr.hh"
#include "aterm.hh"
namespace nix {
void printTermAsXML(Expr e, std::ostream & out, Context & context);
}
#endif /* !__EXPR_TO_XML_H */
+131 -80
View File
@@ -1,4 +1,5 @@
#include "get-drvs.hh" #include "get-drvs.hh"
#include "nixexpr-ast.hh"
#include "util.hh" #include "util.hh"
@@ -7,10 +8,17 @@ namespace nix {
string DrvInfo::queryDrvPath(EvalState & state) const string DrvInfo::queryDrvPath(EvalState & state) const
{ {
if (drvPath == "" && attrs) { if (drvPath == "") {
Bindings::iterator i = attrs->find(state.sDrvPath); Expr a = attrs->get(toATerm("drvPath"));
PathSet context;
(string &) drvPath = i != attrs->end() ? state.coerceToPath(*i->value, context) : ""; /* Backwards compatibility hack with user environments made by
Nix <= 0.10: these contain illegal Path("") expressions. */
ATerm t;
if (a && matchPath(evalExpr(state, a), t))
return aterm2String(t);
Context context;
(string &) drvPath = a ? coerceToPath(state, a, context) : "";
} }
return drvPath; return drvPath;
} }
@@ -18,10 +26,11 @@ string DrvInfo::queryDrvPath(EvalState & state) const
string DrvInfo::queryOutPath(EvalState & state) const string DrvInfo::queryOutPath(EvalState & state) const
{ {
if (outPath == "" && attrs) { if (outPath == "") {
Bindings::iterator i = attrs->find(state.sOutPath); Expr a = attrs->get(toATerm("outPath"));
PathSet context; if (!a) throw TypeError("output path missing");
(string &) outPath = i != attrs->end() ? state.coerceToPath(*i->value, context) : ""; Context context;
(string &) outPath = coerceToPath(state, a, context);
} }
return outPath; return outPath;
} }
@@ -29,30 +38,35 @@ string DrvInfo::queryOutPath(EvalState & state) const
MetaInfo DrvInfo::queryMetaInfo(EvalState & state) const MetaInfo DrvInfo::queryMetaInfo(EvalState & state) const
{ {
if (metaInfoRead) return meta; MetaInfo meta;
(bool &) metaInfoRead = true; Expr a = attrs->get(toATerm("meta"));
if (!a) return meta; /* fine, empty meta information */
Bindings::iterator a = attrs->find(state.sMeta);
if (a == attrs->end()) return meta; /* fine, empty meta information */
state.forceAttrs(*a->value); ATermMap attrs2;
queryAllAttrs(evalExpr(state, a), attrs2);
foreach (Bindings::iterator, i, *a->value->attrs) { for (ATermMap::const_iterator i = attrs2.begin(); i != attrs2.end(); ++i) {
Expr e = evalExpr(state, i->value);
string s;
ATermList context;
MetaValue value; MetaValue value;
state.forceValue(*i->value); int n;
if (i->value->type == tString) { ATermList es;
if (matchStr(e, s, context)) {
value.type = MetaValue::tpString; value.type = MetaValue::tpString;
value.stringValue = i->value->string.s; value.stringValue = s;
} else if (i->value->type == tInt) { meta[aterm2String(i->key)] = value;
} else if (matchInt(e, n)) {
value.type = MetaValue::tpInt; value.type = MetaValue::tpInt;
value.intValue = i->value->integer; value.intValue = n;
} else if (i->value->type == tList) { meta[aterm2String(i->key)] = value;
} else if (matchList(e, es)) {
value.type = MetaValue::tpStrings; value.type = MetaValue::tpStrings;
for (unsigned int j = 0; j < i->value->list.length; ++j) for (ATermIterator j(es); j; ++j)
value.stringValues.push_back(state.forceStringNoCtx(*i->value->list.elems[j])); value.stringValues.push_back(evalStringNoCtx(state, *j));
} else continue; meta[aterm2String(i->key)] = value;
((MetaInfo &) meta)[i->name] = value; }
} }
return meta; return meta;
@@ -68,46 +82,73 @@ MetaValue DrvInfo::queryMetaInfo(EvalState & state, const string & name) const
void DrvInfo::setMetaInfo(const MetaInfo & meta) void DrvInfo::setMetaInfo(const MetaInfo & meta)
{ {
metaInfoRead = true; ATermMap metaAttrs;
this->meta = meta; foreach (MetaInfo::const_iterator, i, meta) {
Expr e;
switch (i->second.type) {
case MetaValue::tpInt: e = makeInt(i->second.intValue); break;
case MetaValue::tpString: e = makeStr(i->second.stringValue); break;
case MetaValue::tpStrings: {
ATermList es = ATempty;
foreach (Strings::const_iterator, j, i->second.stringValues)
es = ATinsert(es, makeStr(*j));
e = makeList(ATreverse(es));
break;
}
default: abort();
}
metaAttrs.set(toATerm(i->first), makeAttrRHS(e, makeNoPos()));
}
attrs->set(toATerm("meta"), makeAttrs(metaAttrs));
} }
/* Cache for already considered attrsets. */ /* Cache for already evaluated derivations. Usually putting ATerms in
typedef set<Bindings *> Done; a STL container is unsafe (they're not scanning for GC roots), but
here it doesn't matter; everything in this set is reachable from
the stack as well. */
typedef set<Expr> Exprs;
/* Evaluate value `v'. If it evaluates to an attribute set of type /* Evaluate expression `e'. If it evaluates to an attribute set of
`derivation', then put information about it in `drvs' (unless it's type `derivation', then put information about it in `drvs' (unless
already in `doneExprs'). The result boolean indicates whether it it's already in `doneExprs'). The result boolean indicates whether
makes sense for the caller to recursively search for derivations in it makes sense for the caller to recursively search for derivations
`v'. */ in `e'. */
static bool getDerivation(EvalState & state, Value & v, static bool getDerivation(EvalState & state, Expr e,
const string & attrPath, DrvInfos & drvs, Done & done) const string & attrPath, DrvInfos & drvs, Exprs & doneExprs)
{ {
try { try {
state.forceValue(v);
if (!state.isDerivation(v)) return true; ATermList es;
e = evalExpr(state, e);
if (!matchAttrs(e, es)) return true;
boost::shared_ptr<ATermMap> attrs(new ATermMap());
queryAllAttrs(e, *attrs, false);
Expr a = attrs->get(toATerm("type"));
if (!a || evalStringNoCtx(state, a) != "derivation") return true;
/* Remove spurious duplicates (e.g., an attribute set like /* Remove spurious duplicates (e.g., an attribute set like
`rec { x = derivation {...}; y = x;}'. */ `rec { x = derivation {...}; y = x;}'. */
if (done.find(v.attrs) != done.end()) return false; if (doneExprs.find(e) != doneExprs.end()) return false;
done.insert(v.attrs); doneExprs.insert(e);
DrvInfo drv; DrvInfo drv;
Bindings::iterator i = v.attrs->find(state.sName); a = attrs->get(toATerm("name"));
/* !!! We really would like to have a decent back trace here. */ /* !!! We really would like to have a decent back trace here. */
if (i == v.attrs->end()) throw TypeError("derivation name missing"); if (!a) throw TypeError("derivation name missing");
drv.name = state.forceStringNoCtx(*i->value); drv.name = evalStringNoCtx(state, a);
Bindings::iterator i2 = v.attrs->find(state.sSystem); a = attrs->get(toATerm("system"));
if (i2 == v.attrs->end()) if (!a)
drv.system = "unknown"; drv.system = "unknown";
else else
drv.system = state.forceStringNoCtx(*i2->value); drv.system = evalStringNoCtx(state, a);
drv.attrs = v.attrs; drv.attrs = attrs;
drv.attrPath = attrPath; drv.attrPath = attrPath;
@@ -120,11 +161,11 @@ static bool getDerivation(EvalState & state, Value & v,
} }
bool getDerivation(EvalState & state, Value & v, DrvInfo & drv) bool getDerivation(EvalState & state, Expr e, DrvInfo & drv)
{ {
Done done; Exprs doneExprs;
DrvInfos drvs; DrvInfos drvs;
getDerivation(state, v, "", drvs, done); getDerivation(state, e, "", drvs, doneExprs);
if (drvs.size() != 1) return false; if (drvs.size() != 1) return false;
drv = drvs.front(); drv = drvs.front();
return true; return true;
@@ -137,73 +178,83 @@ static string addToPath(const string & s1, const string & s2)
} }
static void getDerivations(EvalState & state, Value & vIn, static void getDerivations(EvalState & state, Expr e,
const string & pathPrefix, Bindings & autoArgs, const string & pathPrefix, const ATermMap & autoArgs,
DrvInfos & drvs, Done & done) DrvInfos & drvs, Exprs & doneExprs)
{ {
Value v; e = evalExpr(state, autoCallFunction(evalExpr(state, e), autoArgs));
state.autoCallFunction(autoArgs, vIn, v);
/* Process the expression. */ /* Process the expression. */
ATermList es;
DrvInfo drv; DrvInfo drv;
if (!getDerivation(state, v, pathPrefix, drvs, done)) ; if (!getDerivation(state, e, pathPrefix, drvs, doneExprs))
return;
else if (v.type == tAttrs) { if (matchAttrs(e, es)) {
ATermMap drvMap(ATgetLength(es));
queryAllAttrs(e, drvMap);
/* !!! undocumented hackery to support combining channels in /* !!! undocumented hackery to support combining channels in
nix-env.cc. */ nix-env.cc. */
bool combineChannels = v.attrs->find(state.symbols.create("_combineChannels")) != v.attrs->end(); bool combineChannels = drvMap.get(toATerm("_combineChannels"));
/* Consider the attributes in sorted order to get more /* Consider the attributes in sorted order to get more
deterministic behaviour in nix-env operations (e.g. when deterministic behaviour in nix-env operations (e.g. when
there are names clashes between derivations, the derivation there are names clashes between derivations, the derivation
bound to the attribute with the "lower" name should take bound to the attribute with the "lower" name should take
precedence). */ precedence). */
typedef std::map<string, Symbol> SortedSymbols; typedef std::map<string, Expr> AttrsSorted;
SortedSymbols attrs; AttrsSorted attrsSorted;
foreach (Bindings::iterator, i, *v.attrs) foreach (ATermMap::const_iterator, i, drvMap)
attrs.insert(std::pair<string, Symbol>(i->name, i->name)); attrsSorted[aterm2String(i->key)] = i->value;
foreach (SortedSymbols::iterator, i, attrs) { foreach (AttrsSorted::iterator, i, attrsSorted) {
startNest(nest, lvlDebug, format("evaluating attribute `%1%'") % i->first); startNest(nest, lvlDebug, format("evaluating attribute `%1%'") % i->first);
string pathPrefix2 = addToPath(pathPrefix, i->first); string pathPrefix2 = addToPath(pathPrefix, i->first);
Value & v2(*v.attrs->find(i->second)->value);
if (combineChannels) if (combineChannels)
getDerivations(state, v2, pathPrefix2, autoArgs, drvs, done); getDerivations(state, i->second, pathPrefix2, autoArgs, drvs, doneExprs);
else if (getDerivation(state, v2, pathPrefix2, drvs, done)) { else if (getDerivation(state, i->second, pathPrefix2, drvs, doneExprs)) {
/* If the value of this attribute is itself an /* If the value of this attribute is itself an
attribute set, should we recurse into it? => Only attribute set, should we recurse into it? => Only
if it has a `recurseForDerivations = true' if it has a `recurseForDerivations = true'
attribute. */ attribute. */
if (v2.type == tAttrs) { ATermList es;
Bindings::iterator j = v2.attrs->find(state.symbols.create("recurseForDerivations")); Expr e = evalExpr(state, i->second), e2;
if (j != v2.attrs->end() && state.forceBool(*j->value)) if (matchAttrs(e, es)) {
getDerivations(state, v2, pathPrefix2, autoArgs, drvs, done); ATermMap attrs(ATgetLength(es));
queryAllAttrs(e, attrs, false);
if (((e2 = attrs.get(toATerm("recurseForDerivations")))
&& evalBool(state, e2)))
getDerivations(state, e, pathPrefix2, autoArgs, drvs, doneExprs);
} }
} }
} }
return;
} }
else if (v.type == tList) { if (matchList(e, es)) {
for (unsigned int n = 0; n < v.list.length; ++n) { int n = 0;
for (ATermIterator i(es); i; ++i, ++n) {
startNest(nest, lvlDebug, startNest(nest, lvlDebug,
format("evaluating list element")); format("evaluating list element"));
string pathPrefix2 = addToPath(pathPrefix, (format("%1%") % n).str()); string pathPrefix2 = addToPath(pathPrefix, (format("%1%") % n).str());
if (getDerivation(state, *v.list.elems[n], pathPrefix2, drvs, done)) if (getDerivation(state, *i, pathPrefix2, drvs, doneExprs))
getDerivations(state, *v.list.elems[n], pathPrefix2, autoArgs, drvs, done); getDerivations(state, *i, pathPrefix2, autoArgs, drvs, doneExprs);
} }
return;
} }
else throw TypeError("expression does not evaluate to a derivation (or a set or list of those)"); throw TypeError("expression does not evaluate to a derivation (or a set or list of those)");
} }
void getDerivations(EvalState & state, Value & v, const string & pathPrefix, void getDerivations(EvalState & state, Expr e, const string & pathPrefix,
Bindings & autoArgs, DrvInfos & drvs) const ATermMap & autoArgs, DrvInfos & drvs)
{ {
Done done; Exprs doneExprs;
getDerivations(state, v, pathPrefix, autoArgs, drvs, done); getDerivations(state, e, pathPrefix, autoArgs, drvs, doneExprs);
} }
+12 -18
View File
@@ -1,13 +1,13 @@
#ifndef __GET_DRVS_H #ifndef __GET_DRVS_H
#define __GET_DRVS_H #define __GET_DRVS_H
#include "eval.hh"
#include <string> #include <string>
#include <map> #include <map>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include "eval.hh"
namespace nix { namespace nix {
@@ -29,19 +29,16 @@ struct DrvInfo
private: private:
string drvPath; string drvPath;
string outPath; string outPath;
bool metaInfoRead;
MetaInfo meta;
public: public:
string name; string name;
string attrPath; /* path towards the derivation */ string attrPath; /* path towards the derivation */
string system; string system;
/* !!! make this private */ /* !!! these should really be hidden, and setMetaInfo() should
Bindings * attrs; make a copy since the ATermMap can be shared between multiple
DrvInfos. */
DrvInfo() : metaInfoRead(false), attrs(0) { }; boost::shared_ptr<ATermMap> attrs;
string queryDrvPath(EvalState & state) const; string queryDrvPath(EvalState & state) const;
string queryOutPath(EvalState & state) const; string queryOutPath(EvalState & state) const;
@@ -62,19 +59,16 @@ public:
}; };
#if HAVE_BOEHMGC
typedef list<DrvInfo, traceable_allocator<DrvInfo> > DrvInfos;
#else
typedef list<DrvInfo> DrvInfos; typedef list<DrvInfo> DrvInfos;
#endif
/* If value `v' denotes a derivation, store information about the /* Evaluate expression `e'. If it evaluates to a derivation, store
derivation in `drv' and return true. Otherwise, return false. */ information about the derivation in `drv' and return true.
bool getDerivation(EvalState & state, Value & v, DrvInfo & drv); Otherwise, return false. */
bool getDerivation(EvalState & state, Expr e, DrvInfo & drv);
void getDerivations(EvalState & state, Value & v, const string & pathPrefix, void getDerivations(EvalState & state, Expr e, const string & pathPrefix,
Bindings & autoArgs, DrvInfos & drvs); const ATermMap & autoArgs, DrvInfos & drvs);
} }
+23 -25
View File
@@ -8,7 +8,9 @@
%{ %{
#include "aterm.hh"
#include "nixexpr.hh" #include "nixexpr.hh"
#include "nixexpr-ast.hh"
#define BISON_HEADER_HACK #define BISON_HEADER_HACK
#include "parser-tab.hh" #include "parser-tab.hh"
@@ -19,16 +21,13 @@ namespace nix {
static void initLoc(YYLTYPE * loc) static void initLoc(YYLTYPE * loc)
{ {
loc->first_line = loc->last_line = 1; loc->first_line = 1;
loc->first_column = loc->last_column = 1; loc->first_column = 1;
} }
static void adjustLoc(YYLTYPE * loc, const char * s, size_t len) static void adjustLoc(YYLTYPE * loc, const char * s, size_t len)
{ {
loc->first_line = loc->last_line;
loc->first_column = loc->last_column;
while (len--) { while (len--) {
switch (*s++) { switch (*s++) {
case '\r': case '\r':
@@ -36,17 +35,17 @@ static void adjustLoc(YYLTYPE * loc, const char * s, size_t len)
s++; s++;
/* fall through */ /* fall through */
case '\n': case '\n':
++loc->last_line; ++loc->first_line;
loc->last_column = 1; loc->first_column = 1;
break; break;
default: default:
++loc->last_column; ++loc->first_column;
} }
} }
} }
static Expr * unescapeStr(SymbolTable & symbols, const char * s) static Expr unescapeStr(const char * s)
{ {
string t; string t;
char c; char c;
@@ -66,7 +65,7 @@ static Expr * unescapeStr(SymbolTable & symbols, const char * s)
} }
else t += c; else t += c;
} }
return new ExprString(symbols.create(t)); return makeStr(toATerm(t), ATempty);
} }
@@ -106,20 +105,19 @@ inherit { return INHERIT; }
\/\/ { return UPDATE; } \/\/ { return UPDATE; }
\+\+ { return CONCAT; } \+\+ { return CONCAT; }
{ID} { yylval->id = strdup(yytext); return ID; } {ID} { yylval->t = toATerm(yytext); return ID; /* !!! alloc */ }
{INT} { int n = atoi(yytext); /* !!! overflow */ {INT} { int n = atoi(yytext); /* !!! overflow */
yylval->n = n; yylval->t = ATmake("<int>", n);
return INT; return INT;
} }
\" { BEGIN(STRING); return '"'; } \" { BEGIN(STRING); return '"'; }
<STRING>([^\$\"\\]|\$[^\{\"]|\\.)+ { <STRING>([^\$\"\\]|\$[^\{\"]|\\.)+ {
/* !!! Not quite right: we want a follow restriction on /* !!! Not quite right: we want a follow restriction on "$", it
"$", it shouldn't be followed by a "{". Right now shouldn't be followed by a "{". Right now "$\"" will be consumed
"$\"" will be consumed as part of a string, rather as part of a string, rather than a "$" followed by the string
than a "$" followed by the string terminator. terminator. Disallow "$\"" for now. */
Disallow "$\"" for now. */ yylval->t = unescapeStr(yytext); /* !!! alloc */
yylval->e = unescapeStr(data->symbols, yytext);
return STR; return STR;
} }
<STRING>\$\{ { BEGIN(INITIAL); return DOLLAR_CURLY; } <STRING>\$\{ { BEGIN(INITIAL); return DOLLAR_CURLY; }
@@ -128,31 +126,31 @@ inherit { return INHERIT; }
\'\'(\ *\n)? { BEGIN(IND_STRING); return IND_STRING_OPEN; } \'\'(\ *\n)? { BEGIN(IND_STRING); return IND_STRING_OPEN; }
<IND_STRING>([^\$\']|\$[^\{\']|\'[^\'\$])+ { <IND_STRING>([^\$\']|\$[^\{\']|\'[^\'\$])+ {
yylval->e = new ExprIndStr(yytext); yylval->t = makeIndStr(toATerm(yytext));
return IND_STR; return IND_STR;
} }
<IND_STRING>\'\'\$ { <IND_STRING>\'\'\$ {
yylval->e = new ExprIndStr("$"); yylval->t = makeIndStr(toATerm("$"));
return IND_STR; return IND_STR;
} }
<IND_STRING>\'\'\' { <IND_STRING>\'\'\' {
yylval->e = new ExprIndStr("''"); yylval->t = makeIndStr(toATerm("''"));
return IND_STR; return IND_STR;
} }
<IND_STRING>\'\'\\. { <IND_STRING>\'\'\\. {
yylval->e = unescapeStr(data->symbols, yytext + 2); yylval->t = unescapeStr(yytext + 2);
return IND_STR; return IND_STR;
} }
<IND_STRING>\$\{ { BEGIN(INITIAL); return DOLLAR_CURLY; } <IND_STRING>\$\{ { BEGIN(INITIAL); return DOLLAR_CURLY; }
<IND_STRING>\'\' { BEGIN(INITIAL); return IND_STRING_CLOSE; } <IND_STRING>\'\' { BEGIN(INITIAL); return IND_STRING_CLOSE; }
<IND_STRING>\' { <IND_STRING>\' {
yylval->e = new ExprIndStr("'"); yylval->t = makeIndStr(toATerm("'"));
return IND_STR; return IND_STR;
} }
<IND_STRING>. return yytext[0]; /* just in case: shouldn't be reached */ <IND_STRING>. return yytext[0]; /* just in case: shouldn't be reached */
{PATH} { yylval->path = strdup(yytext); return PATH; } {PATH} { yylval->t = toATerm(yytext); return PATH; /* !!! alloc */ }
{URI} { yylval->uri = strdup(yytext); return URI; } {URI} { yylval->t = toATerm(yytext); return URI; /* !!! alloc */ }
[ \t\r\n]+ /* eat up whitespace */ [ \t\r\n]+ /* eat up whitespace */
\#[^\r\n]* /* single-line comments */ \#[^\r\n]* /* single-line comments */
+99
View File
@@ -0,0 +1,99 @@
init initNixExprHelpers
Pos | string int int | Pos |
NoPos | | Pos |
Function | Pattern Expr Pos | Expr |
Assert | Expr Expr Pos | Expr |
With | Expr Expr Pos | Expr |
If | Expr Expr Expr | Expr |
OpNot | Expr | Expr |
OpEq | Expr Expr | Expr |
OpNEq | Expr Expr | Expr |
OpAnd | Expr Expr | Expr |
OpOr | Expr Expr | Expr |
OpImpl | Expr Expr | Expr |
OpUpdate | Expr Expr | Expr |
SubPath | Expr Expr | Expr |
OpHasAttr | Expr string | Expr |
OpPlus | Expr Expr | Expr |
OpConcat | Expr Expr | Expr |
ConcatStrings | ATermList | Expr |
Call | Expr Expr | Expr |
Select | Expr string | Expr |
Var | string | Expr |
Int | int | Expr |
# Strings in the evaluator carry a so-called `context' (the ATermList)
# which is a list of strings representing store paths. This is to
# allow users to write things like
#
# "--with-freetype2-library=" + freetype + "/lib"
#
# where `freetype' is a derivation (or a source to be copied to the
# store). If we just concatenated the strings without keeping track
# of the referenced store paths, then if the string is used as a
# derivation attribute, the derivation will not have the correct
# dependencies in its inputDrvs and inputSrcs.
#
# The semantics of the context is as follows: when a string with
# context C is used as a derivation attribute, then the derivations in
# C will be added to the inputDrvs of the derivation, and the other
# store paths in C will be added to the inputSrcs of the derivations.
#
# For canonicity, the store paths should be in sorted order.
Str | string ATermList | Expr |
Str | string | Expr | ObsoleteStr
ContextElem | string Expr | ContextElem |
# Internal to the parser, doesn't occur in ASTs.
IndStr | string | Expr |
# A path is a reference to a file system object that is to be copied
# to the Nix store when used as a derivation attribute. When it is
# concatenated to a string (i.e., `str + path'), it is also copied and
# the resulting store path is concatenated to the string (with the
# store path in the context). If a string or path is concatenated to
# a path (i.e., `path + str' or `path + path'), the result is a new
# path (if the right-hand side is a string, the context must be
# empty).
Path | string | Expr |
List | ATermList | Expr |
BlackHole | | Expr |
Undefined | | Expr |
Removed | | Expr |
PrimOp | int ATermBlob ATermList | Expr |
Attrs | ATermList | Expr |
Closed | Expr | Expr |
Rec | ATermList ATermList | Expr |
Bool | ATermBool | Expr |
Null | | Expr |
Bind | string Expr Pos | ATerm |
BindAttrPath | ATermList Expr Pos | ATerm | # desugared during parsing
Bind | string Expr | ATerm | ObsoleteBind
Inherit | Expr ATermList Pos | ATerm |
Scope | | Expr |
VarPat | string | Pattern |
AttrsPat | ATermList ATermBool | Pattern | # bool = `...'
AtPat | Pattern Pattern | Pattern |
Formal | string DefaultValue | ATerm |
DefaultValue | Expr | DefaultValue |
NoDefaultValue | | DefaultValue |
True | | ATermBool |
False | | ATermBool |
PrimOpDef | int ATermBlob | ATerm |
AttrRHS | Expr Pos | ATerm |
eTrue = makeBool(makeTrue())
eFalse = makeBool(makeFalse())
sOverrides = toATerm("__overrides")
+390 -275
View File
@@ -1,314 +1,429 @@
#include "nixexpr.hh" #include "nixexpr.hh"
#include "derivations.hh" #include "derivations.hh"
#include "util.hh" #include "util.hh"
#include "aterm.hh"
#include "nixexpr-ast.hh"
#include "nixexpr-ast.cc"
#include <cstdlib> #include <cstdlib>
namespace nix { namespace nix {
/* Displaying abstract syntax trees. */
std::ostream & operator << (std::ostream & str, Expr & e)
{
e.show(str);
return str;
}
void Expr::show(std::ostream & str)
{
abort();
}
void ExprInt::show(std::ostream & str)
{
str << n;
}
void ExprString::show(std::ostream & str)
{
str << "\"" << s << "\""; // !!! escaping
}
void ExprPath::show(std::ostream & str)
{
str << s;
}
void ExprVar::show(std::ostream & str)
{
str << info.name;
}
void ExprSelect::show(std::ostream & str)
{
str << "(" << *e << ")." << name;
}
void ExprOpHasAttr::show(std::ostream & str)
{
str << "(" << *e << ") ? " << name;
}
void ExprAttrs::show(std::ostream & str)
{
if (recursive) str << "rec ";
str << "{ ";
foreach (AttrDefs::iterator, i, attrs)
if (i->second.inherited)
str << "inherit " << i->first << " " << "; ";
else
str << i->first << " = " << *i->second.e << "; ";
str << "}";
}
void ExprList::show(std::ostream & str)
{
str << "[ ";
foreach (vector<Expr *>::iterator, i, elems)
str << "(" << **i << ") ";
str << "]";
}
void ExprLambda::show(std::ostream & str)
{
str << "(";
if (matchAttrs) {
str << "{ ";
bool first = true;
foreach (Formals::Formals_::iterator, i, formals->formals) {
if (first) first = false; else str << ", ";
str << i->name;
if (i->def) str << " ? " << *i->def;
}
str << " }";
if (!arg.empty()) str << " @ ";
}
if (!arg.empty()) str << arg;
str << ": " << *body << ")";
}
void ExprLet::show(std::ostream & str)
{
str << "let ";
foreach (ExprAttrs::AttrDefs::iterator, i, attrs->attrs)
if (i->second.inherited)
str << "inherit " << i->first << "; ";
else
str << i->first << " = " << *i->second.e << "; ";
str << "in " << *body;
}
void ExprWith::show(std::ostream & str)
{
str << "with " << *attrs << "; " << *body;
}
void ExprIf::show(std::ostream & str)
{
str << "if " << *cond << " then " << *then << " else " << *else_;
}
void ExprAssert::show(std::ostream & str)
{
str << "assert " << *cond << "; " << *body;
}
void ExprOpNot::show(std::ostream & str)
{
str << "! " << *e;
}
void ExprConcatStrings::show(std::ostream & str)
{
bool first = true;
foreach (vector<Expr *>::iterator, i, *es) {
if (first) first = false; else str << " + ";
str << **i;
}
}
std::ostream & operator << (std::ostream & str, const Pos & pos)
{
if (!pos.line)
str << "undefined position";
else
str << (format("`%1%:%2%:%3%'") % pos.file % pos.line % pos.column).str();
return str;
}
Pos noPos;
/* Computing levels/displacements for variables. */
void Expr::bindVars(const StaticEnv & env)
{
abort();
}
void ExprInt::bindVars(const StaticEnv & env)
{
}
void ExprString::bindVars(const StaticEnv & env)
{
}
void ExprPath::bindVars(const StaticEnv & env)
{
}
void VarRef::bind(const StaticEnv & env)
{
/* Check whether the variable appears in the environment. If so,
set its level and displacement. */
const StaticEnv * curEnv;
unsigned int level;
int withLevel = -1;
for (curEnv = &env, level = 0; curEnv; curEnv = curEnv->up, level++) {
if (curEnv->isWith) {
if (withLevel == -1) withLevel = level;
} else {
StaticEnv::Vars::const_iterator i = curEnv->vars.find(name);
if (i != curEnv->vars.end()) {
fromWith = false;
this->level = level;
displ = i->second;
return;
}
}
}
/* Otherwise, the variable must be obtained from the nearest
enclosing `with'. If there is no `with', then we can issue an
"undefined variable" error now. */
if (withLevel == -1) throw EvalError(format("undefined variable `%1%'") % name);
fromWith = true;
this->level = withLevel;
}
void ExprVar::bindVars(const StaticEnv & env)
{
info.bind(env);
}
void ExprSelect::bindVars(const StaticEnv & env)
{
e->bindVars(env);
}
void ExprOpHasAttr::bindVars(const StaticEnv & env)
{
e->bindVars(env);
}
void ExprAttrs::bindVars(const StaticEnv & env)
{
if (recursive) {
StaticEnv newEnv(false, &env);
unsigned int displ = 0;
foreach (AttrDefs::iterator, i, attrs) string showPos(ATerm pos)
newEnv.vars[i->first] = i->second.displ = displ++; {
ATerm path;
int line, column;
if (matchNoPos(pos)) return "undefined position";
if (!matchPos(pos, path, line, column))
throw badTerm("position expected", pos);
return (format("`%1%:%2%:%3%'") % aterm2String(path) % line % column).str();
}
ATerm bottomupRewrite(TermFun & f, ATerm e)
{
checkInterrupt();
if (ATgetType(e) == AT_APPL) {
AFun fun = ATgetAFun(e);
int arity = ATgetArity(fun);
ATerm args[arity];
for (int i = 0; i < arity; ++i)
args[i] = bottomupRewrite(f, ATgetArgument(e, i));
foreach (AttrDefs::iterator, i, attrs) e = (ATerm) ATmakeApplArray(fun, args);
if (i->second.inherited) i->second.var.bind(env);
else i->second.e->bindVars(newEnv);
} }
else else if (ATgetType(e) == AT_LIST) {
foreach (AttrDefs::iterator, i, attrs) ATermList in = (ATermList) e;
if (i->second.inherited) i->second.var.bind(env); ATermList out = ATempty;
else i->second.e->bindVars(env);
}
void ExprList::bindVars(const StaticEnv & env) for (ATermIterator i(in); i; ++i)
{ out = ATinsert(out, bottomupRewrite(f, *i));
foreach (vector<Expr *>::iterator, i, elems)
(*i)->bindVars(env);
}
void ExprLambda::bindVars(const StaticEnv & env) e = (ATerm) ATreverse(out);
{
StaticEnv newEnv(false, &env);
unsigned int displ = 0;
if (!arg.empty()) newEnv.vars[arg] = displ++;
if (matchAttrs) {
foreach (Formals::Formals_::iterator, i, formals->formals)
newEnv.vars[i->name] = displ++;
foreach (Formals::Formals_::iterator, i, formals->formals)
if (i->def) i->def->bindVars(newEnv);
} }
body->bindVars(newEnv); return f(e);
} }
void ExprLet::bindVars(const StaticEnv & env)
void queryAllAttrs(Expr e, ATermMap & attrs, bool withPos)
{ {
StaticEnv newEnv(false, &env); ATermList bnds;
if (!matchAttrs(e, bnds))
unsigned int displ = 0; throw TypeError(format("value is %1% while an attribute set was expected") % showType(e));
foreach (ExprAttrs::AttrDefs::iterator, i, attrs->attrs)
newEnv.vars[i->first] = i->second.displ = displ++; for (ATermIterator i(bnds); i; ++i) {
ATerm name;
foreach (ExprAttrs::AttrDefs::iterator, i, attrs->attrs) Expr e;
if (i->second.inherited) i->second.var.bind(env); ATerm pos;
else i->second.e->bindVars(newEnv); if (!matchBind(*i, name, e, pos)) abort(); /* can't happen */
attrs.set(name, withPos ? makeAttrRHS(e, pos) : e);
body->bindVars(newEnv); }
} }
void ExprWith::bindVars(const StaticEnv & env)
Expr queryAttr(Expr e, const string & name)
{ {
/* Does this `with' have an enclosing `with'? If so, record its ATerm dummy;
level so that `lookupVar' can look up variables in the previous return queryAttr(e, name, dummy);
`with' if this one doesn't contain the desired attribute. */ }
const StaticEnv * curEnv;
unsigned int level;
prevWith = 0; Expr queryAttr(Expr e, const string & name, ATerm & pos)
for (curEnv = &env, level = 1; curEnv; curEnv = curEnv->up, level++) {
if (curEnv->isWith) { ATermList bnds;
prevWith = level; if (!matchAttrs(e, bnds))
break; throw TypeError(format("value is %1% while an attribute set was expected") % showType(e));
for (ATermIterator i(bnds); i; ++i) {
ATerm name2, pos2;
Expr e;
if (!matchBind(*i, name2, e, pos2))
abort(); /* can't happen */
if (aterm2String(name2) == name) {
pos = pos2;
return e;
} }
}
return 0;
}
Expr makeAttrs(const ATermMap & attrs)
{
ATermList bnds = ATempty;
for (ATermMap::const_iterator i = attrs.begin(); i != attrs.end(); ++i) {
Expr e;
ATerm pos;
if (!matchAttrRHS(i->value, e, pos))
abort(); /* can't happen */
bnds = ATinsert(bnds, makeBind(i->key, e, pos));
}
return makeAttrs(bnds);
}
static void varsBoundByPattern(ATermMap & map, Pattern pat)
{
ATerm name;
ATermList formals;
Pattern pat1, pat2;
ATermBool ellipsis;
/* Use makeRemoved() so that it can be used directly in
substitute(). */
if (matchVarPat(pat, name))
map.set(name, makeRemoved());
else if (matchAttrsPat(pat, formals, ellipsis)) {
for (ATermIterator i(formals); i; ++i) {
ATerm d1;
if (!matchFormal(*i, name, d1)) abort();
map.set(name, makeRemoved());
}
}
else if (matchAtPat(pat, pat1, pat2)) {
varsBoundByPattern(map, pat1);
varsBoundByPattern(map, pat2);
}
else abort();
}
Expr substitute(const Substitution & subs, Expr e)
{
checkInterrupt();
//if (subs.size() == 0) return e;
ATerm name, pos, e2;
/* As an optimisation, don't substitute in subterms known to be
closed. */
if (matchClosed(e, e2)) return e;
if (matchVar(e, name)) {
Expr sub = subs.lookup(name);
if (sub == makeRemoved()) sub = 0;
Expr wrapped;
/* Add a "closed" wrapper around terms that aren't already
closed. The check is necessary to prevent repeated
wrapping, e.g., closed(closed(closed(...))), which kills
caching. */
return sub ? (matchClosed(sub, wrapped) ? sub : makeClosed(sub)) : e;
}
/* In case of a function, filter out all variables bound by this
function. */
Pattern pat;
ATerm body;
if (matchFunction(e, pat, body, pos)) {
ATermMap map(16);
varsBoundByPattern(map, pat);
Substitution subs2(&subs, &map);
return makeFunction(
(Pattern) substitute(subs2, (Expr) pat),
substitute(subs2, body), pos);
}
/* Idem for a mutually recursive attribute set. */
ATermList rbnds, nrbnds;
if (matchRec(e, rbnds, nrbnds)) {
ATermMap map(ATgetLength(rbnds) + ATgetLength(nrbnds));
for (ATermIterator i(rbnds); i; ++i)
if (matchBind(*i, name, e2, pos)) map.set(name, makeRemoved());
else abort(); /* can't happen */
for (ATermIterator i(nrbnds); i; ++i)
if (matchBind(*i, name, e2, pos)) map.set(name, makeRemoved());
else abort(); /* can't happen */
return makeRec(
(ATermList) substitute(Substitution(&subs, &map), (ATerm) rbnds),
(ATermList) substitute(subs, (ATerm) nrbnds));
}
if (ATgetType(e) == AT_APPL) {
AFun fun = ATgetAFun(e);
int arity = ATgetArity(fun);
ATerm args[arity];
bool changed = false;
for (int i = 0; i < arity; ++i) {
ATerm arg = ATgetArgument(e, i);
args[i] = substitute(subs, arg);
if (args[i] != arg) changed = true;
}
return changed ? (ATerm) ATmakeApplArray(fun, args) : e;
}
if (ATgetType(e) == AT_LIST) {
unsigned int len = ATgetLength((ATermList) e);
ATerm es[len];
ATermIterator i((ATermList) e);
for (unsigned int j = 0; i; ++i, ++j)
es[j] = substitute(subs, *i);
ATermList out = ATempty;
for (unsigned int j = len; j; --j)
out = ATinsert(out, es[j - 1]);
return (ATerm) out;
}
return e;
}
/* We use memoisation to prevent exponential complexity on heavily
shared ATerms (remember, an ATerm is a graph, not a tree!). Note
that using an STL set is fine here wrt to ATerm garbage collection
since all the ATerms in the set are already reachable from
somewhere else. */
static void checkVarDefs2(set<Expr> & done, const ATermMap & defs, Expr e)
{
if (done.find(e) != done.end()) return;
done.insert(e);
attrs->bindVars(env); ATerm name, pos, value;
StaticEnv newEnv(true, &env); ATerm with, body;
body->bindVars(newEnv); ATermList rbnds, nrbnds;
Pattern pat;
/* Closed terms don't have free variables, so we don't have to
check by definition. */
if (matchClosed(e, value)) return;
else if (matchVar(e, name)) {
if (!defs.get(name))
throw EvalError(format("undefined variable `%1%'")
% aterm2String(name));
}
else if (matchFunction(e, pat, body, pos)) {
ATermMap defs2(defs);
varsBoundByPattern(defs2, pat);
set<Expr> done2;
checkVarDefs2(done2, defs2, pat);
checkVarDefs2(done2, defs2, body);
}
else if (matchRec(e, rbnds, nrbnds)) {
checkVarDefs2(done, defs, (ATerm) nrbnds);
ATermMap defs2(defs);
for (ATermIterator i(rbnds); i; ++i) {
if (!matchBind(*i, name, value, pos)) abort(); /* can't happen */
defs2.set(name, (ATerm) ATempty);
}
for (ATermIterator i(nrbnds); i; ++i) {
if (!matchBind(*i, name, value, pos)) abort(); /* can't happen */
defs2.set(name, (ATerm) ATempty);
}
set<Expr> done2;
checkVarDefs2(done2, defs2, (ATerm) rbnds);
}
else if (matchWith(e, with, body, pos)) {
/* We can't check the body without evaluating the definitions
(which is an arbitrary expression), so we don't do that
here but only when actually evaluating the `with'. */
checkVarDefs2(done, defs, with);
}
else if (ATgetType(e) == AT_APPL) {
int arity = ATgetArity(ATgetAFun(e));
for (int i = 0; i < arity; ++i)
checkVarDefs2(done, defs, ATgetArgument(e, i));
}
else if (ATgetType(e) == AT_LIST)
for (ATermIterator i((ATermList) e); i; ++i)
checkVarDefs2(done, defs, *i);
} }
void ExprIf::bindVars(const StaticEnv & env)
void checkVarDefs(const ATermMap & defs, Expr e)
{ {
cond->bindVars(env); set<Expr> done;
then->bindVars(env); checkVarDefs2(done, defs, e);
else_->bindVars(env);
} }
void ExprAssert::bindVars(const StaticEnv & env)
struct Canonicalise : TermFun
{ {
cond->bindVars(env); ATerm operator () (ATerm e)
body->bindVars(env); {
} /* Remove position info. */
ATerm path;
int line, column;
if (matchPos(e, path, line, column))
return makeNoPos();
void ExprOpNot::bindVars(const StaticEnv & env) /* Sort attribute sets. */
ATermList _;
if (matchAttrs(e, _)) {
ATermMap attrs;
queryAllAttrs(e, attrs);
StringSet names;
for (ATermMap::const_iterator i = attrs.begin(); i != attrs.end(); ++i)
names.insert(aterm2String(i->key));
ATermList attrs2 = ATempty;
for (StringSet::reverse_iterator i = names.rbegin(); i != names.rend(); ++i)
attrs2 = ATinsert(attrs2,
makeBind(toATerm(*i), attrs.get(toATerm(*i)), makeNoPos()));
return makeAttrs(attrs2);
}
return e;
}
};
Expr canonicaliseExpr(Expr e)
{ {
e->bindVars(env); Canonicalise canonicalise;
return bottomupRewrite(canonicalise, e);
} }
void ExprConcatStrings::bindVars(const StaticEnv & env)
Expr makeBool(bool b)
{ {
foreach (vector<Expr *>::iterator, i, *es) return b ? eTrue : eFalse;
(*i)->bindVars(env);
} }
bool matchStr(Expr e, string & s, ATermList & context)
{
ATerm s_;
if (!matchStr(e, s_, context)) return false;
s = aterm2String(s_);
return true;
}
bool matchStr(Expr e, string & s, Context & context)
{
ATermList c;
if (!matchStr(e, s, c)) return false;
matchContext(c, context);
return true;
}
Expr makeStr(const string & s, ATermList context)
{
return makeStr(toATerm(s), context);
}
Expr makeStr(const string & s, const Context & context)
{
ATermList l = ATempty;
/* !!! should define a canonical ordering of context elements. */
foreach (Context::const_iterator, i, context)
l = ATinsert(l, makeContextElem(i->key, i->value));
return makeStr(s, l);
}
void matchContext(ATermList context, Context & result)
{
for (ATermIterator i(context); i; ++i) {
ATerm s, e;
if (!matchContextElem(*i, s, e)) abort();
result.set(s, e);
}
}
string showType(Expr e)
{
ATerm t1, t2;
ATermList l1;
ATermBlob b1;
int i1;
Pattern p1;
if (matchStr(e, t1, l1)) return "a string";
if (matchPath(e, t1)) return "a path";
if (matchNull(e)) return "null";
if (matchInt(e, i1)) return "an integer";
if (matchBool(e, t1)) return "a boolean";
if (matchFunction(e, p1, t1, t2)) return "a function";
if (matchAttrs(e, l1)) return "an attribute set";
if (matchList(e, l1)) return "a list";
if (matchPrimOp(e, i1, b1, l1)) return "a partially applied built-in function";
return "an unknown type";
}
string showValue(Expr e)
{
ATermList context;
string s;
ATerm s2;
int i;
if (matchStr(e, s, context)) {
string u;
for (string::iterator i = s.begin(); i != s.end(); ++i)
if (*i == '\"' || *i == '\\') u += "\\" + *i;
else if (*i == '\n') u += "\\n";
else if (*i == '\r') u += "\\r";
else if (*i == '\t') u += "\\t";
else u += *i;
return "\"" + u + "\"";
}
if (matchPath(e, s2)) return aterm2String(s2);
if (matchNull(e)) return "null";
if (matchInt(e, i)) return (format("%1%") % i).str();
if (e == eTrue) return "true";
if (e == eFalse) return "false";
/* !!! incomplete */
return "<unknown>";
}
} }
+97 -245
View File
@@ -1,10 +1,11 @@
#ifndef __NIXEXPR_H #ifndef __NIXEXPR_H
#define __NIXEXPR_H #define __NIXEXPR_H
#include "symbol-table.hh"
#include <map> #include <map>
#include "aterm-map.hh"
#include "types.hh"
namespace nix { namespace nix {
@@ -15,264 +16,115 @@ MakeError(AssertionError, EvalError)
MakeError(ThrownError, AssertionError) MakeError(ThrownError, AssertionError)
MakeError(Abort, EvalError) MakeError(Abort, EvalError)
MakeError(TypeError, EvalError) MakeError(TypeError, EvalError)
MakeError(ImportError, EvalError) // error building an imported derivation
/* Position objects. */ /* Nix expressions are represented as ATerms. The maximal sharing
property of the ATerm library allows us to implement caching of
normals forms efficiently. */
typedef ATerm Expr;
typedef ATerm DefaultValue;
typedef ATerm Pos;
typedef ATerm Pattern;
typedef ATerm ATermBool;
typedef ATerm ContextElem;
struct Pos
/* A STL vector of ATerms. Should be used with great care since it's
stored on the heap, and the elements are therefore not roots to the
ATerm garbage collector. */
typedef vector<ATerm> ATermVector;
/* A substitution is a linked list of ATermMaps that map names to
identifiers. We use a list of ATermMaps rather than a single to
make it easy to grow or shrink a substitution when entering a
scope. */
struct Substitution
{ {
string file; ATermMap * map;
unsigned int line, column; const Substitution * prev;
Pos() : line(0), column(0) { };
Pos(const string & file, unsigned int line, unsigned int column)
: file(file), line(line), column(column) { };
};
extern Pos noPos; Substitution(const Substitution * prev, ATermMap * map)
std::ostream & operator << (std::ostream & str, const Pos & pos);
struct Env;
struct Value;
struct EvalState;
struct StaticEnv;
/* Abstract syntax of Nix expressions. */
struct Expr
{
virtual void show(std::ostream & str);
virtual void bindVars(const StaticEnv & env);
virtual void eval(EvalState & state, Env & env, Value & v);
};
std::ostream & operator << (std::ostream & str, Expr & e);
#define COMMON_METHODS \
void show(std::ostream & str); \
void eval(EvalState & state, Env & env, Value & v); \
void bindVars(const StaticEnv & env);
struct ExprInt : Expr
{
int n;
ExprInt(int n) : n(n) { };
COMMON_METHODS
};
struct ExprString : Expr
{
Symbol s;
ExprString(const Symbol & s) : s(s) { };
COMMON_METHODS
};
/* Temporary class used during parsing of indented strings. */
struct ExprIndStr : Expr
{
string s;
ExprIndStr(const string & s) : s(s) { };
};
struct ExprPath : Expr
{
string s;
ExprPath(const string & s) : s(s) { };
COMMON_METHODS
};
struct VarRef
{
Symbol name;
/* Whether the variable comes from an environment (e.g. a rec, let
or function argument) or from a "with". */
bool fromWith;
/* In the former case, the value is obtained by going `level'
levels up from the current environment and getting the
`displ'th value in that environment. In the latter case, the
value is obtained by getting the attribute named `name' from
the attribute set stored in the environment that is `level'
levels up from the current one.*/
unsigned int level;
unsigned int displ;
VarRef() { };
VarRef(const Symbol & name) : name(name) { };
void bind(const StaticEnv & env);
};
struct ExprVar : Expr
{
VarRef info;
ExprVar(const Symbol & name) : info(name) { };
COMMON_METHODS
};
struct ExprSelect : Expr
{
Expr * e;
Symbol name;
ExprSelect(Expr * e, const Symbol & name) : e(e), name(name) { };
COMMON_METHODS
};
struct ExprOpHasAttr : Expr
{
Expr * e;
Symbol name;
ExprOpHasAttr(Expr * e, const Symbol & name) : e(e), name(name) { };
COMMON_METHODS
};
struct ExprAttrs : Expr
{
bool recursive;
struct AttrDef {
bool inherited;
Expr * e; // if not inherited
VarRef var; // if inherited
Pos pos;
unsigned int displ; // displacement
AttrDef(Expr * e, const Pos & pos) : inherited(false), e(e), pos(pos) { };
AttrDef(const Symbol & name, const Pos & pos) : inherited(true), var(name), pos(pos) { };
AttrDef() { };
};
typedef std::map<Symbol, AttrDef> AttrDefs;
AttrDefs attrs;
ExprAttrs() : recursive(false) { };
COMMON_METHODS
};
struct ExprList : Expr
{
std::vector<Expr *> elems;
ExprList() { };
COMMON_METHODS
};
struct Formal
{
Symbol name;
Expr * def;
Formal(const Symbol & name, Expr * def) : name(name), def(def) { };
};
struct Formals
{
typedef std::list<Formal> Formals_;
Formals_ formals;
std::set<Symbol> argNames; // used during parsing
bool ellipsis;
};
struct ExprLambda : Expr
{
Pos pos;
Symbol arg;
bool matchAttrs;
Formals * formals;
Expr * body;
ExprLambda(const Pos & pos, const Symbol & arg, bool matchAttrs, Formals * formals, Expr * body)
: pos(pos), arg(arg), matchAttrs(matchAttrs), formals(formals), body(body)
{ {
if (!arg.empty() && formals && formals->argNames.find(arg) != formals->argNames.end()) this->prev = prev;
throw ParseError(format("duplicate formal function argument `%1%' at %2%") this->map = map;
% arg % pos); }
};
COMMON_METHODS Expr lookup(Expr name) const
{
Expr x;
for (const Substitution * s(this); s; s = s->prev)
if ((x = s->map->get(name))) return x;
return 0;
}
}; };
struct ExprLet : Expr
/* Show a position. */
string showPos(ATerm pos);
/* Generic bottomup traversal over ATerms. The traversal first
recursively descends into subterms, and then applies the given term
function to the resulting term. */
struct TermFun
{ {
ExprAttrs * attrs; virtual ~TermFun() { }
Expr * body; virtual ATerm operator () (ATerm e) = 0;
ExprLet(ExprAttrs * attrs, Expr * body) : attrs(attrs), body(body) { };
COMMON_METHODS
};
struct ExprWith : Expr
{
Pos pos;
Expr * attrs, * body;
unsigned int prevWith;
ExprWith(const Pos & pos, Expr * attrs, Expr * body) : pos(pos), attrs(attrs), body(body) { };
COMMON_METHODS
};
struct ExprIf : Expr
{
Expr * cond, * then, * else_;
ExprIf(Expr * cond, Expr * then, Expr * else_) : cond(cond), then(then), else_(else_) { };
COMMON_METHODS
};
struct ExprAssert : Expr
{
Pos pos;
Expr * cond, * body;
ExprAssert(const Pos & pos, Expr * cond, Expr * body) : pos(pos), cond(cond), body(body) { };
COMMON_METHODS
};
struct ExprOpNot : Expr
{
Expr * e;
ExprOpNot(Expr * e) : e(e) { };
COMMON_METHODS
};
#define MakeBinOp(name, s) \
struct Expr##name : Expr \
{ \
Expr * e1, * e2; \
Expr##name(Expr * e1, Expr * e2) : e1(e1), e2(e2) { }; \
void show(std::ostream & str) \
{ \
str << *e1 << " " s " " << *e2; \
} \
void bindVars(const StaticEnv & env) \
{ \
e1->bindVars(env); e2->bindVars(env); \
} \
void eval(EvalState & state, Env & env, Value & v); \
};
MakeBinOp(App, "")
MakeBinOp(OpEq, "==")
MakeBinOp(OpNEq, "!=")
MakeBinOp(OpAnd, "&&")
MakeBinOp(OpOr, "||")
MakeBinOp(OpImpl, "->")
MakeBinOp(OpUpdate, "//")
MakeBinOp(OpConcatLists, "++")
struct ExprConcatStrings : Expr
{
vector<Expr *> * es;
ExprConcatStrings(vector<Expr *> * es) : es(es) { };
COMMON_METHODS
}; };
ATerm bottomupRewrite(TermFun & f, ATerm e);
/* Static environments are used to map variable names onto (level, /* Query all attributes in an attribute set expression. The
displacement) pairs used to obtain the value of the variable at expression must be in normal form. */
runtime. */ void queryAllAttrs(Expr e, ATermMap & attrs, bool withPos = false);
struct StaticEnv
{ /* Query a specific attribute from an attribute set expression. The
bool isWith; expression must be in normal form. */
const StaticEnv * up; Expr queryAttr(Expr e, const string & name);
typedef std::map<Symbol, unsigned int> Vars; Expr queryAttr(Expr e, const string & name, ATerm & pos);
Vars vars;
StaticEnv(bool isWith, const StaticEnv * up) : isWith(isWith), up(up) { }; /* Create an attribute set expression from an Attrs value. */
}; Expr makeAttrs(const ATermMap & attrs);
/* Perform a set of substitutions on an expression. */
Expr substitute(const Substitution & subs, Expr e);
/* Check whether all variables are defined in the given expression.
Throw an exception if this isn't the case. */
void checkVarDefs(const ATermMap & def, Expr e);
/* Canonicalise a Nix expression by sorting attributes and removing
location information. */
Expr canonicaliseExpr(Expr e);
/* Create an expression representing a boolean. */
Expr makeBool(bool b);
/* Manipulation of Str() nodes. */
typedef ATermMap Context;
bool matchStr(Expr e, string & s, ATermList & context);
bool matchStr(Expr e, string & s, Context & context);
Expr makeStr(const string & s, ATermList context = ATempty);
Expr makeStr(const string & s, const Context & context);
void matchContext(ATermList context, Context & result);
/* Showing types, values. */
string showType(Expr e);
string showValue(Expr e);
} }
+4 -3
View File
@@ -8,11 +8,12 @@ namespace nix {
/* Parse a Nix expression from the specified file. If `path' refers /* Parse a Nix expression from the specified file. If `path' refers
to a directory, then "/default.nix" is appended. */ to a directory, the "/default.nix" is appended. */
Expr * parseExprFromFile(EvalState & state, Path path); Expr parseExprFromFile(EvalState & state, Path path);
/* Parse a Nix expression from the specified string. */ /* Parse a Nix expression from the specified string. */
Expr * parseExprFromString(EvalState & state, const string & s, const Path & basePath); Expr parseExprFromString(EvalState & state, const string & s,
const Path & basePath);
} }
+282 -224
View File
@@ -7,126 +7,171 @@
%parse-param { yyscan_t scanner } %parse-param { yyscan_t scanner }
%parse-param { ParseData * data } %parse-param { ParseData * data }
%lex-param { yyscan_t scanner } %lex-param { yyscan_t scanner }
%lex-param { ParseData * data }
%code requires {
#ifndef BISON_HEADER
#define BISON_HEADER
#include "util.hh"
#include "nixexpr.hh"
namespace nix {
struct ParseData
{
SymbolTable & symbols;
Expr * result;
Path basePath;
Path path;
string error;
Symbol sLetBody;
ParseData(SymbolTable & symbols)
: symbols(symbols)
, sLetBody(symbols.create("<let-body>"))
{ };
};
}
#define YY_DECL int yylex \
(YYSTYPE * yylval_param, YYLTYPE * yylloc_param, yyscan_t yyscanner, nix::ParseData * data)
#endif
}
%{ %{
/* Newer versions of Bison copy the declarations below to
#include "parser-tab.hh" parser-tab.hh, which sucks bigtime since lexer.l doesn't want that
#include "lexer-tab.hh" stuff. So allow it to be excluded. */
#define YYSTYPE YYSTYPE // workaround a bug in Bison 2.4 #ifndef BISON_HEADER_HACK
#define BISON_HEADER_HACK
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
YY_DECL; #include "aterm.hh"
#include "util.hh"
#include "parser-tab.hh"
#include "lexer-tab.hh"
#define YYSTYPE YYSTYPE // workaround a bug in Bison 2.4
#include "nixexpr.hh"
#include "nixexpr-ast.hh"
using namespace nix; using namespace nix;
namespace nix { namespace nix {
static string showAttrPath(const vector<Symbol> & attrPath)
struct ParseData
{
Expr result;
Path basePath;
Path path;
string error;
};
static string showAttrPath(ATermList attrPath)
{ {
string s; string s;
foreach (vector<Symbol>::const_iterator, i, attrPath) { for (ATermIterator i(attrPath); i; ++i) {
if (!s.empty()) s += '.'; if (!s.empty()) s += '.';
s += *i; s += aterm2String(*i);
} }
return s; return s;
} }
struct Tree
static void dupAttr(const vector<Symbol> & attrPath, const Pos & pos, const Pos & prevPos)
{ {
throw ParseError(format("attribute `%1%' at %2% already defined at %3%") Expr leaf; ATerm pos; bool recursive;
% showAttrPath(attrPath) % pos % prevPos); typedef std::map<ATerm, Tree> Children;
Children children;
Tree() { leaf = 0; recursive = true; }
};
static ATermList buildAttrs(const Tree & t, ATermList & nonrec)
{
ATermList res = ATempty;
for (Tree::Children::const_reverse_iterator i = t.children.rbegin();
i != t.children.rend(); ++i)
if (!i->second.recursive)
nonrec = ATinsert(nonrec, makeBind(i->first, i->second.leaf, i->second.pos));
else
res = ATinsert(res, i->second.leaf
? makeBind(i->first, i->second.leaf, i->second.pos)
: makeBind(i->first, makeAttrs(buildAttrs(i->second, nonrec)), makeNoPos()));
return res;
} }
static void dupAttr(Symbol attr, const Pos & pos, const Pos & prevPos) static Expr fixAttrs(bool recursive, ATermList as)
{ {
vector<Symbol> attrPath; attrPath.push_back(attr); Tree attrs;
throw ParseError(format("attribute `%1%' at %2% already defined at %3%")
% showAttrPath(attrPath) % pos % prevPos);
}
static void addAttr(ExprAttrs * attrs, const vector<Symbol> & attrPath, for (ATermIterator i(as); i; ++i) {
Expr * e, const Pos & pos) ATermList names, attrPath; Expr src, e; ATerm name, pos;
{
unsigned int n = 0; if (matchInherit(*i, src, names, pos)) {
foreach (vector<Symbol>::const_iterator, i, attrPath) { bool fromScope = matchScope(src);
n++; for (ATermIterator j(names); j; ++j) {
ExprAttrs::AttrDefs::iterator j = attrs->attrs.find(*i); if (attrs.children.find(*j) != attrs.children.end())
if (j != attrs->attrs.end()) { throw ParseError(format("duplicate definition of attribute `%1%' at %2%")
if (!j->second.inherited) { % showAttrPath(ATmakeList1(*j)) % showPos(pos));
ExprAttrs * attrs2 = dynamic_cast<ExprAttrs *>(j->second.e); Tree & t(attrs.children[*j]);
if (!attrs2 || n == attrPath.size()) dupAttr(attrPath, pos, j->second.pos); t.leaf = fromScope ? makeVar(*j) : makeSelect(src, *j);
attrs = attrs2; t.pos = pos;
} else if (recursive && fromScope) t.recursive = false;
dupAttr(attrPath, pos, j->second.pos);
} else {
if (n == attrPath.size())
attrs->attrs[*i] = ExprAttrs::AttrDef(e, pos);
else {
ExprAttrs * nested = new ExprAttrs;
attrs->attrs[*i] = ExprAttrs::AttrDef(nested, pos);
attrs = nested;
} }
} }
else if (matchBindAttrPath(*i, attrPath, e, pos)) {
Tree * t(&attrs);
for (ATermIterator j(attrPath); j; ) {
name = *j; ++j;
if (t->leaf) throw ParseError(format("attribute set containing `%1%' at %2% already defined at %3%")
% showAttrPath(attrPath) % showPos(pos) % showPos (t->pos));
t = &(t->children[name]);
}
if (t->leaf)
throw ParseError(format("duplicate definition of attribute `%1%' at %2% and %3%")
% showAttrPath(attrPath) % showPos(pos) % showPos (t->pos));
if (!t->children.empty())
throw ParseError(format("duplicate definition of attribute `%1%' at %2%")
% showAttrPath(attrPath) % showPos(pos));
t->leaf = e; t->pos = pos;
}
else abort(); /* can't happen */
} }
ATermList nonrec = ATempty;
ATermList rec = buildAttrs(attrs, nonrec);
return recursive ? makeRec(rec, nonrec) : makeAttrs(rec);
} }
static void addFormal(const Pos & pos, Formals * formals, const Formal & formal) static void checkPatternVars(ATerm pos, ATermMap & map, Pattern pat)
{ {
if (formals->argNames.find(formal.name) != formals->argNames.end()) ATerm name;
throw ParseError(format("duplicate formal function argument `%1%' at %2%") ATermList formals;
% formal.name % pos); Pattern pat1, pat2;
formals->formals.push_front(formal); ATermBool ellipsis;
formals->argNames.insert(formal.name); if (matchVarPat(pat, name)) {
if (map.get(name))
throw ParseError(format("duplicate formal function argument `%1%' at %2%")
% aterm2String(name) % showPos(pos));
map.set(name, name);
}
else if (matchAttrsPat(pat, formals, ellipsis)) {
for (ATermIterator i(formals); i; ++i) {
ATerm d1;
if (!matchFormal(*i, name, d1)) abort();
if (map.get(name))
throw ParseError(format("duplicate formal function argument `%1%' at %2%")
% aterm2String(name) % showPos(pos));
map.set(name, name);
}
}
else if (matchAtPat(pat, pat1, pat2)) {
checkPatternVars(pos, map, pat1);
checkPatternVars(pos, map, pat2);
}
else abort();
} }
static Expr * stripIndentation(SymbolTable & symbols, vector<Expr *> & es) static void checkPatternVars(ATerm pos, Pattern pat)
{ {
if (es.empty()) return new ExprString(symbols.create("")); ATermMap map;
checkPatternVars(pos, map, pat);
}
static Expr stripIndentation(ATermList es)
{
if (es == ATempty) return makeStr("");
/* Figure out the minimum indentation. Note that by design /* Figure out the minimum indentation. Note that by design
whitespace-only final lines are not taken into account. (So whitespace-only final lines are not taken into account. (So
@@ -134,9 +179,9 @@ static Expr * stripIndentation(SymbolTable & symbols, vector<Expr *> & es)
bool atStartOfLine = true; /* = seen only whitespace in the current line */ bool atStartOfLine = true; /* = seen only whitespace in the current line */
unsigned int minIndent = 1000000; unsigned int minIndent = 1000000;
unsigned int curIndent = 0; unsigned int curIndent = 0;
foreach (vector<Expr *>::iterator, i, es) { ATerm e;
ExprIndStr * e = dynamic_cast<ExprIndStr *>(*i); for (ATermIterator i(es); i; ++i) {
if (!e) { if (!matchIndStr(*i, e)) {
/* Anti-quotations end the current start-of-line whitespace. */ /* Anti-quotations end the current start-of-line whitespace. */
if (atStartOfLine) { if (atStartOfLine) {
atStartOfLine = false; atStartOfLine = false;
@@ -144,11 +189,12 @@ static Expr * stripIndentation(SymbolTable & symbols, vector<Expr *> & es)
} }
continue; continue;
} }
for (unsigned int j = 0; j < e->s.size(); ++j) { string s = aterm2String(e);
for (unsigned int j = 0; j < s.size(); ++j) {
if (atStartOfLine) { if (atStartOfLine) {
if (e->s[j] == ' ') if (s[j] == ' ')
curIndent++; curIndent++;
else if (e->s[j] == '\n') { else if (s[j] == '\n') {
/* Empty line, doesn't influence minimum /* Empty line, doesn't influence minimum
indentation. */ indentation. */
curIndent = 0; curIndent = 0;
@@ -156,7 +202,7 @@ static Expr * stripIndentation(SymbolTable & symbols, vector<Expr *> & es)
atStartOfLine = false; atStartOfLine = false;
if (curIndent < minIndent) minIndent = curIndent; if (curIndent < minIndent) minIndent = curIndent;
} }
} else if (e->s[j] == '\n') { } else if (s[j] == '\n') {
atStartOfLine = true; atStartOfLine = true;
curIndent = 0; curIndent = 0;
} }
@@ -164,37 +210,37 @@ static Expr * stripIndentation(SymbolTable & symbols, vector<Expr *> & es)
} }
/* Strip spaces from each line. */ /* Strip spaces from each line. */
vector<Expr *> * es2 = new vector<Expr *>; ATermList es2 = ATempty;
atStartOfLine = true; atStartOfLine = true;
unsigned int curDropped = 0; unsigned int curDropped = 0;
unsigned int n = es.size(); unsigned int n = ATgetLength(es);
for (vector<Expr *>::iterator i = es.begin(); i != es.end(); ++i, --n) { for (ATermIterator i(es); i; ++i, --n) {
ExprIndStr * e = dynamic_cast<ExprIndStr *>(*i); if (!matchIndStr(*i, e)) {
if (!e) {
atStartOfLine = false; atStartOfLine = false;
curDropped = 0; curDropped = 0;
es2->push_back(*i); es2 = ATinsert(es2, *i);
continue; continue;
} }
string s = aterm2String(e);
string s2; string s2;
for (unsigned int j = 0; j < e->s.size(); ++j) { for (unsigned int j = 0; j < s.size(); ++j) {
if (atStartOfLine) { if (atStartOfLine) {
if (e->s[j] == ' ') { if (s[j] == ' ') {
if (curDropped++ >= minIndent) if (curDropped++ >= minIndent)
s2 += e->s[j]; s2 += s[j];
} }
else if (e->s[j] == '\n') { else if (s[j] == '\n') {
curDropped = 0; curDropped = 0;
s2 += e->s[j]; s2 += s[j];
} else { } else {
atStartOfLine = false; atStartOfLine = false;
curDropped = 0; curDropped = 0;
s2 += e->s[j]; s2 += s[j];
} }
} else { } else {
s2 += e->s[j]; s2 += s[j];
if (e->s[j] == '\n') atStartOfLine = true; if (s[j] == '\n') atStartOfLine = true;
} }
} }
@@ -205,11 +251,11 @@ static Expr * stripIndentation(SymbolTable & symbols, vector<Expr *> & es)
if (p != string::npos && s2.find_first_not_of(' ', p + 1) == string::npos) if (p != string::npos && s2.find_first_not_of(' ', p + 1) == string::npos)
s2 = string(s2, 0, p + 1); s2 = string(s2, 0, p + 1);
} }
es2->push_back(new ExprString(symbols.create(s2))); es2 = ATinsert(es2, makeStr(s2));
} }
return new ExprConcatStrings(es2); return makeConcatStrings(ATreverse(es2));
} }
@@ -217,12 +263,13 @@ void backToString(yyscan_t scanner);
void backToIndString(yyscan_t scanner); void backToIndString(yyscan_t scanner);
static Pos makeCurPos(const YYLTYPE & loc, ParseData * data) static Pos makeCurPos(YYLTYPE * loc, ParseData * data)
{ {
return Pos(data->path, loc.first_line, loc.first_column); return makePos(toATerm(data->path),
loc->first_line, loc->first_column);
} }
#define CUR_POS makeCurPos(*yylocp, data) #define CUR_POS makeCurPos(yylocp, data)
} }
@@ -230,40 +277,50 @@ static Pos makeCurPos(const YYLTYPE & loc, ParseData * data)
void yyerror(YYLTYPE * loc, yyscan_t scanner, ParseData * data, const char * error) void yyerror(YYLTYPE * loc, yyscan_t scanner, ParseData * data, const char * error)
{ {
data->error = (format("%1%, at %2%") data->error = (format("%1%, at `%2%':%3%:%4%")
% error % makeCurPos(*loc, data)).str(); % error % data->path % loc->first_line % loc->first_column).str();
} }
/* Make sure that the parse stack is scanned by the ATerm garbage
collector. */
static void * mallocAndProtect(size_t size)
{
void * p = malloc(size);
if (p) ATprotectMemory(p, size);
return p;
}
static void freeAndUnprotect(void * p)
{
ATunprotectMemory(p);
free(p);
}
#define YYMALLOC mallocAndProtect
#define YYFREE freeAndUnprotect
#endif
%} %}
%union { %union {
nix::Expr * e; ATerm t;
nix::ExprList * list; ATermList ts;
nix::ExprAttrs * attrs; struct {
nix::Formals * formals; ATermList formals;
nix::Formal * formal; bool ellipsis;
int n; } formals;
char * id; // !!! -> Symbol
char * path;
char * uri;
std::vector<nix::Symbol> * ids;
std::vector<nix::Expr *> * string_parts;
} }
%type <e> start expr expr_function expr_if expr_op %type <t> start expr expr_function expr_if expr_op
%type <e> expr_app expr_select expr_simple %type <t> expr_app expr_select expr_simple bind inheritsrc formal
%type <list> expr_list %type <t> pattern pattern2
%type <attrs> binds %type <ts> binds ids attrpath expr_list string_parts ind_string_parts
%type <formals> formals %type <formals> formals
%type <formal> formal %token <t> ID INT STR IND_STR PATH URI
%type <ids> ids attrpath
%type <string_parts> string_parts ind_string_parts
%token <id> ID ATTRPATH
%token <e> STR IND_STR
%token <n> INT
%token <path> PATH
%token <uri> URI
%token IF THEN ELSE ASSERT WITH LET IN REC INHERIT EQ NEQ AND OR IMPL %token IF THEN ELSE ASSERT WITH LET IN REC INHERIT EQ NEQ AND OR IMPL
%token DOLLAR_CURLY /* == ${ */ %token DOLLAR_CURLY /* == ${ */
%token IND_STRING_OPEN IND_STRING_CLOSE %token IND_STRING_OPEN IND_STRING_CLOSE
@@ -287,170 +344,163 @@ start: expr { data->result = $1; };
expr: expr_function; expr: expr_function;
expr_function expr_function
: ID ':' expr_function : pattern ':' expr_function
{ $$ = new ExprLambda(CUR_POS, data->symbols.create($1), false, 0, $3); } { checkPatternVars(CUR_POS, $1); $$ = makeFunction($1, $3, CUR_POS); }
| '{' formals '}' ':' expr_function
{ $$ = new ExprLambda(CUR_POS, data->symbols.create(""), true, $2, $5); }
| '{' formals '}' '@' ID ':' expr_function
{ $$ = new ExprLambda(CUR_POS, data->symbols.create($5), true, $2, $7); }
| ID '@' '{' formals '}' ':' expr_function
{ $$ = new ExprLambda(CUR_POS, data->symbols.create($1), true, $4, $7); }
| ASSERT expr ';' expr_function | ASSERT expr ';' expr_function
{ $$ = new ExprAssert(CUR_POS, $2, $4); } { $$ = makeAssert($2, $4, CUR_POS); }
| WITH expr ';' expr_function | WITH expr ';' expr_function
{ $$ = new ExprWith(CUR_POS, $2, $4); } { $$ = makeWith($2, $4, CUR_POS); }
| LET binds IN expr_function | LET binds IN expr_function
{ $$ = new ExprLet($2, $4); } { $$ = makeSelect(fixAttrs(true, ATinsert($2, makeBindAttrPath(ATmakeList1(toATerm("<let-body>")), $4, CUR_POS))), toATerm("<let-body>")); }
| expr_if | expr_if
; ;
expr_if expr_if
: IF expr THEN expr ELSE expr { $$ = new ExprIf($2, $4, $6); } : IF expr THEN expr ELSE expr
{ $$ = makeIf($2, $4, $6); }
| expr_op | expr_op
; ;
expr_op expr_op
: '!' expr_op %prec NEG { $$ = new ExprOpNot($2); } : '!' expr_op %prec NEG { $$ = makeOpNot($2); }
| expr_op EQ expr_op { $$ = new ExprOpEq($1, $3); } | expr_op EQ expr_op { $$ = makeOpEq($1, $3); }
| expr_op NEQ expr_op { $$ = new ExprOpNEq($1, $3); } | expr_op NEQ expr_op { $$ = makeOpNEq($1, $3); }
| expr_op AND expr_op { $$ = new ExprOpAnd($1, $3); } | expr_op AND expr_op { $$ = makeOpAnd($1, $3); }
| expr_op OR expr_op { $$ = new ExprOpOr($1, $3); } | expr_op OR expr_op { $$ = makeOpOr($1, $3); }
| expr_op IMPL expr_op { $$ = new ExprOpImpl($1, $3); } | expr_op IMPL expr_op { $$ = makeOpImpl($1, $3); }
| expr_op UPDATE expr_op { $$ = new ExprOpUpdate($1, $3); } | expr_op UPDATE expr_op { $$ = makeOpUpdate($1, $3); }
| expr_op '?' ID { $$ = new ExprOpHasAttr($1, data->symbols.create($3)); } | expr_op '~' expr_op { $$ = makeSubPath($1, $3); }
| expr_op '+' expr_op | expr_op '?' ID { $$ = makeOpHasAttr($1, $3); }
{ vector<Expr *> * l = new vector<Expr *>; | expr_op '+' expr_op { $$ = makeOpPlus($1, $3); }
l->push_back($1); | expr_op CONCAT expr_op { $$ = makeOpConcat($1, $3); }
l->push_back($3);
$$ = new ExprConcatStrings(l);
}
| expr_op CONCAT expr_op { $$ = new ExprOpConcatLists($1, $3); }
| expr_app | expr_app
; ;
expr_app expr_app
: expr_app expr_select : expr_app expr_select
{ $$ = new ExprApp($1, $2); } { $$ = makeCall($1, $2); }
| expr_select { $$ = $1; } | expr_select { $$ = $1; }
; ;
expr_select expr_select
: expr_select '.' ID : expr_select '.' ID
{ $$ = new ExprSelect($1, data->symbols.create($3)); } { $$ = makeSelect($1, $3); }
| expr_simple { $$ = $1; } | expr_simple { $$ = $1; }
; ;
expr_simple expr_simple
: ID { $$ = new ExprVar(data->symbols.create($1)); } : ID { $$ = makeVar($1); }
| INT { $$ = new ExprInt($1); } | INT { $$ = makeInt(ATgetInt((ATermInt) $1)); }
| '"' string_parts '"' { | '"' string_parts '"' {
/* For efficiency, and to simplify parse trees a bit. */ /* For efficiency, and to simplify parse trees a bit. */
if ($2->empty()) $$ = new ExprString(data->symbols.create("")); if ($2 == ATempty) $$ = makeStr(toATerm(""), ATempty);
else if ($2->size() == 1) $$ = $2->front(); else if (ATgetNext($2) == ATempty) $$ = ATgetFirst($2);
else $$ = new ExprConcatStrings($2); else $$ = makeConcatStrings(ATreverse($2));
} }
| IND_STRING_OPEN ind_string_parts IND_STRING_CLOSE { | IND_STRING_OPEN ind_string_parts IND_STRING_CLOSE {
$$ = stripIndentation(data->symbols, *$2); $$ = stripIndentation(ATreverse($2));
} }
| PATH { $$ = new ExprPath(absPath($1, data->basePath)); } | PATH { $$ = makePath(toATerm(absPath(aterm2String($1), data->basePath))); }
| URI { $$ = new ExprString(data->symbols.create($1)); } | URI { $$ = makeStr($1, ATempty); }
| '(' expr ')' { $$ = $2; } | '(' expr ')' { $$ = $2; }
/* Let expressions `let {..., body = ...}' are just desugared /* Let expressions `let {..., body = ...}' are just desugared
into `(rec {..., body = ...}).body'. */ into `(rec {..., body = ...}).body'. */
| LET '{' binds '}' | LET '{' binds '}'
{ $3->recursive = true; $$ = new ExprSelect($3, data->symbols.create("body")); } { $$ = makeSelect(fixAttrs(true, $3), toATerm("body")); }
| REC '{' binds '}' | REC '{' binds '}'
{ $3->recursive = true; $$ = $3; } { $$ = fixAttrs(true, $3); }
| '{' binds '}' | '{' binds '}'
{ $$ = $2; } { $$ = fixAttrs(false, $2); }
| '[' expr_list ']' { $$ = $2; } | '[' expr_list ']' { $$ = makeList(ATreverse($2)); }
; ;
string_parts string_parts
: string_parts STR { $$ = $1; $1->push_back($2); } : string_parts STR { $$ = ATinsert($1, $2); }
| string_parts DOLLAR_CURLY expr '}' { backToString(scanner); $$ = $1; $1->push_back($3); } | string_parts DOLLAR_CURLY expr '}' { backToString(scanner); $$ = ATinsert($1, $3); }
| { $$ = new vector<Expr *>; } | { $$ = ATempty; }
; ;
ind_string_parts ind_string_parts
: ind_string_parts IND_STR { $$ = $1; $1->push_back($2); } : ind_string_parts IND_STR { $$ = ATinsert($1, $2); }
| ind_string_parts DOLLAR_CURLY expr '}' { backToIndString(scanner); $$ = $1; $1->push_back($3); } | ind_string_parts DOLLAR_CURLY expr '}' { backToIndString(scanner); $$ = ATinsert($1, $3); }
| { $$ = new vector<Expr *>; } | { $$ = ATempty; }
;
pattern
: pattern2 '@' pattern { $$ = makeAtPat($1, $3); }
| pattern2
;
pattern2
: ID { $$ = makeVarPat($1); }
| '{' formals '}' { $$ = makeAttrsPat($2.formals, $2.ellipsis ? eTrue : eFalse); }
; ;
binds binds
: binds attrpath '=' expr ';' { $$ = $1; addAttr($$, *$2, $4, makeCurPos(@2, data)); } : binds bind { $$ = ATinsert($1, $2); }
| binds INHERIT ids ';' | { $$ = ATempty; }
{ $$ = $1;
foreach (vector<Symbol>::iterator, i, *$3) {
if ($$->attrs.find(*i) != $$->attrs.end())
dupAttr(*i, makeCurPos(@3, data), $$->attrs[*i].pos);
Pos pos = makeCurPos(@3, data);
$$->attrs[*i] = ExprAttrs::AttrDef(*i, pos);
}
}
| binds INHERIT '(' expr ')' ids ';'
{ $$ = $1;
/* !!! Should ensure sharing of the expression in $4. */
foreach (vector<Symbol>::iterator, i, *$6) {
if ($$->attrs.find(*i) != $$->attrs.end())
dupAttr(*i, makeCurPos(@6, data), $$->attrs[*i].pos);
$$->attrs[*i] = ExprAttrs::AttrDef(new ExprSelect($4, *i), makeCurPos(@6, data));
}}
| { $$ = new ExprAttrs; }
; ;
ids bind
: ids ID { $$ = $1; $1->push_back(data->symbols.create($2)); /* !!! dangerous */ } : attrpath '=' expr ';'
| { $$ = new vector<Symbol>; } { $$ = makeBindAttrPath(ATreverse($1), $3, CUR_POS); }
| INHERIT inheritsrc ids ';'
{ $$ = makeInherit($2, $3, CUR_POS); }
; ;
inheritsrc
: '(' expr ')' { $$ = $2; }
| { $$ = makeScope(); }
;
ids: ids ID { $$ = ATinsert($1, $2); } | { $$ = ATempty; };
attrpath attrpath
: attrpath '.' ID { $$ = $1; $1->push_back(data->symbols.create($3)); } : attrpath '.' ID { $$ = ATinsert($1, $3); }
| ID { $$ = new vector<Symbol>; $$->push_back(data->symbols.create($1)); } | ID { $$ = ATmakeList1($1); }
; ;
expr_list expr_list
: expr_list expr_select { $$ = $1; $1->elems.push_back($2); /* !!! dangerous */ } : expr_list expr_select { $$ = ATinsert($1, $2); }
| { $$ = new ExprList; } | { $$ = ATempty; }
; ;
formals formals
: formal ',' formals : formal ',' formals /* !!! right recursive */
{ $$ = $3; addFormal(CUR_POS, $$, *$1); } { $$.formals = ATinsert($3.formals, $1); $$.ellipsis = $3.ellipsis; }
| formal | formal
{ $$ = new Formals; addFormal(CUR_POS, $$, *$1); $$->ellipsis = false; } { $$.formals = ATinsert(ATempty, $1); $$.ellipsis = false; }
| |
{ $$ = new Formals; $$->ellipsis = false; } { $$.formals = ATempty; $$.ellipsis = false; }
| ELLIPSIS | ELLIPSIS
{ $$ = new Formals; $$->ellipsis = true; } { $$.formals = ATempty; $$.ellipsis = true; }
; ;
formal formal
: ID { $$ = new Formal(data->symbols.create($1), 0); } : ID { $$ = makeFormal($1, makeNoDefaultValue()); }
| ID '?' expr { $$ = new Formal(data->symbols.create($1), $3); } | ID '?' expr { $$ = makeFormal($1, makeDefaultValue($3)); }
; ;
%% %%
#include "eval.hh"
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <eval.hh>
namespace nix { namespace nix {
static Expr * parse(EvalState & state, const char * text, static Expr parse(EvalState & state,
const Path & path, const Path & basePath) const char * text, const Path & path,
const Path & basePath)
{ {
yyscan_t scanner; yyscan_t scanner;
ParseData data(state.symbols); ParseData data;
data.basePath = basePath; data.basePath = basePath;
data.path = path; data.path = path;
@@ -462,7 +512,7 @@ static Expr * parse(EvalState & state, const char * text,
if (res) throw ParseError(data.error); if (res) throw ParseError(data.error);
try { try {
data.result->bindVars(state.staticBaseEnv); checkVarDefs(state.primOps, data.result);
} catch (Error & e) { } catch (Error & e) {
throw ParseError(format("%1%, in `%2%'") % e.msg() % path); throw ParseError(format("%1%, in `%2%'") % e.msg() % path);
} }
@@ -471,10 +521,16 @@ static Expr * parse(EvalState & state, const char * text,
} }
Expr * parseExprFromFile(EvalState & state, Path path) Expr parseExprFromFile(EvalState & state, Path path)
{ {
assert(path[0] == '/'); assert(path[0] == '/');
#if 0
/* Perhaps this is already an imploded parse tree? */
Expr e = ATreadFromNamedFile(path.c_str());
if (e) return e;
#endif
/* If `path' is a symlink, follow it. This is so that relative /* If `path' is a symlink, follow it. This is so that relative
path references work. */ path references work. */
struct stat st; struct stat st;
@@ -486,6 +542,8 @@ Expr * parseExprFromFile(EvalState & state, Path path)
} }
/* If `path' refers to a directory, append `/default.nix'. */ /* If `path' refers to a directory, append `/default.nix'. */
if (stat(path.c_str(), &st))
throw SysError(format("getting status of `%1%'") % path);
if (S_ISDIR(st.st_mode)) if (S_ISDIR(st.st_mode))
path = canonPath(path + "/default.nix"); path = canonPath(path + "/default.nix");
@@ -494,7 +552,7 @@ Expr * parseExprFromFile(EvalState & state, Path path)
} }
Expr * parseExprFromString(EvalState & state, Expr parseExprFromString(EvalState & state,
const string & s, const Path & basePath) const string & s, const Path & basePath)
{ {
return parse(state, s.c_str(), "(string)", basePath); return parse(state, s.c_str(), "(string)", basePath);
+495 -436
View File
File diff suppressed because it is too large. Load diff
-92
View File
@@ -1,92 +0,0 @@
#ifndef __SYMBOL_TABLE_H
#define __SYMBOL_TABLE_H
#include "config.h"
#include <map>
#if HAVE_TR1_UNORDERED_SET
#include <tr1/unordered_set>
#endif
#include "types.hh"
namespace nix {
/* Symbol table used by the parser and evaluator to represent and look
up identifiers and attribute sets efficiently.
SymbolTable::create() converts a string into a symbol. Symbols
have the property that they can be compared efficiently (using a
pointer equality test), because the symbol table stores only one
copy of each string. */
class Symbol
{
private:
const string * s; // pointer into SymbolTable
Symbol(const string * s) : s(s) { };
friend class SymbolTable;
public:
Symbol() : s(0) { };
bool operator == (const Symbol & s2) const
{
return s == s2.s;
}
bool operator != (const Symbol & s2) const
{
return s != s2.s;
}
bool operator < (const Symbol & s2) const
{
return s < s2.s;
}
operator const string & () const
{
return *s;
}
bool empty() const
{
return s->empty();
}
friend std::ostream & operator << (std::ostream & str, const Symbol & sym);
};
inline std::ostream & operator << (std::ostream & str, const Symbol & sym)
{
str << *sym.s;
return str;
}
class SymbolTable
{
private:
#if HAVE_TR1_UNORDERED_SET
typedef std::tr1::unordered_set<string> Symbols;
#else
typedef std::set<string> Symbols;
#endif
Symbols symbols;
public:
Symbol create(const string & s)
{
std::pair<Symbols::iterator, bool> res = symbols.insert(s);
return Symbol(&*res.first);
}
unsigned int size() const
{
return symbols.size();
}
};
}
#endif /* !__SYMBOL_TABLE_H */
-162
View File
@@ -1,162 +0,0 @@
#include "value-to-xml.hh"
#include "xml-writer.hh"
#include "util.hh"
#include <cstdlib>
namespace nix {
static XMLAttrs singletonAttrs(const string & name, const string & value)
{
XMLAttrs attrs;
attrs[name] = value;
return attrs;
}
static void printValueAsXML(EvalState & state, bool strict, bool location,
Value & v, XMLWriter & doc, PathSet & context, PathSet & drvsSeen);
static void posToXML(XMLAttrs & xmlAttrs, const Pos & pos)
{
xmlAttrs["path"] = pos.file;
xmlAttrs["line"] = (format("%1%") % pos.line).str();
xmlAttrs["column"] = (format("%1%") % pos.column).str();
}
static void showAttrs(EvalState & state, bool strict, bool location,
Bindings & attrs, XMLWriter & doc, PathSet & context, PathSet & drvsSeen)
{
StringSet names;
foreach (Bindings::iterator, i, attrs)
names.insert(i->name);
foreach (StringSet::iterator, i, names) {
Attr & a(*attrs.find(state.symbols.create(*i)));
XMLAttrs xmlAttrs;
xmlAttrs["name"] = *i;
if (location && a.pos != &noPos) posToXML(xmlAttrs, *a.pos);
XMLOpenElement _(doc, "attr", xmlAttrs);
printValueAsXML(state, strict, location,
*a.value, doc, context, drvsSeen);
}
}
static void printValueAsXML(EvalState & state, bool strict, bool location,
Value & v, XMLWriter & doc, PathSet & context, PathSet & drvsSeen)
{
checkInterrupt();
if (strict) state.forceValue(v);
switch (v.type) {
case tInt:
doc.writeEmptyElement("int", singletonAttrs("value", (format("%1%") % v.integer).str()));
break;
case tBool:
doc.writeEmptyElement("bool", singletonAttrs("value", v.boolean ? "true" : "false"));
break;
case tString:
/* !!! show the context? */
copyContext(v, context);
doc.writeEmptyElement("string", singletonAttrs("value", v.string.s));
break;
case tPath:
doc.writeEmptyElement("path", singletonAttrs("value", v.path));
break;
case tNull:
doc.writeEmptyElement("null");
break;
case tAttrs:
if (state.isDerivation(v)) {
XMLAttrs xmlAttrs;
Bindings::iterator a = v.attrs->find(state.symbols.create("derivation"));
Path drvPath;
a = v.attrs->find(state.sDrvPath);
if (a != v.attrs->end()) {
if (strict) state.forceValue(*a->value);
if (a->value->type == tString)
xmlAttrs["drvPath"] = drvPath = a->value->string.s;
}
a = v.attrs->find(state.sOutPath);
if (a != v.attrs->end()) {
if (strict) state.forceValue(*a->value);
if (a->value->type == tString)
xmlAttrs["outPath"] = a->value->string.s;
}
XMLOpenElement _(doc, "derivation", xmlAttrs);
if (drvPath != "" && drvsSeen.find(drvPath) == drvsSeen.end()) {
drvsSeen.insert(drvPath);
showAttrs(state, strict, location, *v.attrs, doc, context, drvsSeen);
} else
doc.writeEmptyElement("repeated");
}
else {
XMLOpenElement _(doc, "attrs");
showAttrs(state, strict, location, *v.attrs, doc, context, drvsSeen);
}
break;
case tList: {
XMLOpenElement _(doc, "list");
for (unsigned int n = 0; n < v.list.length; ++n)
printValueAsXML(state, strict, location, *v.list.elems[n], doc, context, drvsSeen);
break;
}
case tLambda: {
XMLAttrs xmlAttrs;
if (location) posToXML(xmlAttrs, v.lambda.fun->pos);
XMLOpenElement _(doc, "function", xmlAttrs);
if (v.lambda.fun->matchAttrs) {
XMLAttrs attrs;
if (!v.lambda.fun->arg.empty()) attrs["name"] = v.lambda.fun->arg;
if (v.lambda.fun->formals->ellipsis) attrs["ellipsis"] = "1";
XMLOpenElement _(doc, "attrspat", attrs);
foreach (Formals::Formals_::iterator, i, v.lambda.fun->formals->formals)
doc.writeEmptyElement("attr", singletonAttrs("name", i->name));
} else
doc.writeEmptyElement("varpat", singletonAttrs("name", v.lambda.fun->arg));
break;
}
default:
doc.writeEmptyElement("unevaluated");
}
}
void printValueAsXML(EvalState & state, bool strict, bool location,
Value & v, std::ostream & out, PathSet & context)
{
XMLWriter doc(true, out);
XMLOpenElement root(doc, "expr");
PathSet drvsSeen;
printValueAsXML(state, strict, location, v, doc, context, drvsSeen);
}
}
-17
View File
@@ -1,17 +0,0 @@
#ifndef __VALUE_TO_XML_H
#define __VALUE_TO_XML_H
#include "nixexpr.hh"
#include "eval.hh"
#include <string>
#include <map>
namespace nix {
void printValueAsXML(EvalState & state, bool strict, bool location,
Value & v, std::ostream & out, PathSet & context);
}
#endif /* !__VALUE_TO_XML_H */
+2 -2
View File
@@ -2,7 +2,7 @@ pkglib_LTLIBRARIES = libmain.la
libmain_la_SOURCES = shared.cc libmain_la_SOURCES = shared.cc
libmain_la_LIBADD = ../libstore/libstore.la @boehmgc_lib@ libmain_la_LIBADD = ../libstore/libstore.la
pkginclude_HEADERS = shared.hh pkginclude_HEADERS = shared.hh
@@ -15,5 +15,5 @@ AM_CXXFLAGS = \
-DNIX_LIBEXEC_DIR=\"$(libexecdir)\" \ -DNIX_LIBEXEC_DIR=\"$(libexecdir)\" \
-DNIX_BIN_DIR=\"$(bindir)\" \ -DNIX_BIN_DIR=\"$(bindir)\" \
-DNIX_VERSION=\"$(VERSION)\" \ -DNIX_VERSION=\"$(VERSION)\" \
-I$(srcdir)/.. -I$(srcdir)/../libutil \ -I$(srcdir)/.. ${aterm_include} -I$(srcdir)/../libutil \
-I$(srcdir)/../libstore -I$(srcdir)/../libstore
+22 -48
View File
@@ -13,9 +13,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#if HAVE_BOEHMGC #include <aterm2.h>
#include <gc/gc.h>
#endif
namespace nix { namespace nix {
@@ -54,26 +52,25 @@ void printGCWarning()
void printMissing(const PathSet & paths) void printMissing(const PathSet & paths)
{ {
unsigned long long downloadSize, narSize; unsigned long long downloadSize;
PathSet willBuild, willSubstitute, unknown; PathSet willBuild, willSubstitute, unknown;
queryMissing(paths, willBuild, willSubstitute, unknown, downloadSize, narSize); queryMissing(paths, willBuild, willSubstitute, unknown, downloadSize);
if (!willBuild.empty()) { if (!willBuild.empty()) {
printMsg(lvlInfo, format("these derivations will be built:")); printMsg(lvlInfo, format("the following derivations will be built:"));
foreach (PathSet::iterator, i, willBuild) foreach (PathSet::iterator, i, willBuild)
printMsg(lvlInfo, format(" %1%") % *i); printMsg(lvlInfo, format(" %1%") % *i);
} }
if (!willSubstitute.empty()) { if (!willSubstitute.empty()) {
printMsg(lvlInfo, format("these paths will be downloaded/copied (%.2f MiB download, %.2f MiB unpacked):") printMsg(lvlInfo, format("the following paths will be downloaded/copied (%.2f MiB):") %
% (downloadSize / (1024.0 * 1024.0)) (downloadSize / (1024.0 * 1024.0)));
% (narSize / (1024.0 * 1024.0)));
foreach (PathSet::iterator, i, willSubstitute) foreach (PathSet::iterator, i, willSubstitute)
printMsg(lvlInfo, format(" %1%") % *i); printMsg(lvlInfo, format(" %1%") % *i);
} }
if (!unknown.empty()) { if (!unknown.empty()) {
printMsg(lvlInfo, format("don't know how to build these paths%1%:") printMsg(lvlInfo, format("don't know how to build the following paths%1%:")
% (readOnlyMode ? " (may be caused by read-only store access)" : "")); % (readOnlyMode ? " (may be caused by read-only store access)" : ""));
foreach (PathSet::iterator, i, unknown) foreach (PathSet::iterator, i, unknown)
printMsg(lvlInfo, format(" %1%") % *i); printMsg(lvlInfo, format(" %1%") % *i);
@@ -90,6 +87,9 @@ static void setLogType(string lt)
} }
void initDerivationsHelpers();
static void closeStore() static void closeStore()
{ {
try { try {
@@ -140,7 +140,6 @@ static void initAndRun(int argc, char * * argv)
/* Get some settings from the configuration file. */ /* Get some settings from the configuration file. */
thisSystem = querySetting("system", SYSTEM); thisSystem = querySetting("system", SYSTEM);
maxBuildJobs = queryIntSetting("build-max-jobs", 1); maxBuildJobs = queryIntSetting("build-max-jobs", 1);
buildCores = queryIntSetting("build-cores", 1);
maxSilentTime = queryIntSetting("build-max-silent-time", 0); maxSilentTime = queryIntSetting("build-max-silent-time", 0);
/* Catch SIGINT. */ /* Catch SIGINT. */
@@ -177,6 +176,9 @@ static void initAndRun(int argc, char * * argv)
string lt = getEnv("NIX_LOG_TYPE"); string lt = getEnv("NIX_LOG_TYPE");
if (lt != "") setLogType(lt); if (lt != "") setLogType(lt);
/* ATerm stuff. !!! find a better place to put this */
initDerivationsHelpers();
/* Put the arguments in a vector. */ /* Put the arguments in a vector. */
Strings args, remaining; Strings args, remaining;
while (argc--) args.push_back(*argv++); while (argc--) args.push_back(*argv++);
@@ -201,16 +203,17 @@ static void initAndRun(int argc, char * * argv)
remaining.clear(); remaining.clear();
/* Process default options. */ /* Process default options. */
int verbosityDelta = 0;
for (Strings::iterator i = args.begin(); i != args.end(); ++i) { for (Strings::iterator i = args.begin(); i != args.end(); ++i) {
string arg = *i; string arg = *i;
if (arg == "--verbose" || arg == "-v") verbosityDelta++; if (arg == "--verbose" || arg == "-v")
else if (arg == "--quiet") verbosityDelta--; verbosity = (Verbosity) ((int) verbosity + 1);
else if (arg == "--log-type") { else if (arg == "--log-type") {
++i; ++i;
if (i == args.end()) throw UsageError("`--log-type' requires an argument"); if (i == args.end()) throw UsageError("`--log-type' requires an argument");
setLogType(*i); setLogType(*i);
} }
else if (arg == "--build-output" || arg == "-B")
; /* !!! obsolete - remove eventually */
else if (arg == "--no-build-output" || arg == "-Q") else if (arg == "--no-build-output" || arg == "-Q")
buildVerbosity = lvlVomit; buildVerbosity = lvlVomit;
else if (arg == "--print-build-trace") else if (arg == "--print-build-trace")
@@ -231,8 +234,6 @@ static void initAndRun(int argc, char * * argv)
tryFallback = true; tryFallback = true;
else if (arg == "--max-jobs" || arg == "-j") else if (arg == "--max-jobs" || arg == "-j")
maxBuildJobs = getIntArg<unsigned int>(arg, i, args.end()); maxBuildJobs = getIntArg<unsigned int>(arg, i, args.end());
else if (arg == "--cores")
buildCores = getIntArg<unsigned int>(arg, i, args.end());
else if (arg == "--readonly-mode") else if (arg == "--readonly-mode")
readOnlyMode = true; readOnlyMode = true;
else if (arg == "--max-silent-time") else if (arg == "--max-silent-time")
@@ -251,9 +252,6 @@ static void initAndRun(int argc, char * * argv)
else remaining.push_back(arg); else remaining.push_back(arg);
} }
verbosityDelta += queryIntSetting("verbosity", lvlInfo);
verbosity = (Verbosity) (verbosityDelta < 0 ? 0 : verbosityDelta);
/* Automatically clean up the temporary roots file when we /* Automatically clean up the temporary roots file when we
exit. */ exit. */
RemoveTempRoots removeTempRoots __attribute__((unused)); RemoveTempRoots removeTempRoots __attribute__((unused));
@@ -321,14 +319,6 @@ static void setuidInit()
} }
/* Called when the Boehm GC runs out of memory. */
static void * oomHandler(size_t requested)
{
/* Convert this to a proper C++ exception. */
throw std::bad_alloc();
}
} }
@@ -343,6 +333,10 @@ int main(int argc, char * * argv)
if (argc == 0) abort(); if (argc == 0) abort();
setuidInit(); setuidInit();
/* ATerm setup. */
ATerm bottomOfStack;
ATinit(argc, argv, &bottomOfStack);
/* Turn on buffering for cerr. */ /* Turn on buffering for cerr. */
#if HAVE_PUBSETBUF #if HAVE_PUBSETBUF
std::cerr.rdbuf()->pubsetbuf(buf, sizeof(buf)); std::cerr.rdbuf()->pubsetbuf(buf, sizeof(buf));
@@ -350,26 +344,6 @@ int main(int argc, char * * argv)
std::ios::sync_with_stdio(false); std::ios::sync_with_stdio(false);
#if HAVE_BOEHMGC
/* Initialise the Boehm garbage collector. This isn't necessary
on most platforms, but for portability we do it anyway. */
GC_INIT();
GC_oom_fn = oomHandler;
/* Set the initial heap size to something fairly big (384 MiB) so
that in most cases we don't need to garbage collect at all.
(Collection has a fairly significant overhead, some.) The heap
size can be overriden through libgc's GC_INITIAL_HEAP_SIZE
environment variable. We should probably also provide a
nix.conf setting for this. Note that GC_expand_hp() causes a
lot of virtual, but not physical (resident) memory to be
allocated. This might be a problem on systems that don't
overcommit. */
if (!getenv("GC_INITIAL_HEAP_SIZE"))
GC_expand_hp(384 * 1024 * 1024);
#endif
try { try {
try { try {
initAndRun(argc, argv); initAndRun(argc, argv);
@@ -393,7 +367,7 @@ int main(int argc, char * * argv)
printMsg(lvlError, format("error: %1%%2%") % (showTrace ? e.prefix() : "") % e.msg()); printMsg(lvlError, format("error: %1%%2%") % (showTrace ? e.prefix() : "") % e.msg());
if (e.prefix() != "" && !showTrace) if (e.prefix() != "" && !showTrace)
printMsg(lvlError, "(use `--show-trace' to show detailed location information)"); printMsg(lvlError, "(use `--show-trace' to show detailed location information)");
return e.status; return 1;
} catch (std::exception & e) { } catch (std::exception & e) {
printMsg(lvlError, format("error: %1%") % e.what()); printMsg(lvlError, format("error: %1%") % e.what());
return 1; return 1;
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef __SHARED_H #ifndef __SHARED_H
#define __SHARED_H #define __SHARED_H
#include "util.hh" #include "types.hh"
#include <signal.h> #include <signal.h>
+7 -7
View File
@@ -10,14 +10,14 @@ pkginclude_HEADERS = \
globals.hh references.hh pathlocks.hh \ globals.hh references.hh pathlocks.hh \
worker-protocol.hh worker-protocol.hh
libstore_la_LIBADD = ../libutil/libutil.la ../boost/format/libformat.la ${aterm_lib} ${sqlite_lib} libstore_la_LIBADD = ../libutil/libutil.la ../boost/format/libformat.la @ADDITIONAL_NETWORK_LIBS@
EXTRA_DIST = schema.sql BUILT_SOURCES = derivations-ast.cc derivations-ast.hh
EXTRA_DIST = derivations-ast.def derivations-ast.cc
AM_CXXFLAGS = -Wall \ AM_CXXFLAGS = -Wall \
${sqlite_include} -I$(srcdir)/.. -I$(srcdir)/../libutil -I$(srcdir)/.. ${aterm_include} -I$(srcdir)/../libutil
local-store.lo: schema.sql.hh derivations-ast.cc derivations-ast.hh: ../aterm-helper.pl derivations-ast.def
$(perl) $(srcdir)/../aterm-helper.pl derivations-ast.hh derivations-ast.cc < $(srcdir)/derivations-ast.def
%.sql.hh: %.sql
../bin2c/bin2c schema < $< > $@ || (rm $@ && exit 1)
+259 -341
View File
@@ -25,7 +25,6 @@
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <cstring>
#include <pwd.h> #include <pwd.h>
#include <grp.h> #include <grp.h>
@@ -64,7 +63,6 @@ static const uid_t rootUserId = 0;
/* Forward definition. */ /* Forward definition. */
class Worker; class Worker;
class HookInstance;
/* A pointer to a goal. */ /* A pointer to a goal. */
@@ -164,7 +162,6 @@ struct Child
{ {
WeakGoalPtr goal; WeakGoalPtr goal;
set<int> fds; set<int> fds;
bool monitorForSilence;
bool inBuildSlot; bool inBuildSlot;
time_t lastOutput; /* time we last got output on stdout/stderr */ time_t lastOutput; /* time we last got output on stdout/stderr */
}; };
@@ -214,14 +211,8 @@ public:
bool cacheFailure; bool cacheFailure;
/* Set if at least one derivation had a BuildError (i.e. permanent
failure). */
bool permanentFailure;
LocalStore & store; LocalStore & store;
boost::shared_ptr<HookInstance> hook;
Worker(LocalStore & store); Worker(LocalStore & store);
~Worker(); ~Worker();
@@ -243,7 +234,7 @@ public:
/* Registers a running child process. `inBuildSlot' means that /* Registers a running child process. `inBuildSlot' means that
the process counts towards the jobs limit. */ the process counts towards the jobs limit. */
void childStarted(GoalPtr goal, pid_t pid, void childStarted(GoalPtr goal, pid_t pid,
const set<int> & fds, bool inBuildSlot, bool monitorForSilence); const set<int> & fds, bool inBuildSlot);
/* Unregisters a running child process. `wakeSleepers' should be /* Unregisters a running child process. `wakeSleepers' should be
false if there is no sense in waking up goals that are sleeping false if there is no sense in waking up goals that are sleeping
@@ -270,13 +261,12 @@ public:
/* Wait for input to become available. */ /* Wait for input to become available. */
void waitForInput(); void waitForInput();
unsigned int exitStatus();
}; };
MakeError(SubstError, Error) MakeError(SubstError, Error)
MakeError(BuildError, Error) /* denotes a permanent build failure */ MakeError(BuildError, Error) /* denoted a permanent build failure */
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
@@ -623,107 +613,6 @@ void deletePathWrapped(const Path & path)
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
struct HookInstance
{
/* Pipes for talking to the build hook. */
Pipe toHook;
/* Pipe for the hook's standard output/error. */
Pipe fromHook;
/* Pipe for the builder's standard output/error. */
Pipe builderOut;
/* The process ID of the hook. */
Pid pid;
HookInstance();
~HookInstance();
};
HookInstance::HookInstance()
{
debug("starting build hook");
Path buildHook = absPath(getEnv("NIX_BUILD_HOOK"));
/* Create a pipe to get the output of the child. */
fromHook.create();
/* Create the communication pipes. */
toHook.create();
/* Create a pipe to get the output of the builder. */
builderOut.create();
/* Fork the hook. */
pid = fork();
switch (pid) {
case -1:
throw SysError("unable to fork");
case 0:
try { /* child */
commonChildInit(fromHook);
if (chdir("/") == -1) throw SysError("changing into `/");
/* Dup the communication pipes. */
toHook.writeSide.close();
if (dup2(toHook.readSide, STDIN_FILENO) == -1)
throw SysError("dupping to-hook read side");
/* Use fd 4 for the builder's stdout/stderr. */
builderOut.readSide.close();
if (dup2(builderOut.writeSide, 4) == -1)
throw SysError("dupping builder's stdout/stderr");
execl(buildHook.c_str(), buildHook.c_str(), thisSystem.c_str(),
(format("%1%") % maxSilentTime).str().c_str(),
(format("%1%") % printBuildTrace).str().c_str(),
NULL);
throw SysError(format("executing `%1%'") % buildHook);
} catch (std::exception & e) {
std::cerr << format("build hook error: %1%") % e.what() << std::endl;
}
quickExit(1);
}
/* parent */
pid.setSeparatePG(true);
pid.setKillSignal(SIGTERM);
fromHook.writeSide.close();
toHook.readSide.close();
}
HookInstance::~HookInstance()
{
try {
/* Cleanly shut down the hook by closing its stdin if it's not
already building. Otherwise pid's destructor will kill
it. */
if (pid != -1 && toHook.writeSide != -1) {
toHook.writeSide.close();
pid.wait(true);
}
} catch (...) {
ignoreException();
}
}
//////////////////////////////////////////////////////////////////////
typedef enum {rpAccept, rpDecline, rpPostpone} HookReply;
class DerivationGoal : public Goal class DerivationGoal : public Goal
{ {
private: private:
@@ -758,11 +647,14 @@ private:
AutoCloseFD fdLogFile; AutoCloseFD fdLogFile;
/* Pipe for the builder's standard output/error. */ /* Pipe for the builder's standard output/error. */
Pipe builderOut; Pipe logPipe;
/* Whether we're building using a build hook. */
bool usingBuildHook;
/* Pipes for talking to the build hook (if any). */
Pipe toHook;
/* The build hook. */
boost::shared_ptr<HookInstance> hook;
/* Whether we're currently doing a chroot build. */ /* Whether we're currently doing a chroot build. */
bool useChroot; bool useChroot;
@@ -800,8 +692,12 @@ private:
void buildDone(); void buildDone();
/* Is the build hook willing to perform the build? */ /* Is the build hook willing to perform the build? */
typedef enum {rpAccept, rpDecline, rpPostpone} HookReply;
HookReply tryBuildHook(); HookReply tryBuildHook();
/* Synchronously wait for a build hook to finish. */
void terminateBuildHook(bool kill = false);
/* Start building a derivation. */ /* Start building a derivation. */
void startBuilder(); void startBuilder();
@@ -813,6 +709,10 @@ private:
/* Open a log file and a pipe to it. */ /* Open a log file and a pipe to it. */
Path openLogFile(); Path openLogFile();
/* Common initialisation to be performed in child processes (i.e.,
both in builders and in build hooks). */
void initChild();
/* Delete the temporary directory, if we have one. */ /* Delete the temporary directory, if we have one. */
void deleteTmpDir(bool force); void deleteTmpDir(bool force);
@@ -840,7 +740,6 @@ DerivationGoal::DerivationGoal(const Path & drvPath, Worker & worker)
trace("created"); trace("created");
} }
DerivationGoal::~DerivationGoal() DerivationGoal::~DerivationGoal()
{ {
/* Careful: we should never ever throw an exception from a /* Careful: we should never ever throw an exception from a
@@ -853,7 +752,6 @@ DerivationGoal::~DerivationGoal()
} }
} }
void DerivationGoal::killChild() void DerivationGoal::killChild()
{ {
if (pid != -1) { if (pid != -1) {
@@ -878,8 +776,6 @@ void DerivationGoal::killChild()
assert(pid == -1); assert(pid == -1);
} }
hook.reset();
} }
@@ -989,10 +885,7 @@ void DerivationGoal::outputsSubstituted()
foreach (PathSet::iterator, i, drv.inputSrcs) foreach (PathSet::iterator, i, drv.inputSrcs)
addWaitee(worker.makeSubstitutionGoal(*i)); addWaitee(worker.makeSubstitutionGoal(*i));
if (waitees.empty()) /* to prevent hang (no wake-up event) */ state = &DerivationGoal::inputsRealised;
inputsRealised();
else
state = &DerivationGoal::inputsRealised;
} }
@@ -1066,16 +959,6 @@ PathSet outputPaths(const DerivationOutputs & outputs)
} }
static bool canBuildLocally(const string & platform)
{
return platform == thisSystem
#ifdef CAN_DO_LINUX32_BUILDS
|| (platform == "i686-linux" && thisSystem == "x86_64-linux")
#endif
;
}
void DerivationGoal::tryToBuild() void DerivationGoal::tryToBuild()
{ {
trace("trying to build"); trace("trying to build");
@@ -1143,36 +1026,28 @@ void DerivationGoal::tryToBuild()
foreach (DerivationOutputs::iterator, i, drv.outputs) foreach (DerivationOutputs::iterator, i, drv.outputs)
if (pathFailed(i->second.path)) return; if (pathFailed(i->second.path)) return;
/* Don't do a remote build if the derivation has the attribute
`preferLocalBuild' set. */
bool preferLocalBuild =
drv.env["preferLocalBuild"] == "1" && canBuildLocally(drv.platform);
/* Is the build hook willing to accept this job? */ /* Is the build hook willing to accept this job? */
if (!preferLocalBuild) { usingBuildHook = true;
switch (tryBuildHook()) { switch (tryBuildHook()) {
case rpAccept: case rpAccept:
/* Yes, it has started doing so. Wait until we get /* Yes, it has started doing so. Wait until we get EOF
EOF from the hook. */ from the hook. */
state = &DerivationGoal::buildDone; state = &DerivationGoal::buildDone;
return; return;
case rpPostpone: case rpPostpone:
/* Not now; wait until at least one child finishes or /* Not now; wait until at least one child finishes. */
the wake-up timeout expires. */ worker.waitForAWhile(shared_from_this());
worker.waitForAWhile(shared_from_this()); outputLocks.unlock();
outputLocks.unlock(); return;
return; case rpDecline:
case rpDecline: /* We should do it ourselves. */
/* We should do it ourselves. */ break;
break;
}
} }
/* Make sure that we are allowed to start a build. If this usingBuildHook = false;
derivation prefers to be done locally, do it even if
maxBuildJobs is 0. */ /* Make sure that we are allowed to start a build. */
unsigned int curBuilds = worker.getNrLocalBuilds(); if (worker.getNrLocalBuilds() >= maxBuildJobs) {
if (curBuilds >= maxBuildJobs && !(preferLocalBuild && curBuilds == 0)) {
worker.waitForBuildSlot(shared_from_this()); worker.waitForBuildSlot(shared_from_this());
outputLocks.unlock(); outputLocks.unlock();
return; return;
@@ -1190,7 +1065,6 @@ void DerivationGoal::tryToBuild()
if (printBuildTrace) if (printBuildTrace)
printMsg(lvlError, format("@ build-failed %1% %2% %3% %4%") printMsg(lvlError, format("@ build-failed %1% %2% %3% %4%")
% drvPath % drv.outputs["out"].path % 0 % e.msg()); % drvPath % drv.outputs["out"].path % 0 % e.msg());
worker.permanentFailure = true;
amDone(ecFailed); amDone(ecFailed);
return; return;
} }
@@ -1209,29 +1083,18 @@ void DerivationGoal::buildDone()
to have terminated. In fact, the builder could also have to have terminated. In fact, the builder could also have
simply have closed its end of the pipe --- just don't do that simply have closed its end of the pipe --- just don't do that
:-) */ :-) */
int status; /* !!! this could block! security problem! solution: kill the
pid_t savedPid; child */
if (hook) { pid_t savedPid = pid;
savedPid = hook->pid; int status = pid.wait(true);
status = hook->pid.wait(true);
} else {
/* !!! this could block! security problem! solution: kill the
child */
savedPid = pid;
status = pid.wait(true);
}
debug(format("builder process for `%1%' finished") % drvPath); debug(format("builder process for `%1%' finished") % drvPath);
/* So the child is gone now. */ /* So the child is gone now. */
worker.childTerminated(savedPid); worker.childTerminated(savedPid);
/* Close the read side of the logger pipe. */ /* Close the read side of the logger pipe. */
if (hook) { logPipe.readSide.close();
hook->builderOut.readSide.close();
hook->fromHook.readSide.close();
}
else builderOut.readSide.close();
/* Close the log file. */ /* Close the log file. */
fdLogFile.close(); fdLogFile.close();
@@ -1304,11 +1167,11 @@ void DerivationGoal::buildDone()
/* When using a build hook, the hook will return a remote /* When using a build hook, the hook will return a remote
build failure using exit code 100. Anything else is a hook build failure using exit code 100. Anything else is a hook
problem. */ problem. */
bool hookError = hook && bool hookError = usingBuildHook &&
(!WIFEXITED(status) || WEXITSTATUS(status) != 100); (!WIFEXITED(status) || WEXITSTATUS(status) != 100);
if (printBuildTrace) { if (printBuildTrace) {
if (hook && hookError) if (usingBuildHook && hookError)
printMsg(lvlError, format("@ hook-failed %1% %2% %3% %4%") printMsg(lvlError, format("@ hook-failed %1% %2% %3% %4%")
% drvPath % drv.outputs["out"].path % status % e.msg()); % drvPath % drv.outputs["out"].path % status % e.msg());
else else
@@ -1327,7 +1190,6 @@ void DerivationGoal::buildDone()
foreach (DerivationOutputs::iterator, i, drv.outputs) foreach (DerivationOutputs::iterator, i, drv.outputs)
worker.store.registerFailedPath(i->second.path); worker.store.registerFailedPath(i->second.path);
worker.permanentFailure = !hookError && !fixedOutput;
amDone(ecFailed); amDone(ecFailed);
return; return;
} }
@@ -1344,85 +1206,162 @@ void DerivationGoal::buildDone()
} }
HookReply DerivationGoal::tryBuildHook() DerivationGoal::HookReply DerivationGoal::tryBuildHook()
{ {
if (!useBuildHook || getEnv("NIX_BUILD_HOOK") == "") return rpDecline; if (!useBuildHook) return rpDecline;
Path buildHook = getEnv("NIX_BUILD_HOOK");
if (buildHook == "") return rpDecline;
buildHook = absPath(buildHook);
if (!worker.hook) /* Create a directory where we will store files used for
worker.hook = boost::shared_ptr<HookInstance>(new HookInstance); communication between us and the build hook. */
tmpDir = createTempDir();
/* Create the log file and pipe. */
Path logFile = openLogFile();
/* Tell the hook about system features (beyond the system type) /* Create the communication pipes. */
required from the build machine. (The hook could parse the toHook.create();
drv file itself, but this is easier.) */
Strings features = tokenizeString(drv.env["requiredSystemFeatures"]);
foreach (Strings::iterator, i, features) checkStoreName(*i); /* !!! abuse */
/* Send the request to the hook. */ /* Fork the hook. */
writeLine(worker.hook->toHook.writeSide, (format("%1% %2% %3% %4%") pid = fork();
% (worker.getNrLocalBuilds() < maxBuildJobs ? "1" : "0") switch (pid) {
% drv.platform % drvPath % concatStringsSep(",", features)).str());
case -1:
throw SysError("unable to fork");
case 0:
try { /* child */
initChild();
string s;
foreach (DerivationOutputs::const_iterator, i, drv.outputs)
s += i->second.path + " ";
if (setenv("NIX_HELD_LOCKS", s.c_str(), 1))
throw SysError("setting an environment variable");
execl(buildHook.c_str(), buildHook.c_str(),
(worker.getNrLocalBuilds() < maxBuildJobs ? (string) "1" : "0").c_str(),
thisSystem.c_str(),
drv.platform.c_str(),
drvPath.c_str(),
(format("%1%") % maxSilentTime).str().c_str(),
NULL);
throw SysError(format("executing `%1%'") % buildHook);
} catch (std::exception & e) {
std::cerr << format("build hook error: %1%") % e.what() << std::endl;
}
quickExit(1);
}
/* parent */
pid.setSeparatePG(true);
pid.setKillSignal(SIGTERM);
logPipe.writeSide.close();
worker.childStarted(shared_from_this(),
pid, singleton<set<int> >(logPipe.readSide), false);
toHook.readSide.close();
/* Read the first line of input, which should be a word indicating /* Read the first line of input, which should be a word indicating
whether the hook wishes to perform the build. */ whether the hook wishes to perform the build. */
string reply; string reply;
while (true) { try {
string s = readLine(worker.hook->fromHook.readSide); while (true) {
if (string(s, 0, 2) == "# ") { string s = readLine(logPipe.readSide);
reply = string(s, 2); if (string(s, 0, 2) == "# ") {
break; reply = string(s, 2);
break;
}
handleChildOutput(logPipe.readSide, s + "\n");
} }
s += "\n"; } catch (Error & e) {
writeToStderr((unsigned char *) s.c_str(), s.size()); terminateBuildHook(true);
throw;
} }
debug(format("hook reply is `%1%'") % reply); debug(format("hook reply is `%1%'") % reply);
if (reply == "decline" || reply == "postpone") if (reply == "decline" || reply == "postpone") {
/* Clean up the child. !!! hacky / should verify */
terminateBuildHook();
return reply == "decline" ? rpDecline : rpPostpone; return reply == "decline" ? rpDecline : rpPostpone;
else if (reply != "accept") }
throw Error(format("bad hook reply `%1%'") % reply);
printMsg(lvlTalkative, format("using hook to build path(s) %1%") else if (reply == "accept") {
% showPaths(outputPaths(drv.outputs)));
hook = worker.hook; printMsg(lvlInfo, format("using hook to build path(s) %1%")
worker.hook.reset(); % showPaths(outputPaths(drv.outputs)));
/* Tell the hook all the inputs that have to be copied to the /* Write the information that the hook needs to perform the
remote system. This unfortunately has to contain the entire build, i.e., the set of input paths, the set of output
derivation closure to ensure that the validity invariant holds paths, and the references (pointer graph) in the input
on the remote system. (I.e., it's unfortunate that we have to paths. */
list it since the remote system *probably* already has it.) */
PathSet allInputs;
allInputs.insert(inputPaths.begin(), inputPaths.end());
computeFSClosure(drvPath, allInputs);
string s; Path inputListFN = tmpDir + "/inputs";
foreach (PathSet::iterator, i, allInputs) s += *i + " "; Path outputListFN = tmpDir + "/outputs";
writeLine(hook->toHook.writeSide, s); Path referencesFN = tmpDir + "/references";
/* Tell the hooks the outputs that have to be copied back from the
remote system. */
s = "";
foreach (DerivationOutputs::iterator, i, drv.outputs)
s += i->second.path + " ";
writeLine(hook->toHook.writeSide, s);
hook->toHook.writeSide.close();
/* Create the log file and pipe. */ /* The `inputs' file lists all inputs that have to be copied
Path logFile = openLogFile(); to the remote system. This unfortunately has to contain
the entire derivation closure to ensure that the validity
set<int> fds; invariant holds on the remote system. (I.e., it's
fds.insert(hook->fromHook.readSide); unfortunate that we have to list it since the remote system
fds.insert(hook->builderOut.readSide); *probably* already has it.) */
worker.childStarted(shared_from_this(), hook->pid, fds, false, false); PathSet allInputs;
allInputs.insert(inputPaths.begin(), inputPaths.end());
if (printBuildTrace) computeFSClosure(drvPath, allInputs);
printMsg(lvlError, format("@ build-started %1% %2% %3% %4%")
% drvPath % drv.outputs["out"].path % drv.platform % logFile);
return rpAccept; string s;
foreach (PathSet::iterator, i, allInputs) s += *i + "\n";
writeFile(inputListFN, s);
/* The `outputs' file lists all outputs that have to be copied
from the remote system. */
s = "";
foreach (DerivationOutputs::iterator, i, drv.outputs)
s += i->second.path + "\n";
writeFile(outputListFN, s);
/* The `references' file has exactly the format accepted by
`nix-store --register-validity'. */
writeFile(referencesFN,
makeValidityRegistration(allInputs, true, false));
/* Tell the hook to proceed. */
writeLine(toHook.writeSide, "okay");
toHook.writeSide.close();
if (printBuildTrace)
printMsg(lvlError, format("@ build-started %1% %2% %3% %4%")
% drvPath % drv.outputs["out"].path % drv.platform % logFile);
return rpAccept;
}
else throw Error(format("bad hook reply `%1%'") % reply);
}
void DerivationGoal::terminateBuildHook(bool kill)
{
debug("terminating build hook");
pid_t savedPid = pid;
if (kill)
pid.kill();
else
pid.wait(true);
/* `false' means don't wake up waiting goals, since we want to
keep this build slot ourselves. */
worker.childTerminated(savedPid, false);
toHook.writeSide.close();
fdLogFile.close();
logPipe.readSide.close();
deleteTmpDir(true); /* get rid of the hook's temporary directory */
} }
@@ -1439,7 +1378,11 @@ void DerivationGoal::startBuilder()
format("building path(s) %1%") % showPaths(outputPaths(drv.outputs))) format("building path(s) %1%") % showPaths(outputPaths(drv.outputs)))
/* Right platform? */ /* Right platform? */
if (!canBuildLocally(drv.platform)) if (drv.platform != thisSystem
#ifdef CAN_DO_LINUX32_BUILDS
&& !(drv.platform == "i686-linux" && thisSystem == "x86_64-linux")
#endif
)
throw Error( throw Error(
format("a `%1%' is required to build `%3%', but I am a `%2%'") format("a `%1%' is required to build `%3%', but I am a `%2%'")
% drv.platform % thisSystem % drvPath); % drv.platform % thisSystem % drvPath);
@@ -1467,9 +1410,6 @@ void DerivationGoal::startBuilder()
in the store or in the build directory). */ in the store or in the build directory). */
env["NIX_STORE"] = nixStore; env["NIX_STORE"] = nixStore;
/* The maximum number of cores to utilize for parallel building. */
env["NIX_BUILD_CORES"] = (format("%d") % buildCores).str();
/* Add all bindings specified in the derivation. */ /* Add all bindings specified in the derivation. */
foreach (StringPairs::iterator, i, drv.env) foreach (StringPairs::iterator, i, drv.env)
env[i->first] = i->second; env[i->first] = i->second;
@@ -1554,7 +1494,7 @@ void DerivationGoal::startBuilder()
/* Write closure info to `fileName'. */ /* Write closure info to `fileName'. */
writeFile(tmpDir + "/" + fileName, writeFile(tmpDir + "/" + fileName,
worker.store.makeValidityRegistration(paths, false, false)); makeValidityRegistration(paths, false, false));
} }
@@ -1604,9 +1544,6 @@ void DerivationGoal::startBuilder()
if (fixedOutput) useChroot = false; if (fixedOutput) useChroot = false;
/* Hack to allow derivations to disable chroot builds. */
if (drv.env["__noChroot"] == "1") useChroot = false;
if (useChroot) { if (useChroot) {
#if CHROOT_ENABLED #if CHROOT_ENABLED
/* Create a temporary directory in which we set up the chroot /* Create a temporary directory in which we set up the chroot
@@ -1628,23 +1565,16 @@ void DerivationGoal::startBuilder()
createDirs(chrootTmpDir); createDirs(chrootTmpDir);
chmod(chrootTmpDir, 01777); chmod(chrootTmpDir, 01777);
/* Create a /etc/passwd with entries for the build user and the /* Create a /etc/passwd with entries for the build user and
nobody account. The latter is kind of a hack to support the nobody account. The latter is kind of a hack to
Samba-in-QEMU. */ support Samba-in-QEMU. */
createDirs(chrootRootDir + "/etc"); createDirs(chrootRootDir + "/etc");
writeFile(chrootRootDir + "/etc/passwd", writeFile(chrootRootDir + "/etc/passwd",
(format( (format(
"nixbld:x:%1%:%2%:Nix build user:/:/noshell\n" "nixbld:x:%1%:65534:Nix build user:/:/noshell\n"
"nobody:x:65534:65534:Nobody:/:/noshell\n") "nobody:x:65534:65534:Nobody:/:/noshell\n")
% (buildUser.enabled() ? buildUser.getUID() : getuid()) % (buildUser.enabled() ? buildUser.getUID() : getuid())).str());
% (buildUser.enabled() ? buildUser.getGID() : getgid())).str());
/* Declare the build user's group so that programs get a consistent
view of the system (e.g., "id -gn"). */
writeFile(chrootRootDir + "/etc/group",
(format("nixbld:!:%1%:\n")
% (buildUser.enabled() ? buildUser.getGID() : getgid())).str());
/* Bind-mount a user-configurable set of directories from the /* Bind-mount a user-configurable set of directories from the
host file system. The `/dev/pts' directory must be mounted host file system. The `/dev/pts' directory must be mounted
@@ -1703,12 +1633,9 @@ void DerivationGoal::startBuilder()
printMsg(lvlChatty, format("executing builder `%1%'") % printMsg(lvlChatty, format("executing builder `%1%'") %
drv.builder); drv.builder);
/* Create the log file. */ /* Create the log file and pipe. */
Path logFile = openLogFile(); Path logFile = openLogFile();
/* Create a pipe to get the output of the builder. */
builderOut.create();
/* Fork a child to build the package. Note that while we /* Fork a child to build the package. Note that while we
currently use forks to run and wait for the children, it currently use forks to run and wait for the children, it
shouldn't be hard to use threads for this on systems where shouldn't be hard to use threads for this on systems where
@@ -1722,7 +1649,7 @@ void DerivationGoal::startBuilder()
case 0: case 0:
/* Warning: in the child we should absolutely not make any /* Warning: in the child we should absolutely not make any
SQLite calls! */ Berkeley DB calls! */
try { /* child */ try { /* child */
@@ -1749,23 +1676,18 @@ void DerivationGoal::startBuilder()
throw SysError(format("bind mount from `%1%' to `%2%' failed") % source % target); throw SysError(format("bind mount from `%1%' to `%2%' failed") % source % target);
} }
/* Do the chroot(). Below we do a chdir() to the /* Do the chroot(). initChild() will do a chdir() to
temporary build directory to make sure the current the temporary build directory to make sure the
directory is in the chroot. (Actually the order current directory is in the chroot. (Actually the
doesn't matter, since due to the bind mount tmpDir order doesn't matter, since due to the bind mount
and tmpRootDit/tmpDir are the same directories.) */ tmpDir and tmpRootDit/tmpDir are the same
directories.) */
if (chroot(chrootRootDir.c_str()) == -1) if (chroot(chrootRootDir.c_str()) == -1)
throw SysError(format("cannot change root directory to `%1%'") % chrootRootDir); throw SysError(format("cannot change root directory to `%1%'") % chrootRootDir);
} }
#endif #endif
commonChildInit(builderOut); initChild();
if (chdir(tmpDir.c_str()) == -1)
throw SysError(format("changing into `%1%'") % tmpDir);
/* Close all other file descriptors. */
closeMostFDs(set<int>());
#ifdef CAN_DO_LINUX32_BUILDS #ifdef CAN_DO_LINUX32_BUILDS
if (drv.platform == "i686-linux" && thisSystem == "x86_64-linux") { if (drv.platform == "i686-linux" && thisSystem == "x86_64-linux") {
@@ -1786,10 +1708,10 @@ void DerivationGoal::startBuilder()
/* If we are running in `build-users' mode, then switch to /* If we are running in `build-users' mode, then switch to
the user we allocated above. Make sure that we drop the user we allocated above. Make sure that we drop
all root privileges. Note that above we have closed all root privileges. Note that initChild() above has
all file descriptors except std*, so that's safe. Also closed all file descriptors except std*, so that's
note that setuid() when run as root sets the real, safe. Also note that setuid() when run as root sets
effective and saved UIDs. */ the real, effective and saved UIDs. */
if (buildUser.enabled()) { if (buildUser.enabled()) {
printMsg(lvlChatty, format("switching to user `%1%'") % buildUser.getUser()); printMsg(lvlChatty, format("switching to user `%1%'") % buildUser.getUser());
@@ -1843,9 +1765,9 @@ void DerivationGoal::startBuilder()
/* parent */ /* parent */
pid.setSeparatePG(true); pid.setSeparatePG(true);
builderOut.writeSide.close(); logPipe.writeSide.close();
worker.childStarted(shared_from_this(), pid, worker.childStarted(shared_from_this(), pid,
singleton<set<int> >(builderOut.readSide), true, true); singleton<set<int> >(logPipe.readSide), true);
if (printBuildTrace) { if (printBuildTrace) {
printMsg(lvlError, format("@ build-started %1% %2% %3% %4%") printMsg(lvlError, format("@ build-started %1% %2% %3% %4%")
@@ -1877,12 +1799,12 @@ PathSet parseReferenceSpecifiers(const Derivation & drv, string attr)
void DerivationGoal::computeClosure() void DerivationGoal::computeClosure()
{ {
map<Path, PathSet> allReferences; map<Path, PathSet> allReferences;
map<Path, HashResult> contentHashes; map<Path, Hash> contentHashes;
/* When using a build hook, the build hook can register the output /* When using a build hook, the build hook can register the output
as valid (by doing `nix-store --import'). If so we don't have as valid (by doing `nix-store --import'). If so we don't have
to do anything here. */ to do anything here. */
if (hook) { if (usingBuildHook) {
bool allValid = true; bool allValid = true;
foreach (DerivationOutputs::iterator, i, drv.outputs) foreach (DerivationOutputs::iterator, i, drv.outputs)
if (!worker.store.isValidPath(i->second.path)) allValid = false; if (!worker.store.isValidPath(i->second.path)) allValid = false;
@@ -1934,7 +1856,7 @@ void DerivationGoal::computeClosure()
if (ht == htUnknown) if (ht == htUnknown)
throw BuildError(format("unknown hash algorithm `%1%'") % algo); throw BuildError(format("unknown hash algorithm `%1%'") % algo);
Hash h = parseHash(ht, i->second.hash); Hash h = parseHash(ht, i->second.hash);
Hash h2 = recursive ? hashPath(ht, path).first : hashFile(ht, path); Hash h2 = recursive ? hashPath(ht, path) : hashFile(ht, path);
if (h != h2) if (h != h2)
throw BuildError( throw BuildError(
format("output path `%1%' should have %2% hash `%3%', instead has `%4%'") format("output path `%1%' should have %2% hash `%3%', instead has `%4%'")
@@ -1948,7 +1870,7 @@ void DerivationGoal::computeClosure()
contained in it. Compute the SHA-256 NAR hash at the same contained in it. Compute the SHA-256 NAR hash at the same
time. The hash is stored in the database so that we can time. The hash is stored in the database so that we can
verify later on whether nobody has messed with the store. */ verify later on whether nobody has messed with the store. */
HashResult hash; Hash hash;
PathSet references = scanForReferences(path, allPaths, hash); PathSet references = scanForReferences(path, allPaths, hash);
contentHashes[path] = hash; contentHashes[path] = hash;
@@ -1977,18 +1899,14 @@ void DerivationGoal::computeClosure()
} }
/* Register each output path as valid, and register the sets of /* Register each output path as valid, and register the sets of
paths referenced by each of them. */ paths referenced by each of them. !!! this should be
ValidPathInfos infos; atomic so that either all paths are registered as valid, or
foreach (DerivationOutputs::iterator, i, drv.outputs) { none are. */
ValidPathInfo info; foreach (DerivationOutputs::iterator, i, drv.outputs)
info.path = i->second.path; worker.store.registerValidPath(i->second.path,
info.hash = contentHashes[i->second.path].first; contentHashes[i->second.path],
info.narSize = contentHashes[i->second.path].second; allReferences[i->second.path],
info.references = allReferences[i->second.path]; drvPath);
info.deriver = drvPath;
infos.push_back(info);
}
worker.store.registerValidPaths(infos);
/* It is now safe to delete the lock files, since all future /* It is now safe to delete the lock files, since all future
lockers will see that the output paths are valid; they will not lockers will see that the output paths are valid; they will not
@@ -2014,10 +1932,32 @@ Path DerivationGoal::openLogFile()
if (fdLogFile == -1) if (fdLogFile == -1)
throw SysError(format("creating log file `%1%'") % logFileName); throw SysError(format("creating log file `%1%'") % logFileName);
/* Create a pipe to get the output of the child. */
logPipe.create();
return logFileName; return logFileName;
} }
void DerivationGoal::initChild()
{
commonChildInit(logPipe);
if (chdir(tmpDir.c_str()) == -1)
throw SysError(format("changing into `%1%'") % tmpDir);
/* When running a hook, dup the communication pipes. */
if (usingBuildHook) {
toHook.writeSide.close();
if (dup2(toHook.readSide, STDIN_FILENO) == -1)
throw SysError("dupping to-hook read side");
}
/* Close all other file descriptors. */
closeMostFDs(set<int>());
}
void DerivationGoal::deleteTmpDir(bool force) void DerivationGoal::deleteTmpDir(bool force)
{ {
if (tmpDir != "") { if (tmpDir != "") {
@@ -2037,22 +1977,19 @@ void DerivationGoal::deleteTmpDir(bool force)
void DerivationGoal::handleChildOutput(int fd, const string & data) void DerivationGoal::handleChildOutput(int fd, const string & data)
{ {
if ((hook && fd == hook->builderOut.readSide) || if (fd == logPipe.readSide) {
(!hook && fd == builderOut.readSide))
{
if (verbosity >= buildVerbosity) if (verbosity >= buildVerbosity)
writeToStderr((unsigned char *) data.c_str(), data.size()); writeToStderr((unsigned char *) data.c_str(), data.size());
writeFull(fdLogFile, (unsigned char *) data.c_str(), data.size()); writeFull(fdLogFile, (unsigned char *) data.c_str(), data.size());
} }
if (hook && fd == hook->fromHook.readSide) else abort();
writeToStderr((unsigned char *) data.c_str(), data.size());
} }
void DerivationGoal::handleEOF(int fd) void DerivationGoal::handleEOF(int fd)
{ {
worker.wakeUp(shared_from_this()); if (fd == logPipe.readSide) worker.wakeUp(shared_from_this());
} }
@@ -2337,7 +2274,7 @@ void SubstitutionGoal::tryToRun()
pid.setKillSignal(SIGTERM); pid.setKillSignal(SIGTERM);
logPipe.writeSide.close(); logPipe.writeSide.close();
worker.childStarted(shared_from_this(), worker.childStarted(shared_from_this(),
pid, singleton<set<int> >(logPipe.readSide), true, true); pid, singleton<set<int> >(logPipe.readSide), true);
state = &SubstitutionGoal::finished; state = &SubstitutionGoal::finished;
@@ -2396,15 +2333,10 @@ void SubstitutionGoal::finished()
canonicalisePathMetaData(storePath); canonicalisePathMetaData(storePath);
HashResult hash = hashPath(htSHA256, storePath); Hash contentHash = hashPath(htSHA256, storePath);
ValidPathInfo info2; worker.store.registerValidPath(storePath, contentHash,
info2.path = storePath; info.references, info.deriver);
info2.hash = hash.first;
info2.narSize = hash.second;
info2.references = info.references;
info2.deriver = info.deriver;
worker.store.registerValidPath(info2);
outputLock->setDeletion(true); outputLock->setDeletion(true);
@@ -2451,7 +2383,6 @@ Worker::Worker(LocalStore & store)
nrLocalBuilds = 0; nrLocalBuilds = 0;
lastWokenUp = 0; lastWokenUp = 0;
cacheFailure = queryBoolSetting("build-cache-failure", false); cacheFailure = queryBoolSetting("build-cache-failure", false);
permanentFailure = false;
} }
@@ -2543,15 +2474,13 @@ unsigned Worker::getNrLocalBuilds()
void Worker::childStarted(GoalPtr goal, void Worker::childStarted(GoalPtr goal,
pid_t pid, const set<int> & fds, bool inBuildSlot, pid_t pid, const set<int> & fds, bool inBuildSlot)
bool monitorForSilence)
{ {
Child child; Child child;
child.goal = goal; child.goal = goal;
child.fds = fds; child.fds = fds;
child.lastOutput = time(0); child.lastOutput = time(0);
child.inBuildSlot = inBuildSlot; child.inBuildSlot = inBuildSlot;
child.monitorForSilence = monitorForSilence;
children[pid] = child; children[pid] = child;
if (inBuildSlot) nrLocalBuilds++; if (inBuildSlot) nrLocalBuilds++;
} }
@@ -2672,16 +2601,12 @@ void Worker::waitForInput()
if (maxSilentTime != 0) { if (maxSilentTime != 0) {
time_t oldest = 0; time_t oldest = 0;
foreach (Children::iterator, i, children) { foreach (Children::iterator, i, children) {
if (i->second.monitorForSilence) { oldest = oldest == 0 || i->second.lastOutput < oldest
oldest = oldest == 0 || i->second.lastOutput < oldest ? i->second.lastOutput : oldest;
? i->second.lastOutput : oldest;
}
}
if (oldest) {
useTimeout = true;
timeout.tv_sec = std::max((time_t) 0, oldest + maxSilentTime - before);
printMsg(lvlVomit, format("sleeping %1% seconds") % timeout.tv_sec);
} }
useTimeout = true;
timeout.tv_sec = std::max((time_t) 0, oldest + maxSilentTime - before);
printMsg(lvlVomit, format("sleeping %1% seconds") % timeout.tv_sec);
} }
/* If we are polling goals that are waiting for a lock, then wake /* If we are polling goals that are waiting for a lock, then wake
@@ -2696,7 +2621,6 @@ void Worker::waitForInput()
timeout.tv_sec = std::max((time_t) 0, lastWokenUp + wakeUpInterval - before); timeout.tv_sec = std::max((time_t) 0, lastWokenUp + wakeUpInterval - before);
} else lastWokenUp = 0; } else lastWokenUp = 0;
using namespace std;
/* Use select() to wait for the input side of any logger pipe to /* Use select() to wait for the input side of any logger pipe to
become `available'. Note that `available' (i.e., non-blocking) become `available'. Note that `available' (i.e., non-blocking)
includes EOF. */ includes EOF. */
@@ -2757,7 +2681,6 @@ void Worker::waitForInput()
} }
if (maxSilentTime != 0 && if (maxSilentTime != 0 &&
j->second.monitorForSilence &&
after - j->second.lastOutput >= (time_t) maxSilentTime) after - j->second.lastOutput >= (time_t) maxSilentTime)
{ {
printMsg(lvlError, printMsg(lvlError,
@@ -2778,11 +2701,6 @@ void Worker::waitForInput()
} }
unsigned int Worker::exitStatus()
{
return permanentFailure ? 100 : 1;
}
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
@@ -2809,7 +2727,7 @@ void LocalStore::buildDerivations(const PathSet & drvPaths)
} }
if (!failed.empty()) if (!failed.empty())
throw Error(format("build of %1% failed") % showPaths(failed), worker.exitStatus()); throw Error(format("build of %1% failed") % showPaths(failed));
} }
@@ -2825,7 +2743,7 @@ void LocalStore::ensurePath(const Path & path)
worker.run(goals); worker.run(goals);
if (goal->getExitCode() != Goal::ecSuccess) if (goal->getExitCode() != Goal::ecSuccess)
throw Error(format("path `%1%' does not exist and cannot be created") % path, worker.exitStatus()); throw Error(format("path `%1%' does not exist and cannot be created") % path);
} }
+10
View File
@@ -0,0 +1,10 @@
init initDerivationsHelpers
Derive | ATermList ATermList ATermList string string ATermList ATermList | ATerm |
| string string | ATerm | EnvBinding |
| string ATermList | ATerm | DerivationInput |
| string string string string | ATerm | DerivationOutput |
Closure | ATermList ATermList | ATerm | OldClosure |
| string ATermList | ATerm | OldClosureElem |
+110 -114
View File
@@ -1,12 +1,22 @@
#include "derivations.hh" #include "derivations.hh"
#include "store-api.hh" #include "store-api.hh"
#include "aterm.hh"
#include "globals.hh" #include "globals.hh"
#include "util.hh" #include "util.hh"
#include "derivations-ast.hh"
#include "derivations-ast.cc"
namespace nix { namespace nix {
Hash hashTerm(ATerm t)
{
return hashString(htSHA256, atPrint(t));
}
Path writeDerivation(const Derivation & drv, const string & name) Path writeDerivation(const Derivation & drv, const string & name)
{ {
PathSet references; PathSet references;
@@ -17,151 +27,137 @@ Path writeDerivation(const Derivation & drv, const string & name)
(that can be missing (of course) and should not necessarily be (that can be missing (of course) and should not necessarily be
held during a garbage collection). */ held during a garbage collection). */
string suffix = name + drvExtension; string suffix = name + drvExtension;
string contents = unparseDerivation(drv); string contents = atPrint(unparseDerivation(drv));
return readOnlyMode return readOnlyMode
? computeStorePathForText(suffix, contents, references) ? computeStorePathForText(suffix, contents, references)
: store->addTextToStore(suffix, contents, references); : store->addTextToStore(suffix, contents, references);
} }
static Path parsePath(std::istream & str) static void checkPath(const string & s)
{ {
string s = parseString(str);
if (s.size() == 0 || s[0] != '/') if (s.size() == 0 || s[0] != '/')
throw Error(format("bad path `%1%' in derivation") % s); throw Error(format("bad path `%1%' in derivation") % s);
return s;
} }
static StringSet parseStrings(std::istream & str, bool arePaths) static void parseStrings(ATermList paths, StringSet & out, bool arePaths)
{ {
StringSet res; for (ATermIterator i(paths); i; ++i) {
while (!endOfList(str)) if (ATgetType(*i) != AT_APPL)
res.insert(arePaths ? parsePath(str) : parseString(str)); throw badTerm("not a path", *i);
return res; string s = aterm2String(*i);
if (arePaths) checkPath(s);
out.insert(s);
}
} }
Derivation parseDerivation(const string & s)
/* Shut up warnings. */
void throwBadDrv(ATerm t) __attribute__ ((noreturn));
void throwBadDrv(ATerm t)
{
throw badTerm("not a valid derivation", t);
}
Derivation parseDerivation(ATerm t)
{ {
Derivation drv; Derivation drv;
std::istringstream str(s); ATermList outs, inDrvs, inSrcs, args, bnds;
expect(str, "Derive(["); ATerm builder, platform;
/* Parse the list of outputs. */ if (!matchDerive(t, outs, inDrvs, inSrcs, platform, builder, args, bnds))
while (!endOfList(str)) { throwBadDrv(t);
for (ATermIterator i(outs); i; ++i) {
ATerm id, path, hashAlgo, hash;
if (!matchDerivationOutput(*i, id, path, hashAlgo, hash))
throwBadDrv(t);
DerivationOutput out; DerivationOutput out;
expect(str, "("); string id = parseString(str); out.path = aterm2String(path);
expect(str, ","); out.path = parsePath(str); checkPath(out.path);
expect(str, ","); out.hashAlgo = parseString(str); out.hashAlgo = aterm2String(hashAlgo);
expect(str, ","); out.hash = parseString(str); out.hash = aterm2String(hash);
expect(str, ")"); drv.outputs[aterm2String(id)] = out;
drv.outputs[id] = out;
} }
/* Parse the list of input derivations. */ for (ATermIterator i(inDrvs); i; ++i) {
expect(str, ",["); ATerm drvPath;
while (!endOfList(str)) { ATermList ids;
expect(str, "("); if (!matchDerivationInput(*i, drvPath, ids))
Path drvPath = parsePath(str); throwBadDrv(t);
expect(str, ",["); Path drvPath2 = aterm2String(drvPath);
drv.inputDrvs[drvPath] = parseStrings(str, false); checkPath(drvPath2);
expect(str, ")"); StringSet ids2;
} parseStrings(ids, ids2, false);
drv.inputDrvs[drvPath2] = ids2;
expect(str, ",["); drv.inputSrcs = parseStrings(str, true);
expect(str, ","); drv.platform = parseString(str);
expect(str, ","); drv.builder = parseString(str);
/* Parse the builder arguments. */
expect(str, ",[");
while (!endOfList(str))
drv.args.push_back(parseString(str));
/* Parse the environment variables. */
expect(str, ",[");
while (!endOfList(str)) {
expect(str, "("); string name = parseString(str);
expect(str, ","); string value = parseString(str);
expect(str, ")");
drv.env[name] = value;
} }
expect(str, ")"); parseStrings(inSrcs, drv.inputSrcs, true);
drv.builder = aterm2String(builder);
drv.platform = aterm2String(platform);
for (ATermIterator i(args); i; ++i) {
if (ATgetType(*i) != AT_APPL)
throw badTerm("string expected", *i);
drv.args.push_back(aterm2String(*i));
}
for (ATermIterator i(bnds); i; ++i) {
ATerm s1, s2;
if (!matchEnvBinding(*i, s1, s2))
throw badTerm("tuple of strings expected", *i);
drv.env[aterm2String(s1)] = aterm2String(s2);
}
return drv; return drv;
} }
static void printString(string & res, const string & s) ATerm unparseDerivation(const Derivation & drv)
{ {
res += '"'; ATermList outputs = ATempty;
for (const char * i = s.c_str(); *i; i++) for (DerivationOutputs::const_reverse_iterator i = drv.outputs.rbegin();
if (*i == '\"' || *i == '\\') { res += "\\"; res += *i; } i != drv.outputs.rend(); ++i)
else if (*i == '\n') res += "\\n"; outputs = ATinsert(outputs,
else if (*i == '\r') res += "\\r"; makeDerivationOutput(
else if (*i == '\t') res += "\\t"; toATerm(i->first),
else res += *i; toATerm(i->second.path),
res += '"'; toATerm(i->second.hashAlgo),
} toATerm(i->second.hash)));
ATermList inDrvs = ATempty;
template<class ForwardIterator> for (DerivationInputs::const_reverse_iterator i = drv.inputDrvs.rbegin();
static void printStrings(string & res, ForwardIterator i, ForwardIterator j) i != drv.inputDrvs.rend(); ++i)
{ inDrvs = ATinsert(inDrvs,
res += '['; makeDerivationInput(
bool first = true; toATerm(i->first),
for ( ; i != j; ++i) { toATermList(i->second)));
if (first) first = false; else res += ',';
printString(res, *i);
}
res += ']';
}
string unparseDerivation(const Derivation & drv)
{
string s;
s.reserve(65536);
s += "Derive([";
bool first = true;
foreach (DerivationOutputs::const_iterator, i, drv.outputs) {
if (first) first = false; else s += ',';
s += '('; printString(s, i->first);
s += ','; printString(s, i->second.path);
s += ','; printString(s, i->second.hashAlgo);
s += ','; printString(s, i->second.hash);
s += ')';
}
s += "],[";
first = true;
foreach (DerivationInputs::const_iterator, i, drv.inputDrvs) {
if (first) first = false; else s += ',';
s += '('; printString(s, i->first);
s += ','; printStrings(s, i->second.begin(), i->second.end());
s += ')';
}
s += "],";
printStrings(s, drv.inputSrcs.begin(), drv.inputSrcs.end());
s += ','; printString(s, drv.platform); ATermList args = ATempty;
s += ','; printString(s, drv.builder); for (Strings::const_reverse_iterator i = drv.args.rbegin();
s += ','; printStrings(s, drv.args.begin(), drv.args.end()); i != drv.args.rend(); ++i)
args = ATinsert(args, toATerm(*i));
s += ",["; ATermList env = ATempty;
first = true; for (StringPairs::const_reverse_iterator i = drv.env.rbegin();
foreach (StringPairs::const_iterator, i, drv.env) { i != drv.env.rend(); ++i)
if (first) first = false; else s += ','; env = ATinsert(env,
s += '('; printString(s, i->first); makeEnvBinding(
s += ','; printString(s, i->second); toATerm(i->first),
s += ')'; toATerm(i->second)));
}
return makeDerive(
s += "])"; outputs,
inDrvs,
return s; toATermList(drv.inputSrcs),
toATerm(drv.platform),
toATerm(drv.builder),
args,
env);
} }
+10 -5
View File
@@ -1,9 +1,11 @@
#ifndef __DERIVATIONS_H #ifndef __DERIVATIONS_H
#define __DERIVATIONS_H #define __DERIVATIONS_H
#include <map> #include <aterm1.h>
#include "types.hh" #include "hash.hh"
#include <map>
namespace nix { namespace nix {
@@ -51,14 +53,17 @@ struct Derivation
}; };
/* Hash an aterm. */
Hash hashTerm(ATerm t);
/* Write a derivation to the Nix store, and return its path. */ /* Write a derivation to the Nix store, and return its path. */
Path writeDerivation(const Derivation & drv, const string & name); Path writeDerivation(const Derivation & drv, const string & name);
/* Parse a derivation. */ /* Parse a derivation. */
Derivation parseDerivation(const string & s); Derivation parseDerivation(ATerm t);
/* Print a derivation. */ /* Parse a derivation. */
string unparseDerivation(const Derivation & drv); ATerm unparseDerivation(const Derivation & drv);
/* Check whether a file name ends with the extensions for /* Check whether a file name ends with the extensions for
derivations. */ derivations. */
+102 -24
View File
@@ -1,5 +1,6 @@
#include "globals.hh" #include "globals.hh"
#include "misc.hh" #include "misc.hh"
#include "pathlocks.hh"
#include "local-store.hh" #include "local-store.hh"
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
@@ -14,6 +15,11 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#ifdef __CYGWIN__
#include <windows.h>
#include <sys/cygwin.h>
#endif
namespace nix { namespace nix {
@@ -30,7 +36,7 @@ static const int defaultGcLevel = 1000;
read. To be precise: when they try to create a new temporary root read. To be precise: when they try to create a new temporary root
file, they will block until the garbage collector has finished / file, they will block until the garbage collector has finished /
yielded the GC lock. */ yielded the GC lock. */
int LocalStore::openGCLock(LockType lockType) static int openGCLock(LockType lockType)
{ {
Path fnGCLock = (format("%1%/%2%") Path fnGCLock = (format("%1%/%2%")
% nixStateDir % gcLockName).str(); % nixStateDir % gcLockName).str();
@@ -126,7 +132,7 @@ Path addPermRoot(const Path & _storePath, const Path & _gcRoot,
Instead of reading all the roots, it would be more efficient to Instead of reading all the roots, it would be more efficient to
check if the root is in a directory in or linked from the check if the root is in a directory in or linked from the
gcroots directory. */ gcroots directory. */
if (queryBoolSetting("gc-check-reachability", false)) { if (queryBoolSetting("gc-check-reachability", true)) {
Roots roots = store->findRoots(); Roots roots = store->findRoots();
if (roots.find(gcRoot) == roots.end()) if (roots.find(gcRoot) == roots.end())
printMsg(lvlError, printMsg(lvlError,
@@ -135,7 +141,7 @@ Path addPermRoot(const Path & _storePath, const Path & _gcRoot,
"therefore, `%2%' might be removed by the garbage collector") "therefore, `%2%' might be removed by the garbage collector")
% gcRoot % storePath); % gcRoot % storePath);
} }
/* Grab the global GC root, causing us to block while a GC is in /* Grab the global GC root, causing us to block while a GC is in
progress. This prevents the set of permanent roots from progress. This prevents the set of permanent roots from
increasing while a GC is in progress. */ increasing while a GC is in progress. */
@@ -173,6 +179,15 @@ void LocalStore::addTempRoot(const Path & path)
fdGCLock.close(); fdGCLock.close();
/* Note that on Cygwin a lot of the following complexity
is unnecessary, since we cannot delete open lock
files. If we have the lock file open, then it's valid;
if we can delete it, then it wasn't in use any more.
Also note that on Cygwin we cannot "upgrade" a lock
from a read lock to a write lock. */
#ifndef __CYGWIN__
debug(format("acquiring read lock on `%1%'") % fnTempRoots); debug(format("acquiring read lock on `%1%'") % fnTempRoots);
lockFile(fdTempRoots, ltRead, true); lockFile(fdTempRoots, ltRead, true);
@@ -186,6 +201,10 @@ void LocalStore::addTempRoot(const Path & path)
/* The garbage collector deleted this file before we could /* The garbage collector deleted this file before we could
get a lock. (It won't delete the file after we get a get a lock. (It won't delete the file after we get a
lock.) Try again. */ lock.) Try again. */
#else
break;
#endif
} }
} }
@@ -198,9 +217,14 @@ void LocalStore::addTempRoot(const Path & path)
string s = path + '\0'; string s = path + '\0';
writeFull(fdTempRoots, (const unsigned char *) s.c_str(), s.size()); writeFull(fdTempRoots, (const unsigned char *) s.c_str(), s.size());
#ifndef __CYGWIN__
/* Downgrade to a read lock. */ /* Downgrade to a read lock. */
debug(format("downgrading to read lock on `%1%'") % fnTempRoots); debug(format("downgrading to read lock on `%1%'") % fnTempRoots);
lockFile(fdTempRoots, ltRead, true); lockFile(fdTempRoots, ltRead, true);
#else
debug(format("releasing write lock on `%1%'") % fnTempRoots);
lockFile(fdTempRoots, ltNone, true);
#endif
} }
@@ -228,6 +252,19 @@ static void readTempRoots(PathSet & tempRoots, FDs & fds)
Path path = (format("%1%/%2%/%3%") % nixStateDir % tempRootsDir % *i).str(); Path path = (format("%1%/%2%/%3%") % nixStateDir % tempRootsDir % *i).str();
debug(format("reading temporary root file `%1%'") % path); debug(format("reading temporary root file `%1%'") % path);
#ifdef __CYGWIN__
/* On Cygwin we just try to delete the lock file. */
char win32Path[MAX_PATH];
cygwin_conv_to_full_win32_path(path.c_str(), win32Path);
if (DeleteFile(win32Path)) {
printMsg(lvlError, format("removed stale temporary roots file `%1%'")
% path);
continue;
} else
debug(format("delete of `%1%' failed: %2%") % path % GetLastError());
#endif
FDPtr fd(new AutoCloseFD(open(path.c_str(), O_RDWR, 0666))); FDPtr fd(new AutoCloseFD(open(path.c_str(), O_RDWR, 0666)));
if (*fd == -1) { if (*fd == -1) {
/* It's okay if the file has disappeared. */ /* It's okay if the file has disappeared. */
@@ -239,6 +276,7 @@ static void readTempRoots(PathSet & tempRoots, FDs & fds)
//FDPtr fd(new AutoCloseFD(openLockFile(path, false))); //FDPtr fd(new AutoCloseFD(openLockFile(path, false)));
//if (*fd == -1) continue; //if (*fd == -1) continue;
#ifndef __CYGWIN__
/* Try to acquire a write lock without blocking. This can /* Try to acquire a write lock without blocking. This can
only succeed if the owning process has died. In that case only succeed if the owning process has died. In that case
we don't care about its temporary roots. */ we don't care about its temporary roots. */
@@ -249,6 +287,7 @@ static void readTempRoots(PathSet & tempRoots, FDs & fds)
writeFull(*fd, (const unsigned char *) "d", 1); writeFull(*fd, (const unsigned char *) "d", 1);
continue; continue;
} }
#endif
/* Acquire a read lock. This will prevent the owning process /* Acquire a read lock. This will prevent the owning process
from upgrading to a write lock, therefore it will block in from upgrading to a write lock, therefore it will block in
@@ -415,13 +454,18 @@ struct LocalStore::GCState
PathSet busy; PathSet busy;
bool gcKeepOutputs; bool gcKeepOutputs;
bool gcKeepDerivations; bool gcKeepDerivations;
GCState(GCResults & results_) : results(results_)
bool drvsIndexed;
typedef std::multimap<string, Path> DrvsByName;
DrvsByName drvsByName; // derivation paths hashed by name attribute
GCState(GCResults & results_) : results(results_), drvsIndexed(false)
{ {
} }
}; };
static bool shouldDelete(GCOptions::GCAction action) static bool doDelete(GCOptions::GCAction action)
{ {
return action == GCOptions::gcDeleteDead return action == GCOptions::gcDeleteDead
|| action == GCOptions::gcDeleteSpecific; || action == GCOptions::gcDeleteSpecific;
@@ -435,11 +479,45 @@ bool LocalStore::isActiveTempFile(const GCState & state,
&& state.tempRoots.find(string(path, 0, path.size() - suffix.size())) != state.tempRoots.end(); && state.tempRoots.find(string(path, 0, path.size() - suffix.size())) != state.tempRoots.end();
} }
/* Return all the derivations in the Nix store that have `path' as an
output. This function assumes that derivations have the same name
as their outputs. */
PathSet LocalStore::findDerivers(GCState & state, const Path & path)
{
PathSet derivers;
Path deriver = queryDeriver(path);
if (deriver != "") derivers.insert(deriver);
if (!state.drvsIndexed) {
Paths entries = readDirectory(nixStore);
foreach (Paths::iterator, i, entries)
if (isDerivation(*i))
state.drvsByName.insert(std::pair<string, Path>(
getNameOfStorePath(*i), nixStore + "/" + *i));
state.drvsIndexed = true;
}
string name = getNameOfStorePath(path);
// Urgh, I should have used Haskell...
std::pair<GCState::DrvsByName::iterator, GCState::DrvsByName::iterator> range =
state.drvsByName.equal_range(name);
for (GCState::DrvsByName::iterator i = range.first; i != range.second; ++i)
if (isValidPath(i->second)) {
Derivation drv = derivationFromPath(i->second);
foreach (DerivationOutputs::iterator, j, drv.outputs)
if (j->second.path == path) derivers.insert(i->second);
}
return derivers;
}
bool LocalStore::tryToDelete(GCState & state, const Path & path) bool LocalStore::tryToDelete(GCState & state, const Path & path)
{ {
checkInterrupt();
if (!pathExists(path)) return true; if (!pathExists(path)) return true;
if (state.deleted.find(path) != state.deleted.end()) return true; if (state.deleted.find(path) != state.deleted.end()) return true;
if (state.live.find(path) != state.live.end()) return false; if (state.live.find(path) != state.live.end()) return false;
@@ -468,10 +546,10 @@ bool LocalStore::tryToDelete(GCState & state, const Path & path)
then don't delete the derivation if any of the outputs are then don't delete the derivation if any of the outputs are
live. */ live. */
if (state.gcKeepDerivations && isDerivation(path)) { if (state.gcKeepDerivations && isDerivation(path)) {
PathSet outputs = queryDerivationOutputs(path); Derivation drv = derivationFromPath(path);
foreach (PathSet::iterator, i, outputs) foreach (DerivationOutputs::iterator, i, drv.outputs)
if (!tryToDelete(state, *i)) { if (!tryToDelete(state, i->second.path)) {
printMsg(lvlDebug, format("cannot delete derivation `%1%' because its output `%2%' is alive") % path % *i); printMsg(lvlDebug, format("cannot delete derivation `%1%' because its output is alive") % path);
goto isLive; goto isLive;
} }
} }
@@ -482,9 +560,18 @@ bool LocalStore::tryToDelete(GCState & state, const Path & path)
if (!pathExists(path)) return true; if (!pathExists(path)) return true;
/* If gc-keep-outputs is set, then don't delete this path if /* If gc-keep-outputs is set, then don't delete this path if
there are derivers of this path that are not garbage. */ its deriver is not garbage. !!! Nix does not reliably
store derivers, so we have to look at all derivations to
determine which of them derive `path'. Since this makes
the garbage collector very slow to start on large Nix
stores, here we just look for all derivations that have the
same name as `path' (where the name is the part of the
filename after the hash, i.e. the `name' attribute of the
derivation). This is somewhat hacky: currently, the
deriver of a path always has the same name as the output,
but this might change in the future. */
if (state.gcKeepOutputs) { if (state.gcKeepOutputs) {
PathSet derivers = queryValidDerivers(path); PathSet derivers = findDerivers(state, path);
foreach (PathSet::iterator, deriver, derivers) { foreach (PathSet::iterator, deriver, derivers) {
/* Break an infinite recursion if gc-keep-derivations /* Break an infinite recursion if gc-keep-derivations
and gc-keep-outputs are both set by tentatively and gc-keep-outputs are both set by tentatively
@@ -518,7 +605,7 @@ bool LocalStore::tryToDelete(GCState & state, const Path & path)
} }
/* The path is garbage, so delete it. */ /* The path is garbage, so delete it. */
if (shouldDelete(state.options.action)) { if (doDelete(state.options.action)) {
printMsg(lvlInfo, format("deleting `%1%'") % path); printMsg(lvlInfo, format("deleting `%1%'") % path);
unsigned long long bytesFreed, blocksFreed; unsigned long long bytesFreed, blocksFreed;
@@ -564,15 +651,6 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
state.gcKeepOutputs = queryBoolSetting("gc-keep-outputs", false); state.gcKeepOutputs = queryBoolSetting("gc-keep-outputs", false);
state.gcKeepDerivations = queryBoolSetting("gc-keep-derivations", true); state.gcKeepDerivations = queryBoolSetting("gc-keep-derivations", true);
/* Using `--ignore-liveness' with `--delete' can have unintended
consequences if `gc-keep-outputs' or `gc-keep-derivations' are
true (the garbage collector will recurse into deleting the
outputs or derivers, respectively). So disable them. */
if (options.action == GCOptions::gcDeleteSpecific && options.ignoreLiveness) {
state.gcKeepOutputs = false;
state.gcKeepDerivations = false;
}
/* Acquire the global GC root. This prevents /* Acquire the global GC root. This prevents
a) New roots from being added. a) New roots from being added.
@@ -627,7 +705,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
vector<Path> entries_(entries.begin(), entries.end()); vector<Path> entries_(entries.begin(), entries.end());
random_shuffle(entries_.begin(), entries_.end()); random_shuffle(entries_.begin(), entries_.end());
if (shouldDelete(state.options.action)) if (doDelete(state.options.action))
printMsg(lvlError, format("deleting garbage...")); printMsg(lvlError, format("deleting garbage..."));
else else
printMsg(lvlError, format("determining live/dead paths...")); printMsg(lvlError, format("determining live/dead paths..."));
+1 -2
View File
@@ -20,9 +20,8 @@ string nixBinDir = "/UNINIT";
bool keepFailed = false; bool keepFailed = false;
bool keepGoing = false; bool keepGoing = false;
bool tryFallback = false; bool tryFallback = false;
Verbosity buildVerbosity = lvlError; Verbosity buildVerbosity = lvlInfo;
unsigned int maxBuildJobs = 1; unsigned int maxBuildJobs = 1;
unsigned int buildCores = 1;
bool readOnlyMode = false; bool readOnlyMode = false;
string thisSystem = "unset"; string thisSystem = "unset";
time_t maxSilentTime = 0; time_t maxSilentTime = 0;
-5
View File
@@ -55,11 +55,6 @@ extern Verbosity buildVerbosity;
/* Maximum number of parallel build jobs. 0 means unlimited. */ /* Maximum number of parallel build jobs. 0 means unlimited. */
extern unsigned int maxBuildJobs; extern unsigned int maxBuildJobs;
/* Number of CPU cores to utilize in parallel within a build, i.e. by passing
this number to Make via '-j'. 0 means that the number of actual CPU cores on
the local host ought to be auto-detected. */
extern unsigned int buildCores;
/* Read-only mode. Don't copy stuff to the store, don't change the /* Read-only mode. Don't copy stuff to the store, don't change the
database. */ database. */
extern bool readOnlyMode; extern bool readOnlyMode;
File diff suppressed because it is too large. Load diff
+22 -87
View File
@@ -5,11 +5,6 @@
#include "store-api.hh" #include "store-api.hh"
#include "util.hh" #include "util.hh"
#include "pathlocks.hh"
class sqlite3;
class sqlite3_stmt;
namespace nix { namespace nix {
@@ -17,9 +12,8 @@ namespace nix {
/* Nix store and database schema version. Version 1 (or 0) was Nix <= /* Nix store and database schema version. Version 1 (or 0) was Nix <=
0.7. Version 2 was Nix 0.8 and 0.9. Version 3 is Nix 0.10. 0.7. Version 2 was Nix 0.8 and 0.9. Version 3 is Nix 0.10.
Version 4 is Nix 0.11. Version 5 is Nix 0.12-0.16. Version 6 is Version 4 is Nix 0.11. Version 5 is Nix 0.12*/
Nix 1.0. */ const int nixSchemaVersion = 5;
const int nixSchemaVersion = 6;
extern string drvsLogDir; extern string drvsLogDir;
@@ -47,34 +41,6 @@ struct RunningSubstituter
}; };
/* Wrapper object to close the SQLite database automatically. */
struct SQLite
{
sqlite3 * db;
SQLite() { db = 0; }
~SQLite();
operator sqlite3 * () { return db; }
};
/* Wrapper object to create and destroy SQLite prepared statements. */
struct SQLiteStmt
{
sqlite3 * db;
sqlite3_stmt * stmt;
unsigned int curArg;
SQLiteStmt() { stmt = 0; }
void create(sqlite3 * db, const string & s);
void reset();
~SQLiteStmt();
operator sqlite3_stmt * () { return stmt; }
void bind(const string & value);
void bind(int value);
void bind64(long long value);
void bind();
};
class LocalStore : public StoreAPI class LocalStore : public StoreAPI
{ {
private: private:
@@ -98,8 +64,6 @@ public:
PathSet queryValidPaths(); PathSet queryValidPaths();
ValidPathInfo queryPathInfo(const Path & path);
Hash queryPathHash(const Path & path); Hash queryPathHash(const Path & path);
void queryReferences(const Path & path, PathSet & references); void queryReferences(const Path & path, PathSet & references);
@@ -107,14 +71,6 @@ public:
void queryReferrers(const Path & path, PathSet & referrers); void queryReferrers(const Path & path, PathSet & referrers);
Path queryDeriver(const Path & path); Path queryDeriver(const Path & path);
/* Return all currently valid derivations that have `path' as an
output. (Note that the result of `queryDeriver()' is the
derivation that was actually used to produce `path', which may
not exist anymore.) */
PathSet queryValidDerivers(const Path & path);
PathSet queryDerivationOutputs(const Path & path);
PathSet querySubstitutablePaths(); PathSet querySubstitutablePaths();
@@ -176,7 +132,8 @@ public:
execution of the derivation (or something equivalent). Also execution of the derivation (or something equivalent). Also
register the hash of the file system contents of the path. The register the hash of the file system contents of the path. The
hash must be a SHA-256 hash. */ hash must be a SHA-256 hash. */
void registerValidPath(const ValidPathInfo & info); void registerValidPath(const Path & path,
const Hash & hash, const PathSet & references, const Path & deriver);
void registerValidPaths(const ValidPathInfos & infos); void registerValidPaths(const ValidPathInfos & infos);
@@ -187,10 +144,6 @@ public:
/* Query whether `path' previously failed to build. */ /* Query whether `path' previously failed to build. */
bool hasPathFailed(const Path & path); bool hasPathFailed(const Path & path);
PathSet queryFailedPaths();
void clearFailedPaths(const PathSet & paths);
private: private:
Path schemaPath; Path schemaPath;
@@ -198,63 +151,45 @@ private:
/* Lock file used for upgrading. */ /* Lock file used for upgrading. */
AutoCloseFD globalLock; AutoCloseFD globalLock;
/* The SQLite database object. */ /* !!! The cache can grow very big. Maybe it should be pruned
SQLite db; every once in a while. */
std::map<Path, ValidPathInfo> pathInfoCache;
/* Some precompiled SQLite statements. */ /* Store paths for which the referrers file must be purged. */
SQLiteStmt stmtRegisterValidPath; PathSet delayedUpdates;
SQLiteStmt stmtUpdatePathInfo;
SQLiteStmt stmtAddReference; /* Whether to do an fsync() after writing Nix metadata. */
SQLiteStmt stmtQueryPathInfo; bool doFsync;
SQLiteStmt stmtQueryReferences;
SQLiteStmt stmtQueryReferrers;
SQLiteStmt stmtInvalidatePath;
SQLiteStmt stmtRegisterFailedPath;
SQLiteStmt stmtHasPathFailed;
SQLiteStmt stmtQueryFailedPaths;
SQLiteStmt stmtClearFailedPath;
SQLiteStmt stmtAddDerivationOutput;
SQLiteStmt stmtQueryValidDerivers;
SQLiteStmt stmtQueryDerivationOutputs;
int getSchema(); int getSchema();
void openDB(bool create); void registerValidPath(const ValidPathInfo & info, bool ignoreValidity = false);
unsigned long long queryValidPathId(const Path & path); ValidPathInfo queryPathInfo(const Path & path, bool ignoreErrors = false);
unsigned long long addValidPath(const ValidPathInfo & info);
void addReference(unsigned long long referrer, unsigned long long reference);
void appendReferrer(const Path & from, const Path & to, bool lock); void appendReferrer(const Path & from, const Path & to, bool lock);
void rewriteReferrers(const Path & path, bool purge, PathSet referrers); void rewriteReferrers(const Path & path, bool purge, PathSet referrers);
void flushDelayedUpdates();
bool queryReferrersInternal(const Path & path, PathSet & referrers);
void invalidatePath(const Path & path); void invalidatePath(const Path & path);
void verifyPath(const Path & path, const PathSet & store, void upgradeStore12();
PathSet & done, PathSet & validPaths);
void updatePathInfo(const ValidPathInfo & info);
void upgradeStore6();
PathSet queryValidPathsOld();
ValidPathInfo queryPathInfoOld(const Path & path);
struct GCState; struct GCState;
bool tryToDelete(GCState & state, const Path & path); bool tryToDelete(GCState & state, const Path & path);
PathSet findDerivers(GCState & state, const Path & path);
bool isActiveTempFile(const GCState & state, bool isActiveTempFile(const GCState & state,
const Path & path, const string & suffix); const Path & path, const string & suffix);
int openGCLock(LockType lockType);
void startSubstituter(const Path & substituter, void startSubstituter(const Path & substituter,
RunningSubstituter & runningSubstituter); RunningSubstituter & runningSubstituter);
Path createTempDirInStore();
}; };
+11 -8
View File
@@ -2,6 +2,8 @@
#include "store-api.hh" #include "store-api.hh"
#include "local-store.hh" #include "local-store.hh"
#include <aterm2.h>
namespace nix { namespace nix {
@@ -10,7 +12,9 @@ Derivation derivationFromPath(const Path & drvPath)
{ {
assertStorePath(drvPath); assertStorePath(drvPath);
store->ensurePath(drvPath); store->ensurePath(drvPath);
return parseDerivation(readFile(drvPath)); ATerm t = ATreadFromNamedFile(drvPath.c_str());
if (!t) throw Error(format("cannot read aterm from `%1%'") % drvPath);
return parseDerivation(t);
} }
@@ -27,10 +31,10 @@ void computeFSClosure(const Path & storePath,
store->queryReferences(storePath, references); store->queryReferences(storePath, references);
if (includeOutputs && isDerivation(storePath)) { if (includeOutputs && isDerivation(storePath)) {
PathSet outputs = store->queryDerivationOutputs(storePath); Derivation drv = derivationFromPath(storePath);
foreach (PathSet::iterator, i, outputs) foreach (DerivationOutputs::iterator, i, drv.outputs)
if (store->isValidPath(*i)) if (store->isValidPath(i->second.path))
computeFSClosure(*i, paths, flipDirection, true); computeFSClosure(i->second.path, paths, flipDirection, true);
} }
foreach (PathSet::iterator, i, references) foreach (PathSet::iterator, i, references)
@@ -48,9 +52,9 @@ Path findOutput(const Derivation & drv, string id)
void queryMissing(const PathSet & targets, void queryMissing(const PathSet & targets,
PathSet & willBuild, PathSet & willSubstitute, PathSet & unknown, PathSet & willBuild, PathSet & willSubstitute, PathSet & unknown,
unsigned long long & downloadSize, unsigned long long & narSize) unsigned long long & downloadSize)
{ {
downloadSize = narSize = 0; downloadSize = 0;
PathSet todo(targets.begin(), targets.end()), done; PathSet todo(targets.begin(), targets.end()), done;
@@ -88,7 +92,6 @@ void queryMissing(const PathSet & targets,
if (store->querySubstitutablePathInfo(p, info)) { if (store->querySubstitutablePathInfo(p, info)) {
willSubstitute.insert(p); willSubstitute.insert(p);
downloadSize += info.downloadSize; downloadSize += info.downloadSize;
narSize += info.narSize;
todo.insert(info.references.begin(), info.references.end()); todo.insert(info.references.begin(), info.references.end());
} else } else
unknown.insert(p); unknown.insert(p);
+1 -1
View File
@@ -31,7 +31,7 @@ Path findOutput(const Derivation & drv, string id);
will be substituted. */ will be substituted. */
void queryMissing(const PathSet & targets, void queryMissing(const PathSet & targets,
PathSet & willBuild, PathSet & willSubstitute, PathSet & unknown, PathSet & willBuild, PathSet & willSubstitute, PathSet & unknown,
unsigned long long & downloadSize, unsigned long long & narSize); unsigned long long & downloadSize);
} }
+2 -16
View File
@@ -68,7 +68,7 @@ static void hashAndLink(bool dryRun, HashToPath & hashToPath,
the contents of the symlink (i.e. the result of the contents of the symlink (i.e. the result of
readlink()), not the contents of the target (which may not readlink()), not the contents of the target (which may not
even exist). */ even exist). */
Hash hash = hashPath(htSHA256, path).first; Hash hash = hashPath(htSHA256, path);
stats.totalFiles++; stats.totalFiles++;
printMsg(lvlDebug, format("`%1%' has hash `%2%'") % path % printHash(hash)); printMsg(lvlDebug, format("`%1%' has hash `%2%'") % path % printHash(hash));
@@ -119,23 +119,9 @@ static void hashAndLink(bool dryRun, HashToPath & hashToPath,
} }
/* Atomically replace the old file with the new hard link. */ /* Atomically replace the old file with the new hard link. */
if (rename(tempLink.c_str(), path.c_str()) == -1) { if (rename(tempLink.c_str(), path.c_str()) == -1)
if (errno == EMLINK) {
/* Some filesystems generate too many links on the
rename, rather than on the original link.
(Probably it temporarily increases the st_nlink
field before decreasing it again.) */
printMsg(lvlInfo, format("`%1%' has maximum number of links") % prevPath.first);
hashToPath[hash] = std::pair<Path, ino_t>(path, st.st_ino);
/* Unlink the temp link. */
if (unlink(tempLink.c_str()) == -1)
printMsg(lvlError, format("unable to unlink `%1%'") % tempLink);
return;
}
throw SysError(format("cannot rename `%1%' to `%2%'") throw SysError(format("cannot rename `%1%' to `%2%'")
% tempLink % path); % tempLink % path);
}
} else } else
printMsg(lvlTalkative, format("would link `%1%' to `%2%'") % path % prevPath.first); printMsg(lvlTalkative, format("would link `%1%' to `%2%'") % path % prevPath.first);
+58 -3
View File
@@ -8,6 +8,11 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#ifdef __CYGWIN__
#include <windows.h>
#include <sys/cygwin.h>
#endif
namespace nix { namespace nix {
@@ -16,24 +21,72 @@ int openLockFile(const Path & path, bool create)
{ {
AutoCloseFD fd; AutoCloseFD fd;
#ifdef __CYGWIN__
/* On Cygwin we have to open the lock file without "DELETE"
sharing mode; otherwise Windows will allow open lock files to
be deleted (which is almost but not quite what Unix does). */
char win32Path[MAX_PATH + 1];
cygwin_conv_to_full_win32_path(path.c_str(), win32Path);
SECURITY_ATTRIBUTES sa; /* required, otherwise inexplicably bad shit happens */
sa.nLength = sizeof sa;
sa.lpSecurityDescriptor = 0;
sa.bInheritHandle = TRUE;
HANDLE h = CreateFile(win32Path, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, &sa,
(create ? OPEN_ALWAYS : OPEN_EXISTING),
FILE_ATTRIBUTE_NORMAL, 0);
if (h == INVALID_HANDLE_VALUE) {
if (create || GetLastError() != ERROR_FILE_NOT_FOUND)
throw Error(format("opening lock file `%1%'") % path);
fd = -1;
}
else
fd = cygwin_attach_handle_to_fd((char *) path.c_str(), -1, h, 1, O_RDWR);
#else
fd = open(path.c_str(), O_RDWR | (create ? O_CREAT : 0), 0666); fd = open(path.c_str(), O_RDWR | (create ? O_CREAT : 0), 0666);
if (fd == -1 && (create || errno != ENOENT)) if (fd == -1 && (create || errno != ENOENT))
throw SysError(format("opening lock file `%1%'") % path); throw SysError(format("opening lock file `%1%'") % path);
#endif
return fd.borrow(); return fd.borrow();
} }
void deleteLockFile(const Path & path, int fd) void deleteLockFilePreClose(const Path & path, int fd)
{ {
#ifndef __CYGWIN__
/* Get rid of the lock file. Have to be careful not to introduce /* Get rid of the lock file. Have to be careful not to introduce
races. Write a (meaningless) token to the file to indicate to races. */
/* On Unix, write a (meaningless) token to the file to indicate to
other processes waiting on this lock that the lock is stale other processes waiting on this lock that the lock is stale
(deleted). */ (deleted). */
unlink(path.c_str()); unlink(path.c_str());
writeFull(fd, (const unsigned char *) "d", 1); writeFull(fd, (const unsigned char *) "d", 1);
/* Note that the result of unlink() is ignored; removing the lock /* Note that the result of unlink() is ignored; removing the lock
file is an optimisation, not a necessity. */ file is an optimisation, not a necessity. */
#endif
}
void deleteLockFilePostClose(const Path & path)
{
#ifdef __CYGWIN__
/* On Windows, just try to delete the lock file. This will fail
if anybody still has the file open. We cannot use unlink()
here, because Cygwin emulates Unix semantics of allowing an
open file to be deleted (but fakes it - the file isn't actually
deleted until later, so a file with the same name cannot be
created in the meantime). */
char win32Path[MAX_PATH + 1];
cygwin_conv_to_full_win32_path(path.c_str(), win32Path);
if (DeleteFile(win32Path))
debug(format("delete of `%1%' succeeded") % path.c_str());
else
/* Not an error: probably means that the lock is still opened
by someone else. */
debug(format("delete of `%1%' failed: %2%") % path.c_str() % GetLastError());
#endif
} }
@@ -167,13 +220,15 @@ PathLocks::~PathLocks()
void PathLocks::unlock() void PathLocks::unlock()
{ {
foreach (list<FDPair>::iterator, i, fds) { foreach (list<FDPair>::iterator, i, fds) {
if (deletePaths) deleteLockFile(i->second, i->first); if (deletePaths) deleteLockFilePreClose(i->second, i->first);
lockedPaths.erase(i->second); lockedPaths.erase(i->second);
if (close(i->first) == -1) if (close(i->first) == -1)
printMsg(lvlError, printMsg(lvlError,
format("error (ignored): cannot close lock file on `%1%'") % i->second); format("error (ignored): cannot close lock file on `%1%'") % i->second);
if (deletePaths) deleteLockFilePostClose(i->second);
debug(format("lock released on `%1%'") % i->second); debug(format("lock released on `%1%'") % i->second);
} }
+4 -2
View File
@@ -12,8 +12,10 @@ namespace nix {
because it doesn't exist. Any other error throws an exception. */ because it doesn't exist. Any other error throws an exception. */
int openLockFile(const Path & path, bool create); int openLockFile(const Path & path, bool create);
/* Delete an open lock file. */ /* Delete an open lock file. Both must be called to be fully portable
void deleteLockFile(const Path & path, int fd); between Unix and Windows. */
void deleteLockFilePreClose(const Path & path, int fd);
void deleteLockFilePostClose(const Path & path);
enum LockType { ltRead, ltWrite, ltNone }; enum LockType { ltRead, ltWrite, ltNone };
+1 -1
View File
@@ -81,7 +81,7 @@ void RefScanSink::operator () (const unsigned char * data, unsigned int len)
PathSet scanForReferences(const string & path, PathSet scanForReferences(const string & path,
const PathSet & refs, HashResult & hash) const PathSet & refs, Hash & hash)
{ {
RefScanSink sink; RefScanSink sink;
std::map<string, Path> backMap; std::map<string, Path> backMap;
+1 -1
View File
@@ -7,7 +7,7 @@
namespace nix { namespace nix {
PathSet scanForReferences(const Path & path, const PathSet & refs, PathSet scanForReferences(const Path & path, const PathSet & refs,
HashResult & hash); Hash & hash);
} }
+11 -61
View File
@@ -13,7 +13,6 @@
#include <iostream> #include <iostream>
#include <unistd.h> #include <unistd.h>
#include <cstring>
namespace nix { namespace nix {
@@ -97,6 +96,10 @@ void RemoteStore::forkSlave()
if (worker == "") if (worker == "")
worker = nixBinDir + "/nix-worker"; worker = nixBinDir + "/nix-worker";
string verbosityArg = "-";
for (int i = 1; i < verbosity; ++i)
verbosityArg += "v";
child = fork(); child = fork();
switch (child) { switch (child) {
@@ -116,7 +119,10 @@ void RemoteStore::forkSlave()
close(fdSocket); close(fdSocket);
close(fdChild); close(fdChild);
execlp(worker.c_str(), worker.c_str(), "--slave", NULL); execlp(worker.c_str(), worker.c_str(), "--slave",
/* hacky - must be at the end */
verbosityArg == "-" ? NULL : verbosityArg.c_str(),
NULL);
throw SysError(format("executing `%1%'") % worker); throw SysError(format("executing `%1%'") % worker);
@@ -152,7 +158,6 @@ void RemoteStore::connectToDaemon()
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
if (socketPathRel.size() >= sizeof(addr.sun_path)) if (socketPathRel.size() >= sizeof(addr.sun_path))
throw Error(format("socket path `%1%' is too long") % socketPathRel); throw Error(format("socket path `%1%' is too long") % socketPathRel);
using namespace std;
strcpy(addr.sun_path, socketPathRel.c_str()); strcpy(addr.sun_path, socketPathRel.c_str());
if (connect(fdSocket, (struct sockaddr *) &addr, sizeof(addr)) == -1) if (connect(fdSocket, (struct sockaddr *) &addr, sizeof(addr)) == -1)
@@ -191,8 +196,6 @@ void RemoteStore::setOptions()
writeInt(logType, to); writeInt(logType, to);
writeInt(printBuildTrace, to); writeInt(printBuildTrace, to);
} }
if (GET_PROTOCOL_MINOR(daemonVersion) >= 6)
writeInt(buildCores, to);
processStderr(); processStderr();
} }
@@ -211,9 +214,7 @@ bool RemoteStore::isValidPath(const Path & path)
PathSet RemoteStore::queryValidPaths() PathSet RemoteStore::queryValidPaths()
{ {
openConnection(); openConnection();
writeInt(wopQueryValidPaths, to); throw Error("not implemented");
processStderr();
return readStorePaths(from);
} }
@@ -242,29 +243,10 @@ bool RemoteStore::querySubstitutablePathInfo(const Path & path,
if (info.deriver != "") assertStorePath(info.deriver); if (info.deriver != "") assertStorePath(info.deriver);
info.references = readStorePaths(from); info.references = readStorePaths(from);
info.downloadSize = readLongLong(from); info.downloadSize = readLongLong(from);
info.narSize = GET_PROTOCOL_MINOR(daemonVersion) >= 7 ? readLongLong(from) : 0;
return true; return true;
} }
ValidPathInfo RemoteStore::queryPathInfo(const Path & path)
{
openConnection();
writeInt(wopQueryPathInfo, to);
writeString(path, to);
processStderr();
ValidPathInfo info;
info.path = path;
info.deriver = readString(from);
if (info.deriver != "") assertStorePath(info.deriver);
info.hash = parseHash(htSHA256, readString(from));
info.references = readStorePaths(from);
info.registrationTime = readInt(from);
info.narSize = readLongLong(from);
return info;
}
Hash RemoteStore::queryPathHash(const Path & path) Hash RemoteStore::queryPathHash(const Path & path)
{ {
openConnection(); openConnection();
@@ -312,16 +294,6 @@ Path RemoteStore::queryDeriver(const Path & path)
} }
PathSet RemoteStore::queryDerivationOutputs(const Path & path)
{
openConnection();
writeInt(wopQueryDerivationOutputs, to);
writeString(path, to);
processStderr();
return readStorePaths(from);
}
Path RemoteStore::addToStore(const Path & _srcPath, Path RemoteStore::addToStore(const Path & _srcPath,
bool recursive, HashType hashAlgo, PathFilter & filter) bool recursive, HashType hashAlgo, PathFilter & filter)
{ {
@@ -467,25 +439,6 @@ void RemoteStore::collectGarbage(const GCOptions & options, GCResults & results)
} }
PathSet RemoteStore::queryFailedPaths()
{
openConnection();
writeInt(wopQueryFailedPaths, to);
processStderr();
return readStorePaths(from);
}
void RemoteStore::clearFailedPaths(const PathSet & paths)
{
openConnection();
writeInt(wopClearFailedPaths, to);
writeStringSet(paths, to);
processStderr();
readInt(from);
}
void RemoteStore::processStderr(Sink * sink, Source * source) void RemoteStore::processStderr(Sink * sink, Source * source)
{ {
unsigned int msg; unsigned int msg;
@@ -509,11 +462,8 @@ void RemoteStore::processStderr(Sink * sink, Source * source)
writeToStderr((const unsigned char *) s.c_str(), s.size()); writeToStderr((const unsigned char *) s.c_str(), s.size());
} }
} }
if (msg == STDERR_ERROR) { if (msg == STDERR_ERROR)
string error = readString(from); throw Error(readString(from));
unsigned int status = GET_PROTOCOL_MINOR(daemonVersion) >= 8 ? readInt(from) : 1;
throw Error(error, status);
}
else if (msg != STDERR_LAST) else if (msg != STDERR_LAST)
throw Error("protocol error processing standard error"); throw Error("protocol error processing standard error");
} }
-8
View File
@@ -29,8 +29,6 @@ public:
PathSet queryValidPaths(); PathSet queryValidPaths();
ValidPathInfo queryPathInfo(const Path & path);
Hash queryPathHash(const Path & path); Hash queryPathHash(const Path & path);
void queryReferences(const Path & path, PathSet & references); void queryReferences(const Path & path, PathSet & references);
@@ -39,8 +37,6 @@ public:
Path queryDeriver(const Path & path); Path queryDeriver(const Path & path);
PathSet queryDerivationOutputs(const Path & path);
bool hasSubstitutes(const Path & path); bool hasSubstitutes(const Path & path);
bool querySubstitutablePathInfo(const Path & path, bool querySubstitutablePathInfo(const Path & path,
@@ -72,10 +68,6 @@ public:
void collectGarbage(const GCOptions & options, GCResults & results); void collectGarbage(const GCOptions & options, GCResults & results);
PathSet queryFailedPaths();
void clearFailedPaths(const PathSet & paths);
private: private:
AutoCloseFD fdSocket; AutoCloseFD fdSocket;
FdSink to; FdSink to;
-44
View File
@@ -1,44 +0,0 @@
create table if not exists ValidPaths (
id integer primary key autoincrement not null,
path text unique not null,
hash text not null,
registrationTime integer not null,
deriver text,
narSize integer
);
create table if not exists Refs (
referrer integer not null,
reference integer not null,
primary key (referrer, reference),
foreign key (referrer) references ValidPaths(id) on delete cascade,
foreign key (reference) references ValidPaths(id) on delete restrict
);
create index if not exists IndexReferrer on Refs(referrer);
create index if not exists IndexReference on Refs(reference);
-- Paths can refer to themselves, causing a tuple (N, N) in the Refs
-- table. This causes a deletion of the corresponding row in
-- ValidPaths to cause a foreign key constraint violation (due to `on
-- delete restrict' on the `reference' column). Therefore, explicitly
-- get rid of self-references.
create trigger if not exists DeleteSelfRefs before delete on ValidPaths
begin
delete from Refs where referrer = old.id and reference = old.id;
end;
create table if not exists DerivationOutputs (
drv integer not null,
id text not null, -- symbolic output id, usually "out"
path text not null,
primary key (drv, id),
foreign key (drv) references ValidPaths(id) on delete cascade
);
create index if not exists IndexDerivationOutputs on DerivationOutputs(path);
create table if not exists FailedPaths (
path text primary key not null,
time integer not null
);
+23 -13
View File
@@ -1,6 +1,7 @@
#include "store-api.hh" #include "store-api.hh"
#include "globals.hh" #include "globals.hh"
#include "util.hh" #include "util.hh"
#include "derivations.hh"
#include <limits.h> #include <limits.h>
@@ -52,6 +53,18 @@ Path toStorePath(const Path & path)
} }
string getNameOfStorePath(const Path & path)
{
Path::size_type slash = path.rfind('/');
string p = slash == Path::npos ? path : string(path, slash + 1);
Path::size_type dash = p.find('-');
assert(dash != Path::npos);
string p2 = string(p, dash + 1);
if (isDerivation(p2)) p2 = string(p2, 0, p2.size() - 4);
return p2;
}
Path followLinksToStore(const Path & _path) Path followLinksToStore(const Path & _path)
{ {
Path path = absPath(_path); Path path = absPath(_path);
@@ -190,7 +203,7 @@ std::pair<Path, Hash> computeStorePathForPath(const Path & srcPath,
bool recursive, HashType hashAlgo, PathFilter & filter) bool recursive, HashType hashAlgo, PathFilter & filter)
{ {
HashType ht(hashAlgo); HashType ht(hashAlgo);
Hash h = recursive ? hashPath(ht, srcPath, filter).first : hashFile(ht, srcPath); Hash h = recursive ? hashPath(ht, srcPath, filter) : hashFile(ht, srcPath);
string name = baseNameOf(srcPath); string name = baseNameOf(srcPath);
Path dstPath = makeFixedOutputPath(recursive, hashAlgo, h, name); Path dstPath = makeFixedOutputPath(recursive, hashAlgo, h, name);
return std::pair<Path, Hash>(dstPath, h); return std::pair<Path, Hash>(dstPath, h);
@@ -216,7 +229,7 @@ Path computeStorePathForText(const string & name, const string & s,
/* Return a string accepted by decodeValidPathInfo() that /* Return a string accepted by decodeValidPathInfo() that
registers the specified paths as valid. Note: it's the registers the specified paths as valid. Note: it's the
responsibility of the caller to provide a closure. */ responsibility of the caller to provide a closure. */
string StoreAPI::makeValidityRegistration(const PathSet & paths, string makeValidityRegistration(const PathSet & paths,
bool showDerivers, bool showHash) bool showDerivers, bool showHash)
{ {
string s = ""; string s = "";
@@ -224,19 +237,18 @@ string StoreAPI::makeValidityRegistration(const PathSet & paths,
foreach (PathSet::iterator, i, paths) { foreach (PathSet::iterator, i, paths) {
s += *i + "\n"; s += *i + "\n";
ValidPathInfo info = queryPathInfo(*i); if (showHash)
s += printHash(store->queryPathHash(*i)) + "\n";
if (showHash) { Path deriver = showDerivers ? store->queryDeriver(*i) : "";
s += printHash(info.hash) + "\n";
s += (format("%1%\n") % info.narSize).str();
}
Path deriver = showDerivers ? info.deriver : "";
s += deriver + "\n"; s += deriver + "\n";
s += (format("%1%\n") % info.references.size()).str(); PathSet references;
store->queryReferences(*i, references);
foreach (PathSet::iterator, j, info.references) s += (format("%1%\n") % references.size()).str();
foreach (PathSet::iterator, j, references)
s += *j + "\n"; s += *j + "\n";
} }
@@ -253,8 +265,6 @@ ValidPathInfo decodeValidPathInfo(std::istream & str, bool hashGiven)
string s; string s;
getline(str, s); getline(str, s);
info.hash = parseHash(htSHA256, s); info.hash = parseHash(htSHA256, s);
getline(str, s);
if (!string2Int(s, info.narSize)) throw Error("number expected");
} }
getline(str, info.deriver); getline(str, info.deriver);
string s; int n; string s; int n;
+44 -38
View File
@@ -1,14 +1,14 @@
#ifndef __STOREAPI_H #ifndef __STOREAPI_H
#define __STOREAPI_H #define __STOREAPI_H
#include "hash.hh"
#include "serialise.hh"
#include <string> #include <string>
#include <map> #include <map>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include "hash.hh"
#include "serialise.hh"
namespace nix { namespace nix {
@@ -16,6 +16,8 @@ namespace nix {
typedef std::map<Path, Path> Roots; typedef std::map<Path, Path> Roots;
struct GCOptions struct GCOptions
{ {
/* Garbage collector operation: /* Garbage collector operation:
@@ -87,25 +89,9 @@ struct SubstitutablePathInfo
Path deriver; Path deriver;
PathSet references; PathSet references;
unsigned long long downloadSize; /* 0 = unknown or inapplicable */ unsigned long long downloadSize; /* 0 = unknown or inapplicable */
unsigned long long narSize; /* 0 = unknown */
}; };
struct ValidPathInfo
{
Path path;
Path deriver;
Hash hash;
PathSet references;
time_t registrationTime;
unsigned long long narSize; // 0 = unknown
unsigned long long id; // internal use only
ValidPathInfo() : registrationTime(0), narSize(0) { }
};
typedef list<ValidPathInfo> ValidPathInfos;
class StoreAPI class StoreAPI
{ {
public: public:
@@ -118,9 +104,6 @@ public:
/* Query the set of valid paths. */ /* Query the set of valid paths. */
virtual PathSet queryValidPaths() = 0; virtual PathSet queryValidPaths() = 0;
/* Query information about a valid path. */
virtual ValidPathInfo queryPathInfo(const Path & path) = 0;
/* Queries the hash of a valid path. */ /* Queries the hash of a valid path. */
virtual Hash queryPathHash(const Path & path) = 0; virtual Hash queryPathHash(const Path & path) = 0;
@@ -129,18 +112,33 @@ public:
virtual void queryReferences(const Path & path, virtual void queryReferences(const Path & path,
PathSet & references) = 0; PathSet & references) = 0;
/* Like queryReferences, but with self-references filtered out. */
PathSet queryReferencesNoSelf(const Path & path)
{
PathSet res;
queryReferences(path, res);
res.erase(path);
return res;
}
/* Queries the set of incoming FS references for a store path. /* Queries the set of incoming FS references for a store path.
The result is not cleared. */ The result is not cleared. */
virtual void queryReferrers(const Path & path, virtual void queryReferrers(const Path & path,
PathSet & referrers) = 0; PathSet & referrers) = 0;
/* Like queryReferrers, but with self-references filtered out. */
PathSet queryReferrersNoSelf(const Path & path)
{
PathSet res;
queryReferrers(path, res);
res.erase(path);
return res;
}
/* Query the deriver of a store path. Return the empty string if /* Query the deriver of a store path. Return the empty string if
no deriver has been set. */ no deriver has been set. */
virtual Path queryDeriver(const Path & path) = 0; virtual Path queryDeriver(const Path & path) = 0;
/* Query the outputs of the derivation denoted by `path'. */
virtual PathSet queryDerivationOutputs(const Path & path) = 0;
/* Query whether a path has substitutes. */ /* Query whether a path has substitutes. */
virtual bool hasSubstitutes(const Path & path) = 0; virtual bool hasSubstitutes(const Path & path) = 0;
@@ -226,19 +224,6 @@ public:
/* Perform a garbage collection. */ /* Perform a garbage collection. */
virtual void collectGarbage(const GCOptions & options, GCResults & results) = 0; virtual void collectGarbage(const GCOptions & options, GCResults & results) = 0;
/* Return the set of paths that have failed to build.*/
virtual PathSet queryFailedPaths() = 0;
/* Clear the "failed" status of the given paths. The special
value `*' causes all failed paths to be cleared. */
virtual void clearFailedPaths(const PathSet & paths) = 0;
/* Return a string representing information about the path that
can be loaded into the database using `nix-store --load-db' or
`nix-store --register-validity'. */
string makeValidityRegistration(const PathSet & paths,
bool showDerivers, bool showHash);
}; };
@@ -258,6 +243,12 @@ void checkStoreName(const string & name);
Path toStorePath(const Path & path); Path toStorePath(const Path & path);
/* Get the "name" part of a store path, that is, the part after the
hash and the dash, and with any ".drv" suffix removed
(e.g. /nix/store/<hash>-foo-1.2.3.drv => foo-1.2.3). */
string getNameOfStorePath(const Path & path);
/* Follow symlinks until we end up with a path in the Nix store. */ /* Follow symlinks until we end up with a path in the Nix store. */
Path followLinksToStore(const Path & path); Path followLinksToStore(const Path & path);
@@ -332,6 +323,21 @@ boost::shared_ptr<StoreAPI> openStore();
string showPaths(const PathSet & paths); string showPaths(const PathSet & paths);
string makeValidityRegistration(const PathSet & paths,
bool showDerivers, bool showHash);
struct ValidPathInfo
{
Path path;
Path deriver;
Hash hash;
PathSet references;
time_t registrationTime;
ValidPathInfo() : registrationTime(0) { }
};
typedef list<ValidPathInfo> ValidPathInfos;
ValidPathInfo decodeValidPathInfo(std::istream & str, ValidPathInfo decodeValidPathInfo(std::istream & str,
bool hashGiven = false); bool hashGiven = false);
+1 -6
View File
@@ -8,7 +8,7 @@ namespace nix {
#define WORKER_MAGIC_1 0x6e697863 #define WORKER_MAGIC_1 0x6e697863
#define WORKER_MAGIC_2 0x6478696f #define WORKER_MAGIC_2 0x6478696f
#define PROTOCOL_VERSION 0x108 #define PROTOCOL_VERSION 0x105
#define GET_PROTOCOL_MAJOR(x) ((x) & 0xff00) #define GET_PROTOCOL_MAJOR(x) ((x) & 0xff00)
#define GET_PROTOCOL_MINOR(x) ((x) & 0x00ff) #define GET_PROTOCOL_MINOR(x) ((x) & 0x00ff)
@@ -34,11 +34,6 @@ typedef enum {
wopSetOptions = 19, wopSetOptions = 19,
wopCollectGarbage = 20, wopCollectGarbage = 20,
wopQuerySubstitutablePathInfo = 21, wopQuerySubstitutablePathInfo = 21,
wopQueryDerivationOutputs = 22,
wopQueryValidPaths = 23,
wopQueryFailedPaths = 24,
wopClearFailedPaths = 25,
wopQueryPathInfo = 26,
} WorkerOp; } WorkerOp;
+4 -4
View File
@@ -1,16 +1,16 @@
pkglib_LTLIBRARIES = libutil.la pkglib_LTLIBRARIES = libutil.la
libutil_la_SOURCES = util.cc hash.cc serialise.cc \ libutil_la_SOURCES = util.cc hash.cc serialise.cc \
archive.cc xml-writer.cc archive.cc aterm.cc aterm-map.cc xml-writer.cc
libutil_la_LIBADD = ../boost/format/libformat.la ${aterm_lib} ${sqlite_lib} libutil_la_LIBADD = ../boost/format/libformat.la
pkginclude_HEADERS = util.hh hash.hh serialise.hh \ pkginclude_HEADERS = util.hh hash.hh serialise.hh \
archive.hh xml-writer.hh types.hh archive.hh aterm.hh aterm-map.hh xml-writer.hh types.hh
if !HAVE_OPENSSL if !HAVE_OPENSSL
libutil_la_SOURCES += \ libutil_la_SOURCES += \
md5.c md5.h sha1.c sha1.h sha256.c sha256.h md32_common.h md5.c md5.h sha1.c sha1.h sha256.c sha256.h md32_common.h
endif endif
AM_CXXFLAGS = -Wall -I$(srcdir)/.. AM_CXXFLAGS = -Wall -I$(srcdir)/.. ${aterm_include}
+8
View File
@@ -181,6 +181,8 @@ static void parseContents(ParseSink & sink, Source & source, const Path & path)
left -= n; left -= n;
} }
sink.finalizeContents(size);
readPadding(size, source); readPadding(size, source);
} }
@@ -315,6 +317,12 @@ struct RestoreSink : ParseSink
writeFull(fd, data, len); writeFull(fd, data, len);
} }
void finalizeContents(unsigned long long size)
{
errno = ftruncate(fd, size);
if (errno) throw SysError(format("truncating file to its allocated length of %1% bytes") % size);
}
void createSymlink(const Path & path, const string & target) void createSymlink(const Path & path, const string & target)
{ {
Path p = dstPath + path; Path p = dstPath + path;
+1
View File
@@ -64,6 +64,7 @@ struct ParseSink
virtual void isExecutable() { }; virtual void isExecutable() { };
virtual void preallocateContents(unsigned long long size) { }; virtual void preallocateContents(unsigned long long size) { };
virtual void receiveContents(unsigned char * data, unsigned int len) { }; virtual void receiveContents(unsigned char * data, unsigned int len) { };
virtual void finalizeContents(unsigned long long size) { };
virtual void createSymlink(const Path & path, const string & target) { }; virtual void createSymlink(const Path & path, const string & target) { };
}; };
+332
View File
@@ -0,0 +1,332 @@
#include "aterm-map.hh"
#include <iostream>
#include <assert.h>
#include <stdlib.h>
#include <aterm2.h>
namespace nix {
static const unsigned int maxLoadFactor = /* 1 / */ 3;
static unsigned int nrResizes = 0;
static unsigned int sizeTotalAlloc = 0;
static unsigned int sizeCurAlloc = 0;
static unsigned int sizeMaxAlloc = 0;
ATermMap::ATermMap(unsigned int expectedCount)
{
init(expectedCount);
}
ATermMap::ATermMap(const ATermMap & map)
{
init(map.maxCount);
copy(map.hashTable, map.capacity);
}
ATermMap & ATermMap::operator = (const ATermMap & map)
{
if (this == &map) return *this;
free();
init(map.maxCount);
copy(map.hashTable, map.capacity);
return *this;
}
ATermMap::~ATermMap()
{
free();
}
void ATermMap::init(unsigned int expectedCount)
{
assert(sizeof(ATerm) * 2 == sizeof(KeyValue));
capacity = 0;
count = 0;
maxCount = 0;
hashTable = 0;
resizeTable(expectedCount);
}
void ATermMap::free()
{
if (hashTable) {
ATunprotectArray((ATerm *) hashTable);
::free(hashTable);
sizeCurAlloc -= sizeof(KeyValue) * capacity;
hashTable = 0;
}
}
static unsigned int roundToPowerOf2(unsigned int x)
{
x--;
x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16;
x++;
return x;
}
void ATermMap::resizeTable(unsigned int expectedCount)
{
if (expectedCount == 0) expectedCount = 1;
// cout << maxCount << " -> " << expectedCount << endl;
// cout << maxCount << " " << size << endl;
// cout << (double) size / maxCount << endl;
unsigned int oldCapacity = capacity;
KeyValue * oldHashTable = hashTable;
maxCount = expectedCount;
capacity = roundToPowerOf2(maxCount * maxLoadFactor);
hashTable = (KeyValue *) calloc(sizeof(KeyValue), capacity);
sizeTotalAlloc += sizeof(KeyValue) * capacity;
sizeCurAlloc += sizeof(KeyValue) * capacity;
if (sizeCurAlloc > sizeMaxAlloc) sizeMaxAlloc = sizeCurAlloc;
ATprotectArray((ATerm *) hashTable, capacity * 2);
// cout << capacity << endl;
/* Re-hash the elements in the old table. */
if (oldCapacity != 0) {
count = 0;
copy(oldHashTable, oldCapacity);
ATunprotectArray((ATerm *) oldHashTable);
::free(oldHashTable);
sizeCurAlloc -= sizeof(KeyValue) * oldCapacity;
nrResizes++;
}
}
void ATermMap::copy(KeyValue * elements, unsigned int capacity)
{
for (unsigned int i = 0; i < capacity; ++i)
if (elements[i].value) /* i.e., non-empty, non-deleted element */
set(elements[i].key, elements[i].value);
}
/* !!! use a bigger shift for 64-bit platforms? */
static const unsigned int shift = 16;
static const unsigned long knuth = (unsigned long) (0.6180339887 * (1 << shift));
unsigned long ATermMap::hash1(ATerm key) const
{
/* Don't care about the least significant bits of the ATerm
pointer since they're always 0. */
unsigned long key2 = ((unsigned long) key) >> 2;
/* Approximately equal to:
double d = key2 * 0.6180339887;
unsigned int h = (int) (capacity * (d - floor(d)));
*/
unsigned long h = (capacity * ((key2 * knuth) & ((1 << shift) - 1))) >> shift;
return h;
}
unsigned long ATermMap::hash2(ATerm key) const
{
unsigned long key2 = ((unsigned long) key) >> 2;
/* Note: the result must be relatively prime to `capacity' (which
is a power of 2), so we make sure that the result is always
odd. */
unsigned long h = ((key2 * 134217689) & (capacity - 1)) | 1;
return h;
}
static unsigned int nrItemsSet = 0;
static unsigned int nrSetProbes = 0;
void ATermMap::set(ATerm key, ATerm value)
{
if (count == maxCount) resizeTable(capacity * 2 / maxLoadFactor);
nrItemsSet++;
for (unsigned int i = 0, h = hash1(key); i < capacity;
++i, h = (h + hash2(key)) & (capacity - 1))
{
// assert(h < capacity);
nrSetProbes++;
/* Note: to see whether a slot is free, we check
hashTable[h].value, not hashTable[h].key, since we use
value == 0 to mark deleted slots. */
if (hashTable[h].value == 0 || hashTable[h].key == key) {
if (hashTable[h].value == 0) count++;
hashTable[h].key = key;
hashTable[h].value = value;
return;
}
}
abort();
}
static unsigned int nrItemsGet = 0;
static unsigned int nrGetProbes = 0;
ATerm ATermMap::get(ATerm key) const
{
nrItemsGet++;
for (unsigned int i = 0, h = hash1(key); i < capacity;
++i, h = (h + hash2(key)) & (capacity - 1))
{
nrGetProbes++;
if (hashTable[h].key == 0) return 0;
if (hashTable[h].key == key) return hashTable[h].value;
}
return 0;
}
void ATermMap::remove(ATerm key)
{
for (unsigned int i = 0, h = hash1(key); i < capacity;
++i, h = (h + hash2(key)) & (capacity - 1))
{
if (hashTable[h].key == 0) return;
if (hashTable[h].key == key) {
if (hashTable[h].value != 0) {
hashTable[h].value = 0;
count--;
}
return;
}
}
}
unsigned int ATermMap::size()
{
return count; /* STL nomenclature */
}
void printATermMapStats()
{
using std::cerr;
using std::endl;
cerr << "RESIZES: " << nrResizes << " "
<< sizeTotalAlloc << " "
<< sizeCurAlloc << " "
<< sizeMaxAlloc << endl;
cerr << "SET: "
<< nrItemsSet << " "
<< nrSetProbes << " "
<< (double) nrSetProbes / nrItemsSet << endl;
cerr << "GET: "
<< nrItemsGet << " "
<< nrGetProbes << " "
<< (double) nrGetProbes / nrItemsGet << endl;
}
#if 0
int main(int argc, char * * argv)
{
ATerm bottomOfStack;
ATinit(argc, argv, &bottomOfStack);
/* Make test terms. */
int nrTestTerms = 100000;
ATerm testTerms[nrTestTerms];
for (int i = 0; i < nrTestTerms; ++i) {
char name[10];
sprintf(name, "%d", (int) random() % 37);
int arity = i == 0 ? 0 : (random() % 37);
ATerm kids[arity];
for (int j = 0; j < arity; ++j)
kids[j] = testTerms[random() % i];
testTerms[i] = (ATerm) ATmakeApplArray(ATmakeAFun(name, arity, ATfalse), kids);
// ATwriteToSharedTextFile(testTerms[i], stdout);
// printf("\n");
}
cout << "testing...\n";
#define someTerm() (testTerms[(int) random() % nrTestTerms])
for (int test = 0; test < 100000; ++test) {
//cerr << test << endl;
unsigned int n = 300;
ATermMap map(300);
ATerm keys[n], values[n];
for (unsigned int i = 0; i < n; ++i) {
keys[i] = someTerm();
values[i] = someTerm();
map.set(keys[i], values[i]);
//cerr << "INSERT: " << keys[i] << " " << values[i] << endl;
}
unsigned int size = map.size();
assert(size <= n);
values[n - 1] = 0;
map.remove(keys[n - 1]);
assert(map.size() == size - 1);
unsigned int checksum;
unsigned int count = 0;
for (ATermMap::const_iterator i = map.begin(); i != map.end(); ++i, ++count) {
assert(i->key);
assert(i->value);
checksum += (unsigned int) (*i).key;
checksum += (unsigned int) (*i).value;
// cout << (*i).key << " " << (*i).value << endl;
}
assert(count == size - 1);
for (unsigned int i = 0; i < n; ++i) {
for (unsigned int j = i + 1; j < n; ++j)
if (keys[i] == keys[j]) goto x;
if (map.get(keys[i]) != values[i]) {
cerr << "MISMATCH: " << keys[i] << " " << values[i] << " " << map.get(keys[i]) << " " << i << endl;
abort();
}
if (values[i] != 0) {
checksum -= (unsigned int) keys[i];
checksum -= (unsigned int) values[i];
}
x: ;
}
assert(checksum == 0);
for (unsigned int i = 0; i < 100; ++i)
map.get(someTerm());
}
printATermMapStats();
}
#endif
}
+130
View File
@@ -0,0 +1,130 @@
#ifndef __ATERM_MAP_H
#define __ATERM_MAP_H
#include <aterm1.h>
#include <aterm2.h>
#include <assert.h>
namespace nix {
class ATermMap
{
public:
struct KeyValue
{
ATerm key;
ATerm value;
};
private:
/* Hash table for the map. We use open addressing, i.e., all
key/value pairs are stored directly in the table, and there are
no pointers. Collisions are resolved through probing. */
KeyValue * hashTable;
/* Current size of the hash table. */
unsigned int capacity;
/* Number of elements in the hash table. */
unsigned int count;
/* Maximum number of elements in the hash table. If `count'
exceeds this number, the hash table is expanded. */
unsigned int maxCount;
public:
/* Create a map. `expectedCount' is the number of elements the
map is expected to hold. */
ATermMap(unsigned int expectedCount = 16);
ATermMap(const ATermMap & map);
~ATermMap();
ATermMap & operator = (const ATermMap & map);
void set(ATerm key, ATerm value);
ATerm get(ATerm key) const;
ATerm operator [](ATerm key) const
{
return get(key);
}
void remove(ATerm key);
unsigned int size();
struct const_iterator
{
const ATermMap & map;
unsigned int pos;
const_iterator(const ATermMap & map, int pos) : map(map)
{
this->pos = pos;
}
bool operator !=(const const_iterator & i)
{
return pos != i.pos;
}
void operator ++()
{
if (pos == map.capacity) return;
do { ++pos;
} while (pos < map.capacity && map.hashTable[pos].value == 0);
}
const KeyValue & operator *()
{
assert(pos < map.capacity);
return map.hashTable[pos];
}
const KeyValue * operator ->()
{
assert(pos < map.capacity);
return &map.hashTable[pos];
}
};
friend class ATermMap::const_iterator;
const_iterator begin() const
{
unsigned int i = 0;
while (i < capacity && hashTable[i].value == 0) ++i;
return const_iterator(*this, i);
}
const_iterator end() const
{
return const_iterator(*this, capacity);
}
private:
void init(unsigned int expectedCount);
void free();
void resizeTable(unsigned int expectedCount);
void copy(KeyValue * elements, unsigned int capacity);
inline unsigned long hash1(ATerm key) const;
inline unsigned long hash2(ATerm key) const;
};
/* Hack. */
void printATermMapStats();
}
#endif /* !__ATERM_MAP_H */
+55
View File
@@ -0,0 +1,55 @@
#include "aterm.hh"
#include <cstring>
using std::string;
string nix::atPrint(ATerm t)
{
if (!t) throw Error("attempt to print null aterm");
char * s = ATwriteToString(t);
if (!s) throw Error("cannot print term");
return s;
}
std::ostream & operator << (std::ostream & stream, ATerm e)
{
return stream << nix::atPrint(e);
}
nix::Error nix::badTerm(const format & f, ATerm t)
{
char * s = ATwriteToString(t);
if (!s) throw Error("cannot print term");
if (strlen(s) > 1000) {
int len;
s = ATwriteToSharedString(t, &len);
if (!s) throw Error("cannot print term");
}
return Error(format("%1%, in `%2%'") % f.str() % (string) s);
}
ATerm nix::toATerm(const char * s)
{
return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s, 0, ATtrue));
}
ATerm nix::toATerm(const string & s)
{
return toATerm(s.c_str());
}
ATermList nix::toATermList(const StringSet & ss)
{
ATermList l = ATempty;
for (StringSet::const_reverse_iterator i = ss.rbegin();
i != ss.rend(); ++i)
l = ATinsert(l, toATerm(*i));
return l;
}
+55
View File
@@ -0,0 +1,55 @@
#ifndef __ATERM_H
#define __ATERM_H
#include <aterm2.h>
#include "types.hh"
namespace nix {
/* Print an ATerm. */
string atPrint(ATerm t);
class ATermIterator
{
ATermList t;
public:
ATermIterator(ATermList _t) : t(_t) { }
ATermIterator & operator ++ ()
{
t = ATgetNext(t);
return *this;
}
ATerm operator * ()
{
return ATgetFirst(t);
}
operator bool ()
{
return t != ATempty;
}
};
/* Throw an exception with an error message containing the given
aterm. */
Error badTerm(const format & f, ATerm t);
/* Convert strings to ATerms. */
ATerm toATerm(const char * s);
ATerm toATerm(const string & s);
ATermList toATermList(const StringSet & ss);
}
/* Write an ATerm to an output stream. */
std::ostream & operator << (std::ostream & stream, ATerm e);
#endif /* !__ATERM_H */
Loaded 100 of 199 files, more files were not shown because too many files have changed in this diff. Show more