Skip to content

Commit

Permalink
Issue: 331 Added directive for native-version, native-compile, native…
Browse files Browse the repository at this point in the history
…-linking (#425)

* Added native-version, native-compile,native-linking in ScalaNativeOptionHandler

* scalafmt formatting completed

* updating usageMd and example

* reference doc generated
  • Loading branch information
asjad02 authored Nov 29, 2021
1 parent 5badf8b commit 868403e
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ case object UsingScalaNativeOptionsDirectiveHandler extends UsingDirectiveHandle

def description: String = "Add Scala Native options"

def usage: String = "Scala Native options"
def usage: String = "using native-gc _value_ | using native-version _value_"

override def usageMd: String = "directive using usage"
override def usageMd: String =
"""`using native-gc` _value_
|
|`using native-version` _value_
|
|`using native-compile` _value1_ _value2_
|
|`using native-linking` _value1_ _value2_""".stripMargin

override def examples: Seq[String] = Seq(
"using native-version 0.4.0"
)

override def examples: Seq[String] = Seq()
def handle(directive: Directive, cwd: ScopePath): Option[Either[BuildException, BuildOptions]] =
directive.values match {
case Seq(param @ "native-gc", value @ _*) =>
Expand All @@ -27,14 +37,37 @@ case object UsingScalaNativeOptionsDirectiveHandler extends UsingDirectiveHandle
Some(Right(options))
}
else Some(Left(SingleValueExpected(param, value)))

case Seq(param @ "native-version", value @ _*) =>
if (value.size == 1) {
val options = BuildOptions(
scalaNativeOptions = ScalaNativeOptions(
version = Some(value.head)
)
)
Some(Right(options))
}
else Some(Left(SingleValueExpected(param, value)))
case Seq(param @ "native-compile", value @ _*) =>
val options = BuildOptions(
scalaNativeOptions = ScalaNativeOptions(
compileOptions = value.toList
)
)
Some(Right(options))
case Seq(param @ "native-linking", value @ _*) =>
val options = BuildOptions(
scalaNativeOptions = ScalaNativeOptions(
linkingOptions = value.toList
)
)
Some(Right(options))
case _ =>
None

}

override def keys: Seq[String] =
Seq("native-gc")
Seq("native-gc", "native-version", "native-compile", "native-linking")

override def handleValues(
values: Seq[Any],
Expand Down
124 changes: 122 additions & 2 deletions modules/build/src/test/scala/scala/build/tests/BuildTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,26 @@ class BuildTests extends munit.FunSuite {
}
}

test("ScalaNative Options with multiple values") {
test("ScalaNativeOptions for native-gc with no values") {
val inputs = TestInputs(
os.rel / "p.sc" ->
"""using native-gc
|def foo() = println("hello foo")
|""".stripMargin
)
val buildOptions = defaultOptions.copy(
internal = defaultOptions.internal.copy(
keepDiagnostics = true
)
)
inputs.withBuild(buildOptions, buildThreads, bloopConfig) { (_, _, maybeBuild) =>
assert(maybeBuild.isLeft)
assert(maybeBuild.left.get == SingleValueExpected("native-gc", Seq()))
}

}

test("ScalaNativeOptions for native-gc with multiple values") {
val inputs = TestInputs(
os.rel / "p.sc" ->
"""using native-gc 78 12
Expand All @@ -591,7 +610,7 @@ class BuildTests extends munit.FunSuite {

}

test("ScalaNative Options for native-gc") {
test("ScalaNativeOptions for native-gc") {
val inputs = TestInputs(
os.rel / "p.sc" ->
"""using native-gc 78
Expand All @@ -608,4 +627,105 @@ class BuildTests extends munit.FunSuite {
assert(maybeBuild.toOption.get.options.scalaNativeOptions.gcStr.get == "78")
}
}

test("ScalaNativeOptions for native-version with multiple values") {
val inputs = TestInputs(
os.rel / "p.sc" ->
"""using native-version 0.4.0 0.3.3
|def foo() = println("hello foo")
|""".stripMargin
)
val buildOptions = defaultOptions.copy(
internal = defaultOptions.internal.copy(
keepDiagnostics = true
)
)
inputs.withBuild(buildOptions, buildThreads, bloopConfig) { (_, _, maybeBuild) =>
assert(maybeBuild.isLeft)
assert(maybeBuild.left.get == SingleValueExpected("native-version", Seq("0.4.0", "0.3.3")))
}

}

test("ScalaNativeOptions for native-version") {
val inputs = TestInputs(
os.rel / "p.sc" ->
"""using native-version 0.4.0
|def foo() = println("hello foo")
|""".stripMargin
)
val buildOptions: BuildOptions = defaultOptions.copy(
internal = defaultOptions.internal.copy(
keepDiagnostics = true
)
)

inputs.withBuild(buildOptions, buildThreads, bloopConfig) { (_, _, maybeBuild) =>
assert(maybeBuild.toOption.get.options.scalaNativeOptions.version.get == "0.4.0")
}
}

test("ScalaNativeOptions for native-compile") {
val inputs = TestInputs(
os.rel / "p.sc" ->
"""using native-compile compileOption1 compileOption2
|def foo() = println("hello foo")
|""".stripMargin
)
val buildOptions: BuildOptions = defaultOptions.copy(
internal = defaultOptions.internal.copy(
keepDiagnostics = true
)
)

inputs.withBuild(buildOptions, buildThreads, bloopConfig) { (_, _, maybeBuild) =>
assert(
maybeBuild.toOption.get.options.scalaNativeOptions.compileOptions(0) == "compileOption1"
)
assert(
maybeBuild.toOption.get.options.scalaNativeOptions.compileOptions(1) == "compileOption2"
)
}
}

test("ScalaNativeOptions for native-linking") {
val inputs = TestInputs(
os.rel / "p.sc" ->
"""using native-linking linkingOption1 linkingOption2
|def foo() = println("hello foo")
|""".stripMargin
)
val buildOptions: BuildOptions = defaultOptions.copy(
internal = defaultOptions.internal.copy(
keepDiagnostics = true
)
)

inputs.withBuild(buildOptions, buildThreads, bloopConfig) { (_, _, maybeBuild) =>
assert(
maybeBuild.toOption.get.options.scalaNativeOptions.linkingOptions(0) == "linkingOption1"
)
assert(
maybeBuild.toOption.get.options.scalaNativeOptions.linkingOptions(1) == "linkingOption2"
)
}
}

test("ScalaNativeOptions for native-linking and no value") {
val inputs = TestInputs(
os.rel / "p.sc" ->
"""using native-linking
|def foo() = println("hello foo")
|""".stripMargin
)
val buildOptions: BuildOptions = defaultOptions.copy(
internal = defaultOptions.internal.copy(
keepDiagnostics = true
)
)

inputs.withBuild(buildOptions, buildThreads, bloopConfig) { (_, _, maybeBuild) =>
assert(maybeBuild.toOption.get.options.scalaNativeOptions.linkingOptions.isEmpty)
}
}
}
11 changes: 10 additions & 1 deletion website/docs/reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,16 @@ Manually add a resource directory to the class path

Add Scala Native options

directive using usage
`using native-gc` _value_

`using native-version` _value_

`using native-compile` _value1_ _value2_

`using native-linking` _value1_ _value2_

#### Examples
`using native-version 0.4.0`

### Scala version

Expand Down

0 comments on commit 868403e

Please sign in to comment.