Skip to content

Commit

Permalink
Add /usr/lib/swift to rpaths of Swift binaries and stop static link…
Browse files Browse the repository at this point in the history
…ing to the runtime.

Also add a temporary workaround for the case where Xcode 10.2b4 is being run on a macOS 10.14.x that doesn't yet have `/usr/lib/swift`.

RELNOTES: The Xcode toolchain now supports dynamically linking to Swift runtimes distributed with the OS (which is the case on macOS 10.14.4 and higher).
PiperOrigin-RevId: 237818384
  • Loading branch information
allevato committed Mar 25, 2019
1 parent 9a2269c commit 19db162
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions swift/internal/xcode_swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,21 @@ def _default_linker_opts(
apple_toolchain,
platform,
target,
xcode_config,
is_static,
is_test):
"""Returns options that should be passed by default to `clang` when linking.
This function is wrapped in a `partial` that will be propagated as part of the toolchain
provider. The first four arguments are pre-bound; the `is_static` and `is_test` arguments are
provider. The first five arguments are pre-bound; the `is_static` and `is_test` arguments are
expected to be passed by the caller.
Args:
apple_fragment: The `apple` configuration fragment.
apple_toolchain: The `apple_common.apple_toolchain()` object.
platform: The `apple_platform` value describing the target platform.
target: The target triple.
xcode_config: The Xcode configuration.
is_static: `True` to link against the static version of the Swift runtime, or `False` to
link against dynamic/shared libraries.
is_test: `True` if the target being linked is a test target.
Expand All @@ -63,7 +65,15 @@ def _default_linker_opts(
platform_framework_dir = apple_toolchain.platform_developer_framework_dir(apple_fragment)
linkopts = []

if is_static:
uses_runtime_in_os = _is_xcode_at_least_version(xcode_config, "10.2")
if uses_runtime_in_os:
# Starting with Xcode 10.2, Apple forbids statically linking to the Swift runtime. The
# libraries are distributed with the OS and located in /usr/lib/swift.
swift_subdir = "swift"
linkopts.append("-Wl,-rpath,/usr/lib/swift")
elif is_static:
# This branch and the branch below now only support Xcode 10.1 and below. Eventually,
# once we drop support for those versions, they can be deleted.
swift_subdir = "swift_static"
linkopts.extend([
"-Wl,-force_load_swift_libs",
Expand All @@ -83,6 +93,15 @@ def _default_linker_opts(
toolchain = "XcodeDefault",
)

# TODO(b/128303533): It's possible to run Xcode 10.2 on a version of macOS 10.14.x that does
# not yet include `/usr/lib/swift`. Later Xcode 10.2 betas have deleted the `swift_static`
# directory, so we must manually add the dylibs to the binary's rpath or those binaries won't
# be able to run at all. This is added after `/usr/lib/swift` above so the system versions
# will always be preferred if they are present.
# This workaround can be removed once Xcode 10.2 and macOS 10.14.4 are out of beta.
if uses_runtime_in_os and platform == apple_common.platform.macos:
linkopts.append("-Wl,-rpath,{}".format(swift_lib_dir))

linkopts.extend([
"-F{}".format(platform_framework_dir),
"-L{}".format(swift_lib_dir),
Expand Down Expand Up @@ -420,6 +439,7 @@ def _xcode_swift_toolchain_impl(ctx):
apple_toolchain,
platform,
target,
xcode_config,
)
swiftc_copts = _default_swiftc_copts(apple_fragment, apple_toolchain, target)

Expand Down

0 comments on commit 19db162

Please sign in to comment.