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 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basics
import Foundation
import class Basics.AsyncProcess
#if os(Windows)
private let hostExecutableSuffix = ".exe"
#else
private let hostExecutableSuffix = ""
#endif
// FIXME: This is messy and needs a redesign.
public final class UserToolchain: Toolchain {
public typealias SwiftCompilers = (compile: AbsolutePath, manifest: AbsolutePath)
/// The toolchain configuration.
private let configuration: ToolchainConfiguration
/// Path of the librarian.
public let librarianPath: AbsolutePath
/// Path of the `swiftc` compiler.
public let swiftCompilerPath: AbsolutePath
/// An array of paths to search for headers and modules at compile time.
public let includeSearchPaths: [AbsolutePath]
/// An array of paths to search for libraries at link time.
public let librarySearchPaths: [AbsolutePath]
/// Path containing Swift resources for dynamic linking.
public var swiftResourcesPath: AbsolutePath? {
swiftSDK.pathsConfiguration.swiftResourcesPath
}
/// Path containing Swift resources for static linking.
public var swiftStaticResourcesPath: AbsolutePath? {
swiftSDK.pathsConfiguration.swiftStaticResourcesPath
}
/// Additional flags to be passed to the build tools.
public var extraFlags: BuildFlags
/// Path of the `swift` interpreter.
public var swiftInterpreterPath: AbsolutePath {
self.swiftCompilerPath.parentDirectory.appending("swift" + hostExecutableSuffix)
}
private let fileSystem: any FileSystem
/// The compilation destination object.
@available(*, deprecated, renamed: "swiftSDK")
public var destination: SwiftSDK { swiftSDK }
/// The Swift SDK used by this toolchain.
public let swiftSDK: SwiftSDK
/// The target triple that should be used for compilation.
@available(*, deprecated, renamed: "targetTriple")
public var triple: Triple { targetTriple }
public let targetTriple: Triple
/// The list of CPU architectures to build for.
public let architectures: [String]?
/// Search paths from the PATH environment variable.
let envSearchPaths: [AbsolutePath]
/// Only use search paths, do not fall back to `xcrun`.
let useXcrun: Bool
private var _clangCompiler: AbsolutePath?
private let environment: Environment
public let installedSwiftPMConfiguration: InstalledSwiftPMConfiguration
/// Returns the runtime library for the given sanitizer.
public func runtimeLibrary(for sanitizer: Sanitizer) throws -> AbsolutePath {
// FIXME: This is only for SwiftPM development time support. It is OK
// for now but we shouldn't need to resolve the symlink. We need to lay
// down symlinks to runtimes in our fake toolchain as part of the
// bootstrap script.
let swiftCompiler = try resolveSymlinks(self.swiftCompilerPath)
let runtime = try swiftCompiler.appending(
RelativePath(validating: "../../lib/swift/clang/lib/darwin/libclang_rt.\(sanitizer.shortName)_osx_dynamic.dylib")
)
// Ensure that the runtime is present.
guard fileSystem.exists(runtime) else {
throw InvalidToolchainDiagnostic("Missing runtime for \(sanitizer) sanitizer")
}
return runtime
}
// MARK: - private utilities
private static func lookup(
variable: String,
searchPaths: [AbsolutePath],
environment: Environment
) -> AbsolutePath? {
lookupExecutablePath(filename: environment[.init(variable)], searchPaths: searchPaths)
}
private static func getTool(
_ name: String,
binDirectories: [AbsolutePath],
fileSystem: any FileSystem
) throws -> AbsolutePath {
let executableName = "\(name)\(hostExecutableSuffix)"
var toolPath: AbsolutePath?
for dir in binDirectories {
let path = dir.appending(component: executableName)
guard fileSystem.isExecutableFile(path) else {
continue
}
toolPath = path
// Take the first match.
break
}
guard let toolPath else {
throw InvalidToolchainDiagnostic("could not find CLI tool `\(name)` at any of these directories: \(binDirectories)")
}
return toolPath
}
private static func findTool(
_ name: String,
envSearchPaths: [AbsolutePath],
useXcrun: Bool,
fileSystem: any FileSystem
) throws -> AbsolutePath {
if useXcrun {
#if os(macOS)
let foundPath = try AsyncProcess.checkNonZeroExit(arguments: ["/usr/bin/xcrun", "--find", name])
.spm_chomp()
return try AbsolutePath(validating: foundPath)
#endif
}
return try getTool(name, binDirectories: envSearchPaths, fileSystem: fileSystem)
}
// MARK: - public API
public static func determineLibrarian(
triple: Triple,
binDirectories: [AbsolutePath],
useXcrun: Bool,
environment: Environment,
searchPaths: [AbsolutePath],
extraSwiftFlags: [String],
fileSystem: any FileSystem
) throws -> AbsolutePath {
let variable: String = triple.isApple() ? "LIBTOOL" : "AR"
let tool: String = {
if triple.isApple() { return "libtool" }
if triple.isWindows() {
if let librarian: AbsolutePath =
UserToolchain.lookup(
variable: "AR",
searchPaths: searchPaths,
environment: environment
)
{
return librarian.basename
}
// TODO(5719) handle `-Xmanifest` vs `-Xswiftc`
// `-use-ld=` is always joined in Swift.
if let ld = extraSwiftFlags.first(where: { $0.starts(with: "-use-ld=") }) {
let linker = String(ld.split(separator: "=").last!)
return linker == "lld" ? "lld-link" : linker
}
return "link"
}
return "llvm-ar"
}()
if let librarian = UserToolchain.lookup(
variable: variable,
searchPaths: searchPaths,
environment: environment
) {
if fileSystem.isExecutableFile(librarian) {
return librarian
}
}
if let librarian = try? UserToolchain.getTool(tool, binDirectories: binDirectories, fileSystem: fileSystem) {
return librarian
}
if triple.isApple() || triple.isWindows() {
return try UserToolchain.findTool(tool, envSearchPaths: searchPaths, useXcrun: useXcrun, fileSystem: fileSystem)
} else {
if let librarian = try? UserToolchain.findTool(tool, envSearchPaths: searchPaths, useXcrun: false, fileSystem: fileSystem) {
return librarian
}
// Fall back to looking for binutils `ar` if `llvm-ar` can't be found.
if let librarian = try? UserToolchain.getTool("ar", binDirectories: binDirectories, fileSystem: fileSystem) {
return librarian
}
return try UserToolchain.findTool("ar", envSearchPaths: searchPaths, useXcrun: false, fileSystem: fileSystem)
}
}
/// Determines the Swift compiler paths for compilation and manifest parsing.
public static func determineSwiftCompilers(
binDirectories: [AbsolutePath],
useXcrun: Bool,
environment: Environment,
searchPaths: [AbsolutePath],
fileSystem: any FileSystem
) throws -> SwiftCompilers {
func validateCompiler(at path: AbsolutePath?) throws {
guard let path else { return }
guard fileSystem.isExecutableFile(path) else {
throw InvalidToolchainDiagnostic(
"could not find the `swiftc\(hostExecutableSuffix)` at expected path \(path)"
)
}
}
let lookup = { UserToolchain.lookup(variable: $0, searchPaths: searchPaths, environment: environment) }
// Get overrides.
let SWIFT_EXEC_MANIFEST = lookup("SWIFT_EXEC_MANIFEST")
let SWIFT_EXEC = lookup("SWIFT_EXEC")
// Validate the overrides.
try validateCompiler(at: SWIFT_EXEC)
try validateCompiler(at: SWIFT_EXEC_MANIFEST)
// We require there is at least one valid swift compiler, either in the
// bin dir or SWIFT_EXEC.
let resolvedBinDirCompiler: AbsolutePath
if let SWIFT_EXEC {
resolvedBinDirCompiler = SWIFT_EXEC
} else if let binDirCompiler = try? UserToolchain.getTool("swiftc", binDirectories: binDirectories, fileSystem: fileSystem) {
resolvedBinDirCompiler = binDirCompiler
} else {
// Try to lookup swift compiler on the system which is possible when
// we're built outside of the Swift toolchain.
resolvedBinDirCompiler = try UserToolchain.findTool(
"swiftc",
envSearchPaths: searchPaths,
useXcrun: useXcrun,
fileSystem: fileSystem
)
}
// The compiler for compilation tasks is SWIFT_EXEC or the bin dir compiler.
// The compiler for manifest is either SWIFT_EXEC_MANIFEST or the bin dir compiler.
return (compile: SWIFT_EXEC ?? resolvedBinDirCompiler, manifest: SWIFT_EXEC_MANIFEST ?? resolvedBinDirCompiler)
}
/// Returns the path to clang compiler tool.
public func getClangCompiler() throws -> AbsolutePath {
// Check if we already computed.
if let clang = self._clangCompiler {
return clang
}
// Check in the environment variable first.
if let toolPath = UserToolchain.lookup(
variable: "CC",
searchPaths: self.envSearchPaths,
environment: environment
) {
self._clangCompiler = toolPath
return toolPath
}
// Then, check the toolchain.
if let toolPath = try? UserToolchain.getTool(
"clang",
binDirectories: self.swiftSDK.toolset.rootPaths,
fileSystem: self.fileSystem
) {
self._clangCompiler = toolPath
return toolPath
}
// Otherwise, lookup it up on the system.
let toolPath = try UserToolchain.findTool(
"clang",
envSearchPaths: self.envSearchPaths,
useXcrun: useXcrun,
fileSystem: self.fileSystem
)
self._clangCompiler = toolPath
return toolPath
}
public func _isClangCompilerVendorApple() throws -> Bool? {
// Assume the vendor is Apple on macOS.
// FIXME: This might not be the best way to determine this.
#if os(macOS)
return true
#else
return false
#endif
}
/// Returns the path to lldb.
public func getLLDB() throws -> AbsolutePath {
// Look for LLDB next to the compiler first.
if let lldbPath = try? UserToolchain.getTool(
"lldb",
binDirectories: [self.swiftCompilerPath.parentDirectory],
fileSystem: self.fileSystem
) {
return lldbPath
}
// If that fails, fall back to xcrun, PATH, etc.
return try UserToolchain.findTool(
"lldb",
envSearchPaths: self.envSearchPaths,
useXcrun: useXcrun,
fileSystem: self.fileSystem
)
}
/// Returns the path to llvm-cov tool.
public func getLLVMCov() throws -> AbsolutePath {
try UserToolchain.getTool(
"llvm-cov",
binDirectories: [self.swiftCompilerPath.parentDirectory],
fileSystem: self.fileSystem
)
}
/// Returns the path to llvm-prof tool.
public func getLLVMProf() throws -> AbsolutePath {
try UserToolchain.getTool(
"llvm-profdata",
binDirectories: [self.swiftCompilerPath.parentDirectory],
fileSystem: self.fileSystem
)
}
public func getSwiftAPIDigester() throws -> AbsolutePath {
if let envValue = UserToolchain.lookup(
variable: "SWIFT_API_DIGESTER",
searchPaths: self.envSearchPaths,
environment: environment
) {
return envValue
}
return try UserToolchain.getTool(
"swift-api-digester",
binDirectories: [self.swiftCompilerPath.parentDirectory],
fileSystem: self.fileSystem
)
}
public func getSymbolGraphExtract() throws -> AbsolutePath {
if let envValue = UserToolchain.lookup(
variable: "SWIFT_SYMBOLGRAPH_EXTRACT",
searchPaths: self.envSearchPaths,
environment: environment
) {
return envValue
}
return try UserToolchain.getTool(
"swift-symbolgraph-extract",
binDirectories: [self.swiftCompilerPath.parentDirectory],
fileSystem: self.fileSystem
)
}
#if os(macOS)
public func getSwiftTestingHelper() throws -> AbsolutePath {
// The helper would be located in `.build/<config>` directory when
// SwiftPM is built locally and `usr/libexec/swift/pm` directory in
// an installed version.
let binDirectories = self.swiftSDK.toolset.rootPaths +
self.swiftSDK.toolset.rootPaths.map {
$0.parentDirectory.appending(components: ["libexec", "swift", "pm"])
}
return try UserToolchain.getTool(
"swiftpm-testing-helper",
binDirectories: binDirectories,
fileSystem: self.fileSystem
)
}
#endif
/// On MacOS toolchain can shadow SDK content. This method is intended
/// to locate and include swift-testing library from a toolchain before
/// sdk content which to sure that builds that use a custom toolchain
/// always get a custom swift-testing library as well.
static func deriveMacOSSpecificSwiftTestingFlags(
derivedSwiftCompiler: AbsolutePath,
fileSystem: any FileSystem
) -> (swiftCFlags: [String], linkerFlags: [String]) {
// If this is CommandLineTools all we need to add is a frameworks path.
if let frameworksPath = try? AbsolutePath(
validating: "../../Library/Developer/Frameworks",
relativeTo: resolveSymlinks(derivedSwiftCompiler).parentDirectory
), fileSystem.exists(frameworksPath.appending("Testing.framework")) {
return (swiftCFlags: [
"-F", frameworksPath.pathString
], linkerFlags: [
"-rpath", frameworksPath.pathString
])
}
guard let toolchainLibDir = try? toolchainLibDir(
swiftCompilerPath: derivedSwiftCompiler
) else {
return (swiftCFlags: [], linkerFlags: [])
}
let testingLibDir = toolchainLibDir.appending(
components: ["swift", "macosx", "testing"]
)
let testingPluginsDir = toolchainLibDir.appending(
components: ["swift", "host", "plugins", "testing"]
)
guard fileSystem.exists(testingLibDir), fileSystem.exists(testingPluginsDir) else {
return (swiftCFlags: [], linkerFlags: [])
}
return (swiftCFlags: [
"-I", testingLibDir.pathString,
"-L", testingLibDir.pathString,
"-plugin-path", testingPluginsDir.pathString
], linkerFlags: [
"-rpath", testingLibDir.pathString
])
}
internal static func deriveSwiftCFlags(
triple: Triple,
swiftSDK: SwiftSDK,
environment: Environment,
fileSystem: any FileSystem
) throws -> [String] {
var swiftCompilerFlags = swiftSDK.toolset.knownTools[.swiftCompiler]?.extraCLIOptions ?? []
if let linker = swiftSDK.toolset.knownTools[.linker]?.path {
swiftCompilerFlags += ["-ld-path=\(linker)"]
}
guard let sdkDir = swiftSDK.pathsConfiguration.sdkRootPath else {
if triple.isWindows() {
// Windows uses a variable named SDKROOT to determine the root of
// the SDK. This is not the same value as the SDKROOT parameter
// in Xcode, however, the value represents a similar concept.
if let sdkroot = environment.windowsSDKRoot {
var runtime: [String] = []
var xctest: [String] = []
var swiftTesting: [String] = []
var extraSwiftCFlags: [String] = []
if let settings = WindowsSDKSettings(
reading: sdkroot.appending("SDKSettings.plist"),
observabilityScope: nil,
filesystem: fileSystem
) {
switch settings.defaults.runtime {
case .multithreadedDebugDLL:
runtime = ["-libc", "MDd"]
case .multithreadedDLL:
runtime = ["-libc", "MD"]
case .multithreadedDebug:
runtime = ["-libc", "MTd"]
case .multithreaded:
runtime = ["-libc", "MT"]
}
}
// The layout of the SDK is as follows:
//
// Library/Developer/Platforms/[PLATFORM].platform/Developer/Library/<Project>-[VERSION]/...
// Library/Developer/Platforms/[PLATFORM].platform/Developer/SDKs/[PLATFORM].sdk/...
//
// SDKROOT points to [PLATFORM].sdk
let platform = sdkroot.parentDirectory.parentDirectory.parentDirectory
if let info = WindowsPlatformInfo(
reading: platform.appending("Info.plist"),
observabilityScope: nil,
filesystem: fileSystem
) {
let XCTestInstallation: AbsolutePath =
platform.appending("Developer")
.appending("Library")
.appending("XCTest-\(info.defaults.xctestVersion)")
xctest = try [
"-I",
AbsolutePath(
validating: "usr/lib/swift/windows",
relativeTo: XCTestInstallation
).pathString,
// Migration Path
//
// Older Swift (<=5.7) installations placed the
// XCTest Swift module into the architecture
// specified directory. This was in order to match
// the SDK setup. However, the toolchain finally
// gained the ability to consult the architecture
// independent directory for Swift modules, allowing
// the merged swiftmodules. XCTest followed suit.
"-I",
AbsolutePath(
validating: "usr/lib/swift/windows/\(triple.archName)",
relativeTo: XCTestInstallation
).pathString,
"-L",
AbsolutePath(
validating: "usr/lib/swift/windows/\(triple.archName)",
relativeTo: XCTestInstallation
).pathString,
]
// Migration Path
//
// In order to support multiple parallel installations
// of an SDK, we need to ensure that we can have all the
// architecture variant libraries available. Prior to
// this getting enabled (~5.7), we always had a singular
// installed SDK. Prefer the new variant which has an
// architecture subdirectory in `bin` if available.
let implib = try AbsolutePath(
validating: "usr/lib/swift/windows/XCTest.lib",
relativeTo: XCTestInstallation
)
if fileSystem.exists(implib) {
xctest.append(contentsOf: ["-L", implib.parentDirectory.pathString])
}
if let swiftTestingVersion = info.defaults.swiftTestingVersion {
let swiftTestingInstallation: AbsolutePath =
platform.appending("Developer")
.appending("Library")
.appending("Testing-\(swiftTestingVersion)")
swiftTesting = try [
"-I",
AbsolutePath(
validating: "usr/lib/swift/windows",
relativeTo: swiftTestingInstallation
).pathString,
"-L",
AbsolutePath(
validating: "usr/lib/swift/windows/\(triple.archName)",
relativeTo: swiftTestingInstallation
).pathString
]
}
extraSwiftCFlags = info.defaults.extraSwiftCFlags ?? []
}
return ["-sdk", sdkroot.pathString] + runtime + xctest + swiftTesting + extraSwiftCFlags
}
}
return swiftCompilerFlags
}
return (
triple.isDarwin() || triple.isAndroid() || triple.isWASI() || triple.isWindows()
? ["-sdk", sdkDir.pathString]
: []
) + swiftCompilerFlags
}
// MARK: - initializer
public enum SearchStrategy {
case `default`
case custom(searchPaths: [AbsolutePath], useXcrun: Bool = true)
}
@available(*, deprecated, message: "use init(swiftSDK:environment:searchStrategy:customLibrariesLocation) instead")
public convenience init(
destination: SwiftSDK,
environment: Environment = .current,
searchStrategy: SearchStrategy = .default,
customLibrariesLocation: ToolchainConfiguration.SwiftPMLibrariesLocation? = nil
) throws {
try self.init(
swiftSDK: destination,
environment: environment,
searchStrategy: searchStrategy,
customLibrariesLocation: customLibrariesLocation,
fileSystem: localFileSystem
)
}
public init(
swiftSDK: SwiftSDK,
environment: Environment = .current,
searchStrategy: SearchStrategy = .default,
customLibrariesLocation: ToolchainConfiguration.SwiftPMLibrariesLocation? = nil,
customInstalledSwiftPMConfiguration: InstalledSwiftPMConfiguration? = nil,
fileSystem: any FileSystem = localFileSystem
) throws {
self.swiftSDK = swiftSDK
self.environment = environment
switch searchStrategy {
case .default:
// Get the search paths from PATH.
self.envSearchPaths = getEnvSearchPaths(
pathString: environment[.path],
currentWorkingDirectory: fileSystem.currentWorkingDirectory
)
self.useXcrun = true
case .custom(let searchPaths, let useXcrun):
self.envSearchPaths = searchPaths
self.useXcrun = useXcrun
}
let swiftCompilers = try UserToolchain.determineSwiftCompilers(
binDirectories: swiftSDK.toolset.rootPaths,
useXcrun: self.useXcrun,
environment: environment,
searchPaths: self.envSearchPaths,
fileSystem: fileSystem
)
self.swiftCompilerPath = swiftCompilers.compile
self.architectures = swiftSDK.architectures
if let customInstalledSwiftPMConfiguration {
self.installedSwiftPMConfiguration = customInstalledSwiftPMConfiguration
} else {
let path = self.swiftCompilerPath.parentDirectory.parentDirectory.appending(components: ["share", "pm", "config.json"])
if localFileSystem.exists(path) {
self.installedSwiftPMConfiguration = try JSONDecoder.makeWithDefaults().decode(path: path, fileSystem: localFileSystem, as: InstalledSwiftPMConfiguration.self)
} else {
// We *could* eventually make this an error, but not for a few releases.
self.installedSwiftPMConfiguration = InstalledSwiftPMConfiguration.default
}
}
// Use the triple from Swift SDK or compute the host triple using swiftc.
var triple = try swiftSDK.targetTriple ?? Triple.getHostTriple(usingSwiftCompiler: swiftCompilers.compile)
// Change the triple to the specified arch if there's exactly one of them.
// The Triple property is only looked at by the native build system currently.
if let architectures = self.architectures, architectures.count == 1 {
let components = triple.tripleString.drop(while: { $0 != "-" })
triple = try Triple(architectures[0] + components)
}
self.targetTriple = triple
var swiftCompilerFlags: [String] = []
var extraLinkerFlags: [String] = []
if triple.isMacOSX {
let (swiftCFlags, linkerFlags) = Self.deriveMacOSSpecificSwiftTestingFlags(
derivedSwiftCompiler: swiftCompilers.compile,
fileSystem: fileSystem
)
swiftCompilerFlags += swiftCFlags
extraLinkerFlags += linkerFlags
}
swiftCompilerFlags += try Self.deriveSwiftCFlags(
triple: triple,
swiftSDK: swiftSDK,
environment: environment,
fileSystem: fileSystem
)
extraLinkerFlags += swiftSDK.toolset.knownTools[.linker]?.extraCLIOptions ?? []
self.extraFlags = BuildFlags(
cCompilerFlags: swiftSDK.toolset.knownTools[.cCompiler]?.extraCLIOptions ?? [],
cxxCompilerFlags: swiftSDK.toolset.knownTools[.cxxCompiler]?.extraCLIOptions ?? [],
swiftCompilerFlags: swiftCompilerFlags,
linkerFlags: extraLinkerFlags,
xcbuildFlags: swiftSDK.toolset.knownTools[.xcbuild]?.extraCLIOptions ?? [])
self.includeSearchPaths = swiftSDK.pathsConfiguration.includeSearchPaths ?? []
self.librarySearchPaths = swiftSDK.pathsConfiguration.includeSearchPaths ?? []
self.librarianPath = try swiftSDK.toolset.knownTools[.librarian]?.path ?? UserToolchain.determineLibrarian(
triple: triple,
binDirectories: swiftSDK.toolset.rootPaths,
useXcrun: useXcrun,
environment: environment,
searchPaths: envSearchPaths,
extraSwiftFlags: self.extraFlags.swiftCompilerFlags,
fileSystem: fileSystem
)
if let sdkDir = swiftSDK.pathsConfiguration.sdkRootPath {
let sysrootFlags = [triple.isDarwin() ? "-isysroot" : "--sysroot", sdkDir.pathString]
self.extraFlags.cCompilerFlags.insert(contentsOf: sysrootFlags, at: 0)
}
if triple.isWindows() {
if let root = environment.windowsSDKRoot {
if let settings = WindowsSDKSettings(
reading: root.appending("SDKSettings.plist"),
observabilityScope: nil,
filesystem: fileSystem
) {
switch settings.defaults.runtime {
case .multithreadedDebugDLL:
// Defines _DEBUG, _MT, and _DLL
// Linker uses MSVCRTD.lib
self.extraFlags.cCompilerFlags += [
"-D_DEBUG",
"-D_MT",
"-D_DLL",
"-Xclang",
"--dependent-lib=msvcrtd",
]
case .multithreadedDLL:
// Defines _MT, and _DLL
// Linker uses MSVCRT.lib
self.extraFlags.cCompilerFlags += ["-D_MT", "-D_DLL", "-Xclang", "--dependent-lib=msvcrt"]
case .multithreadedDebug:
// Defines _DEBUG, and _MT
// Linker uses LIBCMTD.lib
self.extraFlags.cCompilerFlags += ["-D_DEBUG", "-D_MT", "-Xclang", "--dependent-lib=libcmtd"]
case .multithreaded:
// Defines _MT
// Linker uses LIBCMT.lib
self.extraFlags.cCompilerFlags += ["-D_MT", "-Xclang", "--dependent-lib=libcmt"]
}
}
}
}
let swiftPMLibrariesLocation = try customLibrariesLocation ?? Self.deriveSwiftPMLibrariesLocation(
swiftCompilerPath: swiftCompilerPath,
swiftSDK: swiftSDK,
environment: environment,
fileSystem: fileSystem
)
let xctestPath: AbsolutePath?
if case .custom(_, let useXcrun) = searchStrategy, !useXcrun {
xctestPath = nil
} else {
xctestPath = try Self.deriveXCTestPath(
swiftSDK: self.swiftSDK,
triple: triple,
environment: environment,
fileSystem: fileSystem
)
}
let swiftTestingPath: AbsolutePath?
if case .custom(_, let useXcrun) = searchStrategy, !useXcrun {
swiftTestingPath = nil
} else {
swiftTestingPath = try Self.deriveSwiftTestingPath(
swiftSDK: self.swiftSDK,
triple: triple,
environment: environment,
fileSystem: fileSystem
)
}
self.configuration = .init(
librarianPath: librarianPath,
swiftCompilerPath: swiftCompilers.manifest,
swiftCompilerFlags: self.extraFlags.swiftCompilerFlags,
swiftCompilerEnvironment: environment,
swiftPMLibrariesLocation: swiftPMLibrariesLocation,
sdkRootPath: self.swiftSDK.pathsConfiguration.sdkRootPath,
xctestPath: xctestPath,
swiftTestingPath: swiftTestingPath
)
self.fileSystem = fileSystem
}
private static func deriveSwiftPMLibrariesLocation(
swiftCompilerPath: AbsolutePath,
swiftSDK: SwiftSDK,
environment: Environment,
fileSystem: any FileSystem
) throws -> ToolchainConfiguration.SwiftPMLibrariesLocation? {
// Look for an override in the env.
if let pathEnvVariable = environment["SWIFTPM_CUSTOM_LIBS_DIR"] ?? environment["SWIFTPM_PD_LIBS"] {
if environment["SWIFTPM_PD_LIBS"] != nil {
print("SWIFTPM_PD_LIBS was deprecated in favor of SWIFTPM_CUSTOM_LIBS_DIR")
}
// We pick the first path which exists in an environment variable
// delimited by the platform specific string separator.
#if os(Windows)
let separator: Character = ";"
#else
let separator: Character = ":"
#endif
let paths = pathEnvVariable.split(separator: separator).map(String.init)
for pathString in paths {
if let path = try? AbsolutePath(validating: pathString), fileSystem.exists(path) {
// we found the custom one
return .init(root: path)
}
}
// fail if custom one specified but not found
throw InternalError(
"Couldn't find the custom libraries location defined by SWIFTPM_CUSTOM_LIBS_DIR / SWIFTPM_PD_LIBS: \(pathEnvVariable)"
)
}
// FIXME: the following logic is pretty fragile, but has always been this way
// an alternative cloud be to force explicit locations to always be set explicitly when running in Xcode/SwiftPM
// debug and assert if not set but we detect that we are in this mode
for applicationPath in swiftSDK.toolset.rootPaths {
// this is the normal case when using the toolchain
let librariesPath = applicationPath.parentDirectory.appending(components: "lib", "swift", "pm")
if fileSystem.exists(librariesPath) {
return .init(root: librariesPath)
}
// this tests if we are debugging / testing SwiftPM with Xcode
let manifestFrameworksPath = applicationPath.appending(
components: "PackageFrameworks",
"PackageDescription.framework"
)
let pluginFrameworksPath = applicationPath.appending(components: "PackageFrameworks", "PackagePlugin.framework")
if fileSystem.exists(manifestFrameworksPath), fileSystem.exists(pluginFrameworksPath) {
return .init(
manifestLibraryPath: manifestFrameworksPath,
pluginLibraryPath: pluginFrameworksPath
)
}
// this tests if we are debugging / testing SwiftPM with SwiftPM
if localFileSystem.exists(applicationPath.appending("swift-package")) {
// Newer versions of SwiftPM will emit modules to a "Modules" subdirectory, but we're also staying compatible with older versions for development.
let modulesPath: AbsolutePath
if localFileSystem.exists(applicationPath.appending("Modules")) {
modulesPath = applicationPath.appending("Modules")
} else {
modulesPath = applicationPath
}
return .init(
manifestLibraryPath: applicationPath,
manifestModulesPath: modulesPath,
pluginLibraryPath: applicationPath,
pluginModulesPath: modulesPath
)
}
}
// we are using a SwiftPM outside a toolchain, use the compiler path to compute the location
return .init(swiftCompilerPath: swiftCompilerPath)
}
private static func derivePluginServerPath(triple: Triple) throws -> AbsolutePath? {
if triple.isDarwin() {
let pluginServerPathFindArgs = ["/usr/bin/xcrun", "--find", "swift-plugin-server"]
if let path = try? AsyncProcess.checkNonZeroExit(arguments: pluginServerPathFindArgs, environment: [:])
.spm_chomp() {
return try AbsolutePath(validating: path)
}
}
return .none
}
private static func getWindowsPlatformInfo(
swiftSDK: SwiftSDK,
environment: Environment,
fileSystem: any FileSystem
) -> (AbsolutePath, WindowsPlatformInfo)? {
let sdkRoot: AbsolutePath? = if let sdkDir = swiftSDK.pathsConfiguration.sdkRootPath {
sdkDir
} else if let sdkDir = environment.windowsSDKRoot {
sdkDir
} else {
nil
}
guard let sdkRoot else {
return nil
}
// The layout of the SDK is as follows:
//
// Library/Developer/Platforms/[PLATFORM].platform/Developer/Library/<Project>-[VERSION]/...
// Library/Developer/Platforms/[PLATFORM].platform/Developer/SDKs/[PLATFORM].sdk/...
//
// SDKROOT points to [PLATFORM].sdk
let platform = sdkRoot.parentDirectory.parentDirectory.parentDirectory
guard let info = WindowsPlatformInfo(
reading: platform.appending("Info.plist"),
observabilityScope: nil,
filesystem: fileSystem
) else {
return nil
}
return (platform, info)
}
// TODO: We should have some general utility to find tools.
private static func deriveXCTestPath(
swiftSDK: SwiftSDK,
triple: Triple,
environment: Environment,
fileSystem: any FileSystem
) throws -> AbsolutePath? {
if triple.isDarwin() {
// XCTest is optional on macOS, for example when Xcode is not installed
let xctestFindArgs = ["/usr/bin/xcrun", "--sdk", "macosx", "--find", "xctest"]
if let path = try? AsyncProcess.checkNonZeroExit(arguments: xctestFindArgs, environment: environment)
.spm_chomp()
{
return try AbsolutePath(validating: path)
}
} else if triple.isWindows() {
if let (platform, info) = getWindowsPlatformInfo(
swiftSDK: swiftSDK,
environment: environment,
fileSystem: fileSystem
) {
let xctest: AbsolutePath =
platform.appending("Developer")
.appending("Library")
.appending("XCTest-\(info.defaults.xctestVersion)")
// Migration Path
//
// In order to support multiple parallel installations of an
// SDK, we need to ensure that we can have all the architecture
// variant libraries available. Prior to this getting enabled
// (~5.7), we always had a singular installed SDK. Prefer the
// new variant which has an architecture subdirectory in `bin`
// if available.
switch triple.arch {
case .x86_64: // amd64 x86_64 x86_64h
let path: AbsolutePath =
xctest.appending("usr")
.appending("bin64")
if fileSystem.exists(path) {
return path
}
case .x86: // i386 i486 i586 i686 i786 i886 i986
let path: AbsolutePath =
xctest.appending("usr")
.appending("bin32")
if fileSystem.exists(path) {
return path
}
case .arm: // armv7 and many more
let path: AbsolutePath =
xctest.appending("usr")
.appending("bin32a")
if fileSystem.exists(path) {
return path
}
case .aarch64: // aarch6 arm64
let path: AbsolutePath =
xctest.appending("usr")
.appending("bin64a")
if fileSystem.exists(path) {
return path
}
default:
// Fallback to the old-style layout. We should really
// report an error in this case - this architecture is
// unavailable.
break
}
// Assume that we are in the old-style layout.
return xctest.appending("usr")
.appending("bin")
}
}
return .none
}
private static func deriveSwiftTestingPath(
swiftSDK: SwiftSDK,
triple: Triple,
environment: Environment,
fileSystem: any FileSystem
) throws -> AbsolutePath? {
guard triple.isWindows() else {
return nil
}
guard let (platform, info) = getWindowsPlatformInfo(
swiftSDK: swiftSDK,
environment: environment,
fileSystem: fileSystem
) else {
return nil
}
guard let swiftTestingVersion = info.defaults.swiftTestingVersion else {
return nil
}
let swiftTesting: AbsolutePath =
platform.appending("Developer")
.appending("Library")
.appending("Testing-\(swiftTestingVersion)")
let binPath: AbsolutePath? = switch triple.arch {
case .x86_64: // amd64 x86_64 x86_64h
swiftTesting.appending("usr")
.appending("bin64")
case .x86: // i386 i486 i586 i686 i786 i886 i986
swiftTesting.appending("usr")
.appending("bin32")
case .arm: // armv7 and many more
swiftTesting.appending("usr")
.appending("bin32a")
case .aarch64: // aarch6 arm64
swiftTesting.appending("usr")
.appending("bin64a")
default:
nil
}
guard let path = binPath, fileSystem.exists(path) else {
return nil
}
return path
}
public var sdkRootPath: AbsolutePath? {
configuration.sdkRootPath
}
public var swiftCompilerEnvironment: Environment {
configuration.swiftCompilerEnvironment
}
public var swiftCompilerFlags: [String] {
configuration.swiftCompilerFlags
}
public var swiftCompilerPathForManifests: AbsolutePath {
configuration.swiftCompilerPath
}
public var swiftPMLibrariesLocation: ToolchainConfiguration.SwiftPMLibrariesLocation {
configuration.swiftPMLibrariesLocation
}
public var xctestPath: AbsolutePath? {
configuration.xctestPath
}
public var swiftTestingPathOnWindows: AbsolutePath? {
configuration.swiftTestingPath
}
}
extension Environment {
fileprivate var windowsSDKRoot: AbsolutePath? {
if let SDKROOT = self["SDKROOT"], let sdkDir = try? AbsolutePath(validating: SDKROOT) {
return sdkDir
}
return nil
}
}
|