aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/build/create-derivation-and-realise-goal.cc
blob: b01042f0032f73ffb3b87205ce2f03574edd253b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "create-derivation-and-realise-goal.hh"
#include "worker.hh"

namespace nix {

CreateDerivationAndRealiseGoal::CreateDerivationAndRealiseGoal(ref<SingleDerivedPath> drvReq,
    const OutputsSpec & wantedOutputs, Worker & worker, BuildMode buildMode)
    : Goal(worker, DerivedPath::Built { .drvPath = drvReq, .outputs = wantedOutputs })
    , drvReq(drvReq)
    , wantedOutputs(wantedOutputs)
    , buildMode(buildMode)
{
    state = &CreateDerivationAndRealiseGoal::getDerivation;
    name = fmt(
        "outer obtaining drv from '%s' and then building outputs %s",
        drvReq->to_string(worker.store),
        std::visit(overloaded {
            [&](const OutputsSpec::All) -> std::string {
                return "* (all of them)";
            },
            [&](const OutputsSpec::Names os) {
                return concatStringsSep(", ", quoteStrings(os));
            },
        }, wantedOutputs.raw));
    trace("created outer");

    worker.updateProgress();
}


CreateDerivationAndRealiseGoal::~CreateDerivationAndRealiseGoal()
{
}


static StorePath pathPartOfReq(const SingleDerivedPath & req)
{
    return std::visit(overloaded {
        [&](const SingleDerivedPath::Opaque & bo) {
            return bo.path;
        },
        [&](const SingleDerivedPath::Built & bfd) {
            return pathPartOfReq(*bfd.drvPath);
        },
    }, req.raw());
}


std::string CreateDerivationAndRealiseGoal::key()
{
    /* Ensure that derivations get built in order of their name,
       i.e. a derivation named "aardvark" always comes before "baboon". And
       substitution goals and inner derivation goals always happen before
       derivation goals (due to "b$"). */
    return "c$" + std::string(pathPartOfReq(*drvReq).name()) + "$" + drvReq->to_string(worker.store);
}


void CreateDerivationAndRealiseGoal::timedOut(Error && ex)
{
}


void CreateDerivationAndRealiseGoal::work()
{
    (this->*state)();
}


void CreateDerivationAndRealiseGoal::addWantedOutputs(const OutputsSpec & outputs)
{
    /* If we already want all outputs, there is nothing to do. */
    auto newWanted = wantedOutputs.union_(outputs);
    bool needRestart = !newWanted.isSubsetOf(wantedOutputs);
    wantedOutputs = newWanted;

    if (!needRestart) return;

    if (!optDrvPath)
        // haven't started steps where the outputs matter yet
        return;
    worker.makeDerivationGoal(*optDrvPath, outputs, buildMode);
}


void CreateDerivationAndRealiseGoal::getDerivation()
{
    trace("outer init");

    /* The first thing to do is to make sure that the derivation
       exists.  If it doesn't, it may be created through a
       substitute. */
    if (auto optDrvPath = [this]() -> std::optional<StorePath> {
        if (buildMode != bmNormal) return std::nullopt;

        auto drvPath = StorePath::dummy;
        try {
            drvPath = resolveDerivedPath(worker.store, *drvReq);
        } catch (MissingRealisation) {
            return std::nullopt;
        }
        return worker.evalStore.isValidPath(drvPath) || worker.store.isValidPath(drvPath)
            ? std::optional { drvPath }
            : std::nullopt;
    }()) {
        trace(fmt("already have drv '%s' for '%s', can go straight to building",
            worker.store.printStorePath(*optDrvPath),
            drvReq->to_string(worker.store)));

        loadAndBuildDerivation();
    } else {
        trace("need to obtain drv we want to build");

        addWaitee(worker.makeGoal(DerivedPath::fromSingle(*drvReq)));

        state = &CreateDerivationAndRealiseGoal::loadAndBuildDerivation;
        if (waitees.empty()) work();
    }
}


void CreateDerivationAndRealiseGoal::loadAndBuildDerivation()
{
    trace("outer load and build derivation");

    if (nrFailed != 0) {
        amDone(ecFailed, Error("cannot build missing derivation '%s'", drvReq->to_string(worker.store)));
        return;
    }

    StorePath drvPath = resolveDerivedPath(worker.store, *drvReq);
    /* Build this step! */
    concreteDrvGoal = worker.makeDerivationGoal(drvPath, wantedOutputs, buildMode);
    addWaitee(upcast_goal(concreteDrvGoal));
    state = &CreateDerivationAndRealiseGoal::buildDone;
    optDrvPath = std::move(drvPath);
    if (waitees.empty()) work();
}


void CreateDerivationAndRealiseGoal::buildDone()
{
    trace("outer build done");

    buildResult = upcast_goal(concreteDrvGoal)->getBuildResult(DerivedPath::Built {
        .drvPath = drvReq,
        .outputs = wantedOutputs,
    });

    if (buildResult.success())
        amDone(ecSuccess);
    else
        amDone(ecFailed, Error("building '%s' failed", drvReq->to_string(worker.store)));
}


}