Skip to content

Commit

Permalink
Fix "Couldn't lookup symbols" error when linking external Swift libra…
Browse files Browse the repository at this point in the history
…ries on Linux.

Addresses google#4.

On Linux, the REPL fails with "Couldn't lookup symbols" errors when:
- Linking an external Swift shared library (produced by `swift build`) and
  importing the corresponding Swift module.
- Importing the `TensorFlow` and `Python` modules, without manually linking
  `libswiftPython.so` and `libswiftTensorFlow.so`.

A manual workaround involves specifying the `-lswiftPython` and
`-lswiftTensorFlow` flags (in that specific order) when invoking the REPL.
Also, `Python` and `TensorFlow` must be imported before the external Swift
module to avoid the error.

Conditionally adding the linker flags here seems to solve the issue. This is
robust assuming that toolchain artifacts are not manipulated (so that somehow
`Python.swiftmodule` exists while `libswiftPython.so` doesn't).

PiperOrigin-RevId: 195572160
  • Loading branch information
dan-zheng committed May 6, 2018
1 parent 7955f9f commit dd116e6
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/Driver/UnixToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,34 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
} else {
Arguments.push_back(context.Args.MakeArgString(SharedRuntimeLibPath));
Arguments.push_back("-lswiftCore");

// SWIFT_ENABLE_TENSORFLOW
// On Linux, the REPL fails with "Couldn't lookup symbols" errors when:
// - Linking an external Swift shared library (produced by `swift build`)
// and importing the corresponding Swift module.
// - Importing the `TensorFlow` and `Python` modules, without manually
// linking `libswiftPython.so` and `libswiftTensorFlow.so`.
//
// A manual workaround involves specifying the `-lswiftPython` and
// `-lswiftTensorFlow` flags (in that specific order) when invoking the
// REPL. Also, `Python` and `TensorFlow` must be imported before the
// external Swift module to avoid the error.
//
// Conditionally adding the linker flags here seems to solve the issue.
// This is robust assuming that toolchain artifacts are not manipulated
// (so that somehow Python.swiftmodule exists while libswiftPython.so
// doesn't).
//
// https://github.com/google/swift/issues/4
SmallString<128> swiftPythonLibPath = SharedRuntimeLibPath;
llvm::sys::path::append(swiftPythonLibPath, "libswiftPython.so");
if (llvm::sys::fs::exists(swiftPythonLibPath))
Arguments.push_back("-lswiftPython");

SmallString<128> swiftTensorFlowLibPath = SharedRuntimeLibPath;
llvm::sys::path::append(swiftTensorFlowLibPath, "libswiftTensorFlow.so");
if (llvm::sys::fs::exists(swiftTensorFlowLibPath))
Arguments.push_back("-lswiftTensorFlow");
}

// Explicitly pass the target to the linker
Expand Down

0 comments on commit dd116e6

Please sign in to comment.