Skip to content

Commit

Permalink
Add support for nativeLto options in command line and using directives
Browse files Browse the repository at this point in the history
  • Loading branch information
lwronski committed Mar 27, 2023
1 parent a32bcd1 commit ffaf38b
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 12 deletions.
Expand Up @@ -23,6 +23,10 @@ final case class ScalaNativeOptions(
@Tag(tags.should)
nativeMode: Option[String] = None,
@Group(HelpGroup.ScalaNative.toString)
@HelpMessage("Link-time optimisation mode")
@Tag(tags.should)
nativeLto: Option[String] = None,
@Group(HelpGroup.ScalaNative.toString)
@HelpMessage("Set the Scala Native garbage collector")
@Tag(tags.should)
nativeGc: Option[String] = None,
Expand Down
Expand Up @@ -245,6 +245,7 @@ final case class SharedOptions(
options.ScalaNativeOptions(
nativeVersion,
nativeMode,
nativeLto,
nativeGc,
nativeClang,
nativeClangpp,
Expand Down
Expand Up @@ -14,6 +14,8 @@ import scala.cli.commands.SpecificationLevel
|
|`//> using nativeMode` _value_
|
|`//> using nativeLto` _value_
|
|`//> using nativeVersion` _value_
|
|`//> using nativeCompile` _value1_, _value2_
Expand All @@ -32,6 +34,7 @@ import scala.cli.commands.SpecificationLevel
final case class ScalaNative(
nativeGc: Option[String] = None,
nativeMode: Option[String] = None,
nativeLto: Option[String] = None,
nativeVersion: Option[String] = None,
nativeCompile: List[String] = Nil,
nativeLinking: List[String] = Nil,
Expand All @@ -45,6 +48,7 @@ final case class ScalaNative(
val nativeOptions = ScalaNativeOptions(
gcStr = nativeGc,
modeStr = nativeMode,
ltoStr = nativeLto,
version = nativeVersion,
compileOptions = nativeCompile,
linkingOptions = nativeLinking,
Expand Down
Expand Up @@ -33,6 +33,28 @@ trait RunScalaNativeTestDefinitions { _: RunTestDefinitions =>
simpleNativeTests()
}

def scalaNativeLtoTests(): Unit = {
val fileName = "hello.sc"
val message = "Hello"
val inputs = TestInputs(
os.rel / fileName ->
s"""//> using nativeLto "thin"
|println("$message")
|""".stripMargin
)
inputs.fromRoot { root =>
val output =
os.proc(TestUtil.cli, extraOptions, fileName, "--native")
.call(cwd = root)
.out.trim()
expect(output == message)
}
}

test("scala native with lto optimisation") {
scalaNativeLtoTests()
}

test("simple script native command") {
val fileName = "simple.sc"
val message = "Hello"
Expand Down
Expand Up @@ -6,11 +6,13 @@ import dependency._
import java.nio.file.Paths

import scala.build.internal.Constants
import scala.scalanative.build.LTO
import scala.scalanative.{build => sn}

final case class ScalaNativeOptions(
version: Option[String] = None,
modeStr: Option[String] = None,
ltoStr: Option[String] = None,
gcStr: Option[String] = None,
clang: Option[String] = None,
clangpp: Option[String] = None,
Expand Down Expand Up @@ -65,7 +67,10 @@ final case class ScalaNativeOptions(

private def compileCliOptions(): List[String] =
finalCompileOptions().flatMap(option => List("--compile-option", option))

private def ltoOptions(): List[String] =
ltoStr.map(_.trim).filter(_.nonEmpty)
.map(lto => LTO.apply(lto))
.map(lto => List("--lto", lto.name)).getOrElse(Nil)
private def resourcesCliOptions(resourcesExist: Boolean): List[String] =
if (embedResources.getOrElse(true))
(numeralVersion, resourcesExist) match {
Expand Down Expand Up @@ -113,6 +118,7 @@ final case class ScalaNativeOptions(
def configCliOptions(resourcesExist: Boolean): List[String] =
gcCliOption() ++
modeCliOption() ++
ltoOptions() ++
clangCliOption() ++
clangppCliOption() ++
linkingCliOptions() ++
Expand Down
6 changes: 5 additions & 1 deletion website/docs/reference/cli-options.md
Expand Up @@ -1285,12 +1285,16 @@ Enable Scala Native. To show more options for Scala Native pass `--help-native`

### `--native-version`

Set the Scala Native version (0.4.9 by default).
Set the Scala Native version (0.4.12 by default).

### `--native-mode`

Set Scala Native compilation mode

### `--native-lto`

Link-time optimisation mode

### `--native-gc`

Set the Scala Native garbage collector
Expand Down
2 changes: 2 additions & 0 deletions website/docs/reference/directives.md
Expand Up @@ -242,6 +242,8 @@ Add Scala Native options

`//> using nativeMode` _value_

`//> using nativeLto` _value_

`//> using nativeVersion` _value_

`//> using nativeCompile` _value1_, _value2_
Expand Down
8 changes: 7 additions & 1 deletion website/docs/reference/scala-command/cli-options.md
Expand Up @@ -794,14 +794,20 @@ Enable Scala Native. To show more options for Scala Native pass `--help-native`

`SHOULD have` per Scala Runner specification

Set the Scala Native version (0.4.9 by default).
Set the Scala Native version (0.4.12 by default).

### `--native-mode`

`SHOULD have` per Scala Runner specification

Set Scala Native compilation mode

### `--native-lto`

`SHOULD have` per Scala Runner specification

Link-time optimisation mode

### `--native-gc`

`SHOULD have` per Scala Runner specification
Expand Down
2 changes: 2 additions & 0 deletions website/docs/reference/scala-command/directives.md
Expand Up @@ -187,6 +187,8 @@ Add Scala Native options

`//> using nativeMode` _value_

`//> using nativeLto` _value_

`//> using nativeVersion` _value_

`//> using nativeCompile` _value1_, _value2_
Expand Down
54 changes: 45 additions & 9 deletions website/docs/reference/scala-command/runner-specification.md
Expand Up @@ -158,12 +158,16 @@ Enable Scala Native. To show more options for Scala Native pass `--help-native`

**--native-version**

Set the Scala Native version (0.4.9 by default).
Set the Scala Native version (0.4.12 by default).

**--native-mode**

Set Scala Native compilation mode

**--native-lto**

Link-time optimisation mode

**--native-gc**

Set the Scala Native garbage collector
Expand Down Expand Up @@ -864,12 +868,16 @@ Enable Scala Native. To show more options for Scala Native pass `--help-native`

**--native-version**

Set the Scala Native version (0.4.9 by default).
Set the Scala Native version (0.4.12 by default).

**--native-mode**

Set Scala Native compilation mode

**--native-lto**

Link-time optimisation mode

**--native-gc**

Set the Scala Native garbage collector
Expand Down Expand Up @@ -1387,12 +1395,16 @@ Enable Scala Native. To show more options for Scala Native pass `--help-native`

**--native-version**

Set the Scala Native version (0.4.9 by default).
Set the Scala Native version (0.4.12 by default).

**--native-mode**

Set Scala Native compilation mode

**--native-lto**

Link-time optimisation mode

**--native-gc**

Set the Scala Native garbage collector
Expand Down Expand Up @@ -1936,12 +1948,16 @@ Enable Scala Native. To show more options for Scala Native pass `--help-native`

**--native-version**

Set the Scala Native version (0.4.9 by default).
Set the Scala Native version (0.4.12 by default).

**--native-mode**

Set Scala Native compilation mode

**--native-lto**

Link-time optimisation mode

**--native-gc**

Set the Scala Native garbage collector
Expand Down Expand Up @@ -2504,12 +2520,16 @@ Enable Scala Native. To show more options for Scala Native pass `--help-native`

**--native-version**

Set the Scala Native version (0.4.9 by default).
Set the Scala Native version (0.4.12 by default).

**--native-mode**

Set Scala Native compilation mode

**--native-lto**

Link-time optimisation mode

**--native-gc**

Set the Scala Native garbage collector
Expand Down Expand Up @@ -3048,12 +3068,16 @@ Enable Scala Native. To show more options for Scala Native pass `--help-native`

**--native-version**

Set the Scala Native version (0.4.9 by default).
Set the Scala Native version (0.4.12 by default).

**--native-mode**

Set Scala Native compilation mode

**--native-lto**

Link-time optimisation mode

**--native-gc**

Set the Scala Native garbage collector
Expand Down Expand Up @@ -3629,12 +3653,16 @@ Enable Scala Native. To show more options for Scala Native pass `--help-native`

**--native-version**

Set the Scala Native version (0.4.9 by default).
Set the Scala Native version (0.4.12 by default).

**--native-mode**

Set Scala Native compilation mode

**--native-lto**

Link-time optimisation mode

**--native-gc**

Set the Scala Native garbage collector
Expand Down Expand Up @@ -4255,12 +4283,16 @@ Enable Scala Native. To show more options for Scala Native pass `--help-native`

**--native-version**

Set the Scala Native version (0.4.9 by default).
Set the Scala Native version (0.4.12 by default).

**--native-mode**

Set Scala Native compilation mode

**--native-lto**

Link-time optimisation mode

**--native-gc**

Set the Scala Native garbage collector
Expand Down Expand Up @@ -5094,12 +5126,16 @@ Enable Scala Native. To show more options for Scala Native pass `--help-native`

**--native-version**

Set the Scala Native version (0.4.9 by default).
Set the Scala Native version (0.4.12 by default).

**--native-mode**

Set Scala Native compilation mode

**--native-lto**

Link-time optimisation mode

**--native-gc**

Set the Scala Native garbage collector
Expand Down

0 comments on commit ffaf38b

Please sign in to comment.