From 8b55e8151bd69e4ef11a67aff57618193f559618 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 7 Dec 2015 12:16:29 +0200 Subject: Add support for specifying minimum required build2 version The syntax is: using build@0.1.0-a1 The idea is that we will later also use it for modules and 'build' is a special, the "build system itself" module. Also fix a problem with peeking and lexer mode switching. --- build/utility.cxx | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'build/utility.cxx') diff --git a/build/utility.cxx b/build/utility.cxx index d394186..7ee679b 100644 --- a/build/utility.cxx +++ b/build/utility.cxx @@ -4,6 +4,8 @@ #include +#include // strtol() + using namespace std; namespace build @@ -12,5 +14,81 @@ namespace build const path empty_path; const dir_path empty_dir_path; + unsigned int + to_version (const string& s) + { + // See tests/version. + // + + auto parse = [&s] (size_t& p, const char* m, long min = 0, long max = 99) + -> unsigned int + { + if (s[p] == '-' || s[p] == '+') // stoi() allows these. + throw invalid_argument (m); + + const char* b (s.c_str () + p); + char* e; + long r (strtol (b, &e, 10)); + + if (b == e || r < min || r > max) + throw invalid_argument (m); + + p = e - s.c_str (); + return static_cast (r); + }; + + auto bail = [] (const char* m) {throw invalid_argument (m);}; + + unsigned int ma, mi, bf, ab; + + size_t p (0), n (s.size ()); + ma = parse (p, "invalid major version"); + + if (p >= n || s[p] != '.') + bail ("'.' expected after major version"); + + mi = parse (++p, "invalid minor version"); + + if (p >= n || s[p] != '.') + bail ("'.' expected after minor version"); + + bf = parse (++p, "invalid bugfix version"); + + if (p < n) + { + if (s[p] != '-') + bail ("'-' expected after bugfix version"); + + char k (s[++p]); + + if (k != 'a' && k != 'b') + bail ("'a' or 'b' expected in release component"); + + ab = parse (++p, "invalid release component", 1, 49); + + if (p != n) + bail ("junk after release component"); + + if (k == 'b') + ab += 50; + } + + // AABBCCDD + unsigned int r (ma * 1000000U + + mi * 10000U + + bf * 100U); + + if (ab != 0) + { + if (r == 0) + bail ("0.0.0 version with release component"); + + r -= 100; + r += ab; + } + + return r; + } + bool exception_unwinding_dtor = false; } -- cgit v1.1