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