OSTargets.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //===--- OSTargets.cpp - Implement OS target feature support --------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements OS specific TargetInfo types.
  10. //===----------------------------------------------------------------------===//
  11. #include "OSTargets.h"
  12. #include "clang/Basic/MacroBuilder.h"
  13. #include "llvm/ADT/StringRef.h"
  14. using namespace clang;
  15. using namespace clang::targets;
  16. namespace clang {
  17. namespace targets {
  18. void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
  19. const llvm::Triple &Triple, StringRef &PlatformName,
  20. VersionTuple &PlatformMinVersion) {
  21. Builder.defineMacro("__APPLE_CC__", "6000");
  22. Builder.defineMacro("__APPLE__");
  23. Builder.defineMacro("__STDC_NO_THREADS__");
  24. // AddressSanitizer doesn't play well with source fortification, which is on
  25. // by default on Darwin.
  26. if (Opts.Sanitize.has(SanitizerKind::Address))
  27. Builder.defineMacro("_FORTIFY_SOURCE", "0");
  28. // Darwin defines __weak, __strong, and __unsafe_unretained even in C mode.
  29. if (!Opts.ObjC) {
  30. // __weak is always defined, for use in blocks and with objc pointers.
  31. Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
  32. Builder.defineMacro("__strong", "");
  33. Builder.defineMacro("__unsafe_unretained", "");
  34. }
  35. if (Opts.Static)
  36. Builder.defineMacro("__STATIC__");
  37. else
  38. Builder.defineMacro("__DYNAMIC__");
  39. if (Opts.POSIXThreads)
  40. Builder.defineMacro("_REENTRANT");
  41. // Get the platform type and version number from the triple.
  42. VersionTuple OsVersion;
  43. if (Triple.isMacOSX()) {
  44. Triple.getMacOSXVersion(OsVersion);
  45. PlatformName = "macos";
  46. } else {
  47. OsVersion = Triple.getOSVersion();
  48. PlatformName = llvm::Triple::getOSTypeName(Triple.getOS());
  49. if (PlatformName == "ios" && Triple.isMacCatalystEnvironment())
  50. PlatformName = "maccatalyst";
  51. }
  52. // If -target arch-pc-win32-macho option specified, we're
  53. // generating code for Win32 ABI. No need to emit
  54. // __ENVIRONMENT_XX_OS_VERSION_MIN_REQUIRED__.
  55. if (PlatformName == "win32") {
  56. PlatformMinVersion = OsVersion;
  57. return;
  58. }
  59. // Set the appropriate OS version define.
  60. if (Triple.isiOS()) {
  61. assert(OsVersion < VersionTuple(100) && "Invalid version!");
  62. char Str[7];
  63. if (OsVersion.getMajor() < 10) {
  64. Str[0] = '0' + OsVersion.getMajor();
  65. Str[1] = '0' + (OsVersion.getMinor().getValueOr(0) / 10);
  66. Str[2] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
  67. Str[3] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
  68. Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
  69. Str[5] = '\0';
  70. } else {
  71. // Handle versions >= 10.
  72. Str[0] = '0' + (OsVersion.getMajor() / 10);
  73. Str[1] = '0' + (OsVersion.getMajor() % 10);
  74. Str[2] = '0' + (OsVersion.getMinor().getValueOr(0) / 10);
  75. Str[3] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
  76. Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
  77. Str[5] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
  78. Str[6] = '\0';
  79. }
  80. if (Triple.isTvOS())
  81. Builder.defineMacro("__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__", Str);
  82. else
  83. Builder.defineMacro("__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__",
  84. Str);
  85. } else if (Triple.isWatchOS()) {
  86. assert(OsVersion < VersionTuple(10) && "Invalid version!");
  87. char Str[6];
  88. Str[0] = '0' + OsVersion.getMajor();
  89. Str[1] = '0' + (OsVersion.getMinor().getValueOr(0) / 10);
  90. Str[2] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
  91. Str[3] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
  92. Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
  93. Str[5] = '\0';
  94. Builder.defineMacro("__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__", Str);
  95. } else if (Triple.isMacOSX()) {
  96. // Note that the Driver allows versions which aren't representable in the
  97. // define (because we only get a single digit for the minor and micro
  98. // revision numbers). So, we limit them to the maximum representable
  99. // version.
  100. assert(OsVersion < VersionTuple(100) && "Invalid version!");
  101. char Str[7];
  102. if (OsVersion < VersionTuple(10, 10)) {
  103. Str[0] = '0' + (OsVersion.getMajor() / 10);
  104. Str[1] = '0' + (OsVersion.getMajor() % 10);
  105. Str[2] = '0' + std::min(OsVersion.getMinor().getValueOr(0), 9U);
  106. Str[3] = '0' + std::min(OsVersion.getSubminor().getValueOr(0), 9U);
  107. Str[4] = '\0';
  108. } else {
  109. // Handle versions > 10.9.
  110. Str[0] = '0' + (OsVersion.getMajor() / 10);
  111. Str[1] = '0' + (OsVersion.getMajor() % 10);
  112. Str[2] = '0' + (OsVersion.getMinor().getValueOr(0) / 10);
  113. Str[3] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
  114. Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
  115. Str[5] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
  116. Str[6] = '\0';
  117. }
  118. Builder.defineMacro("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__", Str);
  119. }
  120. // Tell users about the kernel if there is one.
  121. if (Triple.isOSDarwin())
  122. Builder.defineMacro("__MACH__");
  123. PlatformMinVersion = OsVersion;
  124. }
  125. static void addMinGWDefines(const llvm::Triple &Triple, const LangOptions &Opts,
  126. MacroBuilder &Builder) {
  127. DefineStd(Builder, "WIN32", Opts);
  128. DefineStd(Builder, "WINNT", Opts);
  129. if (Triple.isArch64Bit()) {
  130. DefineStd(Builder, "WIN64", Opts);
  131. Builder.defineMacro("__MINGW64__");
  132. }
  133. Builder.defineMacro("__MSVCRT__");
  134. Builder.defineMacro("__MINGW32__");
  135. addCygMingDefines(Opts, Builder);
  136. }
  137. static void addVisualCDefines(const LangOptions &Opts, MacroBuilder &Builder) {
  138. if (Opts.CPlusPlus) {
  139. if (Opts.RTTIData)
  140. Builder.defineMacro("_CPPRTTI");
  141. if (Opts.CXXExceptions)
  142. Builder.defineMacro("_CPPUNWIND");
  143. }
  144. if (Opts.Bool)
  145. Builder.defineMacro("__BOOL_DEFINED");
  146. if (!Opts.CharIsSigned)
  147. Builder.defineMacro("_CHAR_UNSIGNED");
  148. // FIXME: POSIXThreads isn't exactly the option this should be defined for,
  149. // but it works for now.
  150. if (Opts.POSIXThreads)
  151. Builder.defineMacro("_MT");
  152. if (Opts.MSCompatibilityVersion) {
  153. Builder.defineMacro("_MSC_VER",
  154. Twine(Opts.MSCompatibilityVersion / 100000));
  155. Builder.defineMacro("_MSC_FULL_VER", Twine(Opts.MSCompatibilityVersion));
  156. // FIXME We cannot encode the revision information into 32-bits
  157. Builder.defineMacro("_MSC_BUILD", Twine(1));
  158. if (Opts.CPlusPlus11 && Opts.isCompatibleWithMSVC(LangOptions::MSVC2015))
  159. Builder.defineMacro("_HAS_CHAR16_T_LANGUAGE_SUPPORT", Twine(1));
  160. if (Opts.isCompatibleWithMSVC(LangOptions::MSVC2015)) {
  161. if (Opts.CPlusPlus2b)
  162. Builder.defineMacro("_MSVC_LANG", "202004L");
  163. else if (Opts.CPlusPlus20)
  164. Builder.defineMacro("_MSVC_LANG", "202002L");
  165. else if (Opts.CPlusPlus17)
  166. Builder.defineMacro("_MSVC_LANG", "201703L");
  167. else if (Opts.CPlusPlus14)
  168. Builder.defineMacro("_MSVC_LANG", "201402L");
  169. }
  170. }
  171. if (Opts.MicrosoftExt) {
  172. Builder.defineMacro("_MSC_EXTENSIONS");
  173. if (Opts.CPlusPlus11) {
  174. Builder.defineMacro("_RVALUE_REFERENCES_V2_SUPPORTED");
  175. Builder.defineMacro("_RVALUE_REFERENCES_SUPPORTED");
  176. Builder.defineMacro("_NATIVE_NULLPTR_SUPPORTED");
  177. }
  178. }
  179. Builder.defineMacro("_INTEGRAL_MAX_BITS", "64");
  180. Builder.defineMacro("__STDC_NO_THREADS__");
  181. // Starting with VS 2022 17.1, MSVC predefines the below macro to inform
  182. // users of the execution character set defined at compile time.
  183. // The value given is the Windows Code Page Identifier:
  184. // https://docs.microsoft.com/en-us/windows/win32/intl/code-page-identifiers
  185. //
  186. // Clang currently only supports UTF-8, so we'll use 65001
  187. Builder.defineMacro("_MSVC_EXECUTION_CHARACTER_SET", "65001");
  188. }
  189. void addWindowsDefines(const llvm::Triple &Triple, const LangOptions &Opts,
  190. MacroBuilder &Builder) {
  191. Builder.defineMacro("_WIN32");
  192. if (Triple.isArch64Bit())
  193. Builder.defineMacro("_WIN64");
  194. if (Triple.isWindowsGNUEnvironment())
  195. addMinGWDefines(Triple, Opts, Builder);
  196. else if (Triple.isKnownWindowsMSVCEnvironment() ||
  197. (Triple.isWindowsItaniumEnvironment() && Opts.MSVCCompat))
  198. addVisualCDefines(Opts, Builder);
  199. }
  200. } // namespace targets
  201. } // namespace clang