aboutsummaryrefslogtreecommitdiff
path: root/src/fix.cc
blob: 83e25d142a19a28f6c71d2739a77fd3ae43ae0d6 (plain)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include <map>
#include <iostream>

#include "globals.hh"
#include "normalise.hh"
#include "shared.hh"


typedef ATerm Expr;

typedef map<ATerm, ATerm> NormalForms;
typedef map<FSId, Strings> PkgPaths;
typedef map<FSId, Hash> PkgHashes;

struct EvalState 
{
    Strings searchDirs;
    NormalForms normalForms;
    PkgPaths pkgPaths;
    PkgHashes pkgHashes; /* normalised package hashes */
    Expr blackHole;

    EvalState()
    {
        blackHole = ATmake("BlackHole()");
        if (!blackHole) throw Error("cannot build black hole");
    }
};


static Expr evalFile(EvalState & state, string fileName);
static Expr evalExpr(EvalState & state, Expr e);


static string searchPath(const Strings & searchDirs, string relPath)
{
    if (string(relPath, 0, 1) == "/") return relPath;

    for (Strings::const_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 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;

    ATermList formals;
    if (ATmatch(e, "Function([<list>], <term>)", &formals, &e2)) {
        while (!ATisEmpty(formals)) {
            if (!ATmatch(ATgetFirst(formals), "<str>", &s))
                throw badTerm("not a list of formals", (ATerm) formals);
            if (x == (string) s)
                return e;
            formals = ATgetNext(formals);
        }
    }

    /* 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 Strings fstatePathsCached(EvalState & state, const FSId & id)
{
    PkgPaths::iterator i = state.pkgPaths.find(id);
    if (i != state.pkgPaths.end())
        return i->second;
    else {
        Strings paths = fstatePaths(id);
        state.pkgPaths[id] = paths;
        return paths;
    }
}


static Hash hashPackage(EvalState & state, FState fs)
{
    if (fs.type == FState::fsDerive) {
        for (FSIds::iterator i = fs.derive.inputs.begin();
             i != fs.derive.inputs.end(); i++)
        {
            PkgHashes::iterator j = state.pkgHashes.find(*i);
            if (j == state.pkgHashes.end())
                throw Error(format("unknown package id %1%") % (string) *i);
            *i = j->second;
        }
    }
    return hashTerm(unparseFState(fs));
}


static string processBinding(EvalState & state, Expr e, FState & fs)
{
    char * s1;

    if (ATmatch(e, "FSId(<str>)", &s1)) {
        FSId id = parseHash(s1);
        Strings paths = fstatePathsCached(state, id);
        if (paths.size() != 1) abort();
        string path = *(paths.begin());
        fs.derive.inputs.push_back(id);
        return path;
    }
    
    if (ATmatch(e, "<str>", &s1))
        return s1;

    if (ATmatch(e, "True")) return "1";
    
    if (ATmatch(e, "False")) return "";

    ATermList l;
    if (ATmatch(e, "[<list>]", &l)) {
	string s;
	bool first = true;
        while (!ATisEmpty(l)) {
	    if (!first) s = s + " "; else first = false;
	    s += processBinding(state, evalExpr(state, ATgetFirst(l)), fs);
            l = ATgetNext(l);
        }
	return s;
    }
    
    throw badTerm("invalid package binding", e);
}


static Expr evalExpr2(EvalState & state, Expr e)
{
    char * s1;
    Expr e1, e2, e3, e4;
    ATermList bnds;

    /* Normal forms. */
    if (ATmatch(e, "<str>", &s1) ||
        ATmatch(e, "[<list>]", &e1) ||
        ATmatch(e, "True") ||
        ATmatch(e, "False") ||
        ATmatch(e, "Function([<list>], <term>)", &e1, &e2) ||
        ATmatch(e, "FSId(<str>)", &s1))
        return e;

    try {
        Hash pkgHash = hashPackage(state, parseFState(e));
        FSId pkgId = writeTerm(e, "");
        state.pkgHashes[pkgId] = pkgHash;
        return ATmake("FSId(<str>)", ((string) pkgId).c_str());
    } catch (...) { /* !!! catch parse errors only */
    }

    /* Application. */
    if (ATmatch(e, "Call(<term>, [<list>])", &e1, &e2) ||
        ATmatch(e, "App(<term>, [<list>])", &e1, &e2)) {
        e1 = evalExpr(state, e1);
        if (!ATmatch(e1, "Function([<list>], <term>)", &e3, &e4))
            throw badTerm("expecting a function", e1);
        return evalExpr(state,
            substExprMany((ATermList) e3, (ATermList) e2, e4));
    }

    /* Conditional. */
    if (ATmatch(e, "If(<term>, <term>, <term>)", &e1, &e2, &e3)) {
        e1 = evalExpr(state, e1);
        Expr x;
        if (ATmatch(e1, "True")) x = e2;
        else if (ATmatch(e1, "False")) x = e3;
        else throw badTerm("expecting a boolean", e1);
        return evalExpr(state, x);
    }

    /* Ad-hoc function for string matching. */
    if (ATmatch(e, "HasSubstr(<term>, <term>)", &e1, &e2)) {
        e1 = evalExpr(state, e1);
        e2 = evalExpr(state, e2);
        
        char * s1, * s2;
        if (!ATmatch(e1, "<str>", &s1))
            throw badTerm("expecting a string", e1);
        if (!ATmatch(e2, "<str>", &s2))
            throw badTerm("expecting a string", e2);
        
        return
            string(s1).find(string(s2)) != string::npos ?
            ATmake("True") : ATmake("False");
    }

    /* Platform constant. */
    if (ATmatch(e, "Platform")) {
        return ATmake("<str>", SYSTEM);
    }

    /* Fix inclusion. */
    if (ATmatch(e, "IncludeFix(<str>)", &s1)) {
        string fileName(s1);
        return evalFile(state, s1);
    }

    /* Relative files. */
    if (ATmatch(e, "Relative(<str>)", &s1)) {
        string srcPath = searchPath(state.searchDirs, s1);
        string dstPath;
        FSId id;
        addToStore(srcPath, dstPath, id, true);

        SliceElem elem;
        elem.path = dstPath;
        elem.id = id;
        FState fs;
        fs.type = FState::fsSlice;
        fs.slice.roots.push_back(dstPath);
        fs.slice.elems.push_back(elem);

        Hash pkgHash = hashPackage(state, fs);
        FSId pkgId = writeTerm(unparseFState(fs), "");
        state.pkgHashes[pkgId] = pkgHash;

        msg(lvlChatty, format("copied `%1%' -> %2%")
            % srcPath % (string) pkgId);

        return ATmake("FSId(<str>)", ((string) pkgId).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(state, e1);
            bnds = ATgetNext(bnds);
        }

        /* Gather information for building the Derive expression. */
        FState fs;
        fs.type = FState::fsDerive;
        fs.derive.platform = SYSTEM;
        string name;
        FSId outId;
        bool outIdGiven = false;
        bnds = ATempty;

        for (map<string, ATerm>::iterator it = bndMap.begin();
             it != bndMap.end(); it++)
        {
            string key = it->first;
            ATerm value = it->second;

            if (key == "args") {
                ATermList args;
                if (!ATmatch(value, "[<list>]", &args))
                    throw badTerm("list expected", value);
                
                while (!ATisEmpty(args)) {
                    Expr arg = evalExpr(state, ATgetFirst(args));
                    fs.derive.args.push_back(processBinding(state, arg, fs));
                    args = ATgetNext(args);
                }
            } 

            else {
                string s = processBinding(state, value, fs);
                fs.derive.env.push_back(StringPair(key, s));

                if (key == "build") fs.derive.builder = s;
                if (key == "name") name = s;
                if (key == "id") { 
                    outId = parseHash(s);
                    outIdGiven = true;
                }
            }

            bnds = ATinsert(bnds, 
                ATmake("(<str>, <term>)", key.c_str(), value));
        }

        if (fs.derive.builder == "")
            throw badTerm("no builder specified", e);
        
        if (name == "")
            throw badTerm("no package name specified", e);
        
        /* Hash the fstate-expression with no outputs to produce a
           unique but deterministic path name for this package. */
        if (!outIdGiven) outId = hashPackage(state, fs);
        string outPath = 
            canonPath(nixStore + "/" + ((string) outId).c_str() + "-" + name);
        fs.derive.env.push_back(StringPair("out", outPath));
        fs.derive.outputs.push_back(DeriveOutput(outPath, outId));

        /* Write the resulting term into the Nix store directory. */
        Hash pkgHash = outIdGiven
            ? hashString((string) outId + outPath)
            : hashPackage(state, fs);
        FSId pkgId = writeTerm(unparseFState(fs), "-d-" + name);
        state.pkgHashes[pkgId] = pkgHash;

        msg(lvlChatty, format("instantiated `%1%' -> %2%")
            % name % (string) pkgId);

        return ATmake("FSId(<str>)", ((string) pkgId).c_str());
    }

    /* BaseName primitive function. */
    if (ATmatch(e, "BaseName(<term>)", &e1)) {
        e1 = evalExpr(state, 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 evalExpr(EvalState & state, Expr e)
{
    Nest nest(lvlVomit, format("evaluating expression: %1%") % printTerm(e));

    /* Consult the memo table to quickly get the normal form of
       previously evaluated expressions. */
    NormalForms::iterator i = state.normalForms.find(e);
    if (i != state.normalForms.end()) {
        if (i->second == state.blackHole)
            throw badTerm("infinite recursion", e);
        return i->second;
    }

    /* Otherwise, evaluate and memoize. */
    state.normalForms[e] = state.blackHole;
    Expr nf = evalExpr2(state, e);
    state.normalForms[e] = nf;
    return nf;
}


static Expr evalFile(EvalState & state, string relPath)
{
    string path = searchPath(state.searchDirs, relPath);
    Nest nest(lvlTalkative, format("evaluating file `%1%'") % path);
    Expr e = ATreadFromNamedFile(path.c_str());
    if (!e) 
        throw Error(format("unable to read a term from `%1%'") % path);
    return evalExpr(state, e);
}


static Expr evalStdin(EvalState & state)
{
    Nest nest(lvlTalkative, format("evaluating standard input"));
    Expr e = ATreadFromFile(stdin);
    if (!e) 
        throw Error(format("unable to read a term from stdin"));
    return evalExpr(state, e);
}


static void printFSId(EvalState & state, Expr e)
{
    ATermList es;
    char * s;
    if (ATmatch(e, "FSId(<str>)", &s)) {
        cout << format("%1%\n") % s;
    } 
    else if (ATmatch(e, "[<list>]", &es)) {
        while (!ATisEmpty(es)) {
            printFSId(state, evalExpr(state, ATgetFirst(es)));
            es = ATgetNext(es);
        }
    }
    else throw badTerm("top level does not evaluate to a (list of) Nix expression(s)", e);
}


void run(Strings args)
{
    openDB();

    EvalState state;
    Strings files;
    bool readStdin = false;

    state.searchDirs.push_back(".");
    state.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);
            state.searchDirs.push_back(*it++);
        }
        else if (arg == "--verbose" || arg == "-v")
            verbosity = (Verbosity) ((int) verbosity + 1);
        else if (arg == "-")
            readStdin = true;
        else if (arg[0] == '-')
            throw UsageError(format("unknown flag `%1%`") % arg);
        else
            files.push_back(arg);
    }

    if (readStdin) {
        Expr e = evalStdin(state);
        printFSId(state, e);
    }

    for (Strings::iterator it = files.begin();
         it != files.end(); it++)
    {
        Expr e = evalFile(state, *it);
        printFSId(state, e);
    }
}


string programId = "fix";