diff options
Diffstat (limited to 'butl')
-rw-r--r-- | butl/buildfile | 2 | ||||
-rw-r--r-- | butl/target-triplet (renamed from butl/triplet) | 61 | ||||
-rw-r--r-- | butl/target-triplet.cxx (renamed from butl/triplet.cxx) | 52 |
3 files changed, 82 insertions, 33 deletions
diff --git a/butl/buildfile b/butl/buildfile index 9168774..dd2e6bb 100644 --- a/butl/buildfile +++ b/butl/buildfile @@ -24,8 +24,8 @@ lib{butl}: \ {hxx cxx}{ sha256 } \ {hxx }{ small-vector } \ {hxx txx }{ string-table } \ +{hxx cxx}{ target-triplet } \ {hxx cxx}{ timestamp } \ -{hxx cxx}{ triplet } \ {hxx ixx cxx}{ utility } \ {hxx }{ vector-view } \ {hxx }{ version } diff --git a/butl/triplet b/butl/target-triplet index 0d1b7f4..d355d75 100644 --- a/butl/triplet +++ b/butl/target-triplet @@ -1,11 +1,12 @@ -// file : butl/triplet -*- C++ -*- +// file : butl/target-triplet -*- C++ -*- // copyright : Copyright (c) 2014-2017 Code Synthesis Ltd // license : MIT; see accompanying LICENSE file -#ifndef BUTL_TRIPLET -#define BUTL_TRIPLET +#ifndef BUTL_TARGET_TRIPLET +#define BUTL_TARGET_TRIPLET #include <string> +#include <ostream> #include <butl/export> @@ -15,7 +16,7 @@ namespace butl // form which, these days, quite often takes the CPU-VENDOR-OS-ABI form. Plus // some fields can sometimes be omitted. This looseness makes it hard to base // any kind of decisions on the triplet without canonicalizing it and then - // splitting it into components. the way we are going to split it is like + // splitting it into components. The way we are going to split it is like // this: // // CPU @@ -95,7 +96,7 @@ namespace butl // // 2. LLVM has the Triple class with similar goals. // - struct LIBBUTL_EXPORT triplet + struct LIBBUTL_EXPORT target_triplet { std::string cpu; std::string vendor; @@ -103,14 +104,52 @@ namespace butl std::string version; std::string class_; - // Parse the triplet optionally returning the canonicalized string. Throw - // std::invalid_argument if the triplet is not recognizable. + // Assemble and returning the canonical (i.e., the one we round-trip) + // target triplet string. + // + std::string + string () const; + + bool + empty () const {return cpu.empty ();} + + int + compare (const target_triplet& y) const + { + int r; + return + (r = cpu.compare (y.cpu)) != 0 ? r : + (r = vendor.compare (y.vendor)) != 0 ? r : + (r = system.compare (y.system)) != 0 ? r : + ( version.compare (y.version)); + } + + // Parse the triplet throw std::invalid_argument if the triplet is not + // recognizable. // explicit - triplet (const std::string&, std::string* canon = nullptr); - triplet (const std::string& s, std::string& canon): triplet (s, &canon) {} - triplet () = default; + target_triplet (const std::string&); + + target_triplet () = default; }; + + inline bool + operator== (const target_triplet& x, const target_triplet& y) + { + return x.compare (y) == 0; + } + + inline bool + operator!= (const target_triplet& x, const target_triplet& y) + { + return !(x == y); + } + + inline std::ostream& + operator<< (std::ostream& o, const target_triplet& x) + { + return o << x.string (); + } }; -#endif // BUTL_TRIPLET +#endif // BUTL_TARGET_TRIPLET diff --git a/butl/triplet.cxx b/butl/target-triplet.cxx index 2177717..f7da724 100644 --- a/butl/triplet.cxx +++ b/butl/target-triplet.cxx @@ -1,8 +1,8 @@ -// file : butl/triplet.cxx -*- C++ -*- +// file : butl/target-triplet.cxx -*- C++ -*- // copyright : Copyright (c) 2014-2017 Code Synthesis Ltd // license : MIT; see accompanying LICENSE file -#include <butl/triplet> +#include <butl/target-triplet> #include <stdexcept> // invalid_argument @@ -10,9 +10,11 @@ using namespace std; namespace butl { - triplet:: - triplet (const string& s, string* c) + target_triplet:: + target_triplet (const std::string& s) { + using std::string; + auto bad = [](const char* m) {throw invalid_argument (m);}; // Find the first and the last components. The first is CPU and the last is @@ -25,9 +27,6 @@ namespace butl cpu.assign (s, 0, f); - if (c != nullptr) - *c = cpu; - // If we have something in between, then the first component after CPU is // VENDOR. Unless it is a first component of two-component system, as in // i686-linux-gnu. @@ -75,15 +74,7 @@ namespace butl if (s.compare (f, n, "pc") != 0 && s.compare (f, n, "none") != 0 && s.compare (f, n, "unknown") != 0) - { vendor.assign (s, f, n); - - if (c != nullptr) - { - *c += '-'; - *c += vendor; - } - } } } @@ -97,12 +88,6 @@ namespace butl if (system.front () == '-' || system.back () == '-') bad ("invalid os/kernel/abi"); - if (c != nullptr) - { - *c += '-'; - *c += system; - } - // Extract VERSION for some recognized systems. // string::size_type v (0); @@ -132,4 +117,29 @@ namespace butl else class_ = "other"; } + + std::string target_triplet:: + string () const + { + std::string r (cpu); + + if (!vendor.empty ()) + { + if (!r.empty ()) r += '-'; + r += vendor; + } + + if (!system.empty ()) + { + if (!r.empty ()) r += '-'; + r += system; + } + + if (!version.empty ()) + { + r += version; + } + + return r; + } } |