* Realisation of Derive(...) expressions.
This commit is contained in:
parent
3da9687854
commit
40b5936691
7 changed files with 193 additions and 165 deletions
167
src/eval.cc
167
src/eval.cc
|
@ -18,7 +18,7 @@ typedef map<string, string> Environment;
|
||||||
|
|
||||||
|
|
||||||
/* Return true iff the given path exists. */
|
/* Return true iff the given path exists. */
|
||||||
bool pathExists(string path)
|
bool pathExists(const string & path)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
@ -30,33 +30,15 @@ bool pathExists(string path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#if 0
|
/* Run a program. */
|
||||||
/* Compute a derived value by running a program. */
|
static void runProgram(const string & program, Environment env)
|
||||||
static Hash computeDerived(Hash sourceHash, string targetName,
|
|
||||||
string platform, Hash prog, Environment env)
|
|
||||||
{
|
{
|
||||||
string targetPath = nixValues + "/" +
|
|
||||||
(string) sourceHash + "-nf";
|
|
||||||
|
|
||||||
/* Check whether the target already exists. */
|
|
||||||
if (pathExists(targetPath))
|
|
||||||
throw Error("derived value in " + targetPath + " already exists");
|
|
||||||
|
|
||||||
/* Find the program corresponding to the hash `prog'. */
|
|
||||||
string progPath = queryValuePath(prog);
|
|
||||||
|
|
||||||
/* Finalize the environment. */
|
|
||||||
env["out"] = targetPath;
|
|
||||||
|
|
||||||
/* Create a log file. */
|
/* Create a log file. */
|
||||||
string logFileName =
|
string logFileName = nixLogDir + "/run.log";
|
||||||
nixLogDir + "/" + baseNameOf(targetPath) + ".log";
|
|
||||||
/* !!! auto-pclose on exit */
|
/* !!! auto-pclose on exit */
|
||||||
FILE * logFile = popen(("tee " + logFileName + " >&2").c_str(), "w"); /* !!! escaping */
|
FILE * logFile = popen(("tee " + logFileName + " >&2").c_str(), "w"); /* !!! escaping */
|
||||||
if (!logFile)
|
if (!logFile)
|
||||||
throw SysError("unable to create log file " + logFileName);
|
throw SysError(format("unable to create log file %1%") % logFileName);
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
/* Fork a child to build the package. */
|
/* Fork a child to build the package. */
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
@ -107,21 +89,20 @@ static Hash computeDerived(Hash sourceHash, string targetName,
|
||||||
|
|
||||||
/* Dup the log handle into stderr. */
|
/* Dup the log handle into stderr. */
|
||||||
if (dup2(fileno(logFile), STDERR_FILENO) == -1)
|
if (dup2(fileno(logFile), STDERR_FILENO) == -1)
|
||||||
throw Error("cannot pipe standard error into log file: " + string(strerror(errno)));
|
throw SysError("cannot pipe standard error into log file");
|
||||||
|
|
||||||
/* Dup stderr to stdin. */
|
/* Dup stderr to stdin. */
|
||||||
if (dup2(STDERR_FILENO, STDOUT_FILENO) == -1)
|
if (dup2(STDERR_FILENO, STDOUT_FILENO) == -1)
|
||||||
throw Error("cannot dup stderr into stdout");
|
throw SysError("cannot dup stderr into stdout");
|
||||||
|
|
||||||
/* Make the program executable. !!! hack. */
|
/* Make the program executable. !!! hack. */
|
||||||
if (chmod(progPath.c_str(), 0755))
|
if (chmod(program.c_str(), 0755))
|
||||||
throw Error("cannot make program executable");
|
throw SysError("cannot make program executable");
|
||||||
|
|
||||||
/* Execute the program. This should not return. */
|
/* Execute the program. This should not return. */
|
||||||
execle(progPath.c_str(), baseNameOf(progPath).c_str(), 0, env2);
|
execle(program.c_str(), baseNameOf(program).c_str(), 0, env2);
|
||||||
|
|
||||||
throw Error("unable to execute builder: " +
|
throw SysError(format("unable to execute %1%") % program);
|
||||||
string(strerror(errno)));
|
|
||||||
|
|
||||||
} catch (exception & e) {
|
} catch (exception & e) {
|
||||||
cerr << "build error: " << e.what() << endl;
|
cerr << "build error: " << e.what() << endl;
|
||||||
|
@ -144,44 +125,12 @@ static Hash computeDerived(Hash sourceHash, string targetName,
|
||||||
|
|
||||||
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
|
||||||
throw Error("unable to build package");
|
throw Error("unable to build package");
|
||||||
|
|
||||||
/* Check whether the result was created. */
|
|
||||||
if (!pathExists(targetPath))
|
|
||||||
throw Error("program " + progPath +
|
|
||||||
" failed to create a result in " + targetPath);
|
|
||||||
|
|
||||||
#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
|
|
||||||
|
|
||||||
} catch (exception &) {
|
|
||||||
// system(("rm -rf " + targetPath).c_str());
|
|
||||||
throw;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Hash the result. */
|
|
||||||
Hash targetHash = hashPath(targetPath);
|
|
||||||
|
|
||||||
/* Register targetHash -> targetPath. !!! this should be in
|
|
||||||
values.cc. */
|
|
||||||
setDB(nixDB, dbRefs, targetHash, targetName);
|
|
||||||
|
|
||||||
/* Register that targetHash was produced by evaluating
|
|
||||||
sourceHash; i.e., that targetHash is a normal form of
|
|
||||||
sourceHash. !!! this shouldn't be here */
|
|
||||||
setDB(nixDB, dbNFs, sourceHash, targetHash);
|
|
||||||
|
|
||||||
return targetHash;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/* Throw an exception if the given platform string is not supported by
|
/* Throw an exception if the given platform string is not supported by
|
||||||
the platform we are executing on. */
|
the platform we are executing on. */
|
||||||
static void checkPlatform(string platform)
|
static void checkPlatform(const string & platform)
|
||||||
{
|
{
|
||||||
if (platform != thisSystem)
|
if (platform != thisSystem)
|
||||||
throw Error(format("a `%1%' is required, but I am a `%2%'")
|
throw Error(format("a `%1%' is required, but I am a `%2%'")
|
||||||
|
@ -250,33 +199,39 @@ struct RStatus
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static void realise(RStatus & status, FState fs)
|
static FState realise(RStatus & status, FState fs)
|
||||||
{
|
{
|
||||||
char * s;
|
char * s1, * s2, * s3;
|
||||||
Content content;
|
Content content;
|
||||||
ATermList refs;
|
ATermList refs, ins, outs, bnds;
|
||||||
|
|
||||||
if (ATmatch(fs, "File(<str>, <term>, [<list>])", &s, &content, &refs)) {
|
if (ATmatch(fs, "File(<str>, <term>, [<list>])", &s1, &content, &refs)) {
|
||||||
string path(s);
|
string path(s1);
|
||||||
|
|
||||||
if (path[0] != '/') throw Error("absolute path expected: " + path);
|
if (path[0] != '/') throw Error("absolute path expected: " + path);
|
||||||
|
|
||||||
/* Realise referenced paths. */
|
/* Realise referenced paths. */
|
||||||
|
ATermList refs2 = ATempty;
|
||||||
while (!ATisEmpty(refs)) {
|
while (!ATisEmpty(refs)) {
|
||||||
realise(status, ATgetFirst(refs));
|
refs2 = ATappend(refs2, realise(status, ATgetFirst(refs)));
|
||||||
refs = ATgetNext(refs);
|
refs = ATgetNext(refs);
|
||||||
}
|
}
|
||||||
|
refs2 = ATreverse(refs2);
|
||||||
|
|
||||||
if (!ATmatch(content, "Hash(<str>)", &s))
|
if (!ATmatch(content, "Hash(<str>)", &s1))
|
||||||
throw badTerm("hash expected", content);
|
throw badTerm("hash expected", content);
|
||||||
Hash hash = parseHash(s);
|
Hash hash = parseHash(s1);
|
||||||
|
|
||||||
|
/* Normal form. */
|
||||||
|
ATerm nf = ATmake("File(<str>, <term>, <list>)",
|
||||||
|
path.c_str(), content, refs2);
|
||||||
|
|
||||||
/* Perhaps the path already exists and has the right hash? */
|
/* Perhaps the path already exists and has the right hash? */
|
||||||
if (pathExists(path)) {
|
if (pathExists(path)) {
|
||||||
if (hash == hashPath(path)) {
|
if (hash == hashPath(path)) {
|
||||||
debug(format("path %1% already has hash %2%")
|
debug(format("path %1% already has hash %2%")
|
||||||
% path % (string) hash);
|
% path % (string) hash);
|
||||||
return;
|
return nf;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw Error(format("path %1% exists, but does not have hash %2%")
|
throw Error(format("path %1% exists, but does not have hash %2%")
|
||||||
|
@ -286,19 +241,79 @@ static void realise(RStatus & status, FState fs)
|
||||||
/* Do we know a path with that hash? If so, copy it. */
|
/* Do we know a path with that hash? If so, copy it. */
|
||||||
string path2 = queryFromStore(hash);
|
string path2 = queryFromStore(hash);
|
||||||
copyFile(path2, path);
|
copyFile(path2, path);
|
||||||
|
|
||||||
|
return nf;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (ATmatch(fs, "Derive()")) {
|
else if (ATmatch(fs, "Derive(<str>, <str>, [<list>], <str>, [<list>])",
|
||||||
|
&s1, &s2, &ins, &s3, &bnds))
|
||||||
|
{
|
||||||
|
string platform(s1), builder(s2), outPath(s3);
|
||||||
|
|
||||||
|
checkPlatform(platform);
|
||||||
|
|
||||||
|
/* Realise inputs. */
|
||||||
|
ATermList ins2 = ATempty;
|
||||||
|
while (!ATisEmpty(ins)) {
|
||||||
|
ins2 = ATappend(ins2, realise(status, ATgetFirst(ins)));
|
||||||
|
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))
|
||||||
|
throw badTerm("string expected", bnd);
|
||||||
|
env[s1] = s2;
|
||||||
|
bnds = ATgetNext(bnds);
|
||||||
}
|
}
|
||||||
|
|
||||||
else throw badTerm("bad file system state expression", fs);
|
/* 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);
|
||||||
|
|
||||||
|
/* 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);
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
/* Register that targetHash was produced by evaluating
|
||||||
|
sourceHash; i.e., that targetHash is a normal form of
|
||||||
|
sourceHash. !!! this shouldn't be here */
|
||||||
|
setDB(nixDB, dbNFs, sourceHash, targetHash);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return ATmake("File(<str>, Hash(<str>), <list>)",
|
||||||
|
outPath.c_str(), ((string) outHash).c_str(), ins2);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw badTerm("bad file system state expression", fs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void realiseFState(FState fs)
|
FState realiseFState(FState fs)
|
||||||
{
|
{
|
||||||
RStatus status;
|
RStatus status;
|
||||||
realise(status, fs);
|
return realise(status, fs);
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ using namespace std;
|
||||||
self-referential. This prevents us from having to deal with
|
self-referential. This prevents us from having to deal with
|
||||||
cycles.
|
cycles.
|
||||||
|
|
||||||
Derive : String * Path * [FState] * [Path] * [(String, String)] -> [FState]
|
Derive : String * Path * [FState] * Path * [(String, String)] -> FState
|
||||||
|
|
||||||
Derive(platform, builder, ins, outs, env) specifies the creation of
|
Derive(platform, builder, ins, outs, env) specifies the creation of
|
||||||
new file objects (in paths declared by `outs') by the execution of
|
new file objects (in paths declared by `outs') by the execution of
|
||||||
|
@ -61,7 +61,7 @@ typedef ATerm Content;
|
||||||
|
|
||||||
|
|
||||||
/* Realise a $f$-normalised expression in the file system. */
|
/* Realise a $f$-normalised expression in the file system. */
|
||||||
void realiseFState(FState fs);
|
FState realiseFState(FState fs);
|
||||||
|
|
||||||
/* Return a canonical textual representation of an expression. */
|
/* Return a canonical textual representation of an expression. */
|
||||||
string printTerm(ATerm t);
|
string printTerm(ATerm t);
|
||||||
|
|
22
src/test.cc
22
src/test.cc
|
@ -13,7 +13,9 @@
|
||||||
|
|
||||||
void realise(FState fs)
|
void realise(FState fs)
|
||||||
{
|
{
|
||||||
realiseFState(fs);
|
cout << format("%1% => %2%\n")
|
||||||
|
% printTerm(fs)
|
||||||
|
% printTerm(realiseFState(fs));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -145,15 +147,25 @@ void runTests()
|
||||||
"File(<str>, Hash(<str>), [])",
|
"File(<str>, Hash(<str>), [])",
|
||||||
builder1fn.c_str(),
|
builder1fn.c_str(),
|
||||||
((string) builder1h).c_str());
|
((string) builder1h).c_str());
|
||||||
realiseFState(fs1);
|
realise(fs1);
|
||||||
realiseFState(fs1);
|
realise(fs1);
|
||||||
|
|
||||||
FState fs2 = ATmake(
|
FState fs2 = ATmake(
|
||||||
"File(<str>, Hash(<str>), [])",
|
"File(<str>, Hash(<str>), [])",
|
||||||
(builder1fn + "_bla").c_str(),
|
(builder1fn + "_bla").c_str(),
|
||||||
((string) builder1h).c_str());
|
((string) builder1h).c_str());
|
||||||
realiseFState(fs2);
|
realise(fs2);
|
||||||
realiseFState(fs2);
|
realise(fs2);
|
||||||
|
|
||||||
|
string out1fn = nixStore + "/hello.txt";
|
||||||
|
FState fs3 = ATmake(
|
||||||
|
"Derive(<str>, <str>, [<term>], <str>, [(\"out\", <str>)])",
|
||||||
|
thisSystem.c_str(),
|
||||||
|
builder1fn.c_str(),
|
||||||
|
fs1,
|
||||||
|
out1fn.c_str(),
|
||||||
|
out1fn.c_str());
|
||||||
|
realise(fs3);
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
Expr e1 = ATmake("Exec(Str(<str>), Hash(<str>), [])",
|
Expr e1 = ATmake("Exec(Str(<str>), Hash(<str>), [])",
|
||||||
|
|
11
src/util.cc
11
src/util.cc
|
@ -11,10 +11,15 @@
|
||||||
string thisSystem = SYSTEM;
|
string thisSystem = SYSTEM;
|
||||||
|
|
||||||
|
|
||||||
SysError::SysError(string msg)
|
Error::Error(const format & f)
|
||||||
|
{
|
||||||
|
err = f.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
SysError::SysError(const format & f)
|
||||||
|
: Error(format("%1%: %2%") % f.str() % strerror(errno))
|
||||||
{
|
{
|
||||||
char * sysMsg = strerror(errno);
|
|
||||||
err = msg + ": " + sysMsg;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -18,22 +18,21 @@ class Error : public exception
|
||||||
protected:
|
protected:
|
||||||
string err;
|
string err;
|
||||||
public:
|
public:
|
||||||
Error() { }
|
Error(const format & f);
|
||||||
Error(format f) { err = f.str(); }
|
~Error() throw () { };
|
||||||
~Error() throw () { }
|
|
||||||
const char * what() const throw () { return err.c_str(); }
|
const char * what() const throw () { return err.c_str(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
class SysError : public Error
|
class SysError : public Error
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SysError(string msg);
|
SysError(const format & f);
|
||||||
};
|
};
|
||||||
|
|
||||||
class UsageError : public Error
|
class UsageError : public Error
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UsageError(string _err) : Error(_err) { };
|
UsageError(const format & f) : Error(f) { };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -127,16 +127,13 @@ string fetchURL(string url)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
void deleteFromStore(Hash hash)
|
void deleteFromStore(const string & path)
|
||||||
{
|
{
|
||||||
string fn;
|
|
||||||
if (queryDB(nixDB, dbRefs, hash, fn)) {
|
|
||||||
string prefix = nixStore + "/";
|
string prefix = nixStore + "/";
|
||||||
if (string(fn, prefix.size()) != prefix)
|
if (string(path, 0, prefix.size()) != prefix)
|
||||||
throw Error("path " + fn + " is not in the store");
|
throw Error(format("path %1% is not in the store") % path);
|
||||||
deletePath(fn);
|
deletePath(path);
|
||||||
delDB(nixDB, dbRefs, hash);
|
// delDB(nixDB, dbRefs, hash);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ void copyFile(string src, string dst);
|
||||||
void addToStore(string srcPath, string & dstPath, Hash & hash);
|
void addToStore(string srcPath, string & dstPath, Hash & hash);
|
||||||
|
|
||||||
/* Delete a value from the nixStore directory. */
|
/* Delete a value from the nixStore directory. */
|
||||||
void deleteFromStore(Hash hash);
|
void deleteFromStore(const string & path);
|
||||||
|
|
||||||
/* !!! */
|
/* !!! */
|
||||||
string queryFromStore(Hash hash);
|
string queryFromStore(Hash hash);
|
||||||
|
|
Loading…
Reference in a new issue