Skip to content

Commit

Permalink
[Unix] Move all ELF SDK runtime libraries into their own architecture…
Browse files Browse the repository at this point in the history
…-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
  • Loading branch information
finagolfin committed Feb 20, 2023
1 parent 738a6a3 commit 2820bd9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
14 changes: 0 additions & 14 deletions Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,6 @@ extension GenericUnixToolchain {
}
}

private func majorArchitectureName(for triple: Triple) -> String {
// The concept of a "major" arch name only applies to Linux triples
guard triple.os == .linux else { return triple.archName }

// HACK: We don't wrap LLVM's ARM target architecture parsing, and we should
// definitely not try to port it. This check was only normalizing
// "armv7a/armv7r" and similar variants for armv6 to 'armv7' and
// 'armv6', so just take a brute-force approach
if triple.archName.contains("armv7") { return "armv7" }
if triple.archName.contains("armv6") { return "armv6" }
if triple.archName.contains("armv5") { return "armv5" }
return triple.archName
}

public func addPlatformSpecificLinkerArgs(
to commandLine: inout [Job.ArgTemplate],
parsedOptions: inout ParsedOptions,
Expand Down
20 changes: 19 additions & 1 deletion Sources/SwiftDriver/Jobs/Toolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ extension Toolchain {
isShared: Bool
) throws -> [VirtualPath] {
let triple = targetInfo.target.triple
let resourceDirPath = VirtualPath.lookup(targetInfo.runtimeResourcePath.path).appending(component: triple.platformName() ?? "")
var resourceDirPath = VirtualPath.lookup(targetInfo.runtimeResourcePath.path).appending(component: triple.platformName() ?? "")
// Only Darwin has multi-architecture libraries, so link against architecture-specific libraries everywhere else.
if !triple.isDarwin {
resourceDirPath = resourceDirPath.appending(component: majorArchitectureName(for: triple))
}
var result = [resourceDirPath]

let secondaryResourceDir = computeSecondaryResourceDirPath(for: triple, primaryPath: resourceDirPath)
Expand Down Expand Up @@ -84,6 +88,20 @@ extension Toolchain {
commandLine.appendFlag(match.option.spelling + match.argument.asSingle)
}
}

func majorArchitectureName(for triple: Triple) -> String {
// The concept of a "major" arch name only applies to Linux triples
guard triple.os == .linux else { return triple.archName }

// HACK: We don't wrap LLVM's ARM target architecture parsing, and we should
// definitely not try to port it. This check was only normalizing
// "armv7a/armv7r" and similar variants for armv6 to 'armv7' and
// 'armv6', so just take a brute-force approach
if triple.archName.contains("armv7") { return "armv7" }
if triple.archName.contains("armv6") { return "armv6" }
if triple.archName.contains("armv5") { return "armv5" }
return triple.archName
}
}

// MARK: - Common argument routines
Expand Down
15 changes: 1 addition & 14 deletions Sources/SwiftDriver/Jobs/WindowsToolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@ import SwiftOptions
import func TSCBasic.lookupExecutablePath
import struct TSCBasic.AbsolutePath

private func architecture(for triple: Triple) -> String {
// The concept of a "major" arch name only applies to Linux triples
guard triple.os == .linux else { return triple.archName }

// HACK: We don't wrap LLVM's ARM target architecture parsing, and we should
// definitely not try to port it. This check was only normalizing
// "armv7a/armv7r" and similar variants for armv6 to 'armv7' and
// 'armv6', so just take a brute-force approach
if triple.archName.contains("armv7") { return "armv7" }
if triple.archName.contains("armv6") { return "armv6" }
return triple.archName
}

extension WindowsToolchain {
public func addPlatformSpecificLinkerArgs(to commandLine: inout [Job.ArgTemplate],
parsedOptions: inout ParsedOptions,
Expand Down Expand Up @@ -137,7 +124,7 @@ extension WindowsToolchain {
rsrc = try VirtualPath(path: AbsolutePath(validating: sdk)
.appending(components: "usr", "lib", "swift",
targetTriple.platformName() ?? "",
architecture(for: targetTriple))
majorArchitectureName(for: targetTriple))
.pathString)
} else {
rsrc = VirtualPath.lookup(targetInfo.runtimeResourcePath.path)
Expand Down
16 changes: 12 additions & 4 deletions Utilities/build-script-helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,22 @@ def get_swiftpm_options(args):
if args.cross_compile_hosts:
swiftpm_args += ['--destination', args.cross_compile_config]

swift_exec = os.path.join(os.path.join(args.toolchain, 'bin'), 'swiftc')
build_target = get_build_target(swift_exec, args, cross_compile=(True if args.cross_compile_hosts else False))
build_arch = build_target.split('-')[0]
build_os = build_target.split('-')[2]
if 'ANDROID_DATA' in os.environ or (args.cross_compile_hosts and re.match(
'android-', args.cross_compile_hosts[0])):
swiftpm_args += [
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/android',
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/android/' + build_arch,
# SwiftPM will otherwise try to compile against GNU strerror_r on
# Android and fail.
'-Xswiftc', '-Xcc', '-Xswiftc', '-U_GNU_SOURCE',
]
else:
# Library rpath for swift, dispatch, Foundation, etc. when installing
swiftpm_args += [
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/linux',
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/' + build_os + '/' + build_arch,
]

if args.action == 'install':
Expand Down Expand Up @@ -595,10 +599,14 @@ def cmake_build(args, swiftc_exec, cmake_args, swift_flags, source_path,
if args.verbose:
print(stdout)

def get_build_target(swiftc_path, args):
def get_build_target(swiftc_path, args, cross_compile=False):
"""Returns the target-triple of the current machine."""
try:
target_info_json = subprocess.check_output([swiftc_path, '-print-target-info'],
command = [swiftc_path, '-print-target-info']
if cross_compile:
cross_compile_json = json.load(open(args.cross_compile_config))
command += ['-target', cross_compile_json["target"]]
target_info_json = subprocess.check_output(command,
stderr=subprocess.PIPE,
universal_newlines=True).strip()
args.target_info = json.loads(target_info_json)
Expand Down

0 comments on commit 2820bd9

Please sign in to comment.