From 24b18c22c2b57bf7dc4ac024aca97f9c197445ec Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 10 Oct 2014 12:59:58 +0300 Subject: [PATCH] Introduce a helper function in Architecture to pick the best matching name --- source/androidtools.cpp | 22 ++++++---------------- source/architecture.cpp | 24 ++++++++++++++++++++++++ source/architecture.h | 1 + 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/source/androidtools.cpp b/source/androidtools.cpp index c0fe6d1..1042595 100644 --- a/source/androidtools.cpp +++ b/source/androidtools.cpp @@ -75,14 +75,8 @@ void AndroidNdk::find_toolchain_dir() const Architecture &native_arch = builder.get_native_arch(); - list tc_archs = list_files(toolchains_dir/use_toolchain/"prebuilt"); - string use_arch; - for(list::const_iterator i=tc_archs.begin(); i!=tc_archs.end(); ++i) - if(native_arch.match_name(*i)) - { - use_arch = *i; - break; - } + FS::Path tc_archs_dir = toolchains_dir/use_toolchain/"prebuilt"; + string use_arch = native_arch.best_match(list_files(tc_archs_dir)); if(use_arch.empty()) throw runtime_error("No matching toolchain found"); @@ -111,14 +105,10 @@ void AndroidNdk::find_platform_dir() if(use_platform.empty()) throw runtime_error("No applicable platforms found"); - list platform_archs = list_files(platforms_dir/use_platform); - string use_arch; - for(list::const_iterator i=platform_archs.begin(); i!=platform_archs.end(); ++i) - if(!i->compare(0, 5, "arch-") && architecture.match_name(i->substr(5))) - { - use_arch = *i; - break; - } + list platform_archs = list_filtered(platforms_dir/use_platform, "^arch-"); + for(list::iterator i=platform_archs.begin(); i!=platform_archs.end(); ++i) + i->erase(0, 5); + string use_arch = architecture.best_match(platform_archs); if(use_arch.empty()) throw runtime_error("No matching platform found"); diff --git a/source/architecture.cpp b/source/architecture.cpp index a943498..ac31abd 100644 --- a/source/architecture.cpp +++ b/source/architecture.cpp @@ -154,6 +154,30 @@ bool Architecture::match_name(const string &pattern) const return !negate; } +string Architecture::best_match(const list &names) const +{ + string best; + unsigned best_size = 0; + for(list::const_iterator i=names.begin(); i!=names.end(); ++i) + if(match_name(*i)) + { + /* TODO Do full parse and alias resolution here? Otherwise x86 and + x86_64 are treated as equally good, even though the latter is more + specific. */ + unsigned size = 1; + for(string::const_iterator j=i->begin(); j!=i->end(); ++j) + size += (*j=='-'); + + if(size>best_size) + { + best = *i; + best_size = size; + } + } + + return best; +} + void Architecture::resolve_aliases(vector &parts) { for(unsigned i=0; i &) const; bool is_native() const { return native; } bool is_cross() const { return !cross_prefix.empty(); } -- 2.43.0