Skip to content

Commit 4d27770

Browse files
committed
Rename Builder to Bindgen and update API implementation
1 parent cbc1fc0 commit 4d27770

File tree

2 files changed

+163
-111
lines changed

2 files changed

+163
-111
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package org.scalanative.bindgen
2+
3+
import java.io.File
4+
5+
import scala.collection.mutable
6+
import scala.sys.process.Process
7+
8+
sealed trait Bindgen {
9+
10+
/**
11+
* Set bindgen executable
12+
*/
13+
def bindgenExecutable(executable: File): Bindgen
14+
15+
/**
16+
* Set header file for which bindings will be generated
17+
*/
18+
def header(header: File): Bindgen
19+
20+
/**
21+
* Library to link with, e.g. -luv for libuv
22+
*/
23+
def link(library: String): Bindgen
24+
25+
/**
26+
* Name of Scala object that contains bindings.
27+
* Default is set to library name.
28+
*/
29+
def scalaObjectName(scalaObjectName: String): Bindgen
30+
31+
/**
32+
* Package name of generated Scala file
33+
*/
34+
def packageName(packageName: String): Bindgen
35+
36+
/**
37+
* Declarations will be removed if their names
38+
* contain given prefix
39+
*/
40+
def excludePrefix(prefix: String): Bindgen
41+
42+
/**
43+
* Additional argument to append to the compiler command line
44+
*/
45+
def extraArg(arg: String): Bindgen
46+
47+
/**
48+
* Additional argument to append to the compiler command line
49+
*/
50+
def extraArgBefore(arg: String): Bindgen
51+
52+
/**
53+
* Run binding generator
54+
*/
55+
def generate(): Bindings
56+
}
57+
58+
object Bindgen {
59+
def apply(): Bindgen = Impl()
60+
61+
private final case class Impl() extends Bindgen {
62+
private var executable: File = _
63+
private var library: String = _
64+
private var header: File = _
65+
private var scalaObjectName: String = _
66+
private var packageName: String = _
67+
private var excludePrefix: String = _
68+
private val extraArg: mutable.Seq[String] = mutable.Seq()
69+
private val extraArgBefore: mutable.Seq[String] = mutable.Seq()
70+
71+
def bindgenExecutable(executable: File): Bindgen = {
72+
require(executable.exists())
73+
this.executable = executable
74+
this
75+
}
76+
77+
def header(header: File): Bindgen = {
78+
require(header.exists())
79+
this.header = header
80+
this
81+
}
82+
83+
def link(library: String): Bindgen = {
84+
require(!library.isEmpty)
85+
this.library = library
86+
this
87+
}
88+
89+
def scalaObjectName(scalaObjectName: String): Bindgen = {
90+
require(!scalaObjectName.isEmpty)
91+
this.scalaObjectName = scalaObjectName
92+
this
93+
}
94+
95+
def packageName(packageName: String): Bindgen = {
96+
require(!packageName.isEmpty)
97+
this.packageName = packageName
98+
this
99+
}
100+
101+
def excludePrefix(prefix: String): Bindgen = {
102+
require(!prefix.isEmpty)
103+
excludePrefix = prefix
104+
this
105+
}
106+
107+
def extraArg(arg: String): Bindgen = {
108+
require(!arg.isEmpty)
109+
extraArg :+ arg
110+
this
111+
}
112+
113+
def extraArgBefore(arg: String): Bindgen = {
114+
require(!arg.isEmpty)
115+
extraArgBefore :+ arg
116+
this
117+
}
118+
119+
private def validateFields(): Unit = {
120+
if (executable == null) {
121+
throw new AssertionError("Specify bindgen executable")
122+
}
123+
if (header == null) {
124+
throw new AssertionError("Specify header file")
125+
}
126+
if (library == null) {
127+
throw new AssertionError("Specify library name")
128+
}
129+
}
130+
131+
def generate(): Bindings = {
132+
validateFields()
133+
134+
if (scalaObjectName == null) {
135+
scalaObjectName = library
136+
}
137+
var cmd = Seq(
138+
executable.getAbsolutePath,
139+
header.getAbsolutePath,
140+
"--name",
141+
scalaObjectName,
142+
"--link",
143+
library
144+
)
145+
146+
if (packageName != null) {
147+
cmd ++= Seq("--package", packageName)
148+
}
149+
150+
if (excludePrefix != null) {
151+
cmd :+= excludePrefix
152+
}
153+
154+
cmd :+= "--"
155+
156+
// TODO: extra args
157+
158+
val output = Process(cmd).lineStream.mkString("\n")
159+
160+
new Bindings(output)
161+
}
162+
}
163+
}

tools/src/main/scala/org/scalanative/bindgen/Builder.scala

Lines changed: 0 additions & 111 deletions
This file was deleted.

0 commit comments

Comments
 (0)