1
0

version.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * version.h
  3. * ---------
  4. * Purpose: OpenMPT version handling.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #include "mptString.h"
  12. #include "openmpt/base/FlagSet.hpp"
  13. #include <stdexcept>
  14. OPENMPT_NAMESPACE_BEGIN
  15. class Version
  16. {
  17. private:
  18. uint32 m_Version; // e.g. 0x01170208
  19. public:
  20. enum class Field
  21. {
  22. Major,
  23. Minor,
  24. Patch,
  25. Test,
  26. };
  27. public:
  28. static Version Current() noexcept;
  29. public:
  30. MPT_CONSTEXPRINLINE Version() noexcept
  31. : m_Version(0)
  32. {}
  33. explicit MPT_CONSTEXPRINLINE Version(uint32 version) noexcept
  34. : m_Version(version)
  35. {}
  36. explicit MPT_CONSTEXPRINLINE Version(uint8 v1, uint8 v2, uint8 v3, uint8 v4) noexcept
  37. : m_Version((static_cast<uint32>(v1) << 24) | (static_cast<uint32>(v2) << 16) | (static_cast<uint32>(v3) << 8) | (static_cast<uint32>(v4) << 0))
  38. {}
  39. public:
  40. mpt::ustring ToUString() const; // e.g "1.17.02.08"
  41. // Returns numerical version value from given version string.
  42. static Version Parse(const mpt::ustring &s);
  43. public:
  44. explicit MPT_CONSTEXPRINLINE operator bool () const noexcept
  45. {
  46. return m_Version != 0;
  47. }
  48. MPT_CONSTEXPRINLINE bool operator ! () const noexcept
  49. {
  50. return m_Version == 0;
  51. }
  52. MPT_CONSTEXPRINLINE uint32 GetRawVersion() const noexcept
  53. {
  54. return m_Version;
  55. }
  56. MPT_FORCEINLINE Version Masked(uint32 mask) const noexcept
  57. {
  58. return Version(m_Version & mask);
  59. }
  60. MPT_CONSTEXPRINLINE uint8 GetField(Field field) const noexcept
  61. {
  62. return
  63. (field == Field::Major) ? static_cast<uint8>((m_Version >> 24) & 0xffu) :
  64. (field == Field::Minor) ? static_cast<uint8>((m_Version >> 16) & 0xffu) :
  65. (field == Field::Patch) ? static_cast<uint8>((m_Version >> 8) & 0xffu) :
  66. (field == Field::Test ) ? static_cast<uint8>((m_Version >> 0) & 0xffu) :
  67. 0u;
  68. }
  69. // Return a version without build number (the last number in the version).
  70. // The current versioning scheme uses this number only for test builds, and it should be 00 for official builds,
  71. // So sometimes it might be wanted to do comparisons without the build number.
  72. Version WithoutTestNumber() const noexcept;
  73. Version WithoutPatchOrTestNumbers() const noexcept;
  74. public:
  75. // Return a OpenMPT version string suitable for file format tags
  76. mpt::ustring GetOpenMPTVersionString() const; // e.g. "OpenMPT 1.17.02.08"
  77. // Returns true if a given version number is from a test build, false if it's a release build.
  78. bool IsTestVersion() const noexcept;
  79. public:
  80. struct LiteralParser
  81. {
  82. public:
  83. // Work-around for GCC 5 which complains about instanciating non-literal type inside a constexpr function when using mpt::constexpr_throw(std::runtime_error("")).
  84. struct ParseException {};
  85. private:
  86. static MPT_CONSTEXPRINLINE uint8 NibbleFromChar(char x)
  87. {
  88. return
  89. ('0' <= x && x <= '9') ? static_cast<uint8>(x - '0' + 0) :
  90. ('a' <= x && x <= 'z') ? static_cast<uint8>(x - 'a' + 10) :
  91. ('A' <= x && x <= 'Z') ? static_cast<uint8>(x - 'A' + 10) :
  92. mpt::constexpr_throw<uint8>(std::domain_error(""));
  93. }
  94. public:
  95. static MPT_CONSTEXPRINLINE Version Parse(const char * str, std::size_t len)
  96. {
  97. // 0123456789
  98. // 1.23.45.67
  99. uint8 v[4] = {0, 0, 0, 0};
  100. std::size_t field = 0;
  101. std::size_t fieldlen = 0;
  102. for(std::size_t i = 0; i < len; ++i)
  103. {
  104. char c = str[i];
  105. if(c == '.')
  106. {
  107. if(field >= 3)
  108. {
  109. mpt::constexpr_throw(ParseException());
  110. }
  111. if(fieldlen == 0)
  112. {
  113. mpt::constexpr_throw(ParseException());
  114. }
  115. field++;
  116. fieldlen = 0;
  117. } else if(('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
  118. {
  119. fieldlen++;
  120. if(fieldlen > 2)
  121. {
  122. mpt::constexpr_throw(ParseException());
  123. }
  124. v[field] <<= 4;
  125. v[field] |= NibbleFromChar(c);
  126. } else
  127. {
  128. mpt::constexpr_throw(ParseException());
  129. }
  130. }
  131. if(fieldlen == 0)
  132. {
  133. mpt::constexpr_throw(ParseException());
  134. }
  135. return Version(v[0], v[1], v[2], v[3]);
  136. }
  137. };
  138. };
  139. MPT_CONSTEXPRINLINE bool operator == (const Version &a, const Version &b) noexcept
  140. {
  141. return a.GetRawVersion() == b.GetRawVersion();
  142. }
  143. MPT_CONSTEXPRINLINE bool operator != (const Version &a, const Version &b) noexcept
  144. {
  145. return a.GetRawVersion() != b.GetRawVersion();
  146. }
  147. MPT_CONSTEXPRINLINE bool operator <= (const Version &a, const Version &b) noexcept
  148. {
  149. return a.GetRawVersion() <= b.GetRawVersion();
  150. }
  151. MPT_CONSTEXPRINLINE bool operator >= (const Version &a, const Version &b) noexcept
  152. {
  153. return a.GetRawVersion() >= b.GetRawVersion();
  154. }
  155. MPT_CONSTEXPRINLINE bool operator < (const Version &a, const Version &b) noexcept
  156. {
  157. return a.GetRawVersion() < b.GetRawVersion();
  158. }
  159. MPT_CONSTEXPRINLINE bool operator > (const Version &a, const Version &b) noexcept
  160. {
  161. return a.GetRawVersion() > b.GetRawVersion();
  162. }
  163. MPT_CONSTEXPRINLINE Version operator "" _LiteralVersionImpl (const char * str, std::size_t len)
  164. {
  165. return Version::LiteralParser::Parse(str, len);
  166. }
  167. // Create Version object from version string and check syntax, all at compile time.
  168. // cppcheck false-positive
  169. // cppcheck-suppress preprocessorErrorDirective
  170. #define MPT_V(strver) MPT_FORCE_CONSTEXPR(Version{( strver ## _LiteralVersionImpl ).GetRawVersion()})
  171. class SourceInfo
  172. {
  173. private:
  174. mpt::ustring m_Url; // svn repository url (or empty string)
  175. int m_Revision; // svn revision (or 0)
  176. bool m_IsDirty; // svn working copy is dirty (or false)
  177. bool m_HasMixedRevisions; // svn working copy has mixed revisions (or false)
  178. bool m_IsPackage; // source code originates from a packaged version of the source code
  179. mpt::ustring m_Date; // svn date (or empty string)
  180. private:
  181. SourceInfo();
  182. public:
  183. static SourceInfo Current();
  184. public:
  185. const mpt::ustring & Url() const { return m_Url; }
  186. int Revision() const { return m_Revision; }
  187. bool IsDirty() const { return m_IsDirty; }
  188. bool HasMixedRevisions() const { return m_HasMixedRevisions; }
  189. bool IsPackage() const { return m_IsPackage; }
  190. const mpt::ustring & Date() const { return m_Date; }
  191. public:
  192. mpt::ustring GetUrlWithRevision() const; // i.e. "https://source.openmpt.org/svn/openmpt/trunk/OpenMPT@1234" or empty string
  193. mpt::ustring GetStateString() const; // i.e. "+dirty" or "clean"
  194. };
  195. struct VersionWithRevision
  196. {
  197. Version version;
  198. uint64 revision;
  199. static VersionWithRevision Current();
  200. static VersionWithRevision Parse(const mpt::ustring &s);
  201. mpt::ustring ToUString() const;
  202. constexpr bool HasRevision() const noexcept
  203. {
  204. return revision != 0;
  205. }
  206. constexpr bool IsEqualTo(VersionWithRevision other) const noexcept
  207. {
  208. return version == other.version && revision == other.revision;
  209. }
  210. constexpr bool IsEquivalentTo(VersionWithRevision other) const noexcept
  211. {
  212. if(version == other.version && revision == other.revision)
  213. {
  214. return true;
  215. }
  216. if(HasRevision() && other.HasRevision())
  217. {
  218. return false;
  219. }
  220. return version == other.version;
  221. }
  222. constexpr bool IsNewerThan(VersionWithRevision other) const noexcept
  223. {
  224. if(version < other.version)
  225. {
  226. return false;
  227. }
  228. if(version > other.version)
  229. {
  230. return true;
  231. }
  232. if(!HasRevision() && !other.HasRevision())
  233. {
  234. return false;
  235. }
  236. if(HasRevision() && other.HasRevision())
  237. {
  238. if(revision < other.revision)
  239. {
  240. return false;
  241. }
  242. if(revision > other.revision)
  243. {
  244. return true;
  245. }
  246. return false;
  247. }
  248. return false;
  249. }
  250. constexpr bool IsOlderThan(VersionWithRevision other) const noexcept
  251. {
  252. if(version < other.version)
  253. {
  254. return true;
  255. }
  256. if(version > other.version)
  257. {
  258. return false;
  259. }
  260. if(!HasRevision() && !other.HasRevision())
  261. {
  262. return false;
  263. }
  264. if(HasRevision() && other.HasRevision())
  265. {
  266. if(revision < other.revision)
  267. {
  268. return true;
  269. }
  270. if(revision > other.revision)
  271. {
  272. return false;
  273. }
  274. return false;
  275. }
  276. return false;
  277. }
  278. };
  279. namespace Build
  280. {
  281. // Returns true if all conditions for an official release build are met
  282. bool IsReleasedBuild();
  283. // Return true if this is a debug build with no optimizations
  284. bool IsDebugBuild();
  285. // Return a string decribing the time of the build process (if built from a svn working copy and tsvn was available during build, otherwise it returns the time version.cpp was last rebuild which could be unreliable as it does not get rebuild every time without tsvn)
  286. mpt::ustring GetBuildDateString();
  287. // Return a string decribing some of the build features
  288. mpt::ustring GetBuildFeaturesString(); // e.g. " NO_VST NO_DSOUND"
  289. // Return a string describing the compiler version used for building.
  290. mpt::ustring GetBuildCompilerString(); // e.g. "Microsoft Compiler 15.00.20706.01"
  291. enum Strings
  292. {
  293. StringsNone = 0,
  294. StringVersion = 1<<0, // "1.23.35.45"
  295. StringRevision = 1<<2, // "-r1234+"
  296. StringSourceInfo = 1<<5, // "https://source.openmpt.org/svn/openmpt/trunk/OpenMPT@1234 (2016-01-02) +dirty"
  297. StringBuildFlags = 1<<6, // "TEST DEBUG"
  298. StringBuildFeatures = 1<<7, // "NO_VST NO_DSOUND"
  299. };
  300. MPT_DECLARE_ENUM(Strings)
  301. // Returns a versions string with the fields selected via @strings.
  302. mpt::ustring GetVersionString(FlagSet<Build::Strings> strings);
  303. // Returns a pure version string
  304. mpt::ustring GetVersionStringPure(); // e.g. "1.17.02.08-r1234+"
  305. // Returns a simple version string
  306. mpt::ustring GetVersionStringSimple(); // e.g. "1.17.02.08-r1234+ TEST"
  307. // Returns Version::CurrentAsString() if the build is a clean release build straight from the repository or an extended string otherwise (if built from a svn working copy and tsvn was available during build)
  308. mpt::ustring GetVersionStringExtended(); // e.g. "1.17.02.08-r1234+ DEBUG"
  309. enum class Url
  310. {
  311. Website,
  312. Download,
  313. Forum,
  314. Bugtracker,
  315. Updates,
  316. TopPicks,
  317. };
  318. // Returns a URL for the respective key.
  319. mpt::ustring GetURL(Build::Url key);
  320. // Returns a multi-line string containing the full credits for the code base
  321. mpt::ustring GetFullCreditsString();
  322. // Returns the OpenMPT license text
  323. mpt::ustring GetLicenseString();
  324. } //namespace Build
  325. OPENMPT_NAMESPACE_END