-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathloader.cpp
More file actions
344 lines (295 loc) · 14.9 KB
/
loader.cpp
File metadata and controls
344 lines (295 loc) · 14.9 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright © 2023-2024 Dylan Baker
// Copyright © 2024 Bret Brown
// SPDX-License-Identifier: MIT
#include "cps/loader.hpp"
#include "cps/error.hpp"
#include <fmt/core.h>
#include <nlohmann/json.hpp>
#include <tl/expected.hpp>
#include <iostream>
#include <optional>
namespace cps::loader {
namespace {
constexpr static std::string_view CPS_VERSION = "0.12.0";
template <typename T>
tl::expected<std::optional<T>, std::string>
get_optional(const nlohmann::json & parent, std::string_view parent_name, const std::string & name) {
// It's okay for a member to be missing from an optional value
if (!parent.contains(name)) {
return std::nullopt;
}
const nlohmann::json & value = parent[name];
if constexpr (std::is_same_v<T, std::string>) {
if (value.is_string()) {
return value.get<std::string>();
}
} else if constexpr (std::is_same_v<T, std::vector<std::string>>) {
if (value.is_array()) {
std::vector<std::string> ret;
for (auto && v : value) {
// TODO: Error handling?
ret.emplace_back(v.get<std::string>());
}
return ret;
}
}
return tl::unexpected(
fmt::format("Optional field `{}` in `{}` is not of type `{}`!", name, parent_name, typeid(T).name()));
};
template <typename T>
tl::expected<T, std::string> get_required(const nlohmann::json & parent, std::string_view parent_name,
const std::string & name) {
if (!parent.contains(name)) {
return tl::unexpected(fmt::format("Required field `{}` in `{}` is missing!", name, parent_name));
}
return get_optional<T>(parent, parent_name, name).and_then([](auto && v) -> tl::expected<T, std::string> {
if (v)
return v.value();
return "bad";
});
// TODO: also need to fixup error message for "Optional type ..."
}
Type string_to_type(std::string_view str) {
if (str == "executable") {
return Type::executable;
}
if (str == "archive") {
return Type::archive;
}
if (str == "dylib") {
return Type::dylib;
}
if (str == "module") {
return Type::module;
}
if (str == "jar") {
return Type::jar;
}
if (str == "interface") {
return Type::interface;
}
if (str == "symbolic") {
return Type::symbolic;
}
return Type::unknown;
}
version::Schema string_to_schema(std::string_view str) {
if (str == "simple") {
return version::Schema::simple;
}
if (str == "rpm") {
return version::Schema::rpm;
}
if (str == "dpkg") {
return version::Schema::dpkg;
}
if (str == "custom") {
return version::Schema::custom;
}
fmt::print(stderr, "Unknown version schema: `{}`", str);
std::abort();
}
template <>
tl::expected<LangValues, std::string> get_required<LangValues>(const nlohmann::json & parent,
std::string_view parent_name,
const std::string & name) {
LangValues ret{};
if (!parent.contains(name)) {
return ret;
}
const nlohmann::json & value = parent[name];
if (value.is_object()) {
auto && fallback = CPS_TRY(get_optional<std::vector<std::string>>(value, name, "*"))
.value_or(std::vector<std::string>{});
ret[KnownLanguages::c] =
CPS_TRY(get_optional<std::vector<std::string>>(value, name, "c")).value_or(fallback);
ret[KnownLanguages::cxx] =
CPS_TRY(get_optional<std::vector<std::string>>(value, name, "c++")).value_or(fallback);
ret[KnownLanguages::fortran] =
CPS_TRY(get_optional<std::vector<std::string>>(value, name, "fortran")).value_or(fallback);
} else if (value.is_array()) {
std::vector<std::string> fin;
for (auto && v : value) {
fin.emplace_back(v.get<std::string>());
}
for (auto && v : {KnownLanguages::c, KnownLanguages::cxx, KnownLanguages::fortran}) {
ret.emplace(v, fin);
}
} else {
return tl::unexpected(
fmt::format("Section `{}` of `{}` is neither an object nor an array!", parent_name, name));
}
return ret;
}
template <>
tl::expected<Defines, std::string>
get_required<Defines>(const nlohmann::json & parent, std::string_view parent_name, const std::string & name) {
Defines ret;
if (!parent.contains(name)) {
return ret;
}
const nlohmann::json & defines = parent[name];
if (!defines.is_object()) {
return tl::unexpected(fmt::format("Section `{}` of `{}` is not an object", parent_name, name));
}
const auto getter =
[&](const std::string & lang) -> tl::expected<std::optional<std::vector<Define>>, std::string> {
if (!defines.contains(lang)) {
return std::nullopt;
}
std::vector<Define> ret2;
for (auto && [k, v] : defines.at(lang).items()) {
if (v.is_null()) {
ret2.emplace_back(Define{k});
continue;
}
if (!v.is_string()) {
return tl::unexpected(fmt::format(
"key `{}` of language `{}` of section `{}` of `{}` has a value that is not a string", k,
lang, parent_name, name));
}
ret2.emplace_back(Define{k, v.get<std::string>()});
}
return ret2;
};
auto && fallback = CPS_TRY(getter("*")).value_or(std::vector<Define>{});
ret[KnownLanguages::c] = CPS_TRY(getter("c")).value_or(fallback);
ret[KnownLanguages::cxx] = CPS_TRY(getter("cxx")).value_or(fallback);
ret[KnownLanguages::fortran] = CPS_TRY(getter("fortran")).value_or(fallback);
return ret;
};
template <>
tl::expected<Requires, std::string>
get_required<Requires>(const nlohmann::json & parent, std::string_view parent_name, const std::string & name) {
Requires ret{};
if (!parent.contains(name)) {
return ret;
}
nlohmann::json require = parent[name];
if (!require.is_object()) {
return tl::unexpected(fmt::format("`{}` field of `{}` is not an object", name, parent_name));
}
for (const auto & item : require.items()) {
// TODO: error handling for not a string?
const std::string key = item.key();
const nlohmann::json & obj = item.value();
ret.emplace(key, Requirement{
CPS_TRY(get_optional<std::vector<std::string>>(obj, name, "components"))
.value_or(std::vector<std::string>{}),
CPS_TRY(get_optional<std::string>(obj, name, "version")),
});
}
return ret;
};
using Components = std::unordered_map<std::string, Component>;
template <>
tl::expected<Components, std::string> get_required<Components>(const nlohmann::json & parent,
std::string_view parent_name,
const std::string & name) {
if (!parent.contains(name)) {
return tl::unexpected(fmt::format("Required field `components` of `{}` is missing!", parent_name));
}
std::unordered_map<std::string, Component> components{};
// TODO: error handling for not an object
nlohmann::json compmap = parent[name];
if (!compmap.is_object()) {
return tl::unexpected(fmt::format("`{}` field of `{}` is not an object", name, parent_name));
}
for (const auto & item : compmap.items()) {
// TODO: Error handling for not a string?
const std::string key = item.key();
const nlohmann::json & comp = item.value();
if (!comp.is_object()) {
return tl::unexpected(fmt::format("`{}` `{}` is not an object", name, key));
}
auto const type = CPS_TRY(get_required<std::string>(comp, name, "type").map(string_to_type));
auto const compile_flags = CPS_TRY(get_required<LangValues>(comp, name, "compile_flags"));
auto const includes = CPS_TRY(get_required<LangValues>(comp, name, "includes"));
auto const definitions = CPS_TRY(get_required<Defines>(comp, name, "definitions"));
auto const link_flags = CPS_TRY(get_optional<std::vector<std::string>>(comp, name, "link_flags"))
.value_or(std::vector<std::string>{});
auto const link_libraries =
CPS_TRY(get_optional<std::vector<std::string>>(comp, name, "link_libraries"))
.value_or(std::vector<std::string>{});
auto const location = CPS_TRY(get_optional<std::string>(comp, name, "location"));
auto const link_location = CPS_TRY(get_optional<std::string>(comp, name, "link_location"));
auto const require = CPS_TRY(get_optional<std::vector<std::string>>(comp, name, "requires"))
.value_or(std::vector<std::string>{});
if (type == Type::unknown) {
continue;
}
if (type == Type::archive && !location.has_value()) {
return tl::make_unexpected(
fmt::format("component `{}` of type `archive` missing required key `location`", key));
}
// TODO: Validate link_location, see https://github.com/cps-org/cps/issues/34
components[key] = Component{
.type = std::move(type),
.compile_flags = std::move(compile_flags),
.includes = std::move(includes),
.definitions = std::move(definitions),
.link_flags = std::move(link_flags),
.link_libraries = std::move(link_libraries),
.location = std::move(location),
.link_location = std::move(link_location),
.require = std::move(require),
};
}
if (components.empty()) {
return tl::unexpected(fmt::format("`{}` must have at least one component", parent_name));
}
return components;
};
} // namespace
Define::Define(std::string name_) : name{std::move(name_)}, value{std::nullopt} {};
Define::Define(std::string name_, std::string value_) : name{std::move(name_)}, value{std::move(value_)} {};
std::string Define::get_name() const { return name; }
std::optional<std::string> Define::get_value() const { return value; }
Configuration::Configuration() = default;
Configuration::Configuration(LangValues cflags) : compile_flags{std::move(cflags)} {};
Requirement::Requirement() = default;
Requirement::Requirement(std::vector<std::string> comps) : components{std::move(comps)} {};
Requirement::Requirement(std::vector<std::string> && comps, std::optional<std::string> && ver)
: components{std::move(comps)}, version{std::move(ver)} {};
Platform::Platform() = default;
tl::expected<Package, std::string> load(std::istream & input_buffer, const std::filesystem::path & filename) {
nlohmann::json root;
try {
root = nlohmann::json::parse(input_buffer);
} catch (const nlohmann::json::exception & ex) {
return tl::make_unexpected(
fmt::format("Exception caught while parsing json for `{}.cps`\n{}", filename.string(), ex.what()));
}
auto const name = CPS_TRY(get_required<std::string>(root, "package", "name"));
auto const cps_version = CPS_TRY(get_required<std::string>(root, "package", "cps_version"));
auto const compat_version = CPS_TRY(get_optional<std::string>(root, "package", "compat_version"));
auto const components = CPS_TRY(get_required<Components>(root, "package", "components"));
auto const cps_path = CPS_TRY(get_optional<std::string>(root, "package", "cps_path"));
auto const default_components =
CPS_TRY(get_optional<std::vector<std::string>>(root, "package", "default_components"));
auto const platform = std::nullopt; // TODO: parse platform
auto const require = CPS_TRY(get_required<Requires>(root, "package", "requires"));
auto const version = CPS_TRY(get_optional<std::string>(root, "package", "version"));
auto const version_schema =
CPS_TRY(get_optional<std::string>(root, "package", "version_schema").map([](auto && v) {
return string_to_schema(v.value_or("simple"));
}));
if (cps_version != CPS_VERSION) {
return tl::make_unexpected(fmt::format("cps-config only supports CPS_VERSION `{}` found `{}` in `{}`",
CPS_VERSION, cps_version, name));
}
return Package{
.name = std::move(name),
.cps_version = std::move(cps_version),
.components = std::move(components),
.compat_version = std::move(compat_version),
.cps_path = std::move(cps_path),
.filename = filename.string(),
.default_components = std::move(default_components),
.platform = std::move(platform),
.require = std::move(require), // requires is a keyword
.version = std::move(version),
.version_schema = std::move(version_schema),
};
}
} // namespace cps::loader