diff options
-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 | ||||
-rw-r--r-- | tests/buildfile | 2 | ||||
-rw-r--r-- | tests/target-triplet/buildfile (renamed from tests/triplet/buildfile) | 2 | ||||
-rw-r--r-- | tests/target-triplet/driver.cxx (renamed from tests/triplet/driver.cxx) | 26 |
6 files changed, 97 insertions, 48 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; + } } diff --git a/tests/buildfile b/tests/buildfile index 7259717..8608ed9 100644 --- a/tests/buildfile +++ b/tests/buildfile @@ -4,7 +4,7 @@ d = base64/ cpfile/ dir-iterator/ fdstream/ link/ manifest-parser/ \ manifest-serializer/ manifest-roundtrip/ pager/ path/ prefix-map/ \ - process/ sha256/ small-vector/ strcase/ timestamp/ triplet/ + process/ sha256/ small-vector/ strcase/ timestamp/ target-triplet/ ./: $d include $d diff --git a/tests/triplet/buildfile b/tests/target-triplet/buildfile index 7724f4c..ca8365d 100644 --- a/tests/triplet/buildfile +++ b/tests/target-triplet/buildfile @@ -1,4 +1,4 @@ -# file : tests/triplet/buildfile +# file : tests/target-triplet/buildfile # copyright : Copyright (c) 2014-2017 Code Synthesis Ltd # license : MIT; see accompanying LICENSE file diff --git a/tests/triplet/driver.cxx b/tests/target-triplet/driver.cxx index 96d76cd..fe055db 100644 --- a/tests/triplet/driver.cxx +++ b/tests/target-triplet/driver.cxx @@ -1,4 +1,4 @@ -// file : tests/triplet/driver.cxx -*- C++ -*- +// file : tests/target-triplet/driver.cxx -*- C++ -*- // copyright : Copyright (c) 2014-2017 Code Synthesis Ltd // license : MIT; see accompanying LICENSE file @@ -6,7 +6,7 @@ #include <iostream> #include <stdexcept> // invalid_argument -#include <butl/triplet> +#include <butl/target-triplet> using namespace std; using namespace butl; @@ -118,21 +118,21 @@ test (const char* s, const char* version, const char* class_) { - string c; - triplet t (s, c); + target_triplet t (s); + string c (t.string ()); auto cmp = [] (const string& a, const char* e, const char* n) -> bool + { + if (a != e) { - if (a != e) - { - cerr << n << " actual: " << a << endl - << n << " expect: " << e << endl; + cerr << n << " actual: " << a << endl + << n << " expect: " << e << endl; - return false; - } + return false; + } - return true; - }; + return true; + }; return cmp (c, canon, "canonical") && @@ -148,7 +148,7 @@ fail (const char* s) { try { - triplet t (s); + target_triplet t (s); cerr << "nofail: " << s << endl; return false; } |