Skip to content

Commit 375964e

Browse files
committed
Use custom linker script to force position of library args
1 parent 1d59167 commit 375964e

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

build.sbt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,23 @@ lazy val samples = project
4040
libraryDependencies += "com.lihaoyi" %%% "utest" % "0.6.3" % "test",
4141
testFrameworks += new TestFramework("utest.runner.Framework"),
4242
nativeLinkStubs := true,
43-
Test / nativeLinkingOptions += {
44-
val cwd = (nativeWorkdir in Test).value.getAbsoluteFile / "bindgen"
45-
s"-L$cwd"
43+
Test / nativeLinkingOptions ++= {
44+
val rootDir = (ThisBuild / baseDirectory).value.getAbsoluteFile
45+
val cwd = (Test / target).value.getAbsoluteFile / "bindgen"
46+
val linker = rootDir / "scripts" / "linker.sh"
47+
Seq(s"-L$cwd", s"-fuse-ld=$linker")
4648
},
4749
Test / compile := {
4850
val log = streams.value.log
49-
val cwd = (nativeWorkdir in Test).value.getAbsoluteFile / "bindgen"
50-
val compileOptions = nativeCompileOptions.value
51-
val cpaths = (baseDirectory.value ** "*.c").get
51+
val cwd = (Test / target).value / "bindgen"
52+
val compileOptions = (Test / nativeCompileOptions).value
53+
val cpaths = (baseDirectory.value.getAbsoluteFile * "*.c").get
5254
val clangPath = nativeClang.value.toPath.toAbsolutePath.toString
5355

5456
cwd.mkdirs()
5557

5658
def abs(path: File): String =
57-
path.getAbsolutePath.toString
59+
path.getAbsolutePath
5860

5961
def run(command: Seq[String]): Int = {
6062
log.info("Running " + command.mkString(" "))
@@ -75,7 +77,7 @@ lazy val samples = project
7577
}
7678

7779
val archivePath = cwd / "libbindgentests.a"
78-
val archive = Seq("ar", "rs", abs(archivePath)) ++ opaths
80+
val archive = Seq("ar", "cr", abs(archivePath)) ++ opaths
7981
if (run(archive) != 0) {
8082
sys.error(s"Failed to create archive $archivePath")
8183
}

scripts/linker.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
args=("$@")
4+
libs=()
5+
6+
for ((i=0; i<"${#args[@]}"; ++i)); do
7+
case ${args[i]} in
8+
-lbindgentests)
9+
libs+="${args[i]}"
10+
unset args[i]
11+
break
12+
;;
13+
esac
14+
done
15+
16+
ld "${args[@]}" "${libs[@]}"

0 commit comments

Comments
 (0)