2017-07-21 22:37:03 +00:00
|
|
|
//===--- OSTargets.cpp - Implement OS target feature support --------------===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-07-21 22:37:03 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements OS specific TargetInfo types.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "OSTargets.h"
|
|
|
|
#include "clang/Basic/MacroBuilder.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::targets;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace targets {
|
|
|
|
|
|
|
|
void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
|
|
|
|
const llvm::Triple &Triple, StringRef &PlatformName,
|
|
|
|
VersionTuple &PlatformMinVersion) {
|
|
|
|
Builder.defineMacro("__APPLE_CC__", "6000");
|
|
|
|
Builder.defineMacro("__APPLE__");
|
|
|
|
Builder.defineMacro("__STDC_NO_THREADS__");
|
2020-01-17 17:15:02 -08:00
|
|
|
|
2017-07-21 22:37:03 +00:00
|
|
|
// AddressSanitizer doesn't play well with source fortification, which is on
|
|
|
|
// by default on Darwin.
|
|
|
|
if (Opts.Sanitize.has(SanitizerKind::Address))
|
|
|
|
Builder.defineMacro("_FORTIFY_SOURCE", "0");
|
|
|
|
|
|
|
|
// Darwin defines __weak, __strong, and __unsafe_unretained even in C mode.
|
2018-10-30 20:31:30 +00:00
|
|
|
if (!Opts.ObjC) {
|
2017-07-21 22:37:03 +00:00
|
|
|
// __weak is always defined, for use in blocks and with objc pointers.
|
|
|
|
Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
|
|
|
|
Builder.defineMacro("__strong", "");
|
|
|
|
Builder.defineMacro("__unsafe_unretained", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Opts.Static)
|
|
|
|
Builder.defineMacro("__STATIC__");
|
|
|
|
else
|
|
|
|
Builder.defineMacro("__DYNAMIC__");
|
|
|
|
|
|
|
|
if (Opts.POSIXThreads)
|
|
|
|
Builder.defineMacro("_REENTRANT");
|
|
|
|
|
|
|
|
// Get the platform type and version number from the triple.
|
2021-12-06 17:41:26 +00:00
|
|
|
VersionTuple OsVersion;
|
2017-07-21 22:37:03 +00:00
|
|
|
if (Triple.isMacOSX()) {
|
2021-12-06 17:41:26 +00:00
|
|
|
Triple.getMacOSXVersion(OsVersion);
|
2017-07-21 22:37:03 +00:00
|
|
|
PlatformName = "macos";
|
|
|
|
} else {
|
2021-12-06 17:41:26 +00:00
|
|
|
OsVersion = Triple.getOSVersion();
|
2017-07-21 22:37:03 +00:00
|
|
|
PlatformName = llvm::Triple::getOSTypeName(Triple.getOS());
|
2021-06-30 07:34:26 -07:00
|
|
|
if (PlatformName == "ios" && Triple.isMacCatalystEnvironment())
|
|
|
|
PlatformName = "maccatalyst";
|
2017-07-21 22:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If -target arch-pc-win32-macho option specified, we're
|
|
|
|
// generating code for Win32 ABI. No need to emit
|
|
|
|
// __ENVIRONMENT_XX_OS_VERSION_MIN_REQUIRED__.
|
|
|
|
if (PlatformName == "win32") {
|
2021-12-06 17:41:26 +00:00
|
|
|
PlatformMinVersion = OsVersion;
|
2017-07-21 22:37:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the appropriate OS version define.
|
|
|
|
if (Triple.isiOS()) {
|
2021-12-06 17:41:26 +00:00
|
|
|
assert(OsVersion < VersionTuple(100) && "Invalid version!");
|
2017-07-21 22:37:03 +00:00
|
|
|
char Str[7];
|
2021-12-06 17:41:26 +00:00
|
|
|
if (OsVersion.getMajor() < 10) {
|
|
|
|
Str[0] = '0' + OsVersion.getMajor();
|
|
|
|
Str[1] = '0' + (OsVersion.getMinor().getValueOr(0) / 10);
|
|
|
|
Str[2] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
|
|
|
|
Str[3] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
|
|
|
|
Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
|
2017-07-21 22:37:03 +00:00
|
|
|
Str[5] = '\0';
|
|
|
|
} else {
|
|
|
|
// Handle versions >= 10.
|
2021-12-06 17:41:26 +00:00
|
|
|
Str[0] = '0' + (OsVersion.getMajor() / 10);
|
|
|
|
Str[1] = '0' + (OsVersion.getMajor() % 10);
|
|
|
|
Str[2] = '0' + (OsVersion.getMinor().getValueOr(0) / 10);
|
|
|
|
Str[3] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
|
|
|
|
Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
|
|
|
|
Str[5] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
|
2017-07-21 22:37:03 +00:00
|
|
|
Str[6] = '\0';
|
|
|
|
}
|
|
|
|
if (Triple.isTvOS())
|
|
|
|
Builder.defineMacro("__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__", Str);
|
|
|
|
else
|
|
|
|
Builder.defineMacro("__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__",
|
|
|
|
Str);
|
|
|
|
|
|
|
|
} else if (Triple.isWatchOS()) {
|
2021-12-06 17:41:26 +00:00
|
|
|
assert(OsVersion < VersionTuple(10) && "Invalid version!");
|
2017-07-21 22:37:03 +00:00
|
|
|
char Str[6];
|
2021-12-06 17:41:26 +00:00
|
|
|
Str[0] = '0' + OsVersion.getMajor();
|
|
|
|
Str[1] = '0' + (OsVersion.getMinor().getValueOr(0) / 10);
|
|
|
|
Str[2] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
|
|
|
|
Str[3] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
|
|
|
|
Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
|
2017-07-21 22:37:03 +00:00
|
|
|
Str[5] = '\0';
|
|
|
|
Builder.defineMacro("__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__", Str);
|
2022-03-16 19:31:04 +00:00
|
|
|
} else if (Triple.isDriverKit()) {
|
|
|
|
assert(OsVersion.getMajor() < 100 &&
|
|
|
|
OsVersion.getMinor().getValueOr(0) < 100 &&
|
|
|
|
OsVersion.getSubminor().getValueOr(0) < 100 && "Invalid version!");
|
|
|
|
char Str[7];
|
|
|
|
Str[0] = '0' + (OsVersion.getMajor() / 10);
|
|
|
|
Str[1] = '0' + (OsVersion.getMajor() % 10);
|
|
|
|
Str[2] = '0' + (OsVersion.getMinor().getValueOr(0) / 10);
|
|
|
|
Str[3] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
|
|
|
|
Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
|
|
|
|
Str[5] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
|
|
|
|
Str[6] = '\0';
|
|
|
|
Builder.defineMacro("__ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__", Str);
|
2017-07-21 22:37:03 +00:00
|
|
|
} else if (Triple.isMacOSX()) {
|
|
|
|
// Note that the Driver allows versions which aren't representable in the
|
|
|
|
// define (because we only get a single digit for the minor and micro
|
|
|
|
// revision numbers). So, we limit them to the maximum representable
|
|
|
|
// version.
|
2021-12-06 17:41:26 +00:00
|
|
|
assert(OsVersion < VersionTuple(100) && "Invalid version!");
|
2017-07-21 22:37:03 +00:00
|
|
|
char Str[7];
|
2021-12-06 17:41:26 +00:00
|
|
|
if (OsVersion < VersionTuple(10, 10)) {
|
|
|
|
Str[0] = '0' + (OsVersion.getMajor() / 10);
|
|
|
|
Str[1] = '0' + (OsVersion.getMajor() % 10);
|
|
|
|
Str[2] = '0' + std::min(OsVersion.getMinor().getValueOr(0), 9U);
|
|
|
|
Str[3] = '0' + std::min(OsVersion.getSubminor().getValueOr(0), 9U);
|
2017-07-21 22:37:03 +00:00
|
|
|
Str[4] = '\0';
|
|
|
|
} else {
|
|
|
|
// Handle versions > 10.9.
|
2021-12-06 17:41:26 +00:00
|
|
|
Str[0] = '0' + (OsVersion.getMajor() / 10);
|
|
|
|
Str[1] = '0' + (OsVersion.getMajor() % 10);
|
|
|
|
Str[2] = '0' + (OsVersion.getMinor().getValueOr(0) / 10);
|
|
|
|
Str[3] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
|
|
|
|
Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
|
|
|
|
Str[5] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
|
2017-07-21 22:37:03 +00:00
|
|
|
Str[6] = '\0';
|
|
|
|
}
|
|
|
|
Builder.defineMacro("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__", Str);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell users about the kernel if there is one.
|
|
|
|
if (Triple.isOSDarwin())
|
|
|
|
Builder.defineMacro("__MACH__");
|
|
|
|
|
2021-12-06 17:41:26 +00:00
|
|
|
PlatformMinVersion = OsVersion;
|
2017-07-21 22:37:03 +00:00
|
|
|
}
|
2019-07-09 20:57:28 +00:00
|
|
|
|
|
|
|
static void addMinGWDefines(const llvm::Triple &Triple, const LangOptions &Opts,
|
|
|
|
MacroBuilder &Builder) {
|
|
|
|
DefineStd(Builder, "WIN32", Opts);
|
|
|
|
DefineStd(Builder, "WINNT", Opts);
|
|
|
|
if (Triple.isArch64Bit()) {
|
|
|
|
DefineStd(Builder, "WIN64", Opts);
|
|
|
|
Builder.defineMacro("__MINGW64__");
|
|
|
|
}
|
|
|
|
Builder.defineMacro("__MSVCRT__");
|
|
|
|
Builder.defineMacro("__MINGW32__");
|
|
|
|
addCygMingDefines(Opts, Builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addVisualCDefines(const LangOptions &Opts, MacroBuilder &Builder) {
|
|
|
|
if (Opts.CPlusPlus) {
|
|
|
|
if (Opts.RTTIData)
|
|
|
|
Builder.defineMacro("_CPPRTTI");
|
|
|
|
|
|
|
|
if (Opts.CXXExceptions)
|
|
|
|
Builder.defineMacro("_CPPUNWIND");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Opts.Bool)
|
|
|
|
Builder.defineMacro("__BOOL_DEFINED");
|
|
|
|
|
|
|
|
if (!Opts.CharIsSigned)
|
|
|
|
Builder.defineMacro("_CHAR_UNSIGNED");
|
|
|
|
|
|
|
|
// FIXME: POSIXThreads isn't exactly the option this should be defined for,
|
|
|
|
// but it works for now.
|
|
|
|
if (Opts.POSIXThreads)
|
|
|
|
Builder.defineMacro("_MT");
|
|
|
|
|
|
|
|
if (Opts.MSCompatibilityVersion) {
|
|
|
|
Builder.defineMacro("_MSC_VER",
|
|
|
|
Twine(Opts.MSCompatibilityVersion / 100000));
|
|
|
|
Builder.defineMacro("_MSC_FULL_VER", Twine(Opts.MSCompatibilityVersion));
|
|
|
|
// FIXME We cannot encode the revision information into 32-bits
|
|
|
|
Builder.defineMacro("_MSC_BUILD", Twine(1));
|
|
|
|
|
|
|
|
if (Opts.CPlusPlus11 && Opts.isCompatibleWithMSVC(LangOptions::MSVC2015))
|
|
|
|
Builder.defineMacro("_HAS_CHAR16_T_LANGUAGE_SUPPORT", Twine(1));
|
|
|
|
|
|
|
|
if (Opts.isCompatibleWithMSVC(LangOptions::MSVC2015)) {
|
2021-12-02 09:12:50 -05:00
|
|
|
if (Opts.CPlusPlus2b)
|
|
|
|
Builder.defineMacro("_MSVC_LANG", "202004L");
|
|
|
|
else if (Opts.CPlusPlus20)
|
2021-12-01 08:35:32 -05:00
|
|
|
Builder.defineMacro("_MSVC_LANG", "202002L");
|
2019-07-09 20:57:28 +00:00
|
|
|
else if (Opts.CPlusPlus17)
|
|
|
|
Builder.defineMacro("_MSVC_LANG", "201703L");
|
|
|
|
else if (Opts.CPlusPlus14)
|
|
|
|
Builder.defineMacro("_MSVC_LANG", "201402L");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Opts.MicrosoftExt) {
|
|
|
|
Builder.defineMacro("_MSC_EXTENSIONS");
|
|
|
|
|
|
|
|
if (Opts.CPlusPlus11) {
|
|
|
|
Builder.defineMacro("_RVALUE_REFERENCES_V2_SUPPORTED");
|
|
|
|
Builder.defineMacro("_RVALUE_REFERENCES_SUPPORTED");
|
|
|
|
Builder.defineMacro("_NATIVE_NULLPTR_SUPPORTED");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 06:42:22 -07:00
|
|
|
if (Opts.Kernel)
|
|
|
|
Builder.defineMacro("_KERNEL_MODE");
|
|
|
|
|
2019-07-09 20:57:28 +00:00
|
|
|
Builder.defineMacro("_INTEGRAL_MAX_BITS", "64");
|
2021-10-19 17:38:28 +02:00
|
|
|
Builder.defineMacro("__STDC_NO_THREADS__");
|
2021-11-30 09:13:02 +01:00
|
|
|
|
|
|
|
// Starting with VS 2022 17.1, MSVC predefines the below macro to inform
|
|
|
|
// users of the execution character set defined at compile time.
|
|
|
|
// The value given is the Windows Code Page Identifier:
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/intl/code-page-identifiers
|
|
|
|
//
|
|
|
|
// Clang currently only supports UTF-8, so we'll use 65001
|
|
|
|
Builder.defineMacro("_MSVC_EXECUTION_CHARACTER_SET", "65001");
|
2019-07-09 20:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void addWindowsDefines(const llvm::Triple &Triple, const LangOptions &Opts,
|
|
|
|
MacroBuilder &Builder) {
|
|
|
|
Builder.defineMacro("_WIN32");
|
|
|
|
if (Triple.isArch64Bit())
|
|
|
|
Builder.defineMacro("_WIN64");
|
|
|
|
if (Triple.isWindowsGNUEnvironment())
|
|
|
|
addMinGWDefines(Triple, Opts, Builder);
|
|
|
|
else if (Triple.isKnownWindowsMSVCEnvironment() ||
|
|
|
|
(Triple.isWindowsItaniumEnvironment() && Opts.MSVCCompat))
|
|
|
|
addVisualCDefines(Opts, Builder);
|
|
|
|
}
|
|
|
|
|
2017-07-21 22:37:03 +00:00
|
|
|
} // namespace targets
|
|
|
|
} // namespace clang
|