-#include <limits>
-#include <msp/strings/format.h>
-#include <msp/strings/utils.h>
-#include "architecture.h"
-#include "builder.h"
-#include "executable.h"
-#include "importlibrary.h"
-#include "objectfile.h"
-#include "sharedlibrary.h"
-#include "staticlibrary.h"
-#include "sysutils.h"
-
-using namespace std;
-using namespace Msp;
-
-namespace {
-
-const char *types[] =
-{
- "x86",
- "arm",
- "ppc",
- 0
-};
-
-const char *cpus[] =
-{
- "i386", "x86",
- "i486", "x86",
- "pentium", "x86",
- "pentiumpro", "x86",
- "pentium2", "x86",
- "pentium3", "x86",
- "pentium4", "x86",
- "core2", "x86",
- "nehalem", "x86",
- "k6", "x86",
- "athlon", "x86",
- "athlonxp", "x86",
- "athlon64", "x86",
- "armv5", "arm",
- "armv6", "arm",
- "armv7", "arm",
- "armv7a", "arm",
- 0
-};
-
-const char *fpus[] =
-{
- "387", "x86",
- "sse", "x86",
- "sse3", "x86",
- "sse4.1", "x86",
- "vfpv3", "arm",
- "neon", "arm",
- 0
-};
-
-const char *systems[] =
-{
- "linux",
- "freebsd",
- "darwin",
- "windows",
- "android",
- 0
-};
-
-const char *toolchains[] =
-{
- "gnu",
- "clang",
- "msvc",
- 0
-};
-
-const char *aliases[] =
-{
- "pc", "x86",
- "x86_64", "x86-64",
- "x64", "x86-64",
- "amd64", "x86-64",
- "i586", "pentium",
- "i686", "pentiumpro",
- "corei7", "nehalem",
- "win32", "windows-32",
- "win64", "windows-64",
- "power macintosh", "ppc",
- "armeabi", "arm",
- "v7a", "armv7a",
- "gcc", "gnu",
- "mingw", "windows-gnu",
- 0
-};
-
-}
-
-Architecture::Architecture(Builder &b, const string &spec):
- builder(b)
-{
- if(spec.empty())
- {
- parse_specification(get_system_type());
- // We really only want to set type for the default arch
- cpu.clear();
- bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
- native = true;
- }
- else
- {
- parse_specification(spec);
- const Architecture &native_arch = builder.get_native_arch();
- if(type.empty())
- type = native_arch.type;
- if(system.empty())
- system = native_arch.system;
- if(!bits)
- {
- if(type==native_arch.type)
- bits = native_arch.bits;
- else
- bits = 32;
- }
-
- if(type!=native_arch.type || system!=native_arch.system)
- cross_prefix = format("%s-%s", type, system);
- else if(bits==native_arch.bits)
- native = true;
- }
-
- if(toolchain.empty())
- {
- if((system=="darwin" || system=="freebsd") && builder.get_vfs().find_binary("clang"))
- toolchain = "clang";
- else if(system=="windows" && native)
- toolchain = "msvc";
- else
- toolchain = "gnu";
- }
-
- update();
-}
-
-void Architecture::refine(const string &spec)
-{
- parse_specification(spec);
- update();
-}
-
-void Architecture::update()
-{
- name = type;
- if(!cpu.empty())
- name += format("-%s", cpu);
- if(!fpu.empty())
- name += format("-%s", fpu);
- name += format("-%d-%s-%s", bits, system, toolchain);
-
- filename_patterns.clear();
- if(system=="windows")
- {
- add_pattern<SharedLibrary>("%.dll");
- if(toolchain=="msvc")
- {
- add_pattern<ObjectFile>("%.obj");
- add_pattern<ImportLibrary>("%.lib");
- add_pattern<StaticLibrary>("%_static.lib");
- }
- else
- {
- add_pattern<ObjectFile>("%.o");
- add_pattern<SharedLibrary>("lib%.dll");
- add_pattern<ImportLibrary>("lib%.dll.a");
- add_pattern<StaticLibrary>("lib%.a");
- }
- add_pattern<Executable>("%.exe");
- }
- else
- {
- add_pattern<ObjectFile>("%.o");
- if(system=="darwin")
- add_pattern<SharedLibrary>("lib%.dylib");
- else
- add_pattern<SharedLibrary>("lib%.so");
- add_pattern<StaticLibrary>("lib%.a");
- add_pattern<Executable>("%");
- }
-}
-
-bool Architecture::match_name(const string &pattern, unsigned *quality) const
-{
- bool negate = (pattern[0]=='!');
- vector<string> parts = split(pattern.substr(negate), "-");
- resolve_aliases(parts);
- for(const string &p: parts)
- {
- if((p=="32" && bits==32) || (p=="64" && bits==64))
- ;
- else if(p!=type && p!=cpu && p!=fpu && p!=system && p!=toolchain)
- {
- if(quality)
- *quality = 0;
- return negate;
- }
- }
-
- if(quality)
- *quality = parts.size();
- return !negate;
-}
-
-string Architecture::best_match(const vector<string> &names) const
-{
- string best;
- unsigned best_quality = 0;
- for(const string &n: names)
- {
- unsigned quality;
- if(match_name(n, &quality))
- if(quality>best_quality)
- {
- best = n;
- best_quality = quality;
- }
- }
-
- return best;
-}
-
-template<typename T>
-void Architecture::add_pattern(const string &pat)
-{
- filename_patterns[typeid(T).name()].push_back(Pattern(pat));
-}
-
-void Architecture::resolve_aliases(vector<string> &parts)
-{
- for(unsigned i=0; i<parts.size(); ++i)
- {
- const string &part = parts[i];
- const char *replace = 0;
- for(unsigned j=0; (!replace && aliases[j]); j+=2)
- if(part==aliases[j])
- replace = aliases[j+1];
-
- if(replace)
- {
- bool has_dash = false;
- for(const char *c=replace; (!has_dash && *c); ++c)
- has_dash = (*c=='-');
-
- if(has_dash)
- {
- vector<string> rparts = split(replace, "-");
- parts[i] = rparts[0];
- parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
- i += rparts.size()-1;
- }
- else
- parts[i] = replace;
- }
- }
-}
-
-void Architecture::parse_specification(const string &spec)
-{
- vector<string> parts = split(spec, "-");
- resolve_aliases(parts);
- for(const string &p: parts)
- {
- bool ok = false;
-
- for(unsigned j=0; (!ok && types[j]); ++j)
- if(p==types[j])
- {
- if(!type.empty() && p!=type)
- throw invalid_argument("Conflicting type specification");
- type = p;
- ok = true;
- }
-
- for(unsigned j=0; (!ok && cpus[j]); j+=2)
- if(p==cpus[j])
- {
- if(type.empty())
- type = cpus[j+1];
- else if(cpus[j+1]!=type)
- throw invalid_argument("Conflicting CPU specification");
- cpu = p;
- ok = true;
- }
-
- for(unsigned j=0; (!ok && fpus[j]); j+=2)
- if(p==fpus[j])
- {
- if(fpus[j+1]!=type)
- throw invalid_argument("Conflicting FPU specification");
- fpu = p;
- ok = true;
- }
-
- for(unsigned j=0; (!ok && systems[j]); ++j)
- if(p==systems[j])
- {
- system = p;
- ok = true;
- }
-
- for(unsigned j=0; (!ok && toolchains[j]); ++j)
- if(p==toolchains[j])
- {
- toolchain = p;
- ok = true;
- }
-
- if(!ok && (p=="32" || p=="64"))
- {
- bits = lexical_cast<unsigned>(p);
- ok = true;
- }
-
- if(!ok)
- throw invalid_argument("Unrecognized part in arch specification: "+p);
- }
-}
-
-
-Architecture::Loader::Loader(Architecture &a):
- DataFile::ObjectLoader<Architecture>(a)
-{
- add("prefix", &Architecture::cross_prefix);
-}