| 1 | //===--- Darwin.cpp - Darwin Tool and 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 | #include "Darwin.h" |
| 10 | #include "Arch/ARM.h" |
| 11 | #include "clang/Basic/AlignedAllocation.h" |
| 12 | #include "clang/Basic/ObjCRuntime.h" |
| 13 | #include "clang/Config/config.h" |
| 14 | #include "clang/Driver/CommonArgs.h" |
| 15 | #include "clang/Driver/Compilation.h" |
| 16 | #include "clang/Driver/Driver.h" |
| 17 | #include "clang/Driver/SanitizerArgs.h" |
| 18 | #include "clang/Options/Options.h" |
| 19 | #include "llvm/ADT/StringSwitch.h" |
| 20 | #include "llvm/Option/ArgList.h" |
| 21 | #include "llvm/ProfileData/InstrProf.h" |
| 22 | #include "llvm/ProfileData/MemProf.h" |
| 23 | #include "llvm/Support/Path.h" |
| 24 | #include "llvm/Support/Threading.h" |
| 25 | #include "llvm/Support/VirtualFileSystem.h" |
| 26 | #include "llvm/TargetParser/TargetParser.h" |
| 27 | #include "llvm/TargetParser/Triple.h" |
| 28 | #include <cstdlib> // ::getenv |
| 29 | |
| 30 | using namespace clang::driver; |
| 31 | using namespace clang::driver::tools; |
| 32 | using namespace clang::driver::toolchains; |
| 33 | using namespace clang; |
| 34 | using namespace llvm::opt; |
| 35 | |
| 36 | static VersionTuple minimumMacCatalystDeploymentTarget() { |
| 37 | return VersionTuple(13, 1); |
| 38 | } |
| 39 | |
| 40 | llvm::Triple::ArchType darwin::getArchTypeForMachOArchName(StringRef Str) { |
| 41 | // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for |
| 42 | // archs which Darwin doesn't use. |
| 43 | |
| 44 | // The matching this routine does is fairly pointless, since it is neither the |
| 45 | // complete architecture list, nor a reasonable subset. The problem is that |
| 46 | // historically the driver accepts this and also ties its -march= |
| 47 | // handling to the architecture name, so we need to be careful before removing |
| 48 | // support for it. |
| 49 | |
| 50 | // This code must be kept in sync with Clang's Darwin specific argument |
| 51 | // translation. |
| 52 | |
| 53 | return llvm::StringSwitch<llvm::Triple::ArchType>(Str) |
| 54 | .Cases(CaseStrings: {"i386" , "i486" , "i486SX" , "i586" , "i686" }, Value: llvm::Triple::x86) |
| 55 | .Cases(CaseStrings: {"pentium" , "pentpro" , "pentIIm3" , "pentIIm5" , "pentium4" }, |
| 56 | Value: llvm::Triple::x86) |
| 57 | .Cases(CaseStrings: {"x86_64" , "x86_64h" }, Value: llvm::Triple::x86_64) |
| 58 | // This is derived from the driver. |
| 59 | .Cases(CaseStrings: {"arm" , "armv4t" , "armv5" , "armv6" , "armv6m" }, Value: llvm::Triple::arm) |
| 60 | .Cases(CaseStrings: {"armv7" , "armv7em" , "armv7k" , "armv7m" }, Value: llvm::Triple::arm) |
| 61 | .Cases(CaseStrings: {"armv7s" , "xscale" }, Value: llvm::Triple::arm) |
| 62 | .Cases(CaseStrings: {"arm64" , "arm64e" }, Value: llvm::Triple::aarch64) |
| 63 | .Case(S: "arm64_32" , Value: llvm::Triple::aarch64_32) |
| 64 | .Case(S: "r600" , Value: llvm::Triple::r600) |
| 65 | .Case(S: "amdgcn" , Value: llvm::Triple::amdgcn) |
| 66 | .Case(S: "nvptx" , Value: llvm::Triple::nvptx) |
| 67 | .Case(S: "nvptx64" , Value: llvm::Triple::nvptx64) |
| 68 | .Case(S: "amdil" , Value: llvm::Triple::amdil) |
| 69 | .Case(S: "spir" , Value: llvm::Triple::spir) |
| 70 | .Default(Value: llvm::Triple::UnknownArch); |
| 71 | } |
| 72 | |
| 73 | void darwin::setTripleTypeForMachOArchName(llvm::Triple &T, StringRef Str, |
| 74 | const ArgList &Args) { |
| 75 | const llvm::Triple::ArchType Arch = getArchTypeForMachOArchName(Str); |
| 76 | llvm::ARM::ArchKind ArchKind = llvm::ARM::parseArch(Arch: Str); |
| 77 | T.setArch(Kind: Arch); |
| 78 | if (Arch != llvm::Triple::UnknownArch) |
| 79 | T.setArchName(Str); |
| 80 | |
| 81 | // Standalone/bare metal compiles often unintentionally come out as |
| 82 | // armv6m-apple-ios (-target not specified, or set from Xcode). Change these |
| 83 | // cases to armv6m-apple-unknown-macho to better reflect intent. |
| 84 | if ((T.getOS() != llvm::Triple::Firmware) && |
| 85 | (ArchKind == llvm::ARM::ArchKind::ARMV6M || |
| 86 | ArchKind == llvm::ARM::ArchKind::ARMV7M || |
| 87 | ArchKind == llvm::ARM::ArchKind::ARMV7EM)) { |
| 88 | // Don't reject these -version-min= if we have the appropriate triple. |
| 89 | if (T.getOS() == llvm::Triple::IOS) |
| 90 | for (Arg *A : Args.filtered(Ids: options::OPT_mios_version_min_EQ)) |
| 91 | A->ignoreTargetSpecific(); |
| 92 | if (T.getOS() == llvm::Triple::WatchOS) |
| 93 | for (Arg *A : Args.filtered(Ids: options::OPT_mwatchos_version_min_EQ)) |
| 94 | A->ignoreTargetSpecific(); |
| 95 | if (T.getOS() == llvm::Triple::TvOS) |
| 96 | for (Arg *A : Args.filtered(Ids: options::OPT_mtvos_version_min_EQ)) |
| 97 | A->ignoreTargetSpecific(); |
| 98 | |
| 99 | T.setOS(llvm::Triple::UnknownOS); |
| 100 | T.setObjectFormat(llvm::Triple::MachO); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void darwin::Assembler::ConstructJob(Compilation &C, const JobAction &JA, |
| 105 | const InputInfo &Output, |
| 106 | const InputInfoList &Inputs, |
| 107 | const ArgList &Args, |
| 108 | const char *LinkingOutput) const { |
| 109 | const llvm::Triple &T(getToolChain().getTriple()); |
| 110 | |
| 111 | ArgStringList CmdArgs; |
| 112 | |
| 113 | assert(Inputs.size() == 1 && "Unexpected number of inputs." ); |
| 114 | const InputInfo &Input = Inputs[0]; |
| 115 | |
| 116 | // Determine the original source input. |
| 117 | const Action *SourceAction = &JA; |
| 118 | while (SourceAction->getKind() != Action::InputClass) { |
| 119 | assert(!SourceAction->getInputs().empty() && "unexpected root action!" ); |
| 120 | SourceAction = SourceAction->getInputs()[0]; |
| 121 | } |
| 122 | |
| 123 | // If -fno-integrated-as is used add -Q to the darwin assembler driver to make |
| 124 | // sure it runs its system assembler not clang's integrated assembler. |
| 125 | // Applicable to darwin11+ and Xcode 4+. darwin<10 lacked integrated-as. |
| 126 | // FIXME: at run-time detect assembler capabilities or rely on version |
| 127 | // information forwarded by -target-assembler-version. |
| 128 | if (Args.hasArg(Ids: options::OPT_fno_integrated_as)) { |
| 129 | if (!(T.isMacOSX() && T.isMacOSXVersionLT(Major: 10, Minor: 7))) |
| 130 | CmdArgs.push_back(Elt: "-Q" ); |
| 131 | } |
| 132 | |
| 133 | // Forward -g, assuming we are dealing with an actual assembly file. |
| 134 | if (SourceAction->getType() == types::TY_Asm || |
| 135 | SourceAction->getType() == types::TY_PP_Asm) { |
| 136 | if (Args.hasArg(Ids: options::OPT_gstabs)) |
| 137 | CmdArgs.push_back(Elt: "--gstabs" ); |
| 138 | else if (Args.hasArg(Ids: options::OPT_g_Group)) |
| 139 | CmdArgs.push_back(Elt: "-g" ); |
| 140 | } |
| 141 | |
| 142 | // Derived from asm spec. |
| 143 | AddMachOArch(Args, CmdArgs); |
| 144 | |
| 145 | // Use -force_cpusubtype_ALL on x86 by default. |
| 146 | if (T.isX86() || Args.hasArg(Ids: options::OPT_force__cpusubtype__ALL)) |
| 147 | CmdArgs.push_back(Elt: "-force_cpusubtype_ALL" ); |
| 148 | |
| 149 | if (getToolChain().getArch() != llvm::Triple::x86_64 && |
| 150 | (((Args.hasArg(Ids: options::OPT_mkernel) || |
| 151 | Args.hasArg(Ids: options::OPT_fapple_kext)) && |
| 152 | getMachOToolChain().isKernelStatic()) || |
| 153 | Args.hasArg(Ids: options::OPT_static))) |
| 154 | CmdArgs.push_back(Elt: "-static" ); |
| 155 | |
| 156 | Args.AddAllArgValues(Output&: CmdArgs, Id0: options::OPT_Wa_COMMA, Id1: options::OPT_Xassembler); |
| 157 | |
| 158 | assert(Output.isFilename() && "Unexpected lipo output." ); |
| 159 | CmdArgs.push_back(Elt: "-o" ); |
| 160 | CmdArgs.push_back(Elt: Output.getFilename()); |
| 161 | |
| 162 | assert(Input.isFilename() && "Invalid input." ); |
| 163 | CmdArgs.push_back(Elt: Input.getFilename()); |
| 164 | |
| 165 | // asm_final spec is empty. |
| 166 | |
| 167 | const char *Exec = Args.MakeArgString(Str: getToolChain().GetProgramPath(Name: "as" )); |
| 168 | C.addCommand(C: std::make_unique<Command>(args: JA, args: *this, args: ResponseFileSupport::None(), |
| 169 | args&: Exec, args&: CmdArgs, args: Inputs, args: Output)); |
| 170 | } |
| 171 | |
| 172 | void darwin::MachOTool::anchor() {} |
| 173 | |
| 174 | void darwin::MachOTool::AddMachOArch(const ArgList &Args, |
| 175 | ArgStringList &CmdArgs) const { |
| 176 | StringRef ArchName = getMachOToolChain().getMachOArchName(Args); |
| 177 | |
| 178 | // Derived from darwin_arch spec. |
| 179 | CmdArgs.push_back(Elt: "-arch" ); |
| 180 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: ArchName)); |
| 181 | |
| 182 | // FIXME: Is this needed anymore? |
| 183 | if (ArchName == "arm" ) |
| 184 | CmdArgs.push_back(Elt: "-force_cpusubtype_ALL" ); |
| 185 | } |
| 186 | |
| 187 | bool darwin::Linker::NeedsTempPath(const InputInfoList &Inputs) const { |
| 188 | // We only need to generate a temp path for LTO if we aren't compiling object |
| 189 | // files. When compiling source files, we run 'dsymutil' after linking. We |
| 190 | // don't run 'dsymutil' when compiling object files. |
| 191 | for (const auto &Input : Inputs) |
| 192 | if (Input.getType() != types::TY_Object) |
| 193 | return true; |
| 194 | |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | /// Pass -no_deduplicate to ld64 under certain conditions: |
| 199 | /// |
| 200 | /// - Either -O0 or -O1 is explicitly specified |
| 201 | /// - No -O option is specified *and* this is a compile+link (implicit -O0) |
| 202 | /// |
| 203 | /// Also do *not* add -no_deduplicate when no -O option is specified and this |
| 204 | /// is just a link (we can't imply -O0) |
| 205 | static bool shouldLinkerNotDedup(bool IsLinkerOnlyAction, const ArgList &Args) { |
| 206 | if (Arg *A = Args.getLastArg(Ids: options::OPT_O_Group)) { |
| 207 | if (A->getOption().matches(ID: options::OPT_O0)) |
| 208 | return true; |
| 209 | if (A->getOption().matches(ID: options::OPT_O)) |
| 210 | return llvm::StringSwitch<bool>(A->getValue()) |
| 211 | .Case(S: "1" , Value: true) |
| 212 | .Default(Value: false); |
| 213 | return false; // OPT_Ofast & OPT_O4 |
| 214 | } |
| 215 | |
| 216 | if (!IsLinkerOnlyAction) // Implicit -O0 for compile+linker only. |
| 217 | return true; |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | void darwin::Linker::AddLinkArgs(Compilation &C, const ArgList &Args, |
| 222 | ArgStringList &CmdArgs, |
| 223 | const InputInfoList &Inputs, |
| 224 | VersionTuple Version, bool LinkerIsLLD, |
| 225 | bool UsePlatformVersion) const { |
| 226 | const Driver &D = getToolChain().getDriver(); |
| 227 | const toolchains::MachO &MachOTC = getMachOToolChain(); |
| 228 | |
| 229 | // Newer linkers support -demangle. Pass it if supported and not disabled by |
| 230 | // the user. |
| 231 | if ((Version >= VersionTuple(100) || LinkerIsLLD) && |
| 232 | !Args.hasArg(Ids: options::OPT_Z_Xlinker__no_demangle)) |
| 233 | CmdArgs.push_back(Elt: "-demangle" ); |
| 234 | |
| 235 | if (Args.hasArg(Ids: options::OPT_rdynamic) && |
| 236 | (Version >= VersionTuple(137) || LinkerIsLLD)) |
| 237 | CmdArgs.push_back(Elt: "-export_dynamic" ); |
| 238 | |
| 239 | // If we are using App Extension restrictions, pass a flag to the linker |
| 240 | // telling it that the compiled code has been audited. |
| 241 | if (Args.hasFlag(Pos: options::OPT_fapplication_extension, |
| 242 | Neg: options::OPT_fno_application_extension, Default: false)) |
| 243 | CmdArgs.push_back(Elt: "-application_extension" ); |
| 244 | |
| 245 | if (D.isUsingLTO() && (Version >= VersionTuple(116) || LinkerIsLLD) && |
| 246 | NeedsTempPath(Inputs)) { |
| 247 | std::string TmpPathName; |
| 248 | if (D.getLTOMode() == LTOK_Full) { |
| 249 | // If we are using full LTO, then automatically create a temporary file |
| 250 | // path for the linker to use, so that it's lifetime will extend past a |
| 251 | // possible dsymutil step. |
| 252 | TmpPathName = |
| 253 | D.GetTemporaryPath(Prefix: "cc" , Suffix: types::getTypeTempSuffix(Id: types::TY_Object)); |
| 254 | } else if (D.getLTOMode() == LTOK_Thin) |
| 255 | // If we are using thin LTO, then create a directory instead. |
| 256 | TmpPathName = D.GetTemporaryDirectory(Prefix: "thinlto" ); |
| 257 | |
| 258 | if (!TmpPathName.empty()) { |
| 259 | auto *TmpPath = C.getArgs().MakeArgString(Str: TmpPathName); |
| 260 | C.addTempFile(Name: TmpPath); |
| 261 | CmdArgs.push_back(Elt: "-object_path_lto" ); |
| 262 | CmdArgs.push_back(Elt: TmpPath); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // Use -lto_library option to specify the libLTO.dylib path. Try to find |
| 267 | // it in clang installed libraries. ld64 will only look at this argument |
| 268 | // when it actually uses LTO, so libLTO.dylib only needs to exist at link |
| 269 | // time if ld64 decides that it needs to use LTO. |
| 270 | // Since this is passed unconditionally, ld64 will never look for libLTO.dylib |
| 271 | // next to it. That's ok since ld64 using a libLTO.dylib not matching the |
| 272 | // clang version won't work anyways. |
| 273 | // lld is built at the same revision as clang and statically links in |
| 274 | // LLVM libraries, so it doesn't need libLTO.dylib. |
| 275 | if (Version >= VersionTuple(133) && !LinkerIsLLD) { |
| 276 | // Search for libLTO in <InstalledDir>/../lib/libLTO.dylib |
| 277 | StringRef P = llvm::sys::path::parent_path(path: D.Dir); |
| 278 | SmallString<128> LibLTOPath(P); |
| 279 | llvm::sys::path::append(path&: LibLTOPath, a: "lib" ); |
| 280 | llvm::sys::path::append(path&: LibLTOPath, a: "libLTO.dylib" ); |
| 281 | CmdArgs.push_back(Elt: "-lto_library" ); |
| 282 | CmdArgs.push_back(Elt: C.getArgs().MakeArgString(Str: LibLTOPath)); |
| 283 | } |
| 284 | |
| 285 | // ld64 version 262 and above runs the deduplicate pass by default. |
| 286 | // FIXME: lld doesn't dedup by default. Should we pass `--icf=safe` |
| 287 | // if `!shouldLinkerNotDedup()` if LinkerIsLLD here? |
| 288 | if (Version >= VersionTuple(262) && |
| 289 | shouldLinkerNotDedup(IsLinkerOnlyAction: C.getJobs().empty(), Args)) |
| 290 | CmdArgs.push_back(Elt: "-no_deduplicate" ); |
| 291 | |
| 292 | // Derived from the "link" spec. |
| 293 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_static); |
| 294 | if (!Args.hasArg(Ids: options::OPT_static)) |
| 295 | CmdArgs.push_back(Elt: "-dynamic" ); |
| 296 | if (Args.hasArg(Ids: options::OPT_fgnu_runtime)) { |
| 297 | // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu |
| 298 | // here. How do we wish to handle such things? |
| 299 | } |
| 300 | |
| 301 | if (!Args.hasArg(Ids: options::OPT_dynamiclib)) { |
| 302 | AddMachOArch(Args, CmdArgs); |
| 303 | // FIXME: Why do this only on this path? |
| 304 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_force__cpusubtype__ALL); |
| 305 | |
| 306 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_bundle); |
| 307 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_bundle__loader); |
| 308 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_client__name); |
| 309 | |
| 310 | Arg *A; |
| 311 | if ((A = Args.getLastArg(Ids: options::OPT_compatibility__version)) || |
| 312 | (A = Args.getLastArg(Ids: options::OPT_current__version)) || |
| 313 | (A = Args.getLastArg(Ids: options::OPT_install__name))) |
| 314 | D.Diag(DiagID: diag::err_drv_argument_only_allowed_with) << A->getAsString(Args) |
| 315 | << "-dynamiclib" ; |
| 316 | |
| 317 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_force__flat__namespace); |
| 318 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_keep__private__externs); |
| 319 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_private__bundle); |
| 320 | } else { |
| 321 | CmdArgs.push_back(Elt: "-dylib" ); |
| 322 | |
| 323 | Arg *A; |
| 324 | if ((A = Args.getLastArg(Ids: options::OPT_bundle)) || |
| 325 | (A = Args.getLastArg(Ids: options::OPT_bundle__loader)) || |
| 326 | (A = Args.getLastArg(Ids: options::OPT_client__name)) || |
| 327 | (A = Args.getLastArg(Ids: options::OPT_force__flat__namespace)) || |
| 328 | (A = Args.getLastArg(Ids: options::OPT_keep__private__externs)) || |
| 329 | (A = Args.getLastArg(Ids: options::OPT_private__bundle))) |
| 330 | D.Diag(DiagID: diag::err_drv_argument_not_allowed_with) << A->getAsString(Args) |
| 331 | << "-dynamiclib" ; |
| 332 | |
| 333 | Args.AddAllArgsTranslated(Output&: CmdArgs, Id0: options::OPT_compatibility__version, |
| 334 | Translation: "-dylib_compatibility_version" ); |
| 335 | Args.AddAllArgsTranslated(Output&: CmdArgs, Id0: options::OPT_current__version, |
| 336 | Translation: "-dylib_current_version" ); |
| 337 | |
| 338 | AddMachOArch(Args, CmdArgs); |
| 339 | |
| 340 | Args.AddAllArgsTranslated(Output&: CmdArgs, Id0: options::OPT_install__name, |
| 341 | Translation: "-dylib_install_name" ); |
| 342 | } |
| 343 | |
| 344 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_all__load); |
| 345 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_allowable__client); |
| 346 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_bind__at__load); |
| 347 | if (MachOTC.isTargetIOSBased()) |
| 348 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_arch__errors__fatal); |
| 349 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_dead__strip); |
| 350 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_no__dead__strip__inits__and__terms); |
| 351 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_dylib__file); |
| 352 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_dynamic); |
| 353 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_exported__symbols__list); |
| 354 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_flat__namespace); |
| 355 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_force__load); |
| 356 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_headerpad__max__install__names); |
| 357 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_image__base); |
| 358 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_init); |
| 359 | |
| 360 | // Add the deployment target. |
| 361 | if (Version >= VersionTuple(520) || LinkerIsLLD || UsePlatformVersion) |
| 362 | MachOTC.addPlatformVersionArgs(Args, CmdArgs); |
| 363 | else |
| 364 | MachOTC.addMinVersionArgs(Args, CmdArgs); |
| 365 | |
| 366 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_nomultidefs); |
| 367 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_multi__module); |
| 368 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_single__module); |
| 369 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_multiply__defined); |
| 370 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_multiply__defined__unused); |
| 371 | |
| 372 | if (const Arg *A = |
| 373 | Args.getLastArg(Ids: options::OPT_fpie, Ids: options::OPT_fPIE, |
| 374 | Ids: options::OPT_fno_pie, Ids: options::OPT_fno_PIE)) { |
| 375 | if (A->getOption().matches(ID: options::OPT_fpie) || |
| 376 | A->getOption().matches(ID: options::OPT_fPIE)) |
| 377 | CmdArgs.push_back(Elt: "-pie" ); |
| 378 | else |
| 379 | CmdArgs.push_back(Elt: "-no_pie" ); |
| 380 | } |
| 381 | |
| 382 | // for embed-bitcode, use -bitcode_bundle in linker command |
| 383 | if (C.getDriver().embedBitcodeEnabled()) { |
| 384 | // Check if the toolchain supports bitcode build flow. |
| 385 | if (MachOTC.SupportsEmbeddedBitcode()) { |
| 386 | CmdArgs.push_back(Elt: "-bitcode_bundle" ); |
| 387 | // FIXME: Pass this if LinkerIsLLD too, once it implements this flag. |
| 388 | if (C.getDriver().embedBitcodeMarkerOnly() && |
| 389 | Version >= VersionTuple(278)) { |
| 390 | CmdArgs.push_back(Elt: "-bitcode_process_mode" ); |
| 391 | CmdArgs.push_back(Elt: "marker" ); |
| 392 | } |
| 393 | } else |
| 394 | D.Diag(DiagID: diag::err_drv_bitcode_unsupported_on_toolchain); |
| 395 | } |
| 396 | |
| 397 | // If GlobalISel is enabled, pass it through to LLVM. |
| 398 | if (Arg *A = Args.getLastArg(Ids: options::OPT_fglobal_isel, |
| 399 | Ids: options::OPT_fno_global_isel)) { |
| 400 | if (A->getOption().matches(ID: options::OPT_fglobal_isel)) { |
| 401 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 402 | CmdArgs.push_back(Elt: "-global-isel" ); |
| 403 | // Disable abort and fall back to SDAG silently. |
| 404 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 405 | CmdArgs.push_back(Elt: "-global-isel-abort=0" ); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | if (Args.hasArg(Ids: options::OPT_mkernel) || |
| 410 | Args.hasArg(Ids: options::OPT_fapple_kext) || |
| 411 | Args.hasArg(Ids: options::OPT_ffreestanding)) { |
| 412 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 413 | CmdArgs.push_back(Elt: "-disable-atexit-based-global-dtor-lowering" ); |
| 414 | } |
| 415 | |
| 416 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_prebind); |
| 417 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_noprebind); |
| 418 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_nofixprebinding); |
| 419 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_prebind__all__twolevel__modules); |
| 420 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_read__only__relocs); |
| 421 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_sectcreate); |
| 422 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_sectorder); |
| 423 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_seg1addr); |
| 424 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_segprot); |
| 425 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_segaddr); |
| 426 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_segs__read__only__addr); |
| 427 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_segs__read__write__addr); |
| 428 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_seg__addr__table); |
| 429 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_seg__addr__table__filename); |
| 430 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_sub__library); |
| 431 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_sub__umbrella); |
| 432 | |
| 433 | // Give --sysroot= preference, over the Apple specific behavior to also use |
| 434 | // --isysroot as the syslibroot. |
| 435 | // We check `OPT__sysroot_EQ` directly instead of `getSysRoot` to make sure we |
| 436 | // prioritise command line arguments over configuration of `DEFAULT_SYSROOT`. |
| 437 | if (const Arg *A = Args.getLastArg(Ids: options::OPT__sysroot_EQ)) { |
| 438 | CmdArgs.push_back(Elt: "-syslibroot" ); |
| 439 | CmdArgs.push_back(Elt: A->getValue()); |
| 440 | } else if (const Arg *A = Args.getLastArg(Ids: options::OPT_isysroot)) { |
| 441 | CmdArgs.push_back(Elt: "-syslibroot" ); |
| 442 | CmdArgs.push_back(Elt: A->getValue()); |
| 443 | } else if (StringRef sysroot = C.getSysRoot(); sysroot != "" ) { |
| 444 | CmdArgs.push_back(Elt: "-syslibroot" ); |
| 445 | CmdArgs.push_back(Elt: C.getArgs().MakeArgString(Str: sysroot)); |
| 446 | } |
| 447 | |
| 448 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_twolevel__namespace); |
| 449 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_twolevel__namespace__hints); |
| 450 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_umbrella); |
| 451 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_undefined); |
| 452 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_unexported__symbols__list); |
| 453 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_weak__reference__mismatches); |
| 454 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_X_Flag); |
| 455 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_y); |
| 456 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_w); |
| 457 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_pagezero__size); |
| 458 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_segs__read__); |
| 459 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_seglinkedit); |
| 460 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_noseglinkedit); |
| 461 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_sectalign); |
| 462 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_sectobjectsymbols); |
| 463 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_segcreate); |
| 464 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_why_load); |
| 465 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_whatsloaded); |
| 466 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_dylinker__install__name); |
| 467 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_dylinker); |
| 468 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_Mach); |
| 469 | |
| 470 | if (LinkerIsLLD) { |
| 471 | if (auto *CSPGOGenerateArg = getLastCSProfileGenerateArg(Args)) { |
| 472 | SmallString<128> Path(CSPGOGenerateArg->getNumValues() == 0 |
| 473 | ? "" |
| 474 | : CSPGOGenerateArg->getValue()); |
| 475 | llvm::sys::path::append(path&: Path, a: "default_%m.profraw" ); |
| 476 | CmdArgs.push_back(Elt: "--cs-profile-generate" ); |
| 477 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine("--cs-profile-path=" ) + Path)); |
| 478 | } else if (auto *ProfileUseArg = getLastProfileUseArg(Args)) { |
| 479 | SmallString<128> Path( |
| 480 | ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue()); |
| 481 | if (Path.empty() || llvm::sys::fs::is_directory(Path)) |
| 482 | llvm::sys::path::append(path&: Path, a: "default.profdata" ); |
| 483 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine("--cs-profile-path=" ) + Path)); |
| 484 | } |
| 485 | |
| 486 | auto *CodeGenDataGenArg = |
| 487 | Args.getLastArg(Ids: options::OPT_fcodegen_data_generate_EQ); |
| 488 | if (CodeGenDataGenArg) |
| 489 | CmdArgs.push_back( |
| 490 | Elt: Args.MakeArgString(Str: Twine("--codegen-data-generate-path=" ) + |
| 491 | CodeGenDataGenArg->getValue())); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | /// Determine whether we are linking the ObjC runtime. |
| 496 | static bool isObjCRuntimeLinked(const ArgList &Args) { |
| 497 | if (isObjCAutoRefCount(Args)) { |
| 498 | Args.ClaimAllArgs(Id0: options::OPT_fobjc_link_runtime); |
| 499 | return true; |
| 500 | } |
| 501 | return Args.hasArg(Ids: options::OPT_fobjc_link_runtime); |
| 502 | } |
| 503 | |
| 504 | static bool (const Driver &D, const ArgList &Args, |
| 505 | const llvm::Triple &Triple) { |
| 506 | // When enabling remarks, we need to error if: |
| 507 | // * The remark file is specified but we're targeting multiple architectures, |
| 508 | // which means more than one remark file is being generated. |
| 509 | bool hasMultipleInvocations = |
| 510 | Args.getAllArgValues(Id: options::OPT_arch).size() > 1; |
| 511 | bool hasExplicitOutputFile = |
| 512 | Args.getLastArg(Ids: options::OPT_foptimization_record_file_EQ); |
| 513 | if (hasMultipleInvocations && hasExplicitOutputFile) { |
| 514 | D.Diag(DiagID: diag::err_drv_invalid_output_with_multiple_archs) |
| 515 | << "-foptimization-record-file" ; |
| 516 | return false; |
| 517 | } |
| 518 | return true; |
| 519 | } |
| 520 | |
| 521 | static void (const ArgList &Args, ArgStringList &CmdArgs, |
| 522 | const llvm::Triple &Triple, |
| 523 | const InputInfo &Output, const JobAction &JA) { |
| 524 | StringRef Format = "yaml" ; |
| 525 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_fsave_optimization_record_EQ)) |
| 526 | Format = A->getValue(); |
| 527 | |
| 528 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 529 | CmdArgs.push_back(Elt: "-lto-pass-remarks-output" ); |
| 530 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 531 | |
| 532 | const Arg *A = Args.getLastArg(Ids: options::OPT_foptimization_record_file_EQ); |
| 533 | if (A) { |
| 534 | CmdArgs.push_back(Elt: A->getValue()); |
| 535 | } else { |
| 536 | assert(Output.isFilename() && "Unexpected ld output." ); |
| 537 | SmallString<128> F; |
| 538 | F = Output.getFilename(); |
| 539 | F += ".opt." ; |
| 540 | F += Format; |
| 541 | |
| 542 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: F)); |
| 543 | } |
| 544 | |
| 545 | if (const Arg *A = |
| 546 | Args.getLastArg(Ids: options::OPT_foptimization_record_passes_EQ)) { |
| 547 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 548 | std::string Passes = |
| 549 | std::string("-lto-pass-remarks-filter=" ) + A->getValue(); |
| 550 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: Passes)); |
| 551 | } |
| 552 | |
| 553 | if (!Format.empty()) { |
| 554 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 555 | Twine FormatArg = Twine("-lto-pass-remarks-format=" ) + Format; |
| 556 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: FormatArg)); |
| 557 | } |
| 558 | |
| 559 | if (getLastProfileUseArg(Args)) { |
| 560 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 561 | CmdArgs.push_back(Elt: "-lto-pass-remarks-with-hotness" ); |
| 562 | |
| 563 | if (const Arg *A = |
| 564 | Args.getLastArg(Ids: options::OPT_fdiagnostics_hotness_threshold_EQ)) { |
| 565 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 566 | std::string Opt = |
| 567 | std::string("-lto-pass-remarks-hotness-threshold=" ) + A->getValue(); |
| 568 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: Opt)); |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
| 574 | const InputInfo &Output, |
| 575 | const InputInfoList &Inputs, |
| 576 | const ArgList &Args, |
| 577 | const char *LinkingOutput) const { |
| 578 | assert((Output.getType() == types::TY_Image || |
| 579 | Output.getType() == types::TY_Object) && |
| 580 | "Invalid linker output type." ); |
| 581 | |
| 582 | // If the number of arguments surpasses the system limits, we will encode the |
| 583 | // input files in a separate file, shortening the command line. To this end, |
| 584 | // build a list of input file names that can be passed via a file with the |
| 585 | // -filelist linker option. |
| 586 | llvm::opt::ArgStringList InputFileList; |
| 587 | |
| 588 | // The logic here is derived from gcc's behavior; most of which |
| 589 | // comes from specs (starting with link_command). Consult gcc for |
| 590 | // more information. |
| 591 | ArgStringList CmdArgs; |
| 592 | |
| 593 | VersionTuple Version = getMachOToolChain().getLinkerVersion(Args); |
| 594 | |
| 595 | bool LinkerIsLLD; |
| 596 | const char *Exec = |
| 597 | Args.MakeArgString(Str: getToolChain().GetLinkerPath(LinkerIsLLD: &LinkerIsLLD)); |
| 598 | |
| 599 | // Newer triples always use -platform-version. |
| 600 | llvm::Triple Triple = getToolChain().getTriple(); |
| 601 | bool UsePlatformVersion = Triple.isXROS() || Triple.isOSFirmware(); |
| 602 | |
| 603 | // I'm not sure why this particular decomposition exists in gcc, but |
| 604 | // we follow suite for ease of comparison. |
| 605 | AddLinkArgs(C, Args, CmdArgs, Inputs, Version, LinkerIsLLD, |
| 606 | UsePlatformVersion); |
| 607 | |
| 608 | if (willEmitRemarks(Args) && |
| 609 | checkRemarksOptions(D: getToolChain().getDriver(), Args, |
| 610 | Triple: getToolChain().getTriple())) |
| 611 | renderRemarksOptions(Args, CmdArgs, Triple: getToolChain().getTriple(), Output, JA); |
| 612 | |
| 613 | // Propagate the -moutline flag to the linker in LTO. |
| 614 | if (Arg *A = |
| 615 | Args.getLastArg(Ids: options::OPT_moutline, Ids: options::OPT_mno_outline)) { |
| 616 | if (A->getOption().matches(ID: options::OPT_moutline)) { |
| 617 | if (getMachOToolChain().getMachOArchName(Args) == "arm64" ) { |
| 618 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 619 | CmdArgs.push_back(Elt: "-enable-machine-outliner" ); |
| 620 | } |
| 621 | } else { |
| 622 | // Disable all outlining behaviour if we have mno-outline. We need to do |
| 623 | // this explicitly, because targets which support default outlining will |
| 624 | // try to do work if we don't. |
| 625 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 626 | CmdArgs.push_back(Elt: "-enable-machine-outliner=never" ); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | // Outline from linkonceodr functions by default in LTO, whenever the outliner |
| 631 | // is enabled. Note that the target may enable the machine outliner |
| 632 | // independently of -moutline. |
| 633 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 634 | CmdArgs.push_back(Elt: "-enable-linkonceodr-outlining" ); |
| 635 | |
| 636 | // Propagate codegen data flags to the linker for the LLVM backend. |
| 637 | auto *CodeGenDataGenArg = |
| 638 | Args.getLastArg(Ids: options::OPT_fcodegen_data_generate_EQ); |
| 639 | auto *CodeGenDataUseArg = Args.getLastArg(Ids: options::OPT_fcodegen_data_use_EQ); |
| 640 | |
| 641 | // We only allow one of them to be specified. |
| 642 | const Driver &D = getToolChain().getDriver(); |
| 643 | if (CodeGenDataGenArg && CodeGenDataUseArg) |
| 644 | D.Diag(DiagID: diag::err_drv_argument_not_allowed_with) |
| 645 | << CodeGenDataGenArg->getAsString(Args) |
| 646 | << CodeGenDataUseArg->getAsString(Args); |
| 647 | |
| 648 | // For codegen data gen, the output file is passed to the linker |
| 649 | // while a boolean flag is passed to the LLVM backend. |
| 650 | if (CodeGenDataGenArg) { |
| 651 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 652 | CmdArgs.push_back(Elt: "-codegen-data-generate" ); |
| 653 | } |
| 654 | |
| 655 | // For codegen data use, the input file is passed to the LLVM backend. |
| 656 | if (CodeGenDataUseArg) { |
| 657 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 658 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine("-codegen-data-use-path=" ) + |
| 659 | CodeGenDataUseArg->getValue())); |
| 660 | } |
| 661 | |
| 662 | // Setup statistics file output. |
| 663 | SmallString<128> StatsFile = |
| 664 | getStatsFileName(Args, Output, Input: Inputs[0], D: getToolChain().getDriver()); |
| 665 | if (!StatsFile.empty()) { |
| 666 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 667 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-lto-stats-file=" + StatsFile.str())); |
| 668 | } |
| 669 | |
| 670 | // Set up stack usage file path. |
| 671 | if (Args.hasArg(Ids: options::OPT_fstack_usage)) { |
| 672 | SmallString<128> StackUsageFile(Output.getFilename()); |
| 673 | llvm::sys::path::replace_extension(path&: StackUsageFile, extension: "su" ); |
| 674 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 675 | CmdArgs.push_back( |
| 676 | Elt: Args.MakeArgString(Str: "-stack-usage-file=" + StackUsageFile)); |
| 677 | } |
| 678 | |
| 679 | // It seems that the 'e' option is completely ignored for dynamic executables |
| 680 | // (the default), and with static executables, the last one wins, as expected. |
| 681 | Args.addAllArgs(Output&: CmdArgs, Ids: {options::OPT_d_Flag, options::OPT_s, options::OPT_t, |
| 682 | options::OPT_Z_Flag, options::OPT_u_Group}); |
| 683 | |
| 684 | // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading |
| 685 | // members of static archive libraries which implement Objective-C classes or |
| 686 | // categories. |
| 687 | if (Args.hasArg(Ids: options::OPT_ObjC) || Args.hasArg(Ids: options::OPT_ObjCXX)) |
| 688 | CmdArgs.push_back(Elt: "-ObjC" ); |
| 689 | |
| 690 | CmdArgs.push_back(Elt: "-o" ); |
| 691 | CmdArgs.push_back(Elt: Output.getFilename()); |
| 692 | |
| 693 | if (!Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nostartfiles)) |
| 694 | getMachOToolChain().addStartObjectFileArgs(Args, CmdArgs); |
| 695 | |
| 696 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_L); |
| 697 | |
| 698 | AddLinkerInputs(TC: getToolChain(), Inputs, Args, CmdArgs, JA); |
| 699 | // Build the input file for -filelist (list of linker input files) in case we |
| 700 | // need it later |
| 701 | for (const auto &II : Inputs) { |
| 702 | if (!II.isFilename()) { |
| 703 | // This is a linker input argument. |
| 704 | // We cannot mix input arguments and file names in a -filelist input, thus |
| 705 | // we prematurely stop our list (remaining files shall be passed as |
| 706 | // arguments). |
| 707 | if (InputFileList.size() > 0) |
| 708 | break; |
| 709 | |
| 710 | continue; |
| 711 | } |
| 712 | |
| 713 | InputFileList.push_back(Elt: II.getFilename()); |
| 714 | } |
| 715 | |
| 716 | // Additional linker set-up and flags for Fortran. This is required in order |
| 717 | // to generate executables. |
| 718 | if (getToolChain().getDriver().IsFlangMode() && |
| 719 | !Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs)) { |
| 720 | getToolChain().addFortranRuntimeLibraryPath(Args, CmdArgs); |
| 721 | getToolChain().addFortranRuntimeLibs(Args, CmdArgs); |
| 722 | } |
| 723 | |
| 724 | if (!Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs)) |
| 725 | addOpenMPRuntime(C, CmdArgs, TC: getToolChain(), Args); |
| 726 | |
| 727 | if (isObjCRuntimeLinked(Args) && |
| 728 | !Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs)) { |
| 729 | // We use arclite library for both ARC and subscripting support. |
| 730 | getMachOToolChain().AddLinkARCArgs(Args, CmdArgs); |
| 731 | |
| 732 | CmdArgs.push_back(Elt: "-framework" ); |
| 733 | CmdArgs.push_back(Elt: "Foundation" ); |
| 734 | // Link libobj. |
| 735 | CmdArgs.push_back(Elt: "-lobjc" ); |
| 736 | } |
| 737 | |
| 738 | if (LinkingOutput) { |
| 739 | CmdArgs.push_back(Elt: "-arch_multiple" ); |
| 740 | CmdArgs.push_back(Elt: "-final_output" ); |
| 741 | CmdArgs.push_back(Elt: LinkingOutput); |
| 742 | } |
| 743 | |
| 744 | if (Args.hasArg(Ids: options::OPT_fnested_functions)) |
| 745 | CmdArgs.push_back(Elt: "-allow_stack_execute" ); |
| 746 | |
| 747 | getMachOToolChain().addProfileRTLibs(Args, CmdArgs); |
| 748 | |
| 749 | StringRef Parallelism = getLTOParallelism(Args, D: getToolChain().getDriver()); |
| 750 | if (!Parallelism.empty()) { |
| 751 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 752 | unsigned NumThreads = |
| 753 | llvm::get_threadpool_strategy(Num: Parallelism)->compute_thread_count(); |
| 754 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-threads=" + Twine(NumThreads))); |
| 755 | } |
| 756 | |
| 757 | if (getToolChain().ShouldLinkCXXStdlib(Args)) |
| 758 | getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); |
| 759 | |
| 760 | bool NoStdOrDefaultLibs = |
| 761 | Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs); |
| 762 | bool ForceLinkBuiltins = Args.hasArg(Ids: options::OPT_fapple_link_rtlib); |
| 763 | if (!NoStdOrDefaultLibs || ForceLinkBuiltins) { |
| 764 | // link_ssp spec is empty. |
| 765 | |
| 766 | // If we have both -nostdlib/nodefaultlibs and -fapple-link-rtlib then |
| 767 | // we just want to link the builtins, not the other libs like libSystem. |
| 768 | if (NoStdOrDefaultLibs && ForceLinkBuiltins) { |
| 769 | getMachOToolChain().AddLinkRuntimeLib(Args, CmdArgs, Component: "builtins" ); |
| 770 | } else { |
| 771 | // Let the tool chain choose which runtime library to link. |
| 772 | getMachOToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs, |
| 773 | ForceLinkBuiltinRT: ForceLinkBuiltins); |
| 774 | |
| 775 | // No need to do anything for pthreads. Claim argument to avoid warning. |
| 776 | Args.ClaimAllArgs(Id0: options::OPT_pthread); |
| 777 | Args.ClaimAllArgs(Id0: options::OPT_pthreads); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | if (!Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nostartfiles)) { |
| 782 | // endfile_spec is empty. |
| 783 | } |
| 784 | |
| 785 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_T_Group); |
| 786 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_F); |
| 787 | |
| 788 | // -iframework should be forwarded as -F. |
| 789 | for (const Arg *A : Args.filtered(Ids: options::OPT_iframework)) |
| 790 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: std::string("-F" ) + A->getValue())); |
| 791 | |
| 792 | if (!Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs)) { |
| 793 | if (Arg *A = Args.getLastArg(Ids: options::OPT_fveclib)) { |
| 794 | if (A->getValue() == StringRef("Accelerate" )) { |
| 795 | CmdArgs.push_back(Elt: "-framework" ); |
| 796 | CmdArgs.push_back(Elt: "Accelerate" ); |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | // Add non-standard, platform-specific search paths, e.g., for DriverKit: |
| 802 | // -L<sysroot>/System/DriverKit/usr/lib |
| 803 | // -F<sysroot>/System/DriverKit/System/Library/Framework |
| 804 | { |
| 805 | bool NonStandardSearchPath = false; |
| 806 | const auto &Triple = getToolChain().getTriple(); |
| 807 | if (Triple.isDriverKit()) { |
| 808 | // ld64 fixed the implicit -F and -L paths in ld64-605.1+. |
| 809 | NonStandardSearchPath = |
| 810 | Version.getMajor() < 605 || |
| 811 | (Version.getMajor() == 605 && Version.getMinor().value_or(u: 0) < 1); |
| 812 | } else { |
| 813 | NonStandardSearchPath = getMachOToolChain().HasPlatformPrefix(T: Triple); |
| 814 | } |
| 815 | |
| 816 | if (NonStandardSearchPath) { |
| 817 | if (auto *Sysroot = Args.getLastArg(Ids: options::OPT_isysroot)) { |
| 818 | auto AddSearchPath = [&](StringRef Flag, StringRef SearchPath) { |
| 819 | SmallString<128> P(Sysroot->getValue()); |
| 820 | getMachOToolChain().AppendPlatformPrefix(Path&: P, T: Triple); |
| 821 | llvm::sys::path::append(path&: P, a: SearchPath); |
| 822 | if (getToolChain().getVFS().exists(Path: P)) { |
| 823 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: Flag + P)); |
| 824 | } |
| 825 | }; |
| 826 | AddSearchPath("-L" , "/usr/lib" ); |
| 827 | AddSearchPath("-F" , "/System/Library/Frameworks" ); |
| 828 | } |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | ResponseFileSupport ResponseSupport; |
| 833 | if (Version >= VersionTuple(705) || LinkerIsLLD) { |
| 834 | ResponseSupport = ResponseFileSupport::AtFileUTF8(); |
| 835 | } else { |
| 836 | // For older versions of the linker, use the legacy filelist method instead. |
| 837 | ResponseSupport = {.ResponseKind: ResponseFileSupport::RF_FileList, .ResponseEncoding: llvm::sys::WEM_UTF8, |
| 838 | .ResponseFlag: "-filelist" }; |
| 839 | } |
| 840 | |
| 841 | std::unique_ptr<Command> Cmd = std::make_unique<Command>( |
| 842 | args: JA, args: *this, args&: ResponseSupport, args&: Exec, args&: CmdArgs, args: Inputs, args: Output); |
| 843 | Cmd->setInputFileList(std::move(InputFileList)); |
| 844 | C.addCommand(C: std::move(Cmd)); |
| 845 | } |
| 846 | |
| 847 | void darwin::StaticLibTool::ConstructJob(Compilation &C, const JobAction &JA, |
| 848 | const InputInfo &Output, |
| 849 | const InputInfoList &Inputs, |
| 850 | const ArgList &Args, |
| 851 | const char *LinkingOutput) const { |
| 852 | const Driver &D = getToolChain().getDriver(); |
| 853 | |
| 854 | // Silence warning for "clang -g foo.o -o foo" |
| 855 | Args.ClaimAllArgs(Id0: options::OPT_g_Group); |
| 856 | // and "clang -emit-llvm foo.o -o foo" |
| 857 | Args.ClaimAllArgs(Id0: options::OPT_emit_llvm); |
| 858 | // and for "clang -w foo.o -o foo". Other warning options are already |
| 859 | // handled somewhere else. |
| 860 | Args.ClaimAllArgs(Id0: options::OPT_w); |
| 861 | // Silence warnings when linking C code with a C++ '-stdlib' argument. |
| 862 | Args.ClaimAllArgs(Id0: options::OPT_stdlib_EQ); |
| 863 | |
| 864 | // libtool <options> <output_file> <input_files> |
| 865 | ArgStringList CmdArgs; |
| 866 | // Create and insert file members with a deterministic index. |
| 867 | CmdArgs.push_back(Elt: "-static" ); |
| 868 | CmdArgs.push_back(Elt: "-D" ); |
| 869 | CmdArgs.push_back(Elt: "-no_warning_for_no_symbols" ); |
| 870 | CmdArgs.push_back(Elt: "-o" ); |
| 871 | CmdArgs.push_back(Elt: Output.getFilename()); |
| 872 | |
| 873 | for (const auto &II : Inputs) { |
| 874 | if (II.isFilename()) { |
| 875 | CmdArgs.push_back(Elt: II.getFilename()); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | // Delete old output archive file if it already exists before generating a new |
| 880 | // archive file. |
| 881 | const auto *OutputFileName = Output.getFilename(); |
| 882 | if (Output.isFilename() && llvm::sys::fs::exists(Path: OutputFileName)) { |
| 883 | if (std::error_code EC = llvm::sys::fs::remove(path: OutputFileName)) { |
| 884 | D.Diag(DiagID: diag::err_drv_unable_to_remove_file) << EC.message(); |
| 885 | return; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | const char *Exec = Args.MakeArgString(Str: getToolChain().GetStaticLibToolPath()); |
| 890 | C.addCommand(C: std::make_unique<Command>(args: JA, args: *this, |
| 891 | args: ResponseFileSupport::AtFileUTF8(), |
| 892 | args&: Exec, args&: CmdArgs, args: Inputs, args: Output)); |
| 893 | } |
| 894 | |
| 895 | void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA, |
| 896 | const InputInfo &Output, |
| 897 | const InputInfoList &Inputs, |
| 898 | const ArgList &Args, |
| 899 | const char *LinkingOutput) const { |
| 900 | ArgStringList CmdArgs; |
| 901 | |
| 902 | CmdArgs.push_back(Elt: "-create" ); |
| 903 | assert(Output.isFilename() && "Unexpected lipo output." ); |
| 904 | |
| 905 | CmdArgs.push_back(Elt: "-output" ); |
| 906 | CmdArgs.push_back(Elt: Output.getFilename()); |
| 907 | |
| 908 | for (const auto &II : Inputs) { |
| 909 | assert(II.isFilename() && "Unexpected lipo input." ); |
| 910 | CmdArgs.push_back(Elt: II.getFilename()); |
| 911 | } |
| 912 | |
| 913 | StringRef LipoName = Args.getLastArgValue(Id: options::OPT_fuse_lipo_EQ, Default: "lipo" ); |
| 914 | const char *Exec = |
| 915 | Args.MakeArgString(Str: getToolChain().GetProgramPath(Name: LipoName.data())); |
| 916 | C.addCommand(C: std::make_unique<Command>(args: JA, args: *this, args: ResponseFileSupport::None(), |
| 917 | args&: Exec, args&: CmdArgs, args: Inputs, args: Output)); |
| 918 | } |
| 919 | |
| 920 | void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA, |
| 921 | const InputInfo &Output, |
| 922 | const InputInfoList &Inputs, |
| 923 | const ArgList &Args, |
| 924 | const char *LinkingOutput) const { |
| 925 | ArgStringList CmdArgs; |
| 926 | |
| 927 | CmdArgs.push_back(Elt: "-o" ); |
| 928 | CmdArgs.push_back(Elt: Output.getFilename()); |
| 929 | |
| 930 | assert(Inputs.size() == 1 && "Unable to handle multiple inputs." ); |
| 931 | const InputInfo &Input = Inputs[0]; |
| 932 | assert(Input.isFilename() && "Unexpected dsymutil input." ); |
| 933 | CmdArgs.push_back(Elt: Input.getFilename()); |
| 934 | |
| 935 | const char *Exec = |
| 936 | Args.MakeArgString(Str: getToolChain().GetProgramPath(Name: "dsymutil" )); |
| 937 | C.addCommand(C: std::make_unique<Command>(args: JA, args: *this, args: ResponseFileSupport::None(), |
| 938 | args&: Exec, args&: CmdArgs, args: Inputs, args: Output)); |
| 939 | } |
| 940 | |
| 941 | void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA, |
| 942 | const InputInfo &Output, |
| 943 | const InputInfoList &Inputs, |
| 944 | const ArgList &Args, |
| 945 | const char *LinkingOutput) const { |
| 946 | ArgStringList CmdArgs; |
| 947 | CmdArgs.push_back(Elt: "--verify" ); |
| 948 | CmdArgs.push_back(Elt: "--debug-info" ); |
| 949 | CmdArgs.push_back(Elt: "--eh-frame" ); |
| 950 | CmdArgs.push_back(Elt: "--quiet" ); |
| 951 | |
| 952 | assert(Inputs.size() == 1 && "Unable to handle multiple inputs." ); |
| 953 | const InputInfo &Input = Inputs[0]; |
| 954 | assert(Input.isFilename() && "Unexpected verify input" ); |
| 955 | |
| 956 | // Grabbing the output of the earlier dsymutil run. |
| 957 | CmdArgs.push_back(Elt: Input.getFilename()); |
| 958 | |
| 959 | const char *Exec = |
| 960 | Args.MakeArgString(Str: getToolChain().GetProgramPath(Name: "dwarfdump" )); |
| 961 | C.addCommand(C: std::make_unique<Command>(args: JA, args: *this, args: ResponseFileSupport::None(), |
| 962 | args&: Exec, args&: CmdArgs, args: Inputs, args: Output)); |
| 963 | } |
| 964 | |
| 965 | MachO::MachO(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) |
| 966 | : ToolChain(D, Triple, Args) { |
| 967 | // We expect 'as', 'ld', etc. to be adjacent to our install dir. |
| 968 | getProgramPaths().push_back(Elt: getDriver().Dir); |
| 969 | } |
| 970 | |
| 971 | AppleMachO::AppleMachO(const Driver &D, const llvm::Triple &Triple, |
| 972 | const ArgList &Args) |
| 973 | : MachO(D, Triple, Args), CudaInstallation(D, Triple, Args), |
| 974 | RocmInstallation(D, Triple, Args), SYCLInstallation(D, Triple, Args) {} |
| 975 | |
| 976 | /// Darwin - Darwin tool chain for i386 and x86_64. |
| 977 | Darwin::Darwin(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) |
| 978 | : AppleMachO(D, Triple, Args), TargetInitialized(false) {} |
| 979 | |
| 980 | types::ID MachO::LookupTypeForExtension(StringRef Ext) const { |
| 981 | types::ID Ty = ToolChain::LookupTypeForExtension(Ext); |
| 982 | |
| 983 | // Darwin always preprocesses assembly files (unless -x is used explicitly). |
| 984 | if (Ty == types::TY_PP_Asm) |
| 985 | return types::TY_Asm; |
| 986 | |
| 987 | return Ty; |
| 988 | } |
| 989 | |
| 990 | bool MachO::HasNativeLLVMSupport() const { return true; } |
| 991 | |
| 992 | ToolChain::CXXStdlibType Darwin::GetDefaultCXXStdlibType() const { |
| 993 | // Always use libc++ by default |
| 994 | return ToolChain::CST_Libcxx; |
| 995 | } |
| 996 | |
| 997 | /// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0. |
| 998 | ObjCRuntime Darwin::getDefaultObjCRuntime(bool isNonFragile) const { |
| 999 | if (isTargetWatchOSBased()) |
| 1000 | return ObjCRuntime(ObjCRuntime::WatchOS, TargetVersion); |
| 1001 | if (isTargetIOSBased()) |
| 1002 | return ObjCRuntime(ObjCRuntime::iOS, TargetVersion); |
| 1003 | if (isTargetXROS()) { |
| 1004 | // XROS uses the iOS runtime. |
| 1005 | auto T = llvm::Triple(Twine("arm64-apple-" ) + |
| 1006 | llvm::Triple::getOSTypeName(Kind: llvm::Triple::XROS) + |
| 1007 | TargetVersion.getAsString()); |
| 1008 | return ObjCRuntime(ObjCRuntime::iOS, T.getiOSVersion()); |
| 1009 | } |
| 1010 | if (isNonFragile) |
| 1011 | return ObjCRuntime(ObjCRuntime::MacOSX, TargetVersion); |
| 1012 | return ObjCRuntime(ObjCRuntime::FragileMacOSX, TargetVersion); |
| 1013 | } |
| 1014 | |
| 1015 | /// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2. |
| 1016 | bool Darwin::hasBlocksRuntime() const { |
| 1017 | if (isTargetWatchOSBased() || isTargetDriverKit() || isTargetXROS()) |
| 1018 | return true; |
| 1019 | else if (isTargetFirmware()) |
| 1020 | return false; |
| 1021 | else if (isTargetIOSBased()) |
| 1022 | return !isIPhoneOSVersionLT(V0: 3, V1: 2); |
| 1023 | else { |
| 1024 | assert(isTargetMacOSBased() && "unexpected darwin target" ); |
| 1025 | return !isMacosxVersionLT(V0: 10, V1: 6); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | void AppleMachO::AddCudaIncludeArgs(const ArgList &DriverArgs, |
| 1030 | ArgStringList &CC1Args) const { |
| 1031 | CudaInstallation->AddCudaIncludeArgs(DriverArgs, CC1Args); |
| 1032 | } |
| 1033 | |
| 1034 | void AppleMachO::AddHIPIncludeArgs(const ArgList &DriverArgs, |
| 1035 | ArgStringList &CC1Args) const { |
| 1036 | RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args); |
| 1037 | } |
| 1038 | |
| 1039 | void AppleMachO::addSYCLIncludeArgs(const ArgList &DriverArgs, |
| 1040 | ArgStringList &CC1Args) const { |
| 1041 | SYCLInstallation->addSYCLIncludeArgs(DriverArgs, CC1Args); |
| 1042 | } |
| 1043 | |
| 1044 | // This is just a MachO name translation routine and there's no |
| 1045 | // way to join this into ARMTargetParser without breaking all |
| 1046 | // other assumptions. Maybe MachO should consider standardising |
| 1047 | // their nomenclature. |
| 1048 | static const char *ArmMachOArchName(StringRef Arch) { |
| 1049 | return llvm::StringSwitch<const char *>(Arch) |
| 1050 | .Case(S: "armv6k" , Value: "armv6" ) |
| 1051 | .Case(S: "armv6m" , Value: "armv6m" ) |
| 1052 | .Case(S: "armv5tej" , Value: "armv5" ) |
| 1053 | .Case(S: "xscale" , Value: "xscale" ) |
| 1054 | .Case(S: "armv4t" , Value: "armv4t" ) |
| 1055 | .Case(S: "armv7" , Value: "armv7" ) |
| 1056 | .Cases(CaseStrings: {"armv7a" , "armv7-a" }, Value: "armv7" ) |
| 1057 | .Cases(CaseStrings: {"armv7r" , "armv7-r" }, Value: "armv7" ) |
| 1058 | .Cases(CaseStrings: {"armv7em" , "armv7e-m" }, Value: "armv7em" ) |
| 1059 | .Cases(CaseStrings: {"armv7k" , "armv7-k" }, Value: "armv7k" ) |
| 1060 | .Cases(CaseStrings: {"armv7m" , "armv7-m" }, Value: "armv7m" ) |
| 1061 | .Cases(CaseStrings: {"armv7s" , "armv7-s" }, Value: "armv7s" ) |
| 1062 | .Default(Value: nullptr); |
| 1063 | } |
| 1064 | |
| 1065 | static const char *ArmMachOArchNameCPU(StringRef CPU) { |
| 1066 | llvm::ARM::ArchKind ArchKind = llvm::ARM::parseCPUArch(CPU); |
| 1067 | if (ArchKind == llvm::ARM::ArchKind::INVALID) |
| 1068 | return nullptr; |
| 1069 | StringRef Arch = llvm::ARM::getArchName(AK: ArchKind); |
| 1070 | |
| 1071 | // FIXME: Make sure this MachO triple mangling is really necessary. |
| 1072 | // ARMv5* normalises to ARMv5. |
| 1073 | if (Arch.starts_with(Prefix: "armv5" )) |
| 1074 | Arch = Arch.substr(Start: 0, N: 5); |
| 1075 | // ARMv6*, except ARMv6M, normalises to ARMv6. |
| 1076 | else if (Arch.starts_with(Prefix: "armv6" ) && !Arch.ends_with(Suffix: "6m" )) |
| 1077 | Arch = Arch.substr(Start: 0, N: 5); |
| 1078 | // ARMv7A normalises to ARMv7. |
| 1079 | else if (Arch.ends_with(Suffix: "v7a" )) |
| 1080 | Arch = Arch.substr(Start: 0, N: 5); |
| 1081 | return Arch.data(); |
| 1082 | } |
| 1083 | |
| 1084 | StringRef MachO::getMachOArchName(const ArgList &Args) const { |
| 1085 | switch (getTriple().getArch()) { |
| 1086 | default: |
| 1087 | return getDefaultUniversalArchName(); |
| 1088 | |
| 1089 | case llvm::Triple::aarch64_32: |
| 1090 | return "arm64_32" ; |
| 1091 | |
| 1092 | case llvm::Triple::aarch64: { |
| 1093 | if (getTriple().isArm64e()) |
| 1094 | return "arm64e" ; |
| 1095 | return "arm64" ; |
| 1096 | } |
| 1097 | |
| 1098 | case llvm::Triple::thumb: |
| 1099 | case llvm::Triple::arm: |
| 1100 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_march_EQ)) |
| 1101 | if (const char *Arch = ArmMachOArchName(Arch: A->getValue())) |
| 1102 | return Arch; |
| 1103 | |
| 1104 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_mcpu_EQ)) |
| 1105 | if (const char *Arch = ArmMachOArchNameCPU(CPU: A->getValue())) |
| 1106 | return Arch; |
| 1107 | |
| 1108 | return "arm" ; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | VersionTuple MachO::getLinkerVersion(const llvm::opt::ArgList &Args) const { |
| 1113 | if (LinkerVersion) { |
| 1114 | #ifndef NDEBUG |
| 1115 | VersionTuple NewLinkerVersion; |
| 1116 | if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) |
| 1117 | (void)NewLinkerVersion.tryParse(A->getValue()); |
| 1118 | assert(NewLinkerVersion == LinkerVersion); |
| 1119 | #endif |
| 1120 | return *LinkerVersion; |
| 1121 | } |
| 1122 | |
| 1123 | VersionTuple NewLinkerVersion; |
| 1124 | if (Arg *A = Args.getLastArg(Ids: options::OPT_mlinker_version_EQ)) { |
| 1125 | // Rejecting subbuild version is probably not necessary, but some |
| 1126 | // existing tests depend on this. |
| 1127 | if (NewLinkerVersion.tryParse(string: A->getValue()) || |
| 1128 | NewLinkerVersion.getSubbuild()) |
| 1129 | getDriver().Diag(DiagID: diag::err_drv_invalid_version_number) |
| 1130 | << A->getAsString(Args); |
| 1131 | } |
| 1132 | |
| 1133 | LinkerVersion = NewLinkerVersion; |
| 1134 | return *LinkerVersion; |
| 1135 | } |
| 1136 | |
| 1137 | Darwin::~Darwin() {} |
| 1138 | |
| 1139 | AppleMachO::~AppleMachO() {} |
| 1140 | |
| 1141 | MachO::~MachO() {} |
| 1142 | |
| 1143 | void Darwin::VerifyTripleForSDK(const llvm::opt::ArgList &Args, |
| 1144 | const llvm::Triple Triple) const { |
| 1145 | if (SDKInfo) { |
| 1146 | if (!SDKInfo->supportsTriple(Triple)) |
| 1147 | getDriver().Diag(DiagID: diag::warn_incompatible_sysroot) |
| 1148 | << SDKInfo->getDisplayName() << Triple.getTriple(); |
| 1149 | } else if (const Arg *A = Args.getLastArg(Ids: options::OPT_isysroot)) { |
| 1150 | // If there is no SDK info, assume this is building against an SDK that |
| 1151 | // predates SDKSettings.json. Try to match the triple to the SDK path. |
| 1152 | const char *isysroot = A->getValue(); |
| 1153 | StringRef SDKName = getSDKName(isysroot); |
| 1154 | if (!SDKName.empty()) { |
| 1155 | bool supported = true; |
| 1156 | if (Triple.isWatchOS()) |
| 1157 | supported = SDKName.starts_with(Prefix: "Watch" ); |
| 1158 | else if (Triple.isTvOS()) |
| 1159 | supported = SDKName.starts_with(Prefix: "AppleTV" ); |
| 1160 | else if (Triple.isDriverKit()) |
| 1161 | supported = SDKName.starts_with(Prefix: "DriverKit" ); |
| 1162 | else if (Triple.isiOS()) |
| 1163 | supported = SDKName.starts_with(Prefix: "iPhone" ); |
| 1164 | else if (Triple.isMacOSX()) |
| 1165 | supported = SDKName.starts_with(Prefix: "MacOSX" ); |
| 1166 | // If it's not an older SDK, then it might be a damaged SDK or a |
| 1167 | // non-standard -isysroot path. Don't try to diagnose that here. |
| 1168 | |
| 1169 | if (!supported) |
| 1170 | getDriver().Diag(DiagID: diag::warn_incompatible_sysroot) |
| 1171 | << SDKName << Triple.getTriple(); |
| 1172 | } |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args, |
| 1177 | types::ID InputType) const { |
| 1178 | llvm::Triple Triple(ComputeLLVMTriple(Args, InputType)); |
| 1179 | |
| 1180 | // If the target isn't initialized (e.g., an unknown Darwin platform, return |
| 1181 | // the default triple). |
| 1182 | if (!isTargetInitialized()) |
| 1183 | return Triple.getTriple(); |
| 1184 | |
| 1185 | SmallString<16> Str; |
| 1186 | if (isTargetWatchOSBased()) |
| 1187 | Str += "watchos" ; |
| 1188 | else if (isTargetTvOSBased()) |
| 1189 | Str += "tvos" ; |
| 1190 | else if (isTargetDriverKit()) |
| 1191 | Str += "driverkit" ; |
| 1192 | else if (isTargetIOSBased() || isTargetMacCatalyst()) |
| 1193 | Str += "ios" ; |
| 1194 | else if (isTargetXROS()) |
| 1195 | Str += llvm::Triple::getOSTypeName(Kind: llvm::Triple::XROS); |
| 1196 | else if (isTargetFirmware()) |
| 1197 | Str += llvm::Triple::getOSTypeName(Kind: llvm::Triple::Firmware); |
| 1198 | else |
| 1199 | Str += "macosx" ; |
| 1200 | Str += getTripleTargetVersion().getAsString(); |
| 1201 | Triple.setOSName(Str); |
| 1202 | |
| 1203 | VerifyTripleForSDK(Args, Triple); |
| 1204 | |
| 1205 | return Triple.getTriple(); |
| 1206 | } |
| 1207 | |
| 1208 | Tool *MachO::getTool(Action::ActionClass AC) const { |
| 1209 | switch (AC) { |
| 1210 | case Action::LipoJobClass: |
| 1211 | if (!Lipo) |
| 1212 | Lipo.reset(p: new tools::darwin::Lipo(*this)); |
| 1213 | return Lipo.get(); |
| 1214 | case Action::DsymutilJobClass: |
| 1215 | if (!Dsymutil) |
| 1216 | Dsymutil.reset(p: new tools::darwin::Dsymutil(*this)); |
| 1217 | return Dsymutil.get(); |
| 1218 | case Action::VerifyDebugInfoJobClass: |
| 1219 | if (!VerifyDebug) |
| 1220 | VerifyDebug.reset(p: new tools::darwin::VerifyDebug(*this)); |
| 1221 | return VerifyDebug.get(); |
| 1222 | default: |
| 1223 | return ToolChain::getTool(AC); |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | Tool *MachO::buildLinker() const { return new tools::darwin::Linker(*this); } |
| 1228 | |
| 1229 | Tool *MachO::buildStaticLibTool() const { |
| 1230 | return new tools::darwin::StaticLibTool(*this); |
| 1231 | } |
| 1232 | |
| 1233 | Tool *MachO::buildAssembler() const { |
| 1234 | return new tools::darwin::Assembler(*this); |
| 1235 | } |
| 1236 | |
| 1237 | DarwinClang::DarwinClang(const Driver &D, const llvm::Triple &Triple, |
| 1238 | const ArgList &Args) |
| 1239 | : Darwin(D, Triple, Args) {} |
| 1240 | |
| 1241 | void DarwinClang::addClangWarningOptions(ArgStringList &CC1Args) const { |
| 1242 | // Always error about undefined 'TARGET_OS_*' macros. |
| 1243 | CC1Args.push_back(Elt: "-Wundef-prefix=TARGET_OS_" ); |
| 1244 | CC1Args.push_back(Elt: "-Werror=undef-prefix" ); |
| 1245 | |
| 1246 | // For modern targets, promote certain warnings to errors. |
| 1247 | if (isTargetWatchOSBased() || getTriple().isArch64Bit()) { |
| 1248 | // Always enable -Wdeprecated-objc-isa-usage and promote it |
| 1249 | // to an error. |
| 1250 | CC1Args.push_back(Elt: "-Wdeprecated-objc-isa-usage" ); |
| 1251 | CC1Args.push_back(Elt: "-Werror=deprecated-objc-isa-usage" ); |
| 1252 | |
| 1253 | // For iOS and watchOS, also error about implicit function declarations, |
| 1254 | // as that can impact calling conventions. |
| 1255 | if (!isTargetMacOS()) |
| 1256 | CC1Args.push_back(Elt: "-Werror=implicit-function-declaration" ); |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | void DarwinClang::addClangTargetOptions( |
| 1261 | const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, |
| 1262 | Action::OffloadKind DeviceOffloadKind) const { |
| 1263 | |
| 1264 | Darwin::addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadKind); |
| 1265 | } |
| 1266 | |
| 1267 | /// Take a path that speculatively points into Xcode and return the |
| 1268 | /// `XCODE/Contents/Developer` path if it is an Xcode path, or an empty path |
| 1269 | /// otherwise. |
| 1270 | static StringRef getXcodeDeveloperPath(StringRef PathIntoXcode) { |
| 1271 | static constexpr llvm::StringLiteral XcodeAppSuffix( |
| 1272 | ".app/Contents/Developer" ); |
| 1273 | size_t Index = PathIntoXcode.find(Str: XcodeAppSuffix); |
| 1274 | if (Index == StringRef::npos) |
| 1275 | return "" ; |
| 1276 | return PathIntoXcode.take_front(N: Index + XcodeAppSuffix.size()); |
| 1277 | } |
| 1278 | |
| 1279 | void DarwinClang::AddLinkARCArgs(const ArgList &Args, |
| 1280 | ArgStringList &CmdArgs) const { |
| 1281 | // Avoid linking compatibility stubs on i386 mac. |
| 1282 | if (isTargetMacOSBased() && getArch() == llvm::Triple::x86) |
| 1283 | return; |
| 1284 | if (isTargetAppleSiliconMac()) |
| 1285 | return; |
| 1286 | // ARC runtime is supported everywhere on arm64e. |
| 1287 | if (getTriple().isArm64e()) |
| 1288 | return; |
| 1289 | if (isTargetXROS()) |
| 1290 | return; |
| 1291 | |
| 1292 | ObjCRuntime runtime = getDefaultObjCRuntime(/*nonfragile*/ isNonFragile: true); |
| 1293 | |
| 1294 | if ((runtime.hasNativeARC() || !isObjCAutoRefCount(Args)) && |
| 1295 | runtime.hasSubscripting()) |
| 1296 | return; |
| 1297 | |
| 1298 | SmallString<128> P(getDriver().ClangExecutable); |
| 1299 | llvm::sys::path::remove_filename(path&: P); // 'clang' |
| 1300 | llvm::sys::path::remove_filename(path&: P); // 'bin' |
| 1301 | llvm::sys::path::append(path&: P, a: "lib" , b: "arc" ); |
| 1302 | |
| 1303 | // 'libarclite' usually lives in the same toolchain as 'clang'. However, the |
| 1304 | // Swift open source toolchains for macOS distribute Clang without libarclite. |
| 1305 | // In that case, to allow the linker to find 'libarclite', we point to the |
| 1306 | // 'libarclite' in the XcodeDefault toolchain instead. |
| 1307 | if (!getVFS().exists(Path: P)) { |
| 1308 | auto updatePath = [&](const Arg *A) { |
| 1309 | // Try to infer the path to 'libarclite' in the toolchain from the |
| 1310 | // specified SDK path. |
| 1311 | StringRef XcodePathForSDK = getXcodeDeveloperPath(PathIntoXcode: A->getValue()); |
| 1312 | if (XcodePathForSDK.empty()) |
| 1313 | return false; |
| 1314 | |
| 1315 | P = XcodePathForSDK; |
| 1316 | llvm::sys::path::append(path&: P, a: "Toolchains/XcodeDefault.xctoolchain/usr" , |
| 1317 | b: "lib" , c: "arc" ); |
| 1318 | return getVFS().exists(Path: P); |
| 1319 | }; |
| 1320 | |
| 1321 | bool updated = false; |
| 1322 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_isysroot)) |
| 1323 | updated = updatePath(A); |
| 1324 | |
| 1325 | if (!updated) { |
| 1326 | if (const Arg *A = Args.getLastArg(Ids: options::OPT__sysroot_EQ)) |
| 1327 | updatePath(A); |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | CmdArgs.push_back(Elt: "-force_load" ); |
| 1332 | llvm::sys::path::append(path&: P, a: "libarclite_" ); |
| 1333 | // Mash in the platform. |
| 1334 | if (isTargetWatchOSSimulator()) |
| 1335 | P += "watchsimulator" ; |
| 1336 | else if (isTargetWatchOS()) |
| 1337 | P += "watchos" ; |
| 1338 | else if (isTargetTvOSSimulator()) |
| 1339 | P += "appletvsimulator" ; |
| 1340 | else if (isTargetTvOS()) |
| 1341 | P += "appletvos" ; |
| 1342 | else if (isTargetIOSSimulator()) |
| 1343 | P += "iphonesimulator" ; |
| 1344 | else if (isTargetIPhoneOS()) |
| 1345 | P += "iphoneos" ; |
| 1346 | else |
| 1347 | P += "macosx" ; |
| 1348 | P += ".a" ; |
| 1349 | |
| 1350 | if (!getVFS().exists(Path: P)) |
| 1351 | getDriver().Diag(DiagID: clang::diag::err_drv_darwin_sdk_missing_arclite) << P; |
| 1352 | |
| 1353 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: P)); |
| 1354 | } |
| 1355 | |
| 1356 | unsigned DarwinClang::GetDefaultDwarfVersion() const { |
| 1357 | // Default to use DWARF 2 on OS X 10.10 / iOS 8 and lower. |
| 1358 | if ((isTargetMacOSBased() && isMacosxVersionLT(V0: 10, V1: 11)) || |
| 1359 | (isTargetIOSBased() && isIPhoneOSVersionLT(V0: 9))) |
| 1360 | return 2; |
| 1361 | // Default to use DWARF 4 on OS X 10.11 - macOS 14 / iOS 9 - iOS 17. |
| 1362 | if ((isTargetMacOSBased() && isMacosxVersionLT(V0: 15)) || |
| 1363 | (isTargetIOSBased() && isIPhoneOSVersionLT(V0: 18)) || |
| 1364 | (isTargetWatchOSBased() && TargetVersion < llvm::VersionTuple(11)) || |
| 1365 | (isTargetXROS() && TargetVersion < llvm::VersionTuple(2)) || |
| 1366 | (isTargetDriverKit() && TargetVersion < llvm::VersionTuple(24)) || |
| 1367 | (isTargetMacOSBased() && |
| 1368 | TargetVersion.empty())) // apple-darwin, no version. |
| 1369 | return 4; |
| 1370 | return 5; |
| 1371 | } |
| 1372 | |
| 1373 | bool DarwinClang::getDefaultDebugSimpleTemplateNames() const { |
| 1374 | // Default to an OS version on which LLDB supports debugging |
| 1375 | // -gsimple-template-names programs. |
| 1376 | if ((isTargetMacOSBased() && isMacosxVersionLT(V0: 26)) || |
| 1377 | (isTargetIOSBased() && isIPhoneOSVersionLT(V0: 26)) || |
| 1378 | (isTargetWatchOSBased() && TargetVersion < llvm::VersionTuple(26)) || |
| 1379 | (isTargetXROS() && TargetVersion < llvm::VersionTuple(26)) || |
| 1380 | (isTargetDriverKit() && TargetVersion < llvm::VersionTuple(25)) || |
| 1381 | (isTargetMacOSBased() && |
| 1382 | TargetVersion.empty())) // apple-darwin, no version. |
| 1383 | return false; |
| 1384 | |
| 1385 | return true; |
| 1386 | } |
| 1387 | |
| 1388 | void MachO::AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs, |
| 1389 | StringRef Component, RuntimeLinkOptions Opts, |
| 1390 | bool IsShared) const { |
| 1391 | std::string P = getCompilerRT( |
| 1392 | Args, Component, Type: IsShared ? ToolChain::FT_Shared : ToolChain::FT_Static); |
| 1393 | |
| 1394 | // For now, allow missing resource libraries to support developers who may |
| 1395 | // not have compiler-rt checked out or integrated into their build (unless |
| 1396 | // we explicitly force linking with this library). |
| 1397 | if ((Opts & RLO_AlwaysLink) || getVFS().exists(Path: P)) { |
| 1398 | const char *LibArg = Args.MakeArgString(Str: P); |
| 1399 | CmdArgs.push_back(Elt: LibArg); |
| 1400 | } |
| 1401 | |
| 1402 | // Adding the rpaths might negatively interact when other rpaths are involved, |
| 1403 | // so we should make sure we add the rpaths last, after all user-specified |
| 1404 | // rpaths. This is currently true from this place, but we need to be |
| 1405 | // careful if this function is ever called before user's rpaths are emitted. |
| 1406 | if (Opts & RLO_AddRPath) { |
| 1407 | assert(StringRef(P).ends_with(".dylib" ) && "must be a dynamic library" ); |
| 1408 | |
| 1409 | // Add @executable_path to rpath to support having the dylib copied with |
| 1410 | // the executable. |
| 1411 | CmdArgs.push_back(Elt: "-rpath" ); |
| 1412 | CmdArgs.push_back(Elt: "@executable_path" ); |
| 1413 | |
| 1414 | // Add the compiler-rt library's directory to rpath to support using the |
| 1415 | // dylib from the default location without copying. |
| 1416 | CmdArgs.push_back(Elt: "-rpath" ); |
| 1417 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: llvm::sys::path::parent_path(path: P))); |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | std::string MachO::getCompilerRT(const ArgList &Args, StringRef Component, |
| 1422 | FileType Type, bool IsFortran) const { |
| 1423 | assert(Type != ToolChain::FT_Object && |
| 1424 | "it doesn't make sense to ask for the compiler-rt library name as an " |
| 1425 | "object file" ); |
| 1426 | SmallString<64> MachOLibName = StringRef("libclang_rt" ); |
| 1427 | // On MachO, the builtins component is not in the library name |
| 1428 | if (Component != "builtins" ) { |
| 1429 | MachOLibName += '.'; |
| 1430 | MachOLibName += Component; |
| 1431 | } |
| 1432 | MachOLibName += Type == ToolChain::FT_Shared ? "_dynamic.dylib" : ".a" ; |
| 1433 | |
| 1434 | SmallString<128> FullPath(getDriver().ResourceDir); |
| 1435 | llvm::sys::path::append(path&: FullPath, a: "lib" , b: "darwin" , c: "macho_embedded" , |
| 1436 | d: MachOLibName); |
| 1437 | return std::string(FullPath); |
| 1438 | } |
| 1439 | |
| 1440 | std::string Darwin::getCompilerRT(const ArgList &Args, StringRef Component, |
| 1441 | FileType Type, bool IsFortran) const { |
| 1442 | // Firmware uses the "bare metal" RT. |
| 1443 | if (TargetPlatform == DarwinPlatformKind::Firmware) |
| 1444 | return MachO::getCompilerRT(Args, Component, Type, IsFortran); |
| 1445 | |
| 1446 | assert(Type != ToolChain::FT_Object && |
| 1447 | "it doesn't make sense to ask for the compiler-rt library name as an " |
| 1448 | "object file" ); |
| 1449 | SmallString<64> DarwinLibName = StringRef("libclang_rt." ); |
| 1450 | // On Darwin, the builtins component is not in the library name |
| 1451 | if (Component != "builtins" ) { |
| 1452 | DarwinLibName += Component; |
| 1453 | DarwinLibName += '_'; |
| 1454 | } |
| 1455 | DarwinLibName += getOSLibraryNameSuffix(); |
| 1456 | DarwinLibName += Type == ToolChain::FT_Shared ? "_dynamic.dylib" : ".a" ; |
| 1457 | |
| 1458 | SmallString<128> FullPath(getDriver().ResourceDir); |
| 1459 | llvm::sys::path::append(path&: FullPath, a: "lib" , b: "darwin" , c: DarwinLibName); |
| 1460 | return std::string(FullPath); |
| 1461 | } |
| 1462 | |
| 1463 | StringRef Darwin::getSDKName(StringRef isysroot) { |
| 1464 | // Assume SDK has path: SOME_PATH/SDKs/PlatformXX.YY.sdk |
| 1465 | auto BeginSDK = llvm::sys::path::rbegin(path: isysroot); |
| 1466 | auto EndSDK = llvm::sys::path::rend(path: isysroot); |
| 1467 | for (auto IT = BeginSDK; IT != EndSDK; ++IT) { |
| 1468 | StringRef SDK = *IT; |
| 1469 | if (SDK.consume_back(Suffix: ".sdk" )) |
| 1470 | return SDK; |
| 1471 | } |
| 1472 | return "" ; |
| 1473 | } |
| 1474 | |
| 1475 | StringRef Darwin::getOSLibraryNameSuffix(bool IgnoreSim) const { |
| 1476 | switch (TargetPlatform) { |
| 1477 | case DarwinPlatformKind::MacOS: |
| 1478 | return "osx" ; |
| 1479 | case DarwinPlatformKind::IPhoneOS: |
| 1480 | if (TargetEnvironment == MacCatalyst) |
| 1481 | return "osx" ; |
| 1482 | return TargetEnvironment == NativeEnvironment || IgnoreSim ? "ios" |
| 1483 | : "iossim" ; |
| 1484 | case DarwinPlatformKind::TvOS: |
| 1485 | return TargetEnvironment == NativeEnvironment || IgnoreSim ? "tvos" |
| 1486 | : "tvossim" ; |
| 1487 | case DarwinPlatformKind::WatchOS: |
| 1488 | return TargetEnvironment == NativeEnvironment || IgnoreSim ? "watchos" |
| 1489 | : "watchossim" ; |
| 1490 | case DarwinPlatformKind::XROS: |
| 1491 | return TargetEnvironment == NativeEnvironment || IgnoreSim ? "xros" |
| 1492 | : "xrossim" ; |
| 1493 | case DarwinPlatformKind::DriverKit: |
| 1494 | return "driverkit" ; |
| 1495 | |
| 1496 | case DarwinPlatformKind::Firmware: |
| 1497 | break; |
| 1498 | } |
| 1499 | llvm_unreachable("Unsupported platform" ); |
| 1500 | } |
| 1501 | |
| 1502 | /// Check if the link command contains a symbol export directive. |
| 1503 | static bool hasExportSymbolDirective(const ArgList &Args) { |
| 1504 | for (Arg *A : Args) { |
| 1505 | if (A->getOption().matches(ID: options::OPT_exported__symbols__list)) |
| 1506 | return true; |
| 1507 | if (!A->getOption().matches(ID: options::OPT_Wl_COMMA) && |
| 1508 | !A->getOption().matches(ID: options::OPT_Xlinker)) |
| 1509 | continue; |
| 1510 | if (A->containsValue(Value: "-exported_symbols_list" ) || |
| 1511 | A->containsValue(Value: "-exported_symbol" )) |
| 1512 | return true; |
| 1513 | } |
| 1514 | return false; |
| 1515 | } |
| 1516 | |
| 1517 | /// Add an export directive for \p Symbol to the link command. |
| 1518 | static void addExportedSymbol(ArgStringList &CmdArgs, const char *Symbol) { |
| 1519 | CmdArgs.push_back(Elt: "-exported_symbol" ); |
| 1520 | CmdArgs.push_back(Elt: Symbol); |
| 1521 | } |
| 1522 | |
| 1523 | /// Add a sectalign directive for \p Segment and \p Section to the maximum |
| 1524 | /// expected page size for Darwin. |
| 1525 | /// |
| 1526 | /// On iPhone 6+ the max supported page size is 16K. On macOS, the max is 4K. |
| 1527 | /// Use a common alignment constant (16K) for now, and reduce the alignment on |
| 1528 | /// macOS if it proves important. |
| 1529 | static void (const ArgList &Args, ArgStringList &CmdArgs, |
| 1530 | StringRef Segment, StringRef Section) { |
| 1531 | for (const char *A : {"-sectalign" , Args.MakeArgString(Str: Segment), |
| 1532 | Args.MakeArgString(Str: Section), "0x4000" }) |
| 1533 | CmdArgs.push_back(Elt: A); |
| 1534 | } |
| 1535 | |
| 1536 | void Darwin::addProfileRTLibs(const ArgList &Args, |
| 1537 | ArgStringList &CmdArgs) const { |
| 1538 | if (!needsProfileRT(Args) && !needsGCovInstrumentation(Args)) |
| 1539 | return; |
| 1540 | |
| 1541 | AddLinkRuntimeLib(Args, CmdArgs, Component: "profile" , |
| 1542 | Opts: RuntimeLinkOptions(RLO_AlwaysLink)); |
| 1543 | |
| 1544 | bool ForGCOV = needsGCovInstrumentation(Args); |
| 1545 | |
| 1546 | // If we have a symbol export directive and we're linking in the profile |
| 1547 | // runtime, automatically export symbols necessary to implement some of the |
| 1548 | // runtime's functionality. |
| 1549 | if (hasExportSymbolDirective(Args) && ForGCOV) { |
| 1550 | addExportedSymbol(CmdArgs, Symbol: "___gcov_dump" ); |
| 1551 | addExportedSymbol(CmdArgs, Symbol: "___gcov_reset" ); |
| 1552 | addExportedSymbol(CmdArgs, Symbol: "_writeout_fn_list" ); |
| 1553 | addExportedSymbol(CmdArgs, Symbol: "_reset_fn_list" ); |
| 1554 | } |
| 1555 | |
| 1556 | // Align __llvm_prf_{cnts,bits,data} sections to the maximum expected page |
| 1557 | // alignment. This allows profile counters to be mmap()'d to disk. Note that |
| 1558 | // it's not enough to just page-align __llvm_prf_cnts: the following section |
| 1559 | // must also be page-aligned so that its data is not clobbered by mmap(). |
| 1560 | // |
| 1561 | // The section alignment is only needed when continuous profile sync is |
| 1562 | // enabled, but this is expected to be the default in Xcode. Specifying the |
| 1563 | // extra alignment also allows the same binary to be used with/without sync |
| 1564 | // enabled. |
| 1565 | if (!ForGCOV) { |
| 1566 | for (auto IPSK : {llvm::IPSK_cnts, llvm::IPSK_bitmap, llvm::IPSK_data}) { |
| 1567 | addSectalignToPage( |
| 1568 | Args, CmdArgs, Segment: "__DATA" , |
| 1569 | Section: llvm::getInstrProfSectionName(IPSK, OF: llvm::Triple::MachO, |
| 1570 | /*AddSegmentInfo=*/false)); |
| 1571 | } |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | void DarwinClang::AddLinkSanitizerLibArgs(const ArgList &Args, |
| 1576 | ArgStringList &CmdArgs, |
| 1577 | StringRef Sanitizer, |
| 1578 | bool Shared) const { |
| 1579 | auto RLO = RuntimeLinkOptions(RLO_AlwaysLink | (Shared ? RLO_AddRPath : 0U)); |
| 1580 | AddLinkRuntimeLib(Args, CmdArgs, Component: Sanitizer, Opts: RLO, IsShared: Shared); |
| 1581 | } |
| 1582 | |
| 1583 | ToolChain::RuntimeLibType DarwinClang::GetRuntimeLibType( |
| 1584 | const ArgList &Args) const { |
| 1585 | if (Arg* A = Args.getLastArg(Ids: options::OPT_rtlib_EQ)) { |
| 1586 | StringRef Value = A->getValue(); |
| 1587 | if (Value != "compiler-rt" && Value != "platform" ) |
| 1588 | getDriver().Diag(DiagID: clang::diag::err_drv_unsupported_rtlib_for_platform) |
| 1589 | << Value << "darwin" ; |
| 1590 | } |
| 1591 | |
| 1592 | return ToolChain::RLT_CompilerRT; |
| 1593 | } |
| 1594 | |
| 1595 | void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args, |
| 1596 | ArgStringList &CmdArgs, |
| 1597 | bool ForceLinkBuiltinRT) const { |
| 1598 | // Firmware uses the "bare metal" runtime lib. |
| 1599 | if (TargetPlatform == DarwinPlatformKind::Firmware) |
| 1600 | return MachO::AddLinkRuntimeLibArgs(Args, CmdArgs, ForceLinkBuiltinRT); |
| 1601 | |
| 1602 | // Call once to ensure diagnostic is printed if wrong value was specified |
| 1603 | GetRuntimeLibType(Args); |
| 1604 | |
| 1605 | // Darwin doesn't support real static executables, don't link any runtime |
| 1606 | // libraries with -static. |
| 1607 | if (Args.hasArg(Ids: options::OPT_static) || |
| 1608 | Args.hasArg(Ids: options::OPT_fapple_kext) || |
| 1609 | Args.hasArg(Ids: options::OPT_mkernel)) { |
| 1610 | if (ForceLinkBuiltinRT) |
| 1611 | AddLinkRuntimeLib(Args, CmdArgs, Component: "builtins" ); |
| 1612 | return; |
| 1613 | } |
| 1614 | |
| 1615 | // Reject -static-libgcc for now, we can deal with this when and if someone |
| 1616 | // cares. This is useful in situations where someone wants to statically link |
| 1617 | // something like libstdc++, and needs its runtime support routines. |
| 1618 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_static_libgcc)) { |
| 1619 | getDriver().Diag(DiagID: diag::err_drv_unsupported_opt) << A->getAsString(Args); |
| 1620 | return; |
| 1621 | } |
| 1622 | |
| 1623 | const SanitizerArgs &Sanitize = getSanitizerArgs(JobArgs: Args); |
| 1624 | |
| 1625 | if (!Sanitize.needsSharedRt()) { |
| 1626 | const char *sanitizer = nullptr; |
| 1627 | if (Sanitize.needsUbsanRt()) { |
| 1628 | sanitizer = "UndefinedBehaviorSanitizer" ; |
| 1629 | } else if (Sanitize.needsRtsanRt()) { |
| 1630 | sanitizer = "RealtimeSanitizer" ; |
| 1631 | } else if (Sanitize.needsAsanRt()) { |
| 1632 | sanitizer = "AddressSanitizer" ; |
| 1633 | } else if (Sanitize.needsTsanRt()) { |
| 1634 | sanitizer = "ThreadSanitizer" ; |
| 1635 | } |
| 1636 | if (sanitizer) { |
| 1637 | getDriver().Diag(DiagID: diag::err_drv_unsupported_static_sanitizer_darwin) |
| 1638 | << sanitizer; |
| 1639 | return; |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | if (Sanitize.linkRuntimes()) { |
| 1644 | if (Sanitize.needsAsanRt()) { |
| 1645 | if (Sanitize.needsStableAbi()) { |
| 1646 | AddLinkSanitizerLibArgs(Args, CmdArgs, Sanitizer: "asan_abi" , /*shared=*/Shared: false); |
| 1647 | } else { |
| 1648 | assert(Sanitize.needsSharedRt() && |
| 1649 | "Static sanitizer runtimes not supported" ); |
| 1650 | AddLinkSanitizerLibArgs(Args, CmdArgs, Sanitizer: "asan" ); |
| 1651 | } |
| 1652 | } |
| 1653 | if (Sanitize.needsRtsanRt()) { |
| 1654 | assert(Sanitize.needsSharedRt() && |
| 1655 | "Static sanitizer runtimes not supported" ); |
| 1656 | AddLinkSanitizerLibArgs(Args, CmdArgs, Sanitizer: "rtsan" ); |
| 1657 | } |
| 1658 | if (Sanitize.needsLsanRt()) |
| 1659 | AddLinkSanitizerLibArgs(Args, CmdArgs, Sanitizer: "lsan" ); |
| 1660 | if (Sanitize.needsUbsanRt()) { |
| 1661 | assert(Sanitize.needsSharedRt() && |
| 1662 | "Static sanitizer runtimes not supported" ); |
| 1663 | AddLinkSanitizerLibArgs( |
| 1664 | Args, CmdArgs, |
| 1665 | Sanitizer: Sanitize.requiresMinimalRuntime() ? "ubsan_minimal" : "ubsan" ); |
| 1666 | } |
| 1667 | if (Sanitize.needsTsanRt()) { |
| 1668 | assert(Sanitize.needsSharedRt() && |
| 1669 | "Static sanitizer runtimes not supported" ); |
| 1670 | AddLinkSanitizerLibArgs(Args, CmdArgs, Sanitizer: "tsan" ); |
| 1671 | } |
| 1672 | if (Sanitize.needsTysanRt()) |
| 1673 | AddLinkSanitizerLibArgs(Args, CmdArgs, Sanitizer: "tysan" ); |
| 1674 | if (Sanitize.needsFuzzer() && !Args.hasArg(Ids: options::OPT_dynamiclib)) { |
| 1675 | AddLinkSanitizerLibArgs(Args, CmdArgs, Sanitizer: "fuzzer" , /*shared=*/Shared: false); |
| 1676 | |
| 1677 | // Libfuzzer is written in C++ and requires libcxx. |
| 1678 | // Since darwin::Linker::ConstructJob already adds -lc++ for clang++ |
| 1679 | // by default if ShouldLinkCXXStdlib(Args), we only add the option if |
| 1680 | // !ShouldLinkCXXStdlib(Args). This avoids duplicate library errors |
| 1681 | // on Darwin. |
| 1682 | if (!ShouldLinkCXXStdlib(Args)) |
| 1683 | AddCXXStdlibLibArgs(Args, CmdArgs); |
| 1684 | } |
| 1685 | if (Sanitize.needsStatsRt()) { |
| 1686 | AddLinkRuntimeLib(Args, CmdArgs, Component: "stats_client" , Opts: RLO_AlwaysLink); |
| 1687 | AddLinkSanitizerLibArgs(Args, CmdArgs, Sanitizer: "stats" ); |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | if (Sanitize.needsMemProfRt()) |
| 1692 | if (hasExportSymbolDirective(Args)) |
| 1693 | addExportedSymbol( |
| 1694 | CmdArgs, |
| 1695 | Symbol: llvm::memprof::getMemprofOptionsSymbolDarwinLinkageName().data()); |
| 1696 | |
| 1697 | const XRayArgs &XRay = getXRayArgs(Args); |
| 1698 | if (XRay.needsXRayRt()) { |
| 1699 | AddLinkRuntimeLib(Args, CmdArgs, Component: "xray" ); |
| 1700 | AddLinkRuntimeLib(Args, CmdArgs, Component: "xray-basic" ); |
| 1701 | AddLinkRuntimeLib(Args, CmdArgs, Component: "xray-fdr" ); |
| 1702 | } |
| 1703 | |
| 1704 | if (isTargetDriverKit() && !Args.hasArg(Ids: options::OPT_nodriverkitlib)) { |
| 1705 | CmdArgs.push_back(Elt: "-framework" ); |
| 1706 | CmdArgs.push_back(Elt: "DriverKit" ); |
| 1707 | } |
| 1708 | |
| 1709 | // Otherwise link libSystem, then the dynamic runtime library, and finally any |
| 1710 | // target specific static runtime library. |
| 1711 | if (!isTargetDriverKit()) |
| 1712 | CmdArgs.push_back(Elt: "-lSystem" ); |
| 1713 | |
| 1714 | // Select the dynamic runtime library and the target specific static library. |
| 1715 | // Some old Darwin versions put builtins, libunwind, and some other stuff in |
| 1716 | // libgcc_s.1.dylib. MacOS X 10.6 and iOS 5 moved those functions to |
| 1717 | // libSystem, and made libgcc_s.1.dylib a stub. We never link libgcc_s when |
| 1718 | // building for aarch64 or iOS simulator, since libgcc_s was made obsolete |
| 1719 | // before either existed. |
| 1720 | if (getTriple().getArch() != llvm::Triple::aarch64 && |
| 1721 | ((isTargetIOSBased() && isIPhoneOSVersionLT(V0: 5, V1: 0) && |
| 1722 | !isTargetIOSSimulator()) || |
| 1723 | (isTargetMacOSBased() && isMacosxVersionLT(V0: 10, V1: 6)))) |
| 1724 | CmdArgs.push_back(Elt: "-lgcc_s.1" ); |
| 1725 | AddLinkRuntimeLib(Args, CmdArgs, Component: "builtins" ); |
| 1726 | } |
| 1727 | |
| 1728 | /// Returns the most appropriate macOS target version for the current process. |
| 1729 | /// |
| 1730 | /// If the macOS SDK version is the same or earlier than the system version, |
| 1731 | /// then the SDK version is returned. Otherwise the system version is returned. |
| 1732 | static std::string getSystemOrSDKMacOSVersion(StringRef MacOSSDKVersion) { |
| 1733 | llvm::Triple SystemTriple(llvm::sys::getProcessTriple()); |
| 1734 | if (!SystemTriple.isMacOSX()) |
| 1735 | return std::string(MacOSSDKVersion); |
| 1736 | VersionTuple SystemVersion; |
| 1737 | SystemTriple.getMacOSXVersion(Version&: SystemVersion); |
| 1738 | |
| 1739 | unsigned Major, Minor, Micro; |
| 1740 | bool ; |
| 1741 | if (!Driver::GetReleaseVersion(Str: MacOSSDKVersion, Major, Minor, Micro, |
| 1742 | HadExtra)) |
| 1743 | return std::string(MacOSSDKVersion); |
| 1744 | VersionTuple SDKVersion(Major, Minor, Micro); |
| 1745 | |
| 1746 | if (SDKVersion > SystemVersion) |
| 1747 | return SystemVersion.getAsString(); |
| 1748 | return std::string(MacOSSDKVersion); |
| 1749 | } |
| 1750 | |
| 1751 | namespace { |
| 1752 | |
| 1753 | /// The Darwin OS and version that was selected or inferred from arguments or |
| 1754 | /// environment. |
| 1755 | struct DarwinPlatform { |
| 1756 | enum SourceKind { |
| 1757 | /// The OS was specified using the -target argument. |
| 1758 | TargetArg, |
| 1759 | /// The OS was specified using the -mtargetos= argument. |
| 1760 | MTargetOSArg, |
| 1761 | /// The OS was specified using the -m<os>-version-min argument. |
| 1762 | OSVersionArg, |
| 1763 | /// The OS was specified using the OS_DEPLOYMENT_TARGET environment. |
| 1764 | DeploymentTargetEnv, |
| 1765 | /// The OS was inferred from the SDK. |
| 1766 | InferredFromSDK, |
| 1767 | /// The OS was inferred from the -arch. |
| 1768 | InferredFromArch |
| 1769 | }; |
| 1770 | |
| 1771 | using DarwinPlatformKind = Darwin::DarwinPlatformKind; |
| 1772 | using DarwinEnvironmentKind = Darwin::DarwinEnvironmentKind; |
| 1773 | |
| 1774 | DarwinPlatformKind getPlatform() const { return Platform; } |
| 1775 | |
| 1776 | DarwinEnvironmentKind getEnvironment() const { return Environment; } |
| 1777 | |
| 1778 | void setEnvironment(DarwinEnvironmentKind Kind) { |
| 1779 | Environment = Kind; |
| 1780 | InferSimulatorFromArch = false; |
| 1781 | } |
| 1782 | |
| 1783 | const VersionTuple getOSVersion() const { |
| 1784 | return UnderlyingOSVersion.value_or(u: VersionTuple()); |
| 1785 | } |
| 1786 | |
| 1787 | VersionTuple takeOSVersion() { |
| 1788 | assert(UnderlyingOSVersion.has_value() && |
| 1789 | "attempting to get an unset OS version" ); |
| 1790 | VersionTuple Result = *UnderlyingOSVersion; |
| 1791 | UnderlyingOSVersion.reset(); |
| 1792 | return Result; |
| 1793 | } |
| 1794 | bool isValidOSVersion() const { |
| 1795 | return llvm::Triple::isValidVersionForOS(OSKind: getOSFromPlatform(Platform), |
| 1796 | Version: getOSVersion()); |
| 1797 | } |
| 1798 | |
| 1799 | VersionTuple getCanonicalOSVersion() const { |
| 1800 | return llvm::Triple::getCanonicalVersionForOS( |
| 1801 | OSKind: getOSFromPlatform(Platform), Version: getOSVersion(), /*IsInValidRange=*/true); |
| 1802 | } |
| 1803 | |
| 1804 | void setOSVersion(const VersionTuple &Version) { |
| 1805 | UnderlyingOSVersion = Version; |
| 1806 | } |
| 1807 | |
| 1808 | bool hasOSVersion() const { return UnderlyingOSVersion.has_value(); } |
| 1809 | |
| 1810 | VersionTuple getZipperedOSVersion() const { |
| 1811 | assert(Environment == DarwinEnvironmentKind::MacCatalyst && |
| 1812 | "zippered target version is specified only for Mac Catalyst" ); |
| 1813 | return ZipperedOSVersion; |
| 1814 | } |
| 1815 | |
| 1816 | /// Returns true if the target OS was explicitly specified. |
| 1817 | bool isExplicitlySpecified() const { return Kind <= DeploymentTargetEnv; } |
| 1818 | |
| 1819 | /// Returns true if the simulator environment can be inferred from the arch. |
| 1820 | bool canInferSimulatorFromArch() const { return InferSimulatorFromArch; } |
| 1821 | |
| 1822 | const std::optional<llvm::Triple> &getTargetVariantTriple() const { |
| 1823 | return TargetVariantTriple; |
| 1824 | } |
| 1825 | |
| 1826 | /// Adds the -m<os>-version-min argument to the compiler invocation. |
| 1827 | void addOSVersionMinArgument(DerivedArgList &Args, const OptTable &Opts) { |
| 1828 | auto &[Arg, OSVersionStr] = Arguments; |
| 1829 | if (Arg) |
| 1830 | return; |
| 1831 | assert(Kind != TargetArg && Kind != MTargetOSArg && Kind != OSVersionArg && |
| 1832 | "Invalid kind" ); |
| 1833 | options::ID Opt; |
| 1834 | switch (Platform) { |
| 1835 | case DarwinPlatformKind::MacOS: |
| 1836 | Opt = options::OPT_mmacos_version_min_EQ; |
| 1837 | break; |
| 1838 | case DarwinPlatformKind::IPhoneOS: |
| 1839 | Opt = options::OPT_mios_version_min_EQ; |
| 1840 | break; |
| 1841 | case DarwinPlatformKind::TvOS: |
| 1842 | Opt = options::OPT_mtvos_version_min_EQ; |
| 1843 | break; |
| 1844 | case DarwinPlatformKind::WatchOS: |
| 1845 | Opt = options::OPT_mwatchos_version_min_EQ; |
| 1846 | break; |
| 1847 | default: |
| 1848 | // New platforms always explicitly provide a version in the triple. |
| 1849 | return; |
| 1850 | } |
| 1851 | Arg = Args.MakeJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt), Value: OSVersionStr); |
| 1852 | Args.append(A: Arg); |
| 1853 | } |
| 1854 | |
| 1855 | /// Returns the OS version with the argument / environment variable that |
| 1856 | /// specified it. |
| 1857 | std::string getAsString(DerivedArgList &Args, const OptTable &Opts) { |
| 1858 | auto &[Arg, OSVersionStr] = Arguments; |
| 1859 | switch (Kind) { |
| 1860 | case TargetArg: |
| 1861 | case MTargetOSArg: |
| 1862 | case OSVersionArg: |
| 1863 | assert(Arg && "OS version argument not yet inferred" ); |
| 1864 | return Arg->getAsString(Args); |
| 1865 | case DeploymentTargetEnv: |
| 1866 | return (llvm::Twine(EnvVarName) + "=" + OSVersionStr).str(); |
| 1867 | case InferredFromSDK: |
| 1868 | case InferredFromArch: |
| 1869 | llvm_unreachable("Cannot print arguments for inferred OS version" ); |
| 1870 | } |
| 1871 | llvm_unreachable("Unsupported Darwin Source Kind" ); |
| 1872 | } |
| 1873 | |
| 1874 | // Returns the inferred source of how the OS version was resolved. |
| 1875 | std::string getInferredSource() { |
| 1876 | assert(!isExplicitlySpecified() && "OS version was not inferred" ); |
| 1877 | return InferredSource.str(); |
| 1878 | } |
| 1879 | |
| 1880 | void setEnvironment(llvm::Triple::EnvironmentType EnvType, |
| 1881 | const VersionTuple &OSVersion, |
| 1882 | const std::optional<DarwinSDKInfo> &SDKInfo) { |
| 1883 | switch (EnvType) { |
| 1884 | case llvm::Triple::Simulator: |
| 1885 | Environment = DarwinEnvironmentKind::Simulator; |
| 1886 | break; |
| 1887 | case llvm::Triple::MacABI: { |
| 1888 | Environment = DarwinEnvironmentKind::MacCatalyst; |
| 1889 | // The minimum native macOS target for MacCatalyst is macOS 10.15. |
| 1890 | ZipperedOSVersion = VersionTuple(10, 15); |
| 1891 | if (hasOSVersion() && SDKInfo) { |
| 1892 | if (const auto *MacCatalystToMacOSMapping = SDKInfo->getVersionMapping( |
| 1893 | Kind: DarwinSDKInfo::OSEnvPair::macCatalystToMacOSPair())) { |
| 1894 | if (auto MacOSVersion = MacCatalystToMacOSMapping->map( |
| 1895 | Key: OSVersion, MinimumValue: ZipperedOSVersion, MaximumValue: std::nullopt)) { |
| 1896 | ZipperedOSVersion = *MacOSVersion; |
| 1897 | } |
| 1898 | } |
| 1899 | } |
| 1900 | // In a zippered build, we could be building for a macOS target that's |
| 1901 | // lower than the version that's implied by the OS version. In that case |
| 1902 | // we need to use the minimum version as the native target version. |
| 1903 | if (TargetVariantTriple) { |
| 1904 | auto TargetVariantVersion = TargetVariantTriple->getOSVersion(); |
| 1905 | if (TargetVariantVersion.getMajor()) { |
| 1906 | if (TargetVariantVersion < ZipperedOSVersion) |
| 1907 | ZipperedOSVersion = std::move(TargetVariantVersion); |
| 1908 | } |
| 1909 | } |
| 1910 | break; |
| 1911 | } |
| 1912 | default: |
| 1913 | break; |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | static DarwinPlatform |
| 1918 | createFromTarget(const llvm::Triple &TT, Arg *A, |
| 1919 | std::optional<llvm::Triple> TargetVariantTriple, |
| 1920 | const std::optional<DarwinSDKInfo> &SDKInfo) { |
| 1921 | DarwinPlatform Result(TargetArg, getPlatformFromOS(OS: TT.getOS()), |
| 1922 | TT.getOSVersion(), A); |
| 1923 | VersionTuple OsVersion = TT.getOSVersion(); |
| 1924 | Result.TargetVariantTriple = TargetVariantTriple; |
| 1925 | Result.setEnvironment(EnvType: TT.getEnvironment(), OSVersion: OsVersion, SDKInfo); |
| 1926 | return Result; |
| 1927 | } |
| 1928 | static DarwinPlatform |
| 1929 | createFromMTargetOS(llvm::Triple::OSType OS, VersionTuple OSVersion, |
| 1930 | llvm::Triple::EnvironmentType Environment, Arg *A, |
| 1931 | const std::optional<DarwinSDKInfo> &SDKInfo) { |
| 1932 | DarwinPlatform Result(MTargetOSArg, getPlatformFromOS(OS), OSVersion, A); |
| 1933 | Result.InferSimulatorFromArch = false; |
| 1934 | Result.setEnvironment(EnvType: Environment, OSVersion, SDKInfo); |
| 1935 | return Result; |
| 1936 | } |
| 1937 | static DarwinPlatform createOSVersionArg(DarwinPlatformKind Platform, Arg *A, |
| 1938 | bool IsSimulator) { |
| 1939 | DarwinPlatform Result{OSVersionArg, Platform, |
| 1940 | getVersionFromString(Input: A->getValue()), A}; |
| 1941 | if (IsSimulator) |
| 1942 | Result.Environment = DarwinEnvironmentKind::Simulator; |
| 1943 | return Result; |
| 1944 | } |
| 1945 | static DarwinPlatform createDeploymentTargetEnv(DarwinPlatformKind Platform, |
| 1946 | StringRef EnvVarName, |
| 1947 | StringRef OSVersion) { |
| 1948 | DarwinPlatform Result(DeploymentTargetEnv, Platform, |
| 1949 | getVersionFromString(Input: OSVersion)); |
| 1950 | Result.EnvVarName = EnvVarName; |
| 1951 | return Result; |
| 1952 | } |
| 1953 | static DarwinPlatform createFromSDKInfo(StringRef SDKRoot, |
| 1954 | const DarwinSDKInfo &SDKInfo) { |
| 1955 | const DarwinSDKInfo::SDKPlatformInfo PlatformInfo = |
| 1956 | SDKInfo.getCanonicalPlatformInfo(); |
| 1957 | const llvm::Triple::OSType OS = PlatformInfo.getOS(); |
| 1958 | VersionTuple Version = SDKInfo.getVersion(); |
| 1959 | if (OS == llvm::Triple::MacOSX) |
| 1960 | Version = getVersionFromString( |
| 1961 | Input: getSystemOrSDKMacOSVersion(MacOSSDKVersion: Version.getAsString())); |
| 1962 | DarwinPlatform Result(InferredFromSDK, getPlatformFromOS(OS), Version); |
| 1963 | Result.Environment = getEnvKindFromEnvType(EnvironmentType: PlatformInfo.getEnvironment()); |
| 1964 | Result.InferSimulatorFromArch = false; |
| 1965 | Result.InferredSource = SDKRoot; |
| 1966 | return Result; |
| 1967 | } |
| 1968 | static DarwinPlatform createFromSDK(StringRef SDKRoot, |
| 1969 | DarwinPlatformKind Platform, |
| 1970 | StringRef Value, |
| 1971 | bool IsSimulator = false) { |
| 1972 | DarwinPlatform Result(InferredFromSDK, Platform, |
| 1973 | getVersionFromString(Input: Value)); |
| 1974 | if (IsSimulator) |
| 1975 | Result.Environment = DarwinEnvironmentKind::Simulator; |
| 1976 | Result.InferSimulatorFromArch = false; |
| 1977 | Result.InferredSource = SDKRoot; |
| 1978 | return Result; |
| 1979 | } |
| 1980 | static DarwinPlatform createFromArch(StringRef Arch, llvm::Triple::OSType OS, |
| 1981 | VersionTuple Version) { |
| 1982 | auto Result = |
| 1983 | DarwinPlatform(InferredFromArch, getPlatformFromOS(OS), Version); |
| 1984 | Result.InferredSource = Arch; |
| 1985 | return Result; |
| 1986 | } |
| 1987 | |
| 1988 | /// Constructs an inferred SDKInfo value based on the version inferred from |
| 1989 | /// the SDK path itself. Only works for values that were created by inferring |
| 1990 | /// the platform from the SDKPath. |
| 1991 | DarwinSDKInfo inferSDKInfo() { |
| 1992 | assert(Kind == InferredFromSDK && "can infer SDK info only" ); |
| 1993 | llvm::Triple::OSType OS = getOSFromPlatform(Platform); |
| 1994 | llvm::Triple::EnvironmentType EnvironmentType = |
| 1995 | getEnvTypeFromEnvKind(EnvironmentKind: Environment); |
| 1996 | StringRef PlatformPrefix = |
| 1997 | (Platform == DarwinPlatformKind::DriverKit) ? "/System/DriverKit" : "" ; |
| 1998 | return DarwinSDKInfo("" , OS, EnvironmentType, getOSVersion(), |
| 1999 | getDisplayName(TargetPlatform: Platform, TargetEnvironment: Environment, Version: getOSVersion()), |
| 2000 | /*MaximumDeploymentTarget=*/ |
| 2001 | VersionTuple(getOSVersion().getMajor(), 0, 99), |
| 2002 | {DarwinSDKInfo::SDKPlatformInfo( |
| 2003 | llvm::Triple::Apple, OS, EnvironmentType, |
| 2004 | llvm::Triple::MachO, PlatformPrefix)}); |
| 2005 | } |
| 2006 | |
| 2007 | private: |
| 2008 | DarwinPlatform(SourceKind Kind, DarwinPlatformKind Platform, Arg *Argument) |
| 2009 | : Kind(Kind), Platform(Platform), |
| 2010 | Arguments({Argument, VersionTuple().getAsString()}) {} |
| 2011 | DarwinPlatform(SourceKind Kind, DarwinPlatformKind Platform, |
| 2012 | VersionTuple Value, Arg *Argument = nullptr) |
| 2013 | : Kind(Kind), Platform(Platform), |
| 2014 | Arguments({Argument, Value.getAsString()}) { |
| 2015 | if (!Value.empty()) |
| 2016 | UnderlyingOSVersion = Value; |
| 2017 | } |
| 2018 | |
| 2019 | static VersionTuple getVersionFromString(const StringRef Input) { |
| 2020 | llvm::VersionTuple Version; |
| 2021 | bool IsValid = !Version.tryParse(string: Input); |
| 2022 | assert(IsValid && "unable to convert input version to version tuple" ); |
| 2023 | (void)IsValid; |
| 2024 | return Version; |
| 2025 | } |
| 2026 | |
| 2027 | static DarwinPlatformKind getPlatformFromOS(llvm::Triple::OSType OS) { |
| 2028 | switch (OS) { |
| 2029 | case llvm::Triple::Darwin: |
| 2030 | case llvm::Triple::MacOSX: |
| 2031 | return DarwinPlatformKind::MacOS; |
| 2032 | case llvm::Triple::IOS: |
| 2033 | return DarwinPlatformKind::IPhoneOS; |
| 2034 | case llvm::Triple::TvOS: |
| 2035 | return DarwinPlatformKind::TvOS; |
| 2036 | case llvm::Triple::WatchOS: |
| 2037 | return DarwinPlatformKind::WatchOS; |
| 2038 | case llvm::Triple::XROS: |
| 2039 | return DarwinPlatformKind::XROS; |
| 2040 | case llvm::Triple::DriverKit: |
| 2041 | return DarwinPlatformKind::DriverKit; |
| 2042 | case llvm::Triple::Firmware: |
| 2043 | return DarwinPlatformKind::Firmware; |
| 2044 | default: |
| 2045 | llvm_unreachable("Unable to infer Darwin variant" ); |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | static llvm::Triple::OSType getOSFromPlatform(DarwinPlatformKind Platform) { |
| 2050 | switch (Platform) { |
| 2051 | case DarwinPlatformKind::MacOS: |
| 2052 | return llvm::Triple::MacOSX; |
| 2053 | case DarwinPlatformKind::IPhoneOS: |
| 2054 | return llvm::Triple::IOS; |
| 2055 | case DarwinPlatformKind::TvOS: |
| 2056 | return llvm::Triple::TvOS; |
| 2057 | case DarwinPlatformKind::WatchOS: |
| 2058 | return llvm::Triple::WatchOS; |
| 2059 | case DarwinPlatformKind::DriverKit: |
| 2060 | return llvm::Triple::DriverKit; |
| 2061 | case DarwinPlatformKind::XROS: |
| 2062 | return llvm::Triple::XROS; |
| 2063 | case DarwinPlatformKind::Firmware: |
| 2064 | return llvm::Triple::Firmware; |
| 2065 | } |
| 2066 | llvm_unreachable("Unknown DarwinPlatformKind enum" ); |
| 2067 | } |
| 2068 | |
| 2069 | static DarwinEnvironmentKind |
| 2070 | getEnvKindFromEnvType(llvm::Triple::EnvironmentType EnvironmentType) { |
| 2071 | switch (EnvironmentType) { |
| 2072 | case llvm::Triple::UnknownEnvironment: |
| 2073 | return DarwinEnvironmentKind::NativeEnvironment; |
| 2074 | case llvm::Triple::Simulator: |
| 2075 | return DarwinEnvironmentKind::Simulator; |
| 2076 | case llvm::Triple::MacABI: |
| 2077 | return DarwinEnvironmentKind::MacCatalyst; |
| 2078 | default: |
| 2079 | llvm_unreachable("Unable to infer Darwin environment" ); |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | static llvm::Triple::EnvironmentType |
| 2084 | getEnvTypeFromEnvKind(DarwinEnvironmentKind EnvironmentKind) { |
| 2085 | switch (EnvironmentKind) { |
| 2086 | case DarwinEnvironmentKind::NativeEnvironment: |
| 2087 | return llvm::Triple::UnknownEnvironment; |
| 2088 | case DarwinEnvironmentKind::Simulator: |
| 2089 | return llvm::Triple::Simulator; |
| 2090 | case DarwinEnvironmentKind::MacCatalyst: |
| 2091 | return llvm::Triple::MacABI; |
| 2092 | } |
| 2093 | llvm_unreachable("Unknown DarwinEnvironmentKind enum" ); |
| 2094 | } |
| 2095 | |
| 2096 | static std::string getDisplayName(DarwinPlatformKind TargetPlatform, |
| 2097 | DarwinEnvironmentKind TargetEnvironment, |
| 2098 | VersionTuple Version) { |
| 2099 | SmallVector<std::string, 3> Components; |
| 2100 | switch (TargetPlatform) { |
| 2101 | case DarwinPlatformKind::MacOS: |
| 2102 | Components.push_back(Elt: "macOS" ); |
| 2103 | break; |
| 2104 | case DarwinPlatformKind::IPhoneOS: |
| 2105 | Components.push_back(Elt: "iOS" ); |
| 2106 | break; |
| 2107 | case DarwinPlatformKind::TvOS: |
| 2108 | Components.push_back(Elt: "tvOS" ); |
| 2109 | break; |
| 2110 | case DarwinPlatformKind::WatchOS: |
| 2111 | Components.push_back(Elt: "watchOS" ); |
| 2112 | break; |
| 2113 | case DarwinPlatformKind::DriverKit: |
| 2114 | Components.push_back(Elt: "DriverKit" ); |
| 2115 | break; |
| 2116 | default: |
| 2117 | llvm::reportFatalUsageError(reason: Twine("Platform: '" ) + |
| 2118 | std::to_string(val: TargetPlatform) + |
| 2119 | "' is unsupported when inferring SDK Info." ); |
| 2120 | } |
| 2121 | switch (TargetEnvironment) { |
| 2122 | case DarwinEnvironmentKind::NativeEnvironment: |
| 2123 | break; |
| 2124 | case DarwinEnvironmentKind::Simulator: |
| 2125 | Components.push_back(Elt: "Simulator" ); |
| 2126 | break; |
| 2127 | default: |
| 2128 | llvm::reportFatalUsageError(reason: Twine("Environment: '" ) + |
| 2129 | std::to_string(val: TargetEnvironment) + |
| 2130 | "' is unsupported when inferring SDK Info." ); |
| 2131 | } |
| 2132 | Components.push_back(Elt: Version.getAsString()); |
| 2133 | return join(R&: Components, Separator: " " ); |
| 2134 | } |
| 2135 | |
| 2136 | SourceKind Kind; |
| 2137 | DarwinPlatformKind Platform; |
| 2138 | DarwinEnvironmentKind Environment = DarwinEnvironmentKind::NativeEnvironment; |
| 2139 | // When compiling for a zippered target, this means both target & |
| 2140 | // target variant is set on the command line, ZipperedOSVersion holds the |
| 2141 | // OSVersion tied to the main target value. |
| 2142 | VersionTuple ZipperedOSVersion; |
| 2143 | // We allow multiple ways to set or default the OS |
| 2144 | // version used for compilation. When set, UnderlyingOSVersion represents |
| 2145 | // the intended version to match the platform information computed from |
| 2146 | // arguments. |
| 2147 | std::optional<VersionTuple> UnderlyingOSVersion; |
| 2148 | bool InferSimulatorFromArch = true; |
| 2149 | std::pair<Arg *, std::string> Arguments; |
| 2150 | StringRef EnvVarName; |
| 2151 | // If the DarwinPlatform information is derived from an inferred source, this |
| 2152 | // captures what that source input was for error reporting. |
| 2153 | StringRef InferredSource; |
| 2154 | // When compiling for a zippered target, this value represents the target |
| 2155 | // triple encoded in the target variant. |
| 2156 | std::optional<llvm::Triple> TargetVariantTriple; |
| 2157 | }; |
| 2158 | |
| 2159 | /// Returns the deployment target that's specified using the -m<os>-version-min |
| 2160 | /// argument. |
| 2161 | std::optional<DarwinPlatform> |
| 2162 | getDeploymentTargetFromOSVersionArg(DerivedArgList &Args, |
| 2163 | const Driver &TheDriver) { |
| 2164 | Arg *macOSVersion = Args.getLastArg(Ids: options::OPT_mmacos_version_min_EQ); |
| 2165 | Arg *iOSVersion = Args.getLastArg(Ids: options::OPT_mios_version_min_EQ, |
| 2166 | Ids: options::OPT_mios_simulator_version_min_EQ); |
| 2167 | Arg *TvOSVersion = |
| 2168 | Args.getLastArg(Ids: options::OPT_mtvos_version_min_EQ, |
| 2169 | Ids: options::OPT_mtvos_simulator_version_min_EQ); |
| 2170 | Arg *WatchOSVersion = |
| 2171 | Args.getLastArg(Ids: options::OPT_mwatchos_version_min_EQ, |
| 2172 | Ids: options::OPT_mwatchos_simulator_version_min_EQ); |
| 2173 | |
| 2174 | auto GetDarwinPlatform = |
| 2175 | [&](DarwinPlatform::DarwinPlatformKind Platform, Arg *VersionArg, |
| 2176 | bool IsSimulator) -> std::optional<DarwinPlatform> { |
| 2177 | if (StringRef(VersionArg->getValue()).empty()) { |
| 2178 | TheDriver.Diag(DiagID: diag::err_drv_missing_version_number) |
| 2179 | << VersionArg->getAsString(Args); |
| 2180 | return std::nullopt; |
| 2181 | } |
| 2182 | return DarwinPlatform::createOSVersionArg(Platform, A: VersionArg, |
| 2183 | /*IsSimulator=*/IsSimulator); |
| 2184 | }; |
| 2185 | |
| 2186 | if (macOSVersion) { |
| 2187 | if (iOSVersion || TvOSVersion || WatchOSVersion) { |
| 2188 | TheDriver.Diag(DiagID: diag::err_drv_argument_not_allowed_with) |
| 2189 | << macOSVersion->getAsString(Args) |
| 2190 | << (iOSVersion ? iOSVersion |
| 2191 | : TvOSVersion ? TvOSVersion : WatchOSVersion) |
| 2192 | ->getAsString(Args); |
| 2193 | } |
| 2194 | return GetDarwinPlatform(Darwin::MacOS, macOSVersion, |
| 2195 | /*IsSimulator=*/false); |
| 2196 | |
| 2197 | } else if (iOSVersion) { |
| 2198 | if (TvOSVersion || WatchOSVersion) { |
| 2199 | TheDriver.Diag(DiagID: diag::err_drv_argument_not_allowed_with) |
| 2200 | << iOSVersion->getAsString(Args) |
| 2201 | << (TvOSVersion ? TvOSVersion : WatchOSVersion)->getAsString(Args); |
| 2202 | } |
| 2203 | return GetDarwinPlatform(Darwin::IPhoneOS, iOSVersion, |
| 2204 | iOSVersion->getOption().getID() == |
| 2205 | options::OPT_mios_simulator_version_min_EQ); |
| 2206 | } else if (TvOSVersion) { |
| 2207 | if (WatchOSVersion) { |
| 2208 | TheDriver.Diag(DiagID: diag::err_drv_argument_not_allowed_with) |
| 2209 | << TvOSVersion->getAsString(Args) |
| 2210 | << WatchOSVersion->getAsString(Args); |
| 2211 | } |
| 2212 | return GetDarwinPlatform(Darwin::TvOS, TvOSVersion, |
| 2213 | TvOSVersion->getOption().getID() == |
| 2214 | options::OPT_mtvos_simulator_version_min_EQ); |
| 2215 | } else if (WatchOSVersion) |
| 2216 | return GetDarwinPlatform( |
| 2217 | Darwin::WatchOS, WatchOSVersion, |
| 2218 | WatchOSVersion->getOption().getID() == |
| 2219 | options::OPT_mwatchos_simulator_version_min_EQ); |
| 2220 | return std::nullopt; |
| 2221 | } |
| 2222 | |
| 2223 | /// Returns the deployment target that's specified using the |
| 2224 | /// OS_DEPLOYMENT_TARGET environment variable. |
| 2225 | std::optional<DarwinPlatform> |
| 2226 | getDeploymentTargetFromEnvironmentVariables(const Driver &TheDriver, |
| 2227 | const llvm::Triple &Triple) { |
| 2228 | const char *EnvVars[] = { |
| 2229 | "MACOSX_DEPLOYMENT_TARGET" , |
| 2230 | "IPHONEOS_DEPLOYMENT_TARGET" , |
| 2231 | "TVOS_DEPLOYMENT_TARGET" , |
| 2232 | "WATCHOS_DEPLOYMENT_TARGET" , |
| 2233 | "DRIVERKIT_DEPLOYMENT_TARGET" , |
| 2234 | "XROS_DEPLOYMENT_TARGET" |
| 2235 | }; |
| 2236 | std::string Targets[std::size(EnvVars)]; |
| 2237 | for (const auto &I : llvm::enumerate(First: llvm::ArrayRef(EnvVars))) { |
| 2238 | if (char *Env = ::getenv(name: I.value())) |
| 2239 | Targets[I.index()] = Env; |
| 2240 | } |
| 2241 | |
| 2242 | // Allow conflicts among OSX and iOS for historical reasons, but choose the |
| 2243 | // default platform. |
| 2244 | if (!Targets[Darwin::MacOS].empty() && |
| 2245 | (!Targets[Darwin::IPhoneOS].empty() || |
| 2246 | !Targets[Darwin::WatchOS].empty() || !Targets[Darwin::TvOS].empty() || |
| 2247 | !Targets[Darwin::XROS].empty())) { |
| 2248 | if (Triple.getArch() == llvm::Triple::arm || |
| 2249 | Triple.getArch() == llvm::Triple::aarch64 || |
| 2250 | Triple.getArch() == llvm::Triple::thumb) |
| 2251 | Targets[Darwin::MacOS] = "" ; |
| 2252 | else |
| 2253 | Targets[Darwin::IPhoneOS] = Targets[Darwin::WatchOS] = |
| 2254 | Targets[Darwin::TvOS] = Targets[Darwin::XROS] = "" ; |
| 2255 | } else { |
| 2256 | // Don't allow conflicts in any other platform. |
| 2257 | unsigned FirstTarget = std::size(Targets); |
| 2258 | for (unsigned I = 0; I != std::size(Targets); ++I) { |
| 2259 | if (Targets[I].empty()) |
| 2260 | continue; |
| 2261 | if (FirstTarget == std::size(Targets)) |
| 2262 | FirstTarget = I; |
| 2263 | else |
| 2264 | TheDriver.Diag(DiagID: diag::err_drv_conflicting_deployment_targets) |
| 2265 | << Targets[FirstTarget] << Targets[I]; |
| 2266 | } |
| 2267 | } |
| 2268 | |
| 2269 | for (const auto &Target : llvm::enumerate(First: llvm::ArrayRef(Targets))) { |
| 2270 | if (!Target.value().empty()) |
| 2271 | return DarwinPlatform::createDeploymentTargetEnv( |
| 2272 | Platform: (Darwin::DarwinPlatformKind)Target.index(), EnvVarName: EnvVars[Target.index()], |
| 2273 | OSVersion: Target.value()); |
| 2274 | } |
| 2275 | return std::nullopt; |
| 2276 | } |
| 2277 | |
| 2278 | /// Tries to infer the deployment target from the SDK specified by -isysroot |
| 2279 | /// (or SDKROOT). Uses the version specified in the SDKSettings.json file if |
| 2280 | /// it's available. |
| 2281 | std::optional<DarwinPlatform> |
| 2282 | inferDeploymentTargetFromSDK(DerivedArgList &Args, |
| 2283 | const std::optional<DarwinSDKInfo> &SDKInfo) { |
| 2284 | const Arg *A = Args.getLastArg(Ids: options::OPT_isysroot); |
| 2285 | if (!A) |
| 2286 | return std::nullopt; |
| 2287 | StringRef isysroot = A->getValue(); |
| 2288 | if (SDKInfo) |
| 2289 | return DarwinPlatform::createFromSDKInfo(SDKRoot: isysroot, SDKInfo: *SDKInfo); |
| 2290 | |
| 2291 | StringRef SDK = Darwin::getSDKName(isysroot); |
| 2292 | if (!SDK.size()) |
| 2293 | return std::nullopt; |
| 2294 | |
| 2295 | std::string Version; |
| 2296 | // Slice the version number out. |
| 2297 | // Version number is between the first and the last number. |
| 2298 | size_t StartVer = SDK.find_first_of(Chars: "0123456789" ); |
| 2299 | size_t EndVer = SDK.find_last_of(Chars: "0123456789" ); |
| 2300 | if (StartVer != StringRef::npos && EndVer > StartVer) |
| 2301 | Version = std::string(SDK.slice(Start: StartVer, End: EndVer + 1)); |
| 2302 | if (Version.empty()) |
| 2303 | return std::nullopt; |
| 2304 | |
| 2305 | if (SDK.starts_with(Prefix: "iPhoneOS" ) || SDK.starts_with(Prefix: "iPhoneSimulator" )) |
| 2306 | return DarwinPlatform::createFromSDK( |
| 2307 | SDKRoot: isysroot, Platform: Darwin::IPhoneOS, Value: Version, |
| 2308 | /*IsSimulator=*/SDK.starts_with(Prefix: "iPhoneSimulator" )); |
| 2309 | else if (SDK.starts_with(Prefix: "MacOSX" )) |
| 2310 | return DarwinPlatform::createFromSDK(SDKRoot: isysroot, Platform: Darwin::MacOS, |
| 2311 | Value: getSystemOrSDKMacOSVersion(MacOSSDKVersion: Version)); |
| 2312 | else if (SDK.starts_with(Prefix: "WatchOS" ) || SDK.starts_with(Prefix: "WatchSimulator" )) |
| 2313 | return DarwinPlatform::createFromSDK( |
| 2314 | SDKRoot: isysroot, Platform: Darwin::WatchOS, Value: Version, |
| 2315 | /*IsSimulator=*/SDK.starts_with(Prefix: "WatchSimulator" )); |
| 2316 | else if (SDK.starts_with(Prefix: "AppleTVOS" ) || SDK.starts_with(Prefix: "AppleTVSimulator" )) |
| 2317 | return DarwinPlatform::createFromSDK( |
| 2318 | SDKRoot: isysroot, Platform: Darwin::TvOS, Value: Version, |
| 2319 | /*IsSimulator=*/SDK.starts_with(Prefix: "AppleTVSimulator" )); |
| 2320 | else if (SDK.starts_with(Prefix: "DriverKit" )) |
| 2321 | return DarwinPlatform::createFromSDK(SDKRoot: isysroot, Platform: Darwin::DriverKit, Value: Version); |
| 2322 | return std::nullopt; |
| 2323 | } |
| 2324 | |
| 2325 | // Compute & get the OS Version when the target triple omitted one. |
| 2326 | VersionTuple getInferredOSVersion(llvm::Triple::OSType OS, |
| 2327 | const llvm::Triple &Triple, |
| 2328 | const Driver &TheDriver) { |
| 2329 | VersionTuple OsVersion; |
| 2330 | llvm::Triple SystemTriple(llvm::sys::getProcessTriple()); |
| 2331 | switch (OS) { |
| 2332 | case llvm::Triple::Darwin: |
| 2333 | case llvm::Triple::MacOSX: |
| 2334 | // If there is no version specified on triple, and both host and target are |
| 2335 | // macos, use the host triple to infer OS version. |
| 2336 | if (Triple.isMacOSX() && SystemTriple.isMacOSX() && |
| 2337 | !Triple.getOSMajorVersion()) |
| 2338 | SystemTriple.getMacOSXVersion(Version&: OsVersion); |
| 2339 | else if (!Triple.getMacOSXVersion(Version&: OsVersion)) |
| 2340 | TheDriver.Diag(DiagID: diag::err_drv_invalid_darwin_version) |
| 2341 | << Triple.getOSName(); |
| 2342 | break; |
| 2343 | case llvm::Triple::IOS: |
| 2344 | if (Triple.isMacCatalystEnvironment() && !Triple.getOSMajorVersion()) { |
| 2345 | OsVersion = VersionTuple(13, 1); |
| 2346 | } else |
| 2347 | OsVersion = Triple.getiOSVersion(); |
| 2348 | break; |
| 2349 | case llvm::Triple::TvOS: |
| 2350 | OsVersion = Triple.getOSVersion(); |
| 2351 | break; |
| 2352 | case llvm::Triple::WatchOS: |
| 2353 | OsVersion = Triple.getWatchOSVersion(); |
| 2354 | break; |
| 2355 | case llvm::Triple::DriverKit: |
| 2356 | OsVersion = Triple.getDriverKitVersion(); |
| 2357 | break; |
| 2358 | default: |
| 2359 | OsVersion = Triple.getOSVersion(); |
| 2360 | if (!OsVersion.getMajor()) |
| 2361 | OsVersion = OsVersion.withMajorReplaced(NewMajor: 1); |
| 2362 | break; |
| 2363 | } |
| 2364 | return OsVersion; |
| 2365 | } |
| 2366 | |
| 2367 | /// Tries to infer the target OS from the -arch. |
| 2368 | std::optional<DarwinPlatform> |
| 2369 | inferDeploymentTargetFromArch(DerivedArgList &Args, const Darwin &Toolchain, |
| 2370 | const llvm::Triple &Triple, |
| 2371 | const Driver &TheDriver) { |
| 2372 | llvm::Triple::OSType OSTy = llvm::Triple::UnknownOS; |
| 2373 | |
| 2374 | StringRef MachOArchName = Toolchain.getMachOArchName(Args); |
| 2375 | if (MachOArchName == "arm64" || MachOArchName == "arm64e" ) |
| 2376 | OSTy = llvm::Triple::MacOSX; |
| 2377 | else if (MachOArchName == "armv7" || MachOArchName == "armv7s" || |
| 2378 | MachOArchName == "armv6" ) |
| 2379 | OSTy = llvm::Triple::IOS; |
| 2380 | else if (MachOArchName == "armv7k" || MachOArchName == "arm64_32" ) |
| 2381 | OSTy = llvm::Triple::WatchOS; |
| 2382 | else if (MachOArchName != "armv6m" && MachOArchName != "armv7m" && |
| 2383 | MachOArchName != "armv7em" ) |
| 2384 | OSTy = llvm::Triple::MacOSX; |
| 2385 | if (OSTy == llvm::Triple::UnknownOS) |
| 2386 | return std::nullopt; |
| 2387 | return DarwinPlatform::createFromArch( |
| 2388 | Arch: MachOArchName, OS: OSTy, Version: getInferredOSVersion(OS: OSTy, Triple, TheDriver)); |
| 2389 | } |
| 2390 | |
| 2391 | /// Returns the deployment target that's specified using the -target option. |
| 2392 | std::optional<DarwinPlatform> getDeploymentTargetFromTargetArg( |
| 2393 | DerivedArgList &Args, const llvm::Triple &Triple, const Driver &TheDriver, |
| 2394 | const std::optional<DarwinSDKInfo> &SDKInfo) { |
| 2395 | if (!Args.hasArg(Ids: options::OPT_target)) |
| 2396 | return std::nullopt; |
| 2397 | if (Triple.getOS() == llvm::Triple::Darwin || |
| 2398 | Triple.getOS() == llvm::Triple::UnknownOS) |
| 2399 | return std::nullopt; |
| 2400 | std::optional<llvm::Triple> TargetVariantTriple; |
| 2401 | for (const Arg *A : Args.filtered(Ids: options::OPT_darwin_target_variant)) { |
| 2402 | llvm::Triple TVT(A->getValue()); |
| 2403 | // Find a matching <arch>-<vendor> target variant triple that can be used. |
| 2404 | if ((Triple.getArch() == llvm::Triple::aarch64 || |
| 2405 | TVT.getArchName() == Triple.getArchName()) && |
| 2406 | TVT.getArch() == Triple.getArch() && |
| 2407 | TVT.getSubArch() == Triple.getSubArch() && |
| 2408 | TVT.getVendor() == Triple.getVendor()) { |
| 2409 | if (TargetVariantTriple) |
| 2410 | continue; |
| 2411 | A->claim(); |
| 2412 | // Accept a -target-variant triple when compiling code that may run on |
| 2413 | // macOS or Mac Catalyst. |
| 2414 | if ((Triple.isMacOSX() && TVT.getOS() == llvm::Triple::IOS && |
| 2415 | TVT.isMacCatalystEnvironment()) || |
| 2416 | (TVT.isMacOSX() && Triple.getOS() == llvm::Triple::IOS && |
| 2417 | Triple.isMacCatalystEnvironment())) { |
| 2418 | TargetVariantTriple = TVT; |
| 2419 | continue; |
| 2420 | } |
| 2421 | TheDriver.Diag(DiagID: diag::err_drv_target_variant_invalid) |
| 2422 | << A->getSpelling() << A->getValue(); |
| 2423 | } |
| 2424 | } |
| 2425 | DarwinPlatform PlatformAndVersion = DarwinPlatform::createFromTarget( |
| 2426 | TT: Triple, A: Args.getLastArg(Ids: options::OPT_target), TargetVariantTriple, |
| 2427 | SDKInfo); |
| 2428 | |
| 2429 | return PlatformAndVersion; |
| 2430 | } |
| 2431 | |
| 2432 | /// Returns the deployment target that's specified using the -mtargetos option. |
| 2433 | std::optional<DarwinPlatform> getDeploymentTargetFromMTargetOSArg( |
| 2434 | DerivedArgList &Args, const Driver &TheDriver, |
| 2435 | const std::optional<DarwinSDKInfo> &SDKInfo) { |
| 2436 | auto *A = Args.getLastArg(Ids: options::OPT_mtargetos_EQ); |
| 2437 | if (!A) |
| 2438 | return std::nullopt; |
| 2439 | llvm::Triple TT(llvm::Twine("unknown-apple-" ) + A->getValue()); |
| 2440 | switch (TT.getOS()) { |
| 2441 | case llvm::Triple::MacOSX: |
| 2442 | case llvm::Triple::IOS: |
| 2443 | case llvm::Triple::TvOS: |
| 2444 | case llvm::Triple::WatchOS: |
| 2445 | case llvm::Triple::XROS: |
| 2446 | break; |
| 2447 | default: |
| 2448 | TheDriver.Diag(DiagID: diag::err_drv_invalid_os_in_arg) |
| 2449 | << TT.getOSName() << A->getAsString(Args); |
| 2450 | return std::nullopt; |
| 2451 | } |
| 2452 | |
| 2453 | VersionTuple Version = TT.getOSVersion(); |
| 2454 | if (!Version.getMajor()) { |
| 2455 | TheDriver.Diag(DiagID: diag::err_drv_invalid_version_number) |
| 2456 | << A->getAsString(Args); |
| 2457 | return std::nullopt; |
| 2458 | } |
| 2459 | return DarwinPlatform::createFromMTargetOS(OS: TT.getOS(), OSVersion: Version, |
| 2460 | Environment: TT.getEnvironment(), A, SDKInfo); |
| 2461 | } |
| 2462 | |
| 2463 | std::optional<DarwinSDKInfo> parseSDKSettings(llvm::vfs::FileSystem &VFS, |
| 2464 | const ArgList &Args, |
| 2465 | const Driver &TheDriver) { |
| 2466 | const Arg *A = Args.getLastArg(Ids: options::OPT_isysroot); |
| 2467 | if (!A) |
| 2468 | return std::nullopt; |
| 2469 | StringRef isysroot = A->getValue(); |
| 2470 | auto SDKInfoOrErr = parseDarwinSDKInfo(VFS, SDKRootPath: isysroot); |
| 2471 | if (!SDKInfoOrErr) { |
| 2472 | llvm::consumeError(Err: SDKInfoOrErr.takeError()); |
| 2473 | TheDriver.Diag(DiagID: diag::warn_drv_darwin_sdk_invalid_settings); |
| 2474 | return std::nullopt; |
| 2475 | } |
| 2476 | return *SDKInfoOrErr; |
| 2477 | } |
| 2478 | |
| 2479 | } // namespace |
| 2480 | |
| 2481 | void Darwin::AddDeploymentTarget(DerivedArgList &Args) const { |
| 2482 | const OptTable &Opts = getDriver().getOpts(); |
| 2483 | |
| 2484 | // Support allowing the SDKROOT environment variable used by xcrun and other |
| 2485 | // Xcode tools to define the default sysroot, by making it the default for |
| 2486 | // isysroot. |
| 2487 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_isysroot)) { |
| 2488 | // Warn if the path does not exist. |
| 2489 | if (!getVFS().exists(Path: A->getValue())) |
| 2490 | getDriver().Diag(DiagID: clang::diag::warn_missing_sysroot) << A->getValue(); |
| 2491 | } else { |
| 2492 | if (char *env = ::getenv(name: "SDKROOT" )) { |
| 2493 | // We only use this value as the default if it is an absolute path, |
| 2494 | // exists, and it is not the root path. |
| 2495 | if (llvm::sys::path::is_absolute(path: env) && getVFS().exists(Path: env) && |
| 2496 | StringRef(env) != "/" ) { |
| 2497 | Args.append(A: Args.MakeSeparateArg( |
| 2498 | BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_isysroot), Value: env)); |
| 2499 | } |
| 2500 | } |
| 2501 | } |
| 2502 | |
| 2503 | // Read the SDKSettings.json file for more information, like the SDK version |
| 2504 | // that we can pass down to the compiler. |
| 2505 | SDKInfo = parseSDKSettings(VFS&: getVFS(), Args, TheDriver: getDriver()); |
| 2506 | // FIXME: If SDKInfo is std::nullopt, diagnose a bad isysroot value (e.g. |
| 2507 | // doesn't end in .sdk). |
| 2508 | |
| 2509 | // The OS and the version can be specified using the -target argument. |
| 2510 | std::optional<DarwinPlatform> PlatformAndVersion = |
| 2511 | getDeploymentTargetFromTargetArg(Args, Triple: getTriple(), TheDriver: getDriver(), SDKInfo); |
| 2512 | if (PlatformAndVersion) { |
| 2513 | // Disallow mixing -target and -mtargetos=. |
| 2514 | if (const auto *MTargetOSArg = Args.getLastArg(Ids: options::OPT_mtargetos_EQ)) { |
| 2515 | std::string TargetArgStr = PlatformAndVersion->getAsString(Args, Opts); |
| 2516 | std::string MTargetOSArgStr = MTargetOSArg->getAsString(Args); |
| 2517 | getDriver().Diag(DiagID: diag::err_drv_cannot_mix_options) |
| 2518 | << TargetArgStr << MTargetOSArgStr; |
| 2519 | } |
| 2520 | // Implicitly allow resolving the OS version when it wasn't explicitly set. |
| 2521 | bool TripleProvidedOSVersion = PlatformAndVersion->hasOSVersion(); |
| 2522 | if (!TripleProvidedOSVersion) |
| 2523 | PlatformAndVersion->setOSVersion( |
| 2524 | getInferredOSVersion(OS: getTriple().getOS(), Triple: getTriple(), TheDriver: getDriver())); |
| 2525 | |
| 2526 | std::optional<DarwinPlatform> PlatformAndVersionFromOSVersionArg = |
| 2527 | getDeploymentTargetFromOSVersionArg(Args, TheDriver: getDriver()); |
| 2528 | if (PlatformAndVersionFromOSVersionArg) { |
| 2529 | unsigned TargetMajor, TargetMinor, TargetMicro; |
| 2530 | bool ; |
| 2531 | unsigned ArgMajor, ArgMinor, ArgMicro; |
| 2532 | bool ; |
| 2533 | if (PlatformAndVersion->getPlatform() != |
| 2534 | PlatformAndVersionFromOSVersionArg->getPlatform() || |
| 2535 | (Driver::GetReleaseVersion( |
| 2536 | Str: PlatformAndVersion->getOSVersion().getAsString(), Major&: TargetMajor, |
| 2537 | Minor&: TargetMinor, Micro&: TargetMicro, HadExtra&: TargetExtra) && |
| 2538 | Driver::GetReleaseVersion( |
| 2539 | Str: PlatformAndVersionFromOSVersionArg->getOSVersion().getAsString(), |
| 2540 | Major&: ArgMajor, Minor&: ArgMinor, Micro&: ArgMicro, HadExtra&: ArgExtra) && |
| 2541 | (VersionTuple(TargetMajor, TargetMinor, TargetMicro) != |
| 2542 | VersionTuple(ArgMajor, ArgMinor, ArgMicro) || |
| 2543 | TargetExtra != ArgExtra))) { |
| 2544 | // Select the OS version from the -m<os>-version-min argument when |
| 2545 | // the -target does not include an OS version. |
| 2546 | if (PlatformAndVersion->getPlatform() == |
| 2547 | PlatformAndVersionFromOSVersionArg->getPlatform() && |
| 2548 | !TripleProvidedOSVersion) { |
| 2549 | PlatformAndVersion->setOSVersion( |
| 2550 | PlatformAndVersionFromOSVersionArg->getOSVersion()); |
| 2551 | } else { |
| 2552 | // Warn about -m<os>-version-min that doesn't match the OS version |
| 2553 | // that's specified in the target. |
| 2554 | std::string OSVersionArg = |
| 2555 | PlatformAndVersionFromOSVersionArg->getAsString(Args, Opts); |
| 2556 | std::string TargetArg = PlatformAndVersion->getAsString(Args, Opts); |
| 2557 | getDriver().Diag(DiagID: clang::diag::warn_drv_overriding_option) |
| 2558 | << OSVersionArg << TargetArg; |
| 2559 | } |
| 2560 | } |
| 2561 | } |
| 2562 | } else if ((PlatformAndVersion = getDeploymentTargetFromMTargetOSArg( |
| 2563 | Args, TheDriver: getDriver(), SDKInfo))) { |
| 2564 | // The OS target can be specified using the -mtargetos= argument. |
| 2565 | // Disallow mixing -mtargetos= and -m<os>version-min=. |
| 2566 | std::optional<DarwinPlatform> PlatformAndVersionFromOSVersionArg = |
| 2567 | getDeploymentTargetFromOSVersionArg(Args, TheDriver: getDriver()); |
| 2568 | if (PlatformAndVersionFromOSVersionArg) { |
| 2569 | std::string MTargetOSArgStr = PlatformAndVersion->getAsString(Args, Opts); |
| 2570 | std::string OSVersionArgStr = |
| 2571 | PlatformAndVersionFromOSVersionArg->getAsString(Args, Opts); |
| 2572 | getDriver().Diag(DiagID: diag::err_drv_cannot_mix_options) |
| 2573 | << MTargetOSArgStr << OSVersionArgStr; |
| 2574 | } |
| 2575 | } else { |
| 2576 | // The OS target can be specified using the -m<os>version-min argument. |
| 2577 | PlatformAndVersion = getDeploymentTargetFromOSVersionArg(Args, TheDriver: getDriver()); |
| 2578 | // If no deployment target was specified on the command line, check for |
| 2579 | // environment defines. |
| 2580 | if (!PlatformAndVersion) { |
| 2581 | PlatformAndVersion = |
| 2582 | getDeploymentTargetFromEnvironmentVariables(TheDriver: getDriver(), Triple: getTriple()); |
| 2583 | if (PlatformAndVersion) { |
| 2584 | // Don't infer simulator from the arch when the SDK is also specified. |
| 2585 | std::optional<DarwinPlatform> SDKTarget = |
| 2586 | inferDeploymentTargetFromSDK(Args, SDKInfo); |
| 2587 | if (SDKTarget) |
| 2588 | PlatformAndVersion->setEnvironment(SDKTarget->getEnvironment()); |
| 2589 | } |
| 2590 | } |
| 2591 | // If there is no command-line argument to specify the Target version and |
| 2592 | // no environment variable defined, see if we can set the default based |
| 2593 | // on -isysroot using SDKSettings.json if it exists. |
| 2594 | if (!PlatformAndVersion) { |
| 2595 | PlatformAndVersion = inferDeploymentTargetFromSDK(Args, SDKInfo); |
| 2596 | /// If the target was successfully constructed from the SDK path, try to |
| 2597 | /// infer the SDK info if the SDK doesn't have it. |
| 2598 | if (PlatformAndVersion && !SDKInfo) |
| 2599 | SDKInfo = PlatformAndVersion->inferSDKInfo(); |
| 2600 | } |
| 2601 | // If no OS targets have been specified, try to guess platform from -target |
| 2602 | // or arch name and compute the version from the triple. |
| 2603 | if (!PlatformAndVersion) |
| 2604 | PlatformAndVersion = |
| 2605 | inferDeploymentTargetFromArch(Args, Toolchain: *this, Triple: getTriple(), TheDriver: getDriver()); |
| 2606 | } |
| 2607 | |
| 2608 | assert(PlatformAndVersion && "Unable to infer Darwin variant" ); |
| 2609 | if (!PlatformAndVersion->isValidOSVersion()) { |
| 2610 | if (PlatformAndVersion->isExplicitlySpecified()) |
| 2611 | getDriver().Diag(DiagID: diag::err_drv_invalid_version_number) |
| 2612 | << PlatformAndVersion->getAsString(Args, Opts); |
| 2613 | else |
| 2614 | getDriver().Diag(DiagID: diag::err_drv_invalid_version_number_inferred) |
| 2615 | << PlatformAndVersion->getOSVersion().getAsString() |
| 2616 | << PlatformAndVersion->getInferredSource(); |
| 2617 | } |
| 2618 | // After the deployment OS version has been resolved, set it to the canonical |
| 2619 | // version before further error detection and converting to a proper target |
| 2620 | // triple. |
| 2621 | VersionTuple CanonicalVersion = PlatformAndVersion->getCanonicalOSVersion(); |
| 2622 | if (CanonicalVersion != PlatformAndVersion->getOSVersion()) { |
| 2623 | getDriver().Diag(DiagID: diag::warn_drv_overriding_deployment_version) |
| 2624 | << PlatformAndVersion->getOSVersion().getAsString() |
| 2625 | << CanonicalVersion.getAsString(); |
| 2626 | PlatformAndVersion->setOSVersion(CanonicalVersion); |
| 2627 | } |
| 2628 | |
| 2629 | PlatformAndVersion->addOSVersionMinArgument(Args, Opts); |
| 2630 | DarwinPlatformKind Platform = PlatformAndVersion->getPlatform(); |
| 2631 | |
| 2632 | unsigned Major, Minor, Micro; |
| 2633 | bool ; |
| 2634 | // The major version should not be over this number. |
| 2635 | const unsigned MajorVersionLimit = 1000; |
| 2636 | const VersionTuple OSVersion = PlatformAndVersion->takeOSVersion(); |
| 2637 | const std::string OSVersionStr = OSVersion.getAsString(); |
| 2638 | // Set the tool chain target information. |
| 2639 | if (Platform == MacOS) { |
| 2640 | if (!Driver::GetReleaseVersion(Str: OSVersionStr, Major, Minor, Micro, |
| 2641 | HadExtra) || |
| 2642 | HadExtra || Major < 10 || Major >= MajorVersionLimit || Minor >= 100 || |
| 2643 | Micro >= 100) |
| 2644 | getDriver().Diag(DiagID: diag::err_drv_invalid_version_number) |
| 2645 | << PlatformAndVersion->getAsString(Args, Opts); |
| 2646 | } else if (Platform == IPhoneOS) { |
| 2647 | if (!Driver::GetReleaseVersion(Str: OSVersionStr, Major, Minor, Micro, |
| 2648 | HadExtra) || |
| 2649 | HadExtra || Major >= MajorVersionLimit || Minor >= 100 || Micro >= 100) |
| 2650 | getDriver().Diag(DiagID: diag::err_drv_invalid_version_number) |
| 2651 | << PlatformAndVersion->getAsString(Args, Opts); |
| 2652 | ; |
| 2653 | if (PlatformAndVersion->getEnvironment() == MacCatalyst && |
| 2654 | (Major < 13 || (Major == 13 && Minor < 1))) { |
| 2655 | getDriver().Diag(DiagID: diag::err_drv_invalid_version_number) |
| 2656 | << PlatformAndVersion->getAsString(Args, Opts); |
| 2657 | Major = 13; |
| 2658 | Minor = 1; |
| 2659 | Micro = 0; |
| 2660 | } |
| 2661 | // For 32-bit targets, the deployment target for iOS has to be earlier than |
| 2662 | // iOS 11. |
| 2663 | if (getTriple().isArch32Bit() && Major >= 11) { |
| 2664 | // If the deployment target is explicitly specified, print a diagnostic. |
| 2665 | if (PlatformAndVersion->isExplicitlySpecified()) { |
| 2666 | if (PlatformAndVersion->getEnvironment() == MacCatalyst) |
| 2667 | getDriver().Diag(DiagID: diag::err_invalid_macos_32bit_deployment_target); |
| 2668 | else |
| 2669 | getDriver().Diag(DiagID: diag::warn_invalid_ios_deployment_target) |
| 2670 | << PlatformAndVersion->getAsString(Args, Opts); |
| 2671 | // Otherwise, set it to 10.99.99. |
| 2672 | } else { |
| 2673 | Major = 10; |
| 2674 | Minor = 99; |
| 2675 | Micro = 99; |
| 2676 | } |
| 2677 | } |
| 2678 | } else if (Platform == TvOS) { |
| 2679 | if (!Driver::GetReleaseVersion(Str: OSVersionStr, Major, Minor, Micro, |
| 2680 | HadExtra) || |
| 2681 | HadExtra || Major >= MajorVersionLimit || Minor >= 100 || Micro >= 100) |
| 2682 | getDriver().Diag(DiagID: diag::err_drv_invalid_version_number) |
| 2683 | << PlatformAndVersion->getAsString(Args, Opts); |
| 2684 | } else if (Platform == WatchOS) { |
| 2685 | if (!Driver::GetReleaseVersion(Str: OSVersionStr, Major, Minor, Micro, |
| 2686 | HadExtra) || |
| 2687 | HadExtra || Major >= MajorVersionLimit || Minor >= 100 || Micro >= 100) |
| 2688 | getDriver().Diag(DiagID: diag::err_drv_invalid_version_number) |
| 2689 | << PlatformAndVersion->getAsString(Args, Opts); |
| 2690 | } else if (Platform == DriverKit) { |
| 2691 | if (!Driver::GetReleaseVersion(Str: OSVersionStr, Major, Minor, Micro, |
| 2692 | HadExtra) || |
| 2693 | HadExtra || Major < 19 || Major >= MajorVersionLimit || Minor >= 100 || |
| 2694 | Micro >= 100) |
| 2695 | getDriver().Diag(DiagID: diag::err_drv_invalid_version_number) |
| 2696 | << PlatformAndVersion->getAsString(Args, Opts); |
| 2697 | } else { |
| 2698 | if (!Driver::GetReleaseVersion(Str: OSVersionStr, Major, Minor, Micro, |
| 2699 | HadExtra) || |
| 2700 | HadExtra || Major < 1 || Major >= MajorVersionLimit || Minor >= 100 || |
| 2701 | Micro >= 100) |
| 2702 | getDriver().Diag(DiagID: diag::err_drv_invalid_version_number) |
| 2703 | << PlatformAndVersion->getAsString(Args, Opts); |
| 2704 | } |
| 2705 | |
| 2706 | DarwinEnvironmentKind Environment = PlatformAndVersion->getEnvironment(); |
| 2707 | // Recognize iOS targets with an x86 architecture as the iOS simulator. |
| 2708 | if (Environment == NativeEnvironment && Platform != MacOS && |
| 2709 | Platform != DriverKit && |
| 2710 | PlatformAndVersion->canInferSimulatorFromArch() && getTriple().isX86()) |
| 2711 | Environment = Simulator; |
| 2712 | |
| 2713 | VersionTuple ZipperedOSVersion; |
| 2714 | if (Environment == MacCatalyst) |
| 2715 | ZipperedOSVersion = PlatformAndVersion->getZipperedOSVersion(); |
| 2716 | setTarget(Platform, Environment, Major, Minor, Micro, NativeTargetVersion: ZipperedOSVersion); |
| 2717 | TargetVariantTriple = PlatformAndVersion->getTargetVariantTriple(); |
| 2718 | if (TargetVariantTriple && |
| 2719 | !llvm::Triple::isValidVersionForOS(OSKind: TargetVariantTriple->getOS(), |
| 2720 | Version: TargetVariantTriple->getOSVersion())) { |
| 2721 | getDriver().Diag(DiagID: diag::err_drv_invalid_version_number) |
| 2722 | << TargetVariantTriple->str(); |
| 2723 | } |
| 2724 | } |
| 2725 | |
| 2726 | bool DarwinClang::HasPlatformPrefix(const llvm::Triple &T) const { |
| 2727 | if (SDKInfo) |
| 2728 | return !SDKInfo->getPlatformPrefix(Triple: T).empty(); |
| 2729 | else |
| 2730 | return Darwin::HasPlatformPrefix(T); |
| 2731 | } |
| 2732 | |
| 2733 | // For certain platforms/environments almost all resources (e.g., headers) are |
| 2734 | // located in sub-directories, e.g., for DriverKit they live in |
| 2735 | // <SYSROOT>/System/DriverKit/usr/include (instead of <SYSROOT>/usr/include). |
| 2736 | void DarwinClang::AppendPlatformPrefix(SmallString<128> &Path, |
| 2737 | const llvm::Triple &T) const { |
| 2738 | if (SDKInfo) { |
| 2739 | const StringRef PlatformPrefix = SDKInfo->getPlatformPrefix(Triple: T); |
| 2740 | if (!PlatformPrefix.empty()) |
| 2741 | llvm::sys::path::append(path&: Path, a: PlatformPrefix); |
| 2742 | } else if (T.isDriverKit()) { |
| 2743 | // The first version of DriverKit didn't have SDKSettings.json, manually add |
| 2744 | // its prefix. |
| 2745 | llvm::sys::path::append(path&: Path, a: "System" , b: "DriverKit" ); |
| 2746 | } |
| 2747 | } |
| 2748 | |
| 2749 | // Returns the effective sysroot from either -isysroot or --sysroot, plus the |
| 2750 | // platform prefix (if any). |
| 2751 | llvm::SmallString<128> |
| 2752 | AppleMachO::GetEffectiveSysroot(const llvm::opt::ArgList &DriverArgs) const { |
| 2753 | llvm::SmallString<128> Path("/" ); |
| 2754 | if (DriverArgs.hasArg(Ids: options::OPT_isysroot)) |
| 2755 | Path = DriverArgs.getLastArgValue(Id: options::OPT_isysroot); |
| 2756 | else if (!getDriver().SysRoot.empty()) |
| 2757 | Path = getDriver().SysRoot; |
| 2758 | |
| 2759 | if (hasEffectiveTriple()) { |
| 2760 | AppendPlatformPrefix(Path, T: getEffectiveTriple()); |
| 2761 | } |
| 2762 | return Path; |
| 2763 | } |
| 2764 | |
| 2765 | void AppleMachO::AddClangSystemIncludeArgs( |
| 2766 | const llvm::opt::ArgList &DriverArgs, |
| 2767 | llvm::opt::ArgStringList &CC1Args) const { |
| 2768 | const Driver &D = getDriver(); |
| 2769 | |
| 2770 | llvm::SmallString<128> Sysroot = GetEffectiveSysroot(DriverArgs); |
| 2771 | |
| 2772 | bool NoStdInc = DriverArgs.hasArg(Ids: options::OPT_nostdinc); |
| 2773 | bool NoStdlibInc = DriverArgs.hasArg(Ids: options::OPT_nostdlibinc); |
| 2774 | bool NoBuiltinInc = DriverArgs.hasFlag( |
| 2775 | Pos: options::OPT_nobuiltininc, Neg: options::OPT_ibuiltininc, /*Default=*/false); |
| 2776 | bool ForceBuiltinInc = DriverArgs.hasFlag( |
| 2777 | Pos: options::OPT_ibuiltininc, Neg: options::OPT_nobuiltininc, /*Default=*/false); |
| 2778 | |
| 2779 | // Add <sysroot>/usr/local/include |
| 2780 | if (!NoStdInc && !NoStdlibInc) { |
| 2781 | SmallString<128> P(Sysroot); |
| 2782 | llvm::sys::path::append(path&: P, a: "usr" , b: "local" , c: "include" ); |
| 2783 | addSystemInclude(DriverArgs, CC1Args, Path: P); |
| 2784 | } |
| 2785 | |
| 2786 | // Add the Clang builtin headers (<resource>/include) |
| 2787 | if (!(NoStdInc && !ForceBuiltinInc) && !NoBuiltinInc) { |
| 2788 | SmallString<128> P(D.ResourceDir); |
| 2789 | llvm::sys::path::append(path&: P, a: "include" ); |
| 2790 | addSystemInclude(DriverArgs, CC1Args, Path: P); |
| 2791 | } |
| 2792 | |
| 2793 | if (NoStdInc || NoStdlibInc) |
| 2794 | return; |
| 2795 | |
| 2796 | // Check for configure-time C include directories. |
| 2797 | llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS); |
| 2798 | if (!CIncludeDirs.empty()) { |
| 2799 | llvm::SmallVector<llvm::StringRef, 5> dirs; |
| 2800 | CIncludeDirs.split(A&: dirs, Separator: ":" ); |
| 2801 | for (llvm::StringRef dir : dirs) { |
| 2802 | llvm::StringRef Prefix = |
| 2803 | llvm::sys::path::is_absolute(path: dir) ? "" : llvm::StringRef(Sysroot); |
| 2804 | addExternCSystemInclude(DriverArgs, CC1Args, Path: Prefix + dir); |
| 2805 | } |
| 2806 | } else { |
| 2807 | // Otherwise, add <sysroot>/usr/include. |
| 2808 | SmallString<128> P(Sysroot); |
| 2809 | llvm::sys::path::append(path&: P, a: "usr" , b: "include" ); |
| 2810 | addExternCSystemInclude(DriverArgs, CC1Args, Path: P.str()); |
| 2811 | } |
| 2812 | } |
| 2813 | |
| 2814 | void DarwinClang::AddClangSystemIncludeArgs( |
| 2815 | const llvm::opt::ArgList &DriverArgs, |
| 2816 | llvm::opt::ArgStringList &CC1Args) const { |
| 2817 | AppleMachO::AddClangSystemIncludeArgs(DriverArgs, CC1Args); |
| 2818 | |
| 2819 | if (DriverArgs.hasArg(Ids: options::OPT_nostdinc, Ids: options::OPT_nostdlibinc)) |
| 2820 | return; |
| 2821 | |
| 2822 | llvm::SmallString<128> Sysroot = GetEffectiveSysroot(DriverArgs); |
| 2823 | |
| 2824 | // Add <sysroot>/System/Library/Frameworks |
| 2825 | // Add <sysroot>/System/Library/SubFrameworks |
| 2826 | // Add <sysroot>/Library/Frameworks |
| 2827 | SmallString<128> P1(Sysroot), P2(Sysroot), P3(Sysroot); |
| 2828 | llvm::sys::path::append(path&: P1, a: "System" , b: "Library" , c: "Frameworks" ); |
| 2829 | llvm::sys::path::append(path&: P2, a: "System" , b: "Library" , c: "SubFrameworks" ); |
| 2830 | llvm::sys::path::append(path&: P3, a: "Library" , b: "Frameworks" ); |
| 2831 | addSystemFrameworkIncludes(DriverArgs, CC1Args, Paths: {P1, P2, P3}); |
| 2832 | } |
| 2833 | |
| 2834 | bool DarwinClang::AddGnuCPlusPlusIncludePaths(const llvm::opt::ArgList &DriverArgs, |
| 2835 | llvm::opt::ArgStringList &CC1Args, |
| 2836 | llvm::SmallString<128> Base, |
| 2837 | llvm::StringRef Version, |
| 2838 | llvm::StringRef ArchDir, |
| 2839 | llvm::StringRef BitDir) const { |
| 2840 | llvm::sys::path::append(path&: Base, a: Version); |
| 2841 | |
| 2842 | // Add the base dir |
| 2843 | addSystemInclude(DriverArgs, CC1Args, Path: Base); |
| 2844 | |
| 2845 | // Add the multilib dirs |
| 2846 | { |
| 2847 | llvm::SmallString<128> P = Base; |
| 2848 | if (!ArchDir.empty()) |
| 2849 | llvm::sys::path::append(path&: P, a: ArchDir); |
| 2850 | if (!BitDir.empty()) |
| 2851 | llvm::sys::path::append(path&: P, a: BitDir); |
| 2852 | addSystemInclude(DriverArgs, CC1Args, Path: P); |
| 2853 | } |
| 2854 | |
| 2855 | // Add the backward dir |
| 2856 | { |
| 2857 | llvm::SmallString<128> P = Base; |
| 2858 | llvm::sys::path::append(path&: P, a: "backward" ); |
| 2859 | addSystemInclude(DriverArgs, CC1Args, Path: P); |
| 2860 | } |
| 2861 | |
| 2862 | return getVFS().exists(Path: Base); |
| 2863 | } |
| 2864 | |
| 2865 | void AppleMachO::AddClangCXXStdlibIncludeArgs( |
| 2866 | const llvm::opt::ArgList &DriverArgs, |
| 2867 | llvm::opt::ArgStringList &CC1Args) const { |
| 2868 | // The implementation from a base class will pass through the -stdlib to |
| 2869 | // CC1Args. |
| 2870 | // FIXME: this should not be necessary, remove usages in the frontend |
| 2871 | // (e.g. HeaderSearchOptions::UseLibcxx) and don't pipe -stdlib. |
| 2872 | // Also check whether this is used for setting library search paths. |
| 2873 | ToolChain::AddClangCXXStdlibIncludeArgs(DriverArgs, CC1Args); |
| 2874 | |
| 2875 | if (DriverArgs.hasArg(Ids: options::OPT_nostdinc, Ids: options::OPT_nostdlibinc, |
| 2876 | Ids: options::OPT_nostdincxx)) |
| 2877 | return; |
| 2878 | |
| 2879 | llvm::SmallString<128> Sysroot = GetEffectiveSysroot(DriverArgs); |
| 2880 | |
| 2881 | switch (GetCXXStdlibType(Args: DriverArgs)) { |
| 2882 | case ToolChain::CST_Libcxx: { |
| 2883 | // On Darwin, libc++ can be installed in one of the following places: |
| 2884 | // 1. Alongside the compiler in <clang-executable-folder>/../include/c++/v1 |
| 2885 | // 2. In a SDK (or a custom sysroot) in <sysroot>/usr/include/c++/v1 |
| 2886 | // |
| 2887 | // The precedence of paths is as listed above, i.e. we take the first path |
| 2888 | // that exists. Note that we never include libc++ twice -- we take the first |
| 2889 | // path that exists and don't send the other paths to CC1 (otherwise |
| 2890 | // include_next could break). |
| 2891 | |
| 2892 | // Check for (1) |
| 2893 | // Get from '<install>/bin' to '<install>/include/c++/v1'. |
| 2894 | // Note that InstallBin can be relative, so we use '..' instead of |
| 2895 | // parent_path. |
| 2896 | llvm::SmallString<128> InstallBin(getDriver().Dir); // <install>/bin |
| 2897 | llvm::sys::path::append(path&: InstallBin, a: ".." , b: "include" , c: "c++" , d: "v1" ); |
| 2898 | if (getVFS().exists(Path: InstallBin)) { |
| 2899 | addSystemInclude(DriverArgs, CC1Args, Path: InstallBin); |
| 2900 | return; |
| 2901 | } else if (DriverArgs.hasArg(Ids: options::OPT_v)) { |
| 2902 | llvm::errs() << "ignoring nonexistent directory \"" << InstallBin |
| 2903 | << "\"\n" ; |
| 2904 | } |
| 2905 | |
| 2906 | // Otherwise, check for (2) |
| 2907 | llvm::SmallString<128> SysrootUsr = Sysroot; |
| 2908 | llvm::sys::path::append(path&: SysrootUsr, a: "usr" , b: "include" , c: "c++" , d: "v1" ); |
| 2909 | if (getVFS().exists(Path: SysrootUsr)) { |
| 2910 | addSystemInclude(DriverArgs, CC1Args, Path: SysrootUsr); |
| 2911 | return; |
| 2912 | } else if (DriverArgs.hasArg(Ids: options::OPT_v)) { |
| 2913 | llvm::errs() << "ignoring nonexistent directory \"" << SysrootUsr |
| 2914 | << "\"\n" ; |
| 2915 | } |
| 2916 | |
| 2917 | // Otherwise, don't add any path. |
| 2918 | break; |
| 2919 | } |
| 2920 | |
| 2921 | case ToolChain::CST_Libstdcxx: |
| 2922 | AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args); |
| 2923 | break; |
| 2924 | } |
| 2925 | } |
| 2926 | |
| 2927 | void AppleMachO::AddGnuCPlusPlusIncludePaths( |
| 2928 | const llvm::opt::ArgList &DriverArgs, |
| 2929 | llvm::opt::ArgStringList &CC1Args) const {} |
| 2930 | |
| 2931 | void DarwinClang::AddGnuCPlusPlusIncludePaths( |
| 2932 | const llvm::opt::ArgList &DriverArgs, |
| 2933 | llvm::opt::ArgStringList &CC1Args) const { |
| 2934 | llvm::SmallString<128> UsrIncludeCxx = GetEffectiveSysroot(DriverArgs); |
| 2935 | llvm::sys::path::append(path&: UsrIncludeCxx, a: "usr" , b: "include" , c: "c++" ); |
| 2936 | |
| 2937 | llvm::Triple::ArchType arch = getTriple().getArch(); |
| 2938 | bool IsBaseFound = true; |
| 2939 | switch (arch) { |
| 2940 | default: |
| 2941 | break; |
| 2942 | |
| 2943 | case llvm::Triple::x86: |
| 2944 | case llvm::Triple::x86_64: |
| 2945 | IsBaseFound = AddGnuCPlusPlusIncludePaths( |
| 2946 | DriverArgs, CC1Args, Base: UsrIncludeCxx, Version: "4.2.1" , ArchDir: "i686-apple-darwin10" , |
| 2947 | BitDir: arch == llvm::Triple::x86_64 ? "x86_64" : "" ); |
| 2948 | IsBaseFound |= AddGnuCPlusPlusIncludePaths( |
| 2949 | DriverArgs, CC1Args, Base: UsrIncludeCxx, Version: "4.0.0" , ArchDir: "i686-apple-darwin8" , BitDir: "" ); |
| 2950 | break; |
| 2951 | |
| 2952 | case llvm::Triple::arm: |
| 2953 | case llvm::Triple::thumb: |
| 2954 | IsBaseFound = |
| 2955 | AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, Base: UsrIncludeCxx, Version: "4.2.1" , |
| 2956 | ArchDir: "arm-apple-darwin10" , BitDir: "v7" ); |
| 2957 | IsBaseFound |= |
| 2958 | AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, Base: UsrIncludeCxx, Version: "4.2.1" , |
| 2959 | ArchDir: "arm-apple-darwin10" , BitDir: "v6" ); |
| 2960 | break; |
| 2961 | |
| 2962 | case llvm::Triple::aarch64: |
| 2963 | IsBaseFound = |
| 2964 | AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, Base: UsrIncludeCxx, Version: "4.2.1" , |
| 2965 | ArchDir: "arm64-apple-darwin10" , BitDir: "" ); |
| 2966 | break; |
| 2967 | } |
| 2968 | |
| 2969 | if (!IsBaseFound) { |
| 2970 | getDriver().Diag(DiagID: diag::warn_drv_libstdcxx_not_found); |
| 2971 | } |
| 2972 | } |
| 2973 | |
| 2974 | void AppleMachO::AddCXXStdlibLibArgs(const ArgList &Args, |
| 2975 | ArgStringList &CmdArgs) const { |
| 2976 | CXXStdlibType Type = GetCXXStdlibType(Args); |
| 2977 | |
| 2978 | switch (Type) { |
| 2979 | case ToolChain::CST_Libcxx: |
| 2980 | CmdArgs.push_back(Elt: "-lc++" ); |
| 2981 | if (Args.hasArg(Ids: options::OPT_fexperimental_library)) |
| 2982 | CmdArgs.push_back(Elt: "-lc++experimental" ); |
| 2983 | break; |
| 2984 | |
| 2985 | case ToolChain::CST_Libstdcxx: |
| 2986 | // Unfortunately, -lstdc++ doesn't always exist in the standard search path; |
| 2987 | // it was previously found in the gcc lib dir. However, for all the Darwin |
| 2988 | // platforms we care about it was -lstdc++.6, so we search for that |
| 2989 | // explicitly if we can't see an obvious -lstdc++ candidate. |
| 2990 | |
| 2991 | // Check in the sysroot first. |
| 2992 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_isysroot)) { |
| 2993 | SmallString<128> P(A->getValue()); |
| 2994 | llvm::sys::path::append(path&: P, a: "usr" , b: "lib" , c: "libstdc++.dylib" ); |
| 2995 | |
| 2996 | if (!getVFS().exists(Path: P)) { |
| 2997 | llvm::sys::path::remove_filename(path&: P); |
| 2998 | llvm::sys::path::append(path&: P, a: "libstdc++.6.dylib" ); |
| 2999 | if (getVFS().exists(Path: P)) { |
| 3000 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: P)); |
| 3001 | return; |
| 3002 | } |
| 3003 | } |
| 3004 | } |
| 3005 | |
| 3006 | // Otherwise, look in the root. |
| 3007 | // FIXME: This should be removed someday when we don't have to care about |
| 3008 | // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist. |
| 3009 | if (!getVFS().exists(Path: "/usr/lib/libstdc++.dylib" ) && |
| 3010 | getVFS().exists(Path: "/usr/lib/libstdc++.6.dylib" )) { |
| 3011 | CmdArgs.push_back(Elt: "/usr/lib/libstdc++.6.dylib" ); |
| 3012 | return; |
| 3013 | } |
| 3014 | |
| 3015 | // Otherwise, let the linker search. |
| 3016 | CmdArgs.push_back(Elt: "-lstdc++" ); |
| 3017 | break; |
| 3018 | } |
| 3019 | } |
| 3020 | |
| 3021 | void DarwinClang::AddCCKextLibArgs(const ArgList &Args, |
| 3022 | ArgStringList &CmdArgs) const { |
| 3023 | // For Darwin platforms, use the compiler-rt-based support library |
| 3024 | // instead of the gcc-provided one (which is also incidentally |
| 3025 | // only present in the gcc lib dir, which makes it hard to find). |
| 3026 | |
| 3027 | SmallString<128> P(getDriver().ResourceDir); |
| 3028 | llvm::sys::path::append(path&: P, a: "lib" , b: "darwin" ); |
| 3029 | |
| 3030 | // Use the newer cc_kext for iOS ARM after 6.0. |
| 3031 | if (isTargetWatchOS()) { |
| 3032 | llvm::sys::path::append(path&: P, a: "libclang_rt.cc_kext_watchos.a" ); |
| 3033 | } else if (isTargetTvOS()) { |
| 3034 | llvm::sys::path::append(path&: P, a: "libclang_rt.cc_kext_tvos.a" ); |
| 3035 | } else if (isTargetIPhoneOS()) { |
| 3036 | llvm::sys::path::append(path&: P, a: "libclang_rt.cc_kext_ios.a" ); |
| 3037 | } else if (isTargetDriverKit()) { |
| 3038 | // DriverKit doesn't want extra runtime support. |
| 3039 | } else if (isTargetXROSDevice()) { |
| 3040 | llvm::sys::path::append( |
| 3041 | path&: P, a: llvm::Twine("libclang_rt.cc_kext_" ) + |
| 3042 | llvm::Triple::getOSTypeName(Kind: llvm::Triple::XROS) + ".a" ); |
| 3043 | } else { |
| 3044 | llvm::sys::path::append(path&: P, a: "libclang_rt.cc_kext.a" ); |
| 3045 | } |
| 3046 | |
| 3047 | // For now, allow missing resource libraries to support developers who may |
| 3048 | // not have compiler-rt checked out or integrated into their build. |
| 3049 | if (getVFS().exists(Path: P)) |
| 3050 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: P)); |
| 3051 | } |
| 3052 | |
| 3053 | DerivedArgList *MachO::TranslateArgs(const DerivedArgList &Args, |
| 3054 | StringRef BoundArch, |
| 3055 | Action::OffloadKind) const { |
| 3056 | DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs()); |
| 3057 | const OptTable &Opts = getDriver().getOpts(); |
| 3058 | |
| 3059 | // FIXME: We really want to get out of the tool chain level argument |
| 3060 | // translation business, as it makes the driver functionality much |
| 3061 | // more opaque. For now, we follow gcc closely solely for the |
| 3062 | // purpose of easily achieving feature parity & testability. Once we |
| 3063 | // have something that works, we should reevaluate each translation |
| 3064 | // and try to push it down into tool specific logic. |
| 3065 | |
| 3066 | for (Arg *A : Args) { |
| 3067 | // Sob. These is strictly gcc compatible for the time being. Apple |
| 3068 | // gcc translates options twice, which means that self-expanding |
| 3069 | // options add duplicates. |
| 3070 | switch ((options::ID)A->getOption().getID()) { |
| 3071 | default: |
| 3072 | DAL->append(A); |
| 3073 | break; |
| 3074 | |
| 3075 | case options::OPT_mkernel: |
| 3076 | case options::OPT_fapple_kext: |
| 3077 | DAL->append(A); |
| 3078 | DAL->AddFlagArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_static)); |
| 3079 | break; |
| 3080 | |
| 3081 | case options::OPT_dependency_file: |
| 3082 | DAL->AddSeparateArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_MF), Value: A->getValue()); |
| 3083 | break; |
| 3084 | |
| 3085 | case options::OPT_gfull: |
| 3086 | DAL->AddFlagArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_g_Flag)); |
| 3087 | DAL->AddFlagArg( |
| 3088 | BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_fno_eliminate_unused_debug_symbols)); |
| 3089 | break; |
| 3090 | |
| 3091 | case options::OPT_gused: |
| 3092 | DAL->AddFlagArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_g_Flag)); |
| 3093 | DAL->AddFlagArg( |
| 3094 | BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_feliminate_unused_debug_symbols)); |
| 3095 | break; |
| 3096 | |
| 3097 | case options::OPT_shared: |
| 3098 | DAL->AddFlagArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_dynamiclib)); |
| 3099 | break; |
| 3100 | |
| 3101 | case options::OPT_fconstant_cfstrings: |
| 3102 | DAL->AddFlagArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_mconstant_cfstrings)); |
| 3103 | break; |
| 3104 | |
| 3105 | case options::OPT_fno_constant_cfstrings: |
| 3106 | DAL->AddFlagArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_mno_constant_cfstrings)); |
| 3107 | break; |
| 3108 | |
| 3109 | case options::OPT_Wnonportable_cfstrings: |
| 3110 | DAL->AddFlagArg(BaseArg: A, |
| 3111 | Opt: Opts.getOption(Opt: options::OPT_mwarn_nonportable_cfstrings)); |
| 3112 | break; |
| 3113 | |
| 3114 | case options::OPT_Wno_nonportable_cfstrings: |
| 3115 | DAL->AddFlagArg( |
| 3116 | BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_mno_warn_nonportable_cfstrings)); |
| 3117 | break; |
| 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | // Add the arch options based on the particular spelling of -arch, to match |
| 3122 | // how the driver works. |
| 3123 | if (!BoundArch.empty()) { |
| 3124 | StringRef Name = BoundArch; |
| 3125 | const Option MCpu = Opts.getOption(Opt: options::OPT_mcpu_EQ); |
| 3126 | const Option MArch = Opts.getOption(Opt: options::OPT_march_EQ); |
| 3127 | |
| 3128 | // This code must be kept in sync with LLVM's getArchTypeForDarwinArch, |
| 3129 | // which defines the list of which architectures we accept. |
| 3130 | if (Name == "ppc" ) |
| 3131 | ; |
| 3132 | else if (Name == "ppc601" ) |
| 3133 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MCpu, Value: "601" ); |
| 3134 | else if (Name == "ppc603" ) |
| 3135 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MCpu, Value: "603" ); |
| 3136 | else if (Name == "ppc604" ) |
| 3137 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MCpu, Value: "604" ); |
| 3138 | else if (Name == "ppc604e" ) |
| 3139 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MCpu, Value: "604e" ); |
| 3140 | else if (Name == "ppc750" ) |
| 3141 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MCpu, Value: "750" ); |
| 3142 | else if (Name == "ppc7400" ) |
| 3143 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MCpu, Value: "7400" ); |
| 3144 | else if (Name == "ppc7450" ) |
| 3145 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MCpu, Value: "7450" ); |
| 3146 | else if (Name == "ppc970" ) |
| 3147 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MCpu, Value: "970" ); |
| 3148 | |
| 3149 | else if (Name == "ppc64" || Name == "ppc64le" ) |
| 3150 | DAL->AddFlagArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_m64)); |
| 3151 | |
| 3152 | else if (Name == "i386" ) |
| 3153 | ; |
| 3154 | else if (Name == "i486" ) |
| 3155 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "i486" ); |
| 3156 | else if (Name == "i586" ) |
| 3157 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "i586" ); |
| 3158 | else if (Name == "i686" ) |
| 3159 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "i686" ); |
| 3160 | else if (Name == "pentium" ) |
| 3161 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "pentium" ); |
| 3162 | else if (Name == "pentium2" ) |
| 3163 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "pentium2" ); |
| 3164 | else if (Name == "pentpro" ) |
| 3165 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "pentiumpro" ); |
| 3166 | else if (Name == "pentIIm3" ) |
| 3167 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "pentium2" ); |
| 3168 | |
| 3169 | else if (Name == "x86_64" || Name == "x86_64h" ) |
| 3170 | DAL->AddFlagArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_m64)); |
| 3171 | |
| 3172 | else if (Name == "arm" ) |
| 3173 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "armv4t" ); |
| 3174 | else if (Name == "armv4t" ) |
| 3175 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "armv4t" ); |
| 3176 | else if (Name == "armv5" ) |
| 3177 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "armv5tej" ); |
| 3178 | else if (Name == "xscale" ) |
| 3179 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "xscale" ); |
| 3180 | else if (Name == "armv6" ) |
| 3181 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "armv6k" ); |
| 3182 | else if (Name == "armv6m" ) |
| 3183 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "armv6m" ); |
| 3184 | else if (Name == "armv7" ) |
| 3185 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "armv7a" ); |
| 3186 | else if (Name == "armv7em" ) |
| 3187 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "armv7em" ); |
| 3188 | else if (Name == "armv7k" ) |
| 3189 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "armv7k" ); |
| 3190 | else if (Name == "armv7m" ) |
| 3191 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "armv7m" ); |
| 3192 | else if (Name == "armv7s" ) |
| 3193 | DAL->AddJoinedArg(BaseArg: nullptr, Opt: MArch, Value: "armv7s" ); |
| 3194 | } |
| 3195 | |
| 3196 | return DAL; |
| 3197 | } |
| 3198 | |
| 3199 | void MachO::AddLinkRuntimeLibArgs(const ArgList &Args, |
| 3200 | ArgStringList &CmdArgs, |
| 3201 | bool ForceLinkBuiltinRT) const { |
| 3202 | // Embedded targets are simple at the moment, not supporting sanitizers and |
| 3203 | // with different libraries for each member of the product { static, PIC } x |
| 3204 | // { hard-float, soft-float } |
| 3205 | llvm::SmallString<32> CompilerRT = StringRef("" ); |
| 3206 | CompilerRT += |
| 3207 | (tools::arm::getARMFloatABI(TC: *this, Args) == tools::arm::FloatABI::Hard) |
| 3208 | ? "hard" |
| 3209 | : "soft" ; |
| 3210 | CompilerRT += Args.hasArg(Ids: options::OPT_fPIC) ? "_pic" : "_static" ; |
| 3211 | |
| 3212 | AddLinkRuntimeLib(Args, CmdArgs, Component: CompilerRT, Opts: RLO_IsEmbedded); |
| 3213 | } |
| 3214 | |
| 3215 | bool Darwin::isAlignedAllocationUnavailable() const { |
| 3216 | llvm::Triple::OSType OS; |
| 3217 | |
| 3218 | if (isTargetMacCatalyst()) |
| 3219 | return TargetVersion < alignedAllocMinVersion(OS: llvm::Triple::MacOSX); |
| 3220 | switch (TargetPlatform) { |
| 3221 | case MacOS: // Earlier than 10.13. |
| 3222 | OS = llvm::Triple::MacOSX; |
| 3223 | break; |
| 3224 | case IPhoneOS: |
| 3225 | OS = llvm::Triple::IOS; |
| 3226 | break; |
| 3227 | case TvOS: // Earlier than 11.0. |
| 3228 | OS = llvm::Triple::TvOS; |
| 3229 | break; |
| 3230 | case WatchOS: // Earlier than 4.0. |
| 3231 | OS = llvm::Triple::WatchOS; |
| 3232 | break; |
| 3233 | default: // Always available on newer platforms. |
| 3234 | return false; |
| 3235 | } |
| 3236 | |
| 3237 | return TargetVersion < alignedAllocMinVersion(OS); |
| 3238 | } |
| 3239 | |
| 3240 | static bool |
| 3241 | sdkSupportsBuiltinModules(const std::optional<DarwinSDKInfo> &SDKInfo) { |
| 3242 | if (!SDKInfo) |
| 3243 | // If there is no SDK info, assume this is building against an SDK that |
| 3244 | // predates SDKSettings.json. None of those support builtin modules. |
| 3245 | return false; |
| 3246 | |
| 3247 | switch (SDKInfo->getEnvironment()) { |
| 3248 | case llvm::Triple::UnknownEnvironment: |
| 3249 | case llvm::Triple::Simulator: |
| 3250 | case llvm::Triple::MacABI: |
| 3251 | // Standard xnu/Mach/Darwin based environments depend on the SDK version. |
| 3252 | break; |
| 3253 | |
| 3254 | default: |
| 3255 | // All other environments support builtin modules from the start. |
| 3256 | return true; |
| 3257 | } |
| 3258 | |
| 3259 | VersionTuple SDKVersion = SDKInfo->getVersion(); |
| 3260 | switch (SDKInfo->getOS()) { |
| 3261 | // Existing SDKs added support for builtin modules in the fall |
| 3262 | // 2024 major releases. |
| 3263 | case llvm::Triple::MacOSX: |
| 3264 | return SDKVersion >= VersionTuple(15U); |
| 3265 | case llvm::Triple::IOS: |
| 3266 | return SDKVersion >= VersionTuple(18U); |
| 3267 | case llvm::Triple::TvOS: |
| 3268 | return SDKVersion >= VersionTuple(18U); |
| 3269 | case llvm::Triple::WatchOS: |
| 3270 | return SDKVersion >= VersionTuple(11U); |
| 3271 | case llvm::Triple::XROS: |
| 3272 | return SDKVersion >= VersionTuple(2U); |
| 3273 | |
| 3274 | // New SDKs support builtin modules from the start. |
| 3275 | default: |
| 3276 | return true; |
| 3277 | } |
| 3278 | } |
| 3279 | |
| 3280 | static inline llvm::VersionTuple |
| 3281 | sizedDeallocMinVersion(llvm::Triple::OSType OS) { |
| 3282 | switch (OS) { |
| 3283 | default: |
| 3284 | break; |
| 3285 | case llvm::Triple::Darwin: |
| 3286 | case llvm::Triple::MacOSX: // Earliest supporting version is 10.12. |
| 3287 | return llvm::VersionTuple(10U, 12U); |
| 3288 | case llvm::Triple::IOS: |
| 3289 | case llvm::Triple::TvOS: // Earliest supporting version is 10.0.0. |
| 3290 | return llvm::VersionTuple(10U); |
| 3291 | case llvm::Triple::WatchOS: // Earliest supporting version is 3.0.0. |
| 3292 | return llvm::VersionTuple(3U); |
| 3293 | } |
| 3294 | |
| 3295 | llvm_unreachable("Unexpected OS" ); |
| 3296 | } |
| 3297 | |
| 3298 | bool Darwin::isSizedDeallocationUnavailable() const { |
| 3299 | llvm::Triple::OSType OS; |
| 3300 | |
| 3301 | if (isTargetMacCatalyst()) |
| 3302 | return TargetVersion < sizedDeallocMinVersion(OS: llvm::Triple::MacOSX); |
| 3303 | switch (TargetPlatform) { |
| 3304 | case MacOS: // Earlier than 10.12. |
| 3305 | OS = llvm::Triple::MacOSX; |
| 3306 | break; |
| 3307 | case IPhoneOS: |
| 3308 | OS = llvm::Triple::IOS; |
| 3309 | break; |
| 3310 | case TvOS: // Earlier than 10.0. |
| 3311 | OS = llvm::Triple::TvOS; |
| 3312 | break; |
| 3313 | case WatchOS: // Earlier than 3.0. |
| 3314 | OS = llvm::Triple::WatchOS; |
| 3315 | break; |
| 3316 | default: |
| 3317 | // Always available on newer platforms. |
| 3318 | return false; |
| 3319 | } |
| 3320 | |
| 3321 | return TargetVersion < sizedDeallocMinVersion(OS); |
| 3322 | } |
| 3323 | |
| 3324 | void MachO::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, |
| 3325 | llvm::opt::ArgStringList &CC1Args, |
| 3326 | Action::OffloadKind DeviceOffloadKind) const { |
| 3327 | |
| 3328 | ToolChain::addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadKind); |
| 3329 | |
| 3330 | // On arm64e, we enable all the features required for the Darwin userspace |
| 3331 | // ABI |
| 3332 | if (getTriple().isArm64e()) { |
| 3333 | // Core platform ABI |
| 3334 | if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_calls, |
| 3335 | Ids: options::OPT_fno_ptrauth_calls)) |
| 3336 | CC1Args.push_back(Elt: "-fptrauth-calls" ); |
| 3337 | if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_returns, |
| 3338 | Ids: options::OPT_fno_ptrauth_returns)) |
| 3339 | CC1Args.push_back(Elt: "-fptrauth-returns" ); |
| 3340 | if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_intrinsics, |
| 3341 | Ids: options::OPT_fno_ptrauth_intrinsics)) |
| 3342 | CC1Args.push_back(Elt: "-fptrauth-intrinsics" ); |
| 3343 | if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_indirect_gotos, |
| 3344 | Ids: options::OPT_fno_ptrauth_indirect_gotos)) |
| 3345 | CC1Args.push_back(Elt: "-fptrauth-indirect-gotos" ); |
| 3346 | if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_auth_traps, |
| 3347 | Ids: options::OPT_fno_ptrauth_auth_traps)) |
| 3348 | CC1Args.push_back(Elt: "-fptrauth-auth-traps" ); |
| 3349 | |
| 3350 | // C++ v-table ABI |
| 3351 | if (!DriverArgs.hasArg( |
| 3352 | Ids: options::OPT_fptrauth_vtable_pointer_address_discrimination, |
| 3353 | Ids: options::OPT_fno_ptrauth_vtable_pointer_address_discrimination)) |
| 3354 | CC1Args.push_back(Elt: "-fptrauth-vtable-pointer-address-discrimination" ); |
| 3355 | if (!DriverArgs.hasArg( |
| 3356 | Ids: options::OPT_fptrauth_vtable_pointer_type_discrimination, |
| 3357 | Ids: options::OPT_fno_ptrauth_vtable_pointer_type_discrimination)) |
| 3358 | CC1Args.push_back(Elt: "-fptrauth-vtable-pointer-type-discrimination" ); |
| 3359 | |
| 3360 | // Objective-C ABI |
| 3361 | if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_objc_isa, |
| 3362 | Ids: options::OPT_fno_ptrauth_objc_isa)) |
| 3363 | CC1Args.push_back(Elt: "-fptrauth-objc-isa" ); |
| 3364 | if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_objc_class_ro, |
| 3365 | Ids: options::OPT_fno_ptrauth_objc_class_ro)) |
| 3366 | CC1Args.push_back(Elt: "-fptrauth-objc-class-ro" ); |
| 3367 | if (!DriverArgs.hasArg(Ids: options::OPT_fptrauth_objc_interface_sel, |
| 3368 | Ids: options::OPT_fno_ptrauth_objc_interface_sel)) |
| 3369 | CC1Args.push_back(Elt: "-fptrauth-objc-interface-sel" ); |
| 3370 | } |
| 3371 | } |
| 3372 | |
| 3373 | void Darwin::addClangTargetOptions( |
| 3374 | const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, |
| 3375 | Action::OffloadKind DeviceOffloadKind) const { |
| 3376 | |
| 3377 | MachO::addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadKind); |
| 3378 | |
| 3379 | // Pass "-faligned-alloc-unavailable" only when the user hasn't manually |
| 3380 | // enabled or disabled aligned allocations. |
| 3381 | if (!DriverArgs.hasArgNoClaim(Ids: options::OPT_faligned_allocation, |
| 3382 | Ids: options::OPT_fno_aligned_allocation) && |
| 3383 | isAlignedAllocationUnavailable()) |
| 3384 | CC1Args.push_back(Elt: "-faligned-alloc-unavailable" ); |
| 3385 | |
| 3386 | // Pass "-fno-sized-deallocation" only when the user hasn't manually enabled |
| 3387 | // or disabled sized deallocations. |
| 3388 | if (!DriverArgs.hasArgNoClaim(Ids: options::OPT_fsized_deallocation, |
| 3389 | Ids: options::OPT_fno_sized_deallocation) && |
| 3390 | isSizedDeallocationUnavailable()) |
| 3391 | CC1Args.push_back(Elt: "-fno-sized-deallocation" ); |
| 3392 | |
| 3393 | addClangCC1ASTargetOptions(Args: DriverArgs, CC1ASArgs&: CC1Args); |
| 3394 | |
| 3395 | if (SDKInfo) { |
| 3396 | // Make the SDKSettings.json an explicit dependency for the compiler |
| 3397 | // invocation, in case the compiler needs to read it to remap versions. |
| 3398 | if (!SDKInfo->getFilePath().empty()) { |
| 3399 | SmallString<64> ("-fdepfile-entry=" ); |
| 3400 | ExtraDepOpt += SDKInfo->getFilePath(); |
| 3401 | CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: ExtraDepOpt)); |
| 3402 | } |
| 3403 | } |
| 3404 | |
| 3405 | // Enable compatibility mode for NSItemProviderCompletionHandler in |
| 3406 | // Foundation/NSItemProvider.h. |
| 3407 | CC1Args.push_back(Elt: "-fcompatibility-qualified-id-block-type-checking" ); |
| 3408 | |
| 3409 | // Give static local variables in inline functions hidden visibility when |
| 3410 | // -fvisibility-inlines-hidden is enabled. |
| 3411 | if (!DriverArgs.getLastArgNoClaim( |
| 3412 | Ids: options::OPT_fvisibility_inlines_hidden_static_local_var, |
| 3413 | Ids: options::OPT_fno_visibility_inlines_hidden_static_local_var)) |
| 3414 | CC1Args.push_back(Elt: "-fvisibility-inlines-hidden-static-local-var" ); |
| 3415 | |
| 3416 | // Earlier versions of the darwin SDK have the C standard library headers |
| 3417 | // all together in the Darwin module. That leads to module cycles with |
| 3418 | // the _Builtin_ modules. e.g. <inttypes.h> on darwin includes <stdint.h>. |
| 3419 | // The builtin <stdint.h> include-nexts <stdint.h>. When both of those |
| 3420 | // darwin headers are in the Darwin module, there's a module cycle Darwin -> |
| 3421 | // _Builtin_stdint -> Darwin (i.e. inttypes.h (darwin) -> stdint.h (builtin) -> |
| 3422 | // stdint.h (darwin)). This is fixed in later versions of the darwin SDK, |
| 3423 | // but until then, the builtin headers need to join the system modules. |
| 3424 | // i.e. when the builtin stdint.h is in the Darwin module too, the cycle |
| 3425 | // goes away. Note that -fbuiltin-headers-in-system-modules does nothing |
| 3426 | // to fix the same problem with C++ headers, and is generally fragile. |
| 3427 | if (!sdkSupportsBuiltinModules(SDKInfo)) |
| 3428 | CC1Args.push_back(Elt: "-fbuiltin-headers-in-system-modules" ); |
| 3429 | |
| 3430 | if (!DriverArgs.hasArgNoClaim(Ids: options::OPT_fdefine_target_os_macros, |
| 3431 | Ids: options::OPT_fno_define_target_os_macros)) |
| 3432 | CC1Args.push_back(Elt: "-fdefine-target-os-macros" ); |
| 3433 | |
| 3434 | // Disable subdirectory modulemap search on sufficiently recent SDKs. |
| 3435 | if (SDKInfo && |
| 3436 | !DriverArgs.hasFlag(Pos: options::OPT_fmodulemap_allow_subdirectory_search, |
| 3437 | Neg: options::OPT_fno_modulemap_allow_subdirectory_search, |
| 3438 | Default: false)) { |
| 3439 | bool RequiresSubdirectorySearch; |
| 3440 | VersionTuple SDKVersion = SDKInfo->getVersion(); |
| 3441 | switch (TargetPlatform) { |
| 3442 | default: |
| 3443 | RequiresSubdirectorySearch = true; |
| 3444 | break; |
| 3445 | case MacOS: |
| 3446 | RequiresSubdirectorySearch = SDKVersion < VersionTuple(15, 0); |
| 3447 | break; |
| 3448 | case IPhoneOS: |
| 3449 | case TvOS: |
| 3450 | RequiresSubdirectorySearch = SDKVersion < VersionTuple(18, 0); |
| 3451 | break; |
| 3452 | case WatchOS: |
| 3453 | RequiresSubdirectorySearch = SDKVersion < VersionTuple(11, 0); |
| 3454 | break; |
| 3455 | case XROS: |
| 3456 | RequiresSubdirectorySearch = SDKVersion < VersionTuple(2, 0); |
| 3457 | break; |
| 3458 | } |
| 3459 | if (!RequiresSubdirectorySearch) |
| 3460 | CC1Args.push_back(Elt: "-fno-modulemap-allow-subdirectory-search" ); |
| 3461 | } |
| 3462 | } |
| 3463 | |
| 3464 | void Darwin::addClangCC1ASTargetOptions( |
| 3465 | const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CC1ASArgs) const { |
| 3466 | if (TargetVariantTriple) { |
| 3467 | CC1ASArgs.push_back(Elt: "-darwin-target-variant-triple" ); |
| 3468 | CC1ASArgs.push_back(Elt: Args.MakeArgString(Str: TargetVariantTriple->getTriple())); |
| 3469 | } |
| 3470 | |
| 3471 | if (SDKInfo) { |
| 3472 | /// Pass the SDK version to the compiler when the SDK information is |
| 3473 | /// available. |
| 3474 | auto EmitTargetSDKVersionArg = [&](const VersionTuple &V) { |
| 3475 | std::string Arg; |
| 3476 | llvm::raw_string_ostream OS(Arg); |
| 3477 | OS << "-target-sdk-version=" << V; |
| 3478 | CC1ASArgs.push_back(Elt: Args.MakeArgString(Str: Arg)); |
| 3479 | }; |
| 3480 | |
| 3481 | if (isTargetMacCatalyst()) { |
| 3482 | if (const auto *MacOStoMacCatalystMapping = SDKInfo->getVersionMapping( |
| 3483 | Kind: DarwinSDKInfo::OSEnvPair::macOStoMacCatalystPair())) { |
| 3484 | std::optional<VersionTuple> SDKVersion = MacOStoMacCatalystMapping->map( |
| 3485 | Key: SDKInfo->getVersion(), MinimumValue: minimumMacCatalystDeploymentTarget(), |
| 3486 | MaximumValue: std::nullopt); |
| 3487 | EmitTargetSDKVersionArg( |
| 3488 | SDKVersion ? *SDKVersion : minimumMacCatalystDeploymentTarget()); |
| 3489 | } |
| 3490 | } else { |
| 3491 | EmitTargetSDKVersionArg(SDKInfo->getVersion()); |
| 3492 | } |
| 3493 | |
| 3494 | /// Pass the target variant SDK version to the compiler when the SDK |
| 3495 | /// information is available and is required for target variant. |
| 3496 | if (TargetVariantTriple) { |
| 3497 | if (isTargetMacCatalyst()) { |
| 3498 | std::string Arg; |
| 3499 | llvm::raw_string_ostream OS(Arg); |
| 3500 | OS << "-darwin-target-variant-sdk-version=" << SDKInfo->getVersion(); |
| 3501 | CC1ASArgs.push_back(Elt: Args.MakeArgString(Str: Arg)); |
| 3502 | } else if (const auto *MacOStoMacCatalystMapping = |
| 3503 | SDKInfo->getVersionMapping( |
| 3504 | Kind: DarwinSDKInfo::OSEnvPair::macOStoMacCatalystPair())) { |
| 3505 | if (std::optional<VersionTuple> SDKVersion = |
| 3506 | MacOStoMacCatalystMapping->map( |
| 3507 | Key: SDKInfo->getVersion(), MinimumValue: minimumMacCatalystDeploymentTarget(), |
| 3508 | MaximumValue: std::nullopt)) { |
| 3509 | std::string Arg; |
| 3510 | llvm::raw_string_ostream OS(Arg); |
| 3511 | OS << "-darwin-target-variant-sdk-version=" << *SDKVersion; |
| 3512 | CC1ASArgs.push_back(Elt: Args.MakeArgString(Str: Arg)); |
| 3513 | } |
| 3514 | } |
| 3515 | } |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | DerivedArgList * |
| 3520 | Darwin::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch, |
| 3521 | Action::OffloadKind DeviceOffloadKind) const { |
| 3522 | // First get the generic Apple args, before moving onto Darwin-specific ones. |
| 3523 | DerivedArgList *DAL = |
| 3524 | MachO::TranslateArgs(Args, BoundArch, DeviceOffloadKind); |
| 3525 | |
| 3526 | // If no architecture is bound, none of the translations here are relevant. |
| 3527 | if (BoundArch.empty()) |
| 3528 | return DAL; |
| 3529 | |
| 3530 | // Add an explicit version min argument for the deployment target. We do this |
| 3531 | // after argument translation because -Xarch_ arguments may add a version min |
| 3532 | // argument. |
| 3533 | AddDeploymentTarget(Args&: *DAL); |
| 3534 | |
| 3535 | // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext. |
| 3536 | // FIXME: It would be far better to avoid inserting those -static arguments, |
| 3537 | // but we can't check the deployment target in the translation code until |
| 3538 | // it is set here. |
| 3539 | if (isTargetWatchOSBased() || isTargetDriverKit() || isTargetXROS() || |
| 3540 | (isTargetIOSBased() && !isIPhoneOSVersionLT(V0: 6, V1: 0))) { |
| 3541 | for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) { |
| 3542 | Arg *A = *it; |
| 3543 | ++it; |
| 3544 | if (A->getOption().getID() != options::OPT_mkernel && |
| 3545 | A->getOption().getID() != options::OPT_fapple_kext) |
| 3546 | continue; |
| 3547 | assert(it != ie && "unexpected argument translation" ); |
| 3548 | A = *it; |
| 3549 | assert(A->getOption().getID() == options::OPT_static && |
| 3550 | "missing expected -static argument" ); |
| 3551 | *it = nullptr; |
| 3552 | ++it; |
| 3553 | } |
| 3554 | } |
| 3555 | |
| 3556 | auto Arch = tools::darwin::getArchTypeForMachOArchName(Str: BoundArch); |
| 3557 | if ((Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)) { |
| 3558 | if (Args.hasFlag(Pos: options::OPT_fomit_frame_pointer, |
| 3559 | Neg: options::OPT_fno_omit_frame_pointer, Default: false)) |
| 3560 | getDriver().Diag(DiagID: clang::diag::warn_drv_unsupported_opt_for_target) |
| 3561 | << "-fomit-frame-pointer" << BoundArch; |
| 3562 | } |
| 3563 | |
| 3564 | return DAL; |
| 3565 | } |
| 3566 | |
| 3567 | ToolChain::UnwindTableLevel MachO::getDefaultUnwindTableLevel(const ArgList &Args) const { |
| 3568 | // Unwind tables are not emitted if -fno-exceptions is supplied (except when |
| 3569 | // targeting x86_64). |
| 3570 | if (getArch() == llvm::Triple::x86_64 || |
| 3571 | (GetExceptionModel(Args) != llvm::ExceptionHandling::SjLj && |
| 3572 | Args.hasFlag(Pos: options::OPT_fexceptions, Neg: options::OPT_fno_exceptions, |
| 3573 | Default: true))) |
| 3574 | return (getArch() == llvm::Triple::aarch64 || |
| 3575 | getArch() == llvm::Triple::aarch64_32) |
| 3576 | ? UnwindTableLevel::Synchronous |
| 3577 | : UnwindTableLevel::Asynchronous; |
| 3578 | |
| 3579 | return UnwindTableLevel::None; |
| 3580 | } |
| 3581 | |
| 3582 | bool MachO::UseDwarfDebugFlags() const { |
| 3583 | if (const char *S = ::getenv(name: "RC_DEBUG_OPTIONS" )) |
| 3584 | return S[0] != '\0'; |
| 3585 | return false; |
| 3586 | } |
| 3587 | |
| 3588 | std::string MachO::GetGlobalDebugPathRemapping() const { |
| 3589 | if (const char *S = ::getenv(name: "RC_DEBUG_PREFIX_MAP" )) |
| 3590 | return S; |
| 3591 | return {}; |
| 3592 | } |
| 3593 | |
| 3594 | llvm::ExceptionHandling Darwin::GetExceptionModel(const ArgList &Args) const { |
| 3595 | // Darwin uses SjLj exceptions on ARM. |
| 3596 | if (getTriple().getArch() != llvm::Triple::arm && |
| 3597 | getTriple().getArch() != llvm::Triple::thumb) |
| 3598 | return llvm::ExceptionHandling::None; |
| 3599 | |
| 3600 | // Only watchOS uses the new DWARF/Compact unwinding method. |
| 3601 | llvm::Triple Triple(ComputeLLVMTriple(Args)); |
| 3602 | if (Triple.isWatchABI()) |
| 3603 | return llvm::ExceptionHandling::DwarfCFI; |
| 3604 | |
| 3605 | return llvm::ExceptionHandling::SjLj; |
| 3606 | } |
| 3607 | |
| 3608 | bool Darwin::SupportsEmbeddedBitcode() const { |
| 3609 | assert(TargetInitialized && "Target not initialized!" ); |
| 3610 | if (isTargetIPhoneOS() && isIPhoneOSVersionLT(V0: 6, V1: 0)) |
| 3611 | return false; |
| 3612 | return true; |
| 3613 | } |
| 3614 | |
| 3615 | bool MachO::isPICDefault() const { return true; } |
| 3616 | |
| 3617 | bool MachO::isPIEDefault(const llvm::opt::ArgList &Args) const { return false; } |
| 3618 | |
| 3619 | bool MachO::isPICDefaultForced() const { |
| 3620 | return (getArch() == llvm::Triple::x86_64 || |
| 3621 | getArch() == llvm::Triple::aarch64); |
| 3622 | } |
| 3623 | |
| 3624 | bool MachO::SupportsProfiling() const { |
| 3625 | // Profiling instrumentation is only supported on x86. |
| 3626 | return getTriple().isX86(); |
| 3627 | } |
| 3628 | |
| 3629 | void Darwin::addMinVersionArgs(const ArgList &Args, |
| 3630 | ArgStringList &CmdArgs) const { |
| 3631 | VersionTuple TargetVersion = getTripleTargetVersion(); |
| 3632 | |
| 3633 | assert(!isTargetXROS() && "xrOS always uses -platform-version" ); |
| 3634 | |
| 3635 | if (isTargetWatchOS()) |
| 3636 | CmdArgs.push_back(Elt: "-watchos_version_min" ); |
| 3637 | else if (isTargetWatchOSSimulator()) |
| 3638 | CmdArgs.push_back(Elt: "-watchos_simulator_version_min" ); |
| 3639 | else if (isTargetTvOS()) |
| 3640 | CmdArgs.push_back(Elt: "-tvos_version_min" ); |
| 3641 | else if (isTargetTvOSSimulator()) |
| 3642 | CmdArgs.push_back(Elt: "-tvos_simulator_version_min" ); |
| 3643 | else if (isTargetDriverKit()) |
| 3644 | CmdArgs.push_back(Elt: "-driverkit_version_min" ); |
| 3645 | else if (isTargetIOSSimulator()) |
| 3646 | CmdArgs.push_back(Elt: "-ios_simulator_version_min" ); |
| 3647 | else if (isTargetIOSBased()) |
| 3648 | CmdArgs.push_back(Elt: "-iphoneos_version_min" ); |
| 3649 | else if (isTargetMacCatalyst()) |
| 3650 | CmdArgs.push_back(Elt: "-maccatalyst_version_min" ); |
| 3651 | else { |
| 3652 | assert(isTargetMacOS() && "unexpected target" ); |
| 3653 | CmdArgs.push_back(Elt: "-macosx_version_min" ); |
| 3654 | } |
| 3655 | |
| 3656 | VersionTuple MinTgtVers = getEffectiveTriple().getMinimumSupportedOSVersion(); |
| 3657 | if (!MinTgtVers.empty() && MinTgtVers > TargetVersion) |
| 3658 | TargetVersion = MinTgtVers; |
| 3659 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: TargetVersion.getAsString())); |
| 3660 | if (TargetVariantTriple) { |
| 3661 | assert(isTargetMacOSBased() && "unexpected target" ); |
| 3662 | VersionTuple VariantTargetVersion; |
| 3663 | if (TargetVariantTriple->isMacOSX()) { |
| 3664 | CmdArgs.push_back(Elt: "-macosx_version_min" ); |
| 3665 | TargetVariantTriple->getMacOSXVersion(Version&: VariantTargetVersion); |
| 3666 | } else { |
| 3667 | assert(TargetVariantTriple->isiOS() && |
| 3668 | TargetVariantTriple->isMacCatalystEnvironment() && |
| 3669 | "unexpected target variant triple" ); |
| 3670 | CmdArgs.push_back(Elt: "-maccatalyst_version_min" ); |
| 3671 | VariantTargetVersion = TargetVariantTriple->getiOSVersion(); |
| 3672 | } |
| 3673 | VersionTuple MinTgtVers = |
| 3674 | TargetVariantTriple->getMinimumSupportedOSVersion(); |
| 3675 | if (MinTgtVers.getMajor() && MinTgtVers > VariantTargetVersion) |
| 3676 | VariantTargetVersion = MinTgtVers; |
| 3677 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: VariantTargetVersion.getAsString())); |
| 3678 | } |
| 3679 | } |
| 3680 | |
| 3681 | static const char *getPlatformName(Darwin::DarwinPlatformKind Platform, |
| 3682 | Darwin::DarwinEnvironmentKind Environment) { |
| 3683 | switch (Platform) { |
| 3684 | case Darwin::MacOS: |
| 3685 | return "macos" ; |
| 3686 | case Darwin::IPhoneOS: |
| 3687 | if (Environment == Darwin::MacCatalyst) |
| 3688 | return "mac catalyst" ; |
| 3689 | return "ios" ; |
| 3690 | case Darwin::TvOS: |
| 3691 | return "tvos" ; |
| 3692 | case Darwin::WatchOS: |
| 3693 | return "watchos" ; |
| 3694 | case Darwin::XROS: |
| 3695 | return "xros" ; |
| 3696 | case Darwin::DriverKit: |
| 3697 | return "driverkit" ; |
| 3698 | default: |
| 3699 | break; |
| 3700 | } |
| 3701 | llvm_unreachable("invalid platform" ); |
| 3702 | } |
| 3703 | |
| 3704 | void Darwin::addPlatformVersionArgs(const llvm::opt::ArgList &Args, |
| 3705 | llvm::opt::ArgStringList &CmdArgs) const { |
| 3706 | // Firmware doesn't use -platform_version. |
| 3707 | if (TargetPlatform == DarwinPlatformKind::Firmware) |
| 3708 | return MachO::addPlatformVersionArgs(Args, CmdArgs); |
| 3709 | |
| 3710 | auto EmitPlatformVersionArg = |
| 3711 | [&](const VersionTuple &TV, Darwin::DarwinPlatformKind TargetPlatform, |
| 3712 | Darwin::DarwinEnvironmentKind TargetEnvironment, |
| 3713 | const llvm::Triple &TT) { |
| 3714 | // -platform_version <platform> <target_version> <sdk_version> |
| 3715 | // Both the target and SDK version support only up to 3 components. |
| 3716 | CmdArgs.push_back(Elt: "-platform_version" ); |
| 3717 | std::string PlatformName = |
| 3718 | getPlatformName(Platform: TargetPlatform, Environment: TargetEnvironment); |
| 3719 | if (TargetEnvironment == Darwin::Simulator) |
| 3720 | PlatformName += "-simulator" ; |
| 3721 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: PlatformName)); |
| 3722 | VersionTuple TargetVersion = TV.withoutBuild(); |
| 3723 | if ((TargetPlatform == Darwin::IPhoneOS || |
| 3724 | TargetPlatform == Darwin::TvOS) && |
| 3725 | getTriple().getArchName() == "arm64e" && |
| 3726 | TargetVersion.getMajor() < 14) { |
| 3727 | // arm64e slice is supported on iOS/tvOS 14+ only. |
| 3728 | TargetVersion = VersionTuple(14, 0); |
| 3729 | } |
| 3730 | VersionTuple MinTgtVers = TT.getMinimumSupportedOSVersion(); |
| 3731 | if (!MinTgtVers.empty() && MinTgtVers > TargetVersion) |
| 3732 | TargetVersion = MinTgtVers; |
| 3733 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: TargetVersion.getAsString())); |
| 3734 | |
| 3735 | if (TargetPlatform == IPhoneOS && TargetEnvironment == MacCatalyst) { |
| 3736 | // Mac Catalyst programs must use the appropriate iOS SDK version |
| 3737 | // that corresponds to the macOS SDK version used for the compilation. |
| 3738 | std::optional<VersionTuple> iOSSDKVersion; |
| 3739 | if (SDKInfo) { |
| 3740 | if (const auto *MacOStoMacCatalystMapping = |
| 3741 | SDKInfo->getVersionMapping( |
| 3742 | Kind: DarwinSDKInfo::OSEnvPair::macOStoMacCatalystPair())) { |
| 3743 | iOSSDKVersion = MacOStoMacCatalystMapping->map( |
| 3744 | Key: SDKInfo->getVersion().withoutBuild(), |
| 3745 | MinimumValue: minimumMacCatalystDeploymentTarget(), MaximumValue: std::nullopt); |
| 3746 | } |
| 3747 | } |
| 3748 | CmdArgs.push_back(Elt: Args.MakeArgString( |
| 3749 | Str: (iOSSDKVersion ? *iOSSDKVersion |
| 3750 | : minimumMacCatalystDeploymentTarget()) |
| 3751 | .getAsString())); |
| 3752 | return; |
| 3753 | } |
| 3754 | |
| 3755 | if (SDKInfo) { |
| 3756 | VersionTuple SDKVersion = SDKInfo->getVersion().withoutBuild(); |
| 3757 | if (!SDKVersion.getMinor()) |
| 3758 | SDKVersion = VersionTuple(SDKVersion.getMajor(), 0); |
| 3759 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: SDKVersion.getAsString())); |
| 3760 | } else { |
| 3761 | // Use an SDK version that's matching the deployment target if the SDK |
| 3762 | // version is missing. This is preferred over an empty SDK version |
| 3763 | // (0.0.0) as the system's runtime might expect the linked binary to |
| 3764 | // contain a valid SDK version in order for the binary to work |
| 3765 | // correctly. It's reasonable to use the deployment target version as |
| 3766 | // a proxy for the SDK version because older SDKs don't guarantee |
| 3767 | // support for deployment targets newer than the SDK versions, so that |
| 3768 | // rules out using some predetermined older SDK version, which leaves |
| 3769 | // the deployment target version as the only reasonable choice. |
| 3770 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: TargetVersion.getAsString())); |
| 3771 | } |
| 3772 | }; |
| 3773 | EmitPlatformVersionArg(getTripleTargetVersion(), TargetPlatform, |
| 3774 | TargetEnvironment, getEffectiveTriple()); |
| 3775 | if (!TargetVariantTriple) |
| 3776 | return; |
| 3777 | Darwin::DarwinPlatformKind Platform; |
| 3778 | Darwin::DarwinEnvironmentKind Environment; |
| 3779 | VersionTuple TargetVariantVersion; |
| 3780 | if (TargetVariantTriple->isMacOSX()) { |
| 3781 | TargetVariantTriple->getMacOSXVersion(Version&: TargetVariantVersion); |
| 3782 | Platform = Darwin::MacOS; |
| 3783 | Environment = Darwin::NativeEnvironment; |
| 3784 | } else { |
| 3785 | assert(TargetVariantTriple->isiOS() && |
| 3786 | TargetVariantTriple->isMacCatalystEnvironment() && |
| 3787 | "unexpected target variant triple" ); |
| 3788 | TargetVariantVersion = TargetVariantTriple->getiOSVersion(); |
| 3789 | Platform = Darwin::IPhoneOS; |
| 3790 | Environment = Darwin::MacCatalyst; |
| 3791 | } |
| 3792 | EmitPlatformVersionArg(TargetVariantVersion, Platform, Environment, |
| 3793 | *TargetVariantTriple); |
| 3794 | } |
| 3795 | |
| 3796 | // Add additional link args for the -dynamiclib option. |
| 3797 | static void addDynamicLibLinkArgs(const Darwin &D, const ArgList &Args, |
| 3798 | ArgStringList &CmdArgs) { |
| 3799 | // Derived from darwin_dylib1 spec. |
| 3800 | if (D.isTargetIPhoneOS()) { |
| 3801 | if (D.isIPhoneOSVersionLT(V0: 3, V1: 1)) |
| 3802 | CmdArgs.push_back(Elt: "-ldylib1.o" ); |
| 3803 | return; |
| 3804 | } |
| 3805 | |
| 3806 | if (!D.isTargetMacOS()) |
| 3807 | return; |
| 3808 | if (D.isMacosxVersionLT(V0: 10, V1: 5)) |
| 3809 | CmdArgs.push_back(Elt: "-ldylib1.o" ); |
| 3810 | else if (D.isMacosxVersionLT(V0: 10, V1: 6)) |
| 3811 | CmdArgs.push_back(Elt: "-ldylib1.10.5.o" ); |
| 3812 | } |
| 3813 | |
| 3814 | // Add additional link args for the -bundle option. |
| 3815 | static void addBundleLinkArgs(const Darwin &D, const ArgList &Args, |
| 3816 | ArgStringList &CmdArgs) { |
| 3817 | if (Args.hasArg(Ids: options::OPT_static)) |
| 3818 | return; |
| 3819 | // Derived from darwin_bundle1 spec. |
| 3820 | if ((D.isTargetIPhoneOS() && D.isIPhoneOSVersionLT(V0: 3, V1: 1)) || |
| 3821 | (D.isTargetMacOS() && D.isMacosxVersionLT(V0: 10, V1: 6))) |
| 3822 | CmdArgs.push_back(Elt: "-lbundle1.o" ); |
| 3823 | } |
| 3824 | |
| 3825 | // Add additional link args for the -pg option. |
| 3826 | static void addPgProfilingLinkArgs(const Darwin &D, const ArgList &Args, |
| 3827 | ArgStringList &CmdArgs) { |
| 3828 | if (D.isTargetMacOS() && D.isMacosxVersionLT(V0: 10, V1: 9)) { |
| 3829 | if (Args.hasArg(Ids: options::OPT_static) || Args.hasArg(Ids: options::OPT_object) || |
| 3830 | Args.hasArg(Ids: options::OPT_preload)) { |
| 3831 | CmdArgs.push_back(Elt: "-lgcrt0.o" ); |
| 3832 | } else { |
| 3833 | CmdArgs.push_back(Elt: "-lgcrt1.o" ); |
| 3834 | |
| 3835 | // darwin_crt2 spec is empty. |
| 3836 | } |
| 3837 | // By default on OS X 10.8 and later, we don't link with a crt1.o |
| 3838 | // file and the linker knows to use _main as the entry point. But, |
| 3839 | // when compiling with -pg, we need to link with the gcrt1.o file, |
| 3840 | // so pass the -no_new_main option to tell the linker to use the |
| 3841 | // "start" symbol as the entry point. |
| 3842 | if (!D.isMacosxVersionLT(V0: 10, V1: 8)) |
| 3843 | CmdArgs.push_back(Elt: "-no_new_main" ); |
| 3844 | } else { |
| 3845 | D.getDriver().Diag(DiagID: diag::err_drv_clang_unsupported_opt_pg_darwin) |
| 3846 | << D.isTargetMacOSBased(); |
| 3847 | } |
| 3848 | } |
| 3849 | |
| 3850 | static void addDefaultCRTLinkArgs(const Darwin &D, const ArgList &Args, |
| 3851 | ArgStringList &CmdArgs) { |
| 3852 | // Derived from darwin_crt1 spec. |
| 3853 | if (D.isTargetIPhoneOS()) { |
| 3854 | if (D.getArch() == llvm::Triple::aarch64) |
| 3855 | ; // iOS does not need any crt1 files for arm64 |
| 3856 | else if (D.isIPhoneOSVersionLT(V0: 3, V1: 1)) |
| 3857 | CmdArgs.push_back(Elt: "-lcrt1.o" ); |
| 3858 | else if (D.isIPhoneOSVersionLT(V0: 6, V1: 0)) |
| 3859 | CmdArgs.push_back(Elt: "-lcrt1.3.1.o" ); |
| 3860 | return; |
| 3861 | } |
| 3862 | |
| 3863 | if (!D.isTargetMacOS()) |
| 3864 | return; |
| 3865 | if (D.isMacosxVersionLT(V0: 10, V1: 5)) |
| 3866 | CmdArgs.push_back(Elt: "-lcrt1.o" ); |
| 3867 | else if (D.isMacosxVersionLT(V0: 10, V1: 6)) |
| 3868 | CmdArgs.push_back(Elt: "-lcrt1.10.5.o" ); |
| 3869 | else if (D.isMacosxVersionLT(V0: 10, V1: 8)) |
| 3870 | CmdArgs.push_back(Elt: "-lcrt1.10.6.o" ); |
| 3871 | // darwin_crt2 spec is empty. |
| 3872 | } |
| 3873 | |
| 3874 | void Darwin::addStartObjectFileArgs(const ArgList &Args, |
| 3875 | ArgStringList &CmdArgs) const { |
| 3876 | // Firmware uses the "bare metal" start object file args. |
| 3877 | if (isTargetFirmware()) |
| 3878 | return MachO::addStartObjectFileArgs(Args, CmdArgs); |
| 3879 | |
| 3880 | // Derived from startfile spec. |
| 3881 | if (Args.hasArg(Ids: options::OPT_dynamiclib)) |
| 3882 | addDynamicLibLinkArgs(D: *this, Args, CmdArgs); |
| 3883 | else if (Args.hasArg(Ids: options::OPT_bundle)) |
| 3884 | addBundleLinkArgs(D: *this, Args, CmdArgs); |
| 3885 | else if (Args.hasArg(Ids: options::OPT_pg) && SupportsProfiling()) |
| 3886 | addPgProfilingLinkArgs(D: *this, Args, CmdArgs); |
| 3887 | else if (Args.hasArg(Ids: options::OPT_static) || |
| 3888 | Args.hasArg(Ids: options::OPT_object) || |
| 3889 | Args.hasArg(Ids: options::OPT_preload)) |
| 3890 | CmdArgs.push_back(Elt: "-lcrt0.o" ); |
| 3891 | else |
| 3892 | addDefaultCRTLinkArgs(D: *this, Args, CmdArgs); |
| 3893 | |
| 3894 | if (isTargetMacOS() && Args.hasArg(Ids: options::OPT_shared_libgcc) && |
| 3895 | isMacosxVersionLT(V0: 10, V1: 5)) { |
| 3896 | const char *Str = Args.MakeArgString(Str: GetFilePath(Name: "crt3.o" )); |
| 3897 | CmdArgs.push_back(Elt: Str); |
| 3898 | } |
| 3899 | } |
| 3900 | |
| 3901 | void Darwin::CheckObjCARC() const { |
| 3902 | if (isTargetIOSBased() || isTargetWatchOSBased() || isTargetXROS() || |
| 3903 | (isTargetMacOSBased() && !isMacosxVersionLT(V0: 10, V1: 6))) |
| 3904 | return; |
| 3905 | getDriver().Diag(DiagID: diag::err_arc_unsupported_on_toolchain); |
| 3906 | } |
| 3907 | |
| 3908 | SanitizerMask Darwin::getSupportedSanitizers() const { |
| 3909 | const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; |
| 3910 | const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64; |
| 3911 | SanitizerMask Res = ToolChain::getSupportedSanitizers(); |
| 3912 | Res |= SanitizerKind::Address; |
| 3913 | Res |= SanitizerKind::PointerCompare; |
| 3914 | Res |= SanitizerKind::PointerSubtract; |
| 3915 | Res |= SanitizerKind::Realtime; |
| 3916 | Res |= SanitizerKind::Leak; |
| 3917 | Res |= SanitizerKind::Fuzzer; |
| 3918 | Res |= SanitizerKind::FuzzerNoLink; |
| 3919 | Res |= SanitizerKind::ObjCCast; |
| 3920 | |
| 3921 | // Prior to 10.9, macOS shipped a version of the C++ standard library without |
| 3922 | // C++11 support. The same is true of iOS prior to version 5. These OS'es are |
| 3923 | // incompatible with -fsanitize=vptr. |
| 3924 | if (!(isTargetMacOSBased() && isMacosxVersionLT(V0: 10, V1: 9)) && |
| 3925 | !(isTargetIPhoneOS() && isIPhoneOSVersionLT(V0: 5, V1: 0))) |
| 3926 | Res |= SanitizerKind::Vptr; |
| 3927 | |
| 3928 | if ((IsX86_64 || IsAArch64) && |
| 3929 | (isTargetMacOSBased() || isTargetIOSSimulator() || |
| 3930 | isTargetTvOSSimulator() || isTargetWatchOSSimulator())) { |
| 3931 | Res |= SanitizerKind::Thread; |
| 3932 | } |
| 3933 | |
| 3934 | if ((IsX86_64 || IsAArch64) && isTargetMacOSBased()) { |
| 3935 | Res |= SanitizerKind::Type; |
| 3936 | } |
| 3937 | |
| 3938 | if (IsX86_64) |
| 3939 | Res |= SanitizerKind::NumericalStability; |
| 3940 | |
| 3941 | return Res; |
| 3942 | } |
| 3943 | |
| 3944 | void AppleMachO::printVerboseInfo(raw_ostream &OS) const { |
| 3945 | CudaInstallation->print(OS); |
| 3946 | RocmInstallation->print(OS); |
| 3947 | } |
| 3948 | |