2003-07-20 19:29:38 +00:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "normalise.hh"
|
|
|
|
#include "references.hh"
|
|
|
|
#include "db.hh"
|
|
|
|
#include "exec.hh"
|
2003-08-01 14:11:19 +00:00
|
|
|
#include "pathlocks.hh"
|
2003-07-20 19:29:38 +00:00
|
|
|
#include "globals.hh"
|
|
|
|
|
|
|
|
|
2003-08-01 15:41:47 +00:00
|
|
|
void registerSuccessor(const Transaction & txn,
|
|
|
|
const FSId & id1, const FSId & id2)
|
2003-07-20 19:29:38 +00:00
|
|
|
{
|
2003-07-31 16:05:35 +00:00
|
|
|
nixDB.setString(txn, dbSuccessors, id1, id2);
|
2003-07-20 19:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-01 14:11:19 +00:00
|
|
|
static FSId useSuccessor(const FSId & id)
|
|
|
|
{
|
|
|
|
string idSucc;
|
|
|
|
if (nixDB.queryString(noTxn, dbSuccessors, id, idSucc)) {
|
|
|
|
debug(format("successor %1% -> %2%") % (string) id % idSucc);
|
|
|
|
return parseHash(idSucc);
|
|
|
|
} else
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typedef map<string, FSId> OutPaths;
|
|
|
|
typedef map<string, SliceElem> ElemMap;
|
|
|
|
|
|
|
|
|
|
|
|
Strings pathsFromOutPaths(const OutPaths & ps)
|
|
|
|
{
|
|
|
|
Strings ss;
|
|
|
|
for (OutPaths::const_iterator i = ps.begin();
|
|
|
|
i != ps.end(); i++)
|
|
|
|
ss.push_back(i->first);
|
|
|
|
return ss;
|
|
|
|
}
|
2003-07-20 19:29:38 +00:00
|
|
|
|
|
|
|
|
2003-07-29 09:45:03 +00:00
|
|
|
FSId normaliseFState(FSId id, FSIdSet pending)
|
2003-07-20 19:29:38 +00:00
|
|
|
{
|
2003-07-24 13:43:16 +00:00
|
|
|
Nest nest(lvlTalkative, format("normalising fstate %1%") % (string) id);
|
2003-07-20 19:29:38 +00:00
|
|
|
|
|
|
|
/* Try to substitute $id$ by any known successors in order to
|
|
|
|
speed up the rewrite process. */
|
2003-08-01 14:11:19 +00:00
|
|
|
id = useSuccessor(id);
|
2003-07-20 19:29:38 +00:00
|
|
|
|
|
|
|
/* Get the fstate expression. */
|
|
|
|
FState fs = parseFState(termFromId(id));
|
|
|
|
|
2003-08-01 14:11:19 +00:00
|
|
|
/* If this is a normal form (i.e., a slice) we are done. */
|
2003-07-29 09:45:03 +00:00
|
|
|
if (fs.type == FState::fsSlice) return id;
|
2003-08-01 14:11:19 +00:00
|
|
|
if (fs.type != FState::fsDerive) abort();
|
2003-07-20 19:29:38 +00:00
|
|
|
|
2003-08-01 14:11:19 +00:00
|
|
|
|
|
|
|
/* Otherwise, it's a derive expression, and we have to build it to
|
|
|
|
determine its normal form. */
|
|
|
|
|
|
|
|
|
|
|
|
/* Some variables. */
|
|
|
|
|
|
|
|
/* Output paths, with their ids. */
|
|
|
|
OutPaths outPaths;
|
|
|
|
|
|
|
|
/* Input paths, with their slice elements. */
|
|
|
|
ElemMap inMap;
|
|
|
|
|
|
|
|
/* Referencable paths (i.e., input and output paths). */
|
|
|
|
Strings refPaths;
|
|
|
|
|
|
|
|
/* The environment to be passed to the builder. */
|
|
|
|
Environment env;
|
|
|
|
|
|
|
|
|
|
|
|
/* Parse the outputs. */
|
|
|
|
for (DeriveOutputs::iterator i = fs.derive.outputs.begin();
|
|
|
|
i != fs.derive.outputs.end(); i++)
|
|
|
|
{
|
|
|
|
debug(format("building %1% in `%2%'") % (string) i->second % i->first);
|
|
|
|
outPaths[i->first] = i->second;
|
|
|
|
refPaths.push_back(i->first);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Obtain locks on all output paths. The locks are automatically
|
|
|
|
released when we exit this function or Nix crashes. */
|
2003-08-04 07:09:36 +00:00
|
|
|
PathLocks outputLocks(pathsFromOutPaths(outPaths));
|
2003-08-01 14:11:19 +00:00
|
|
|
|
|
|
|
/* Now check again whether there is a successor. This is because
|
|
|
|
another process may have started building in parallel. After
|
|
|
|
it has finished and released the locks, we can (and should)
|
|
|
|
reuse its results. (Strictly speaking the first successor
|
|
|
|
check above can be omitted, but that would be less efficient.)
|
|
|
|
Note that since we now hold the locks on the output paths, no
|
|
|
|
other process can build this expression, so no further checks
|
|
|
|
are necessary. */
|
|
|
|
{
|
|
|
|
FSId id2 = useSuccessor(id);
|
|
|
|
if (id2 != id) {
|
|
|
|
FState fs = parseFState(termFromId(id2));
|
|
|
|
debug(format("skipping build of %1%, someone beat us to it")
|
|
|
|
% (string) id);
|
|
|
|
if (fs.type != FState::fsSlice) abort();
|
|
|
|
return id2;
|
|
|
|
}
|
|
|
|
}
|
2003-07-20 19:29:38 +00:00
|
|
|
|
|
|
|
/* Right platform? */
|
|
|
|
if (fs.derive.platform != thisSystem)
|
|
|
|
throw Error(format("a `%1%' is required, but I am a `%2%'")
|
|
|
|
% fs.derive.platform % thisSystem);
|
|
|
|
|
|
|
|
/* Realise inputs (and remember all input paths). */
|
|
|
|
for (FSIds::iterator i = fs.derive.inputs.begin();
|
|
|
|
i != fs.derive.inputs.end(); i++) {
|
2003-07-29 09:45:03 +00:00
|
|
|
FSId nf = normaliseFState(*i, pending);
|
|
|
|
realiseSlice(nf, pending);
|
2003-08-01 14:11:19 +00:00
|
|
|
/* !!! nf should be a root of the garbage collector while we
|
|
|
|
are building */
|
2003-07-29 09:45:03 +00:00
|
|
|
FState fs = parseFState(termFromId(nf));
|
|
|
|
if (fs.type != FState::fsSlice) abort();
|
|
|
|
for (SliceElems::iterator j = fs.slice.elems.begin();
|
|
|
|
j != fs.slice.elems.end(); j++)
|
2003-07-20 19:29:38 +00:00
|
|
|
inMap[j->path] = *j;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ElemMap::iterator i = inMap.begin(); i != inMap.end(); i++)
|
2003-08-01 14:11:19 +00:00
|
|
|
refPaths.push_back(i->second.path);
|
2003-07-20 19:29:38 +00:00
|
|
|
|
|
|
|
/* Build the environment. */
|
|
|
|
for (StringPairs::iterator i = fs.derive.env.begin();
|
|
|
|
i != fs.derive.env.end(); i++)
|
|
|
|
env[i->first] = i->second;
|
|
|
|
|
|
|
|
/* We can skip running the builder if we can expand all output
|
|
|
|
paths from their ids. */
|
2003-07-21 20:07:12 +00:00
|
|
|
bool fastBuild = true;
|
2003-08-01 14:11:19 +00:00
|
|
|
for (OutPaths::iterator i = outPaths.begin();
|
2003-07-20 19:29:38 +00:00
|
|
|
i != outPaths.end(); i++)
|
|
|
|
{
|
|
|
|
try {
|
2003-07-22 15:15:15 +00:00
|
|
|
expandId(i->second, i->first, "/", pending);
|
2003-07-21 20:07:12 +00:00
|
|
|
} catch (Error & e) {
|
2003-07-24 15:03:36 +00:00
|
|
|
debug(format("fast build failed for `%1%': %2%")
|
2003-08-01 15:41:47 +00:00
|
|
|
% i->first % e.what());
|
2003-07-20 19:29:38 +00:00
|
|
|
fastBuild = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fastBuild) {
|
|
|
|
|
|
|
|
/* Check that none of the outputs exist. */
|
|
|
|
for (OutPaths::iterator i = outPaths.begin();
|
|
|
|
i != outPaths.end(); i++)
|
|
|
|
if (pathExists(i->first))
|
|
|
|
throw Error(format("path `%1%' exists") % i->first);
|
|
|
|
|
|
|
|
/* Run the builder. */
|
2003-07-24 13:43:16 +00:00
|
|
|
msg(lvlChatty, format("building..."));
|
2003-07-20 19:29:38 +00:00
|
|
|
runProgram(fs.derive.builder, env);
|
2003-07-24 13:43:16 +00:00
|
|
|
msg(lvlChatty, format("build completed"));
|
2003-07-20 19:29:38 +00:00
|
|
|
|
|
|
|
} else
|
2003-07-24 13:43:16 +00:00
|
|
|
msg(lvlChatty, format("fast build succesful"));
|
2003-07-20 19:29:38 +00:00
|
|
|
|
2003-08-01 15:41:47 +00:00
|
|
|
/* Check whether the output paths were created, and grep each
|
|
|
|
output path to determine what other paths it references. */
|
2003-07-20 19:29:38 +00:00
|
|
|
FSIdSet used;
|
|
|
|
for (OutPaths::iterator i = outPaths.begin();
|
|
|
|
i != outPaths.end(); i++)
|
|
|
|
{
|
|
|
|
string path = i->first;
|
|
|
|
if (!pathExists(path))
|
|
|
|
throw Error(format("path `%1%' does not exist") % path);
|
|
|
|
fs.slice.roots.push_back(i->second);
|
|
|
|
|
2003-08-01 14:11:19 +00:00
|
|
|
Strings refs = filterReferences(path, refPaths);
|
2003-07-20 19:29:38 +00:00
|
|
|
|
|
|
|
SliceElem elem;
|
|
|
|
elem.path = path;
|
|
|
|
elem.id = i->second;
|
|
|
|
|
|
|
|
for (Strings::iterator j = refs.begin(); j != refs.end(); j++) {
|
|
|
|
ElemMap::iterator k;
|
|
|
|
OutPaths::iterator l;
|
|
|
|
if ((k = inMap.find(*j)) != inMap.end()) {
|
|
|
|
elem.refs.push_back(k->second.id);
|
|
|
|
used.insert(k->second.id);
|
|
|
|
for (FSIds::iterator m = k->second.refs.begin();
|
|
|
|
m != k->second.refs.end(); m++)
|
|
|
|
used.insert(*m);
|
|
|
|
} else if ((l = outPaths.find(*j)) != outPaths.end()) {
|
|
|
|
elem.refs.push_back(l->second);
|
|
|
|
used.insert(l->second);
|
|
|
|
} else
|
|
|
|
throw Error(format("unknown referenced path `%1%'") % *j);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.slice.elems.push_back(elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ElemMap::iterator i = inMap.begin();
|
|
|
|
i != inMap.end(); i++)
|
|
|
|
{
|
|
|
|
FSIdSet::iterator j = used.find(i->second.id);
|
|
|
|
if (j == used.end())
|
|
|
|
debug(format("NOT referenced: `%1%'") % i->second.path);
|
|
|
|
else {
|
|
|
|
debug(format("referenced: `%1%'") % i->second.path);
|
|
|
|
fs.slice.elems.push_back(i->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-01 15:41:47 +00:00
|
|
|
/* Write the normal form. This does not have to occur in the
|
|
|
|
transaction below because writing terms is idem-potent. */
|
2003-07-20 19:29:38 +00:00
|
|
|
fs.type = FState::fsSlice;
|
|
|
|
ATerm nf = unparseFState(fs);
|
2003-07-24 13:43:16 +00:00
|
|
|
msg(lvlVomit, format("normal form: %1%") % printTerm(nf));
|
2003-08-01 15:41:47 +00:00
|
|
|
FSId idNF = writeTerm(nf, "-s-" + (string) id);
|
|
|
|
|
|
|
|
/* Register each outpat path, and register the normal form. This
|
|
|
|
is wrapped in one database transaction to ensure that if we
|
|
|
|
crash, either everything is registered or nothing is. This is
|
|
|
|
for recoverability: unregistered paths in the store can be
|
|
|
|
deleted arbitrarily, while registered paths can only be deleted
|
|
|
|
by running the garbage collector. */
|
|
|
|
Transaction txn(nixDB);
|
|
|
|
for (OutPaths::iterator i = outPaths.begin();
|
|
|
|
i != outPaths.end(); i++)
|
|
|
|
registerPath(txn, i->first, i->second);
|
|
|
|
registerSuccessor(txn, id, idNF);
|
|
|
|
txn.commit();
|
|
|
|
|
|
|
|
return idNF;
|
2003-07-20 19:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-29 09:45:03 +00:00
|
|
|
void realiseSlice(const FSId & id, FSIdSet pending)
|
2003-07-20 19:29:38 +00:00
|
|
|
{
|
2003-07-29 09:45:03 +00:00
|
|
|
Nest nest(lvlDebug,
|
|
|
|
format("realising slice %1%") % (string) id);
|
2003-07-20 19:29:38 +00:00
|
|
|
|
2003-07-29 09:45:03 +00:00
|
|
|
FState fs = parseFState(termFromId(id));
|
|
|
|
if (fs.type != FState::fsSlice)
|
|
|
|
throw Error(format("expected slice in %1%") % (string) id);
|
|
|
|
|
|
|
|
for (SliceElems::const_iterator i = fs.slice.elems.begin();
|
|
|
|
i != fs.slice.elems.end(); i++)
|
2003-07-20 19:29:38 +00:00
|
|
|
{
|
|
|
|
SliceElem elem = *i;
|
2003-07-22 15:15:15 +00:00
|
|
|
expandId(elem.id, elem.path, "/", pending);
|
2003-07-20 19:29:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-29 10:43:12 +00:00
|
|
|
Strings fstatePaths(const FSId & id)
|
2003-07-20 19:29:38 +00:00
|
|
|
{
|
|
|
|
Strings paths;
|
|
|
|
|
2003-07-29 10:43:12 +00:00
|
|
|
FState fs = parseFState(termFromId(id));
|
2003-07-20 19:29:38 +00:00
|
|
|
|
|
|
|
if (fs.type == FState::fsSlice) {
|
|
|
|
/* !!! fix complexity */
|
|
|
|
for (FSIds::const_iterator i = fs.slice.roots.begin();
|
|
|
|
i != fs.slice.roots.end(); i++)
|
|
|
|
for (SliceElems::const_iterator j = fs.slice.elems.begin();
|
|
|
|
j != fs.slice.elems.end(); j++)
|
|
|
|
if (*i == j->id) paths.push_back(j->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (fs.type == FState::fsDerive) {
|
|
|
|
for (DeriveOutputs::iterator i = fs.derive.outputs.begin();
|
|
|
|
i != fs.derive.outputs.end(); i++)
|
|
|
|
paths.push_back(i->first);
|
|
|
|
}
|
|
|
|
|
|
|
|
else abort();
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-29 14:28:17 +00:00
|
|
|
static void fstateRequisitesSet(const FSId & id,
|
|
|
|
bool includeExprs, bool includeSuccessors, StringSet & paths)
|
2003-07-29 10:43:12 +00:00
|
|
|
{
|
|
|
|
FState fs = parseFState(termFromId(id));
|
|
|
|
|
|
|
|
if (fs.type == FState::fsSlice) {
|
|
|
|
for (SliceElems::iterator i = fs.slice.elems.begin();
|
|
|
|
i != fs.slice.elems.end(); i++)
|
|
|
|
paths.insert(i->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (fs.type == FState::fsDerive) {
|
|
|
|
for (FSIds::iterator i = fs.derive.inputs.begin();
|
|
|
|
i != fs.derive.inputs.end(); i++)
|
2003-07-29 14:28:17 +00:00
|
|
|
fstateRequisitesSet(*i,
|
|
|
|
includeExprs, includeSuccessors, paths);
|
2003-07-29 10:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else abort();
|
2003-07-29 14:28:17 +00:00
|
|
|
|
|
|
|
if (includeExprs)
|
|
|
|
paths.insert(expandId(id));
|
|
|
|
|
|
|
|
string idSucc;
|
|
|
|
if (includeSuccessors &&
|
2003-07-31 13:47:13 +00:00
|
|
|
nixDB.queryString(noTxn, dbSuccessors, id, idSucc))
|
2003-07-29 14:28:17 +00:00
|
|
|
fstateRequisitesSet(parseHash(idSucc),
|
|
|
|
includeExprs, includeSuccessors, paths);
|
2003-07-29 10:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-29 14:28:17 +00:00
|
|
|
Strings fstateRequisites(const FSId & id,
|
|
|
|
bool includeExprs, bool includeSuccessors)
|
2003-07-20 19:29:38 +00:00
|
|
|
{
|
2003-07-29 10:43:12 +00:00
|
|
|
StringSet paths;
|
2003-07-29 14:28:17 +00:00
|
|
|
fstateRequisitesSet(id, includeExprs, includeSuccessors, paths);
|
2003-07-29 10:43:12 +00:00
|
|
|
return Strings(paths.begin(), paths.end());
|
2003-07-20 19:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-21 14:46:01 +00:00
|
|
|
FSIds findGenerators(const FSIds & _ids)
|
2003-07-20 19:29:38 +00:00
|
|
|
{
|
2003-07-21 14:46:01 +00:00
|
|
|
FSIdSet ids(_ids.begin(), _ids.end());
|
|
|
|
FSIds generators;
|
|
|
|
|
|
|
|
/* !!! hack; for performance, we just look at the rhs of successor
|
|
|
|
mappings, since we know that those are Nix expressions. */
|
|
|
|
|
|
|
|
Strings sucs;
|
2003-07-31 13:47:13 +00:00
|
|
|
nixDB.enumTable(noTxn, dbSuccessors, sucs);
|
2003-07-21 14:46:01 +00:00
|
|
|
|
|
|
|
for (Strings::iterator i = sucs.begin();
|
|
|
|
i != sucs.end(); i++)
|
|
|
|
{
|
|
|
|
string s;
|
2003-07-31 13:47:13 +00:00
|
|
|
if (!nixDB.queryString(noTxn, dbSuccessors, *i, s)) continue;
|
2003-07-21 14:46:01 +00:00
|
|
|
FSId id = parseHash(s);
|
|
|
|
|
|
|
|
FState fs;
|
|
|
|
try {
|
|
|
|
/* !!! should substitutes be used? */
|
|
|
|
fs = parseFState(termFromId(id));
|
|
|
|
} catch (...) { /* !!! only catch parse errors */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.type != FState::fsSlice) continue;
|
|
|
|
|
|
|
|
bool okay = true;
|
|
|
|
for (SliceElems::const_iterator i = fs.slice.elems.begin();
|
|
|
|
i != fs.slice.elems.end(); i++)
|
|
|
|
if (ids.find(i->id) == ids.end()) {
|
|
|
|
okay = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!okay) continue;
|
|
|
|
|
|
|
|
generators.push_back(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return generators;
|
2003-07-20 19:29:38 +00:00
|
|
|
}
|