aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/derived-path.cc
blob: 3594b7570535a245f5259dbfdd056ff91396300c (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "derived-path.hh"
#include "store-api.hh"

#include <nlohmann/json.hpp>

#include <optional>

namespace nix {

#define CMP_ONE(CHILD_TYPE, MY_TYPE, FIELD, COMPARATOR) \
    bool MY_TYPE ::operator COMPARATOR (const MY_TYPE & other) const \
    { \
        const MY_TYPE* me = this; \
        auto fields1 = std::make_tuple<const CHILD_TYPE &, const FIELD_TYPE &>(*me->drvPath, me->FIELD); \
        me = &other; \
        auto fields2 = std::make_tuple<const CHILD_TYPE &, const FIELD_TYPE &>(*me->drvPath, me->FIELD); \
        return fields1 COMPARATOR fields2; \
    }
#define CMP(CHILD_TYPE, MY_TYPE, FIELD) \
    CMP_ONE(CHILD_TYPE, MY_TYPE, FIELD, ==) \
    CMP_ONE(CHILD_TYPE, MY_TYPE, FIELD, !=) \
    CMP_ONE(CHILD_TYPE, MY_TYPE, FIELD, <)

#define FIELD_TYPE std::string
CMP(SingleDerivedPath, SingleDerivedPathBuilt, output)
#undef FIELD_TYPE

#define FIELD_TYPE OutputsSpec
CMP(SingleDerivedPath, DerivedPathBuilt, outputs)
#undef FIELD_TYPE

#undef CMP
#undef CMP_ONE

nlohmann::json DerivedPath::Opaque::toJSON(const Store & store) const
{
    return store.printStorePath(path);
}

nlohmann::json SingleDerivedPath::Built::toJSON(Store & store) const {
    nlohmann::json res;
    res["drvPath"] = drvPath->toJSON(store);
    // Fallback for the input-addressed derivation case: We expect to always be
    // able to print the output paths, so let’s do it
    // FIXME try-resolve on drvPath
    const auto outputMap = store.queryPartialDerivationOutputMap(resolveDerivedPath(store, *drvPath));
    res["output"] = output;
    auto outputPathIter = outputMap.find(output);
    if (outputPathIter == outputMap.end())
        res["outputPath"] = nullptr;
    else if (std::optional p = outputPathIter->second)
        res["outputPath"] = store.printStorePath(*p);
    else
        res["outputPath"] = nullptr;
    return res;
}

nlohmann::json DerivedPath::Built::toJSON(Store & store) const {
    nlohmann::json res;
    res["drvPath"] = drvPath->toJSON(store);
    // Fallback for the input-addressed derivation case: We expect to always be
    // able to print the output paths, so let’s do it
    // FIXME try-resolve on drvPath
    const auto outputMap = store.queryPartialDerivationOutputMap(resolveDerivedPath(store, *drvPath));
    for (const auto & [output, outputPathOpt] : outputMap) {
        if (!outputs.contains(output)) continue;
        if (outputPathOpt)
            res["outputs"][output] = store.printStorePath(*outputPathOpt);
        else
            res["outputs"][output] = nullptr;
    }
    return res;
}

nlohmann::json SingleDerivedPath::toJSON(Store & store) const
{
    return std::visit([&](const auto & buildable) {
        return buildable.toJSON(store);
    }, raw());
}

nlohmann::json DerivedPath::toJSON(Store & store) const
{
    return std::visit([&](const auto & buildable) {
        return buildable.toJSON(store);
    }, raw());
}

std::string DerivedPath::Opaque::to_string(const Store & store) const
{
    return store.printStorePath(path);
}

std::string SingleDerivedPath::Built::to_string(const Store & store) const
{
    return drvPath->to_string(store) + "^" + output;
}

std::string SingleDerivedPath::Built::to_string_legacy(const Store & store) const
{
    return drvPath->to_string(store) + "!" + output;
}

std::string DerivedPath::Built::to_string(const Store & store) const
{
    return drvPath->to_string(store)
        + '^'
        + outputs.to_string();
}

std::string DerivedPath::Built::to_string_legacy(const Store & store) const
{
    return drvPath->to_string_legacy(store)
        + "!"
        + outputs.to_string();
}

std::string SingleDerivedPath::to_string(const Store & store) const
{
    return std::visit(
        [&](const auto & req) { return req.to_string(store); },
        raw());
}

std::string DerivedPath::to_string(const Store & store) const
{
    return std::visit(
        [&](const auto & req) { return req.to_string(store); },
        raw());
}

std::string SingleDerivedPath::to_string_legacy(const Store & store) const
{
    return std::visit(overloaded {
        [&](const SingleDerivedPath::Built & req) { return req.to_string_legacy(store); },
        [&](const SingleDerivedPath::Opaque & req) { return req.to_string(store); },
    }, this->raw());
}

std::string DerivedPath::to_string_legacy(const Store & store) const
{
    return std::visit(overloaded {
        [&](const DerivedPath::Built & req) { return req.to_string_legacy(store); },
        [&](const DerivedPath::Opaque & req) { return req.to_string(store); },
    }, this->raw());
}


DerivedPath::Opaque DerivedPath::Opaque::parse(const Store & store, std::string_view s)
{
    return {store.parseStorePath(s)};
}

void drvRequireExperiment(
    const SingleDerivedPath & drv,
    const ExperimentalFeatureSettings & xpSettings)
{
    std::visit(overloaded {
        [&](const SingleDerivedPath::Opaque &) {
            // plain drv path; no experimental features required.
        },
        [&](const SingleDerivedPath::Built &) {
            xpSettings.require(Xp::DynamicDerivations);
        },
    }, drv.raw());
}

SingleDerivedPath::Built SingleDerivedPath::Built::parse(
    const Store & store, ref<SingleDerivedPath> drv,
    std::string_view output,
    const ExperimentalFeatureSettings & xpSettings)
{
    drvRequireExperiment(*drv, xpSettings);
    return {
        .drvPath = drv,
        .output = std::string { output },
    };
}

DerivedPath::Built DerivedPath::Built::parse(
    const Store & store, ref<SingleDerivedPath> drv,
    std::string_view outputsS,
    const ExperimentalFeatureSettings & xpSettings)
{
    drvRequireExperiment(*drv, xpSettings);
    return {
        .drvPath = drv,
        .outputs = OutputsSpec::parse(outputsS),
    };
}

static SingleDerivedPath parseWithSingle(
    const Store & store, std::string_view s, std::string_view separator,
    const ExperimentalFeatureSettings & xpSettings)
{
    size_t n = s.rfind(separator);
    return n == s.npos
        ? (SingleDerivedPath) SingleDerivedPath::Opaque::parse(store, s)
        : (SingleDerivedPath) SingleDerivedPath::Built::parse(store,
            make_ref<SingleDerivedPath>(parseWithSingle(
                store,
                s.substr(0, n),
                separator,
                xpSettings)),
            s.substr(n + 1),
            xpSettings);
}

SingleDerivedPath SingleDerivedPath::parse(
    const Store & store,
    std::string_view s,
    const ExperimentalFeatureSettings & xpSettings)
{
    return parseWithSingle(store, s, "^", xpSettings);
}

SingleDerivedPath SingleDerivedPath::parseLegacy(
    const Store & store,
    std::string_view s,
    const ExperimentalFeatureSettings & xpSettings)
{
    return parseWithSingle(store, s, "!", xpSettings);
}

static DerivedPath parseWith(
    const Store & store, std::string_view s, std::string_view separator,
    const ExperimentalFeatureSettings & xpSettings)
{
    size_t n = s.rfind(separator);
    return n == s.npos
        ? (DerivedPath) DerivedPath::Opaque::parse(store, s)
        : (DerivedPath) DerivedPath::Built::parse(store,
            make_ref<SingleDerivedPath>(parseWithSingle(
                store,
                s.substr(0, n),
                separator,
                xpSettings)),
            s.substr(n + 1),
            xpSettings);
}

DerivedPath DerivedPath::parse(
    const Store & store,
    std::string_view s,
    const ExperimentalFeatureSettings & xpSettings)
{
    return parseWith(store, s, "^", xpSettings);
}

DerivedPath DerivedPath::parseLegacy(
    const Store & store,
    std::string_view s,
    const ExperimentalFeatureSettings & xpSettings)
{
    return parseWith(store, s, "!", xpSettings);
}

DerivedPath DerivedPath::fromSingle(const SingleDerivedPath & req)
{
    return std::visit(overloaded {
        [&](const SingleDerivedPath::Opaque & o) -> DerivedPath {
            return o;
        },
        [&](const SingleDerivedPath::Built & b) -> DerivedPath {
            return DerivedPath::Built {
                .drvPath = b.drvPath,
                .outputs = OutputsSpec::Names { b.output },
            };
        },
    }, req.raw());
}

const StorePath & SingleDerivedPath::Built::getBaseStorePath() const
{
	return drvPath->getBaseStorePath();
}

const StorePath & DerivedPath::Built::getBaseStorePath() const
{
	return drvPath->getBaseStorePath();
}

template<typename DP>
static inline const StorePath & getBaseStorePath_(const DP & derivedPath)
{
    return std::visit(overloaded {
        [&](const typename DP::Built & bfd) -> auto & {
            return bfd.drvPath->getBaseStorePath();
        },
        [&](const typename DP::Opaque & bo) -> auto & {
            return bo.path;
        },
    }, derivedPath.raw());
}

const StorePath & SingleDerivedPath::getBaseStorePath() const
{
	return getBaseStorePath_(*this);
}

const StorePath & DerivedPath::getBaseStorePath() const
{
	return getBaseStorePath_(*this);
}

}