@@ -29,18 +29,10 @@ import org.scalanative.bindgen.Bindgen
29
29
* Keys are defined in [[ScalaNativeBindgenPlugin.autoImport ]].
30
30
*
31
31
* - `nativeBindgenPath`: Path to the `scala-native-bindgen` executable.
32
- * - `nativeBindgenHeader`: The C header file to read.
33
- *
34
- * - `nativeBindgenPackage`: Package of the enclosing object.
35
- * No package by default.
36
- *
37
- * - `name in nativeBindgen`: Name of the enclosing object.
38
- *
32
+ * - `nativeBindings`: Settings for each binding to generate.
33
+ * - `target in nativeBindgen`: Output folder of the generated code.
39
34
* - `version in nativeBindgen`: Version of the `scala-native-bindgen`
40
35
* to use when automatically downloading the executable.
41
- *
42
- * - `nativeBindgenLink`: Name of library to be linked.
43
- *
44
36
* - `nativeBindgen`: Generate Scala Native bindings.
45
37
*
46
38
* @example
@@ -53,62 +45,65 @@ import org.scalanative.bindgen.Bindgen
53
45
object ScalaNativeBindgenPlugin extends AutoPlugin {
54
46
55
47
object autoImport {
48
+ case class NativeBinding (
49
+ name : String ,
50
+ header : File ,
51
+ packageName : Option [String ],
52
+ link : Option [String ],
53
+ excludePrefix : Option [String ]
54
+ )
56
55
val ScalaNativeBindgen = config(" scala-native-bindgen" ).hide
57
56
val nativeBindgenPath =
58
57
taskKey[File ](" Path to the scala-native-bindgen executable" )
59
- val nativeBindgenHeader = taskKey[File ](" C header file" )
60
- val nativeBindgenPackage =
61
- settingKey[Option [String ]](" Package for the generated code" )
62
- val nativeBindgenLink =
63
- settingKey[Option [String ]](" Name of library to be linked" )
64
- val nativeBindgenExclude = settingKey[Option [String ]](" Exclude prefix" )
65
- val nativeBindgen = taskKey[File ](" Generate Scala Native bindings" )
58
+ val nativeBindings =
59
+ settingKey[Seq [NativeBinding ]](" Configuration for each bindings" )
60
+ val nativeBindgen = taskKey[Seq [File ]](" Generate Scala Native bindings" )
66
61
}
67
62
import autoImport ._
68
63
69
64
override def requires = plugins.JvmPlugin
70
65
71
66
override def projectSettings : Seq [Setting [_]] =
72
67
inConfig(ScalaNativeBindgen )(Defaults .configSettings) ++
73
- nativeBindgenScopedSettings(Compile ) ++
74
- Def .settings(
75
- ivyConfigurations += ScalaNativeBindgen ,
76
- version in nativeBindgen := BuildInfo .version,
77
- libraryDependencies ++= {
78
- artifactName.map { name =>
79
- val bindgenVersion = (version in nativeBindgen).value
80
- val url =
81
- s " ${BuildInfo .projectUrl}/releases/download/v $bindgenVersion/ $name"
82
-
83
- BuildInfo .organization % name % bindgenVersion % ScalaNativeBindgen from (url)
84
- }.toSeq
85
- },
86
- nativeBindgenPath := {
87
- val scalaNativeBindgenUpdate = (update in ScalaNativeBindgen ).value
88
-
89
- val artifactFile = artifactName match {
90
- case None =>
91
- sys.error(
92
- " No downloadable binaries available for your OS, " +
93
- " please provide path via `nativeBindgenPath`" )
94
- case Some (name) =>
95
- scalaNativeBindgenUpdate
96
- .select(artifact = artifactFilter(name = name))
97
- .head
98
- }
99
-
100
- // Set the executable bit on the expected path to fail if it doesn't exist
101
- for (view <- Option (
102
- Files .getFileAttributeView(artifactFile.toPath,
103
- classOf [PosixFileAttributeView ]))) {
104
- val permissions = view.readAttributes.permissions
105
- if (permissions.add(PosixFilePermission .OWNER_EXECUTE ))
106
- view.setPermissions(permissions)
68
+ nativeBindgenScopedSettings(Compile ) ++
69
+ Def .settings(
70
+ ivyConfigurations += ScalaNativeBindgen ,
71
+ version in nativeBindgen := BuildInfo .version,
72
+ libraryDependencies ++= {
73
+ artifactName.map { name =>
74
+ val bindgenVersion = (version in nativeBindgen).value
75
+ val url =
76
+ s " ${BuildInfo .projectUrl}/releases/download/v $bindgenVersion/ $name"
77
+
78
+ BuildInfo .organization % name % bindgenVersion % ScalaNativeBindgen from (url)
79
+ }.toSeq
80
+ },
81
+ nativeBindgenPath := {
82
+ val scalaNativeBindgenUpdate = (update in ScalaNativeBindgen ).value
83
+
84
+ val artifactFile = artifactName match {
85
+ case None =>
86
+ sys.error(
87
+ " No downloadable binaries available for your OS, " +
88
+ " please provide path via `nativeBindgenPath`" )
89
+ case Some (name) =>
90
+ scalaNativeBindgenUpdate
91
+ .select(artifact = artifactFilter(name = name))
92
+ .head
93
+ }
94
+
95
+ // Set the executable bit on the expected path to fail if it doesn't exist
96
+ for (view <- Option (
97
+ Files .getFileAttributeView(artifactFile.toPath,
98
+ classOf [PosixFileAttributeView ]))) {
99
+ val permissions = view.readAttributes.permissions
100
+ if (permissions.add(PosixFilePermission .OWNER_EXECUTE ))
101
+ view.setPermissions(permissions)
102
+ }
103
+
104
+ artifactFile
107
105
}
108
-
109
- artifactFile
110
- }
111
- )
106
+ )
112
107
113
108
private implicit class BindgenOps (val bindgen : Bindgen ) extends AnyVal {
114
109
def maybe [T ](opt : Option [T ], f : Bindgen => T => Bindgen ): Bindgen =
@@ -127,27 +122,30 @@ object ScalaNativeBindgenPlugin extends AutoPlugin {
127
122
def nativeBindgenScopedSettings (conf : Configuration ): Seq [Setting [_]] =
128
123
inConfig(conf)(
129
124
Def .settings(
130
- nativeBindgenHeader := {
131
- sys.error(" nativeBindgenHeader not configured" )
132
- },
133
- nativeBindgenPackage := None ,
134
- nativeBindgenExclude := None ,
135
- sourceGenerators += Def .task { Seq (nativeBindgen.value) },
136
- name in nativeBindgen := " ScalaNativeBindgen" ,
125
+ nativeBindings := Seq .empty,
126
+ sourceGenerators += Def .task { nativeBindgen.value },
127
+ target in nativeBindgen := sourceManaged.value / " sbt-scala-native-bindgen" ,
137
128
nativeBindgen := {
138
- val output = sourceManaged.value / " sbt-scala-native-bindgen" / " ScalaNativeBindgen.scala"
139
-
140
- Bindgen ()
141
- .bindgenExecutable(nativeBindgenPath.value)
142
- .header(nativeBindgenHeader.value)
143
- .name((name in nativeBindgen).value)
144
- .maybe(nativeBindgenLink.value, _.link)
145
- .maybe(nativeBindgenPackage.value, _.packageName)
146
- .maybe(nativeBindgenExclude.value, _.excludePrefix)
147
- .generate()
148
- .writeToFile(output)
149
-
150
- output
129
+ val bindgenPath = nativeBindgenPath.value
130
+ val bindings = nativeBindings.value
131
+ val outputDirectory = (target in nativeBindgen).value
132
+
133
+ bindings.map {
134
+ binding =>
135
+ val output = outputDirectory / s " ${binding.name}.scala "
136
+
137
+ Bindgen ()
138
+ .bindgenExecutable(bindgenPath)
139
+ .header(binding.header)
140
+ .name(binding.name)
141
+ .maybe(binding.link, _.link)
142
+ .maybe(binding.packageName, _.packageName)
143
+ .maybe(binding.excludePrefix, _.excludePrefix)
144
+ .generate()
145
+ .writeToFile(output)
146
+
147
+ output
148
+ }
151
149
}
152
150
))
153
151
}
0 commit comments