aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/build/entry-points.cc
blob: a0f18a02c067b50fd1bda7ffe8c47f8eadca2aa0 (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 "worker.hh"
#include "substitution-goal.hh"
#include "derivation-goal.hh"
#include "local-store.hh"
#include "strings.hh"

namespace nix {

static auto runWorker(Worker & worker, auto mkGoals)
{
    return worker.run(mkGoals);
}

void Store::buildPaths(const std::vector<DerivedPath> & reqs, BuildMode buildMode, std::shared_ptr<Store> evalStore)
{
    auto aio = kj::setupAsyncIo();
    Worker worker(*this, evalStore ? *evalStore : *this, aio);

    auto goals = runWorker(worker, [&](GoalFactory & gf) {
        Goals goals;
        for (auto & br : reqs)
            goals.insert(gf.makeGoal(br, buildMode));
        return goals;
    });

    StringSet failed;
    std::shared_ptr<Error> ex;
    for (auto & i : goals) {
        if (i->ex) {
            if (ex)
                logError(i->ex->info());
            else
                ex = i->ex;
        }
        if (i->exitCode != Goal::ecSuccess) {
            if (auto i2 = dynamic_cast<DerivationGoal *>(i.get()))
                failed.insert(printStorePath(i2->drvPath));
            else if (auto i2 = dynamic_cast<PathSubstitutionGoal *>(i.get()))
                failed.insert(printStorePath(i2->storePath));
        }
    }

    if (failed.size() == 1 && ex) {
        ex->withExitStatus(worker.failingExitStatus());
        throw std::move(*ex);
    } else if (!failed.empty()) {
        if (ex) logError(ex->info());
        throw Error(worker.failingExitStatus(), "build of %s failed", concatStringsSep(", ", quoteStrings(failed)));
    }
}

std::vector<KeyedBuildResult> Store::buildPathsWithResults(
    const std::vector<DerivedPath> & reqs,
    BuildMode buildMode,
    std::shared_ptr<Store> evalStore)
{
    auto aio = kj::setupAsyncIo();
    Worker worker(*this, evalStore ? *evalStore : *this, aio);

    std::vector<std::pair<const DerivedPath &, GoalPtr>> state;

    auto goals = runWorker(worker, [&](GoalFactory & gf) {
        Goals goals;
        for (const auto & req : reqs) {
            auto goal = gf.makeGoal(req, buildMode);
            goals.insert(goal);
            state.push_back({req, goal});
        }
        return goals;
    });

    std::vector<KeyedBuildResult> results;

    for (auto & [req, goalPtr] : state)
        results.emplace_back(goalPtr->buildResult.restrictTo(req));

    return results;
}

BuildResult Store::buildDerivation(const StorePath & drvPath, const BasicDerivation & drv,
    BuildMode buildMode)
{
    auto aio = kj::setupAsyncIo();
    Worker worker(*this, *this, aio);

    try {
        auto goals = runWorker(worker, [&](GoalFactory & gf) -> Goals {
            return Goals{gf.makeBasicDerivationGoal(drvPath, drv, OutputsSpec::All{}, buildMode)};
        });
        auto goal = *goals.begin();
        return goal->buildResult.restrictTo(DerivedPath::Built {
            .drvPath = makeConstantStorePathRef(drvPath),
            .outputs = OutputsSpec::All {},
        });
    } catch (Error & e) {
        return BuildResult {
            .status = BuildResult::MiscFailure,
            .errorMsg = e.msg(),
        };
    };
}


void Store::ensurePath(const StorePath & path)
{
    /* If the path is already valid, we're done. */
    if (isValidPath(path)) return;

    auto aio = kj::setupAsyncIo();
    Worker worker(*this, *this, aio);

    auto goals = runWorker(worker, [&](GoalFactory & gf) {
        return Goals{gf.makePathSubstitutionGoal(path)};
    });
    auto goal = *goals.begin();

    if (goal->exitCode != Goal::ecSuccess) {
        if (goal->ex) {
            goal->ex->withExitStatus(worker.failingExitStatus());
            throw std::move(*goal->ex);
        } else
            throw Error(worker.failingExitStatus(), "path '%s' does not exist and cannot be created", printStorePath(path));
    }
}


void Store::repairPath(const StorePath & path)
{
    auto aio = kj::setupAsyncIo();
    Worker worker(*this, *this, aio);

    auto goals = runWorker(worker, [&](GoalFactory & gf) {
        return Goals{gf.makePathSubstitutionGoal(path, Repair)};
    });
    auto goal = *goals.begin();

    if (goal->exitCode != Goal::ecSuccess) {
        /* Since substituting the path didn't work, if we have a valid
           deriver, then rebuild the deriver. */
        auto info = queryPathInfo(path);
        if (info->deriver && isValidPath(*info->deriver)) {
            worker.run([&](GoalFactory & gf) {
                return Goals{gf.makeGoal(
                    DerivedPath::Built{
                        .drvPath = makeConstantStorePathRef(*info->deriver),
                        // FIXME: Should just build the specific output we need.
                        .outputs = OutputsSpec::All{},
                    },
                    bmRepair
                )};
            });
        } else
            throw Error(worker.failingExitStatus(), "cannot repair path '%s'", printStorePath(path));
    }
}

}