This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 217
/
deps_format.cpp
487 lines (410 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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include "deps_entry.h"
#include "deps_format.h"
#include "utils.h"
#include "trace.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")
};
const deps_entry_t& deps_json_t::try_ni(const deps_entry_t& entry) const
{
if (m_ni_entries.count(entry.asset.name))
{
int index = m_ni_entries.at(entry.asset.name);
return m_deps_entries[deps_entry_t::asset_types::runtime][index];
}
return entry;
}
pal::string_t deps_json_t::get_optional_property(
const json_object& properties,
const pal::string_t& key) const
{
pal::string_t value;
const auto& iter = properties.find(key);
if (iter != properties.end())
{
value = iter->second.as_string();
}
return value;
}
pal::string_t deps_json_t::get_optional_path(
const json_object& properties,
const pal::string_t& key) const
{
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_value& json,
const std::function<bool(const pal::string_t&)>& library_exists_fn,
const std::function<const vec_asset_t&(const pal::string_t&, int, bool*)>& get_assets_fn)
{
pal::string_t deps_file = get_filename(deps_path);
const auto& libraries = json.at(_X("libraries")).as_object();
for (const auto& library : libraries)
{
trace::info(_X("Reconciling library %s"), library.first.c_str());
if (!library_exists_fn(library.first))
{
trace::info(_X("Library %s does not exist"), library.first.c_str());
continue;
}
const auto& properties = library.second.as_object();
const pal::string_t& hash = properties.at(_X("sha512")).as_string();
bool serviceable = properties.at(_X("serviceable")).as_bool();
pal::string_t library_path = get_optional_path(properties, _X("path"));
pal::string_t library_hash_path = get_optional_path(properties, _X("hashPath"));
pal::string_t runtime_store_manifest_list = get_optional_path(properties, _X("runtimeStoreManifestName"));
for (int i = 0; i < deps_entry_t::s_known_asset_types.size(); ++i)
{
bool rid_specific = false;
for (const auto& asset : get_assets_fn(library.first, i, &rid_specific))
{
bool ni_dll = false;
auto asset_name = asset.name;
if (ends_with(asset_name, _X(".ni"), false))
{
ni_dll = true;
asset_name = strip_file_ext(asset_name);
}
deps_entry_t entry;
size_t pos = library.first.find(_X("/"));
entry.library_name = library.first.substr(0, pos);
entry.library_version = library.first.substr(pos + 1);
entry.library_type = pal::to_lower(properties.at(_X("type")).as_string());
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 = (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;
m_deps_entries[i].push_back(entry);
if (ni_dll)
{
m_ni_entries[entry.asset.name] = m_deps_entries
[deps_entry_t::asset_types::runtime].size() - 1;
}
trace::info(_X("Parsed %s deps entry %d 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());
}
}
}
}
// 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;
if (!pal::getenv(_X("DOTNET_RUNTIME_ID"), ¤tRid))
{
currentRid = pal::get_current_os_rid_platform();
if (!currentRid.empty())
{
currentRid = currentRid + pal::string_t(_X("-")) + get_arch();
}
}
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_arch();
trace::info(_X("Falling back to base HostRID: %s"), currentRid.c_str());
}
return currentRid;
}
bool 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)
{
pal::string_t matched_rid = package.second.rid_assets.count(host_rid) ? host_rid : _X("");
if (matched_rid.empty())
{
if (rid_fallback_graph.count(host_rid) == 0)
{
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_graph.find(host_rid)->second;
auto iter = std::find_if(fallback_rids.begin(), fallback_rids.end(), [&package](const pal::string_t& rid) {
return package.second.rid_assets.count(rid);
});
if (iter != fallback_rids.end())
{
matched_rid = *iter;
}
}
}
if (matched_rid.empty())
{
package.second.rid_assets.clear();
}
for (auto iter = package.second.rid_assets.begin(); iter != package.second.rid_assets.end(); /* */)
{
if (iter->first != matched_rid)
{
trace::verbose(_X("Chose %s, so removing rid (%s) specific assets for package %s"), matched_rid.c_str(), iter->first.c_str(), package.first.c_str());
iter = package.second.rid_assets.erase(iter);
}
else
{
++iter;
}
}
}
return true;
}
bool deps_json_t::process_runtime_targets(const json_value& 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.at(_X("targets")).at(target_name).as_object())
{
const auto& targets = package.second.as_object();
auto iter = targets.find(_X("runtimeTargets"));
if (iter == targets.end())
{
continue;
}
const auto& files = iter->second.as_object();
for (const auto& file : files)
{
const auto& type = file.second.at(_X("assetType")).as_string();
for (int i = 0; i < deps_entry_t::s_known_asset_types.size(); ++i)
{
if (pal::strcasecmp(type.c_str(), deps_entry_t::s_known_asset_types[i]) == 0)
{
const auto& rid = file.second.at(_X("rid")).as_string();
version_t assembly_version, file_version;
const auto& properties = file.second.as_object();
pal::string_t assembly_version_str = get_optional_property(properties, _X("assemblyVersion"));
if (assembly_version_str.length() > 0)
{
version_t::parse(assembly_version_str, &assembly_version);
}
pal::string_t file_version_str = get_optional_property(properties, _X("fileVersion"));
if (file_version_str.length() > 0)
{
version_t::parse(file_version_str, &file_version);
}
deps_asset_t asset(get_filename_without_ext(file.first), file.first, assembly_version, file_version);
trace::info(_X("Adding runtimeTargets %s asset %s rid=%s assemblyVersion=%s fileVersion=%s from %s"),
deps_entry_t::s_known_asset_types[i],
asset.relative_path.c_str(),
rid.c_str(),
asset.assembly_version.as_str().c_str(),
asset.file_version.as_str().c_str(),
package.first.c_str());
assets.libs[package.first].rid_assets[rid][i].push_back(asset);
}
}
}
}
if (!perform_rid_fallback(&assets, rid_fallback_graph))
{
return false;
}
return true;
}
bool deps_json_t::process_targets(const json_value& json, const pal::string_t& target_name, deps_assets_t* p_assets)
{
deps_assets_t& assets = *p_assets;
for (const auto& package : json.at(_X("targets")).at(target_name).as_object())
{
const auto& asset_types = package.second.as_object();
for (int i = 0; i < deps_entry_t::s_known_asset_types.size(); ++i)
{
auto iter = asset_types.find(deps_entry_t::s_known_asset_types[i]);
if (iter != asset_types.end())
{
for (const auto& file : iter->second.as_object())
{
const auto& properties = file.second.as_object();
version_t assembly_version, file_version;
pal::string_t assembly_version_str = get_optional_property(properties, _X("assemblyVersion"));
if (assembly_version_str.length() > 0)
{
version_t::parse(assembly_version_str, &assembly_version);
}
pal::string_t file_version_str = get_optional_property(properties, _X("fileVersion"));
if (file_version_str.length() > 0)
{
version_t::parse(file_version_str, &file_version);
}
deps_asset_t asset(get_filename_without_ext(file.first), file.first, assembly_version, file_version);
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.first.c_str());
assets.libs[package.first][i].push_back(asset);
}
}
}
}
return true;
}
bool deps_json_t::load_framework_dependent(const pal::string_t& deps_path, const json_value& json, const pal::string_t& target_name, const rid_fallback_graph_t& rid_fallback_graph)
{
if (!process_runtime_targets(json, target_name, rid_fallback_graph, &m_rid_assets))
{
return false;
}
if (!process_targets(json, target_name, &m_assets))
{
return false;
}
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, int 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].rid_assets.empty())
{
const auto& assets_by_type = m_rid_assets.libs[package].rid_assets.begin()->second[type_index];
if (!assets_by_type.empty())
{
*rid_specific = true;
return assets_by_type;
}
trace::verbose(_X("There were no rid specific %s asset for %s"), deps_entry_t::s_known_asset_types[type_index], package.c_str());
}
if (m_assets.libs.count(package))
{
return m_assets.libs[package][type_index];
}
return empty;
};
reconcile_libraries_with_targets(deps_path, json, package_exists, get_relpaths);
return true;
}
bool deps_json_t::load_self_contained(const pal::string_t& deps_path, const json_value& json, const pal::string_t& target_name)
{
if (!process_targets(json, target_name, &m_assets))
{
return false;
}
auto package_exists = [&](const pal::string_t& package) -> bool {
return m_assets.libs.count(package);
};
auto get_relpaths = [&](const pal::string_t& package, int 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.as_object();
const auto iter = json_object.find(_X("runtimes"));
if (iter != json_object.end())
{
for (const auto& rid : iter->second.as_object())
{
auto& vec = m_rid_fallback_graph[rid.first];
for (const auto& fallback : rid.second.as_array())
{
vec.push_back(fallback.as_string());
}
}
}
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("}"));
}
return true;
}
bool deps_json_t::has_package(const pal::string_t& name, const pal::string_t& ver) const
{
pal::string_t pv = name;
pv.push_back(_X('/'));
pv.append(ver);
auto iter = m_rid_assets.libs.find(pv);
if (iter != m_rid_assets.libs.end())
{
if (!iter->second.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.
//
bool 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 = pal::file_exists(deps_path);
// If file doesn't exist, then assume parsed.
if (!m_file_exists)
{
trace::verbose(_X("Could not locate the dependencies manifest file [%s]. Some libraries may fail to resolve."), deps_path.c_str());
return true;
}
// Somehow the file stream could not be opened. This is an error.
pal::ifstream_t file(deps_path);
if (!file.good())
{
trace::error(_X("Could not open dependencies manifest file [%s]"), deps_path.c_str());
return false;
}
if (skip_utf8_bom(&file))
{
trace::verbose(_X("UTF-8 BOM skipped while reading [%s]"), deps_path.c_str());
}
try
{
const auto json = json_value::parse(file);
const auto& runtime_target = json.at(_X("runtimeTarget"));
const pal::string_t& name = runtime_target.is_string()?
runtime_target.as_string():
runtime_target.at(_X("name")).as_string();
trace::verbose(_X("Loading deps file... %s as framework dependent=[%d]"), deps_path.c_str(), is_framework_dependent);
return (is_framework_dependent) ? load_framework_dependent(deps_path, json, name, rid_fallback_graph) : load_self_contained(deps_path, json, name);
}
catch (const std::exception& je)
{
pal::string_t jes;
(void) pal::utf8_palstring(je.what(), &jes);
trace::error(_X("A JSON parsing exception occurred in [%s]: %s"), deps_path.c_str(), jes.c_str());
return false;
}
}