mirror of
https://github.com/NixOS/nix
synced 2025-06-27 12:41:15 +02:00
* Refactoring: move derivation evaluation to libexpr.
This commit is contained in:
parent
4db4b61380
commit
39f50db731
4 changed files with 209 additions and 187 deletions
|
@ -3,7 +3,8 @@ lib_LTLIBRARIES = libexpr.la
|
|||
libexpr_la_SOURCES = nixexpr.cc nixexpr.hh parser.cc parser.hh \
|
||||
eval.cc eval.hh primops.cc \
|
||||
lexer-tab.c lexer-tab.h parser-tab.c parser-tab.h \
|
||||
nixexpr-ast.hh
|
||||
nixexpr-ast.hh \
|
||||
get-drvs.cc get-drvs.hh
|
||||
|
||||
EXTRA_DIST = lexer.l parser.y nixexpr-ast.def nixexpr-ast.cc
|
||||
|
||||
|
|
67
src/libexpr/get-drvs.cc
Normal file
67
src/libexpr/get-drvs.cc
Normal file
|
@ -0,0 +1,67 @@
|
|||
#include "get-drvs.hh"
|
||||
#include "nixexpr-ast.hh"
|
||||
|
||||
|
||||
bool getDerivation(EvalState & state, Expr e, DrvInfo & drv)
|
||||
{
|
||||
ATermList es;
|
||||
e = evalExpr(state, e);
|
||||
if (!matchAttrs(e, es)) return false;
|
||||
|
||||
ATermMap attrs;
|
||||
queryAllAttrs(e, attrs, false);
|
||||
|
||||
Expr a = attrs.get("type");
|
||||
if (!a || evalString(state, a) != "derivation") return false;
|
||||
|
||||
a = attrs.get("name");
|
||||
if (!a) throw badTerm("derivation name missing", e);
|
||||
drv.name = evalString(state, a);
|
||||
|
||||
a = attrs.get("system");
|
||||
if (!a)
|
||||
drv.system = "unknown";
|
||||
else
|
||||
drv.system = evalString(state, a);
|
||||
|
||||
drv.attrs = attrs;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void getDerivations(EvalState & state, Expr e, DrvInfos & drvs)
|
||||
{
|
||||
ATermList es;
|
||||
DrvInfo drv;
|
||||
|
||||
e = evalExpr(state, e);
|
||||
|
||||
if (getDerivation(state, e, drv))
|
||||
drvs.push_back(drv);
|
||||
|
||||
else if (matchAttrs(e, es)) {
|
||||
ATermMap drvMap;
|
||||
queryAllAttrs(e, drvMap);
|
||||
for (ATermIterator i(drvMap.keys()); i; ++i) {
|
||||
debug(format("evaluating attribute `%1%'") % aterm2String(*i));
|
||||
if (getDerivation(state, drvMap.get(*i), drv))
|
||||
drvs.push_back(drv);
|
||||
else
|
||||
;
|
||||
// parseDerivations(state, drvMap.get(*i), drvs);
|
||||
}
|
||||
}
|
||||
|
||||
else if (matchList(e, es)) {
|
||||
for (ATermIterator i(es); i; ++i) {
|
||||
debug(format("evaluating list element"));
|
||||
if (getDerivation(state, *i, drv))
|
||||
drvs.push_back(drv);
|
||||
else
|
||||
getDerivations(state, *i, drvs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
64
src/libexpr/get-drvs.hh
Normal file
64
src/libexpr/get-drvs.hh
Normal file
|
@ -0,0 +1,64 @@
|
|||
#ifndef __GET_DRVS_H
|
||||
#define __GET_DRVS_H
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
#include "eval.hh"
|
||||
|
||||
|
||||
struct DrvInfo
|
||||
{
|
||||
private:
|
||||
string drvPath;
|
||||
string outPath;
|
||||
|
||||
public:
|
||||
string name;
|
||||
string system;
|
||||
|
||||
ATermMap attrs;
|
||||
|
||||
string queryDrvPath(EvalState & state) const
|
||||
{
|
||||
if (drvPath == "") {
|
||||
Expr a = attrs.get("drvPath");
|
||||
(string &) drvPath = a ? evalPath(state, a) : "";
|
||||
}
|
||||
return drvPath;
|
||||
}
|
||||
|
||||
string queryOutPath(EvalState & state) const
|
||||
{
|
||||
if (outPath == "") {
|
||||
Expr a = attrs.get("outPath");
|
||||
if (!a) throw Error("output path missing");
|
||||
(string &) outPath = evalPath(state, a);
|
||||
}
|
||||
return outPath;
|
||||
}
|
||||
|
||||
void setDrvPath(const string & s)
|
||||
{
|
||||
drvPath = s;
|
||||
}
|
||||
|
||||
void setOutPath(const string & s)
|
||||
{
|
||||
outPath = s;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
typedef list<DrvInfo> DrvInfos;
|
||||
|
||||
|
||||
/* Evaluate expression `e'. If it evaluates to a derivation, store
|
||||
information about the derivation in `drv' and return true.
|
||||
Otherwise, return false. */
|
||||
bool getDerivation(EvalState & state, Expr e, DrvInfo & drv);
|
||||
|
||||
void getDerivations(EvalState & state, Expr e, DrvInfos & drvs);
|
||||
|
||||
|
||||
#endif /* !__GET_DRVS_H */
|
Loading…
Add table
Add a link
Reference in a new issue