nix-super/src/libstore/build/goal.hh

113 lines
2.7 KiB
C++
Raw Normal View History

2020-10-11 19:17:24 +03:00
#pragma once
2020-10-12 20:16:00 +03:00
#include "types.hh"
#include "store-api.hh"
#include "build-result.hh"
namespace nix {
/* Forward definition. */
2020-10-12 20:16:00 +03:00
struct Goal;
class Worker;
/* A pointer to a goal. */
2014-03-30 01:49:23 +02:00
typedef std::shared_ptr<Goal> GoalPtr;
typedef std::weak_ptr<Goal> WeakGoalPtr;
struct CompareGoalPtrs {
2017-12-11 20:05:14 +02:00
bool operator() (const GoalPtr & a, const GoalPtr & b) const;
};
/* Set of goals. */
2022-02-21 17:28:23 +02: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 16:59:18 +03: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-25 00:09:43 +02:00
size_t nrFailed = 0;
/* Number of substitution goals we are/were waiting for that
failed because there are no substituters. */
2022-03-25 00:09:43 +02:00
size_t nrNoSubstituters = 0;
/* Number of substitution goals we are/were waiting for that
2020-10-18 15:26:23 +03:00
failed because they had unsubstitutable references. */
2022-03-25 00:09:43 +02:00
size_t nrIncompleteClosure = 0;
/* Name of this goal for debugging purposes. */
std::string name;
/* Whether the goal is finished. */
2022-03-25 00:09:43 +02:00
ExitCode exitCode = ecBusy;
/* Build result. */
BuildResult buildResult;
/* Exception containing an error message, if any. */
std::optional<Error> ex;
2022-03-09 13:25:35 +02:00
Goal(Worker & worker, DerivedPath path)
: worker(worker)
, buildResult { .path = std::move(path) }
2022-03-25 00:09:43 +02: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 12:41:50 +03:00
{
abort();
}
void trace(const FormatOrString & fs);
std::string getName()
{
return name;
}
2012-07-27 16:59:18 +03: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 19:17:24 +03:00
void addToWeakGoals(WeakGoals & goals, GoalPtr p);
}