Skip to content

Commit f708879

Browse files
authored
On ELF platforms, only add runpaths as needed (#6321) (#6430)
1 parent 6651d98 commit f708879

File tree

2 files changed

+50
-51
lines changed

2 files changed

+50
-51
lines changed

Package.swift

+17-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@
1515
import PackageDescription
1616
import class Foundation.ProcessInfo
1717

18+
// When building the toolchain on the CI for ELF platforms, remove the CI's
19+
// stdlib absolute runpath and add ELF's $ORIGIN relative paths before installing.
20+
let swiftpmLinkSettings : [LinkerSetting]
21+
let packageLibraryLinkSettings : [LinkerSetting]
22+
if let resourceDirPath = ProcessInfo.processInfo.environment["SWIFTCI_INSTALL_RPATH_OS"] {
23+
swiftpmLinkSettings = [ .unsafeFlags(["-no-toolchain-stdlib-rpath", "-Xlinker", "-rpath", "-Xlinker", "$ORIGIN/../lib/swift/\(resourceDirPath)"]) ]
24+
packageLibraryLinkSettings = [ .unsafeFlags(["-no-toolchain-stdlib-rpath", "-Xlinker", "-rpath", "-Xlinker", "$ORIGIN/../../\(resourceDirPath)"]) ]
25+
} else {
26+
swiftpmLinkSettings = []
27+
packageLibraryLinkSettings = []
28+
}
1829

1930
/** SwiftPMDataModel is the subset of SwiftPM product that includes just its data model.
2031
This allows some clients (such as IDEs) that use SwiftPM's data model but not its build system
@@ -152,7 +163,8 @@ let package = Package(
152163
swiftSettings: [
153164
.unsafeFlags(["-package-description-version", "999.0"]),
154165
.unsafeFlags(["-enable-library-evolution"], .when(platforms: [.macOS]))
155-
]
166+
],
167+
linkerSettings: packageLibraryLinkSettings
156168
),
157169

158170
// The `PackagePlugin` target provides the API that is available to
@@ -164,7 +176,8 @@ let package = Package(
164176
swiftSettings: [
165177
.unsafeFlags(["-package-description-version", "999.0"]),
166178
.unsafeFlags(["-enable-library-evolution"], .when(platforms: [.macOS]))
167-
]
179+
],
180+
linkerSettings: packageLibraryLinkSettings
168181
),
169182

170183
// MARK: SwiftPM specific support libraries
@@ -471,7 +484,8 @@ let package = Package(
471484
"CrossCompilationDestinationsTool",
472485
"PackageCollectionsTool",
473486
"PackageRegistryTool"
474-
]
487+
],
488+
linkerSettings: swiftpmLinkSettings
475489
),
476490
.executableTarget(
477491
/** Interact with package registry */

Utilities/bootstrap

+33-48
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,21 @@ def build_swiftpm_with_swiftpm(args, integrated_swift_driver):
670670

671671
def call_swiftpm(args, cmd, cwd=None):
672672
"""Calls a SwiftPM binary with the necessary environment variables and flags."""
673+
674+
args.build_target = get_build_target(args, cross_compile=(True if args.cross_compile_config else False))
675+
676+
args.platform_path = None
677+
for path in args.target_info["paths"]["runtimeLibraryPaths"]:
678+
args.platform_path = re.search(r"(lib/swift/([^/]+))$", path)
679+
if args.platform_path:
680+
break
681+
682+
if not args.platform_path:
683+
error(
684+
"the command `%s -print-target-info` didn't return a valid runtime library path"
685+
% args.swiftc_path
686+
)
687+
673688
full_cmd = get_swiftpm_env_cmd(args) + cmd + get_swiftpm_flags(args)
674689
if cwd is None:
675690
cwd = args.project_root
@@ -717,6 +732,9 @@ def get_swiftpm_env_cmd(args):
717732
env_cmd.append("SWIFTCI_USE_LOCAL_DEPS=1")
718733
env_cmd.append("SWIFTPM_MACOS_DEPLOYMENT_TARGET=%s" % g_macos_deployment_target)
719734

735+
if not '-macosx' in args.build_target and args.command == 'install':
736+
env_cmd.append("SWIFTCI_INSTALL_RPATH_OS=%s" % args.platform_path.group(2))
737+
720738
if args.bootstrap:
721739
libs_joined = ":".join([
722740
os.path.join(args.bootstrap_dir, "lib"),
@@ -770,67 +788,34 @@ def get_swiftpm_flags(args):
770788
# On Darwin platforms, a relative rpath is necessary for experimental
771789
# toolchains that include libraries not part of the OS (e.g. PythonKit or
772790
# TensorFlow).
773-
if platform.system() == "Darwin":
791+
if '-macosx' in args.build_target:
774792
# rpaths for compatibility libraries
775793
for lib_path in get_swift_backdeploy_library_paths(args):
776794
build_flags.extend(["-Xlinker", "-rpath", "-Xlinker", lib_path])
777795

778-
swift_library_rpath_prefix = "@executable_path/../"
779-
elif platform.system() == 'Linux' or platform.system() == 'OpenBSD':
780-
# `$ORIGIN` is an ELF construct.
781-
swift_library_rpath_prefix = "$ORIGIN/../"
782-
if platform.system() == 'OpenBSD':
783-
build_flags.extend(["-Xlinker", "-z", "-Xlinker", "origin"])
784-
785-
platform_path = None
786-
for path in args.target_info["paths"]["runtimeLibraryPaths"]:
787-
platform_path = re.search(r"(lib/swift/([^/]+))$", path)
788-
if platform_path:
789-
build_flags.extend(
790-
[
791-
"-Xlinker",
792-
"-rpath",
793-
"-Xlinker",
794-
swift_library_rpath_prefix + platform_path.group(1),
795-
]
796-
)
797-
if platform.system() == 'Linux':
798-
build_flags.extend(
799-
[
800-
"-Xlinker",
801-
"-rpath",
802-
"-Xlinker",
803-
swift_library_rpath_prefix + '../' + platform_path.group(2),
804-
]
805-
)
806-
break
807-
808-
if not platform_path:
809-
error(
810-
"the command `%s -print-target-info` didn't return a valid runtime library path"
811-
% args.swiftc_path
796+
build_flags.extend(
797+
[
798+
"-Xlinker",
799+
"-rpath",
800+
"-Xlinker",
801+
"@executable_path/../" + args.platform_path.group(1),
802+
]
812803
)
813804

814-
# Don't use GNU strerror_r on Android.
815-
if 'ANDROID_DATA' in os.environ or (args.cross_compile_hosts and re.match(
816-
'android-', args.cross_compile_hosts)):
817-
build_flags.extend(["-Xswiftc", "-Xcc", "-Xswiftc", "-U_GNU_SOURCE"])
818-
819-
if platform.system() == "OpenBSD":
805+
if '-openbsd' in args.build_target:
806+
build_flags.extend(["-Xlinker", "-z", "-Xlinker", "origin"])
820807
build_flags.extend(["-Xcc", "-I/usr/local/include"])
821808
build_flags.extend(["-Xlinker", "-L/usr/local/lib"])
822809

823-
# On ELF platforms, remove the host toolchain's stdlib absolute rpath from
824-
# installed executables and shared libraries.
825-
if platform.system() != "Darwin" and args.command == 'install':
826-
build_flags.extend(["-Xswiftc", "-no-toolchain-stdlib-rpath"])
810+
# Don't use GNU strerror_r on Android.
811+
if '-android' in args.build_target:
812+
build_flags.extend(["-Xswiftc", "-Xcc", "-Xswiftc", "-U_GNU_SOURCE"])
827813

828-
build_target = get_build_target(args)
829814
cross_compile_hosts = args.cross_compile_hosts
830815
if cross_compile_hosts:
831-
if re.search('-apple-macosx', build_target) and re.match('macosx-', cross_compile_hosts):
816+
if '-apple-macosx' in args.build_target and cross_compile_hosts.startswith('macosx-'):
832817
build_flags += ["--arch", "x86_64", "--arch", "arm64"]
833-
elif re.match('android-', cross_compile_hosts):
818+
elif cross_compile_hosts.startswith('android-'):
834819
build_flags.extend(["--destination", args.cross_compile_config])
835820
else:
836821
error("cannot cross-compile for %s" % cross_compile_hosts)

0 commit comments

Comments
 (0)