Skip to content

Commit 3162d37

Browse files
authored
On ELF platforms, only add runpaths as needed (#6321) (#6409)
As long as I'm modifying the Python bootstrap script, clean up some incorrect checks of the host `platform.system()` and replace some unnecessary regexes.
1 parent 15d82c3 commit 3162d37

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
@@ -151,7 +162,8 @@ let package = Package(
151162
swiftSettings: [
152163
.unsafeFlags(["-package-description-version", "999.0"]),
153164
.unsafeFlags(["-enable-library-evolution"]),
154-
]
165+
],
166+
linkerSettings: packageLibraryLinkSettings
155167
),
156168

157169
// The `PackagePlugin` target provides the API that is available to
@@ -163,7 +175,8 @@ let package = Package(
163175
swiftSettings: [
164176
.unsafeFlags(["-package-description-version", "999.0"]),
165177
.unsafeFlags(["-enable-library-evolution"]),
166-
]
178+
],
179+
linkerSettings: packageLibraryLinkSettings
167180
),
168181

169182
// MARK: SwiftPM specific support libraries
@@ -497,7 +510,8 @@ let package = Package(
497510
"CrossCompilationDestinationsTool",
498511
"PackageCollectionsTool",
499512
"PackageRegistryTool"
500-
]
513+
],
514+
linkerSettings: swiftpmLinkSettings
501515
),
502516
.executableTarget(
503517
/** Interact with package registry */

Utilities/bootstrap

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

685685
def call_swiftpm(args, cmd, cwd=None):
686686
"""Calls a SwiftPM binary with the necessary environment variables and flags."""
687+
688+
args.build_target = get_build_target(args, cross_compile=(True if args.cross_compile_config else False))
689+
690+
args.platform_path = None
691+
for path in args.target_info["paths"]["runtimeLibraryPaths"]:
692+
args.platform_path = re.search(r"(lib/swift/([^/]+))$", path)
693+
if args.platform_path:
694+
break
695+
696+
if not args.platform_path:
697+
error(
698+
"the command `%s -print-target-info` didn't return a valid runtime library path"
699+
% args.swiftc_path
700+
)
701+
687702
full_cmd = get_swiftpm_env_cmd(args) + cmd + get_swiftpm_flags(args)
688703
if cwd is None:
689704
cwd = args.project_root
@@ -731,6 +746,9 @@ def get_swiftpm_env_cmd(args):
731746
env_cmd.append("SWIFTCI_USE_LOCAL_DEPS=1")
732747
env_cmd.append("SWIFTPM_MACOS_DEPLOYMENT_TARGET=%s" % g_macos_deployment_target)
733748

749+
if not '-macosx' in args.build_target and args.command == 'install':
750+
env_cmd.append("SWIFTCI_INSTALL_RPATH_OS=%s" % args.platform_path.group(2))
751+
734752
if args.bootstrap:
735753
libs_joined = ":".join([
736754
os.path.join(args.bootstrap_dir, "lib"),
@@ -787,67 +805,34 @@ def get_swiftpm_flags(args):
787805
# On Darwin platforms, a relative rpath is necessary for experimental
788806
# toolchains that include libraries not part of the OS (e.g. PythonKit or
789807
# TensorFlow).
790-
if platform.system() == "Darwin":
808+
if '-macosx' in args.build_target:
791809
# rpaths for compatibility libraries
792810
for lib_path in get_swift_backdeploy_library_paths(args):
793811
build_flags.extend(["-Xlinker", "-rpath", "-Xlinker", lib_path])
794812

795-
swift_library_rpath_prefix = "@executable_path/../"
796-
elif platform.system() == 'Linux' or platform.system() == 'OpenBSD':
797-
# `$ORIGIN` is an ELF construct.
798-
swift_library_rpath_prefix = "$ORIGIN/../"
799-
if platform.system() == 'OpenBSD':
800-
build_flags.extend(["-Xlinker", "-z", "-Xlinker", "origin"])
801-
802-
platform_path = None
803-
for path in args.target_info["paths"]["runtimeLibraryPaths"]:
804-
platform_path = re.search(r"(lib/swift/([^/]+))$", path)
805-
if platform_path:
806-
build_flags.extend(
807-
[
808-
"-Xlinker",
809-
"-rpath",
810-
"-Xlinker",
811-
swift_library_rpath_prefix + platform_path.group(1),
812-
]
813-
)
814-
if platform.system() == 'Linux':
815-
build_flags.extend(
816-
[
817-
"-Xlinker",
818-
"-rpath",
819-
"-Xlinker",
820-
swift_library_rpath_prefix + '../' + platform_path.group(2),
821-
]
822-
)
823-
break
824-
825-
if not platform_path:
826-
error(
827-
"the command `%s -print-target-info` didn't return a valid runtime library path"
828-
% args.swiftc_path
813+
build_flags.extend(
814+
[
815+
"-Xlinker",
816+
"-rpath",
817+
"-Xlinker",
818+
"@executable_path/../" + args.platform_path.group(1),
819+
]
829820
)
830821

831-
# Don't use GNU strerror_r on Android.
832-
if 'ANDROID_DATA' in os.environ or (args.cross_compile_hosts and re.match(
833-
'android-', args.cross_compile_hosts)):
834-
build_flags.extend(["-Xswiftc", "-Xcc", "-Xswiftc", "-U_GNU_SOURCE"])
835-
836-
if platform.system() == "OpenBSD":
822+
if '-openbsd' in args.build_target:
823+
build_flags.extend(["-Xlinker", "-z", "-Xlinker", "origin"])
837824
build_flags.extend(["-Xcc", "-I/usr/local/include"])
838825
build_flags.extend(["-Xlinker", "-L/usr/local/lib"])
839826

840-
# On ELF platforms, remove the host toolchain's stdlib absolute rpath from
841-
# installed executables and shared libraries.
842-
if platform.system() != "Darwin" and args.command == 'install':
843-
build_flags.extend(["-Xswiftc", "-no-toolchain-stdlib-rpath"])
827+
# Don't use GNU strerror_r on Android.
828+
if '-android' in args.build_target:
829+
build_flags.extend(["-Xswiftc", "-Xcc", "-Xswiftc", "-U_GNU_SOURCE"])
844830

845-
build_target = get_build_target(args)
846831
cross_compile_hosts = args.cross_compile_hosts
847832
if cross_compile_hosts:
848-
if re.search('-apple-macosx', build_target) and re.match('macosx-', cross_compile_hosts):
833+
if '-apple-macosx' in args.build_target and cross_compile_hosts.startswith('macosx-'):
849834
build_flags += ["--arch", "x86_64", "--arch", "arm64"]
850-
elif re.match('android-', cross_compile_hosts):
835+
elif cross_compile_hosts.startswith('android-'):
851836
build_flags.extend(["--destination", args.cross_compile_config])
852837
else:
853838
error("cannot cross-compile for %s" % cross_compile_hosts)

0 commit comments

Comments
 (0)