Skip to content

Commit

Permalink
Add javacOpt using directive
Browse files Browse the repository at this point in the history
  • Loading branch information
lwronski committed Oct 10, 2022
1 parent 4c007b4 commit 347fa89
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 9 deletions.
2 changes: 1 addition & 1 deletion modules/build/src/main/scala/scala/build/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ object Build {

val javacReleaseV = releaseFlagVersion.map(v => List("--release", v)).getOrElse(Nil)

javacReleaseV ++ semanticDbJavacOptions ++ options.javaOptions.javacOptions
javacReleaseV ++ semanticDbJavacOptions ++ options.javaOptions.javacOptions.map(_.value)
}

// `test` scope should contains class path to main scope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ case object ScalaPreprocessor extends Preprocessor {
UsingCustomJarDirectiveHandler,
UsingDependencyDirectiveHandler,
UsingJavaHomeDirectiveHandler,
UsingJavacOptionsDirectiveHandler,
UsingJavaOptionsDirectiveHandler,
UsingJavaPropsDirectiveHandler,
UsingMainClassDirectiveHandler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,20 @@ class DirectiveTests extends munit.FunSuite {
}
}

test("should parse javac options") {
val testInputs = TestInputs(
os.rel / "simple.sc" ->
"""//> using javacOpt "source", "1.8", "target", "1.8"
|""".stripMargin
)
testInputs.withBuild(baseOptions, buildThreads, bloopConfigOpt) {
(_, _, maybeBuild) =>
val build = maybeBuild.orThrow
val javacOpt = build.options.javaOptions.javacOptions

val expectedJavacOpt = Seq("source", "1.8", "target", "1.8")
expect(javacOpt.map(_.value) == expectedJavacOpt)
}
}

}
6 changes: 4 additions & 2 deletions modules/cli/src/main/scala/scala/cli/commands/Package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ object Package extends ScalaCommand[PackageOptions] with BuildCommandHelpers {
val finalBuildOptions = options.finalBuildOptions.orExit(options.shared.logger)
val buildOptions = finalBuildOptions.copy(javaOptions =
finalBuildOptions.javaOptions.copy(javacOptions =
finalBuildOptions.javaOptions.javacOptions ++ options.java.allJavaOpts
finalBuildOptions.javaOptions.javacOptions ++ options.java.allJavaOpts.map(
Positioned.commandLine(_)
)
)
)
buildOptions
Expand Down Expand Up @@ -704,7 +706,7 @@ object Package extends ScalaCommand[PackageOptions] with BuildCommandHelpers {
val preamble = Preamble()
.withOsKind(Properties.isWin)
.callsItself(Properties.isWin)
.withJavaOpts(build.options.javaOptions.javacOptions)
.withJavaOpts(build.options.javaOptions.javacOptions.map(_.value))
val params = Parameters.Bootstrap(Seq(loaderContent), mainClass)
.withDeterministic(true)
.withPreamble(preamble)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ abstract class ScalaCommand[T](implicit myParser: Parser[T], help: Help[T])
scalaVersion = scalaArtifacts.params.scalaVersion
compileClassPath = artifacts.compileClassPath
simpleScalaCompiler = SimpleScalaCompiler("java", Nil, scaladoc = false)
javacOptions = buildOptions.javaOptions.javacOptions
javacOptions = buildOptions.javaOptions.javacOptions.map(_.value)
javaHome = buildOptions.javaHomeLocation().value
} {
val exitCode = simpleScalaCompiler.runSimpleScalacLike(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ object JvmUtils {
ignoreErrors = false
),
javacPlugins = javacFilePlugins.map(s => Positioned.none(os.Path(s, Os.pwd))),
javacOptions = javacOption
javacOptions = javacOption.map(Positioned.commandLine(_))
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import scala.build.options.{BuildOptions, JavaOpt, JavaOptions, ShadowingSeq}

case object UsingJavaOptionsDirectiveHandler extends UsingDirectiveHandler {
def name = "Java options"
def description = "Add Java options"
def description = "Add Java options. Options to be used when running java."
def usage = "//> using java-opt _options_ | //> using javaOpt _options_"
override def usageMd =
"""`//> using java-opt `_options_
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package scala.build.preprocessing.directives

import scala.build.Logger
import scala.build.errors.BuildException
import scala.build.options.{BuildOptions, JavaOpt, JavaOptions, ShadowingSeq}

case object UsingJavacOptionsDirectiveHandler extends UsingDirectiveHandler {
def name = "Javac options"
def description = "Add Javac options. Options to be used when compiling things."
def usage = "//> using javac-opt _options_ | //> using javacOpt _options_"
override def usageMd =
"""`//> using javac-opt `_options_
|
|`//> using javacOpt `_options_""".stripMargin
override def examples = Seq(
"//> using javacOpt \"source\", \"1.8\"",
"\"target\", \"1.8\""
)
override def isRestricted = false
def keys = Seq("javacOpt", "javacOptions", "javac-opt", "javac-options")
def handleValues(
scopedDirective: ScopedDirective,
logger: Logger
): Either[BuildException, ProcessedUsingDirective] =
checkIfValuesAreExpected(scopedDirective).map { groupedValuesContainer =>
val javacOptions = groupedValuesContainer.scopedStringValues
val options = BuildOptions(
javaOptions = JavaOptions(
javacOptions = javacOptions.map(_.positioned)
)
)
ProcessedDirective(Some(options), Seq.empty)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import scala.build.{Os, Position, Positioned}
import scala.concurrent.ExecutionContextExecutorService
import scala.util.control.NonFatal

/**
* @javaOpts java options to be used when running java
* @javacOptions java options to be used when compiling things
*/
final case class JavaOptions(
javaHomeOpt: Option[Positioned[os.Path]] = None,
jvmIdOpt: Option[String] = None,
Expand All @@ -21,7 +25,7 @@ final case class JavaOptions(
javaOpts: ShadowingSeq[Positioned[JavaOpt]] = ShadowingSeq.empty,
javacPluginDependencies: Seq[Positioned[AnyDependency]] = Nil,
javacPlugins: Seq[Positioned[os.Path]] = Nil,
javacOptions: Seq[String] = Nil
javacOptions: Seq[Positioned[String]] = Nil
) {

private def finalJvmIndexOs = jvmIndexOs.getOrElse(OsLibc.jvmIndexOs)
Expand Down
13 changes: 13 additions & 0 deletions website/docs/commands/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,16 @@ java -cp "$(scala-cli compile --print-class-path Hello.scala)" Hello

Note that you should favor the [`run`](./run.md) command to run your code, rather than running `java -cp`.
The class path obtained this way is only meant for scenarios where `scala-cli` doesn't offer a more convenient option.

### JVM options

`--javac-opt` lets you add javac options which will be passed during compiling things.

```bash
scala-cli Hello.scala --javac-opt source --javac-opt 1.8 --javac-opt target --javac-opt 1.8
```

You can also add javac options with the using directive `//> using javacOpt`:
```
//> using javacOpt "source", "1.8", "target", "1.8"
```
14 changes: 14 additions & 0 deletions website/docs/commands/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ JVMs are [managed by coursier](https://get-coursier.io/docs/cli-java#managed-jvm
(New JVM versions are automatically checked daily, and updates for those are - manually - merged
swiftly.)

### JVM options

`--java-opt` lets you add java options to running application:

```bash
scala-cli Hello.scala --java-opt -Xmx1g --java-opt -Dfoo=bar
```

You can also add java options with the using directive `//> using javaOpt`:
```
//> using javaOpt "-Xmx1g", "-Dfoo=bar"
```


## Define source files in using directives

You can also add source files with the using directive `//> using file`:
Expand Down
15 changes: 14 additions & 1 deletion website/docs/reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Sets Java home used to run your application or tests

### Java options

Add Java options
Add Java options. Options to be used when running java.

`//> using java-opt `_options_

Expand All @@ -96,6 +96,19 @@ Add Java properties
#### Examples
`//> using javaProp "foo1=bar", "foo2"`

### Javac options

Add Javac options. Options to be used when compiling things.

`//> using javac-opt `_options_

`//> using javacOpt `_options_

#### Examples
`//> using javacOpt "source", "1.8"`

`"target", "1.8"`

### Main class

Specify default main class
Expand Down
15 changes: 14 additions & 1 deletion website/docs/reference/scala-command/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Sets Java home used to run your application or tests

### Java options

Add Java options
Add Java options. Options to be used when running java.

`//> using java-opt `_options_

Expand All @@ -96,6 +96,19 @@ Add Java properties
#### Examples
`//> using javaProp "foo1=bar", "foo2"`

### Javac options

Add Javac options. Options to be used when compiling things.

`//> using javac-opt `_options_

`//> using javacOpt `_options_

#### Examples
`//> using javacOpt "source", "1.8"`

`"target", "1.8"`

### Main class

Specify default main class
Expand Down

0 comments on commit 347fa89

Please sign in to comment.