Skip to content

Commit

Permalink
Refactor sbt plugin to make it more idiomatic (#630)
Browse files Browse the repository at this point in the history
* Remove shared library mode that has never been tested
* Make nativeClang and nativeClangPP a task
* Make nativeCompileOptions and nativeLinkingOptions to be a task
* Introduce linker result object to aggregate all the info linker produces
* Make resource scope to be tighter, just around linking and codegen, not around the whole pipeline
* Simplify OptimizerDriver API
* Update sbt docs

Fixes #562
  • Loading branch information
densh committed Apr 12, 2017
1 parent 8053a9a commit 036de7e
Show file tree
Hide file tree
Showing 22 changed files with 594 additions and 667 deletions.
25 changes: 2 additions & 23 deletions build.sbt
@@ -1,7 +1,7 @@
import java.io.File.pathSeparator
import scala.util.Try
import scalanative.tools.OptimizerReporter
import scalanative.sbtplugin.ScalaNativePluginInternal.nativeOptimizerReporter
import java.io.File.pathSeparator
import scalanative.sbtplugin.ScalaNativePluginInternal._

val toolScalaVersion = "2.10.6"

Expand Down Expand Up @@ -272,27 +272,6 @@ lazy val nativelib =
.in(file("nativelib"))
.settings(libSettings)
.settings(mavenPublishSettings)
.settings(compile in Compile := {
val clang = findClang(nativeClang.value)
val clangpp = findClangPP(nativeClangPP.value)

val source = baseDirectory.value
val compileSuccess =
IO.withTemporaryDirectory { tmp =>
IO.copyDirectory(baseDirectory.value, tmp)
scala.scalanative.sbtplugin.ScalaNativePluginInternal
.compileCSources(clang,
clangpp,
tmp,
nativeGC.value,
streams.value.log)
}
if (compileSuccess) {
(compile in Compile).value
} else {
sys.error("Compilation failed")
}
})

lazy val javalib =
project
Expand Down
45 changes: 18 additions & 27 deletions docs/user/sbt.rst
Expand Up @@ -35,33 +35,24 @@ and now you can write your first application in ``./src/main/scala/HelloWorld.sc
now simply run ``sbt run`` to get everything compiled and have the expected output!

Sbt settings
Sbt settings and tasks
----------------------

===== ======================== ================ =================================================== =========================================================
Since Name Type Default Value Description
===== ======================== ================ =================================================== =========================================================
0.1 ``nativeClang`` ``Option[File]`` ``None`` Path to ``clang`` command
0.1 ``nativeClangPP`` ``Option[File]`` ``None`` Path to ``clang++`` command
0.1 ``nativeCompileOptions`` ``Seq[String]`` ``Seq("-O0")`` Extra options passed to clang verbatim during compilation
0.1 ``nativeLinkingOptions`` ``Seq[String]`` ``Seq("-I/usr/local/include", "-L/usr/local/lib")`` Extra options passed to clang verbatim during linking
0.1 ``nativeMode`` ``String`` ``"debug"`` Either ``"debug"`` or ``"release"`` (2)
0.2 ``nativeGC`` ``String`` ``"boehm"`` Either ``"none"`` or ``"boehm"`` (3)
===== ======================== ================ =================================================== =========================================================

Sbt tasks
----------------------

===== ======================== ============ ====================================================
Since Name Type Description
===== ======================== ============ ====================================================
0.1 ``compile`` ``Analysis`` Compile Scala code to NIR
0.1 ``run`` ``Unit`` Compile, link and run the generated binary
0.1 ``package`` ``File`` Similar to standard package with addition of NIR
0.1 ``publish`` ``Unit`` Similar to standard publish with addition of NIR (1)
0.1 ``nativeLink`` ``File`` Link NIR and generate native binary
===== ======================== ============ ====================================================

===== ======================== =============== =========================================================
Since Name Type Description
===== ======================== =============== =========================================================
0.1 ``compile`` ``Analysis`` Compile Scala code to NIR
0.1 ``run`` ``Unit`` Compile, link and run the generated binary
0.1 ``package`` ``File`` Similar to standard package with addition of NIR
0.1 ``publish`` ``Unit`` Similar to standard publish with addition of NIR (1)
0.1 ``nativeLink`` ``File`` Link NIR and generate native binary
0.1 ``nativeClang`` ``File`` Path to ``clang`` command
0.1 ``nativeClangPP`` ``File`` Path to ``clang++`` command
0.1 ``nativeCompileOptions`` ``Seq[String]`` Extra options passed to clang verbatim during compilation
0.1 ``nativeLinkingOptions`` ``Seq[String]`` Extra options passed to clang verbatim during linking
0.1 ``nativeMode`` ``String`` Either ``"debug"`` or ``"release"`` (2)
0.2 ``nativeGC`` ``String`` Either ``"none"`` or ``"boehm"`` (3)
===== ======================== =============== =========================================================

1. See `Publishing`_ and `Cross compilation`_ for details.
2. See `Compilation modes`_ for details.
Expand All @@ -72,7 +63,7 @@ Compilation modes

Scala Native supports two distinct linking modes:

1. **debug.**
1. **debug.** (default)

Default mode. Optimized for shortest compilation time. Runs fewer
optimizations and is much more suited for iterative development workflow.
Expand All @@ -87,7 +78,7 @@ Scala Native supports two distinct linking modes:
Garbage collectors
------------------

1. **boehm.**
1. **boehm.** (default)

Conservative generational garbage collector. More information is available
at the `project's page <https://www.hboehm.info/gc/>`_.
Expand Down
Expand Up @@ -13,39 +13,35 @@ object ScalaNativePlugin extends AutoPlugin {

object AutoImport extends NativeCross {

def findClang(default: Option[File]): File =
default.getOrElse(
discover("clang", clangVersions)
)

def findClangPP(default: Option[File]): File =
default.getOrElse(
discover("clang++", clangVersions)
)

val ScalaNativeCrossVersion = sbtplugin.ScalaNativeCrossVersion

val nativeVersion = nir.Versions.current

val nativeClang =
settingKey[Option[File]]("Location of the clang compiler.")
taskKey[File]("Location of the clang compiler.")

val nativeClangPP =
settingKey[Option[File]]("Location of the clang++ compiler.")
taskKey[File]("Location of the clang++ compiler.")

val nativeCompileOptions =
settingKey[Seq[String]](
taskKey[Seq[String]](
"Additional options are passed to clang during compilation.")

val nativeLinkingOptions =
settingKey[Seq[String]](
taskKey[Seq[String]](
"Additional options that are pased to clang during linking.")

val nativeLink =
taskKey[File]("Generates native binary without running it.")

val nativeSharedLibrary = settingKey[Boolean](
"Will create a shared library instead of a program with a main method.")
val nativeExternalDependencies =
taskKey[Seq[String]]("List all external dependencies at link time.")

val nativeAvailableDependencies =
taskKey[Seq[String]]("List all symbols available at link time")

val nativeMissingDependencies =
taskKey[Seq[String]]("List all symbols not available at link time")

val nativeMode =
settingKey[String]("Compilation mode, either \"debug\" or \"release\".")
Expand Down

0 comments on commit 036de7e

Please sign in to comment.