2007-03-30 13:24:35 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
#include "shared.hh"
|
|
|
|
#include "globals.hh"
|
2006-11-30 17:43:04 +00:00
|
|
|
#include "store-api.hh"
|
2006-09-04 21:06:23 +00:00
|
|
|
#include "util.hh"
|
2008-08-04 13:44:46 +00:00
|
|
|
#include "misc.hh"
|
2006-09-04 21:06:23 +00:00
|
|
|
|
2003-07-04 15:42:03 +00:00
|
|
|
#include <iostream>
|
2003-07-24 08:53:43 +00:00
|
|
|
#include <cctype>
|
2007-05-01 15:16:17 +00:00
|
|
|
#include <exception>
|
2003-07-04 15:42:03 +00:00
|
|
|
|
2012-12-11 10:49:42 +00:00
|
|
|
#include <sys/time.h>
|
2004-03-27 17:58:04 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2010-10-28 12:29:40 +00:00
|
|
|
#if HAVE_BOEHMGC
|
|
|
|
#include <gc/gc.h>
|
|
|
|
#endif
|
|
|
|
|
2003-07-04 15:42:03 +00:00
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
namespace nix {
|
2003-07-04 15:42:03 +00:00
|
|
|
|
|
|
|
|
2004-05-11 18:05:44 +00:00
|
|
|
volatile sig_atomic_t blockInt = 0;
|
|
|
|
|
|
|
|
|
2006-03-10 22:27:26 +00:00
|
|
|
static void sigintHandler(int signo)
|
2004-01-15 20:23:55 +00:00
|
|
|
{
|
2004-05-11 18:05:44 +00:00
|
|
|
if (!blockInt) {
|
|
|
|
_isInterrupted = 1;
|
|
|
|
blockInt = 1;
|
|
|
|
}
|
2004-01-15 20:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-01 12:36:25 +00:00
|
|
|
void printGCWarning()
|
|
|
|
{
|
2006-08-29 15:40:49 +00:00
|
|
|
static bool haveWarned = false;
|
2012-07-30 21:13:25 +00:00
|
|
|
warnOnce(haveWarned,
|
2006-12-05 00:34:42 +00:00
|
|
|
"you did not specify `--add-root'; "
|
2006-08-29 15:40:49 +00:00
|
|
|
"the result might be removed by the garbage collector");
|
2005-02-01 12:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-31 21:11:50 +00:00
|
|
|
void printMissing(StoreAPI & store, const PathSet & paths)
|
2008-08-04 13:44:46 +00:00
|
|
|
{
|
2010-11-17 14:31:42 +00:00
|
|
|
unsigned long long downloadSize, narSize;
|
2008-08-04 13:44:46 +00:00
|
|
|
PathSet willBuild, willSubstitute, unknown;
|
2011-08-31 21:11:50 +00:00
|
|
|
queryMissing(store, paths, willBuild, willSubstitute, unknown, downloadSize, narSize);
|
2012-11-19 23:27:25 +00:00
|
|
|
printMissing(willBuild, willSubstitute, unknown, downloadSize, narSize);
|
|
|
|
}
|
|
|
|
|
2008-08-04 13:44:46 +00:00
|
|
|
|
2012-11-19 23:27:25 +00:00
|
|
|
void printMissing(const PathSet & willBuild,
|
|
|
|
const PathSet & willSubstitute, const PathSet & unknown,
|
|
|
|
unsigned long long downloadSize, unsigned long long narSize)
|
|
|
|
{
|
2008-08-04 13:44:46 +00:00
|
|
|
if (!willBuild.empty()) {
|
2010-11-17 14:31:42 +00:00
|
|
|
printMsg(lvlInfo, format("these derivations will be built:"));
|
2008-08-04 13:44:46 +00:00
|
|
|
foreach (PathSet::iterator, i, willBuild)
|
|
|
|
printMsg(lvlInfo, format(" %1%") % *i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!willSubstitute.empty()) {
|
2011-11-29 13:00:41 +00:00
|
|
|
printMsg(lvlInfo, format("these paths will be fetched (%.2f MiB download, %.2f MiB unpacked):")
|
2010-11-17 14:31:42 +00:00
|
|
|
% (downloadSize / (1024.0 * 1024.0))
|
|
|
|
% (narSize / (1024.0 * 1024.0)));
|
2008-08-04 13:44:46 +00:00
|
|
|
foreach (PathSet::iterator, i, willSubstitute)
|
|
|
|
printMsg(lvlInfo, format(" %1%") % *i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!unknown.empty()) {
|
2010-11-17 14:31:42 +00:00
|
|
|
printMsg(lvlInfo, format("don't know how to build these paths%1%:")
|
2012-07-30 23:55:41 +00:00
|
|
|
% (settings.readOnlyMode ? " (may be caused by read-only store access)" : ""));
|
2008-08-04 13:44:46 +00:00
|
|
|
foreach (PathSet::iterator, i, unknown)
|
|
|
|
printMsg(lvlInfo, format(" %1%") % *i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-10 22:27:26 +00:00
|
|
|
static void setLogType(string lt)
|
2004-03-22 20:53:49 +00:00
|
|
|
{
|
|
|
|
if (lt == "pretty") logType = ltPretty;
|
|
|
|
else if (lt == "escapes") logType = ltEscapes;
|
|
|
|
else if (lt == "flat") logType = ltFlat;
|
|
|
|
else throw UsageError("unknown log type");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-30 13:28:29 +00:00
|
|
|
static bool showTrace = false;
|
|
|
|
|
|
|
|
|
2012-07-30 23:55:41 +00:00
|
|
|
string getArg(const string & opt,
|
|
|
|
Strings::iterator & i, const Strings::iterator & end)
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
if (i == end) throw UsageError(format("`%1%' requires an argument") % opt);
|
|
|
|
return *i;
|
|
|
|
}
|
|
|
|
|
2012-10-03 20:37:06 +00:00
|
|
|
|
2003-07-04 15:42:03 +00:00
|
|
|
/* Initialize and reorder arguments, then call the actual argument
|
|
|
|
processor. */
|
|
|
|
static void initAndRun(int argc, char * * argv)
|
|
|
|
{
|
2012-07-30 23:55:41 +00:00
|
|
|
settings.processEnvironment();
|
|
|
|
settings.loadConfFile();
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2004-01-15 20:23:55 +00:00
|
|
|
/* Catch SIGINT. */
|
2010-01-12 12:22:38 +00:00
|
|
|
struct sigaction act;
|
2004-01-15 20:23:55 +00:00
|
|
|
act.sa_handler = sigintHandler;
|
|
|
|
sigfillset(&act.sa_mask);
|
|
|
|
act.sa_flags = 0;
|
2010-01-12 12:22:38 +00:00
|
|
|
if (sigaction(SIGINT, &act, 0))
|
2004-01-15 20:23:55 +00:00
|
|
|
throw SysError("installing handler for SIGINT");
|
2010-01-12 12:22:38 +00:00
|
|
|
if (sigaction(SIGTERM, &act, 0))
|
2005-11-04 15:34:09 +00:00
|
|
|
throw SysError("installing handler for SIGTERM");
|
2010-01-12 12:22:38 +00:00
|
|
|
if (sigaction(SIGHUP, &act, 0))
|
2005-11-04 15:34:09 +00:00
|
|
|
throw SysError("installing handler for SIGHUP");
|
2004-01-15 20:23:55 +00:00
|
|
|
|
2004-05-13 19:14:49 +00:00
|
|
|
/* Ignore SIGPIPE. */
|
|
|
|
act.sa_handler = SIG_IGN;
|
|
|
|
act.sa_flags = 0;
|
2010-01-12 12:22:38 +00:00
|
|
|
if (sigaction(SIGPIPE, &act, 0))
|
2004-05-13 19:14:49 +00:00
|
|
|
throw SysError("ignoring SIGPIPE");
|
|
|
|
|
2010-01-12 12:22:38 +00:00
|
|
|
/* Reset SIGCHLD to its default. */
|
|
|
|
act.sa_handler = SIG_DFL;
|
|
|
|
act.sa_flags = 0;
|
|
|
|
if (sigaction(SIGCHLD, &act, 0))
|
|
|
|
throw SysError("resetting SIGCHLD");
|
|
|
|
|
2004-09-09 14:16:02 +00:00
|
|
|
/* There is no privacy in the Nix system ;-) At least not for
|
|
|
|
now. In particular, store objects should be readable by
|
2012-10-03 21:30:45 +00:00
|
|
|
everybody. */
|
2004-09-09 14:16:02 +00:00
|
|
|
umask(0022);
|
|
|
|
|
2012-12-11 10:49:42 +00:00
|
|
|
/* Initialise the PRNG. */
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, 0);
|
|
|
|
srandom(tv.tv_usec);
|
|
|
|
|
2004-03-22 20:53:49 +00:00
|
|
|
/* Process the NIX_LOG_TYPE environment variable. */
|
2004-05-04 12:15:30 +00:00
|
|
|
string lt = getEnv("NIX_LOG_TYPE");
|
|
|
|
if (lt != "") setLogType(lt);
|
2004-03-22 20:53:49 +00:00
|
|
|
|
2003-07-04 15:42:03 +00:00
|
|
|
/* Put the arguments in a vector. */
|
2003-12-01 15:55:05 +00:00
|
|
|
Strings args, remaining;
|
2003-07-04 15:42:03 +00:00
|
|
|
while (argc--) args.push_back(*argv++);
|
|
|
|
args.erase(args.begin());
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2003-11-03 18:21:53 +00:00
|
|
|
/* Expand compound dash options (i.e., `-qlf' -> `-q -l -f'), and
|
|
|
|
ignore options for the ATerm library. */
|
2003-12-01 15:55:05 +00:00
|
|
|
for (Strings::iterator i = args.begin(); i != args.end(); ++i) {
|
|
|
|
string arg = *i;
|
2011-09-06 12:06:30 +00:00
|
|
|
if (arg.length() > 2 && arg[0] == '-' && arg[1] != '-' && !isdigit(arg[1])) {
|
2003-12-01 15:55:05 +00:00
|
|
|
for (unsigned int j = 1; j < arg.length(); j++)
|
|
|
|
if (isalpha(arg[j]))
|
|
|
|
remaining.push_back((string) "-" + arg[j]);
|
2011-09-06 12:06:30 +00:00
|
|
|
else {
|
2003-12-01 15:55:05 +00:00
|
|
|
remaining.push_back(string(arg, j));
|
2003-07-24 08:53:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-12-01 15:55:05 +00:00
|
|
|
} else remaining.push_back(arg);
|
2003-07-04 15:42:03 +00:00
|
|
|
}
|
2003-12-01 15:55:05 +00:00
|
|
|
args = remaining;
|
|
|
|
remaining.clear();
|
2003-07-04 15:42:03 +00:00
|
|
|
|
2003-12-01 15:55:05 +00:00
|
|
|
/* Process default options. */
|
2013-03-07 22:55:55 +00:00
|
|
|
int verbosityDelta = lvlInfo;
|
2003-12-01 15:55:05 +00:00
|
|
|
for (Strings::iterator i = args.begin(); i != args.end(); ++i) {
|
|
|
|
string arg = *i;
|
2013-03-07 22:55:55 +00:00
|
|
|
if (arg == "--verbose" || arg == "-v") verbosityDelta++;
|
|
|
|
else if (arg == "--quiet") verbosityDelta--;
|
2004-03-22 20:53:49 +00:00
|
|
|
else if (arg == "--log-type") {
|
2012-07-30 23:55:41 +00:00
|
|
|
string s = getArg(arg, i, args.end());
|
|
|
|
setLogType(s);
|
2004-05-12 14:20:32 +00:00
|
|
|
}
|
2004-08-18 12:19:06 +00:00
|
|
|
else if (arg == "--no-build-output" || arg == "-Q")
|
2012-07-30 23:55:41 +00:00
|
|
|
settings.buildVerbosity = lvlVomit;
|
2008-11-12 11:08:27 +00:00
|
|
|
else if (arg == "--print-build-trace")
|
2012-07-30 23:55:41 +00:00
|
|
|
settings.printBuildTrace = true;
|
2003-12-01 15:55:05 +00:00
|
|
|
else if (arg == "--help") {
|
|
|
|
printHelp();
|
|
|
|
return;
|
2004-05-12 14:20:32 +00:00
|
|
|
}
|
|
|
|
else if (arg == "--version") {
|
2012-11-27 12:29:55 +00:00
|
|
|
std::cout << format("%1% (Nix) %2%") % programId % nixVersion << std::endl;
|
2004-02-02 10:51:54 +00:00
|
|
|
return;
|
2004-05-12 14:20:32 +00:00
|
|
|
}
|
|
|
|
else if (arg == "--keep-failed" || arg == "-K")
|
2012-07-30 23:55:41 +00:00
|
|
|
settings.keepFailed = true;
|
2004-06-25 15:36:09 +00:00
|
|
|
else if (arg == "--keep-going" || arg == "-k")
|
2012-07-30 23:55:41 +00:00
|
|
|
settings.keepGoing = true;
|
2004-06-28 10:42:57 +00:00
|
|
|
else if (arg == "--fallback")
|
2012-07-31 21:56:02 +00:00
|
|
|
settings.set("build-fallback", "true");
|
2006-12-08 15:44:00 +00:00
|
|
|
else if (arg == "--max-jobs" || arg == "-j")
|
2012-07-30 23:55:41 +00:00
|
|
|
settings.set("build-max-jobs", getArg(arg, i, args.end()));
|
2010-08-04 12:23:59 +00:00
|
|
|
else if (arg == "--cores")
|
2012-07-30 23:55:41 +00:00
|
|
|
settings.set("build-cores", getArg(arg, i, args.end()));
|
2004-10-25 14:38:23 +00:00
|
|
|
else if (arg == "--readonly-mode")
|
2012-07-30 23:55:41 +00:00
|
|
|
settings.readOnlyMode = true;
|
2006-12-08 15:44:00 +00:00
|
|
|
else if (arg == "--max-silent-time")
|
2012-07-30 23:55:41 +00:00
|
|
|
settings.set("build-max-silent-time", getArg(arg, i, args.end()));
|
2011-06-30 15:19:13 +00:00
|
|
|
else if (arg == "--timeout")
|
2012-07-30 23:55:41 +00:00
|
|
|
settings.set("build-timeout", getArg(arg, i, args.end()));
|
2007-11-16 16:15:26 +00:00
|
|
|
else if (arg == "--no-build-hook")
|
2012-07-30 23:55:41 +00:00
|
|
|
settings.useBuildHook = false;
|
2009-06-30 13:28:29 +00:00
|
|
|
else if (arg == "--show-trace")
|
|
|
|
showTrace = true;
|
2009-02-27 11:04:41 +00:00
|
|
|
else if (arg == "--option") {
|
|
|
|
++i; if (i == args.end()) throw UsageError("`--option' requires two arguments");
|
|
|
|
string name = *i;
|
|
|
|
++i; if (i == args.end()) throw UsageError("`--option' requires two arguments");
|
|
|
|
string value = *i;
|
2012-07-30 23:55:41 +00:00
|
|
|
settings.set(name, value);
|
2009-02-27 11:04:41 +00:00
|
|
|
}
|
2003-12-01 15:55:05 +00:00
|
|
|
else remaining.push_back(arg);
|
|
|
|
}
|
|
|
|
|
2013-03-07 22:55:55 +00:00
|
|
|
verbosity = (Verbosity) (verbosityDelta < 0 ? 0 : verbosityDelta);
|
|
|
|
|
2012-07-30 23:55:41 +00:00
|
|
|
settings.update();
|
|
|
|
|
2003-12-01 15:55:05 +00:00
|
|
|
run(remaining);
|
2006-03-01 16:36:35 +00:00
|
|
|
|
2006-11-30 17:43:04 +00:00
|
|
|
/* Close the Nix database. */
|
|
|
|
store.reset((StoreAPI *) 0);
|
2003-07-04 15:42:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-28 12:29:40 +00:00
|
|
|
/* Called when the Boehm GC runs out of memory. */
|
|
|
|
static void * oomHandler(size_t requested)
|
|
|
|
{
|
|
|
|
/* Convert this to a proper C++ exception. */
|
|
|
|
throw std::bad_alloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-03 20:37:06 +00:00
|
|
|
void showManPage(const string & name)
|
|
|
|
{
|
|
|
|
string cmd = "man " + name;
|
|
|
|
if (system(cmd.c_str()) != 0)
|
|
|
|
throw Error(format("command `%1%' failed") % cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-06 12:06:30 +00:00
|
|
|
int exitCode = 0;
|
2012-03-05 18:19:29 +00:00
|
|
|
char * * argvSaved = 0;
|
2011-09-06 12:06:30 +00:00
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-16 16:29:57 +00:00
|
|
|
static char buf[1024];
|
|
|
|
|
2003-07-04 15:42:03 +00:00
|
|
|
int main(int argc, char * * argv)
|
|
|
|
{
|
2006-09-04 21:06:23 +00:00
|
|
|
using namespace nix;
|
2006-12-02 15:45:51 +00:00
|
|
|
|
2012-03-05 18:19:29 +00:00
|
|
|
argvSaved = argv;
|
|
|
|
|
2003-10-16 11:55:37 +00:00
|
|
|
/* Turn on buffering for cerr. */
|
2003-12-22 16:40:46 +00:00
|
|
|
#if HAVE_PUBSETBUF
|
2006-09-04 21:06:23 +00:00
|
|
|
std::cerr.rdbuf()->pubsetbuf(buf, sizeof(buf));
|
2003-12-22 16:40:46 +00:00
|
|
|
#endif
|
2003-10-16 11:55:37 +00:00
|
|
|
|
2006-11-18 18:56:30 +00:00
|
|
|
std::ios::sync_with_stdio(false);
|
|
|
|
|
2010-10-28 12:29:40 +00:00
|
|
|
#if HAVE_BOEHMGC
|
|
|
|
/* Initialise the Boehm garbage collector. This isn't necessary
|
|
|
|
on most platforms, but for portability we do it anyway. */
|
|
|
|
GC_INIT();
|
|
|
|
|
|
|
|
GC_oom_fn = oomHandler;
|
|
|
|
#endif
|
|
|
|
|
2003-07-04 15:42:03 +00:00
|
|
|
try {
|
2004-05-11 18:05:44 +00:00
|
|
|
try {
|
|
|
|
initAndRun(argc, argv);
|
|
|
|
} catch (...) {
|
|
|
|
/* Subtle: we have to make sure that any `interrupted'
|
|
|
|
condition is discharged before we reach printMsg()
|
|
|
|
below, since otherwise it will throw an (uncaught)
|
|
|
|
exception. */
|
|
|
|
blockInt = 1; /* ignore further SIGINTs */
|
|
|
|
_isInterrupted = 0;
|
|
|
|
throw;
|
|
|
|
}
|
2003-07-04 15:42:03 +00:00
|
|
|
} catch (UsageError & e) {
|
2012-07-30 21:13:25 +00:00
|
|
|
printMsg(lvlError,
|
2003-07-24 08:53:43 +00:00
|
|
|
format(
|
|
|
|
"error: %1%\n"
|
|
|
|
"Try `%2% --help' for more information.")
|
|
|
|
% e.what() % programId);
|
2003-07-04 15:42:03 +00:00
|
|
|
return 1;
|
2007-08-12 00:29:28 +00:00
|
|
|
} catch (BaseError & e) {
|
2009-06-30 13:28:29 +00:00
|
|
|
printMsg(lvlError, format("error: %1%%2%") % (showTrace ? e.prefix() : "") % e.msg());
|
|
|
|
if (e.prefix() != "" && !showTrace)
|
|
|
|
printMsg(lvlError, "(use `--show-trace' to show detailed location information)");
|
2010-12-13 16:53:23 +00:00
|
|
|
return e.status;
|
2006-09-04 21:06:23 +00:00
|
|
|
} catch (std::exception & e) {
|
2003-11-09 10:35:45 +00:00
|
|
|
printMsg(lvlError, format("error: %1%") % e.what());
|
2003-07-04 15:42:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-09-06 12:06:30 +00:00
|
|
|
return exitCode;
|
2003-07-04 15:42:03 +00:00
|
|
|
}
|