Skip to content

Commit 2820bd9

Browse files
committed
[Unix] Move all ELF SDK runtime libraries into their own architecture-specific directories
This is needed for all platforms that don't have multi-architecture libraries like Darwin. Also, add the new architecture-specific rpath to the resulting swift-driver. This is the Swift translation of the Driver-specific changes of swiftlang/swift#63782. Resolves #63645
1 parent 738a6a3 commit 2820bd9

File tree

4 files changed

+32
-33
lines changed

4 files changed

+32
-33
lines changed

Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift

-14
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,6 @@ extension GenericUnixToolchain {
3838
}
3939
}
4040

41-
private func majorArchitectureName(for triple: Triple) -> String {
42-
// The concept of a "major" arch name only applies to Linux triples
43-
guard triple.os == .linux else { return triple.archName }
44-
45-
// HACK: We don't wrap LLVM's ARM target architecture parsing, and we should
46-
// definitely not try to port it. This check was only normalizing
47-
// "armv7a/armv7r" and similar variants for armv6 to 'armv7' and
48-
// 'armv6', so just take a brute-force approach
49-
if triple.archName.contains("armv7") { return "armv7" }
50-
if triple.archName.contains("armv6") { return "armv6" }
51-
if triple.archName.contains("armv5") { return "armv5" }
52-
return triple.archName
53-
}
54-
5541
public func addPlatformSpecificLinkerArgs(
5642
to commandLine: inout [Job.ArgTemplate],
5743
parsedOptions: inout ParsedOptions,

Sources/SwiftDriver/Jobs/Toolchain+LinkerSupport.swift

+19-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ extension Toolchain {
3838
isShared: Bool
3939
) throws -> [VirtualPath] {
4040
let triple = targetInfo.target.triple
41-
let resourceDirPath = VirtualPath.lookup(targetInfo.runtimeResourcePath.path).appending(component: triple.platformName() ?? "")
41+
var resourceDirPath = VirtualPath.lookup(targetInfo.runtimeResourcePath.path).appending(component: triple.platformName() ?? "")
42+
// Only Darwin has multi-architecture libraries, so link against architecture-specific libraries everywhere else.
43+
if !triple.isDarwin {
44+
resourceDirPath = resourceDirPath.appending(component: majorArchitectureName(for: triple))
45+
}
4246
var result = [resourceDirPath]
4347

4448
let secondaryResourceDir = computeSecondaryResourceDirPath(for: triple, primaryPath: resourceDirPath)
@@ -84,6 +88,20 @@ extension Toolchain {
8488
commandLine.appendFlag(match.option.spelling + match.argument.asSingle)
8589
}
8690
}
91+
92+
func majorArchitectureName(for triple: Triple) -> String {
93+
// The concept of a "major" arch name only applies to Linux triples
94+
guard triple.os == .linux else { return triple.archName }
95+
96+
// HACK: We don't wrap LLVM's ARM target architecture parsing, and we should
97+
// definitely not try to port it. This check was only normalizing
98+
// "armv7a/armv7r" and similar variants for armv6 to 'armv7' and
99+
// 'armv6', so just take a brute-force approach
100+
if triple.archName.contains("armv7") { return "armv7" }
101+
if triple.archName.contains("armv6") { return "armv6" }
102+
if triple.archName.contains("armv5") { return "armv5" }
103+
return triple.archName
104+
}
87105
}
88106

89107
// MARK: - Common argument routines

Sources/SwiftDriver/Jobs/WindowsToolchain+LinkerSupport.swift

+1-14
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,6 @@ import SwiftOptions
1515
import func TSCBasic.lookupExecutablePath
1616
import struct TSCBasic.AbsolutePath
1717

18-
private func architecture(for triple: Triple) -> String {
19-
// The concept of a "major" arch name only applies to Linux triples
20-
guard triple.os == .linux else { return triple.archName }
21-
22-
// HACK: We don't wrap LLVM's ARM target architecture parsing, and we should
23-
// definitely not try to port it. This check was only normalizing
24-
// "armv7a/armv7r" and similar variants for armv6 to 'armv7' and
25-
// 'armv6', so just take a brute-force approach
26-
if triple.archName.contains("armv7") { return "armv7" }
27-
if triple.archName.contains("armv6") { return "armv6" }
28-
return triple.archName
29-
}
30-
3118
extension WindowsToolchain {
3219
public func addPlatformSpecificLinkerArgs(to commandLine: inout [Job.ArgTemplate],
3320
parsedOptions: inout ParsedOptions,
@@ -137,7 +124,7 @@ extension WindowsToolchain {
137124
rsrc = try VirtualPath(path: AbsolutePath(validating: sdk)
138125
.appending(components: "usr", "lib", "swift",
139126
targetTriple.platformName() ?? "",
140-
architecture(for: targetTriple))
127+
majorArchitectureName(for: targetTriple))
141128
.pathString)
142129
} else {
143130
rsrc = VirtualPath.lookup(targetInfo.runtimeResourcePath.path)

Utilities/build-script-helper.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,22 @@ def get_swiftpm_options(args):
9999
if args.cross_compile_hosts:
100100
swiftpm_args += ['--destination', args.cross_compile_config]
101101

102+
swift_exec = os.path.join(os.path.join(args.toolchain, 'bin'), 'swiftc')
103+
build_target = get_build_target(swift_exec, args, cross_compile=(True if args.cross_compile_hosts else False))
104+
build_arch = build_target.split('-')[0]
105+
build_os = build_target.split('-')[2]
102106
if 'ANDROID_DATA' in os.environ or (args.cross_compile_hosts and re.match(
103107
'android-', args.cross_compile_hosts[0])):
104108
swiftpm_args += [
105-
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/android',
109+
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/android/' + build_arch,
106110
# SwiftPM will otherwise try to compile against GNU strerror_r on
107111
# Android and fail.
108112
'-Xswiftc', '-Xcc', '-Xswiftc', '-U_GNU_SOURCE',
109113
]
110114
else:
111115
# Library rpath for swift, dispatch, Foundation, etc. when installing
112116
swiftpm_args += [
113-
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/linux',
117+
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/' + build_os + '/' + build_arch,
114118
]
115119

116120
if args.action == 'install':
@@ -595,10 +599,14 @@ def cmake_build(args, swiftc_exec, cmake_args, swift_flags, source_path,
595599
if args.verbose:
596600
print(stdout)
597601

598-
def get_build_target(swiftc_path, args):
602+
def get_build_target(swiftc_path, args, cross_compile=False):
599603
"""Returns the target-triple of the current machine."""
600604
try:
601-
target_info_json = subprocess.check_output([swiftc_path, '-print-target-info'],
605+
command = [swiftc_path, '-print-target-info']
606+
if cross_compile:
607+
cross_compile_json = json.load(open(args.cross_compile_config))
608+
command += ['-target', cross_compile_json["target"]]
609+
target_info_json = subprocess.check_output(command,
602610
stderr=subprocess.PIPE,
603611
universal_newlines=True).strip()
604612
args.target_info = json.loads(target_info_json)

0 commit comments

Comments
 (0)