Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Scala Native to 0.5.1 #2862

Merged
merged 15 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ trait Core extends ScalaCliCrossSbtModule
| def ammoniteVersion = "${Deps.ammonite.dep.version}"
| def millVersion = "${InternalDeps.Versions.mill}"
| def lefouMillwRef = "${InternalDeps.Versions.lefouMillwRef}"
| def maxScalaNativeForMillExport = "${Deps.Versions.maxScalaNativeForMillExport}"
|
| def scalafmtOrganization = "${Deps.scalafmtCli.dep.module.organization.value}"
| def scalafmtName = "${Deps.scalafmtCli.dep.module.name.value}"
Expand All @@ -442,9 +443,11 @@ trait Core extends ScalaCliCrossSbtModule
| def toolkitName = "${Deps.toolkit.dep.module.name.value}"
| def toolkitTestName = "${Deps.toolkitTest.dep.module.name.value}"
| def toolkitDefaultVersion = "${Deps.toolkitVersion}"
| def toolkitMaxScalaNative = "${Deps.Versions.maxScalaNativeForToolkit}"
|
| def typelevelOrganization = "${Deps.typelevelToolkit.dep.module.organization.value}"
| def typelevelToolkitDefaultVersion = "${Deps.typelevelToolkitVersion}"
| def typelevelToolkitMaxScalaNative = "${Deps.Versions.maxScalaNativeForTypelevelToolkit}"
|
| def defaultScalaVersion = "${Scala.defaultUser}"
| def defaultScala212Version = "${Scala.scala212}"
Expand Down Expand Up @@ -473,6 +476,7 @@ trait Core extends ScalaCliCrossSbtModule
| def libsodiumjniVersion = "${Deps.libsodiumjni.dep.version}"
|
| def scalaPyVersion = "${Deps.scalaPy.dep.version}"
| def scalaPyMaxScalaNative = "${Deps.Versions.maxScalaNativeForScalaPy}"
|
| def giter8Organization = "${Deps.giter8.dep.module.organization.value}"
| def giter8Name = "${Deps.giter8.dep.module.name.value}"
Expand Down Expand Up @@ -983,6 +987,7 @@ trait CliIntegration extends SbtModule with ScalaCliPublishModule with HasTests
| def defaultGraalVMJavaVersion = "${deps.graalVmJavaVersion}"
| def defaultGraalVMVersion = "${deps.graalVmVersion}"
| def scalaPyVersion = "${Deps.scalaPy.dep.version}"
| def scalaPyMaxScalaNative = "${Deps.Versions.maxScalaNativeForScalaPy}"
| def bloopVersion = "${Deps.bloopRifle.dep.version}"
| def pprintVersion = "${TestDeps.pprint.dep.version}"
| def munitVersion = "${TestDeps.munit.dep.version}"
Expand All @@ -998,8 +1003,10 @@ trait CliIntegration extends SbtModule with ScalaCliPublishModule with HasTests
| def libsodiumVersion = "${deps.libsodiumVersion}"
| def dockerArchLinuxImage = "${TestDeps.archLinuxImage}"
|
| def toolkitVersion = "${Deps.toolkitVersion}"
| def typelevelToolkitVersion = "${Deps.typelevelToolkitVersion}"
| def toolkitVersion = "${Deps.toolkitVersion}"
| def toolkiMaxScalaNative = "${Deps.Versions.maxScalaNativeForToolkit}"
| def typelevelToolkitVersion = "${Deps.typelevelToolkitVersion}"
| def typelevelToolkitMaxScalaNative = "${Deps.Versions.maxScalaNativeForTypelevelToolkit}"
|
| def ghOrg = "$ghOrg"
| def ghName = "$ghName"
Expand Down
4 changes: 2 additions & 2 deletions gifs/scenarios/universal_tool.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if [[ -z "${ASCIINEMA_REC}" ]]; then
# Warm up scala-cli
echo "println(1)" | scala-cli -
echo "println(1)" | scala-cli --js - &&
echo "println(1)" | scala-cli --native -S 2.13.6 -
echo "println(1)" | scala-cli --native -S 2.13 -

