Skip to content

Commit b45f2db

Browse files
authored
[TF] Set default RPATH to toolchain stdlib instead of /usr/lib/swift on macOS. (#27206)
Previously, the default RPATH was set to `/usr/lib/swift`. This caused linker issues on macOS, as tensorflow-branch-specific modules like TensorFlow and Python do not exist in `/usr/lib/swift`. This patch defaults the `-toolchain-stdlib-rpath` flag to be true, so the default RPATH is the toolchain standard library instead of `/usr/lib/swift`. With this default, the linker issues are fixed. A `-no-toolchain-stdlib-path` flag is added to opt out of this default, making `/usr/lib/swift` the RPATH. Add positive/negative tests. `swift test` works on macOS for packages that import/use TensorFlow again. Partially reverts #24787. Resolves TF-797.
1 parent 0641a22 commit b45f2db

File tree

5 files changed

+86
-21
lines changed

5 files changed

+86
-21
lines changed

include/swift/Option/Options.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,14 @@ def no_static_stdlib: Flag<["-"], "no-static-stdlib">,
513513
def toolchain_stdlib_rpath: Flag<["-"], "toolchain-stdlib-rpath">,
514514
Flags<[HelpHidden,DoesNotAffectIncrementalBuild]>,
515515
HelpText<"Add an rpath entry for the toolchain's standard library, rather than the OS's">;
516+
// SWIFT_ENABLE_TENSORFLOW
517+
// NOTE(TF-797): The default for `toolchain_stdlib_rpath` is set to true to
518+
// prevent TensorFlow linker errors. Thus, the `no_toolchain_stdlib_rpath` flag
519+
// is necessary to opt out of the default behavior.
520+
def no_toolchain_stdlib_rpath: Flag<["-"], "no-toolchain-stdlib-rpath">,
521+
Flags<[HelpHidden,DoesNotAffectIncrementalBuild]>,
522+
HelpText<"Do not add an rpath entry for the toolchain's standard library, rather than the OS's">;
523+
// SWIFT_ENABLE_TENSORFLOW END
516524
def no_stdlib_rpath: Flag<["-"], "no-stdlib-rpath">,
517525
Flags<[HelpHidden,DoesNotAffectIncrementalBuild]>,
518526
HelpText<"Don't add any rpath entries.">;

lib/Driver/DarwinToolChains.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,16 @@ toolchains::Darwin::addArgsToLinkStdlib(ArgStringList &Arguments,
389389
Arguments.push_back(context.Args.MakeArgString(path));
390390
}
391391

392-
if (context.Args.hasArg(options::OPT_toolchain_stdlib_rpath)) {
392+
// SWIFT_ENABLE_TENSORFLOW
393+
// NOTE(TF-797): default true for toolchain stdlib rpath to prevent linker
394+
// issues. This works around the fact that TensorFlow/Python modules do not
395+
// exist in `/usr/lib/swift` on Darwin platforms.
396+
// Relevant Swift-in-Darwin-OSs patch:
397+
// https://github.com/apple/swift/pull/24787.
398+
if (!context.Args.hasArg(options::OPT_no_stdlib_rpath) &&
399+
context.Args.hasFlag(options::OPT_toolchain_stdlib_rpath,
400+
options::OPT_no_toolchain_stdlib_rpath, true)) {
401+
// SWIFT_ENABLE_TENSORFLOW END
393402
// If the user has explicitly asked for a toolchain stdlib, we should
394403
// provide one using -rpath. This used to be the default behaviour but it
395404
// was considered annoying in at least the SwiftPM scenario (see

test/Driver/linker-rpath.swift

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,32 @@
22
// Note: This is really about the /host/ environment, but since there are RUN
33
// lines for multiple targets anyway it doesn't make a huge difference.
44

5-
// RUN: %swiftc_driver_plain -driver-print-jobs -target x86_64-apple-macosx10.9 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
6-
// RUN: %swiftc_driver_plain -driver-print-jobs -target x86_64-apple-macosx10.14 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
7-
// RUN: %swiftc_driver_plain -driver-print-jobs -target x86_64-apple-macosx10.14.3 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
8-
// RUN: %swiftc_driver_plain -driver-print-jobs -target x86_64-apple-macosx10.14.4 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
9-
// RUN: %swiftc_driver_plain -driver-print-jobs -target x86_64-apple-macosx10.15 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO-RPATH %s
10-
11-
// RUN: %swiftc_driver_plain -driver-print-jobs -target arm64-apple-ios12 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
12-
// RUN: %swiftc_driver_plain -driver-print-jobs -target arm64-apple-ios12.1 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
13-
// RUN: %swiftc_driver_plain -driver-print-jobs -target arm64-apple-ios12.2 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO-RPATH %s
14-
// RUN: %swiftc_driver_plain -driver-print-jobs -target arm64-apple-ios13 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO-RPATH %s
15-
16-
// RUN: %swiftc_driver_plain -driver-print-jobs -target arm64-apple-tvos12 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
17-
// RUN: %swiftc_driver_plain -driver-print-jobs -target arm64-apple-tvos12.1 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
18-
// RUN: %swiftc_driver_plain -driver-print-jobs -target arm64-apple-tvos12.2 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO-RPATH %s
19-
// RUN: %swiftc_driver_plain -driver-print-jobs -target arm64-apple-tvos13 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO-RPATH %s
20-
21-
// RUN: %swiftc_driver_plain -driver-print-jobs -target armv7k-apple-watchos5 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
22-
// RUN: %swiftc_driver_plain -driver-print-jobs -target armv7k-apple-watchos5.1 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
23-
// RUN: %swiftc_driver_plain -driver-print-jobs -target armv7k-apple-watchos5.2 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO-RPATH %s
24-
// RUN: %swiftc_driver_plain -driver-print-jobs -target armv7k-apple-watchos6 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO-RPATH %s
5+
// SWIFT_ENABLE_TENSORFLOW
6+
// All "-no-toolchain-stdlib-rpath" additions are SWIFT_ENABLE_TENSORFLOW.
7+
8+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target x86_64-apple-macosx10.9 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
9+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target x86_64-apple-macosx10.9 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
10+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target x86_64-apple-macosx10.14 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
11+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target x86_64-apple-macosx10.14.3 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
12+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target x86_64-apple-macosx10.14.4 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
13+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target x86_64-apple-macosx10.15 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO-RPATH %s
14+
15+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target arm64-apple-ios12 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
16+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target arm64-apple-ios12.1 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
17+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target arm64-apple-ios12.2 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO-RPATH %s
18+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target arm64-apple-ios13 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO-RPATH %s
19+
20+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target arm64-apple-tvos12 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
21+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target arm64-apple-tvos12.1 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
22+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target arm64-apple-tvos12.2 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO-RPATH %s
23+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target arm64-apple-tvos13 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO-RPATH %s
24+
25+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target armv7k-apple-watchos5 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
26+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target armv7k-apple-watchos5.1 %S/../Inputs/empty.swift | %FileCheck -check-prefix RPATH %s
27+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target armv7k-apple-watchos5.2 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO-RPATH %s
28+
// RUN: %swiftc_driver_plain -driver-print-jobs -no-toolchain-stdlib-rpath -target armv7k-apple-watchos6 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO-RPATH %s
29+
30+
// SWIFT_ENABLE_TENSORFLOW END
2531

2632
// RPATH: bin/ld{{"? }}
2733
// RPATH-SAME: -rpath {{"?/usr/lib/swift(-.+)?"? }}
@@ -37,6 +43,12 @@
3743
// RUN: %swiftc_driver_plain -driver-print-jobs -toolchain-stdlib-rpath -target x86_64-apple-macosx10.9 %S/../Inputs/empty.swift -resource-dir garbage/ | %FileCheck -check-prefix TOOLCHAIN-RPATH -DPLATFORM=%target-sdk-name %s
3844
// RUN: %swiftc_driver_plain -driver-print-jobs -toolchain-stdlib-rpath -target x86_64-apple-macosx10.15 %S/../Inputs/empty.swift -resource-dir garbage/ | %FileCheck -check-prefix TOOLCHAIN-RPATH -DPLATFORM=%target-sdk-name %s
3945

46+
// SWIFT_ENABLE_TENSORFLOW
47+
// ### Test with implicit -toolchain-stdlib-rpath
48+
// RUN: %swiftc_driver_plain -driver-print-jobs -target x86_64-apple-macosx10.9 %S/../Inputs/empty.swift -resource-dir garbage/ | %FileCheck -check-prefix TOOLCHAIN-RPATH -DPLATFORM=%target-sdk-name %s
49+
// RUN: %swiftc_driver_plain -driver-print-jobs -target x86_64-apple-macosx10.15 %S/../Inputs/empty.swift -resource-dir garbage/ | %FileCheck -check-prefix TOOLCHAIN-RPATH -DPLATFORM=%target-sdk-name %s
50+
// SWIFT_ENABLE_TENSORFLOW END
51+
4052
// TOOLCHAIN-RPATH: bin/ld{{"? }}
4153
// TOOLCHAIN-RPATH-SAME: -rpath garbage/[[PLATFORM]]{{ }}
4254
// TOOLCHAIN-RPATH-SAME: -o {{[^ ]+}}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Note: RUN lines are copied from substitutions for %target-run-simple-swift.
2+
// Explicit references to `-toolchain-stdlib-rpath` are omitted.
3+
4+
// REQUIRES: OS=macosx
5+
6+
// RUN: %empty-directory(%t)
7+
// RUN: xcrun --toolchain $TOOLCHAINS --sdk %sdk %swiftc_driver_plain %mcp_opt %s -o %t/a.out -module-name main
8+
// RUN: %target-codesign %t/a.out
9+
// RUN: %t/a.out
10+
11+
// TF-797: test default true behavior for `-toolchain-stdlib-rpath` on Darwin platforms.
12+
// Verify no linker issues without explicitly specifying `-toolchain-stdlib-rpath`.
13+
14+
import Python
15+
print(Python.import)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Note: RUN lines are copied from substitutions for %target-run-simple-swift.
2+
// Explicit references to `-toolchain-stdlib-rpath` are omitted.
3+
// Explicit `-no-toolchain-stdlib-rpath` flag is added.
4+
5+
// REQUIRES: OS=macosx
6+
7+
// RUN: %empty-directory(%t)
8+
// RUN: xcrun --toolchain $TOOLCHAINS --sdk %sdk %swiftc_driver_plain %mcp_opt -no-toolchain-stdlib-rpath %s -o %t/a.out -module-name main
9+
// RUN: %target-codesign %t/a.out
10+
// RUN: not --crash %t/a.out 2>&1 | %FileCheck %s
11+
12+
// TF-797: test `-no-toolchain-stdlib-rpath` on Darwin platforms.
13+
// Expected crash due to linker error because RPATH is `/usr/lib/swift` instead
14+
// of toolchain standard library.
15+
16+
import Python
17+
print(Python.import)
18+
19+
// CHECK: dyld: Library not loaded: @rpath/libswiftPython.dylib
20+
// CHECK: Referenced from: BUILD_DIR/test-macosx-x86_64/Python/Output/toolchain_stdlib_rpath_false.swift.tmp/a.out
21+
// CHECK: Reason: image not found

0 commit comments

Comments
 (0)