Skip to content

Commit 6d9f1a8

Browse files
authored
Merge pull request #8691 from obsidiansystems/built-path
Move `BuiltPath` to its own header/C++ file in libcmd
2 parents bbc08a1 + 2c3fb0e commit 6d9f1a8

File tree

5 files changed

+115
-97
lines changed

5 files changed

+115
-97
lines changed

src/libcmd/built-path.cc

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "built-path.hh"
2+
#include "derivations.hh"
3+
#include "store-api.hh"
4+
5+
#include <nlohmann/json.hpp>
6+
7+
#include <optional>
8+
9+
namespace nix {
10+
11+
nlohmann::json BuiltPath::Built::toJSON(ref<Store> store) const {
12+
nlohmann::json res;
13+
res["drvPath"] = store->printStorePath(drvPath);
14+
for (const auto& [output, path] : outputs) {
15+
res["outputs"][output] = store->printStorePath(path);
16+
}
17+
return res;
18+
}
19+
20+
StorePathSet BuiltPath::outPaths() const
21+
{
22+
return std::visit(
23+
overloaded{
24+
[](const BuiltPath::Opaque & p) { return StorePathSet{p.path}; },
25+
[](const BuiltPath::Built & b) {
26+
StorePathSet res;
27+
for (auto & [_, path] : b.outputs)
28+
res.insert(path);
29+
return res;
30+
},
31+
}, raw()
32+
);
33+
}
34+
35+
RealisedPath::Set BuiltPath::toRealisedPaths(Store & store) const
36+
{
37+
RealisedPath::Set res;
38+
std::visit(
39+
overloaded{
40+
[&](const BuiltPath::Opaque & p) { res.insert(p.path); },
41+
[&](const BuiltPath::Built & p) {
42+
auto drvHashes =
43+
staticOutputHashes(store, store.readDerivation(p.drvPath));
44+
for (auto& [outputName, outputPath] : p.outputs) {
45+
if (experimentalFeatureSettings.isEnabled(
46+
Xp::CaDerivations)) {
47+
auto drvOutput = get(drvHashes, outputName);
48+
if (!drvOutput)
49+
throw Error(
50+
"the derivation '%s' has unrealised output '%s' (derived-path.cc/toRealisedPaths)",
51+
store.printStorePath(p.drvPath), outputName);
52+
auto thisRealisation = store.queryRealisation(
53+
DrvOutput{*drvOutput, outputName});
54+
assert(thisRealisation); // We’ve built it, so we must
55+
// have the realisation
56+
res.insert(*thisRealisation);
57+
} else {
58+
res.insert(outputPath);
59+
}
60+
}
61+
},
62+
},
63+
raw());
64+
return res;
65+
}
66+
67+
}

src/libcmd/built-path.hh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "derived-path.hh"
2+
3+
namespace nix {
4+
5+
/**
6+
* A built derived path with hints in the form of optional concrete output paths.
7+
*
8+
* See 'BuiltPath' for more an explanation.
9+
*/
10+
struct BuiltPathBuilt {
11+
StorePath drvPath;
12+
std::map<std::string, StorePath> outputs;
13+
14+
nlohmann::json toJSON(ref<Store> store) const;
15+
static BuiltPathBuilt parse(const Store & store, std::string_view);
16+
17+
GENERATE_CMP(BuiltPathBuilt, me->drvPath, me->outputs);
18+
};
19+
20+
using _BuiltPathRaw = std::variant<
21+
DerivedPath::Opaque,
22+
BuiltPathBuilt
23+
>;
24+
25+
/**
26+
* A built path. Similar to a DerivedPath, but enriched with the corresponding
27+
* output path(s).
28+
*/
29+
struct BuiltPath : _BuiltPathRaw {
30+
using Raw = _BuiltPathRaw;
31+
using Raw::Raw;
32+
33+
using Opaque = DerivedPathOpaque;
34+
using Built = BuiltPathBuilt;
35+
36+
inline const Raw & raw() const {
37+
return static_cast<const Raw &>(*this);
38+
}
39+
40+
StorePathSet outPaths() const;
41+
RealisedPath::Set toRealisedPaths(Store & store) const;
42+
43+
};
44+
45+
typedef std::vector<BuiltPath> BuiltPaths;
46+
47+
}

src/libcmd/installables.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "path.hh"
66
#include "outputs-spec.hh"
77
#include "derived-path.hh"
8+
#include "built-path.hh"
89
#include "store-api.hh"
910
#include "build-result.hh"
1011

