Nix/src/libstore/storeexpr.cc

189 lines
4.9 KiB
C++
Raw Normal View History

#include "storeexpr.hh"
#include "globals.hh"
2003-07-07 09:43:58 +02:00
#include "store.hh"
#include "storeexpr-ast.hh"
#include "storeexpr-ast.cc"
Hash hashTerm(ATerm t)
{
return hashString(atPrint(t), htMD5);
}
Path writeTerm(ATerm t, const string & suffix)
2003-07-04 14:18:06 +02:00
{
char * s = ATwriteToString(t);
if (!s) throw Error("cannot print aterm");
return addTextToStore(suffix + ".store", string(s));
2003-07-04 14:18:06 +02:00
}
static void parsePaths(ATermList paths, PathSet & out)
{
for (ATermIterator i(paths); i; ++i) {
if (ATgetType(*i) != AT_APPL)
throw badTerm("not a path", *i);
string s = aterm2String(*i);
if (s.size() == 0 || s[0] != '/')
throw badTerm("not a path", *i);
out.insert(s);
}
}
static void checkClosure(const Closure & closure)
2003-07-20 21:29:38 +02:00
{
if (closure.elems.size() == 0)
throw Error("empty closure");
2003-07-20 21:29:38 +02:00
PathSet decl;
for (ClosureElems::const_iterator i = closure.elems.begin();
i != closure.elems.end(); i++)
decl.insert(i->first);
2003-07-20 21:29:38 +02:00
for (PathSet::const_iterator i = closure.roots.begin();
i != closure.roots.end(); i++)
2003-07-20 21:29:38 +02:00
if (decl.find(*i) == decl.end())
throw Error(format("undefined root path `%1%'") % *i);
2003-07-20 21:29:38 +02:00
for (ClosureElems::const_iterator i = closure.elems.begin();
i != closure.elems.end(); i++)
for (PathSet::const_iterator j = i->second.refs.begin();
j != i->second.refs.end(); j++)
2003-07-20 21:29:38 +02:00
if (decl.find(*j) == decl.end())
throw Error(
format("undefined path `%1%' referenced by `%2%'")
% *j % i->first);
2003-07-20 21:29:38 +02:00
}
/* Parse a closure. */
static bool parseClosure(ATerm t, Closure & closure)
{
ATermList roots, elems;
if (!matchClosure(t, roots, elems))
2003-07-20 21:29:38 +02:00
return false;
parsePaths(roots, closure.roots);
for (ATermIterator i(elems); i; ++i) {
ATerm path;
ATermList refs;
if (!matchClosureElem(*i, path, refs))
throw badTerm("not a closure element", *i);
ClosureElem elem;
parsePaths(refs, elem.refs);
closure.elems[aterm2String(path)] = elem;
}
checkClosure(closure);
2003-07-20 21:29:38 +02:00
return true;
}
static bool parseDerivation(ATerm t, Derivation & derivation)
{
ATermList outs, ins, args, bnds;
ATerm builder, platform;
if (!matchDerive(t, outs, ins, platform, builder, args, bnds))
return false;
parsePaths(outs, derivation.outputs);
parsePaths(ins, derivation.inputs);
derivation.builder = aterm2String(builder);
derivation.platform = aterm2String(platform);
2003-07-20 21:29:38 +02:00
for (ATermIterator i(args); i; ++i) {
if (ATgetType(*i) != AT_APPL)
throw badTerm("string expected", *i);
derivation.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);
derivation.env[aterm2String(s1)] = aterm2String(s2);
}
2003-07-20 21:29:38 +02:00
return true;
}
StoreExpr parseStoreExpr(ATerm t)
2003-07-16 13:05:59 +02:00
{
StoreExpr ne;
if (parseClosure(t, ne.closure))
ne.type = StoreExpr::neClosure;
else if (parseDerivation(t, ne.derivation))
ne.type = StoreExpr::neDerivation;
else throw badTerm("not a store expression", t);
return ne;
2003-07-16 13:05:59 +02:00
}
static ATermList unparsePaths(const PathSet & paths)
{
2003-07-20 21:29:38 +02:00
ATermList l = ATempty;
for (PathSet::const_iterator i = paths.begin();
i != paths.end(); i++)
2004-11-03 19:12:03 +01:00
l = ATinsert(l, toATerm(*i));
2003-07-20 21:29:38 +02:00
return ATreverse(l);
}
static ATerm unparseClosure(const Closure & closure)
{
ATermList roots = unparsePaths(closure.roots);
2003-07-20 21:29:38 +02:00
ATermList elems = ATempty;
for (ClosureElems::const_iterator i = closure.elems.begin();
i != closure.elems.end(); i++)
2003-07-20 21:29:38 +02:00
elems = ATinsert(elems,
makeClosureElem(
2004-11-03 19:12:03 +01:00
toATerm(i->first),
unparsePaths(i->second.refs)));
return makeClosure(roots, elems);
}
2003-07-16 00:28:27 +02:00
static ATerm unparseDerivation(const Derivation & derivation)
2003-07-16 00:28:27 +02:00
{
ATermList args = ATempty;
for (Strings::const_iterator i = derivation.args.begin();
i != derivation.args.end(); i++)
2004-11-03 19:12:03 +01:00
args = ATinsert(args, toATerm(*i));
2003-07-20 21:29:38 +02:00
ATermList env = ATempty;
for (StringPairs::const_iterator i = derivation.env.begin();
i != derivation.env.end(); i++)
2003-07-20 21:29:38 +02:00
env = ATinsert(env,
makeEnvBinding(
2004-11-03 19:12:03 +01:00
toATerm(i->first),
toATerm(i->second)));
2003-07-16 00:28:27 +02:00
return makeDerive(
unparsePaths(derivation.outputs),
unparsePaths(derivation.inputs),
2004-11-03 19:12:03 +01:00
toATerm(derivation.platform),
toATerm(derivation.builder),
ATreverse(args),
2003-07-20 21:29:38 +02:00
ATreverse(env));
2003-07-16 00:28:27 +02:00
}
2003-07-16 13:05:59 +02:00
ATerm unparseStoreExpr(const StoreExpr & ne)
2003-07-16 13:05:59 +02:00
{
if (ne.type == StoreExpr::neClosure)
return unparseClosure(ne.closure);
else if (ne.type == StoreExpr::neDerivation)
return unparseDerivation(ne.derivation);
2003-07-20 21:29:38 +02:00
else abort();
2003-07-16 13:05:59 +02:00
}