1 //===--- OSTargets.cpp - Implement OS target feature support --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements OS specific TargetInfo types.
11 //===----------------------------------------------------------------------===//
13 #include "OSTargets.h"
14 #include "clang/Basic/MacroBuilder.h"
15 #include "llvm/ADT/StringRef.h"
17 using namespace clang;
18 using namespace clang::targets;
23 void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
24 const llvm::Triple &Triple, StringRef &PlatformName,
25 VersionTuple &PlatformMinVersion) {
26 Builder.defineMacro("__APPLE_CC__", "6000");
27 Builder.defineMacro("__APPLE__");
28 Builder.defineMacro("__STDC_NO_THREADS__");
29 Builder.defineMacro("OBJC_NEW_PROPERTIES");
30 // AddressSanitizer doesn't play well with source fortification, which is on
31 // by default on Darwin.
32 if (Opts.Sanitize.has(SanitizerKind::Address))
33 Builder.defineMacro("_FORTIFY_SOURCE", "0");
35 // Darwin defines __weak, __strong, and __unsafe_unretained even in C mode.
37 // __weak is always defined, for use in blocks and with objc pointers.
38 Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
39 Builder.defineMacro("__strong", "");
40 Builder.defineMacro("__unsafe_unretained", "");
44 Builder.defineMacro("__STATIC__");
46 Builder.defineMacro("__DYNAMIC__");
48 if (Opts.POSIXThreads)
49 Builder.defineMacro("_REENTRANT");
51 // Get the platform type and version number from the triple.
52 unsigned Maj, Min, Rev;
53 if (Triple.isMacOSX()) {
54 Triple.getMacOSXVersion(Maj, Min, Rev);
55 PlatformName = "macos";
57 Triple.getOSVersion(Maj, Min, Rev);
58 PlatformName = llvm::Triple::getOSTypeName(Triple.getOS());
61 // If -target arch-pc-win32-macho option specified, we're
62 // generating code for Win32 ABI. No need to emit
63 // __ENVIRONMENT_XX_OS_VERSION_MIN_REQUIRED__.
64 if (PlatformName == "win32") {
65 PlatformMinVersion = VersionTuple(Maj, Min, Rev);
69 // Set the appropriate OS version define.
71 assert(Maj < 100 && Min < 100 && Rev < 100 && "Invalid version!");
75 Str[1] = '0' + (Min / 10);
76 Str[2] = '0' + (Min % 10);
77 Str[3] = '0' + (Rev / 10);
78 Str[4] = '0' + (Rev % 10);
81 // Handle versions >= 10.
82 Str[0] = '0' + (Maj / 10);
83 Str[1] = '0' + (Maj % 10);
84 Str[2] = '0' + (Min / 10);
85 Str[3] = '0' + (Min % 10);
86 Str[4] = '0' + (Rev / 10);
87 Str[5] = '0' + (Rev % 10);
91 Builder.defineMacro("__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__", Str);
93 Builder.defineMacro("__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__",
96 } else if (Triple.isWatchOS()) {
97 assert(Maj < 10 && Min < 100 && Rev < 100 && "Invalid version!");
100 Str[1] = '0' + (Min / 10);
101 Str[2] = '0' + (Min % 10);
102 Str[3] = '0' + (Rev / 10);
103 Str[4] = '0' + (Rev % 10);
105 Builder.defineMacro("__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__", Str);
106 } else if (Triple.isMacOSX()) {
107 // Note that the Driver allows versions which aren't representable in the
108 // define (because we only get a single digit for the minor and micro
109 // revision numbers). So, we limit them to the maximum representable
111 assert(Maj < 100 && Min < 100 && Rev < 100 && "Invalid version!");
113 if (Maj < 10 || (Maj == 10 && Min < 10)) {
114 Str[0] = '0' + (Maj / 10);
115 Str[1] = '0' + (Maj % 10);
116 Str[2] = '0' + std::min(Min, 9U);
117 Str[3] = '0' + std::min(Rev, 9U);
120 // Handle versions > 10.9.
121 Str[0] = '0' + (Maj / 10);
122 Str[1] = '0' + (Maj % 10);
123 Str[2] = '0' + (Min / 10);
124 Str[3] = '0' + (Min % 10);
125 Str[4] = '0' + (Rev / 10);
126 Str[5] = '0' + (Rev % 10);
129 Builder.defineMacro("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__", Str);
132 // Tell users about the kernel if there is one.
133 if (Triple.isOSDarwin())
134 Builder.defineMacro("__MACH__");
136 PlatformMinVersion = VersionTuple(Maj, Min, Rev);
138 } // namespace targets