@@ -212,8 +212,7 @@ lazy val bindings = project("bindings")
212
212
libutf8proc
213
213
)
214
214
215
- lazy val libiconv = bindingProject(" iconv" )
216
- .configure(binding(" iconv.h" ))
215
+ lazy val libiconv = bindingProject(" iconv" )(" iconv.h" )
217
216
.settings(
218
217
// #sbt-iconv-linking-options
219
218
Compile / nativeLinkingOptions ++= {
@@ -230,14 +229,15 @@ lazy val libiconv = bindingProject("iconv")
230
229
)
231
230
232
231
// #sbt-binding-project-multi-header
233
- lazy val libposix = bindingProject(" posix" )
234
- .configure(binding(" fnmatch.h" ))
235
- .configure(binding(" regex.h" ))
232
+ lazy val libposix = bindingProject(" posix" )(
233
+ " fnmatch.h" ,
234
+ " regex.h"
235
+ )
236
236
// #sbt-binding-project-multi-header
237
237
238
238
// #sbt-binding-project
239
- lazy val libutf8proc = bindingProject( " utf8proc " )
240
- .configure(binding( " utf8proc.h " , Some (" utf8proc" )))
239
+ lazy val libutf8proc =
240
+ bindingProject( " utf8proc" , Some (" utf8proc" ))( " utf8proc.h " )
241
241
// #sbt-binding-project
242
242
243
243
def project (name : String , plugged : AutoPlugin * ) = {
@@ -333,45 +333,51 @@ lazy val bindingsExtraArgs = Try {
333
333
s " -I $libDir/clang/ $version/include "
334
334
}.toOption
335
335
336
- def bindingProject (name : String ) = {
336
+ def bindingProject (name : String , link : Option [String ] = None )(
337
+ headers : String * ) = {
338
+ val packagePath = Seq (" org" , " scalanative" , " bindings" ) ++
339
+ (if (headers.length > 1 ) Seq (name) else Seq .empty)
340
+
337
341
nativeProject(s " lib $name" )
338
342
.enablePlugins(ScalaNativeBindgenPlugin )
339
343
.in(file(s " bindings/ $name" ))
340
344
.settings(
345
+ Keys .name := name,
341
346
libraryDependencies += " org.scalatest" %%% " scalatest" % " 3.2.0-SNAP10" % Test ,
342
- Compile / nativeBindgen / target :=
343
- (Compile / scalaSource).value / " org/scalanative/bindgen/bindings" / name
347
+ Compile / nativeBindgen / target := {
348
+ packagePath.foldLeft((Compile / scalaSource).value)(_ / _)
349
+ }
344
350
)
351
+ .settings(
352
+ headers.flatMap(binding(name, packagePath.mkString(" ." ), link)): _* )
345
353
}
346
354
347
- def binding (header : String , link : Option [String ] = None )(
348
- project : Project ): Project = {
349
- val headerFile = file(" /usr/include" ) / header
350
- val libname = project.base.getName
351
- project.settings(
352
- inConfig(Compile )(
355
+ def binding (name : String , packageName : String , link : Option [String ])(
356
+ header : String ): Seq [Setting [_]] = {
357
+ val includeDirs = Seq (" /usr/include" , " /usr/local/include" )
358
+ val headerFiles = includeDirs.map(dir => file(dir) / header).filter(_.exists)
359
+
360
+ headerFiles.headOption match {
361
+ case Some (headerFile) =>
353
362
Def .settings(
354
- nativeBindings ++= {
355
- if (headerFile.exists) Seq {
356
- NativeBinding (headerFile)
357
- .name(header.replace(" .h" , " " ))
358
- .packageName(s " org.scalanative.bindgen.bindings. $libname" )
359
- .maybe(link, _.link)
360
- .excludePrefix(" __" )
361
- .extraArgs(bindingsExtraArgs.toSeq: _* )
362
- .extraArgs(" -D_POSIX_C_SOURCE" )
363
- } else {
364
- Seq .empty
365
- }
363
+ organization := " org.scala-native.binding" ,
364
+ Compile / nativeBindings += {
365
+ NativeBinding (headerFile)
366
+ .name(link.map(_ => name).getOrElse(header.replace(" .h" , " " )))
367
+ .packageName(packageName)
368
+ .maybe(link, _.link)
369
+ .excludePrefix(" __" )
370
+ .extraArgs(bindingsExtraArgs.toSeq: _* )
371
+ .extraArgs(" -D_POSIX_C_SOURCE" )
366
372
}
367
- )),
368
- test := ( Def .taskDyn {
369
- if (headerFile.exists)
370
- Def .task { ( Test / test).value } else
371
- Def .task {
372
- streams.value.log.warn(
373
- s " Skipping $libname tests due to missing header file $headerFile " )
373
+ )
374
+
375
+ case None =>
376
+ Def .settings(
377
+ test := {
378
+ streams.value.log
379
+ .warn( s " Skipping $name tests due to missing header file < $header > " )
374
380
}
375
- }).value
376
- )
381
+ )
382
+ }
377
383
}
0 commit comments