Skip to content

Commit ca68832

Browse files
committed
Compile-time RID list
1 parent 602c48d commit ca68832

File tree

2 files changed

+58
-47
lines changed

2 files changed

+58
-47
lines changed

src/native/corehost/hostpolicy/deps_format.cpp

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <iterator>
1212
#include <cassert>
1313
#include <functional>
14+
#include <minipal/utils.h>
1415

1516
const std::array<const pal::char_t*, deps_entry_t::asset_types::count> deps_entry_t::s_known_asset_types = {{
1617
_X("runtime"), _X("resources"), _X("native")
@@ -182,25 +183,31 @@ void deps_json_t::reconcile_libraries_with_targets(
182183

183184
namespace
184185
{
186+
#define CURRENT_ARCH_SUFFIX _X("-") _STRINGIFY(CURRENT_ARCH_NAME)
187+
#define RID_CURRENT_ARCH_LIST(os) \
188+
_X(os) CURRENT_ARCH_SUFFIX, \
189+
_X(os),
190+
185191
const pal::char_t* s_host_rids[] =
186192
{
187193
#if defined(TARGET_WINDOWS)
188-
_X("win"),
194+
RID_CURRENT_ARCH_LIST("win")
189195
#elif defined(TARGET_OSX)
190-
_X("osx"),
191-
_X("unix"),
196+
RID_CURRENT_ARCH_LIST("osx")
197+
RID_CURRENT_ARCH_LIST("unix")
192198
#elif defined(TARGET_LINUX_MUSL)
193-
_X("linux-musl"),
194-
_X("linux"),
195-
_X("unix"),
199+
RID_CURRENT_ARCH_LIST("linux-musl")
200+
RID_CURRENT_ARCH_LIST("linux")
201+
RID_CURRENT_ARCH_LIST("unix")
196202
#elif defined(TARGET_ANDROID)
197-
_X("linux-bionic"),
198-
_X("linux"),
199-
_X("unix"),
203+
RID_CURRENT_ARCH_LIST("linux-bionic")
204+
RID_CURRENT_ARCH_LIST("linux")
205+
RID_CURRENT_ARCH_LIST("unix")
200206
#else
201-
_X("linux"),
202-
_X("unix"),
207+
RID_CURRENT_ARCH_LIST(FALLBACK_HOST_OS)
208+
RID_CURRENT_ARCH_LIST("unix")
203209
#endif
210+
_X("any"),
204211
};
205212

206213
// Returns the RID determined (computed or fallback) for the platform the host is running on.
@@ -225,51 +232,48 @@ namespace
225232
return currentRid;
226233
}
227234

228-
std::vector<pal::string_t> get_host_rid_list(const pal::string_t& host_rid)
235+
void print_host_rid_list(const pal::string_t& host_rid, const pal::string_t& host_rid_no_arch)
229236
{
230-
std::vector<pal::string_t> rids;
231-
rids.reserve((sizeof(s_host_rids) / sizeof(*s_host_rids)) * 2 + 3);
232-
233-
pal::string_t arch_suffix = _X("-");
234-
arch_suffix.append(get_current_arch_name());
235-
236-
rids.push_back(host_rid);
237-
if (ends_with(host_rid, arch_suffix, true))
238-
{
239-
// Host RID without architecture
240-
rids.push_back(host_rid.substr(0, host_rid.size() - arch_suffix.size()));
241-
}
242-
243-
// Use our list of known portable RIDs
244-
for (const pal::char_t* rid : s_host_rids)
245-
{
246-
// Architecture-specific RID
247-
rids.push_back(rid + arch_suffix);
248-
249-
// RID without architecture
250-
rids.push_back(rid);
251-
}
252-
253-
rids.push_back(_X("any"));
254-
255237
if (trace::is_enabled())
256238
{
257239
trace::verbose(_X("Host RID list = ["));
258-
for (const pal::string_t& rid : rids)
240+
trace::verbose(_X(" %s,"), host_rid.c_str());
241+
if (!host_rid_no_arch.empty())
242+
trace::verbose(_X(" %s,"), host_rid_no_arch.c_str());
243+
244+
for (const pal::char_t* rid : s_host_rids)
259245
{
260-
trace::verbose(_X(" %s,"), rid.c_str());
246+
trace::verbose(_X(" %s,"), rid);
261247
}
262248
trace::verbose(_X("]"));
263249
}
264-
265-
return rids;
266250
}
267251

268-
bool try_get_matching_rid(const std::unordered_map<pal::string_t, std::vector<deps_asset_t>>& rid_assets, const std::vector<pal::string_t>& rid_list, pal::string_t& out_rid)
252+
bool try_get_matching_rid(const std::unordered_map<pal::string_t, std::vector<deps_asset_t>>& rid_assets, const pal::string_t& host_rid, const pal::string_t& host_rid_no_arch, pal::string_t& out_rid)
269253
{
270-
for (const pal::string_t& rid : rid_list)
254+
// Check for exact match with the host RID
255+
if (rid_assets.count(host_rid) != 0)
256+
{
257+
out_rid = host_rid;
258+
return true;
259+
}
260+
261+
// Host RID without architecture
262+
if (!host_rid_no_arch.empty() && rid_assets.count(host_rid_no_arch) != 0)
263+
{
264+
out_rid = host_rid_no_arch;
265+
return true;
266+
}
267+
268+
// Use our list of known portable RIDs
269+
for (const pal::char_t* rid : s_host_rids)
271270
{
272-
if (rid_assets.count(rid) != 0)
271+
const auto& iter = std::find_if(rid_assets.cbegin(), rid_assets.cend(),
272+
[&](const std::pair<pal::string_t, std::vector<deps_asset_t>>& rid_asset)
273+
{
274+
return pal::strcmp(rid_asset.first.c_str(), rid) == 0;
275+
});
276+
if (iter != rid_assets.cend())
273277
{
274278
out_rid = rid;
275279
return true;
@@ -319,9 +323,14 @@ void deps_json_t::perform_rid_fallback(rid_specific_assets_t* portable_assets)
319323

320324
const pal::string_t host_rid = get_current_rid(rid_fallback_graph);
321325

322-
std::vector<pal::string_t> host_rid_list;
326+
pal::string_t host_rid_no_arch;
323327
if (!m_rid_resolution_options.use_fallback_graph)
324-
host_rid_list = get_host_rid_list(host_rid);
328+
{
329+
if (ends_with(host_rid, CURRENT_ARCH_SUFFIX, true))
330+
host_rid_no_arch = host_rid.substr(0, host_rid.size() - STRING_LENGTH(CURRENT_ARCH_SUFFIX));
331+
332+
print_host_rid_list(host_rid, host_rid_no_arch);
333+
}
325334

326335
for (auto& package : portable_assets->libs)
327336
{
@@ -335,7 +344,7 @@ void deps_json_t::perform_rid_fallback(rid_specific_assets_t* portable_assets)
335344
pal::string_t matched_rid;
336345
bool found_match = m_rid_resolution_options.use_fallback_graph
337346
? try_get_matching_rid_with_fallback_graph(rid_assets, host_rid, *rid_fallback_graph, matched_rid)
338-
: try_get_matching_rid(rid_assets, host_rid_list, matched_rid);
347+
: try_get_matching_rid(rid_assets, host_rid, host_rid_no_arch, matched_rid);
339348
if (!found_match)
340349
{
341350
trace::verbose(_X(" No matching %s assets for package %s"), deps_entry_t::s_known_asset_types[asset_type_index], package.first.c_str());

src/native/corehost/setup.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,5 @@ if("${CLI_CMAKE_FALLBACK_OS}" STREQUAL "")
5656
else()
5757
add_definitions(-DFALLBACK_HOST_OS="${CLI_CMAKE_FALLBACK_OS}")
5858
endif()
59+
60+
add_definitions(-DCURRENT_ARCH_NAME="${CLR_CMAKE_TARGET_ARCH}")

0 commit comments

Comments
 (0)