2003-10-07 12:27:49 +00:00
|
|
|
#include "expr.hh"
|
2003-06-16 13:33:38 +00:00
|
|
|
#include "globals.hh"
|
2003-07-07 07:43:58 +00:00
|
|
|
#include "store.hh"
|
2003-06-16 13:33:38 +00:00
|
|
|
|
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
string printTerm(ATerm t)
|
2003-06-17 13:37:44 +00:00
|
|
|
{
|
2003-06-27 13:55:12 +00:00
|
|
|
char * s = ATwriteToString(t);
|
2003-11-05 16:20:57 +00:00
|
|
|
if (!s) throw Error("cannot print term");
|
2003-06-17 13:37:44 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-06 14:20:47 +00:00
|
|
|
Error badTerm(const format & f, ATerm t)
|
2003-06-16 13:33:38 +00:00
|
|
|
{
|
2003-11-05 16:20:57 +00:00
|
|
|
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);
|
2003-06-16 13:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
Hash hashTerm(ATerm t)
|
2003-06-16 13:33:38 +00:00
|
|
|
{
|
2003-06-27 13:55:12 +00:00
|
|
|
return hashString(printTerm(t));
|
2003-06-16 13:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
Path writeTerm(ATerm t, const string & suffix)
|
2003-07-04 12:18:06 +00:00
|
|
|
{
|
2003-10-08 15:06:59 +00:00
|
|
|
/* The id of a term is its hash. */
|
|
|
|
Hash h = hashTerm(t);
|
2003-07-15 16:28:54 +00:00
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
Path path = canonPath(nixStore + "/" +
|
|
|
|
(string) h + suffix + ".nix");
|
2003-07-15 16:28:54 +00:00
|
|
|
|
2003-10-10 13:22:29 +00:00
|
|
|
if (!isValidPath(path)) {
|
2003-10-15 12:42:39 +00:00
|
|
|
char * s = ATwriteToString(t);
|
|
|
|
if (!s) throw Error(format("cannot write aterm to `%1%'") % path);
|
|
|
|
addTextToStore(path, string(s));
|
2003-10-10 13:22:29 +00:00
|
|
|
}
|
2003-10-15 12:42:39 +00:00
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
return path;
|
2003-07-04 12:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
static void parsePaths(ATermList paths, PathSet & out)
|
2003-07-15 16:28:54 +00:00
|
|
|
{
|
* Change the abstract syntax of slices. It used to be that ids were used as
keys to reference slice elements, e.g.,
Slice(["1ef7..."], [("/nix/store/1ef7...-foo", "1ef7", ["8c99..."]), ...])
This was wrong, since ids represent contents, not locations. Therefore we
now have:
Slice(["/nix/store/1ef7..."], [("/nix/store/1ef7...-foo", "1ef7", ["/nix/store/8c99-..."]), ...])
* Fix a bug in the computation of slice closures that could cause slice
elements to be duplicated.
2003-08-20 12:39:56 +00:00
|
|
|
while (!ATisEmpty(paths)) {
|
2003-07-15 16:28:54 +00:00
|
|
|
char * s;
|
* Change the abstract syntax of slices. It used to be that ids were used as
keys to reference slice elements, e.g.,
Slice(["1ef7..."], [("/nix/store/1ef7...-foo", "1ef7", ["8c99..."]), ...])
This was wrong, since ids represent contents, not locations. Therefore we
now have:
Slice(["/nix/store/1ef7..."], [("/nix/store/1ef7...-foo", "1ef7", ["/nix/store/8c99-..."]), ...])
* Fix a bug in the computation of slice closures that could cause slice
elements to be duplicated.
2003-08-20 12:39:56 +00:00
|
|
|
ATerm t = ATgetFirst(paths);
|
|
|
|
if (!ATmatch(t, "<str>", &s))
|
|
|
|
throw badTerm("not a path", t);
|
2003-08-20 14:11:40 +00:00
|
|
|
out.insert(s);
|
* Change the abstract syntax of slices. It used to be that ids were used as
keys to reference slice elements, e.g.,
Slice(["1ef7..."], [("/nix/store/1ef7...-foo", "1ef7", ["8c99..."]), ...])
This was wrong, since ids represent contents, not locations. Therefore we
now have:
Slice(["/nix/store/1ef7..."], [("/nix/store/1ef7...-foo", "1ef7", ["/nix/store/8c99-..."]), ...])
* Fix a bug in the computation of slice closures that could cause slice
elements to be duplicated.
2003-08-20 12:39:56 +00:00
|
|
|
paths = ATgetNext(paths);
|
2003-07-15 16:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-07 12:27:49 +00:00
|
|
|
static void checkClosure(const Closure & closure)
|
2003-07-20 19:29:38 +00:00
|
|
|
{
|
2003-10-07 12:27:49 +00:00
|
|
|
if (closure.elems.size() == 0)
|
|
|
|
throw Error("empty closure");
|
2003-07-20 19:29:38 +00:00
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
PathSet decl;
|
2003-10-07 12:27:49 +00:00
|
|
|
for (ClosureElems::const_iterator i = closure.elems.begin();
|
|
|
|
i != closure.elems.end(); i++)
|
2003-08-20 14:11:40 +00:00
|
|
|
decl.insert(i->first);
|
2003-07-20 19:29:38 +00:00
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
for (PathSet::const_iterator i = closure.roots.begin();
|
2003-10-07 12:27:49 +00:00
|
|
|
i != closure.roots.end(); i++)
|
2003-07-20 19:29:38 +00:00
|
|
|
if (decl.find(*i) == decl.end())
|
* Change the abstract syntax of slices. It used to be that ids were used as
keys to reference slice elements, e.g.,
Slice(["1ef7..."], [("/nix/store/1ef7...-foo", "1ef7", ["8c99..."]), ...])
This was wrong, since ids represent contents, not locations. Therefore we
now have:
Slice(["/nix/store/1ef7..."], [("/nix/store/1ef7...-foo", "1ef7", ["/nix/store/8c99-..."]), ...])
* Fix a bug in the computation of slice closures that could cause slice
elements to be duplicated.
2003-08-20 12:39:56 +00:00
|
|
|
throw Error(format("undefined root path `%1%'") % *i);
|
2003-07-20 19:29:38 +00:00
|
|
|
|
2003-10-07 12:27:49 +00:00
|
|
|
for (ClosureElems::const_iterator i = closure.elems.begin();
|
|
|
|
i != closure.elems.end(); i++)
|
2003-10-08 15:06:59 +00:00
|
|
|
for (PathSet::const_iterator j = i->second.refs.begin();
|
2003-08-20 14:11:40 +00:00
|
|
|
j != i->second.refs.end(); j++)
|
2003-07-20 19:29:38 +00:00
|
|
|
if (decl.find(*j) == decl.end())
|
* Change the abstract syntax of slices. It used to be that ids were used as
keys to reference slice elements, e.g.,
Slice(["1ef7..."], [("/nix/store/1ef7...-foo", "1ef7", ["8c99..."]), ...])
This was wrong, since ids represent contents, not locations. Therefore we
now have:
Slice(["/nix/store/1ef7..."], [("/nix/store/1ef7...-foo", "1ef7", ["/nix/store/8c99-..."]), ...])
* Fix a bug in the computation of slice closures that could cause slice
elements to be duplicated.
2003-08-20 12:39:56 +00:00
|
|
|
throw Error(
|
|
|
|
format("undefined path `%1%' referenced by `%2%'")
|
2003-08-20 14:11:40 +00:00
|
|
|
% *j % i->first);
|
2003-07-20 19:29:38 +00:00
|
|
|
}
|
2003-07-16 20:33:29 +00:00
|
|
|
|
|
|
|
|
2003-10-07 12:27:49 +00:00
|
|
|
/* Parse a closure. */
|
|
|
|
static bool parseClosure(ATerm t, Closure & closure)
|
2003-07-15 16:28:54 +00:00
|
|
|
{
|
|
|
|
ATermList roots, elems;
|
|
|
|
|
2003-10-07 12:27:49 +00:00
|
|
|
if (!ATmatch(t, "Closure([<list>], [<list>])", &roots, &elems))
|
2003-07-20 19:29:38 +00:00
|
|
|
return false;
|
2003-07-15 16:28:54 +00:00
|
|
|
|
2003-10-07 12:27:49 +00:00
|
|
|
parsePaths(roots, closure.roots);
|
2003-07-15 16:28:54 +00:00
|
|
|
|
|
|
|
while (!ATisEmpty(elems)) {
|
2003-10-08 15:06:59 +00:00
|
|
|
char * s1;
|
2003-07-15 16:28:54 +00:00
|
|
|
ATermList refs;
|
|
|
|
ATerm t = ATgetFirst(elems);
|
2003-10-08 15:06:59 +00:00
|
|
|
if (!ATmatch(t, "(<str>, [<list>])", &s1, &refs))
|
2003-10-07 12:27:49 +00:00
|
|
|
throw badTerm("not a closure element", t);
|
|
|
|
ClosureElem elem;
|
* Change the abstract syntax of slices. It used to be that ids were used as
keys to reference slice elements, e.g.,
Slice(["1ef7..."], [("/nix/store/1ef7...-foo", "1ef7", ["8c99..."]), ...])
This was wrong, since ids represent contents, not locations. Therefore we
now have:
Slice(["/nix/store/1ef7..."], [("/nix/store/1ef7...-foo", "1ef7", ["/nix/store/8c99-..."]), ...])
* Fix a bug in the computation of slice closures that could cause slice
elements to be duplicated.
2003-08-20 12:39:56 +00:00
|
|
|
parsePaths(refs, elem.refs);
|
2003-10-07 12:27:49 +00:00
|
|
|
closure.elems[s1] = elem;
|
2003-07-15 16:28:54 +00:00
|
|
|
elems = ATgetNext(elems);
|
|
|
|
}
|
|
|
|
|
2003-10-07 12:27:49 +00:00
|
|
|
checkClosure(closure);
|
2003-07-20 19:29:38 +00:00
|
|
|
return true;
|
2003-07-15 16:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-07 12:27:49 +00:00
|
|
|
static bool parseDerivation(ATerm t, Derivation & derivation)
|
2003-07-15 21:24:05 +00:00
|
|
|
{
|
2003-08-15 12:32:37 +00:00
|
|
|
ATermList outs, ins, args, bnds;
|
2003-07-15 16:28:54 +00:00
|
|
|
char * builder;
|
|
|
|
char * platform;
|
2003-07-15 21:24:05 +00:00
|
|
|
|
2003-08-15 12:32:37 +00:00
|
|
|
if (!ATmatch(t, "Derive([<list>], [<list>], <str>, <str>, [<list>], [<list>])",
|
|
|
|
&outs, &ins, &platform, &builder, &args, &bnds))
|
|
|
|
{
|
|
|
|
/* !!! compatibility -> remove eventually */
|
|
|
|
if (!ATmatch(t, "Derive([<list>], [<list>], <str>, <str>, [<list>])",
|
|
|
|
&outs, &ins, &builder, &platform, &bnds))
|
|
|
|
return false;
|
|
|
|
args = ATempty;
|
|
|
|
}
|
2003-07-15 16:28:54 +00:00
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
parsePaths(outs, derivation.outputs);
|
|
|
|
parsePaths(ins, derivation.inputs);
|
2003-07-15 16:28:54 +00:00
|
|
|
|
2003-10-07 12:27:49 +00:00
|
|
|
derivation.builder = builder;
|
|
|
|
derivation.platform = platform;
|
2003-07-20 19:29:38 +00:00
|
|
|
|
2003-08-15 12:32:37 +00:00
|
|
|
while (!ATisEmpty(args)) {
|
|
|
|
char * s;
|
|
|
|
ATerm arg = ATgetFirst(args);
|
|
|
|
if (!ATmatch(arg, "<str>", &s))
|
|
|
|
throw badTerm("string expected", arg);
|
2003-10-07 12:27:49 +00:00
|
|
|
derivation.args.push_back(s);
|
2003-08-15 12:32:37 +00:00
|
|
|
args = ATgetNext(args);
|
|
|
|
}
|
|
|
|
|
2003-07-15 16:28:54 +00:00
|
|
|
while (!ATisEmpty(bnds)) {
|
|
|
|
char * s1, * s2;
|
|
|
|
ATerm bnd = ATgetFirst(bnds);
|
|
|
|
if (!ATmatch(bnd, "(<str>, <str>)", &s1, &s2))
|
|
|
|
throw badTerm("tuple of strings expected", bnd);
|
2003-10-07 12:27:49 +00:00
|
|
|
derivation.env[s1] = s2;
|
2003-07-15 16:28:54 +00:00
|
|
|
bnds = ATgetNext(bnds);
|
|
|
|
}
|
|
|
|
|
2003-07-20 19:29:38 +00:00
|
|
|
return true;
|
2003-07-15 16:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-07 12:27:49 +00:00
|
|
|
NixExpr parseNixExpr(ATerm t)
|
2003-07-16 11:05:59 +00:00
|
|
|
{
|
2003-10-07 12:27:49 +00:00
|
|
|
NixExpr ne;
|
|
|
|
if (parseClosure(t, ne.closure))
|
|
|
|
ne.type = NixExpr::neClosure;
|
|
|
|
else if (parseDerivation(t, ne.derivation))
|
|
|
|
ne.type = NixExpr::neDerivation;
|
|
|
|
else throw badTerm("not a Nix expression", t);
|
|
|
|
return ne;
|
2003-07-16 11:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
static ATermList unparsePaths(const PathSet & paths)
|
2003-07-15 21:24:05 +00:00
|
|
|
{
|
2003-07-20 19:29:38 +00:00
|
|
|
ATermList l = ATempty;
|
2003-10-08 15:06:59 +00:00
|
|
|
for (PathSet::const_iterator i = paths.begin();
|
* Change the abstract syntax of slices. It used to be that ids were used as
keys to reference slice elements, e.g.,
Slice(["1ef7..."], [("/nix/store/1ef7...-foo", "1ef7", ["8c99..."]), ...])
This was wrong, since ids represent contents, not locations. Therefore we
now have:
Slice(["/nix/store/1ef7..."], [("/nix/store/1ef7...-foo", "1ef7", ["/nix/store/8c99-..."]), ...])
* Fix a bug in the computation of slice closures that could cause slice
elements to be duplicated.
2003-08-20 12:39:56 +00:00
|
|
|
i != paths.end(); i++)
|
|
|
|
l = ATinsert(l, ATmake("<str>", i->c_str()));
|
2003-07-20 19:29:38 +00:00
|
|
|
return ATreverse(l);
|
2003-07-15 21:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-07 12:27:49 +00:00
|
|
|
static ATerm unparseClosure(const Closure & closure)
|
2003-07-15 16:28:54 +00:00
|
|
|
{
|
2003-10-07 12:27:49 +00:00
|
|
|
ATermList roots = unparsePaths(closure.roots);
|
2003-07-20 19:29:38 +00:00
|
|
|
|
|
|
|
ATermList elems = ATempty;
|
2003-10-07 12:27:49 +00:00
|
|
|
for (ClosureElems::const_iterator i = closure.elems.begin();
|
|
|
|
i != closure.elems.end(); i++)
|
2003-07-20 19:29:38 +00:00
|
|
|
elems = ATinsert(elems,
|
2003-10-08 15:06:59 +00:00
|
|
|
ATmake("(<str>, <term>)",
|
2003-08-20 14:11:40 +00:00
|
|
|
i->first.c_str(),
|
|
|
|
unparsePaths(i->second.refs)));
|
2003-07-15 16:28:54 +00:00
|
|
|
|
2003-10-07 12:27:49 +00:00
|
|
|
return ATmake("Closure(<term>, <term>)", roots, elems);
|
2003-07-15 16:28:54 +00:00
|
|
|
}
|
2003-07-15 22:28:27 +00:00
|
|
|
|
|
|
|
|
2003-10-07 12:27:49 +00:00
|
|
|
static ATerm unparseDerivation(const Derivation & derivation)
|
2003-07-15 22:28:27 +00:00
|
|
|
{
|
2003-08-15 12:32:37 +00:00
|
|
|
ATermList args = ATempty;
|
2003-10-07 12:27:49 +00:00
|
|
|
for (Strings::const_iterator i = derivation.args.begin();
|
|
|
|
i != derivation.args.end(); i++)
|
2003-08-15 12:32:37 +00:00
|
|
|
args = ATinsert(args, ATmake("<str>", i->c_str()));
|
|
|
|
|
2003-07-20 19:29:38 +00:00
|
|
|
ATermList env = ATempty;
|
2003-10-07 12:27:49 +00:00
|
|
|
for (StringPairs::const_iterator i = derivation.env.begin();
|
|
|
|
i != derivation.env.end(); i++)
|
2003-07-20 19:29:38 +00:00
|
|
|
env = ATinsert(env,
|
|
|
|
ATmake("(<str>, <str>)",
|
|
|
|
i->first.c_str(), i->second.c_str()));
|
2003-07-15 22:28:27 +00:00
|
|
|
|
2003-08-15 12:32:37 +00:00
|
|
|
return ATmake("Derive(<term>, <term>, <str>, <str>, <term>, <term>)",
|
2003-10-08 15:06:59 +00:00
|
|
|
unparsePaths(derivation.outputs),
|
|
|
|
unparsePaths(derivation.inputs),
|
2003-10-07 12:27:49 +00:00
|
|
|
derivation.platform.c_str(),
|
|
|
|
derivation.builder.c_str(),
|
2003-08-15 12:32:37 +00:00
|
|
|
ATreverse(args),
|
2003-07-20 19:29:38 +00:00
|
|
|
ATreverse(env));
|
2003-07-15 22:28:27 +00:00
|
|
|
}
|
2003-07-16 11:05:59 +00:00
|
|
|
|
|
|
|
|
2003-10-07 12:27:49 +00:00
|
|
|
ATerm unparseNixExpr(const NixExpr & ne)
|
2003-07-16 11:05:59 +00:00
|
|
|
{
|
2003-10-07 12:27:49 +00:00
|
|
|
if (ne.type == NixExpr::neClosure)
|
|
|
|
return unparseClosure(ne.closure);
|
|
|
|
else if (ne.type == NixExpr::neDerivation)
|
|
|
|
return unparseDerivation(ne.derivation);
|
2003-07-20 19:29:38 +00:00
|
|
|
else abort();
|
2003-07-16 11:05:59 +00:00
|
|
|
}
|