1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
#include <map>
#include <iostream>
#include "globals.hh"
#include "fstate.hh"
#include "store.hh"
#include "shared.hh"
typedef ATerm Expr;
static Strings searchDirs;
static string searchPath(string relPath)
{
if (string(relPath, 0, 1) == "/") return relPath;
for (Strings::iterator i = searchDirs.begin();
i != searchDirs.end(); i++)
{
string path = *i + "/" + relPath;
if (pathExists(path)) return path;
}
throw Error(
format("path `%1%' not found in any of the search directories")
% relPath);
}
static Expr evalFile(string fileName);
static Expr substExpr(string x, Expr rep, Expr e)
{
char * s;
Expr e2;
if (ATmatch(e, "Var(<str>)", &s))
if (x == s)
return rep;
else
return e;
if (ATmatch(e, "Lam(<str>, <term>)", &s, &e2))
if (x == s)
return e;
/* !!! unfair substitutions */
/* Generically substitute in subterms. */
if (ATgetType(e) == AT_APPL) {
AFun fun = ATgetAFun(e);
int arity = ATgetArity(fun);
ATermList args = ATempty;
for (int i = arity - 1; i >= 0; i--)
args = ATinsert(args, substExpr(x, rep, ATgetArgument(e, i)));
return (ATerm) ATmakeApplList(fun, args);
}
if (ATgetType(e) == AT_LIST) {
ATermList in = (ATermList) e;
ATermList out = ATempty;
while (!ATisEmpty(in)) {
out = ATinsert(out, substExpr(x, rep, ATgetFirst(in)));
in = ATgetNext(in);
}
return (ATerm) ATreverse(out);
}
throw badTerm("do not know how to substitute", e);
}
static Expr substExprMany(ATermList formals, ATermList args, Expr body)
{
char * s;
Expr e;
/* !!! check args against formals */
while (!ATisEmpty(args)) {
ATerm tup = ATgetFirst(args);
if (!ATmatch(tup, "(<str>, <term>)", &s, &e))
throw badTerm("expected an argument tuple", tup);
body = substExpr(s, e, body);
args = ATgetNext(args);
}
return body;
}
static Expr evalExpr(Expr e)
{
char * s1;
Expr e1, e2, e3, e4;
ATermList bnds;
/* Normal forms. */
if (ATmatch(e, "<str>", &s1) ||
ATmatch(e, "Function([<list>], <term>)", &e1, &e2) ||
ATmatch(e, "FSId(<str>)", &s1))
return e;
/* Application. */
if (ATmatch(e, "App(<term>, [<list>])", &e1, &e2)) {
e1 = evalExpr(e1);
if (!ATmatch(e1, "Function([<list>], <term>)", &e3, &e4))
throw badTerm("expecting a function", e1);
return evalExpr(substExprMany((ATermList) e3, (ATermList) e2, e4));
}
/* Fix inclusion. */
if (ATmatch(e, "IncludeFix(<str>)", &s1)) {
string fileName(s1);
return evalFile(s1);
}
/* Relative files. */
if (ATmatch(e, "Relative(<str>)", &s1)) {
string srcPath = searchPath(s1);
string dstPath;
FSId id;
addToStore(srcPath, dstPath, id, true);
FState fs = ATmake("Slice([<str>], [(<str>, <str>, [])])",
((string) id).c_str(), dstPath.c_str(), ((string) id).c_str());
return ATmake("FSId(<str>)",
((string) writeTerm(fs, "", 0)).c_str());
}
/* Packages are transformed into Derive fstate expressions. */
if (ATmatch(e, "Package([<list>])", &bnds)) {
/* Evaluate the bindings and put them in a map. */
map<string, ATerm> bndMap;
bndMap["platform"] = ATmake("<str>", SYSTEM);
while (!ATisEmpty(bnds)) {
ATerm bnd = ATgetFirst(bnds);
if (!ATmatch(bnd, "(<str>, <term>)", &s1, &e1))
throw badTerm("binding expected", bnd);
bndMap[s1] = evalExpr(e1);
bnds = ATgetNext(bnds);
}
/* Gather information for building the Derive expression. */
ATermList ins = ATempty, env = ATempty;
string builder, name;
bnds = ATempty;
for (map<string, ATerm>::iterator it = bndMap.begin();
it != bndMap.end(); it++)
{
string key = it->first;
ATerm value = it->second;
char * id;
if (ATmatch(value, "FSId(<str>)", &id)) {
Strings paths = fstatePaths(parseHash(id));
if (paths.size() != 1) abort();
string path = *(paths.begin());
ins = ATinsert(ins, ATmake("<str>", id));
env = ATinsert(env, ATmake("(<str>, <str>)",
key.c_str(), path.c_str()));
if (key == "build") builder = path;
}
else if (ATmatch(value, "<str>", &s1)) {
if (key == "name") name = s1;
env = ATinsert(env,
ATmake("(<str>, <str>)", key.c_str(), s1));
}
else throw badTerm("invalid package argument", value);
bnds = ATinsert(bnds,
ATmake("(<str>, <term>)", key.c_str(), value));
}
/* Hash the normal form to produce a unique but deterministic
path name for this package. */
ATerm nf = ATmake("Package(<term>)", ATreverse(bnds));
FSId outId = hashTerm(nf);
if (builder == "")
throw badTerm("no builder specified", nf);
if (name == "")
throw badTerm("no package name specified", nf);
string outPath =
canonPath(nixStore + "/" + ((string) outId).c_str() + "-" + name);
env = ATinsert(env, ATmake("(<str>, <str>)", "out", outPath.c_str()));
/* Construct the result. */
FState fs =
ATmake("Derive([(<str>, <str>)], <term>, <str>, <str>, <term>)",
outPath.c_str(), ((string) outId).c_str(),
ins, builder.c_str(), SYSTEM, env);
/* Write the resulting term into the Nix store directory. */
return ATmake("FSId(<str>)",
((string) writeTerm(fs, "-d-" + name, 0)).c_str());
}
/* BaseName primitive function. */
if (ATmatch(e, "BaseName(<term>)", &e1)) {
e1 = evalExpr(e1);
if (!ATmatch(e1, "<str>", &s1))
throw badTerm("string expected", e1);
return ATmake("<str>", baseNameOf(s1).c_str());
}
/* Barf. */
throw badTerm("invalid expression", e);
}
static Expr evalFile(string relPath)
{
string path = searchPath(relPath);
Expr e = ATreadFromNamedFile(path.c_str());
if (!e)
throw Error(format("unable to read a term from `%1%'") % path);
return evalExpr(e);
}
void run(Strings args)
{
Strings files;
searchDirs.push_back(".");
searchDirs.push_back(nixDataDir + "/fix");
for (Strings::iterator it = args.begin();
it != args.end(); )
{
string arg = *it++;
if (arg == "--includedir" || arg == "-I") {
if (it == args.end())
throw UsageError(format("argument required in `%1%'") % arg);
searchDirs.push_back(*it++);
}
else if (arg[0] == '-')
throw UsageError(format("unknown flag `%1%`") % arg);
else
files.push_back(arg);
}
if (files.empty()) throw UsageError("no files specified");
for (Strings::iterator it = files.begin();
it != files.end(); it++)
{
Expr e = evalFile(*it);
char * s;
if (ATmatch(e, "FSId(<str>)", &s)) {
cout << format("%1%\n") % s;
}
else throw badTerm("top level is not a package", e);
}
}
string programId = "fix";
|