lix/src/libstore/build/goal.hh

113 lines
2.7 KiB
C++
Raw Normal View History

2020-10-11 16:17:24 +00:00
#pragma once
2020-10-12 17:16:00 +00:00
#include "types.hh"
#include "store-api.hh"
#include "build-result.hh"
namespace nix {
/* Forward definition. */
2020-10-12 17:16:00 +00:00
struct Goal;
class Worker;
/* A pointer to a goal. */
2014-03-29 23:49:23 +00:00
typedef std::shared_ptr<Goal> GoalPtr;
typedef std::weak_ptr<Goal> WeakGoalPtr;
struct CompareGoalPtrs {
2017-12-11 18:05:14 +00:00
bool operator() (const GoalPtr & a, const GoalPtr & b) const;
};
/* Set of goals. */
2022-02-21 15:28:23 +00:00
typedef std::set<GoalPtr, CompareGoalPtrs> Goals;
typedef std::set<WeakGoalPtr, std::owner_less<WeakGoalPtr>> WeakGoals;
/* A map of paths to goals (and the other way around). */
typedef std::map<StorePath, WeakGoalPtr> WeakGoalMap;
struct Goal : public std::enable_shared_from_this<Goal>
{
typedef enum {ecBusy, ecSuccess, ecFailed, ecNoSubstituters, ecIncompleteClosure} ExitCode;
2012-07-27 13:59:18 +00:00
/* Backlink to the worker. */
Worker & worker;
/* Goals that this goal is waiting for. */
Goals waitees;
/* Goals waiting for this one to finish. Must use weak pointers
here to prevent cycles. */
WeakGoals waiters;
/* Number of goals we are/were waiting for that have failed. */
2022-03-24 22:09:43 +00:00
size_t nrFailed = 0;
/* Number of substitution goals we are/were waiting for that
failed because there are no substituters. */
2022-03-24 22:09:43 +00:00
size_t nrNoSubstituters = 0;
/* Number of substitution goals we are/were waiting for that
2020-10-18 12:26:23 +00:00
failed because they had unsubstitutable references. */
2022-03-24 22:09:43 +00:00
size_t nrIncompleteClosure = 0;
/* Name of this goal for debugging purposes. */
std::string name;
/* Whether the goal is finished. */
2022-03-24 22:09:43 +00:00
ExitCode exitCode = ecBusy;
/* Build result. */
BuildResult buildResult;
/* Exception containing an error message, if any. */
std::optional<Error> ex;
2022-03-09 11:25:35 +00:00
Goal(Worker & worker, DerivedPath path)
: worker(worker)
, buildResult { .path = std::move(path) }
2022-03-24 22:09:43 +00:00
{ }
virtual ~Goal()
{
trace("goal destroyed");
}
virtual void work() = 0;
void addWaitee(GoalPtr waitee);
virtual void waiteeDone(GoalPtr waitee, ExitCode result);
virtual void handleChildOutput(int fd, std::string_view data)
{
abort();
}
virtual void handleEOF(int fd)
2004-06-29 09:41:50 +00:00
{
abort();
}
void trace(const FormatOrString & fs);
std::string getName()
{
return name;
}
2012-07-27 13:59:18 +00:00
/* Callback in case of a timeout. It should wake up its waiters,
get rid of any running child processes that are being monitored
by the worker (important!), etc. */
virtual void timedOut(Error && ex) = 0;
virtual std::string key() = 0;
void amDone(ExitCode result, std::optional<Error> ex = {});
virtual void cleanup() { }
};
2020-10-11 16:17:24 +00:00
void addToWeakGoals(WeakGoals & goals, GoalPtr p);
}