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
345
346
347
348
349
350
351
352
353
354
355
|
/*
*/
#include "manifest.h"
#include "toml.h"
#include "debug.h"
#include "helpers.h"
#include <cassert>
#include <algorithm>
#include "repository.h"
static ::std::vector<::std::shared_ptr<PackageManifest>> g_loaded_manifests;
PackageManifest::PackageManifest()
{
}
namespace
{
void target_edit_from_kv(PackageTarget& target, TomlKeyValue& kv, unsigned base_idx);
}
PackageManifest PackageManifest::load_from_toml(const ::std::string& path)
{
PackageManifest rv;
rv.m_manifest_path = path;
TomlFile toml_file(path);
for(auto key_val : toml_file)
{
assert(key_val.path.size() > 0);
DEBUG(key_val.path << " = " << key_val.value);
const auto& section = key_val.path[0];
if( section == "package" )
{
assert(key_val.path.size() > 1);
const auto& key = key_val.path[1];
if(key == "authors")
{
// TODO: Use the `authors` key
// - Ignore ofr now.
}
else if( key == "name" )
{
if(rv.m_name != "" )
{
// TODO: Warn/error
throw ::std::runtime_error("Package name set twice");
}
rv.m_name = key_val.value.as_string();
if(rv.m_name == "")
{
// TODO: Error
throw ::std::runtime_error("Package name cannot be empty");
}
}
else if( key == "version" )
{
rv.m_version = PackageVersion::from_string(key_val.value.as_string());
}
else if( key == "build" )
{
if(rv.m_build_script != "" )
{
// TODO: Warn/error
throw ::std::runtime_error("Build script path set twice");
}
rv.m_build_script = key_val.value.as_string();
if(rv.m_build_script == "")
{
// TODO: Error
throw ::std::runtime_error("Build script path cannot be empty");
}
}
else
{
// Unknown value in `package`
throw ::std::runtime_error("Unknown key `" + key + "` in [package]");
}
}
else if( section == "lib" )
{
// 1. Find (and add if needed) the `lib` descriptor
auto it = ::std::find_if(rv.m_targets.begin(), rv.m_targets.end(), [](const auto& x){ return x.m_type == PackageTarget::Type::Lib; });
if(it == rv.m_targets.end())
it = rv.m_targets.insert(it, PackageTarget { PackageTarget::Type::Lib });
// 2. Parse from the key-value pair
target_edit_from_kv(*it, key_val, 1);
}
else if( section == "bin" )
{
assert(key_val.path.size() > 1);
unsigned idx = ::std::stoi( key_val.path[1] );
auto it = ::std::find_if(rv.m_targets.begin(), rv.m_targets.end(), [&idx](const auto& x) { return x.m_type == PackageTarget::Type::Bin && idx-- == 0; });
if (it == rv.m_targets.end())
it = rv.m_targets.insert(it, PackageTarget{ PackageTarget::Type::Bin });
target_edit_from_kv(*it, key_val, 2);
}
else if (section == "test")
{
assert(key_val.path.size() > 1);
unsigned idx = ::std::stoi(key_val.path[1]);
auto it = ::std::find_if(rv.m_targets.begin(), rv.m_targets.end(), [&idx](const auto& x) { return x.m_type == PackageTarget::Type::Test && idx-- == 0; });
if (it == rv.m_targets.end())
it = rv.m_targets.insert(it, PackageTarget{ PackageTarget::Type::Test });
target_edit_from_kv(*it, key_val, 2);
}
else if (section == "bench")
{
assert(key_val.path.size() > 1);
unsigned idx = ::std::stoi(key_val.path[1]);
auto it = ::std::find_if(rv.m_targets.begin(), rv.m_targets.end(), [&idx](const auto& x) { return x.m_type == PackageTarget::Type::Bench && idx-- == 0; });
if (it == rv.m_targets.end())
it = rv.m_targets.insert(it, PackageTarget{ PackageTarget::Type::Bench });
target_edit_from_kv(*it, key_val, 2);
}
else if( section == "dependencies" )
{
assert(key_val.path.size() > 1);
const auto& depname = key_val.path[1];
// Find/create dependency descriptor
auto it = ::std::find_if(rv.m_dependencies.begin(), rv.m_dependencies.end(), [&](const auto& x) { return x.m_name == depname; });
bool was_added = (it == rv.m_dependencies.end());
if (it == rv.m_dependencies.end())
{
it = rv.m_dependencies.insert(it, PackageRef{ depname });
}
auto& ref = *it;
if( key_val.path.size() == 2 )
{
// Shorthand, picks a version from the package repository
if(!was_added)
{
throw ::std::runtime_error(::format("ERROR: Duplicate dependency `", depname, "`"));
}
const auto& version_spec_str = key_val.value.as_string();
ref.m_version = PackageVersionSpec::from_string(version_spec_str);
}
else
{
// (part of a) Full dependency specification
const auto& attr = key_val.path[2];
if( attr == "path" )
{
// Set path specification of the named depenency
ref.m_path = key_val.value.as_string();
}
else if( attr == "git" )
{
// Load from git repo.
TODO("Support git dependencies");
}
else if( attr == "branch" )
{
// Specify git branch
TODO("Support git dependencies (branch)");
}
else if( attr == "version" )
{
assert(key_val.path.size() == 3);
// Parse version specifier
ref.m_version = PackageVersionSpec::from_string(key_val.value.as_string());
}
else if( attr == "optional" )
{
assert(key_val.path.size() == 3);
ref.m_optional = key_val.value.as_bool();
// TODO: Add a feature with the same name as the dependency.
}
else
{
// TODO: Error
throw ::std::runtime_error(::format("ERROR: Unkown depencency attribute `", attr, "` on dependency `", depname, "`"));
}
}
}
else if( section == "build-dependencies" )
{
// TODO: Build deps
}
else if( section == "patch" )
{
//const auto& repo = key_val.path[1];
TODO("Support repository patches");
}
else if( section == "target" )
{
// TODO: Target opts?
}
else if( section == "features" )
{
// TODO: Features
}
else
{
// Unknown manifest section
TODO("Unknown manifest section " << section);
}
}
return rv;
}
namespace
{
void target_edit_from_kv(PackageTarget& target, TomlKeyValue& kv, unsigned base_idx)
{
const auto& key = kv.path[base_idx];
if(key == "name")
{
assert(kv.path.size() == base_idx+1);
target.m_name = kv.value.as_string();
}
else if(key == "path")
{
assert(kv.path.size() == base_idx + 1);
target.m_path = kv.value.as_string();
}
else if(key == "test")
{
assert(kv.path.size() == base_idx + 1);
target.m_enable_test = kv.value.as_bool();
}
else if( key == "doctest" )
{
assert(kv.path.size() == base_idx + 1);
target.m_enable_doctest = kv.value.as_bool();
}
else if( key == "bench" )
{
assert(kv.path.size() == base_idx + 1);
target.m_enable_bench = kv.value.as_bool();
}
else if( key == "doc" )
{
assert(kv.path.size() == base_idx + 1);
target.m_enable_doc = kv.value.as_bool();
}
else if( key == "plugin" )
{
assert(kv.path.size() == base_idx + 1);
target.m_is_plugin = kv.value.as_bool();
}
else if( key == "proc-macro" )
{
assert(kv.path.size() == base_idx + 1);
target.m_is_proc_macro = kv.value.as_bool();
}
else if( key == "harness" )
{
assert(kv.path.size() == base_idx + 1);
target.m_is_own_harness = kv.value.as_bool();
}
else if( key == "crate-type" )
{
// TODO: Support crate types
}
else
{
throw ::std::runtime_error( ::format("TODO: Handle target option `", key, "`") );
}
}
}
const PackageTarget& PackageManifest::get_library() const
{
auto it = ::std::find_if(m_targets.begin(), m_targets.end(), [](const auto& x) { return x.m_type == PackageTarget::Type::Lib; });
if (it == m_targets.end())
{
throw ::std::runtime_error(::format("Package ", m_name, " doesn't have a library"));
}
return *it;
}
void PackageManifest::load_dependencies(Repository& repo)
{
DEBUG("Loading depencencies for " << m_name);
auto base_path = ::helpers::path(m_manifest_path).parent();
// 2. Recursively load dependency manifests
for(auto& dep : m_dependencies)
{
if( dep.m_optional )
{
// TODO: Check for feature that enables this (option to a feature with no '/')
continue ;
}
dep.load_manifest(repo, base_path);
}
}
void PackageRef::load_manifest(Repository& repo, const ::helpers::path& base_path)
{
// If the path isn't set, check for:
// - Git (checkout and use)
// - Version and repository (check vendored, check cache, download into cache)
if( ! this->has_path() )
{
if( this->has_git() )
{
DEBUG("Load dependency " << this->name() << " from git");
throw "TODO: Git";
}
else
{
DEBUG("Load dependency " << this->name() << " from repo");
m_manifest = repo.find(this->name(), this->get_version());
}
}
else
{
DEBUG("Load dependency " << m_name << " from path " << m_path);
// Search for a copy of this already loaded
m_manifest = repo.from_path(base_path / ::helpers::path(m_path) / "Cargo.toml");
}
m_manifest->load_dependencies(repo);
}
PackageVersion PackageVersion::from_string(const ::std::string& s)
{
PackageVersion rv;
::std::istringstream iss { s };
iss >> rv.major;
iss.get();
iss >> rv.minor;
if( iss.get() != EOF )
{
iss >> rv.patch;
}
return rv;
}
PackageVersionSpec PackageVersionSpec::from_string(const ::std::string& s)
{
PackageVersionSpec rv;
throw "";
return rv;
}
bool PackageVersionSpec::accepts(const PackageVersion& v) const
{
throw "";
}
|