src/libstore/derived-path.cc

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "derived-path.hh"
2-
#include "derivations.hh"
32
#include "store-api.hh"
43

54
#include <nlohmann/json.hpp>
@@ -30,30 +29,6 @@ nlohmann::json DerivedPath::Built::toJSON(ref<Store> store) const {
3029
return res;
3130
}
3231

33-
nlohmann::json BuiltPath::Built::toJSON(ref<Store> store) const {
34-
nlohmann::json res;
35-
res["drvPath"] = store->printStorePath(drvPath);
36-
for (const auto& [output, path] : outputs) {
37-
res["outputs"][output] = store->printStorePath(path);
38-
}
39-
return res;
40-
}
41-
42-
StorePathSet BuiltPath::outPaths() const
43-
{
44-
return std::visit(
45-
overloaded{
46-
[](const BuiltPath::Opaque & p) { return StorePathSet{p.path}; },
47-
[](const BuiltPath::Built & b) {
48-
StorePathSet res;
49-
for (auto & [_, path] : b.outputs)
50-
res.insert(path);
51-
return res;
52-
},
53-
}, raw()
54-
);
55-
}
56-
5732
std::string DerivedPath::Opaque::to_string(const Store & store) const
5833
{
5934
return store.printStorePath(path);
@@ -121,35 +96,4 @@ DerivedPath DerivedPath::parseLegacy(const Store & store, std::string_view s)
12196
return parseWith(store, s, "!");
12297
}
12398

124-
RealisedPath::Set BuiltPath::toRealisedPaths(Store & store) const
125-
{
126-
RealisedPath::Set res;
127-
std::visit(
128-
overloaded{
129-
[&](const BuiltPath::Opaque & p) { res.insert(p.path); },
130-
[&](const BuiltPath::Built & p) {
131-
auto drvHashes =
132-
staticOutputHashes(store, store.readDerivation(p.drvPath));
133-
for (auto& [outputName, outputPath] : p.outputs) {
134-
if (experimentalFeatureSettings.isEnabled(
135-
Xp::CaDerivations)) {
136-
auto drvOutput = get(drvHashes, outputName);
137-
if (!drvOutput)
138-
throw Error(
139-
"the derivation '%s' has unrealised output '%s' (derived-path.cc/toRealisedPaths)",
140-
store.printStorePath(p.drvPath), outputName);
141-
auto thisRealisation = store.queryRealisation(
142-
DrvOutput{*drvOutput, outputName});
143-
assert(thisRealisation); // We’ve built it, so we must
144-
// have the realisation
145-
res.insert(*thisRealisation);
146-
} else {
147-
res.insert(outputPath);
148-
}
149-
}
150-
},
151-
},
152-
raw());
153-
return res;
154-
}
15599
}

src/libstore/derived-path.hh

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -109,47 +109,6 @@ struct DerivedPath : _DerivedPathRaw {
109109
static DerivedPath parseLegacy(const Store & store, std::string_view);
110110
};
111111

112-
/**
113-
* A built derived path with hints in the form of optional concrete output paths.
114-
*
115-
* See 'BuiltPath' for more an explanation.
116-
*/
117-
struct BuiltPathBuilt {
118-
StorePath drvPath;
119-
std::map<std::string, StorePath> outputs;
120-
121-
nlohmann::json toJSON(ref<Store> store) const;
122-
static BuiltPathBuilt parse(const Store & store, std::string_view);
123-
124-
GENERATE_CMP(BuiltPathBuilt, me->drvPath, me->outputs);
125-
};
126-
127-
using _BuiltPathRaw = std::variant<
128-
DerivedPath::Opaque,
129-
BuiltPathBuilt
130-
>;
131-
132-
/**
133-
* A built path. Similar to a DerivedPath, but enriched with the corresponding
134-
* output path(s).
135-
*/
136-
struct BuiltPath : _BuiltPathRaw {
137-
using Raw = _BuiltPathRaw;
138-
using Raw::Raw;
139-
140-
using Opaque = DerivedPathOpaque;
141-
using Built = BuiltPathBuilt;
142-
143-
inline const Raw & raw() const {
144-
return static_cast<const Raw &>(*this);
145-
}
146-
147-
StorePathSet outPaths() const;
148-
RealisedPath::Set toRealisedPaths(Store & store) const;
149-
150-
};
151-
152112
typedef std::vector<DerivedPath> DerivedPaths;
153-
typedef std::vector<BuiltPath> BuiltPaths;
154113

155114
}

0 commit comments

Comments
 (0)