Skip to content

Commit 4fa4fbc

Browse files
committed
[Build] Set rpath to load C shared objects on linux
1 parent 3801f1b commit 4fa4fbc

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

Fixtures/ClangModules/SwiftCMixed/Package.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ import PackageDescription
22

33
let package = Package(
44
name: "SwiftCMixed",
5-
targets: [Target(name: "SeaExec", dependencies: ["SeaLib"])]
5+
targets: [
6+
Target(name: "SeaExec", dependencies: ["SeaLib"]),
7+
Target(name: "CExec", dependencies: ["SeaLib"])
8+
]
69
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "Foo.h"
2+
#include<stdio.h>
3+
4+
int main() {
5+
printf("%d", foo(5));
6+
return 0;
7+
}

Sources/Build/Command.link(ClangModule).swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ extension Command {
3333

3434
args += try ClangModuleBuildMetadata.basicArgs() + otherArgs
3535
args += ["-L\(prefix)"]
36+
// Linux doesn't search executable directory for shared libs so embed runtime search path.
37+
#if os(Linux)
38+
args += ["-Xlinker", "-rpath=$ORIGIN"]
39+
#endif
3640
args += linkFlags
3741
args += objects
3842

Tests/Functional/ClangModuleTests.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ class ClangModulesTestCase: XCTestCase {
4040
XCTAssertFileExists(prefix, ".build", "debug", "SeaLib".soname)
4141
let exec = ".build/debug/SeaExec"
4242
XCTAssertFileExists(prefix, exec)
43-
let output = try popen([Path.join(prefix, exec)])
43+
var output = try popen([Path.join(prefix, exec)])
4444
XCTAssertEqual(output, "a = 5\n")
45+
let cExec = ".build/debug/CExec"
46+
output = try popen([Path.join(prefix, cExec)])
47+
XCTAssertEqual(output, "5")
4548
}
4649
}
4750

Tests/Xcodeproj/FunctionalTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class FunctionalTests: XCTestCase {
4141
let build = Path.join(prefix, "build", "Debug")
4242
XCTAssertDirectoryExists(build, "SeaLib.framework")
4343
XCTAssertFileExists(build, "SeaExec")
44+
XCTAssertFileExists(build, "CExec")
4445
}
4546
}
4647

0 commit comments

Comments
 (0)