|
| 1 | +import scala.scalanative.sbtplugin.ScalaNativePluginInternal.nativeWorkdir |
| 2 | +import scala.sys.process._ |
| 3 | +import java.nio.file.Path |
| 4 | + |
1 | 5 | inThisBuild( |
2 | 6 | Def.settings( |
3 | 7 | organization := "org.scalanative.bindgen", |
@@ -32,4 +36,50 @@ val tests = project |
32 | 36 | lazy val samples = project |
33 | 37 | .in(file("tests/samples")) |
34 | 38 | .enablePlugins(ScalaNativePlugin) |
35 | | - .settings(test := (compile in Compile).value) |
| 39 | + .settings( |
| 40 | + libraryDependencies += "com.lihaoyi" %%% "utest" % "0.6.3" % "test", |
| 41 | + testFrameworks += new TestFramework("utest.runner.Framework"), |
| 42 | + nativeLinkStubs := true, |
| 43 | + Test / nativeLinkingOptions += { |
| 44 | + val cwd = (nativeWorkdir in Test).value.getAbsoluteFile / "bindgen" |
| 45 | + s"-L$cwd" |
| 46 | + }, |
| 47 | + Test / compile := { |
| 48 | + 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 |
| 52 | + val clangPath = nativeClang.value.toPath.toAbsolutePath.toString |
| 53 | + |
| 54 | + cwd.mkdirs() |
| 55 | + |
| 56 | + def abs(path: File): String = |
| 57 | + path.getAbsolutePath.toString |
| 58 | + |
| 59 | + def run(command: Seq[String]): Int = { |
| 60 | + log.info("Running " + command.mkString(" ")) |
| 61 | + Process(command, cwd) ! log |
| 62 | + } |
| 63 | + |
| 64 | + val opaths = cpaths.map { cpath => |
| 65 | + val opath = abs(cwd / s"${cpath.getName}.o") |
| 66 | + val command = Seq(clangPath) ++ compileOptions ++ Seq("-c", |
| 67 | + abs(cpath), |
| 68 | + "-o", |
| 69 | + opath) |
| 70 | + |
| 71 | + if (run(command) != 0) { |
| 72 | + sys.error(s"Failed to compile $cpath") |
| 73 | + } |
| 74 | + opath |
| 75 | + } |
| 76 | + |
| 77 | + val archivePath = cwd / "libbindgentests.a" |
| 78 | + val archive = Seq("ar", "rs", abs(archivePath)) ++ opaths |
| 79 | + if (run(archive) != 0) { |
| 80 | + sys.error(s"Failed to create archive $archivePath") |
| 81 | + } |
| 82 | + |
| 83 | + (Test / compile).value |
| 84 | + } |
| 85 | + ) |
0 commit comments