| 1 | //===--- Darwin.h - Darwin ToolChain Implementations ------------*- C++ -*-===// |
| 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 | #ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_DARWIN_H |
| 10 | #define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_DARWIN_H |
| 11 | |
| 12 | #include "clang/Basic/DarwinSDKInfo.h" |
| 13 | #include "clang/Basic/LangOptions.h" |
| 14 | #include "clang/Driver/CudaInstallationDetector.h" |
| 15 | #include "clang/Driver/LazyDetector.h" |
| 16 | #include "clang/Driver/RocmInstallationDetector.h" |
| 17 | #include "clang/Driver/SyclInstallationDetector.h" |
| 18 | #include "clang/Driver/Tool.h" |
| 19 | #include "clang/Driver/ToolChain.h" |
| 20 | #include "clang/Driver/XRayArgs.h" |
| 21 | |
| 22 | namespace clang { |
| 23 | namespace driver { |
| 24 | |
| 25 | namespace toolchains { |
| 26 | class MachO; |
| 27 | } // end namespace toolchains |
| 28 | |
| 29 | namespace tools { |
| 30 | |
| 31 | namespace darwin { |
| 32 | llvm::Triple::ArchType getArchTypeForMachOArchName(StringRef Str); |
| 33 | void setTripleTypeForMachOArchName(llvm::Triple &T, StringRef Str, |
| 34 | const llvm::opt::ArgList &Args); |
| 35 | |
| 36 | class LLVM_LIBRARY_VISIBILITY MachOTool : public Tool { |
| 37 | virtual void anchor(); |
| 38 | |
| 39 | protected: |
| 40 | void AddMachOArch(const llvm::opt::ArgList &Args, |
| 41 | llvm::opt::ArgStringList &CmdArgs) const; |
| 42 | void AddMachOArchOnly(const llvm::opt::ArgList &Args, |
| 43 | llvm::opt::ArgStringList &CmdArgs) const; |
| 44 | |
| 45 | const toolchains::MachO &getMachOToolChain() const { |
| 46 | return reinterpret_cast<const toolchains::MachO &>(getToolChain()); |
| 47 | } |
| 48 | |
| 49 | public: |
| 50 | MachOTool(const char *Name, const char *ShortName, const ToolChain &TC) |
| 51 | : Tool(Name, ShortName, TC) {} |
| 52 | }; |
| 53 | |
| 54 | class LLVM_LIBRARY_VISIBILITY Assembler : public MachOTool { |
| 55 | public: |
| 56 | Assembler(const ToolChain &TC) |
| 57 | : MachOTool("darwin::Assembler" , "assembler" , TC) {} |
| 58 | |
| 59 | bool hasIntegratedCPP() const override { return false; } |
| 60 | |
| 61 | void ConstructJob(Compilation &C, const JobAction &JA, |
| 62 | const InputInfo &Output, const InputInfoList &Inputs, |
| 63 | const llvm::opt::ArgList &TCArgs, |
| 64 | const char *LinkingOutput) const override; |
| 65 | }; |
| 66 | |
| 67 | class LLVM_LIBRARY_VISIBILITY Linker : public MachOTool { |
| 68 | bool NeedsTempPath(const InputInfoList &Inputs) const; |
| 69 | void AddLinkArgs(Compilation &C, const llvm::opt::ArgList &Args, |
| 70 | llvm::opt::ArgStringList &CmdArgs, |
| 71 | const InputInfoList &Inputs, VersionTuple Version, |
| 72 | bool LinkerIsLLD, bool UsePlatformVersion) const; |
| 73 | |
| 74 | public: |
| 75 | Linker(const ToolChain &TC) : MachOTool("darwin::Linker" , "linker" , TC) {} |
| 76 | |
| 77 | bool hasIntegratedCPP() const override { return false; } |
| 78 | bool isLinkJob() const override { return true; } |
| 79 | |
| 80 | void ConstructJob(Compilation &C, const JobAction &JA, |
| 81 | const InputInfo &Output, const InputInfoList &Inputs, |
| 82 | const llvm::opt::ArgList &TCArgs, |
| 83 | const char *LinkingOutput) const override; |
| 84 | }; |
| 85 | |
| 86 | class LLVM_LIBRARY_VISIBILITY StaticLibTool : public MachOTool { |
| 87 | public: |
| 88 | StaticLibTool(const ToolChain &TC) |
| 89 | : MachOTool("darwin::StaticLibTool" , "static-lib-linker" , TC) {} |
| 90 | |
| 91 | bool hasIntegratedCPP() const override { return false; } |
| 92 | bool isLinkJob() const override { return true; } |
| 93 | |
| 94 | void ConstructJob(Compilation &C, const JobAction &JA, |
| 95 | const InputInfo &Output, const InputInfoList &Inputs, |
| 96 | const llvm::opt::ArgList &TCArgs, |
| 97 | const char *LinkingOutput) const override; |
| 98 | }; |
| 99 | |
| 100 | class LLVM_LIBRARY_VISIBILITY Lipo : public MachOTool { |
| 101 | public: |
| 102 | Lipo(const ToolChain &TC) : MachOTool("darwin::Lipo" , "lipo" , TC) {} |
| 103 | |
| 104 | bool hasIntegratedCPP() const override { return false; } |
| 105 | |
| 106 | void ConstructJob(Compilation &C, const JobAction &JA, |
| 107 | const InputInfo &Output, const InputInfoList &Inputs, |
| 108 | const llvm::opt::ArgList &TCArgs, |
| 109 | const char *LinkingOutput) const override; |
| 110 | }; |
| 111 | |
| 112 | class LLVM_LIBRARY_VISIBILITY Dsymutil : public MachOTool { |
| 113 | public: |
| 114 | Dsymutil(const ToolChain &TC) |
| 115 | : MachOTool("darwin::Dsymutil" , "dsymutil" , TC) {} |
| 116 | |
| 117 | bool hasIntegratedCPP() const override { return false; } |
| 118 | bool isDsymutilJob() const override { return true; } |
| 119 | |
| 120 | void ConstructJob(Compilation &C, const JobAction &JA, |
| 121 | const InputInfo &Output, const InputInfoList &Inputs, |
| 122 | const llvm::opt::ArgList &TCArgs, |
| 123 | const char *LinkingOutput) const override; |
| 124 | }; |
| 125 | |
| 126 | class LLVM_LIBRARY_VISIBILITY VerifyDebug : public MachOTool { |
| 127 | public: |
| 128 | VerifyDebug(const ToolChain &TC) |
| 129 | : MachOTool("darwin::VerifyDebug" , "dwarfdump" , TC) {} |
| 130 | |
| 131 | bool hasIntegratedCPP() const override { return false; } |
| 132 | |
| 133 | void ConstructJob(Compilation &C, const JobAction &JA, |
| 134 | const InputInfo &Output, const InputInfoList &Inputs, |
| 135 | const llvm::opt::ArgList &TCArgs, |
| 136 | const char *LinkingOutput) const override; |
| 137 | }; |
| 138 | } // end namespace darwin |
| 139 | } // end namespace tools |
| 140 | |
| 141 | namespace toolchains { |
| 142 | |
| 143 | class LLVM_LIBRARY_VISIBILITY MachO : public ToolChain { |
| 144 | protected: |
| 145 | Tool *buildAssembler() const override; |
| 146 | Tool *buildLinker() const override; |
| 147 | Tool *buildStaticLibTool() const override; |
| 148 | Tool *getTool(Action::ActionClass AC) const override; |
| 149 | |
| 150 | void |
| 151 | addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, |
| 152 | llvm::opt::ArgStringList &CC1Args, BoundArch BA, |
| 153 | Action::OffloadKind DeviceOffloadKind) const override; |
| 154 | |
| 155 | private: |
| 156 | mutable std::unique_ptr<tools::darwin::Lipo> Lipo; |
| 157 | mutable std::unique_ptr<tools::darwin::Dsymutil> Dsymutil; |
| 158 | mutable std::unique_ptr<tools::darwin::VerifyDebug> VerifyDebug; |
| 159 | |
| 160 | /// The version of the linker known to be available in the tool chain. |
| 161 | mutable std::optional<VersionTuple> LinkerVersion; |
| 162 | |
| 163 | public: |
| 164 | MachO(const Driver &D, const llvm::Triple &Triple, |
| 165 | const llvm::opt::ArgList &Args); |
| 166 | ~MachO() override; |
| 167 | |
| 168 | /// @name MachO specific toolchain API |
| 169 | /// { |
| 170 | |
| 171 | /// Get the "MachO" arch name for a particular compiler invocation. For |
| 172 | /// example, Apple treats different ARM variations as distinct architectures. |
| 173 | StringRef getMachOArchName(const llvm::opt::ArgList &Args) const; |
| 174 | |
| 175 | /// Get the version of the linker known to be available for a particular |
| 176 | /// compiler invocation (via the `-mlinker-version=` arg). |
| 177 | VersionTuple getLinkerVersion(const llvm::opt::ArgList &Args) const; |
| 178 | |
| 179 | /// Add the linker arguments to link the ARC runtime library. |
| 180 | virtual void AddLinkARCArgs(const llvm::opt::ArgList &Args, |
| 181 | llvm::opt::ArgStringList &CmdArgs) const {} |
| 182 | |
| 183 | /// Add the linker arguments to link the compiler runtime library. |
| 184 | /// |
| 185 | /// FIXME: This API is intended for use with embedded libraries only, and is |
| 186 | /// misleadingly named. |
| 187 | virtual void AddLinkRuntimeLibArgs(const llvm::opt::ArgList &Args, |
| 188 | llvm::opt::ArgStringList &CmdArgs, |
| 189 | bool ForceLinkBuiltinRT = false) const; |
| 190 | |
| 191 | virtual void addStartObjectFileArgs(const llvm::opt::ArgList &Args, |
| 192 | llvm::opt::ArgStringList &CmdArgs) const { |
| 193 | } |
| 194 | |
| 195 | virtual void addMinVersionArgs(const llvm::opt::ArgList &Args, |
| 196 | llvm::opt::ArgStringList &CmdArgs) const {} |
| 197 | |
| 198 | virtual void addPlatformVersionArgs(const llvm::opt::ArgList &Args, |
| 199 | llvm::opt::ArgStringList &CmdArgs) const { |
| 200 | } |
| 201 | |
| 202 | virtual bool HasPlatformPrefix(const llvm::Triple &T) const { return false; } |
| 203 | |
| 204 | virtual void AppendPlatformPrefix(SmallString<128> &Path, |
| 205 | const llvm::Triple &T) const {} |
| 206 | |
| 207 | /// On some iOS platforms, kernel and kernel modules were built statically. Is |
| 208 | /// this such a target? |
| 209 | virtual bool isKernelStatic() const { return false; } |
| 210 | |
| 211 | /// Is the target either iOS or an iOS simulator? |
| 212 | bool isTargetIOSBased() const { return false; } |
| 213 | |
| 214 | /// Options to control how a runtime library is linked. |
| 215 | enum RuntimeLinkOptions : unsigned { |
| 216 | /// Link the library in even if it can't be found in the VFS. |
| 217 | RLO_AlwaysLink = 1 << 0, |
| 218 | |
| 219 | /// Use the embedded runtime from the macho_embedded directory. |
| 220 | RLO_IsEmbedded = 1 << 1, |
| 221 | |
| 222 | /// Emit rpaths for @executable_path as well as the resource directory. |
| 223 | RLO_AddRPath = 1 << 2, |
| 224 | }; |
| 225 | |
| 226 | /// Add a runtime library to the list of items to link. |
| 227 | void AddLinkRuntimeLib(const llvm::opt::ArgList &Args, |
| 228 | llvm::opt::ArgStringList &CmdArgs, StringRef Component, |
| 229 | RuntimeLinkOptions Opts = RuntimeLinkOptions(), |
| 230 | bool IsShared = false) const; |
| 231 | |
| 232 | /// Add any profiling runtime libraries that are needed. This is essentially a |
| 233 | /// MachO specific version of addProfileRT in Tools.cpp. |
| 234 | void addProfileRTLibs(const llvm::opt::ArgList &Args, |
| 235 | llvm::opt::ArgStringList &CmdArgs) const override { |
| 236 | // There aren't any profiling libs for embedded targets currently. |
| 237 | } |
| 238 | |
| 239 | // Return the full path of the compiler-rt library on a non-Darwin MachO |
| 240 | // system. Those are under |
| 241 | // <resourcedir>/lib/darwin/macho_embedded/<...>(.dylib|.a). |
| 242 | std::string getCompilerRT(const llvm::opt::ArgList &Args, StringRef Component, |
| 243 | FileType Type = ToolChain::FT_Static, |
| 244 | bool IsFortran = false) const override; |
| 245 | |
| 246 | /// } |
| 247 | /// @name ToolChain Implementation |
| 248 | /// { |
| 249 | |
| 250 | types::ID LookupTypeForExtension(StringRef Ext) const override; |
| 251 | |
| 252 | bool HasNativeLLVMSupport() const override; |
| 253 | |
| 254 | llvm::opt::DerivedArgList * |
| 255 | TranslateArgs(const llvm::opt::DerivedArgList &Args, BoundArch BA, |
| 256 | Action::OffloadKind DeviceOffloadKind) const override; |
| 257 | |
| 258 | bool IsBlocksDefault() const override { |
| 259 | // Always allow blocks on Apple; users interested in versioning are |
| 260 | // expected to use /usr/include/Block.h. |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | bool IsMathErrnoDefault() const override { return false; } |
| 265 | |
| 266 | bool IsEncodeExtendedBlockSignatureDefault() const override { return true; } |
| 267 | |
| 268 | bool IsObjCNonFragileABIDefault() const override { |
| 269 | // Non-fragile ABI is default for everything but i386. |
| 270 | return getTriple().getArch() != llvm::Triple::x86; |
| 271 | } |
| 272 | |
| 273 | bool UseObjCMixedDispatch() const override { return true; } |
| 274 | |
| 275 | UnwindTableLevel |
| 276 | getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override; |
| 277 | |
| 278 | RuntimeLibType GetDefaultRuntimeLibType() const override { |
| 279 | return ToolChain::RLT_CompilerRT; |
| 280 | } |
| 281 | |
| 282 | bool isPICDefault() const override; |
| 283 | bool isPIEDefault(const llvm::opt::ArgList &Args) const override; |
| 284 | bool isPICDefaultForced() const override; |
| 285 | |
| 286 | bool SupportsProfiling() const override; |
| 287 | |
| 288 | bool UseDwarfDebugFlags() const override; |
| 289 | std::string GetGlobalDebugPathRemapping() const override; |
| 290 | |
| 291 | llvm::ExceptionHandling |
| 292 | GetExceptionModel(const llvm::opt::ArgList &Args) const override { |
| 293 | return llvm::ExceptionHandling::None; |
| 294 | } |
| 295 | |
| 296 | virtual StringRef getOSLibraryNameSuffix(bool IgnoreSim = false) const { |
| 297 | return "" ; |
| 298 | } |
| 299 | |
| 300 | // Darwin toolchain uses legacy thin LTO API, which is not |
| 301 | // capable of unit splitting. |
| 302 | bool canSplitThinLTOUnit() const override { return false; } |
| 303 | /// } |
| 304 | }; |
| 305 | |
| 306 | /// Apple specific MachO extensions |
| 307 | class LLVM_LIBRARY_VISIBILITY AppleMachO : public MachO { |
| 308 | public: |
| 309 | AppleMachO(const Driver &D, const llvm::Triple &Triple, |
| 310 | const llvm::opt::ArgList &Args); |
| 311 | ~AppleMachO() override; |
| 312 | |
| 313 | /// } |
| 314 | /// @name Apple Specific ToolChain Implementation |
| 315 | /// { |
| 316 | void |
| 317 | AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, |
| 318 | llvm::opt::ArgStringList &CC1Args) const override; |
| 319 | |
| 320 | void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs, |
| 321 | llvm::opt::ArgStringList &CC1Args) const override; |
| 322 | void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs, |
| 323 | llvm::opt::ArgStringList &CC1Args) const override; |
| 324 | void addSYCLIncludeArgs(const llvm::opt::ArgList &DriverArgs, |
| 325 | llvm::opt::ArgStringList &CC1Args) const override; |
| 326 | |
| 327 | void AddClangCXXStdlibIncludeArgs( |
| 328 | const llvm::opt::ArgList &DriverArgs, |
| 329 | llvm::opt::ArgStringList &CC1Args) const override; |
| 330 | void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, |
| 331 | llvm::opt::ArgStringList &CmdArgs) const override; |
| 332 | |
| 333 | void printVerboseInfo(raw_ostream &OS) const override; |
| 334 | /// } |
| 335 | |
| 336 | LazyDetector<CudaInstallationDetector> CudaInstallation; |
| 337 | LazyDetector<RocmInstallationDetector> RocmInstallation; |
| 338 | LazyDetector<SYCLInstallationDetector> SYCLInstallation; |
| 339 | |
| 340 | protected: |
| 341 | llvm::SmallString<128> |
| 342 | GetEffectiveSysroot(const llvm::opt::ArgList &DriverArgs) const; |
| 343 | |
| 344 | private: |
| 345 | virtual void |
| 346 | AddGnuCPlusPlusIncludePaths(const llvm::opt::ArgList &DriverArgs, |
| 347 | llvm::opt::ArgStringList &CC1Args) const; |
| 348 | }; |
| 349 | |
| 350 | /// Darwin - The base Darwin tool chain. |
| 351 | class LLVM_LIBRARY_VISIBILITY Darwin : public AppleMachO { |
| 352 | public: |
| 353 | /// Whether the information on the target has been initialized. |
| 354 | // |
| 355 | // FIXME: This should be eliminated. What we want to do is make this part of |
| 356 | // the "default target for arguments" selection process, once we get out of |
| 357 | // the argument translation business. |
| 358 | mutable bool TargetInitialized; |
| 359 | |
| 360 | /// Whether the target was lazily initialized from the triple by |
| 361 | /// ensureTargetInitialized() rather than by AddDeploymentTarget(). Such a |
| 362 | /// target is a best-effort guess that setTarget() may overwrite. |
| 363 | mutable bool TargetInitializedLazily = false; |
| 364 | |
| 365 | // TODO: Are these useful? Can we use Triple::OSType/EnvironmentType instead? |
| 366 | enum DarwinPlatformKind { |
| 367 | MacOS, |
| 368 | IPhoneOS, |
| 369 | TvOS, |
| 370 | WatchOS, |
| 371 | DriverKit, |
| 372 | XROS, |
| 373 | Firmware, |
| 374 | }; |
| 375 | enum DarwinEnvironmentKind { |
| 376 | NativeEnvironment, |
| 377 | Simulator, |
| 378 | MacCatalyst, |
| 379 | }; |
| 380 | |
| 381 | mutable DarwinPlatformKind TargetPlatform; |
| 382 | mutable DarwinEnvironmentKind TargetEnvironment; |
| 383 | |
| 384 | /// The native OS version we are targeting. |
| 385 | mutable VersionTuple TargetVersion; |
| 386 | /// The OS version we are targeting as specified in the triple. |
| 387 | mutable VersionTuple OSTargetVersion; |
| 388 | |
| 389 | /// The information about the darwin SDK that was used. |
| 390 | mutable std::optional<DarwinSDKInfo> SDKInfo; |
| 391 | |
| 392 | /// The target variant triple that was specified (if any). |
| 393 | mutable std::optional<llvm::Triple> TargetVariantTriple; |
| 394 | |
| 395 | private: |
| 396 | void AddDeploymentTarget(llvm::opt::DerivedArgList &Args) const; |
| 397 | |
| 398 | void VerifyTripleForSDK(const llvm::opt::ArgList &Args, |
| 399 | const llvm::Triple &Triple) const; |
| 400 | |
| 401 | protected: |
| 402 | /// Lazily initialize the target platform from the triple when |
| 403 | /// AddDeploymentTarget has not run yet (e.g. when Darwin is used as |
| 404 | /// a host toolchain for device offloading). |
| 405 | void ensureTargetInitialized() const; |
| 406 | |
| 407 | public: |
| 408 | Darwin(const Driver &D, const llvm::Triple &Triple, |
| 409 | const llvm::opt::ArgList &Args); |
| 410 | ~Darwin() override; |
| 411 | |
| 412 | std::string ComputeEffectiveClangTriple(const llvm::opt::ArgList &Args, |
| 413 | BoundArch BA, |
| 414 | types::ID InputType) const override; |
| 415 | |
| 416 | /// @name Darwin Specific Toolchain Implementation |
| 417 | /// { |
| 418 | |
| 419 | void addMinVersionArgs(const llvm::opt::ArgList &Args, |
| 420 | llvm::opt::ArgStringList &CmdArgs) const override; |
| 421 | |
| 422 | void addPlatformVersionArgs(const llvm::opt::ArgList &Args, |
| 423 | llvm::opt::ArgStringList &CmdArgs) const override; |
| 424 | |
| 425 | void addStartObjectFileArgs(const llvm::opt::ArgList &Args, |
| 426 | llvm::opt::ArgStringList &CmdArgs) const override; |
| 427 | |
| 428 | bool isKernelStatic() const override { |
| 429 | return (!(isTargetIPhoneOS() && !isIPhoneOSVersionLT(V0: 6, V1: 0)) && |
| 430 | !isTargetWatchOS() && !isTargetDriverKit()); |
| 431 | } |
| 432 | |
| 433 | void addProfileRTLibs(const llvm::opt::ArgList &Args, |
| 434 | llvm::opt::ArgStringList &CmdArgs) const override; |
| 435 | |
| 436 | // Return the full path of the compiler-rt library on a Darwin MachO system. |
| 437 | // Those are under <resourcedir>/lib/darwin/<...>(.dylib|.a). |
| 438 | std::string getCompilerRT(const llvm::opt::ArgList &Args, StringRef Component, |
| 439 | FileType Type = ToolChain::FT_Static, |
| 440 | bool IsFortran = false) const override; |
| 441 | |
| 442 | protected: |
| 443 | /// } |
| 444 | /// @name Darwin specific Toolchain functions |
| 445 | /// { |
| 446 | |
| 447 | // FIXME: Eliminate these ...Target functions and derive separate tool chains |
| 448 | // for these targets and put version in constructor. |
| 449 | void setTarget(DarwinPlatformKind Platform, DarwinEnvironmentKind Environment, |
| 450 | unsigned Major, unsigned Minor, unsigned Micro, |
| 451 | VersionTuple NativeTargetVersion) const { |
| 452 | // FIXME: For now, allow reinitialization as long as values don't |
| 453 | // change. This will go away when we move away from argument translation. |
| 454 | if (TargetInitialized && TargetPlatform == Platform && |
| 455 | TargetEnvironment == Environment && |
| 456 | (Environment == MacCatalyst ? OSTargetVersion : TargetVersion) == |
| 457 | VersionTuple(Major, Minor, Micro)) { |
| 458 | TargetInitializedLazily = false; |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | // A lazily-initialized target (see ensureTargetInitialized()) is a |
| 463 | // best-effort guess from the triple alone; the authoritative |
| 464 | // initialization from AddDeploymentTarget() may overwrite it. |
| 465 | assert((!TargetInitialized || TargetInitializedLazily) && |
| 466 | "Target already initialized!" ); |
| 467 | TargetInitializedLazily = false; |
| 468 | TargetInitialized = true; |
| 469 | TargetPlatform = Platform; |
| 470 | TargetEnvironment = Environment; |
| 471 | TargetVersion = VersionTuple(Major, Minor, Micro); |
| 472 | if (Environment == Simulator) |
| 473 | const_cast<Darwin *>(this)->setTripleEnvironment(llvm::Triple::Simulator); |
| 474 | else if (Environment == MacCatalyst) { |
| 475 | const_cast<Darwin *>(this)->setTripleEnvironment(llvm::Triple::MacABI); |
| 476 | TargetVersion = NativeTargetVersion; |
| 477 | OSTargetVersion = VersionTuple(Major, Minor, Micro); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | public: |
| 482 | bool isTargetIPhoneOS() const { |
| 483 | assert(TargetInitialized && "Target not initialized!" ); |
| 484 | return (TargetPlatform == IPhoneOS || TargetPlatform == TvOS) && |
| 485 | TargetEnvironment == NativeEnvironment; |
| 486 | } |
| 487 | |
| 488 | bool isTargetIOSSimulator() const { |
| 489 | assert(TargetInitialized && "Target not initialized!" ); |
| 490 | return (TargetPlatform == IPhoneOS || TargetPlatform == TvOS) && |
| 491 | TargetEnvironment == Simulator; |
| 492 | } |
| 493 | |
| 494 | bool isTargetIOSBased() const { |
| 495 | assert(TargetInitialized && "Target not initialized!" ); |
| 496 | return isTargetIPhoneOS() || isTargetIOSSimulator(); |
| 497 | } |
| 498 | |
| 499 | bool isTargetXROSDevice() const { |
| 500 | return TargetPlatform == XROS && TargetEnvironment == NativeEnvironment; |
| 501 | } |
| 502 | |
| 503 | bool isTargetXROSSimulator() const { |
| 504 | return TargetPlatform == XROS && TargetEnvironment == Simulator; |
| 505 | } |
| 506 | |
| 507 | bool isTargetXROS() const { return TargetPlatform == XROS; } |
| 508 | |
| 509 | bool isTargetTvOS() const { |
| 510 | assert(TargetInitialized && "Target not initialized!" ); |
| 511 | return TargetPlatform == TvOS && TargetEnvironment == NativeEnvironment; |
| 512 | } |
| 513 | |
| 514 | bool isTargetTvOSSimulator() const { |
| 515 | assert(TargetInitialized && "Target not initialized!" ); |
| 516 | return TargetPlatform == TvOS && TargetEnvironment == Simulator; |
| 517 | } |
| 518 | |
| 519 | bool isTargetTvOSBased() const { |
| 520 | assert(TargetInitialized && "Target not initialized!" ); |
| 521 | return TargetPlatform == TvOS; |
| 522 | } |
| 523 | |
| 524 | bool isTargetWatchOS() const { |
| 525 | assert(TargetInitialized && "Target not initialized!" ); |
| 526 | return TargetPlatform == WatchOS && TargetEnvironment == NativeEnvironment; |
| 527 | } |
| 528 | |
| 529 | bool isTargetWatchOSSimulator() const { |
| 530 | assert(TargetInitialized && "Target not initialized!" ); |
| 531 | return TargetPlatform == WatchOS && TargetEnvironment == Simulator; |
| 532 | } |
| 533 | |
| 534 | bool isTargetWatchOSBased() const { |
| 535 | assert(TargetInitialized && "Target not initialized!" ); |
| 536 | return TargetPlatform == WatchOS; |
| 537 | } |
| 538 | |
| 539 | bool isTargetDriverKit() const { |
| 540 | assert(TargetInitialized && "Target not initialized!" ); |
| 541 | return TargetPlatform == DriverKit; |
| 542 | } |
| 543 | |
| 544 | bool isTargetFirmware() const { return TargetPlatform == Firmware; } |
| 545 | |
| 546 | bool isTargetMacCatalyst() const { |
| 547 | return TargetPlatform == IPhoneOS && TargetEnvironment == MacCatalyst; |
| 548 | } |
| 549 | |
| 550 | bool isTargetMacOS() const { |
| 551 | assert(TargetInitialized && "Target not initialized!" ); |
| 552 | return TargetPlatform == MacOS; |
| 553 | } |
| 554 | |
| 555 | bool isTargetMacOSBased() const { |
| 556 | assert(TargetInitialized && "Target not initialized!" ); |
| 557 | return TargetPlatform == MacOS || isTargetMacCatalyst(); |
| 558 | } |
| 559 | |
| 560 | bool isTargetAppleSiliconMac() const { |
| 561 | assert(TargetInitialized && "Target not initialized!" ); |
| 562 | return isTargetMacOSBased() && getArch() == llvm::Triple::aarch64; |
| 563 | } |
| 564 | |
| 565 | bool isTargetInitialized() const { return TargetInitialized; } |
| 566 | |
| 567 | /// The version of the OS that's used by the OS specified in the target |
| 568 | /// triple. It might be different from the actual target OS on which the |
| 569 | /// program will run, e.g. MacCatalyst code runs on a macOS target, but its |
| 570 | /// target triple is iOS. |
| 571 | VersionTuple getTripleTargetVersion() const { |
| 572 | assert(TargetInitialized && "Target not initialized!" ); |
| 573 | return isTargetMacCatalyst() ? OSTargetVersion : TargetVersion; |
| 574 | } |
| 575 | |
| 576 | bool isIPhoneOSVersionLT(unsigned V0, unsigned V1 = 0, |
| 577 | unsigned V2 = 0) const { |
| 578 | assert(isTargetIOSBased() && "Unexpected call for non iOS target!" ); |
| 579 | return TargetVersion < VersionTuple(V0, V1, V2); |
| 580 | } |
| 581 | |
| 582 | /// Returns true if the minimum supported macOS version for the slice that's |
| 583 | /// being built is less than the specified version. If there's no minimum |
| 584 | /// supported macOS version, the deployment target version is compared to the |
| 585 | /// specifed version instead. |
| 586 | bool isMacosxVersionLT(unsigned V0, unsigned V1 = 0, unsigned V2 = 0) const { |
| 587 | assert(isTargetMacOSBased() && |
| 588 | (getTriple().isMacOSX() || getTriple().isMacCatalystEnvironment()) && |
| 589 | "Unexpected call for non OS X target!" ); |
| 590 | // The effective triple might not be initialized yet, so construct a |
| 591 | // pseudo-effective triple to get the minimum supported OS version. |
| 592 | VersionTuple MinVers = |
| 593 | llvm::Triple(getTriple().getArchName(), "apple" , "macos" ) |
| 594 | .getMinimumSupportedOSVersion(); |
| 595 | return (!MinVers.empty() && MinVers > TargetVersion |
| 596 | ? MinVers |
| 597 | : TargetVersion) < VersionTuple(V0, V1, V2); |
| 598 | } |
| 599 | |
| 600 | protected: |
| 601 | /// Return true if c++17 aligned allocation/deallocation functions are not |
| 602 | /// implemented in the c++ standard library of the deployment target we are |
| 603 | /// targeting. |
| 604 | bool isAlignedAllocationUnavailable() const; |
| 605 | |
| 606 | /// Return true if c++14 sized deallocation functions are not implemented in |
| 607 | /// the c++ standard library of the deployment target we are targeting. |
| 608 | bool isSizedDeallocationUnavailable() const; |
| 609 | |
| 610 | void |
| 611 | addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, |
| 612 | llvm::opt::ArgStringList &CC1Args, BoundArch BA, |
| 613 | Action::OffloadKind DeviceOffloadKind) const override; |
| 614 | |
| 615 | void addClangCC1ASTargetOptions( |
| 616 | const llvm::opt::ArgList &Args, |
| 617 | llvm::opt::ArgStringList &CC1ASArgs) const override; |
| 618 | |
| 619 | StringRef getOSLibraryNameSuffix(bool IgnoreSim = false) const override; |
| 620 | |
| 621 | public: |
| 622 | static StringRef getSDKName(StringRef isysroot); |
| 623 | |
| 624 | /// } |
| 625 | /// @name ToolChain Implementation |
| 626 | /// { |
| 627 | |
| 628 | // Darwin tools support multiple architecture (e.g., i386 and x86_64) and |
| 629 | // most development is done against SDKs, so compiling for a different |
| 630 | // architecture should not get any special treatment. |
| 631 | bool isCrossCompiling() const override { return false; } |
| 632 | |
| 633 | llvm::opt::DerivedArgList * |
| 634 | TranslateArgs(const llvm::opt::DerivedArgList &Args, BoundArch BA, |
| 635 | Action::OffloadKind DeviceOffloadKind) const override; |
| 636 | |
| 637 | CXXStdlibType GetDefaultCXXStdlibType() const override; |
| 638 | ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const override; |
| 639 | bool hasBlocksRuntime() const override; |
| 640 | |
| 641 | bool UseObjCMixedDispatch() const override { |
| 642 | // This is only used with the non-fragile ABI and non-legacy dispatch. |
| 643 | |
| 644 | // Mixed dispatch is used everywhere except OS X before 10.6. |
| 645 | return !(isTargetMacOSBased() && isMacosxVersionLT(V0: 10, V1: 6)); |
| 646 | } |
| 647 | |
| 648 | LangOptions::StackProtectorMode |
| 649 | GetDefaultStackProtectorLevel(bool KernelOrKext) const override { |
| 650 | // Stack protectors default to on for user code on 10.5, |
| 651 | // and for everything in 10.6 and beyond |
| 652 | if (isTargetIOSBased() || isTargetWatchOSBased() || isTargetDriverKit() || |
| 653 | isTargetXROS()) |
| 654 | return LangOptions::SSPOn; |
| 655 | else if (isTargetMacOSBased() && !isMacosxVersionLT(V0: 10, V1: 6)) |
| 656 | return LangOptions::SSPOn; |
| 657 | else if (isTargetMacOSBased() && !isMacosxVersionLT(V0: 10, V1: 5) && !KernelOrKext) |
| 658 | return LangOptions::SSPOn; |
| 659 | |
| 660 | return LangOptions::SSPOff; |
| 661 | } |
| 662 | |
| 663 | void CheckObjCARC() const override; |
| 664 | |
| 665 | llvm::ExceptionHandling GetExceptionModel( |
| 666 | const llvm::opt::ArgList &Args) const override; |
| 667 | |
| 668 | bool SupportsEmbeddedBitcode() const override; |
| 669 | |
| 670 | SanitizerMask |
| 671 | getSupportedSanitizers(BoundArch BA, |
| 672 | Action::OffloadKind DeviceOffloadKind) const override; |
| 673 | }; |
| 674 | |
| 675 | /// DarwinClang - The Darwin toolchain used by Clang. |
| 676 | class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin { |
| 677 | public: |
| 678 | DarwinClang(const Driver &D, const llvm::Triple &Triple, |
| 679 | const llvm::opt::ArgList &Args); |
| 680 | |
| 681 | /// @name Apple ToolChain Implementation |
| 682 | /// { |
| 683 | |
| 684 | void |
| 685 | AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, |
| 686 | llvm::opt::ArgStringList &CC1Args) const override; |
| 687 | |
| 688 | RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const override; |
| 689 | |
| 690 | void AddLinkRuntimeLibArgs(const llvm::opt::ArgList &Args, |
| 691 | llvm::opt::ArgStringList &CmdArgs, |
| 692 | bool ForceLinkBuiltinRT = false) const override; |
| 693 | |
| 694 | void AddCCKextLibArgs(const llvm::opt::ArgList &Args, |
| 695 | llvm::opt::ArgStringList &CmdArgs) const override; |
| 696 | |
| 697 | void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const override; |
| 698 | |
| 699 | void |
| 700 | addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, |
| 701 | llvm::opt::ArgStringList &CC1Args, BoundArch BA, |
| 702 | Action::OffloadKind DeviceOffloadKind) const override; |
| 703 | |
| 704 | void AddLinkARCArgs(const llvm::opt::ArgList &Args, |
| 705 | llvm::opt::ArgStringList &CmdArgs) const override; |
| 706 | |
| 707 | bool HasPlatformPrefix(const llvm::Triple &T) const override; |
| 708 | |
| 709 | void AppendPlatformPrefix(SmallString<128> &Path, |
| 710 | const llvm::Triple &T) const override; |
| 711 | |
| 712 | unsigned GetDefaultDwarfVersion() const override; |
| 713 | // Until dtrace (via CTF) and LLDB can deal with distributed debug info, |
| 714 | // Darwin defaults to standalone/full debug info. |
| 715 | bool GetDefaultStandaloneDebug() const override { return true; } |
| 716 | llvm::DebuggerKind getDefaultDebuggerTuning() const override { |
| 717 | return llvm::DebuggerKind::LLDB; |
| 718 | } |
| 719 | |
| 720 | bool getDefaultDebugSimpleTemplateNames() const override; |
| 721 | |
| 722 | /// } |
| 723 | |
| 724 | private: |
| 725 | void AddLinkSanitizerLibArgs(const llvm::opt::ArgList &Args, |
| 726 | llvm::opt::ArgStringList &CmdArgs, |
| 727 | StringRef Sanitizer, |
| 728 | bool shared = true) const; |
| 729 | |
| 730 | void |
| 731 | AddGnuCPlusPlusIncludePaths(const llvm::opt::ArgList &DriverArgs, |
| 732 | llvm::opt::ArgStringList &CC1Args) const override; |
| 733 | |
| 734 | bool AddGnuCPlusPlusIncludePaths(const llvm::opt::ArgList &DriverArgs, |
| 735 | llvm::opt::ArgStringList &CC1Args, |
| 736 | llvm::SmallString<128> Base, |
| 737 | llvm::StringRef Version, |
| 738 | llvm::StringRef ArchDir, |
| 739 | llvm::StringRef BitDir) const; |
| 740 | }; |
| 741 | |
| 742 | } // end namespace toolchains |
| 743 | } // end namespace driver |
| 744 | } // end namespace clang |
| 745 | |
| 746 | #endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_DARWIN_H |
| 747 | |