1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
|
//===------ DarwinToolChains.cpp - Job invocations (Darwin-specific) ------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "ToolChains.h"
#include "swift/AST/DiagnosticsDriver.h"
#include "swift/AST/PlatformKind.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/Platform.h"
#include "swift/Basic/Range.h"
#include "swift/Basic/STLExtras.h"
#include "swift/Basic/TaskQueue.h"
#include "swift/Config.h"
#include "swift/Driver/Compilation.h"
#include "swift/Driver/Driver.h"
#include "swift/Driver/Job.h"
#include "swift/IDETool/CompilerInvocation.h"
#include "swift/Option/Options.h"
#include "clang/Basic/DarwinSDKInfo.h"
#include "clang/Basic/Version.h"
#include "clang/Driver/Util.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/VersionTuple.h"
using namespace swift;
using namespace swift::driver;
using namespace llvm::opt;
std::string
toolchains::Darwin::findProgramRelativeToSwiftImpl(StringRef name) const {
StringRef swiftPath = getDriver().getSwiftProgramPath();
StringRef swiftBinDir = llvm::sys::path::parent_path(swiftPath);
// See if we're in an Xcode toolchain.
bool hasToolchain = false;
llvm::SmallString<128> path{swiftBinDir};
llvm::sys::path::remove_filename(path); // bin
llvm::sys::path::remove_filename(path); // usr
if (llvm::sys::path::extension(path) == ".xctoolchain") {
hasToolchain = true;
llvm::sys::path::remove_filename(path); // *.xctoolchain
llvm::sys::path::remove_filename(path); // Toolchains
llvm::sys::path::append(path, "usr", "bin");
}
StringRef paths[] = {swiftBinDir, path};
auto pathsRef = llvm::ArrayRef(paths);
if (!hasToolchain)
pathsRef = pathsRef.drop_back();
auto result = llvm::sys::findProgramByName(name, pathsRef);
if (result)
return result.get();
return {};
}
ToolChain::InvocationInfo
toolchains::Darwin::constructInvocation(const InterpretJobAction &job,
const JobContext &context) const {
InvocationInfo II = ToolChain::constructInvocation(job, context);
SmallVector<std::string, 4> runtimeLibraryPaths;
getRuntimeLibraryPaths(runtimeLibraryPaths, context.Args, context.OI.SDKPath,
/*Shared=*/true);
addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "DYLD_LIBRARY_PATH",
":", options::OPT_L, context.Args,
runtimeLibraryPaths);
addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "DYLD_FRAMEWORK_PATH",
":", options::OPT_F, context.Args,
{"/System/Library/Frameworks"});
// FIXME: Add options::OPT_Fsystem paths to DYLD_FRAMEWORK_PATH as well.
return II;
}
static StringRef
getDarwinLibraryNameSuffixForTriple(const llvm::Triple &triple) {
const DarwinPlatformKind kind = getDarwinPlatformKind(triple);
switch (kind) {
case DarwinPlatformKind::MacOS:
return "osx";
case DarwinPlatformKind::IPhoneOS:
// Here we return "osx" under the assumption that all the
// darwin runtime libraries are zippered and so the "osx" variants
// should be used for macCatalyst targets.
if (tripleIsMacCatalystEnvironment(triple))
return "osx";
return "ios";
case DarwinPlatformKind::IPhoneOSSimulator:
return "iossim";
case DarwinPlatformKind::TvOS:
return "tvos";
case DarwinPlatformKind::TvOSSimulator:
return "tvossim";
case DarwinPlatformKind::WatchOS:
return "watchos";
case DarwinPlatformKind::WatchOSSimulator:
return "watchossim";
case DarwinPlatformKind::VisionOS:
return "xros";
case DarwinPlatformKind::VisionOSSimulator:
return "xrossim";
}
llvm_unreachable("Unsupported Darwin platform");
}
std::string toolchains::Darwin::sanitizerRuntimeLibName(StringRef Sanitizer,
bool shared) const {
return (Twine("libclang_rt.") + Sanitizer + "_" +
getDarwinLibraryNameSuffixForTriple(this->getTriple()) +
(shared ? "_dynamic.dylib" : ".a"))
.str();
}
void
toolchains::Darwin::addPluginArguments(const ArgList &Args,
ArgStringList &Arguments) const {
SmallString<64> pluginPath;
auto programPath = getDriver().getSwiftProgramPath();
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
programPath, /*shared=*/true, pluginPath);
auto defaultPluginPath = pluginPath;
llvm::sys::path::append(defaultPluginPath, "host", "plugins");
// Default plugin path.
Arguments.push_back("-plugin-path");
Arguments.push_back(Args.MakeArgString(defaultPluginPath));
// Local plugin path.
llvm::sys::path::remove_filename(pluginPath); // Remove "swift"
llvm::sys::path::remove_filename(pluginPath); // Remove "lib"
llvm::sys::path::append(pluginPath, "local", "lib");
llvm::sys::path::append(pluginPath, "swift");
llvm::sys::path::append(pluginPath, "host", "plugins");
Arguments.push_back("-plugin-path");
Arguments.push_back(Args.MakeArgString(pluginPath));
}
static void addLinkRuntimeLibRPath(const ArgList &Args,
ArgStringList &Arguments,
StringRef DarwinLibName,
const ToolChain &TC) {
// Adding the rpaths might negatively interact when other rpaths are involved,
// so we should make sure we add the rpaths last, after all user-specified
// rpaths. This is currently true from this place, but we need to be
// careful if this function is ever called before user's rpaths are emitted.
assert(DarwinLibName.endswith(".dylib") && "must be a dynamic library");
// Add @executable_path to rpath to support having the dylib copied with
// the executable.
Arguments.push_back("-rpath");
Arguments.push_back("@executable_path");
// Add the path to the resource dir to rpath to support using the dylib
// from the default location without copying.
SmallString<128> ClangLibraryPath;
TC.getClangLibraryPath(Args, ClangLibraryPath);
Arguments.push_back("-rpath");
Arguments.push_back(Args.MakeArgString(ClangLibraryPath));
}
static void addLinkSanitizerLibArgsForDarwin(const ArgList &Args,
ArgStringList &Arguments,
StringRef Sanitizer,
const ToolChain &TC,
bool shared = true) {
// Sanitizer runtime libraries requires C++.
Arguments.push_back("-lc++");
// Add explicit dependency on -lc++abi, as -lc++ doesn't re-export
// all RTTI-related symbols that are used.
Arguments.push_back("-lc++abi");
auto LibName = TC.sanitizerRuntimeLibName(Sanitizer, shared);
TC.addLinkRuntimeLib(Args, Arguments, LibName);
if (shared)
addLinkRuntimeLibRPath(Args, Arguments, LibName, TC);
}
/// Runs <code>xcrun -f clang</code> in order to find the location of Clang for
/// the currently active Xcode.
///
/// We get the "currently active" part by passing through the DEVELOPER_DIR
/// environment variable (along with the rest of the environment).
static bool findXcodeClangPath(llvm::SmallVectorImpl<char> &path) {
assert(path.empty());
auto xcrunPath = llvm::sys::findProgramByName("xcrun");
if (!xcrunPath.getError()) {
// Explicitly ask for the default toolchain so that we don't find a Clang
// included with an open-source toolchain.
const char *args[] = {"-toolchain", "default", "-f", "clang", nullptr};
sys::TaskQueue queue;
queue.addTask(xcrunPath->c_str(), args, /*Env=*/std::nullopt,
/*Context=*/nullptr,
/*SeparateErrors=*/true);
queue.execute(nullptr,
[&path](sys::ProcessId PID, int returnCode, StringRef output,
StringRef errors,
sys::TaskProcessInformation ProcInfo,
void *unused) -> sys::TaskFinishedResponse {
if (returnCode == 0) {
output = output.rtrim();
path.append(output.begin(), output.end());
}
return sys::TaskFinishedResponse::ContinueExecution;
});
}
return !path.empty();
}
static bool findXcodeClangLibPath(const Twine &libName,
llvm::SmallVectorImpl<char> &path) {
assert(path.empty());
if (!findXcodeClangPath(path)) {
return false;
}
llvm::sys::path::remove_filename(path); // 'clang'
llvm::sys::path::remove_filename(path); // 'bin'
llvm::sys::path::append(path, "lib", libName);
return true;
}
static void addVersionString(const ArgList &inputArgs, ArgStringList &arguments,
llvm::VersionTuple version) {
llvm::SmallString<8> buf;
llvm::raw_svector_ostream os{buf};
os << version.getMajor() << '.' << version.getMinor().value_or(0) << '.'
<< version.getSubminor().value_or(0);
arguments.push_back(inputArgs.MakeArgString(os.str()));
}
void
toolchains::Darwin::addLinkerInputArgs(InvocationInfo &II,
const JobContext &context) const {
ArgStringList &Arguments = II.Arguments;
if (context.shouldUseInputFileList()) {
Arguments.push_back("-filelist");
Arguments.push_back(context.getTemporaryFilePath("inputs", "LinkFileList"));
II.FilelistInfos.push_back(
{Arguments.back(), context.OI.CompilerOutputType,
FilelistInfo::WhichFiles::InputJobsAndSourceInputActions});
} else {
addPrimaryInputsOfType(Arguments, context.Inputs, context.Args,
file_types::TY_Object);
addPrimaryInputsOfType(Arguments, context.Inputs, context.Args,
file_types::TY_TBD);
addPrimaryInputsOfType(Arguments, context.Inputs, context.Args,
file_types::TY_LLVM_BC);
addInputsOfType(Arguments, context.InputActions, file_types::TY_Object);
addInputsOfType(Arguments, context.InputActions, file_types::TY_TBD);
addInputsOfType(Arguments, context.InputActions, file_types::TY_LLVM_BC);
}
if (context.OI.CompilerMode == OutputInfo::Mode::SingleCompile)
addInputsOfType(Arguments, context.Inputs, context.Args,
file_types::TY_SwiftModuleFile, "-add_ast_path");
else
addPrimaryInputsOfType(Arguments, context.Inputs, context.Args,
file_types::TY_SwiftModuleFile, "-add_ast_path");
// Add all .swiftmodule file inputs as arguments, preceded by the
// "-add_ast_path" linker option.
addInputsOfType(Arguments, context.InputActions,
file_types::TY_SwiftModuleFile, "-add_ast_path");
}
void toolchains::Darwin::addLTOLibArgs(ArgStringList &Arguments,
const JobContext &context) const {
if (!context.OI.LibLTOPath.empty()) {
// Check for user-specified LTO library.
Arguments.push_back("-lto_library");
Arguments.push_back(context.Args.MakeArgString(context.OI.LibLTOPath));
} else {
// Check for relative libLTO.dylib. This would be the expected behavior in an
// Xcode toolchain.
StringRef P = llvm::sys::path::parent_path(getDriver().getSwiftProgramPath());
llvm::SmallString<128> LibLTOPath(P);
llvm::sys::path::remove_filename(LibLTOPath); // Remove '/bin'
llvm::sys::path::append(LibLTOPath, "lib");
llvm::sys::path::append(LibLTOPath, "libLTO.dylib");
if (llvm::sys::fs::exists(LibLTOPath)) {
Arguments.push_back("-lto_library");
Arguments.push_back(context.Args.MakeArgString(LibLTOPath));
} else {
// Use libLTO.dylib from the default toolchain if a relative one does not exist.
llvm::SmallString<128> LibLTOPath;
if (findXcodeClangLibPath("libLTO.dylib", LibLTOPath)) {
Arguments.push_back("-lto_library");
Arguments.push_back(context.Args.MakeArgString(LibLTOPath));
}
}
}
}
void
toolchains::Darwin::addSanitizerArgs(ArgStringList &Arguments,
const DynamicLinkJobAction &job,
const JobContext &context) const {
// Linking sanitizers will add rpaths, which might negatively interact when
// other rpaths are involved, so we should make sure we add the rpaths after
// all user-specified rpaths.
if (context.OI.SelectedSanitizers & SanitizerKind::Address) {
if (context.OI.SanitizerUseStableABI)
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "asan_abi",
*this, false);
else
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "asan", *this);
}
if (context.OI.SelectedSanitizers & SanitizerKind::Thread)
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "tsan", *this);
if (context.OI.SelectedSanitizers & SanitizerKind::Undefined)
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "ubsan", *this);
// Only link in libFuzzer for executables.
if (job.getKind() == LinkKind::Executable &&
(context.OI.SelectedSanitizers & SanitizerKind::Fuzzer))
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "fuzzer", *this,
/*shared=*/false);
}
namespace {
enum class BackDeployLibFilter {
executable,
all
};
// Whether the given job matches the backward-deployment library filter.
bool jobMatchesFilter(LinkKind jobKind, BackDeployLibFilter filter) {
switch (filter) {
case BackDeployLibFilter::executable:
return jobKind == LinkKind::Executable;
case BackDeployLibFilter::all:
return true;
}
llvm_unreachable("unhandled back deploy lib filter!");
}
}
void
toolchains::Darwin::addArgsToLinkStdlib(ArgStringList &Arguments,
const DynamicLinkJobAction &job,
const JobContext &context) const {
// Link compatibility libraries, if we're deploying back to OSes that
// have an older Swift runtime.
SmallString<128> SharedResourceDirPath;
getResourceDirPath(SharedResourceDirPath, context.Args, /*Shared=*/true);
std::optional<llvm::VersionTuple> runtimeCompatibilityVersion;
if (context.Args.hasArg(options::OPT_runtime_compatibility_version)) {
auto value = context.Args.getLastArgValue(
options::OPT_runtime_compatibility_version);
if (value.equals("5.0")) {
runtimeCompatibilityVersion = llvm::VersionTuple(5, 0);
} else if (value.equals("5.1")) {
runtimeCompatibilityVersion = llvm::VersionTuple(5, 1);
} else if (value.equals("5.5")) {
runtimeCompatibilityVersion = llvm::VersionTuple(5, 5);
} else if (value.equals("5.6")) {
runtimeCompatibilityVersion = llvm::VersionTuple(5, 6);
} else if (value.equals("5.8")) {
runtimeCompatibilityVersion = llvm::VersionTuple(5, 8);
} else if (value.equals("6.0")) {
runtimeCompatibilityVersion = llvm::VersionTuple(6, 0);
} else if (value.equals("none")) {
runtimeCompatibilityVersion = std::nullopt;
} else {
// TODO: diagnose unknown runtime compatibility version?
}
} else if (job.getKind() == LinkKind::Executable) {
runtimeCompatibilityVersion
= getSwiftRuntimeCompatibilityVersionForTarget(getTriple());
}
if (runtimeCompatibilityVersion) {
auto addBackDeployLib = [&](llvm::VersionTuple version,
BackDeployLibFilter filter,
StringRef libraryName,
bool forceLoad) {
if (*runtimeCompatibilityVersion > version)
return;
if (!jobMatchesFilter(job.getKind(), filter))
return;
SmallString<128> BackDeployLib;
BackDeployLib.append(SharedResourceDirPath);
llvm::sys::path::append(BackDeployLib, "lib" + libraryName + ".a");
if (llvm::sys::fs::exists(BackDeployLib)) {
if (forceLoad)
Arguments.push_back("-force_load");
Arguments.push_back(context.Args.MakeArgString(BackDeployLib));
}
};
#define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName, ForceLoad) \
addBackDeployLib( \
llvm::VersionTuple Version, BackDeployLibFilter::Filter, \
LibraryName, ForceLoad);
#include "swift/Frontend/BackDeploymentLibs.def"
}
// Add the runtime library link path, which is platform-specific and found
// relative to the compiler.
SmallVector<std::string, 4> RuntimeLibPaths;
getRuntimeLibraryPaths(RuntimeLibPaths, context.Args,
context.OI.SDKPath, /*Shared=*/true);
for (auto path : RuntimeLibPaths) {
Arguments.push_back("-L");
Arguments.push_back(context.Args.MakeArgString(path));
}
if (context.Args.hasFlag(options::OPT_toolchain_stdlib_rpath,
options::OPT_no_toolchain_stdlib_rpath, false)) {
// If the user has explicitly asked for a toolchain stdlib, we should
// provide one using -rpath. This used to be the default behaviour but it
// was considered annoying in at least the SwiftPM scenario (see
// https://github.com/apple/swift/issues/44576) and is obsolete in all
// scenarios of deploying for Swift-in-the-OS. We keep it here as an
// optional behaviour so that people downloading snapshot toolchains for
// testing new stdlibs will be able to link to the stdlib bundled in
// that toolchain.
for (auto path : RuntimeLibPaths) {
Arguments.push_back("-rpath");
Arguments.push_back(context.Args.MakeArgString(path));
}
} else if (!tripleRequiresRPathForSwiftLibrariesInOS(getTriple()) ||
context.Args.hasArg(options::OPT_no_stdlib_rpath)) {
// If targeting an OS with Swift in /usr/lib/swift, the LC_ID_DYLIB
// install_name the stdlib will be an absolute path like
// /usr/lib/swift/libswiftCore.dylib, and we do not need to provide an rpath
// at all.
//
// Also, if the user explicitly asks for no rpath entry, we assume they know
// what they're doing and do not add one here.
} else {
// The remaining cases are back-deploying (to OSs predating
// Swift-in-the-OS). In these cases, the stdlib will be giving us (via
// stdlib/linker-support/magic-symbols-for-install-name.c) an LC_ID_DYLIB
// install_name that is rpath-relative, like @rpath/libswiftCore.dylib.
//
// If we're linking an app bundle, it's possible there's an embedded stdlib
// in there, in which case we'd want to put @executable_path/../Frameworks
// in the rpath to find and prefer it, but (a) we don't know when we're
// linking an app bundle and (b) we probably _never_ will be because Xcode
// links using clang, not the swift driver.
//
// So that leaves us with the case of linking a command-line app. These are
// only supported by installing a secondary package that puts some frozen
// Swift-in-OS libraries in the /usr/lib/swift location. That's the best we
// can give for rpath, though it might fail at runtime if the support
// package isn't installed.
Arguments.push_back("-rpath");
Arguments.push_back(context.Args.MakeArgString("/usr/lib/swift"));
// We don’t need an rpath for /System/iOSSupport/usr/lib/swift because:
// 1. The standard library and overlays were part of the OS before
// Catalyst was introduced, so they are always available for Catalyst.
// 2. The _Concurrency back-deployment library is zippered, whereas only
// unzippered frameworks need an unzippered twin in /System/iOSSupport.
}
}
void
toolchains::Darwin::addProfileGenerationArgs(ArgStringList &Arguments,
const JobContext &context) const {
const llvm::Triple &Triple = getTriple();
if (context.Args.hasArg(options::OPT_profile_generate)) {
SmallString<128> LibProfile;
getClangLibraryPath(context.Args, LibProfile);
StringRef RT;
if (Triple.isiOS()) {
if (Triple.isTvOS())
RT = "tvos";
else
RT = "ios";
} else if (Triple.isWatchOS()) {
RT = "watchos";
} else if (Triple.isXROS()) {
RT = "xros";
} else {
assert(Triple.isMacOSX());
RT = "osx";
}
StringRef Sim;
if (Triple.isSimulatorEnvironment()) {
Sim = "sim";
}
llvm::sys::path::append(LibProfile,
"libclang_rt.profile_" + RT + Sim + ".a");
// FIXME: Continue accepting the old path for simulator libraries for now.
if (!Sim.empty() && !llvm::sys::fs::exists(LibProfile)) {
llvm::sys::path::remove_filename(LibProfile);
llvm::sys::path::append(LibProfile, "libclang_rt.profile_" + RT + ".a");
}
Arguments.push_back(context.Args.MakeArgString(LibProfile));
}
}
std::optional<llvm::VersionTuple>
toolchains::Darwin::getTargetSDKVersion(const llvm::Triple &triple) const {
if (!SDKInfo)
return std::nullopt;
return swift::getTargetSDKVersion(*SDKInfo, triple);
}
void
toolchains::Darwin::addDeploymentTargetArgs(ArgStringList &Arguments,
const JobContext &context) const {
auto addPlatformVersionArg = [&](const llvm::Triple &triple) {
// Compute the name of the platform for the linker.
const char *platformName;
if (tripleIsMacCatalystEnvironment(triple)) {
platformName = "mac-catalyst";
} else {
switch (getDarwinPlatformKind(triple)) {
case DarwinPlatformKind::MacOS:
platformName = "macos";
break;
case DarwinPlatformKind::IPhoneOS:
platformName = "ios";
break;
case DarwinPlatformKind::IPhoneOSSimulator:
platformName = "ios-simulator";
break;
case DarwinPlatformKind::TvOS:
platformName = "tvos";
break;
case DarwinPlatformKind::TvOSSimulator:
platformName = "tvos-simulator";
break;
case DarwinPlatformKind::WatchOS:
platformName = "watchos";
break;
case DarwinPlatformKind::WatchOSSimulator:
platformName = "watchos-simulator";
break;
case DarwinPlatformKind::VisionOS:
platformName = "xros";
break;
case DarwinPlatformKind::VisionOSSimulator:
platformName = "xros-simulator";
break;
}
}
// Compute the platform version.
llvm::VersionTuple osVersion;
if (tripleIsMacCatalystEnvironment(triple)) {
osVersion = triple.getiOSVersion();
if (osVersion.getMajor() < 14 && triple.isAArch64()) {
// Mac Catalyst on arm was introduced with an iOS deployment target of
// 14.0; the linker doesn't want to see a deployment target before that.
osVersion = llvm::VersionTuple(/*Major=*/14, /*Minor=*/0);
} else if (osVersion.getMajor() < 13) {
// Mac Catalyst was introduced with an iOS deployment target of 13.1;
// the linker doesn't want to see a deployment target before that.
osVersion = llvm::VersionTuple(/*Major=*/13, /*Minor=*/1);
}
} else {
switch (getDarwinPlatformKind((triple))) {
case DarwinPlatformKind::MacOS:
triple.getMacOSXVersion(osVersion);
// The first deployment of arm64 for macOS is version 10.16;
if (triple.isAArch64() && osVersion.getMajor() <= 10 &&
osVersion.getMinor().value_or(0) < 16) {
osVersion = llvm::VersionTuple(/*Major=*/10, /*Minor=*/16);
osVersion = canonicalizePlatformVersion(PlatformKind::macOS,
osVersion);
}
break;
case DarwinPlatformKind::IPhoneOS:
case DarwinPlatformKind::IPhoneOSSimulator:
case DarwinPlatformKind::TvOS:
case DarwinPlatformKind::TvOSSimulator:
osVersion = triple.getiOSVersion();
// The first deployment of arm64 simulators is iOS/tvOS 14.0;
// the linker doesn't want to see a deployment target before that.
if (triple.isSimulatorEnvironment() && triple.isAArch64() &&
osVersion.getMajor() < 14) {
osVersion = llvm::VersionTuple(/*Major=*/14, /*Minor=*/0);
}
break;
case DarwinPlatformKind::WatchOS:
case DarwinPlatformKind::WatchOSSimulator:
osVersion = triple.getOSVersion();
break;
case DarwinPlatformKind::VisionOS:
case DarwinPlatformKind::VisionOSSimulator:
osVersion = triple.getOSVersion();
// The first deployment of 64-bit xrOS simulator is version 1.0.
if (triple.isArch64Bit() && triple.isSimulatorEnvironment() &&
osVersion.getMajor() < 1) {
osVersion = llvm::VersionTuple(/*Major=*/1, /*Minor=*/0);
}
break;
}
}
// Compute the SDK version.
auto sdkVersion = getTargetSDKVersion(triple)
.value_or(llvm::VersionTuple());
Arguments.push_back("-platform_version");
Arguments.push_back(platformName);
addVersionString(context.Args, Arguments, osVersion);
addVersionString(context.Args, Arguments, sdkVersion);
};
addPlatformVersionArg(getTriple());
if (auto targetVariant = getTargetVariant()) {
assert(triplesAreValidForZippering(getTriple(), *targetVariant));
addPlatformVersionArg(*targetVariant);
}
}
static unsigned getDWARFVersionForTriple(const llvm::Triple &triple) {
llvm::VersionTuple osVersion;
const DarwinPlatformKind kind = getDarwinPlatformKind(triple);
// Default to DWARF 2 on OS X 10.10 / iOS 8 and lower.
// Default to DWARF 4 on OS X 10.11 - macOS 14 / iOS - iOS 17.
switch (kind) {
case DarwinPlatformKind::MacOS:
triple.getMacOSXVersion(osVersion);
if (osVersion < llvm::VersionTuple(10, 11))
return 2;
if (osVersion < llvm::VersionTuple(15))
return 4;
return 5;
case DarwinPlatformKind::IPhoneOSSimulator:
case DarwinPlatformKind::IPhoneOS:
case DarwinPlatformKind::TvOS:
case DarwinPlatformKind::TvOSSimulator:
osVersion = triple.getiOSVersion();
if (osVersion < llvm::VersionTuple(9))
return 2;
if (osVersion < llvm::VersionTuple(18))
return 4;
return 5;
case DarwinPlatformKind::WatchOS:
case DarwinPlatformKind::WatchOSSimulator:
osVersion = triple.getWatchOSVersion();
if (osVersion < llvm::VersionTuple(11))
return 4;
return 5;
case DarwinPlatformKind::VisionOS:
case DarwinPlatformKind::VisionOSSimulator:
osVersion = triple.getOSVersion();
if (osVersion < llvm::VersionTuple(2))
return 4;
return 5;
}
llvm_unreachable("unsupported platform kind");
}
void toolchains::Darwin::addCommonFrontendArgs(
const OutputInfo &OI, const CommandOutput &output,
const llvm::opt::ArgList &inputArgs,
llvm::opt::ArgStringList &arguments) const {
ToolChain::addCommonFrontendArgs(OI, output, inputArgs, arguments);
if (auto sdkVersion = getTargetSDKVersion(getTriple())) {
arguments.push_back("-target-sdk-version");
arguments.push_back(inputArgs.MakeArgString(sdkVersion->getAsString()));
}
if (auto targetVariant = getTargetVariant()) {
if (auto variantSDKVersion = getTargetSDKVersion(*targetVariant)) {
arguments.push_back("-target-variant-sdk-version");
arguments.push_back(
inputArgs.MakeArgString(variantSDKVersion->getAsString()));
}
}
std::string dwarfVersion;
{
llvm::raw_string_ostream os(dwarfVersion);
os << "-dwarf-version=";
if (OI.DWARFVersion)
os << *OI.DWARFVersion;
else
os << getDWARFVersionForTriple(getTriple());
}
arguments.push_back(inputArgs.MakeArgString(dwarfVersion));
}
/// Add the frontend arguments needed to find external plugins in standard
/// locations based on the base path.
static void addExternalPluginFrontendArgs(
StringRef basePath, const llvm::opt::ArgList &inputArgs,
llvm::opt::ArgStringList &arguments) {
// Plugin server: $BASE/usr/bin/swift-plugin-server
SmallString<128> pluginServer;
llvm::sys::path::append(
pluginServer, basePath, "usr", "bin", "swift-plugin-server");
SmallString<128> pluginDir;
llvm::sys::path::append(pluginDir, basePath, "usr", "lib");
llvm::sys::path::append(pluginDir, "swift", "host", "plugins");
arguments.push_back("-external-plugin-path");
arguments.push_back(inputArgs.MakeArgString(pluginDir + "#" + pluginServer));
pluginDir.clear();
llvm::sys::path::append(pluginDir, basePath, "usr", "local", "lib");
llvm::sys::path::append(pluginDir, "swift", "host", "plugins");
arguments.push_back("-external-plugin-path");
arguments.push_back(inputArgs.MakeArgString(pluginDir + "#" + pluginServer));
}
void toolchains::Darwin::addPlatformSpecificPluginFrontendArgs(
const OutputInfo &OI,
const CommandOutput &output,
const llvm::opt::ArgList &inputArgs,
llvm::opt::ArgStringList &arguments) const {
// Add SDK-relative directories for plugins.
if (!OI.SDKPath.empty()) {
addExternalPluginFrontendArgs(OI.SDKPath, inputArgs, arguments);
}
// Add platform-relative directories for plugins.
if (!OI.SDKPath.empty()) {
SmallString<128> platformPath;
llvm::sys::path::append(platformPath, OI.SDKPath);
llvm::sys::path::remove_filename(platformPath); // specific SDK
llvm::sys::path::remove_filename(platformPath); // SDKs
llvm::sys::path::remove_filename(platformPath); // Developer
StringRef platformName = llvm::sys::path::filename(platformPath);
if (platformName.endswith("Simulator.platform")){
StringRef devicePlatformName =
platformName.drop_back(strlen("Simulator.platform"));
llvm::sys::path::remove_filename(platformPath); // Platform
llvm::sys::path::append(platformPath, devicePlatformName + "OS.platform");
}
llvm::sys::path::append(platformPath, "Developer");
addExternalPluginFrontendArgs(platformPath, inputArgs, arguments);
}
}
ToolChain::InvocationInfo
toolchains::Darwin::constructInvocation(const DynamicLinkJobAction &job,
const JobContext &context) const {
assert(context.Output.getPrimaryOutputType() == file_types::TY_Image &&
"Invalid linker output type.");
if (context.Args.hasFlag(options::OPT_static_executable,
options::OPT_no_static_executable, false)) {
llvm::report_fatal_error("-static-executable is not supported on Darwin");
}
const llvm::Triple &Triple = getTriple();
// Configure the toolchain.
// By default, use the system `ld` to link.
const char *LD = "ld";
if (const Arg *A = context.Args.getLastArg(options::OPT_tools_directory)) {
StringRef toolchainPath(A->getValue());
// If there is a 'ld' in the toolchain folder, use that instead.
if (auto toolchainLD =
llvm::sys::findProgramByName("ld", {toolchainPath})) {
LD = context.Args.MakeArgString(toolchainLD.get());
}
}
InvocationInfo II = {LD};
ArgStringList &Arguments = II.Arguments;
addLinkerInputArgs(II, context);
switch (job.getKind()) {
case LinkKind::None:
llvm_unreachable("invalid link kind");
case LinkKind::Executable:
// The default for ld; no extra flags necessary.
break;
case LinkKind::DynamicLibrary:
Arguments.push_back("-dylib");
break;
case LinkKind::StaticLibrary:
llvm_unreachable("the dynamic linker cannot build static libraries");
}
assert(Triple.isOSDarwin());
// FIXME: If we used Clang as a linker instead of going straight to ld,
// we wouldn't have to replicate a bunch of Clang's logic here.
// Always link the regular compiler_rt if it's present.
//
// Note: Normally we'd just add this unconditionally, but it's valid to build
// Swift and use it as a linker without building compiler_rt.
SmallString<128> CompilerRTPath;
getClangLibraryPath(context.Args, CompilerRTPath);
llvm::sys::path::append(
CompilerRTPath,
Twine("libclang_rt.") +
getDarwinLibraryNameSuffixForTriple(Triple) +
".a");
if (llvm::sys::fs::exists(CompilerRTPath))
Arguments.push_back(context.Args.MakeArgString(CompilerRTPath));
if (job.shouldPerformLTO()) {
addLTOLibArgs(Arguments, context);
}
for (const Arg *arg :
context.Args.filtered(options::OPT_F, options::OPT_Fsystem)) {
Arguments.push_back("-F");
Arguments.push_back(arg->getValue());
}
if (context.Args.hasArg(options::OPT_enable_app_extension)) {
// Keep this string fixed in case the option used by the
// compiler itself changes.
Arguments.push_back("-application_extension");
}
addSanitizerArgs(Arguments, job, context);
if (context.Args.hasArg(options::OPT_embed_bitcode,
options::OPT_embed_bitcode_marker)) {
Arguments.push_back("-bitcode_bundle");
}
if (!context.OI.SDKPath.empty()) {
Arguments.push_back("-syslibroot");
Arguments.push_back(context.Args.MakeArgString(context.OI.SDKPath));
}
Arguments.push_back("-lobjc");
Arguments.push_back("-lSystem");
Arguments.push_back("-arch");
Arguments.push_back(context.Args.MakeArgString(getTriple().getArchName()));
// On Darwin, we only support libc++.
if (context.Args.hasArg(options::OPT_enable_experimental_cxx_interop)) {
Arguments.push_back("-lc++");
}
addArgsToLinkStdlib(Arguments, job, context);
addProfileGenerationArgs(Arguments, context);
addDeploymentTargetArgs(Arguments, context);
Arguments.push_back("-no_objc_category_merging");
// These custom arguments should be right before the object file at the end.
context.Args.AddAllArgsExcept(Arguments, {options::OPT_linker_option_Group},
{options::OPT_l});
ToolChain::addLinkedLibArgs(context.Args, Arguments);
context.Args.AddAllArgValues(Arguments, options::OPT_Xlinker);
// This should be the last option, for convenience in checking output.
Arguments.push_back("-o");
Arguments.push_back(
context.Args.MakeArgString(context.Output.getPrimaryOutputFilename()));
return II;
}
ToolChain::InvocationInfo
toolchains::Darwin::constructInvocation(const StaticLinkJobAction &job,
const JobContext &context) const {
assert(context.Output.getPrimaryOutputType() == file_types::TY_Image &&
"Invalid linker output type.");
// Configure the toolchain.
const char *LibTool = "libtool";
InvocationInfo II = {LibTool};
ArgStringList &Arguments = II.Arguments;
Arguments.push_back("-static");
if (context.shouldUseInputFileList()) {
Arguments.push_back("-filelist");
Arguments.push_back(context.getTemporaryFilePath("inputs", "LinkFileList"));
II.FilelistInfos.push_back({Arguments.back(), context.OI.CompilerOutputType,
FilelistInfo::WhichFiles::InputJobs});
} else {
addPrimaryInputsOfType(Arguments, context.Inputs, context.Args,
file_types::TY_Object);
addPrimaryInputsOfType(Arguments, context.Inputs, context.Args,
file_types::TY_LLVM_BC);
}
addInputsOfType(Arguments, context.InputActions, file_types::TY_Object);
addInputsOfType(Arguments, context.InputActions, file_types::TY_LLVM_BC);
Arguments.push_back("-o");
Arguments.push_back(
context.Args.MakeArgString(context.Output.getPrimaryOutputFilename()));
return II;
}
bool toolchains::Darwin::shouldStoreInvocationInDebugInfo() const {
// This matches the behavior in Clang (see
// clang/lib/driver/ToolChains/Darwin.cpp).
if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
return S[0] != '\0';
return false;
}
std::string toolchains::Darwin::getGlobalDebugPathRemapping() const {
// This matches the behavior in Clang (see
// clang/lib/driver/ToolChains/Darwin.cpp).
if (const char *S = ::getenv("RC_DEBUG_PREFIX_MAP"))
return S;
return {};
}
static void validateDeploymentTarget(const toolchains::Darwin &TC,
DiagnosticEngine &diags,
const llvm::opt::ArgList &args) {
// Check minimum supported OS versions.
auto triple = TC.getTriple();
if (triple.isMacOSX()) {
if (triple.isMacOSXVersionLT(10, 9))
diags.diagnose(SourceLoc(), diag::error_os_minimum_deployment,
"OS X 10.9");
} else if (triple.isiOS()) {
if (triple.isTvOS()) {
if (triple.isOSVersionLT(9, 0)) {
diags.diagnose(SourceLoc(), diag::error_os_minimum_deployment,
"tvOS 9.0");
return;
}
}
if (triple.isOSVersionLT(7))
diags.diagnose(SourceLoc(), diag::error_os_minimum_deployment,
"iOS 7");
if (triple.isArch32Bit() && !triple.isOSVersionLT(11)) {
diags.diagnose(SourceLoc(), diag::error_ios_maximum_deployment_32,
triple.getOSMajorVersion());
}
} else if (triple.isWatchOS()) {
if (triple.isOSVersionLT(2, 0)) {
diags.diagnose(SourceLoc(), diag::error_os_minimum_deployment,
"watchOS 2.0");
return;
}
}
}
static void validateTargetVariant(const toolchains::Darwin &TC,
DiagnosticEngine &diags,
const llvm::opt::ArgList &args,
StringRef defaultTarget) {
if (TC.getTargetVariant().has_value()) {
auto target = TC.getTriple();
auto variant = *TC.getTargetVariant();
if (!triplesAreValidForZippering(target, variant)) {
diags.diagnose(SourceLoc(), diag::error_unsupported_target_variant,
variant.str(),
variant.isiOS());
}
}
}
void
toolchains::Darwin::validateArguments(DiagnosticEngine &diags,
const llvm::opt::ArgList &args,
StringRef defaultTarget) const {
// Validating apple platforms deployment targets.
validateDeploymentTarget(*this, diags, args);
validateTargetVariant(*this, diags, args, defaultTarget);
// Validating darwin unsupported -static-stdlib argument.
if (args.hasArg(options::OPT_static_stdlib)) {
diags.diagnose(SourceLoc(), diag::error_darwin_static_stdlib_not_supported);
}
// Validating darwin deprecated -link-objc-runtime.
if (args.hasArg(options::OPT_link_objc_runtime,
options::OPT_no_link_objc_runtime)) {
diags.diagnose(SourceLoc(), diag::warn_darwin_link_objc_deprecated);
}
// If a C++ standard library is specified, it has to be libc++.
if (auto arg = args.getLastArg(options::OPT_experimental_cxx_stdlib)) {
if (StringRef(arg->getValue()) != "libc++") {
diags.diagnose(SourceLoc(), diag::error_darwin_only_supports_libcxx);
}
}
}
void
toolchains::Darwin::validateOutputInfo(DiagnosticEngine &diags,
const OutputInfo &outputInfo) const {
// If we have been provided with an SDK, go read the SDK information.
if (!outputInfo.SDKPath.empty()) {
auto SDKInfoOrErr = clang::parseDarwinSDKInfo(
*llvm::vfs::getRealFileSystem(), outputInfo.SDKPath);
if (SDKInfoOrErr) {
SDKInfo = *SDKInfoOrErr;
} else {
llvm::consumeError(SDKInfoOrErr.takeError());
diags.diagnose(SourceLoc(), diag::warn_drv_darwin_sdk_invalid_settings);
}
}
}
|