-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
deps_format.cpp
454 lines (387 loc) · 17.4 KB
/
deps_format.cpp
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "deps_entry.h"
#include "deps_format.h"
#include "utils.h"
#include "trace.h"
#include "bundle/info.h"
#include <tuple>
#include <array>
#include <iterator>
#include <cassert>
#include <functional>
const std::array<const pal::char_t*, deps_entry_t::asset_types::count> deps_entry_t::s_known_asset_types = {{
_X("runtime"), _X("resources"), _X("native")
}};
namespace
{
pal::string_t get_optional_property(
const json_parser_t::value_t& properties,
const pal::string_t& key)
{
const auto& prop = properties.FindMember(key.c_str());
return (prop != properties.MemberEnd() && prop->value.IsString()) ? prop->value.GetString() : _X("");
}
pal::string_t get_optional_path(
const json_parser_t::value_t& properties,
const pal::string_t& key)
{
pal::string_t path = get_optional_property(properties, key);
if (path.length() > 0 && _X('/') != DIR_SEPARATOR)
{
replace_char(&path, _X('/'), DIR_SEPARATOR);
}
return path;
}
}
void deps_json_t::reconcile_libraries_with_targets(
const pal::string_t& deps_path,
const json_parser_t::value_t& json,
const std::function<bool(const pal::string_t&)>& library_exists_fn,
const std::function<const vec_asset_t&(const pal::string_t&, size_t, bool*)>& get_assets_fn)
{
pal::string_t deps_file = get_filename(deps_path);
for (const auto& library : json[_X("libraries")].GetObject())
{
trace::info(_X("Reconciling library %s"), library.name.GetString());
pal::string_t lib_name{library.name.GetString()};
if (!library_exists_fn(lib_name))
{
trace::info(_X("Library %s does not exist"), library.name.GetString());
continue;
}
const pal::string_t& hash = library.value[_X("sha512")].GetString();
bool serviceable = library.value[_X("serviceable")].GetBool();
pal::string_t library_path = get_optional_path(library.value, _X("path"));
pal::string_t library_hash_path = get_optional_path(library.value, _X("hashPath"));
pal::string_t runtime_store_manifest_list = get_optional_path(library.value, _X("runtimeStoreManifestName"));
for (size_t i = 0; i < deps_entry_t::s_known_asset_types.size(); ++i)
{
bool rid_specific = false;
const vec_asset_t& assets = get_assets_fn(lib_name, i, &rid_specific);
m_deps_entries[i].reserve(assets.size());
for (const auto& asset : assets)
{
auto asset_name = asset.name;
if (ends_with(asset_name, _X(".ni"), false))
{
asset_name = strip_file_ext(asset_name);
}
deps_entry_t entry;
size_t pos = lib_name.find(_X("/"));
entry.library_name = lib_name.substr(0, pos);
entry.library_version = lib_name.substr(pos + 1);
entry.library_type = to_lower(library.value[_X("type")].GetString());
entry.library_hash = hash;
entry.library_path = library_path;
entry.library_hash_path = library_hash_path;
entry.runtime_store_manifest_list = runtime_store_manifest_list;
entry.asset_type = static_cast<deps_entry_t::asset_types>(i);
entry.is_serviceable = serviceable;
entry.is_rid_specific = rid_specific;
entry.deps_file = deps_file;
entry.asset = asset;
entry.asset.name = asset_name;
if (trace::is_enabled())
{
trace::info(_X("Parsed %s deps entry %zu for asset name: %s from %s: %s, library version: %s, relpath: %s, assemblyVersion %s, fileVersion %s"),
deps_entry_t::s_known_asset_types[i],
m_deps_entries[i].size() - 1,
entry.asset.name.c_str(),
entry.library_type.c_str(),
entry.library_name.c_str(),
entry.library_version.c_str(),
entry.asset.relative_path.c_str(),
entry.asset.assembly_version.as_str().c_str(),
entry.asset.file_version.as_str().c_str());
}
m_deps_entries[i].push_back(std::move(entry));
}
}
}
}
// Returns the RID determined (computed or fallback) for the platform the host is running on.
pal::string_t deps_json_t::get_current_rid(const rid_fallback_graph_t& rid_fallback_graph)
{
pal::string_t currentRid = get_current_runtime_id(false /*use_fallback*/);
trace::info(_X("HostRID is %s"), currentRid.empty()? _X("not available"): currentRid.c_str());
// If the current RID is not present in the RID fallback graph, then the platform
// is unknown to us. At this point, we will fallback to using the base RIDs and attempt
// asset lookup using them.
//
// We do the same even when the RID is empty.
if (currentRid.empty() || (rid_fallback_graph.count(currentRid) == 0))
{
currentRid = pal::get_current_os_fallback_rid() + pal::string_t(_X("-")) + get_current_arch_name();
trace::info(_X("Falling back to base HostRID: %s"), currentRid.c_str());
}
return currentRid;
}
void deps_json_t::perform_rid_fallback(rid_specific_assets_t* portable_assets, const rid_fallback_graph_t& rid_fallback_graph)
{
pal::string_t host_rid = get_current_rid(rid_fallback_graph);
for (auto& package : portable_assets->libs)
{
for (size_t asset_type_index = 0; asset_type_index < deps_entry_t::asset_types::count; asset_type_index++)
{
auto& rid_assets = package.second[asset_type_index].rid_assets;
pal::string_t matched_rid = rid_assets.count(host_rid) ? host_rid : _X("");
if (matched_rid.empty())
{
auto rid_fallback_iter = rid_fallback_graph.find(host_rid);
if (rid_fallback_iter == rid_fallback_graph.end())
{
trace::warning(_X("The targeted framework does not support the runtime '%s'. Some native libraries from [%s] may fail to load on this platform."), host_rid.c_str(), package.first.c_str());
}
else
{
const auto& fallback_rids = rid_fallback_iter->second;
auto iter = std::find_if(fallback_rids.begin(), fallback_rids.end(), [&rid_assets](const pal::string_t& rid) {
return rid_assets.count(rid);
});
if (iter != fallback_rids.end())
{
matched_rid = *iter;
}
}
}
if (matched_rid.empty())
{
rid_assets.clear();
}
for (auto iter = rid_assets.begin(); iter != rid_assets.end(); /* */)
{
if (iter->first != matched_rid)
{
trace::verbose(
_X("Chose %s, so removing rid (%s) specific assets for package %s and asset type %s"),
matched_rid.c_str(),
iter->first.c_str(),
package.first.c_str(),
deps_entry_t::s_known_asset_types[asset_type_index]);
iter = rid_assets.erase(iter);
}
else
{
++iter;
}
}
}
}
}
void deps_json_t::process_runtime_targets(const json_parser_t::value_t& json, const pal::string_t& target_name, const rid_fallback_graph_t& rid_fallback_graph, rid_specific_assets_t* p_assets)
{
rid_specific_assets_t& assets = *p_assets;
for (const auto& package : json[_X("targets")][target_name.c_str()].GetObject())
{
const auto& runtimeTargets = package.value.FindMember(_X("runtimeTargets"));
if (runtimeTargets == package.value.MemberEnd())
{
continue;
}
for (const auto& file : runtimeTargets->value.GetObject())
{
const auto& type = file.value[_X("assetType")].GetString();
for (size_t asset_type_index = 0; asset_type_index < deps_entry_t::s_known_asset_types.size(); ++asset_type_index)
{
if (pal::strcasecmp(type, deps_entry_t::s_known_asset_types[asset_type_index]) != 0)
{
continue;
}
version_t assembly_version, file_version;
const pal::string_t& assembly_version_str = get_optional_property(file.value, _X("assemblyVersion"));
if (!assembly_version_str.empty())
{
version_t::parse(assembly_version_str, &assembly_version);
}
const pal::string_t& file_version_str = get_optional_property(file.value, _X("fileVersion"));
if (!file_version_str.empty())
{
version_t::parse(file_version_str, &file_version);
}
pal::string_t file_name{file.name.GetString()};
deps_asset_t asset(get_filename_without_ext(file_name), file_name, assembly_version, file_version);
const auto& rid = file.value[_X("rid")].GetString();
if (trace::is_enabled())
{
trace::info(_X("Adding runtimeTargets %s asset %s rid=%s assemblyVersion=%s fileVersion=%s from %s"),
deps_entry_t::s_known_asset_types[asset_type_index],
asset.relative_path.c_str(),
rid,
asset.assembly_version.as_str().c_str(),
asset.file_version.as_str().c_str(),
package.name.GetString());
}
assets.libs[package.name.GetString()][asset_type_index].rid_assets[rid].push_back(asset);
}
}
}
perform_rid_fallback(&assets, rid_fallback_graph);
}
void deps_json_t::process_targets(const json_parser_t::value_t& json, const pal::string_t& target_name, deps_assets_t* p_assets)
{
deps_assets_t& assets = *p_assets;
for (const auto& package : json[_X("targets")][target_name.c_str()].GetObject())
{
const auto& asset_types = package.value.GetObject();
for (size_t i = 0; i < deps_entry_t::s_known_asset_types.size(); ++i)
{
const auto& iter = asset_types.FindMember(deps_entry_t::s_known_asset_types[i]);
if (iter == asset_types.MemberEnd())
{
continue;
}
const auto& files = iter->value.GetObject();
vec_asset_t& asset_files = assets.libs[package.name.GetString()][i];
asset_files.reserve(files.MemberCount());
for (const auto& file : files)
{
version_t assembly_version, file_version;
const pal::string_t& assembly_version_str = get_optional_property(file.value, _X("assemblyVersion"));
if (assembly_version_str.length() > 0)
{
version_t::parse(assembly_version_str, &assembly_version);
}
const pal::string_t& file_version_str = get_optional_property(file.value, _X("fileVersion"));
if (file_version_str.length() > 0)
{
version_t::parse(file_version_str, &file_version);
}
pal::string_t file_name{file.name.GetString()};
deps_asset_t asset(get_filename_without_ext(file_name), file_name, assembly_version, file_version);
if (trace::is_enabled())
{
trace::info(_X("Adding %s asset %s assemblyVersion=%s fileVersion=%s from %s"),
deps_entry_t::s_known_asset_types[i],
asset.relative_path.c_str(),
asset.assembly_version.as_str().c_str(),
asset.file_version.as_str().c_str(),
package.name.GetString());
}
asset_files.push_back(std::move(asset));
}
}
}
}
void deps_json_t::load_framework_dependent(const pal::string_t& deps_path, const json_parser_t::value_t& json, const pal::string_t& target_name, const rid_fallback_graph_t& rid_fallback_graph)
{
process_runtime_targets(json, target_name, rid_fallback_graph, &m_rid_assets);
process_targets(json, target_name, &m_assets);
auto package_exists = [&](const pal::string_t& package) -> bool {
return m_rid_assets.libs.count(package) || m_assets.libs.count(package);
};
const vec_asset_t empty;
auto get_relpaths = [&](const pal::string_t& package, size_t asset_type_index, bool* rid_specific) -> const vec_asset_t& {
*rid_specific = false;
// Is there any rid specific assets for this type ("native" or "runtime" or "resources")
if (m_rid_assets.libs.count(package) && !m_rid_assets.libs[package][asset_type_index].rid_assets.empty())
{
const auto& assets_for_type = m_rid_assets.libs[package][asset_type_index].rid_assets.begin()->second;
if (!assets_for_type.empty())
{
*rid_specific = true;
return assets_for_type;
}
trace::verbose(_X("There were no rid specific %s asset for %s"), deps_entry_t::s_known_asset_types[asset_type_index], package.c_str());
}
if (m_assets.libs.count(package))
{
return m_assets.libs[package][asset_type_index];
}
return empty;
};
reconcile_libraries_with_targets(deps_path, json, package_exists, get_relpaths);
}
void deps_json_t::load_self_contained(const pal::string_t& deps_path, const json_parser_t::value_t& json, const pal::string_t& target_name)
{
process_targets(json, target_name, &m_assets);
auto package_exists = [&](const pal::string_t& package) -> bool {
return m_assets.libs.count(package);
};
auto get_relpaths = [&](const pal::string_t& package, size_t type_index, bool* rid_specific) -> const vec_asset_t& {
*rid_specific = false;
return m_assets.libs[package][type_index];
};
reconcile_libraries_with_targets(deps_path, json, package_exists, get_relpaths);
const auto& json_object = json.GetObject();
if (json_object.HasMember(_X("runtimes")))
{
for (const auto& rid : json[_X("runtimes")].GetObject())
{
auto& vec = m_rid_fallback_graph[rid.name.GetString()];
const auto& fallback_array = rid.value.GetArray();
vec.reserve(fallback_array.Size());
for (const auto& fallback : fallback_array)
{
vec.push_back(fallback.GetString());
}
}
}
if (trace::is_enabled())
{
trace::verbose(_X("The rid fallback graph is: {"));
for (const auto& rid : m_rid_fallback_graph)
{
trace::verbose(_X("%s => ["), rid.first.c_str());
for (const auto& fallback : rid.second)
{
trace::verbose(_X("%s, "), fallback.c_str());
}
trace::verbose(_X("]"));
}
trace::verbose(_X("}"));
}
}
bool deps_json_t::has_package(const pal::string_t& name, const pal::string_t& ver) const
{
pal::string_t pv;
pv.reserve(name.length() + ver.length() + 1);
pv.assign(name);
pv.push_back(_X('/'));
pv.append(ver);
auto iter = m_rid_assets.libs.find(pv);
if (iter != m_rid_assets.libs.end())
{
for (size_t asset_type_index = 0; asset_type_index < deps_entry_t::asset_types::count; asset_type_index++)
{
if (!iter->second[asset_type_index].rid_assets.empty())
{
return true;
}
}
}
return m_assets.libs.count(pv);
}
// -----------------------------------------------------------------------------
// Load the deps file and parse its "entry" lines which contain the "fields" of
// the entry. Populate an array of these entries.
//
void deps_json_t::load(bool is_framework_dependent, const pal::string_t& deps_path, const rid_fallback_graph_t& rid_fallback_graph)
{
m_deps_file = deps_path;
m_file_exists = bundle::info_t::config_t::probe(deps_path) || pal::realpath(&m_deps_file, true);
json_parser_t json;
if (!m_file_exists)
{
// If file doesn't exist, then assume parsed.
trace::verbose(_X("Could not locate the dependencies manifest file [%s]. Some libraries may fail to resolve."), deps_path.c_str());
m_valid = true;
return;
}
if (!json.parse_file(m_deps_file))
return;
m_valid = true;
const auto& runtime_target = json.document()[_X("runtimeTarget")];
const pal::string_t& name = runtime_target.IsString() ?
runtime_target.GetString() :
runtime_target[_X("name")].GetString();
trace::verbose(_X("Loading deps file... %s as framework dependent=[%d]"), deps_path.c_str(), is_framework_dependent);
if (is_framework_dependent)
{
load_framework_dependent(deps_path, json.document(), name, rid_fallback_graph);
}
else
{
load_self_contained(deps_path, json.document(), name);
}
}