Skip to content

Commit

Permalink
Add unmanagedPlatformDependentNativeDirectories (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
pomadchin authored Aug 1, 2021
1 parent 0a26c38 commit 051cf3a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
19 changes: 17 additions & 2 deletions plugin/src/main/scala/com/github/sbt/jni/plugins/JniPackage.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.sbt.jni
package plugins

import util.CollectionOps
import sbt._
import sbt.Keys._
import sbt.io.Path._
Expand Down Expand Up @@ -29,6 +30,13 @@ object JniPackage extends AutoPlugin {
"the x86_64 architecture and running the Linux kernel."
)

val unmanagedPlatformDependentNativeDirectories = settingKey[Seq[(String, File)]](
"Unmanaged directories containing native libraries. The libraries must be regular files " +
"contained in a subdirectory corresponding to a platform. For example " +
"`<unmanagedNativeDirectory>/x86_64-linux/libfoo.so` is an unmanaged library for machines having " +
"the x86_64 architecture and running the Linux kernel."
)

val unmanagedNativeLibraries = taskKey[Seq[(File, String)]](
"Reads `unmanagedNativeDirectories` and maps libraries to their locations on the classpath " +
"(i.e. their path in a fat jar)."
Expand All @@ -45,16 +53,23 @@ object JniPackage extends AutoPlugin {
}
import autoImport._
import JniNative.autoImport._
import CollectionOps._

lazy val settings: Seq[Setting[_]] = Seq(
enableNativeCompilation := true,
unmanagedNativeDirectories := Seq(baseDirectory.value / "lib_native"),
unmanagedPlatformDependentNativeDirectories := Seq(nativePlatform.value -> baseDirectory.value / "lib_native"),
unmanagedNativeLibraries := {
val mappings: Seq[(File, String)] = unmanagedNativeDirectories.value.flatMap { dir =>
val files: Seq[File] = (dir ** "*").get.filter(_.isFile)
files.pair(rebase(dir, "/native"))
}
mappings
val mappingsPlatform: Seq[(File, String)] = unmanagedPlatformDependentNativeDirectories.value.flatMap {
case (platform, dir) =>
val files: Seq[File] = (dir ** "*").get.filter(_.isFile)
files.pair(rebase(dir, s"/native/$platform"))
}
mappings ++ mappingsPlatform
},
managedNativeLibraries := Def
.taskDyn[Seq[(File, String)]] {
Expand All @@ -71,7 +86,7 @@ object JniPackage extends AutoPlugin {
}
}
.value,
nativeLibraries := unmanagedNativeLibraries.value ++ managedNativeLibraries.value,
nativeLibraries := (unmanagedNativeLibraries.value ++ managedNativeLibraries.value).distinctBy(_._2),
resourceGenerators += Def.task {
val libraries: Seq[(File, String)] = nativeLibraries.value
val resources: Seq[File] = for ((file, path) <- libraries) yield {
Expand Down
27 changes: 27 additions & 0 deletions plugin/src/main/scala/com/github/sbt/jni/util/CollectionOps.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.sbt.jni
package util

import scala.collection.IterableLike
import scala.collection.generic.CanBuildFrom
import scala.language.implicitConversions

class CollectionOps[A, Repr](xs: IterableLike[A, Repr]) {
def distinctBy[B, That](f: A => B)(implicit cbf: CanBuildFrom[Repr, A, That]) = {
val builder = cbf(xs.repr)
val i = xs.iterator
var set = Set[B]()
while (i.hasNext) {
val o = i.next
val b = f(o)
if (!set(b)) {
set += b
builder += o
}
}
builder.result
}
}

object CollectionOps {
implicit def toCollectionOps[A, Repr](xs: IterableLike[A, Repr]) = new CollectionOps(xs)
}

0 comments on commit 051cf3a

Please sign in to comment.