From 0577d7927c86b506faa658a5481984199478ba5c Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Wed, 5 May 2021 19:49:53 +0200 Subject: [PATCH 01/14] Add support for Cargo (Rust) --- README.md | 1 + .../ch/jodersky/sbt/jni/templates/Cargo.toml | 11 +++ .../ch/jodersky/sbt/jni/templates/src/lib.rs | 24 +++++++ .../ch/jodersky/sbt/jni/build/BuildTool.scala | 1 + .../ch/jodersky/sbt/jni/build/Cargo.scala | 70 +++++++++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 plugin/src/main/resources/ch/jodersky/sbt/jni/templates/Cargo.toml create mode 100644 plugin/src/main/resources/ch/jodersky/sbt/jni/templates/src/lib.rs create mode 100644 plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala diff --git a/README.md b/README.md index 9254af3e..3f1eccab 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,7 @@ JniNative adds the capability of building native code (compiling and linking) to Since this plugin is basically a command-line wrapper, native build tools must follow certain calling conventions to be compatible. The supported build tools are currently: - CMake +- Cargo (Rust) An initial, compatible build template can be obtained by running `sbt nativeInit `. Once the native build tool initialised, projects are built by calling the `sbt nativeCompile` task. diff --git a/plugin/src/main/resources/ch/jodersky/sbt/jni/templates/Cargo.toml b/plugin/src/main/resources/ch/jodersky/sbt/jni/templates/Cargo.toml new file mode 100644 index 00000000..92ce7832 --- /dev/null +++ b/plugin/src/main/resources/ch/jodersky/sbt/jni/templates/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "{{project}}" +version = "0.1.0" +authors = ["John Doe "] +edition = "2018" + +[dependencies] +jni = "0.19" + +[lib] +crate_type = ["cdylib"] diff --git a/plugin/src/main/resources/ch/jodersky/sbt/jni/templates/src/lib.rs b/plugin/src/main/resources/ch/jodersky/sbt/jni/templates/src/lib.rs new file mode 100644 index 00000000..1c926bba --- /dev/null +++ b/plugin/src/main/resources/ch/jodersky/sbt/jni/templates/src/lib.rs @@ -0,0 +1,24 @@ +// This is the interface to the JVM that we'll call the majority of our +// methods on. +use jni::JNIEnv; + +// These objects are what you should use as arguments to your native +// function. They carry extra lifetime information to prevent them escaping +// this context and getting used after being GC'd. +use jni::objects::JObject; + +use jni::sys::jint; + +// This keeps Rust from "mangling" the name and making it unique for this +// crate. +#[no_mangle] +pub extern "system" fn Java_org_example_Adder_plus( + env: JNIEnv, + object: JObject, + term: jint, +) -> jint { + let base = env.get_field(object, "base", "I").unwrap().i().unwrap(); + println!("Printing from rust library. base: {}", base); + println!("Printing from rust library. term: {}", term); + base + term +} diff --git a/plugin/src/main/scala/ch/jodersky/sbt/jni/build/BuildTool.scala b/plugin/src/main/scala/ch/jodersky/sbt/jni/build/BuildTool.scala index 931ee583..d37dd014 100644 --- a/plugin/src/main/scala/ch/jodersky/sbt/jni/build/BuildTool.scala +++ b/plugin/src/main/scala/ch/jodersky/sbt/jni/build/BuildTool.scala @@ -37,6 +37,7 @@ trait BuildTool { baseDirectory.mkdir() val out = baseDirectory.toPath().resolve(name) + Files.createDirectories(out.getParent) Files.write(out, replaced.getBytes) out.toFile() } diff --git a/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala b/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala new file mode 100644 index 00000000..55149829 --- /dev/null +++ b/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala @@ -0,0 +1,70 @@ +package ch.jodersky.sbt.jni.build + +import sbt._ + +import java.io.File +import scala.sys.process._ + +class Cargo(protected val release: Boolean = true) extends BuildTool { + + def name: String = "Cargo" + + def detect(baseDirectory: File): Boolean = + baseDirectory.list().contains("Cargo.toml") + + protected def templateMappings: List[(String, String)] = List( + "/ch/jodersky/sbt/jni/templates/Cargo.toml" -> "Cargo.toml", + "/ch/jodersky/sbt/jni/templates/src/lib.rs" -> "src/lib.rs", + ) + + def getInstance(baseDirectory: File, buildDirectory: File, logger: sbt.Logger): super.Instance = + new Instance(baseDirectory, logger) + + class Instance(protected val baseDirectory: File, protected val logger: sbt.Logger) extends super.Instance { + // IntelliJ friendly logger, IntelliJ doesn't start tests if a line is printed as "error", which Cargo does for regular output + protected val log: ProcessLogger = new ProcessLogger { + def out(s: => String): Unit = logger.info(s) + def err(s: => String): Unit = logger.warn(s) + def buffer[T](f: => T): T = f + } + + def clean(): Unit = + Process("cargo clean", baseDirectory) ! log + + def library(targetDirectory: File): File = { + val releaseFlag = if (release) "--release " else "" + val ev = + Process( + s"cargo build $releaseFlag--target-dir ${targetDirectory.getAbsolutePath}", + baseDirectory, + ) ! log + if (ev != 0) sys.error(s"Building native library failed. Exit code: $ev") + + val subdir = if (release) "release" else "debug" + val products: List[File] = + (targetDirectory / subdir * ("*.so" | "*.dylib")).get.filter(_.isFile).toList + + // only one produced library is expected + products match { + case Nil => + sys.error( + s"No files were created during compilation, " + + s"something went wrong with the $name configuration.", + ) + case head :: Nil => + head + case head :: _ => + logger.warn( + "More than one file was created during compilation, " + + s"only the first one (${head.getAbsolutePath}) will be used.", + ) + head + } + } + } +} + +object Cargo { + /** If `release` is `true`, `cargo build` will run with the `--release` flag. */ + def make(release: Boolean = true): BuildTool = new Cargo(release) +} From e5b1f12af5f6ad734c19c4e990df2bc1e1aebbac Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Wed, 5 May 2021 20:57:14 +0200 Subject: [PATCH 02/14] small tweak --- plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala b/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala index 55149829..bc5595d8 100644 --- a/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala +++ b/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala @@ -17,7 +17,7 @@ class Cargo(protected val release: Boolean = true) extends BuildTool { "/ch/jodersky/sbt/jni/templates/src/lib.rs" -> "src/lib.rs", ) - def getInstance(baseDirectory: File, buildDirectory: File, logger: sbt.Logger): super.Instance = + def getInstance(baseDirectory: File, buildDirectory: File, logger: sbt.Logger): Instance = new Instance(baseDirectory, logger) class Instance(protected val baseDirectory: File, protected val logger: sbt.Logger) extends super.Instance { From 2e645d65eb1cff914a8932ec102b4e760b96141b Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Mon, 26 Jul 2021 12:33:51 +0200 Subject: [PATCH 03/14] Add simple-cargo test --- .../sbt-test/sbt-jni/simple-cargo/README.md | 1 + .../sbt-test/sbt-jni/simple-cargo/build.sbt | 11 +++++++++ .../src/main/scala/simplecargo/Adder.scala | 8 +++++++ .../src/main/scala/simplecargo/Main.scala | 12 ++++++++++ .../src/test/scala/simplecargo/Test.scala | 11 +++++++++ .../sbt-jni/simple-cargo/native/src/lib.rs | 24 +++++++++++++++++++ .../simple-cargo/project/ScriptedHelper.scala | 15 ++++++++++++ .../simple-cargo/project/build.properties | 1 + .../sbt-jni/simple-cargo/project/plugins.sbt | 3 +++ plugin/src/sbt-test/sbt-jni/simple-cargo/test | 3 +++ 10 files changed, 89 insertions(+) create mode 100644 plugin/src/sbt-test/sbt-jni/simple-cargo/README.md create mode 100644 plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt create mode 100644 plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/main/scala/simplecargo/Adder.scala create mode 100644 plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/main/scala/simplecargo/Main.scala create mode 100644 plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/test/scala/simplecargo/Test.scala create mode 100644 plugin/src/sbt-test/sbt-jni/simple-cargo/native/src/lib.rs create mode 100644 plugin/src/sbt-test/sbt-jni/simple-cargo/project/ScriptedHelper.scala create mode 100644 plugin/src/sbt-test/sbt-jni/simple-cargo/project/build.properties create mode 100644 plugin/src/sbt-test/sbt-jni/simple-cargo/project/plugins.sbt create mode 100644 plugin/src/sbt-test/sbt-jni/simple-cargo/test diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/README.md b/plugin/src/sbt-test/sbt-jni/simple-cargo/README.md new file mode 100644 index 00000000..354d1120 --- /dev/null +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/README.md @@ -0,0 +1 @@ +Very basic Rust/Cargo test. diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt b/plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt new file mode 100644 index 00000000..1bf43506 --- /dev/null +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt @@ -0,0 +1,11 @@ +ivyLoggingLevel := UpdateLogging.Quiet + +lazy val root = (project in file(".")).aggregate(core, native) + +lazy val core = project + .settings(libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.9" % Test) + .dependsOn(native % Runtime) + +lazy val native = project + .settings(nativeCompile / sourceDirectory := sourceDirectory.value) + .enablePlugins(JniNative) diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/main/scala/simplecargo/Adder.scala b/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/main/scala/simplecargo/Adder.scala new file mode 100644 index 00000000..db088f96 --- /dev/null +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/main/scala/simplecargo/Adder.scala @@ -0,0 +1,8 @@ +package simplecargo + +import ch.jodersky.jni.nativeLoader + +@nativeLoader("adder") +class Adder(val base: Int) { + @native def plus(term: Int): Int // implemented in libadder.so +} diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/main/scala/simplecargo/Main.scala b/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/main/scala/simplecargo/Main.scala new file mode 100644 index 00000000..8e4b6acf --- /dev/null +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/main/scala/simplecargo/Main.scala @@ -0,0 +1,12 @@ +package simplecargo + +object Main { + + def main(args: Array[String]): Unit = { + println("hello") + val adder = new Adder(1) + val sum = adder.plus(2) + println(s"sum: $sum") + } + +} diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/test/scala/simplecargo/Test.scala b/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/test/scala/simplecargo/Test.scala new file mode 100644 index 00000000..8da96531 --- /dev/null +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/test/scala/simplecargo/Test.scala @@ -0,0 +1,11 @@ +package simplecargo + +import org.scalatest.flatspec._ + +class SimpleSpec extends AnyFlatSpec { + + "Calling native methods in tests" should "work" in { + assert(new Adder(12).plus(34) == 46) + } + +} diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/native/src/lib.rs b/plugin/src/sbt-test/sbt-jni/simple-cargo/native/src/lib.rs new file mode 100644 index 00000000..53cb122a --- /dev/null +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/native/src/lib.rs @@ -0,0 +1,24 @@ +// This is the interface to the JVM that we'll call the majority of our +// methods on. +use jni::JNIEnv; + +// These objects are what you should use as arguments to your native +// function. They carry extra lifetime information to prevent them escaping +// this context and getting used after being GC'd. +use jni::objects::JObject; + +use jni::sys::jint; + +// This keeps Rust from "mangling" the name and making it unique for this +// crate. +#[no_mangle] +pub extern "system" fn Java_simplecargo_Adder_plus( + env: JNIEnv, + object: JObject, + term: jint, +) -> jint { + let base = env.get_field(object, "base", "I").unwrap().i().unwrap(); + println!("Printing from rust library. base: {}", base); + println!("Printing from rust library. term: {}", term); + base + term +} diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/project/ScriptedHelper.scala b/plugin/src/sbt-test/sbt-jni/simple-cargo/project/ScriptedHelper.scala new file mode 100644 index 00000000..75679d6f --- /dev/null +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/project/ScriptedHelper.scala @@ -0,0 +1,15 @@ +import sbt._ +import sbt.Keys._ + +object ScriptedHelper extends AutoPlugin { + + override def requires = empty + override def trigger = allRequirements + + override def projectSettings = Seq( + scalacOptions ++= Seq("-feature", "-deprecation"), + crossScalaVersions := Seq("2.13.6", "2.12.14"), + scalaVersion := crossScalaVersions.value.head + ) + +} diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/project/build.properties b/plugin/src/sbt-test/sbt-jni/simple-cargo/project/build.properties new file mode 100644 index 00000000..10fd9eee --- /dev/null +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.5.5 diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/project/plugins.sbt b/plugin/src/sbt-test/sbt-jni/simple-cargo/project/plugins.sbt new file mode 100644 index 00000000..9300e7cf --- /dev/null +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/project/plugins.sbt @@ -0,0 +1,3 @@ +ivyLoggingLevel := UpdateLogging.Quiet + +addSbtPlugin("com.github.sbt" % "sbt-jni" % System.getProperty("plugin.version")) diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/test b/plugin/src/sbt-test/sbt-jni/simple-cargo/test new file mode 100644 index 00000000..33d14117 --- /dev/null +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/test @@ -0,0 +1,3 @@ +> nativeInit cargo demo +> +test +> +core/run From 1c02c65fac4ac33bab3f948bf027b340c7ac1935 Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Mon, 26 Jul 2021 12:35:38 +0200 Subject: [PATCH 04/14] fmt --- .../scala/ch/jodersky/sbt/jni/build/Cargo.scala | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala b/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala index bc5595d8..45ff2ee5 100644 --- a/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala +++ b/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala @@ -14,7 +14,7 @@ class Cargo(protected val release: Boolean = true) extends BuildTool { protected def templateMappings: List[(String, String)] = List( "/ch/jodersky/sbt/jni/templates/Cargo.toml" -> "Cargo.toml", - "/ch/jodersky/sbt/jni/templates/src/lib.rs" -> "src/lib.rs", + "/ch/jodersky/sbt/jni/templates/src/lib.rs" -> "src/lib.rs" ) def getInstance(baseDirectory: File, buildDirectory: File, logger: sbt.Logger): Instance = @@ -36,7 +36,7 @@ class Cargo(protected val release: Boolean = true) extends BuildTool { val ev = Process( s"cargo build $releaseFlag--target-dir ${targetDirectory.getAbsolutePath}", - baseDirectory, + baseDirectory ) ! log if (ev != 0) sys.error(s"Building native library failed. Exit code: $ev") @@ -49,14 +49,14 @@ class Cargo(protected val release: Boolean = true) extends BuildTool { case Nil => sys.error( s"No files were created during compilation, " + - s"something went wrong with the $name configuration.", + s"something went wrong with the $name configuration." ) case head :: Nil => head case head :: _ => logger.warn( "More than one file was created during compilation, " + - s"only the first one (${head.getAbsolutePath}) will be used.", + s"only the first one (${head.getAbsolutePath}) will be used." ) head } @@ -65,6 +65,9 @@ class Cargo(protected val release: Boolean = true) extends BuildTool { } object Cargo { - /** If `release` is `true`, `cargo build` will run with the `--release` flag. */ + + /** + * If `release` is `true`, `cargo build` will run with the `--release` flag. + */ def make(release: Boolean = true): BuildTool = new Cargo(release) } From 3c503d4157134de83e6713223b25067c2931f848 Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Mon, 26 Jul 2021 12:41:49 +0200 Subject: [PATCH 05/14] Don't add lib.rs in nativeInit --- .../ch/jodersky/sbt/jni/templates/src/lib.rs | 24 ------------------- .../ch/jodersky/sbt/jni/build/Cargo.scala | 3 +-- .../src/test/scala/simplecargo/Test.scala | 2 +- 3 files changed, 2 insertions(+), 27 deletions(-) delete mode 100644 plugin/src/main/resources/ch/jodersky/sbt/jni/templates/src/lib.rs diff --git a/plugin/src/main/resources/ch/jodersky/sbt/jni/templates/src/lib.rs b/plugin/src/main/resources/ch/jodersky/sbt/jni/templates/src/lib.rs deleted file mode 100644 index 1c926bba..00000000 --- a/plugin/src/main/resources/ch/jodersky/sbt/jni/templates/src/lib.rs +++ /dev/null @@ -1,24 +0,0 @@ -// This is the interface to the JVM that we'll call the majority of our -// methods on. -use jni::JNIEnv; - -// These objects are what you should use as arguments to your native -// function. They carry extra lifetime information to prevent them escaping -// this context and getting used after being GC'd. -use jni::objects::JObject; - -use jni::sys::jint; - -// This keeps Rust from "mangling" the name and making it unique for this -// crate. -#[no_mangle] -pub extern "system" fn Java_org_example_Adder_plus( - env: JNIEnv, - object: JObject, - term: jint, -) -> jint { - let base = env.get_field(object, "base", "I").unwrap().i().unwrap(); - println!("Printing from rust library. base: {}", base); - println!("Printing from rust library. term: {}", term); - base + term -} diff --git a/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala b/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala index 45ff2ee5..46a3466c 100644 --- a/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala +++ b/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala @@ -13,8 +13,7 @@ class Cargo(protected val release: Boolean = true) extends BuildTool { baseDirectory.list().contains("Cargo.toml") protected def templateMappings: List[(String, String)] = List( - "/ch/jodersky/sbt/jni/templates/Cargo.toml" -> "Cargo.toml", - "/ch/jodersky/sbt/jni/templates/src/lib.rs" -> "src/lib.rs" + "/ch/jodersky/sbt/jni/templates/Cargo.toml" -> "Cargo.toml" ) def getInstance(baseDirectory: File, buildDirectory: File, logger: sbt.Logger): Instance = diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/test/scala/simplecargo/Test.scala b/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/test/scala/simplecargo/Test.scala index 8da96531..2ba0d495 100644 --- a/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/test/scala/simplecargo/Test.scala +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/test/scala/simplecargo/Test.scala @@ -5,7 +5,7 @@ import org.scalatest.flatspec._ class SimpleSpec extends AnyFlatSpec { "Calling native methods in tests" should "work" in { - assert(new Adder(12).plus(34) == 46) + assert(new Adder(12).plus(34) == 999) } } From cd9e06238d18e972f28bfcb025a1ab1f471d667e Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Mon, 26 Jul 2021 12:47:00 +0200 Subject: [PATCH 06/14] nativeInit Cargo --- plugin/src/sbt-test/sbt-jni/simple-cargo/test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/test b/plugin/src/sbt-test/sbt-jni/simple-cargo/test index 33d14117..66dd470a 100644 --- a/plugin/src/sbt-test/sbt-jni/simple-cargo/test +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/test @@ -1,3 +1,3 @@ -> nativeInit cargo demo +> nativeInit Cargo demo > +test > +core/run From 0103d087f39f889a3684bf59408d97bbfccb77d3 Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Mon, 26 Jul 2021 13:01:02 +0200 Subject: [PATCH 07/14] Find Cargo as a BuildTool --- .../main/scala/ch/jodersky/sbt/jni/build/BuildTool.scala | 7 +++++++ .../src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala | 5 +++++ .../scala/ch/jodersky/sbt/jni/plugins/JniNative.scala | 8 ++++---- plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt | 5 ++++- plugin/src/sbt-test/sbt-jni/simple-cargo/test | 2 +- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/plugin/src/main/scala/ch/jodersky/sbt/jni/build/BuildTool.scala b/plugin/src/main/scala/ch/jodersky/sbt/jni/build/BuildTool.scala index 00b18767..3b4e80ee 100644 --- a/plugin/src/main/scala/ch/jodersky/sbt/jni/build/BuildTool.scala +++ b/plugin/src/main/scala/ch/jodersky/sbt/jni/build/BuildTool.scala @@ -78,3 +78,10 @@ trait BuildTool { def getInstance(baseDirectory: File, buildDirectory: File, logger: Logger): Instance } + +object BuildTool { + lazy val buildTools: Map[String, BuildTool] = Map( + CMake.name.toLowerCase -> CMake, + Cargo.release.name.toLowerCase -> Cargo.release + ) +} diff --git a/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala b/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala index 46a3466c..44ad7423 100644 --- a/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala +++ b/plugin/src/main/scala/ch/jodersky/sbt/jni/build/Cargo.scala @@ -69,4 +69,9 @@ object Cargo { * If `release` is `true`, `cargo build` will run with the `--release` flag. */ def make(release: Boolean = true): BuildTool = new Cargo(release) + + /** + * Cargo build tool, with the `--release` flag. + */ + lazy val release: BuildTool = make() } diff --git a/plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala b/plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala index 0ed74eac..61a451b5 100644 --- a/plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala +++ b/plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala @@ -126,10 +126,10 @@ object JniNative extends AutoPlugin { val log = streams.value.log - def getTool(toolName: String): BuildTool = toolName.toLowerCase match { - case "cmake" => CMake - case _ => sys.error("Unsupported build tool: " + toolName) - } + def getTool(toolName: String): BuildTool = BuildTool.buildTools.getOrElse( + toolName.toLowerCase, + sys.error("Unsupported build tool: " + toolName) + ) val args = spaceDelimited(" []").parsed.toList diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt b/plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt index 1bf43506..dec1353e 100644 --- a/plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt @@ -7,5 +7,8 @@ lazy val core = project .dependsOn(native % Runtime) lazy val native = project - .settings(nativeCompile / sourceDirectory := sourceDirectory.value) + .settings( + nativeCompile / sourceDirectory := sourceDirectory.value, + nativeBuildTool := Cargo.release + ) .enablePlugins(JniNative) diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/test b/plugin/src/sbt-test/sbt-jni/simple-cargo/test index 66dd470a..33d14117 100644 --- a/plugin/src/sbt-test/sbt-jni/simple-cargo/test +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/test @@ -1,3 +1,3 @@ -> nativeInit Cargo demo +> nativeInit cargo demo > +test > +core/run From d71e1900dd9e36904494e5524c37f8a8d173d8b2 Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Mon, 26 Jul 2021 13:07:27 +0200 Subject: [PATCH 08/14] Detect Cargo --- .../main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala | 2 +- plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala b/plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala index 61a451b5..abebc984 100644 --- a/plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala +++ b/plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala @@ -62,7 +62,7 @@ object JniNative extends AutoPlugin { nativeCompile / sourceDirectory := sourceDirectory.value / "native", nativeCompile / target := target.value / "native" / nativePlatform.value, nativeBuildTool := { - val tools = Seq(CMake) + val tools = Seq(CMake, Cargo.release) val src = (nativeCompile / sourceDirectory).value diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt b/plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt index dec1353e..1bf43506 100644 --- a/plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt @@ -7,8 +7,5 @@ lazy val core = project .dependsOn(native % Runtime) lazy val native = project - .settings( - nativeCompile / sourceDirectory := sourceDirectory.value, - nativeBuildTool := Cargo.release - ) + .settings(nativeCompile / sourceDirectory := sourceDirectory.value) .enablePlugins(JniNative) From 70f8b33c370bc07b9520d6fdc154b30c36d62c60 Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Mon, 26 Jul 2021 13:21:41 +0200 Subject: [PATCH 09/14] nativeCompile / sourceDirectory := baseDirectory.value --- plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt | 2 +- plugin/src/sbt-test/sbt-jni/simple-cargo/project/plugins.sbt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt b/plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt index 1bf43506..c036c7e2 100644 --- a/plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/build.sbt @@ -7,5 +7,5 @@ lazy val core = project .dependsOn(native % Runtime) lazy val native = project - .settings(nativeCompile / sourceDirectory := sourceDirectory.value) + .settings(nativeCompile / sourceDirectory := baseDirectory.value) // `baseDirectory`, not `sourceDirectory` .enablePlugins(JniNative) diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/project/plugins.sbt b/plugin/src/sbt-test/sbt-jni/simple-cargo/project/plugins.sbt index 9300e7cf..568bfad4 100644 --- a/plugin/src/sbt-test/sbt-jni/simple-cargo/project/plugins.sbt +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/project/plugins.sbt @@ -1,3 +1,3 @@ ivyLoggingLevel := UpdateLogging.Quiet -addSbtPlugin("com.github.sbt" % "sbt-jni" % System.getProperty("plugin.version")) +addSbtPlugin("com.github.sbt" % "sbt-jni" % "1.4.1+35-d71e1900-SNAPSHOT") From 75b31434f50653ceec1dbceda76b17d251afaf17 Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Mon, 26 Jul 2021 13:22:23 +0200 Subject: [PATCH 10/14] roll back plugins.sbt change --- plugin/src/sbt-test/sbt-jni/simple-cargo/project/plugins.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/project/plugins.sbt b/plugin/src/sbt-test/sbt-jni/simple-cargo/project/plugins.sbt index 568bfad4..9300e7cf 100644 --- a/plugin/src/sbt-test/sbt-jni/simple-cargo/project/plugins.sbt +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/project/plugins.sbt @@ -1,3 +1,3 @@ ivyLoggingLevel := UpdateLogging.Quiet -addSbtPlugin("com.github.sbt" % "sbt-jni" % "1.4.1+35-d71e1900-SNAPSHOT") +addSbtPlugin("com.github.sbt" % "sbt-jni" % System.getProperty("plugin.version")) From 3e7cec7426c42556bbbf256793789d38295b26a3 Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Mon, 26 Jul 2021 13:30:10 +0200 Subject: [PATCH 11/14] nativeInit cargo adder --- plugin/src/sbt-test/sbt-jni/simple-cargo/test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/test b/plugin/src/sbt-test/sbt-jni/simple-cargo/test index 33d14117..3777d5be 100644 --- a/plugin/src/sbt-test/sbt-jni/simple-cargo/test +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/test @@ -1,3 +1,3 @@ -> nativeInit cargo demo +> nativeInit cargo adder > +test > +core/run From 5e56a7a574d25c4b2c5b18207d918a89cddf7612 Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Mon, 26 Jul 2021 14:01:36 +0200 Subject: [PATCH 12/14] fix test --- .../simple-cargo/core/src/test/scala/simplecargo/Test.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/test/scala/simplecargo/Test.scala b/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/test/scala/simplecargo/Test.scala index 2ba0d495..8da96531 100644 --- a/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/test/scala/simplecargo/Test.scala +++ b/plugin/src/sbt-test/sbt-jni/simple-cargo/core/src/test/scala/simplecargo/Test.scala @@ -5,7 +5,7 @@ import org.scalatest.flatspec._ class SimpleSpec extends AnyFlatSpec { "Calling native methods in tests" should "work" in { - assert(new Adder(12).plus(34) == 999) + assert(new Adder(12).plus(34) == 46) } } From e0a827015695af7472f7200b84dfe87b8f0c1e02 Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Mon, 26 Jul 2021 14:19:39 +0200 Subject: [PATCH 13/14] Reuse BuildTool.buildTools --- .../src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala b/plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala index abebc984..ac7477e7 100644 --- a/plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala +++ b/plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala @@ -62,7 +62,7 @@ object JniNative extends AutoPlugin { nativeCompile / sourceDirectory := sourceDirectory.value / "native", nativeCompile / target := target.value / "native" / nativePlatform.value, nativeBuildTool := { - val tools = Seq(CMake, Cargo.release) + val tools = BuildTool.buildTools.values.toList val src = (nativeCompile / sourceDirectory).value From f1dca96729c1c9ca745d0197a06f8c398d27e82b Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Mon, 26 Jul 2021 16:41:48 +0200 Subject: [PATCH 14/14] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e45f3d74..26602ca0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Scala 3 support [#45](https://github.com/sbt/sbt-jni/issues/45) - Add CMake support of versions < 3.15 [#51](https://github.com/sbt/sbt-jni/pull/51) +- Add support for Rust/Cargo [#42](https://github.com/sbt/sbt-jni/pull/42) ### Changed - Upgrade gjavah [#43](https://github.com/sbt/sbt-jni/pull/43)