Skip to content

Commit 10c68b5

Browse files
committed
Make the new bahavior configurable and compatible with the previous release so there is no implicit behavior change
1 parent 911e358 commit 10c68b5

File tree

7 files changed

+35
-12
lines changed

7 files changed

+35
-12
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,18 @@ Since this plugin is basically a command-line wrapper, native build tools must f
153153

154154
An initial, compatible build template can be obtained by running `sbt nativeInit <tool>`. Once the native build tool initialised, projects are built by calling the `sbt nativeCompile` task.
155155

156-
Source and output directories are configurable
156+
Source and output directories are configurable:
157157
```scala
158158
nativeCompile / sourceDirectory := sourceDirectory.value / "native"
159159
nativeCompile / target := target.value / "native" / nativePlatform.value
160160
```
161161

162+
Some JNI projects may produce more than a single output. If that's an intended behavior it's possible to include all of the produced
163+
binaries into the native package:
164+
```scala
165+
nativeMultipleOutputs := true
166+
```
167+
162168
#### CMake
163169

164170
A regular `CMake` native project definition usually looks this following way:

plugin/src/main/scala/com/github/sbt/jni/build/BuildTool.scala

+11-2
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ trait BuildTool {
7777
/**
7878
* Get an instance (build configuration) of this tool, in the specified directory.
7979
*/
80-
def getInstance(baseDirectory: File, buildDirectory: File, logger: Logger): Instance
80+
def getInstance(baseDirectory: File, buildDirectory: File, logger: Logger, multipleOutputs: Boolean): Instance
8181

8282
/**
8383
* At least one produced library is expected.
8484
*/
85-
def validate(list: List[File], logger: Logger): List[File] = {
85+
def validate(list: List[File], multipleOutputs: Boolean, logger: Logger): List[File] = {
8686
list match {
8787
case Nil =>
8888
sys.error(
@@ -91,6 +91,15 @@ trait BuildTool {
9191
)
9292
case list @ _ :: Nil =>
9393
list
94+
95+
case head :: _ if !multipleOutputs =>
96+
logger.warn(
97+
"More than one file was created during compilation, " +
98+
s"only the first one (${head.getAbsolutePath}) will be used." +
99+
"Consider setting nativeMultipleOutputs := true."
100+
)
101+
List(head)
102+
94103
case list =>
95104
logger.info("More than one file was created during compilation.")
96105
list

plugin/src/main/scala/com/github/sbt/jni/build/CMake.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ class CMake(protected val configuration: Seq[String]) extends BuildTool with Con
1414
"/com/github/sbt/jni/templates/CMakeLists.txt" -> "CMakeLists.txt"
1515
)
1616

17-
override def getInstance(baseDir: File, buildDir: File, logger: Logger) = new Instance {
17+
override def getInstance(baseDir: File, buildDir: File, logger: Logger, nativeMultipleOutputs: Boolean) = new Instance {
1818

1919
override def log = logger
2020
override def baseDirectory = baseDir
2121
override def buildDirectory = buildDir
22+
override def multipleOutputs = multipleNativeOutputs
2223

2324
def cmakeProcess(args: String*): ProcessBuilder = Process("cmake" +: args, buildDirectory)
2425

plugin/src/main/scala/com/github/sbt/jni/build/Cargo.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ class Cargo(protected val configuration: Seq[String]) extends BuildTool {
1818

1919
def release: Boolean = configuration.exists(_.toLowerCase.contains("release"))
2020

21-
def getInstance(baseDirectory: File, buildDirectory: File, logger: sbt.Logger): Instance =
22-
new Instance(baseDirectory, logger)
21+
def getInstance(baseDirectory: File, buildDirectory: File, logger: sbt.Logger, multipleOutputs: Boolean): Instance =
22+
new Instance(baseDirectory, logger, multipleOutputs)
2323

24-
class Instance(protected val baseDirectory: File, protected val logger: sbt.Logger) extends super.Instance {
24+
class Instance(protected val baseDirectory: File, protected val logger: sbt.Logger, protected val multipleOutputs: Boolean) extends super.Instance {
2525
// IntelliJ friendly logger, IntelliJ doesn't start tests if a line is printed as "error", which Cargo does for regular output
2626
protected val log: ProcessLogger = new ProcessLogger {
2727
def out(s: => String): Unit = logger.info(s)
@@ -44,7 +44,7 @@ class Cargo(protected val configuration: Seq[String]) extends BuildTool {
4444
val products: List[File] =
4545
(targetDirectory / subdir * ("*.so" | "*.dylib")).get.filter(_.isFile).toList
4646

47-
validate(products, logger)
47+
validate(products, multipleOutputs, logger)
4848
}
4949
}
5050
}

plugin/src/main/scala/com/github/sbt/jni/build/ConfigureMakeInstall.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ trait ConfigureMakeInstall { self: BuildTool =>
1313
trait Instance extends self.Instance {
1414

1515
def log: Logger
16+
def multipleOutputs: Boolean
1617
def baseDirectory: File
1718
def buildDirectory: File
1819

@@ -36,7 +37,7 @@ trait ConfigureMakeInstall { self: BuildTool =>
3637
val products: List[File] =
3738
(targetDirectory ** ("*.so" | "*.dylib")).get.filter(_.isFile).toList
3839

39-
validate(products, log)
40+
validate(products, multipleOutputs, log)
4041
}
4142
}
4243

plugin/src/main/scala/com/github/sbt/jni/build/Meson.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ class Meson(protected val configuration: Seq[String]) extends BuildTool with Con
1515
"/com/github/sbt/jni/templates/meson.options" -> "meson.options"
1616
)
1717

18-
override def getInstance(baseDir: File, buildDir: File, logger: Logger) = new Instance {
18+
override def getInstance(baseDir: File, buildDir: File, logger: Logger, nativeMultipleOutputs: Boolean) = new Instance {
1919

2020
override def log = logger
2121
override def baseDirectory = baseDir
2222
override def buildDirectory = buildDir
23+
override def multipleOutputs = multipleNativeOutputs
2324

2425
def mesonProcess(args: String*): ProcessBuilder = Process("meson" +: args, buildDirectory)
2526

plugin/src/main/scala/com/github/sbt/jni/plugins/JniNative.scala

+7-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ object JniNative extends AutoPlugin {
3030
"Initialize a native build script from a template."
3131
)
3232

33+
val nativeMultipleOutputs = taskKey[Boolean](
34+
"Enable multiple native outputs support. Disabled by default."
35+
)
36+
3337
}
3438
import autoImport._
3539

@@ -79,8 +83,8 @@ object JniNative extends AutoPlugin {
7983
tools.map(_.name).mkString(",")
8084
)
8185
)
82-
8386
},
87+
nativeMultipleOutputs := false,
8488
nativeBuildToolInstance := {
8589
val tool = nativeBuildTool.value
8690
val srcDir = (nativeCompile / sourceDirectory).value
@@ -89,7 +93,8 @@ object JniNative extends AutoPlugin {
8993
tool.getInstance(
9094
baseDirectory = srcDir,
9195
buildDirectory = buildDir,
92-
logger = streams.value.log
96+
logger = streams.value.log,
97+
multipleOutputs = nativeMultipleOutputs.value
9398
)
9499
},
95100
nativeCompile / clean := {

0 commit comments

Comments
 (0)