# or do other preparation (e.g. create code)
else
Expand All @@ -38,7 +38,7 @@ object Native extends App {
EOF

pe "# Scala Native works only with Scala 2.x so far"
pe "scala-cli --native -S 2.13.6 native.scala"
pe "scala-cli --native -S 2.13 native.scala"
doSleep 3

echo " " && echo "ok" > status.txt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,20 @@ object Export extends ScalaCommand[ExportOptions] {
val inputs = options.shared.inputs(args.all).orExit(logger)
CurrentParams.workspaceOpt = Some(inputs.workspace)
val baseOptions =
initialBuildOptions.copy(mainClass = options.mainClass.mainClass.filter(_.nonEmpty))
initialBuildOptions
.copy(
scalaNativeOptions = initialBuildOptions.scalaNativeOptions.copy(
maxDefaultNativeVersions =
Gedochao marked this conversation as resolved.
Show resolved Hide resolved
initialBuildOptions.scalaNativeOptions.maxDefaultNativeVersions ++
(if shouldExportToMill && Constants.scalaNativeVersion != Constants.maxScalaNativeForMillExport
then
val warningMsg =
s"Mill export does not support Scala Native ${Constants.scalaNativeVersion}, ${Constants.maxScalaNativeForMillExport} should be used instead."
List(Constants.maxScalaNativeForMillExport -> warningMsg)
else Nil)
),
mainClass = options.mainClass.mainClass.filter(_.nonEmpty)
)

val (sourcesMain, optionsMain0) =
prepareBuild(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import scala.build.internal.{Constants, FetchExternalBinary, OsLibc, Util}
import scala.build.options.ScalaVersionUtil.fileWithTtl0
import scala.build.options.{BuildOptions, ComputeVersion, Platform, ScalacOpt, ShadowingSeq}
import scala.build.preprocessing.directives.ClasspathUtils.*
import scala.build.preprocessing.directives.Toolkit
import scala.build.preprocessing.directives.{Python, Toolkit}
import scala.build.options as bo
import scala.cli.ScalaCli
import scala.cli.commands.publish.ConfigUtil.*
Expand Down Expand Up @@ -265,21 +265,25 @@ final case class SharedOptions(
)
}

private def scalaNativeOptions(opts: ScalaNativeOptions): options.ScalaNativeOptions = {
private def scalaNativeOptions(
opts: ScalaNativeOptions,
maxDefaultScalaNativeVersions: List[(String, String)]
): options.ScalaNativeOptions = {
import opts._
options.ScalaNativeOptions(
nativeVersion,
nativeMode,
nativeLto,
nativeGc,
nativeClang,
nativeClangpp,
nativeLinking,
nativeLinkingDefaults,
nativeCompile,
nativeCompileDefaults,
embedResources,
nativeTarget
version = nativeVersion,
modeStr = nativeMode,
ltoStr = nativeLto,
gcStr = nativeGc,
clang = nativeClang,
clangpp = nativeClangpp,
linkingOptions = nativeLinking,
linkingDefaults = nativeLinkingDefaults,
compileOptions = nativeCompile,
compileDefaults = nativeCompileDefaults,
embedResources = embedResources,
buildTargetStr = nativeTarget,
maxDefaultNativeVersions = maxDefaultScalaNativeVersions
)
}

Expand Down Expand Up @@ -324,7 +328,15 @@ final case class SharedOptions(
s"""[${Console.YELLOW}warn${Console.RESET}] Jars with the ${ScalaCliConsole.GRAY}*-sources.jar${Console.RESET} name suffix are assumed to be source jars.
|The following jars were assumed to be source jars and will be treated as such: $assumedSourceJarsString""".stripMargin
)
val resolvedToolkitDependency = SharedOptions.resolveToolkitDependency(withToolkit, logger)
val (resolvedToolkitDependency, toolkitMaxDefaultScalaNativeVersions) =
SharedOptions.resolveToolkitDependencyAndScalaNativeVersionReqs(withToolkit, logger)
val scalapyMaxDefaultScalaNativeVersions =
if sharedPython.python.contains(true) then
List(Constants.scalaPyMaxScalaNative -> Python.maxScalaNativeWarningMsg)
else Nil
val maxDefaultScalaNativeVersions =
toolkitMaxDefaultScalaNativeVersions.toList ++ scalapyMaxDefaultScalaNativeVersions
val snOpts = scalaNativeOptions(native, maxDefaultScalaNativeVersions)
bo.BuildOptions(
sourceGeneratorOptions = bo.SourceGeneratorOptions(
useBuildInfo = sourceGenerator.useBuildInfo,
Expand Down Expand Up @@ -382,7 +394,7 @@ final case class SharedOptions(
forceObjectWrapper = objectWrapper
),
scalaJsOptions = scalaJsOptions(js),
scalaNativeOptions = scalaNativeOptions(native),
scalaNativeOptions = snOpts,
javaOptions = value(scala.cli.commands.util.JvmUtils.javaOptions(jvm)),
jmhOptions = bo.JmhOptions(
addJmhDependencies =
Expand Down Expand Up @@ -727,10 +739,10 @@ object SharedOptions {

// TODO: remove this state after resolving https://github.com/VirtusLab/scala-cli/issues/2658
private val loggedDeprecatedToolkitWarning: AtomicBoolean = AtomicBoolean(false)
private def resolveToolkitDependency(
private def resolveToolkitDependencyAndScalaNativeVersionReqs(
toolkitVersion: Option[String],
logger: Logger
): Seq[Positioned[AnyDependency]] = {
): (Seq[Positioned[AnyDependency]], Seq[(String, String)]) = {
Gedochao marked this conversation as resolved.
Show resolved Hide resolved
if (
(toolkitVersion.contains("latest")
|| toolkitVersion.contains(Toolkit.typelevel + ":latest")
Expand All @@ -743,7 +755,28 @@ object SharedOptions {
)
)

toolkitVersion.toList.map(Positioned.commandLine)
.flatMap(Toolkit.resolveDependenciesWithRequirements(_).map(_.value))
val (dependencies, toolkitDefaults) =
toolkitVersion.toList.map(Positioned.commandLine)
.flatMap(Toolkit.resolveDependenciesWithRequirements(_).map((wbr, td) => wbr.value -> td))
.unzip
val maxScalaNativeVersions =
toolkitDefaults.flatMap {
case Toolkit.ToolkitDefaults(isScalaToolkitDefault, isTypelevelToolkitDefault) =>
val st = if (isScalaToolkitDefault)
Seq(Constants.toolkitMaxScalaNative -> Toolkit.maxScalaNativeWarningMsg(
"Scala Toolkit",
Constants.toolkitMaxScalaNative
))
else Nil
val tlt =
if (isTypelevelToolkitDefault)
Seq(Constants.typelevelToolkitMaxScalaNative -> Toolkit.maxScalaNativeWarningMsg(
"TypeLevel Toolkit",
Constants.typelevelToolkitMaxScalaNative
))
else Nil
st ++ tlt
}
dependencies -> maxScalaNativeVersions
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ final case class MillProjectDescriptor(
)
}

private def scalaNativeSettings(options: ScalaNativeOptions): MillProject = {
val scalaNativeVersion = Some(options.version.getOrElse(Constants.scalaNativeVersion))
MillProject(scalaNativeVersion = scalaNativeVersion)
}
private def scalaNativeSettings(options: ScalaNativeOptions): MillProject =
MillProject(scalaNativeVersion = Some(options.finalVersion))

private def dependencySettings(
mainOptions: BuildOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package scala.build.preprocessing.directives

import scala.build.directives.*
import scala.build.errors.BuildException
import scala.build.options.{BuildOptions, PostBuildOptions}
import scala.build.internal.Constants
import scala.build.options.{BuildOptions, PostBuildOptions, ScalaNativeOptions}
import scala.build.preprocessing.ScopePath
import scala.cli.commands.SpecificationLevel

Expand All @@ -17,6 +18,10 @@ final case class Python(
val options = BuildOptions(
notForBloopOptions = PostBuildOptions(
python = Some(true)
),
scalaNativeOptions = ScalaNativeOptions(
maxDefaultNativeVersions =
List(Constants.scalaPyMaxScalaNative -> Python.maxScalaNativeWarningMsg)
)
)
Right(options)
Expand All @@ -25,4 +30,6 @@ final case class Python(

object Python {
val handler: DirectiveHandler[Python] = DirectiveHandler.derive
val maxScalaNativeWarningMsg =
s"ScalaPy does not support Scala Native ${Constants.scalaNativeVersion}, ${Constants.scalaPyMaxScalaNative} should be used instead."
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ import scala.build.errors.BuildException
import scala.build.internal.Constants
import scala.build.options.BuildRequirements.ScopeRequirement
import scala.build.options.WithBuildRequirements.*
import scala.build.options.{
BuildOptions,
BuildRequirements,
ClassPathOptions,
Scope,
ShadowingSeq,
WithBuildRequirements
}
import scala.build.options._
import scala.cli.commands.SpecificationLevel

@DirectiveGroupName("Toolkit")
Expand All @@ -42,42 +35,63 @@ final case class Toolkit(

object Toolkit {
val typelevel = "typelevel"
val scala = "scala"

def maxScalaNativeWarningMsg(toolkitName: String, maxNative: String): String =
s"$toolkitName does not support Scala Native ${Constants.scalaNativeVersion}, $maxNative should be used instead."

object TypelevelToolkit {
def unapply(s: Option[String]): Boolean =
s.contains(typelevel) || s.contains(Constants.typelevelOrganization)
}

object ScalaToolkit {
def unapply(s: Option[String]): Boolean =
s.isEmpty || s.contains(Constants.toolkitOrganization) || s.contains(scala)
}

case class ToolkitDefaults(
isScalaToolkitDefault: Boolean = false,
isTypelevelToolkitDefault: Boolean = false
)

/** @param toolkitCoords
* the toolkit coordinates
* @return
* the `toolkit` and `toolkit-test` dependencies with the appropriate build requirements
*/
def resolveDependenciesWithRequirements(toolkitCoords: Positioned[String])
: List[WithBuildRequirements[Positioned[DependencyLike[NameAttributes, NameAttributes]]]] =
def resolveDependenciesWithRequirements(toolkitCoords: Positioned[String]): List[(
WithBuildRequirements[Positioned[DependencyLike[NameAttributes, NameAttributes]]],
ToolkitDefaults
)] =
toolkitCoords match
case Positioned(positions, coords) =>
val tokens = coords.split(':')
val rawVersion = tokens.last
def isDefault = rawVersion == "default"
val notDefaultVersion = if rawVersion == "latest" then "latest.release" else rawVersion
val flavor = tokens.dropRight(1).headOption
val (org, v) = flavor match {
case TypelevelToolkit() => Constants.typelevelOrganization -> {
val (org, v, trv: ToolkitDefaults) = flavor match {
case TypelevelToolkit() => (
Constants.typelevelOrganization,
if isDefault then Constants.typelevelToolkitDefaultVersion
else notDefaultVersion
}
case Some(org) => org -> notDefaultVersion
case None => Constants.toolkitOrganization -> {
else notDefaultVersion,
ToolkitDefaults(isTypelevelToolkitDefault = isDefault)
)
case ScalaToolkit() | None =>
(
Constants.toolkitOrganization,
if isDefault then Constants.toolkitDefaultVersion
else notDefaultVersion
}
else notDefaultVersion,
ToolkitDefaults(isScalaToolkitDefault = isDefault)
)
case Some(org) => (org, notDefaultVersion, ToolkitDefaults())
}
List(
Positioned(positions, dep"$org::${Constants.toolkitName}::$v,toolkit")
.withEmptyRequirements,
.withEmptyRequirements -> trv,
Positioned(positions, dep"$org::${Constants.toolkitTestName}::$v,toolkit")
.withScopeRequirement(Scope.Test)
.withScopeRequirement(Scope.Test) -> trv
)
val handler: DirectiveHandler[Toolkit] = DirectiveHandler.derive

Expand Down Expand Up @@ -105,20 +119,43 @@ object Toolkit {
): List[Either[BuildException, WithBuildRequirements[BuildOptions]]] = t
.toList
.flatMap(resolveDependenciesWithRequirements) // resolve dependencies
.map { case WithBuildRequirements(requirements, positionedDep) =>
positionedDep
.withBuildRequirements {
if requirements.scope.isEmpty then // if the scope is not set, set it to the default
requirements.copy(scope = defaultScope.map(_.asScopeRequirement))
else requirements
}
.map { dep =>
BuildOptions(
classPathOptions = ClassPathOptions(
extraDependencies = ShadowingSeq.from(List(dep))
.map {
case (
WithBuildRequirements(requirements, positionedDep),
ToolkitDefaults(isScalaToolkitDefault, isTypelevelToolkitDefault)
) =>
val scalaToolkitMaxNativeVersions =
if isScalaToolkitDefault then
List(Constants.toolkitMaxScalaNative -> maxScalaNativeWarningMsg(
"Scala Toolkit",
Constants.toolkitMaxScalaNative
))
else Nil
val typelevelToolkitMaxNativeVersions =
if isTypelevelToolkitDefault then
List(Constants.typelevelToolkitMaxScalaNative -> maxScalaNativeWarningMsg(
"TypeLevel Toolkit",
Constants.typelevelToolkitMaxScalaNative
))
else Nil
val maxNativeVersions =
(scalaToolkitMaxNativeVersions ++ typelevelToolkitMaxNativeVersions).distinct
positionedDep
.withBuildRequirements {
if requirements.scope.isEmpty then // if the scope is not set, set it to the default
requirements.copy(scope = defaultScope.map(_.asScopeRequirement))
else requirements
}
.map { dep =>
BuildOptions(
classPathOptions = ClassPathOptions(
extraDependencies = ShadowingSeq.from(List(dep))
),
scalaNativeOptions = ScalaNativeOptions(
maxDefaultNativeVersions = maxNativeVersions
)
)
)
}
}
}
.groupBy(_.requirements.scope.map(_.scope))
.toList
Expand Down
Loading
Loading