Skip to content

Commit 1d59167

Browse files
committed
Implement end-to-end testing for function generation
1 parent 543d603 commit 1d59167

File tree

15 files changed

+158
-18
lines changed

15 files changed

+158
-18
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ RUN set -x \
2828
libclang-$LLVM_VERSION-dev llvm-$LLVM_VERSION-dev \
2929
&& rm -rf /var/lib/apt/lists/*
3030

31+
ENV PATH=$PATH:/usr/lib/llvm-$LLVM_VERSION/bin
3132
WORKDIR /src

build.sbt

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import scala.scalanative.sbtplugin.ScalaNativePluginInternal.nativeWorkdir
2+
import scala.sys.process._
3+
import java.nio.file.Path
4+
15
inThisBuild(
26
Def.settings(
37
organization := "org.scalanative.bindgen",
@@ -32,4 +36,50 @@ val tests = project
3236
lazy val samples = project
3337
.in(file("tests/samples"))
3438
.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+
)

tests/samples/Enum.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
package org.scalanative.bindgen.samples
2+
13
import scala.scalanative._
24
import scala.scalanative.native._
35

4-
@native.link("Enum")
6+
@native.link("bindgentests")
57
@native.extern
68
object Enum {
79
type enum_days = native.CUnsignedInt

tests/samples/Function.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdarg.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
#include "Function.h"
6+
7+
int no_args() { return 42; }
8+
9+
float void_arg(void) { return 1.5; }
10+
11+
char one_arg(int a) { return (char)a; }
12+
13+
void *two_args(float a, int b) {
14+
static char buf[128];
15+
if (snprintf(buf, sizeof(buf), "%.2f %d", a, b) > sizeof(buf))
16+
return "oops";
17+
return buf;
18+
}
19+
20+
double anonymous_args(float a, int b) { return a + b; }
21+
22+
double variadic_args(double a, char *varArgs, ...) {
23+
va_list args;
24+
25+
va_start(args, varArgs);
26+
for (; *varArgs; varArgs++)
27+
a += (*varArgs - '0') * va_arg(args, int);
28+
return a;
29+
}

tests/samples/Function.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
int no_args();
2-
int void_arg(void);
3-
void one_arg(int a);
2+
float void_arg(void);
3+
char one_arg(int a);
44
void *two_args(float a, int b);
5-
void anonymous_args(float, int);
6-
double variadic_args(double a, void *varArgs, ...);
5+
double anonymous_args(float, int);
6+
double variadic_args(double a, char *varArgs, ...);

tests/samples/Function.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
package org.scalanative.bindgen.samples
2+
13
import scala.scalanative._
24
import scala.scalanative.native._
35

4-
@native.link("Function")
6+
@native.link("bindgentests")
57
@native.extern
68
object Function {
79
def no_args(): native.CInt = native.extern
8-
def void_arg(): native.CInt = native.extern
9-
def one_arg(a: native.CInt): Unit = native.extern
10+
def void_arg(): native.CFloat = native.extern
11+
def one_arg(a: native.CInt): native.CChar = native.extern
1012
def two_args(a: native.CFloat, b: native.CInt): native.Ptr[Byte] = native.extern
11-
def anonymous_args(anonymous0: native.CFloat, anonymous1: native.CInt): Unit = native.extern
12-
def variadic_args(a: native.CDouble, varArgs: native.Ptr[Byte], varArgs0: native.CVararg*): native.CDouble = native.extern
13+
def anonymous_args(anonymous0: native.CFloat, anonymous1: native.CInt): native.CDouble = native.extern
14+
def variadic_args(a: native.CDouble, varArgs: native.CString, varArgs0: native.CVararg*): native.CDouble = native.extern
1315
}

tests/samples/NativeTypes.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
package org.scalanative.bindgen.samples
2+
13
import scala.scalanative._
24
import scala.scalanative.native._
35

4-
@native.link("NativeTypes")
6+
@native.link("bindgentests")
57
@native.extern
68
object NativeTypes {
79
type size_t = native.CUnsignedInt

tests/samples/PrivateMembers.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
package org.scalanative.bindgen.samples
2+
13
import scala.scalanative._
24
import scala.scalanative.native._
35

4-
@native.link("PrivateMembers")
6+
@native.link("bindgentests")
57
@native.extern
68
object PrivateMembers {
79
type pid_t = native.CInt

tests/samples/ReservedWords.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
package org.scalanative.bindgen.samples
2+
13
import scala.scalanative._
24
import scala.scalanative.native._
35

4-
@native.link("ReservedWords")
6+
@native.link("bindgentests")
57
@native.extern
68
object ReservedWords {
79
type `match` = native.CInt

tests/samples/Struct.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
package org.scalanative.bindgen.samples
2+
13
import scala.scalanative._
24
import scala.scalanative.native._
35

4-
@native.link("Struct")
6+
@native.link("bindgentests")
57
@native.extern
68
object Struct {
79
type point_s = native.Ptr[struct_point]

0 commit comments

Comments
 (0)