2003-06-16 13:33:38 +00:00
|
|
|
#include <map>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
2003-06-27 09:55:31 +00:00
|
|
|
#include <fcntl.h>
|
2003-06-16 13:33:38 +00:00
|
|
|
|
|
|
|
#include "eval.hh"
|
|
|
|
#include "globals.hh"
|
|
|
|
#include "values.hh"
|
|
|
|
#include "db.hh"
|
|
|
|
|
|
|
|
|
|
|
|
/* A Unix environment is a mapping from strings to strings. */
|
|
|
|
typedef map<string, string> Environment;
|
|
|
|
|
|
|
|
|
|
|
|
/* Return true iff the given path exists. */
|
2003-06-27 14:56:12 +00:00
|
|
|
bool pathExists(const string & path)
|
2003-06-16 13:33:38 +00:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
struct stat st;
|
|
|
|
res = stat(path.c_str(), &st);
|
|
|
|
if (!res) return true;
|
|
|
|
if (errno != ENOENT)
|
2003-07-04 12:18:06 +00:00
|
|
|
throw SysError(format("getting status of %1%") % path);
|
2003-06-16 13:33:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
/* Run a program. */
|
|
|
|
static void runProgram(const string & program, Environment env)
|
2003-06-16 13:33:38 +00:00
|
|
|
{
|
|
|
|
/* Create a log file. */
|
2003-06-27 14:56:12 +00:00
|
|
|
string logFileName = nixLogDir + "/run.log";
|
2003-06-16 13:33:38 +00:00
|
|
|
/* !!! auto-pclose on exit */
|
|
|
|
FILE * logFile = popen(("tee " + logFileName + " >&2").c_str(), "w"); /* !!! escaping */
|
|
|
|
if (!logFile)
|
2003-06-27 14:56:12 +00:00
|
|
|
throw SysError(format("unable to create log file %1%") % logFileName);
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
/* Fork a child to build the package. */
|
|
|
|
pid_t pid;
|
|
|
|
switch (pid = fork()) {
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
case -1:
|
|
|
|
throw SysError("unable to fork");
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
case 0:
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
try { /* child */
|
2003-06-16 13:33:38 +00:00
|
|
|
|
|
|
|
#if 0
|
2003-06-27 14:56:12 +00:00
|
|
|
/* Try to use a prebuilt. */
|
|
|
|
string prebuiltHashS, prebuiltFile;
|
|
|
|
if (queryDB(nixDB, dbPrebuilts, hash, prebuiltHashS)) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
prebuiltFile = getFile(parseHash(prebuiltHashS));
|
|
|
|
} catch (Error e) {
|
|
|
|
cerr << "cannot obtain prebuilt (ignoring): " << e.what() << endl;
|
|
|
|
goto build;
|
|
|
|
}
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
cerr << "substituting prebuilt " << prebuiltFile << endl;
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
int res = system(("tar xfj " + prebuiltFile + " 1>&2").c_str()); // !!! escaping
|
|
|
|
if (WEXITSTATUS(res) != 0)
|
|
|
|
/* This is a fatal error, because path may now
|
|
|
|
have clobbered. */
|
|
|
|
throw Error("cannot unpack " + prebuiltFile);
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
_exit(0);
|
|
|
|
}
|
2003-06-16 13:33:38 +00:00
|
|
|
#endif
|
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
// build:
|
|
|
|
|
|
|
|
/* Fill in the environment. We don't bother freeing
|
|
|
|
the strings, since we'll exec or die soon
|
|
|
|
anyway. */
|
|
|
|
const char * env2[env.size() + 1];
|
|
|
|
int i = 0;
|
|
|
|
for (Environment::iterator it = env.begin();
|
|
|
|
it != env.end(); it++, i++)
|
|
|
|
env2[i] = (new string(it->first + "=" + it->second))->c_str();
|
|
|
|
env2[i] = 0;
|
|
|
|
|
|
|
|
/* Dup the log handle into stderr. */
|
|
|
|
if (dup2(fileno(logFile), STDERR_FILENO) == -1)
|
|
|
|
throw SysError("cannot pipe standard error into log file");
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
/* Dup stderr to stdin. */
|
|
|
|
if (dup2(STDERR_FILENO, STDOUT_FILENO) == -1)
|
|
|
|
throw SysError("cannot dup stderr into stdout");
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
/* Make the program executable. !!! hack. */
|
|
|
|
if (chmod(program.c_str(), 0755))
|
|
|
|
throw SysError("cannot make program executable");
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
/* Execute the program. This should not return. */
|
|
|
|
execle(program.c_str(), baseNameOf(program).c_str(), 0, env2);
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
throw SysError(format("unable to execute %1%") % program);
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
} catch (exception & e) {
|
2003-07-04 12:18:06 +00:00
|
|
|
cerr << format("build error: %1%\n") % e.what();
|
2003-07-04 15:42:03 +00:00
|
|
|
}
|
2003-06-27 14:56:12 +00:00
|
|
|
_exit(1);
|
2003-06-16 13:33:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
/* parent */
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
/* Close the logging pipe. Note that this should not cause
|
|
|
|
the logger to exit until builder exits (because the latter
|
|
|
|
has an open file handle to the former). */
|
|
|
|
pclose(logFile);
|
|
|
|
|
|
|
|
/* Wait for the child to finish. */
|
|
|
|
int status;
|
|
|
|
if (waitpid(pid, &status, 0) != pid)
|
|
|
|
throw Error("unable to wait for child");
|
|
|
|
|
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
|
|
|
|
throw Error("unable to build package");
|
2003-06-16 13:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Throw an exception if the given platform string is not supported by
|
|
|
|
the platform we are executing on. */
|
2003-06-27 14:56:12 +00:00
|
|
|
static void checkPlatform(const string & platform)
|
2003-06-16 13:33:38 +00:00
|
|
|
{
|
|
|
|
if (platform != thisSystem)
|
2003-06-27 13:55:12 +00:00
|
|
|
throw Error(format("a `%1%' is required, but I am a `%2%'")
|
|
|
|
% platform % thisSystem);
|
2003-06-16 13:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
string printTerm(ATerm t)
|
2003-06-17 13:37:44 +00:00
|
|
|
{
|
2003-06-27 13:55:12 +00:00
|
|
|
char * s = ATwriteToString(t);
|
2003-06-17 13:37:44 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-06 14:20:47 +00:00
|
|
|
Error badTerm(const format & f, ATerm t)
|
2003-06-16 13:33:38 +00:00
|
|
|
{
|
2003-06-27 13:55:12 +00:00
|
|
|
return Error(format("%1%, in `%2%'") % f.str() % printTerm(t));
|
2003-06-16 13:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
Hash hashTerm(ATerm t)
|
2003-06-16 13:33:38 +00:00
|
|
|
{
|
2003-06-27 13:55:12 +00:00
|
|
|
return hashString(printTerm(t));
|
2003-06-16 13:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
struct RStatus
|
2003-06-27 09:55:31 +00:00
|
|
|
{
|
2003-06-27 13:55:12 +00:00
|
|
|
/* !!! the comparator of this hash should match the semantics of
|
|
|
|
the file system */
|
|
|
|
// map<string, Hash> paths;
|
|
|
|
};
|
2003-06-27 09:55:31 +00:00
|
|
|
|
|
|
|
|
2003-07-04 12:18:06 +00:00
|
|
|
static ATerm termFromHash(const Hash & hash)
|
|
|
|
{
|
|
|
|
string path = queryFromStore(hash);
|
|
|
|
ATerm t = ATreadFromNamedFile(path.c_str());
|
|
|
|
if (!t) throw Error(format("cannot read aterm %1%") % path);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-06 14:20:47 +00:00
|
|
|
Hash writeTerm(ATerm t)
|
2003-07-04 12:18:06 +00:00
|
|
|
{
|
|
|
|
string path = nixStore + "/tmp.nix"; /* !!! */
|
|
|
|
if (!ATwriteToNamedTextFile(t, path.c_str()))
|
|
|
|
throw Error(format("cannot write aterm %1%") % path);
|
|
|
|
Hash hash = hashPath(path);
|
|
|
|
string path2 = nixStore + "/" + (string) hash + ".nix";
|
|
|
|
if (rename(path.c_str(), path2.c_str()) == -1)
|
|
|
|
throw SysError(format("renaming %1% to %2%") % path % path2);
|
|
|
|
setDB(nixDB, dbRefs, hash, path2);
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
static FState realise(RStatus & status, FState fs)
|
2003-06-27 09:55:31 +00:00
|
|
|
{
|
2003-06-27 14:56:12 +00:00
|
|
|
char * s1, * s2, * s3;
|
2003-06-27 13:55:12 +00:00
|
|
|
Content content;
|
2003-07-04 12:18:06 +00:00
|
|
|
ATermList refs, ins, bnds;
|
|
|
|
|
|
|
|
/* First repeatedly try to substitute $fs$ by any known successors
|
|
|
|
in order to speed up the rewrite process. */
|
|
|
|
{
|
|
|
|
string fsHash, scHash;
|
|
|
|
while (queryDB(nixDB, dbSuccessors, fsHash = hashTerm(fs), scHash)) {
|
|
|
|
debug(format("successor %1% -> %2%") % (string) fsHash % scHash);
|
|
|
|
FState fs2 = termFromHash(parseHash(scHash));
|
|
|
|
if (fs == fs2) {
|
|
|
|
debug(format("successor cycle detected in %1%") % printTerm(fs));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fs = fs2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fall through. */
|
|
|
|
|
|
|
|
if (ATmatch(fs, "Include(<str>)", &s1)) {
|
|
|
|
return realise(status, termFromHash(parseHash(s1)));
|
|
|
|
}
|
2003-06-27 13:55:12 +00:00
|
|
|
|
2003-07-06 14:20:47 +00:00
|
|
|
else if (ATmatch(fs, "Path(<str>, <term>, [<list>])", &s1, &content, &refs)) {
|
2003-06-27 14:56:12 +00:00
|
|
|
string path(s1);
|
2003-06-27 09:55:31 +00:00
|
|
|
|
2003-07-04 12:18:06 +00:00
|
|
|
msg(format("realising atomic path %1%") % path);
|
|
|
|
Nest nest(true);
|
|
|
|
|
2003-07-06 14:20:47 +00:00
|
|
|
if (path[0] != '/')
|
|
|
|
throw Error(format("path `%1% is not absolute") % path);
|
2003-06-27 09:55:31 +00:00
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
/* Realise referenced paths. */
|
2003-06-27 14:56:12 +00:00
|
|
|
ATermList refs2 = ATempty;
|
2003-06-27 09:55:31 +00:00
|
|
|
while (!ATisEmpty(refs)) {
|
2003-07-06 14:20:47 +00:00
|
|
|
refs2 = ATinsert(refs2, realise(status, ATgetFirst(refs)));
|
2003-06-27 09:55:31 +00:00
|
|
|
refs = ATgetNext(refs);
|
|
|
|
}
|
2003-06-27 14:56:12 +00:00
|
|
|
refs2 = ATreverse(refs2);
|
2003-06-27 09:55:31 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
if (!ATmatch(content, "Hash(<str>)", &s1))
|
2003-06-27 13:55:12 +00:00
|
|
|
throw badTerm("hash expected", content);
|
2003-06-27 14:56:12 +00:00
|
|
|
Hash hash = parseHash(s1);
|
|
|
|
|
|
|
|
/* Normal form. */
|
2003-07-06 14:20:47 +00:00
|
|
|
ATerm nf = ATmake("Path(<str>, <term>, <term>)",
|
2003-06-27 14:56:12 +00:00
|
|
|
path.c_str(), content, refs2);
|
2003-06-27 09:55:31 +00:00
|
|
|
|
2003-07-04 12:18:06 +00:00
|
|
|
/* Register the normal form. */
|
|
|
|
if (fs != nf) {
|
|
|
|
Hash nfHash = writeTerm(nf);
|
|
|
|
setDB(nixDB, dbSuccessors, hashTerm(fs), nfHash);
|
|
|
|
}
|
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
/* Perhaps the path already exists and has the right hash? */
|
|
|
|
if (pathExists(path)) {
|
|
|
|
if (hash == hashPath(path)) {
|
|
|
|
debug(format("path %1% already has hash %2%")
|
|
|
|
% path % (string) hash);
|
2003-06-27 14:56:12 +00:00
|
|
|
return nf;
|
2003-06-27 13:55:12 +00:00
|
|
|
}
|
2003-06-27 09:55:31 +00:00
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
throw Error(format("path %1% exists, but does not have hash %2%")
|
|
|
|
% path % (string) hash);
|
|
|
|
}
|
2003-06-27 09:55:31 +00:00
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
/* Do we know a path with that hash? If so, copy it. */
|
|
|
|
string path2 = queryFromStore(hash);
|
2003-07-06 14:20:47 +00:00
|
|
|
copyPath(path2, path);
|
2003-06-27 14:56:12 +00:00
|
|
|
|
|
|
|
return nf;
|
2003-06-27 09:55:31 +00:00
|
|
|
}
|
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
else if (ATmatch(fs, "Derive(<str>, <str>, [<list>], <str>, [<list>])",
|
|
|
|
&s1, &s2, &ins, &s3, &bnds))
|
|
|
|
{
|
|
|
|
string platform(s1), builder(s2), outPath(s3);
|
|
|
|
|
2003-07-04 12:18:06 +00:00
|
|
|
msg(format("realising derivate path %1%") % outPath);
|
|
|
|
Nest nest(true);
|
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
checkPlatform(platform);
|
2003-06-27 13:55:12 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
/* Realise inputs. */
|
|
|
|
ATermList ins2 = ATempty;
|
|
|
|
while (!ATisEmpty(ins)) {
|
2003-07-06 14:20:47 +00:00
|
|
|
ins2 = ATinsert(ins2, realise(status, ATgetFirst(ins)));
|
2003-06-27 14:56:12 +00:00
|
|
|
ins = ATgetNext(ins);
|
|
|
|
}
|
|
|
|
ins2 = ATreverse(ins2);
|
|
|
|
|
|
|
|
/* Build the environment. */
|
|
|
|
Environment env;
|
|
|
|
while (!ATisEmpty(bnds)) {
|
|
|
|
ATerm bnd = ATgetFirst(bnds);
|
|
|
|
if (!ATmatch(bnd, "(<str>, <str>)", &s1, &s2))
|
2003-07-06 14:20:47 +00:00
|
|
|
throw badTerm("tuple of strings expected", bnd);
|
2003-06-27 14:56:12 +00:00
|
|
|
env[s1] = s2;
|
|
|
|
bnds = ATgetNext(bnds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check whether the target already exists. */
|
|
|
|
if (pathExists(outPath))
|
|
|
|
deleteFromStore(outPath);
|
|
|
|
// throw Error(format("path %1% already exists") % outPath);
|
|
|
|
|
|
|
|
/* Run the builder. */
|
|
|
|
runProgram(builder, env);
|
2003-06-27 13:55:12 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
/* Check whether the result was created. */
|
|
|
|
if (!pathExists(outPath))
|
|
|
|
throw Error(format("program %1% failed to create a result in %2%")
|
|
|
|
% builder % outPath);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* Remove write permission from the value. */
|
|
|
|
int res = system(("chmod -R -w " + targetPath).c_str()); // !!! escaping
|
|
|
|
if (WEXITSTATUS(res) != 0)
|
|
|
|
throw Error("cannot remove write permission from " + targetPath);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Hash the result. */
|
|
|
|
Hash outHash = hashPath(outPath);
|
|
|
|
|
|
|
|
/* Register targetHash -> targetPath. !!! this should be in
|
|
|
|
values.cc. */
|
|
|
|
setDB(nixDB, dbRefs, outHash, outPath);
|
|
|
|
|
2003-07-04 12:18:06 +00:00
|
|
|
/* Register the normal form of fs. */
|
2003-07-06 14:20:47 +00:00
|
|
|
FState nf = ATmake("Path(<str>, Hash(<str>), <term>)",
|
2003-06-27 14:56:12 +00:00
|
|
|
outPath.c_str(), ((string) outHash).c_str(), ins2);
|
2003-07-04 12:18:06 +00:00
|
|
|
Hash nfHash = writeTerm(nf);
|
|
|
|
setDB(nixDB, dbSuccessors, hashTerm(fs), nfHash);
|
|
|
|
|
|
|
|
return nf;
|
2003-06-27 09:55:31 +00:00
|
|
|
}
|
2003-06-27 13:55:12 +00:00
|
|
|
|
2003-07-04 15:29:58 +00:00
|
|
|
throw badTerm("bad fstate expression", fs);
|
2003-06-27 09:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
FState realiseFState(FState fs)
|
2003-06-27 09:55:31 +00:00
|
|
|
{
|
|
|
|
RStatus status;
|
2003-06-27 14:56:12 +00:00
|
|
|
return realise(status, fs);
|
2003-06-27 09:55:31 +00:00
|
|
|
}
|