osspecificaspects.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (C) 2016 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
  3. #pragma once
  4. #include "expected.h"
  5. #include <QDebug>
  6. #include <QString>
  7. #include <algorithm>
  8. #define QTC_WIN_EXE_SUFFIX ".exe"
  9. namespace Utils {
  10. // Add more as needed.
  11. enum OsType { OsTypeWindows, OsTypeLinux, OsTypeMac, OsTypeOtherUnix, OsTypeOther };
  12. enum OsArch { OsArchUnknown, OsArchX86, OsArchAMD64, OsArchItanium, OsArchArm, OsArchArm64 };
  13. inline QString osTypeToString(OsType osType)
  14. {
  15. switch (osType) {
  16. case OsTypeWindows:
  17. return "Windows";
  18. case OsTypeLinux:
  19. return "Linux";
  20. case OsTypeMac:
  21. return "Mac";
  22. case OsTypeOtherUnix:
  23. return "Other Unix";
  24. case OsTypeOther:
  25. default:
  26. return "Other";
  27. }
  28. }
  29. inline Utils::expected_str<OsType> osTypeFromString(const QString &string)
  30. {
  31. if (string.compare("windows", Qt::CaseInsensitive) == 0)
  32. return OsTypeWindows;
  33. if (string.compare("linux", Qt::CaseInsensitive) == 0)
  34. return OsTypeLinux;
  35. if (string.compare("mac", Qt::CaseInsensitive) == 0
  36. || string.compare("darwin", Qt::CaseInsensitive) == 0)
  37. return OsTypeMac;
  38. if (string.compare("other unix", Qt::CaseInsensitive) == 0)
  39. return OsTypeOtherUnix;
  40. return Utils::make_unexpected(QString::fromLatin1("Unknown os type: %1").arg(string));
  41. }
  42. inline Utils::expected_str<OsArch> osArchFromString(const QString &architecture)
  43. {
  44. if (architecture == QLatin1String("x86_64") || architecture == QLatin1String("amd64"))
  45. return OsArchAMD64;
  46. if (architecture == QLatin1String("x86"))
  47. return OsArchX86;
  48. if (architecture == QLatin1String("ia64"))
  49. return OsArchItanium;
  50. if (architecture == QLatin1String("arm"))
  51. return OsArchArm;
  52. if (architecture == QLatin1String("arm64") || architecture == QLatin1String("aarch64"))
  53. return OsArchArm64;
  54. return Utils::make_unexpected(QString::fromLatin1("Unknown architecture: %1").arg(architecture));
  55. }
  56. namespace OsSpecificAspects {
  57. inline QString withExecutableSuffix(OsType osType, const QString &executable)
  58. {
  59. QString finalName = executable;
  60. if (osType == OsTypeWindows && !finalName.endsWith(QTC_WIN_EXE_SUFFIX))
  61. finalName += QLatin1String(QTC_WIN_EXE_SUFFIX);
  62. return finalName;
  63. }
  64. constexpr Qt::CaseSensitivity fileNameCaseSensitivity(OsType osType)
  65. {
  66. return osType == OsTypeWindows || osType == OsTypeMac ? Qt::CaseInsensitive : Qt::CaseSensitive;
  67. }
  68. constexpr Qt::CaseSensitivity envVarCaseSensitivity(OsType osType)
  69. {
  70. return fileNameCaseSensitivity(osType);
  71. }
  72. constexpr QChar pathListSeparator(OsType osType)
  73. {
  74. return QLatin1Char(osType == OsTypeWindows ? ';' : ':');
  75. }
  76. constexpr Qt::KeyboardModifier controlModifier(OsType osType)
  77. {
  78. return osType == OsTypeMac ? Qt::MetaModifier : Qt::ControlModifier;
  79. }
  80. inline QString pathWithNativeSeparators(OsType osType, const QString &pathName)
  81. {
  82. if (osType == OsTypeWindows) {
  83. const int pos = pathName.indexOf('/');
  84. if (pos >= 0) {
  85. QString n = pathName;
  86. std::replace(std::begin(n) + pos, std::end(n), '/', '\\');
  87. return n;
  88. }
  89. } else {
  90. const int pos = pathName.indexOf('\\');
  91. if (pos >= 0) {
  92. QString n = pathName;
  93. std::replace(std::begin(n) + pos, std::end(n), '\\', '/');
  94. return n;
  95. }
  96. }
  97. return pathName;
  98. }
  99. } // namespace OsSpecificAspects
  100. } // namespace Utils