Skip to content

Commit

Permalink
Update compiler and run options
Browse files Browse the repository at this point in the history
  • Loading branch information
RustedBones committed Dec 6, 2023
1 parent c0c1770 commit 172c354
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 187 deletions.
180 changes: 65 additions & 115 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
* under the License.
*/

import sbt._
import Keys._
import sbt.*
import Keys.*
import explicitdeps.ExplicitDepsPlugin.autoImport.moduleFilterRemoveValue
import sbtassembly.AssemblyPlugin.autoImport._
import sbtassembly.AssemblyPlugin.autoImport.*
import com.github.sbt.git.SbtGit.GitKeys.gitRemoteRepo

import de.heikoseeberger.sbtheader.CommentCreator
import org.typelevel.scalacoptions.JavaMajorVersion.javaMajorVersion

val beamVendorVersion = "0.1"
val beamVersion = "2.52.0"
Expand Down Expand Up @@ -217,6 +217,9 @@ val scala213 = "2.13.12"
val scala212 = "2.12.18"
val scalaDefault = scala213

// java target
ThisBuild / tlJdkRelease := Some(8)

// github actions
val java21 = JavaSpec.corretto("21")
val java17 = JavaSpec.corretto("17")
Expand Down Expand Up @@ -254,7 +257,7 @@ ThisBuild / githubWorkflowPublishPreamble := Seq(
)
ThisBuild / githubWorkflowPublishPostamble := Seq(
WorkflowStep.Use(
UseRef.Public("softprops", "action-gh-release" , "v1"),
UseRef.Public("softprops", "action-gh-release", "v1"),
Map(
"files" -> "scio-repl/target/scala-2.13/scio-repl.jar",
"draft" -> "true"
Expand Down Expand Up @@ -361,115 +364,60 @@ lazy val keepExistingHeader =
)
.trim()
})
ThisBuild / headerLicense := Some(HeaderLicense.ALv2(currentYear.toString, "Spotify AB"))
ThisBuild / headerMappings := headerMappings.value + (HeaderFileType.scala -> keepExistingHeader, HeaderFileType.java -> keepExistingHeader)

//ThisBuild / tpolecatDefaultOptionsMode := DevMode
//ThisBuild / tpolecatDevModeOptions ~= { opts =>
// val excludes = Set(
// ScalacOptions.lintPackageObjectClasses,
// ScalacOptions.privateWarnDeadCode,
// ScalacOptions.privateWarnValueDiscard,
// ScalacOptions.warnDeadCode,
// ScalacOptions.warnNonUnitStatement,
// ScalacOptions.warnValueDiscard
// )
//
// val extras = Set(
// Scalac.delambdafyInlineOption,
// Scalac.macroAnnotationsOption,
// Scalac.macroSettingsOption,
// Scalac.maxClassfileName,
// Scalac.privateBackendParallelism,
// Scalac.privateWarnMacrosOption,
// Scalac.release8,
// Scalac.warnConfOption,
// Scalac.warnMacrosOption
// )
//
// opts.filterNot(excludes).union(extras)
//}
//
//ThisBuild / doc / tpolecatDevModeOptions ++= Set(
// Scalac.docNoJavaCommentOption
//)

// scalafix
ThisBuild / scalafixScalaBinaryVersion := CrossVersion.binaryScalaVersion(scalaVersion.value)
val excludeLint = SettingKey[Set[Def.KeyedInitialize[_]]]("excludeLintKeys")
Global / excludeLint := (Global / excludeLint).?.value.getOrElse(Set.empty)
Global / excludeLint += sonatypeProfileName
Global / excludeLint += site / Paradox / sourceManaged

lazy val javaSettings = sys.props("java.version") match {
case v if v.startsWith("17.") || v.startsWith("21.") =>
Def.settings(
javaOptions ++= Seq(
"--add-opens",
"java.base/java.util=ALL-UNNAMED",
"--add-opens",
"java.base/java.lang.invoke=ALL-UNNAMED"
)
val commonSettings = Def.settings(
headerLicense := Some(HeaderLicense.ALv2(currentYear.toString, "Spotify AB")),
headerMappings := headerMappings.value ++ Map(
HeaderFileType.scala -> keepExistingHeader,
HeaderFileType.java -> keepExistingHeader
),
scalacOptions ++= ScalacOptions.defaults(scalaVersion.value),
scalacOptions ~= { opts =>
val exclude = Set(
"-Wdead-code", // too many false positives
"-Wvalue-discard", // too many warnings
"-Xsource:3" // not ready for scala 3 yet
)
case _ => Def.settings()
}

val commonSettings = javaSettings ++
Def.settings(
// organization := "com.spotify",
// scalaVersion := "2.13.12",
// crossScalaVersions := Seq("2.12.18", scalaVersion.value),
// // this setting is not derived in sbt-tpolecat
// // https://github.com/typelevel/sbt-tpolecat/issues/36
// inTask(doc)(TpolecatPlugin.projectSettings),
javacOptions ++= Seq("-source", "1.8", "-target", "1.8", "-Xlint:unchecked"),
Compile / doc / javacOptions := Seq("-source", "1.8"),
excludeDependencies += Exclude.beamKafka,
excludeDependencies ++= Exclude.loggerImplementations,
resolvers ++= Resolver.sonatypeOssRepos("public"),
fork := true,
run / outputStrategy := Some(OutputStrategy.StdoutOutput),
Test / classLoaderLayeringStrategy := ClassLoaderLayeringStrategy.Flat,
Test / javaOptions ++= Seq(
"-Xms512m",
"-Xmx2G",
"-XX:+UseParallelGC",
"-Dfile.encoding=UTF8",
"-Dscio.ignoreVersionWarning=true",
"-Dorg.slf4j.simpleLogger.defaultLogLevel=info",
"-Dorg.slf4j.simpleLogger.logFile=scio.log"
) ++ Seq(
"bigquery.project",
"bigquery.secret",
"cloudsql.sqlserver.password"
).flatMap { prop =>
sys.props.get(prop).map(value => s"-D$prop=$value")
},
Test / testOptions += Tests.Argument("-oD"),
testOptions ++= {
if (sys.env.contains("SLOW")) {
Nil
} else {
Seq(Tests.Argument(TestFrameworks.ScalaTest, "-l", "org.scalatest.tags.Slow"))
}
},
unusedCompileDependenciesFilter -= Seq(
moduleFilter("org.scala-lang", "scala-reflect"),
moduleFilter("org.scala-lang.modules", "scala-collection-compat")
).reduce(_ | _),
coverageExcludedPackages := (Seq(
"com\\.spotify\\.scio\\.examples\\..*",
"com\\.spotify\\.scio\\.repl\\..*",
"com\\.spotify\\.scio\\.util\\.MultiJoin",
"com\\.spotify\\.scio\\.smb\\.util\\.SMBMultiJoin"
) ++ (2 to 10).map(x => s"com\\.spotify\\.scio\\.sql\\.Query$x")).mkString(";"),
coverageHighlighting := true
)

//lazy val publishSettings = Def.settings(
// // Release settings
// sonatypeProfileName := "com.spotify"
//)
opts.filterNot(exclude.contains)
},
Compile / doc / scalacOptions ++= ScalacOptions.tokensForVersion(
scalaVersion.value,
Set(ScalacOptions.docNoJavaCommentOption)
),
javacOptions ~= { opts =>
val exclude = Set(
"-Xlint:all" // too many warnings
)
opts.filterNot(exclude.contains)
},
javaOptions := JavaOptions.defaults(javaMajorVersion),
excludeDependencies += Exclude.beamKafka,
excludeDependencies ++= Exclude.loggerImplementations,
resolvers ++= Resolver.sonatypeOssRepos("public"),
fork := true,
run / outputStrategy := Some(OutputStrategy.StdoutOutput),
Test / classLoaderLayeringStrategy := ClassLoaderLayeringStrategy.Flat,
Test / javaOptions ++= JavaOptions.testDefaults(javaMajorVersion),
Test / testOptions += Tests.Argument("-oD"),
testOptions ++= {
if (sys.env.contains("SLOW")) {
Nil
} else {
Seq(Tests.Argument(TestFrameworks.ScalaTest, "-l", "org.scalatest.tags.Slow"))
}
},
unusedCompileDependenciesFilter -= Seq(
moduleFilter("org.scala-lang", "scala-reflect"),
moduleFilter("org.scala-lang.modules", "scala-collection-compat")
).reduce(_ | _),
coverageExcludedPackages := (Seq(
"com\\.spotify\\.scio\\.examples\\..*",
"com\\.spotify\\.scio\\.repl\\..*",
"com\\.spotify\\.scio\\.util\\.MultiJoin",
"com\\.spotify\\.scio\\.smb\\.util\\.SMBMultiJoin"
) ++ (2 to 10).map(x => s"com\\.spotify\\.scio\\.sql\\.Query$x")).mkString(";"),
coverageHighlighting := true
)

// for modules containing java jUnit 4 tests
lazy val jUnitSettings = Def.settings(
Expand All @@ -492,8 +440,10 @@ lazy val macroSettings = Def.settings(
case _ => Nil
}
},
// see MacroSettings.scala
scalacOptions += "-Xmacro-settings:cache-implicit-schemas=true"
scalacOptions ++= ScalacOptions.tokensForVersion(
scalaVersion.value,
Set(ScalacOptions.macroCacheImplicitSchemas(true))
)
)

lazy val directRunnerDependencies = Seq(
Expand Down Expand Up @@ -1562,7 +1512,7 @@ lazy val site = project
mdocIn := (paradox / sourceDirectory).value,
mdocExtraArguments ++= Seq("--no-link-hygiene"),
// paradox
paradox / sourceManaged := mdocOut.value,
Compile / paradox / sourceManaged := mdocOut.value,
paradoxProperties ++= Map(
"extref.example.base_url" -> "https://spotify.github.io/scio/examples/%s.scala.html",
"github.base_url" -> "https://github.com/spotify/scio",
Expand Down
89 changes: 89 additions & 0 deletions project/JavaOptions.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
object JavaOptions {
class JavaOption(
val option: String,
val args: List[String],
val isSupported: Int => Boolean
) {
override def hashCode(): Int =
41 * option.hashCode

override def equals(other: Any): Boolean =
other match {
case that: JavaOption =>
this.option == that.option &&
this.args.size == that.args.size &&
this.args.sorted.zip(that.args.sorted).forall { case (s1, s2) => s1 == s2 }
case _ => false
}

override def toString =
(option :: args).mkString("JavaOption(", " ", ")")
}

def property(
key: String,
value: String,
isSupported: Int => Boolean = _ => true
): JavaOption =
new JavaOption(s"-D$key=$value", List.empty, isSupported)
def extra(
name: String,
arguments: List[String] = List.empty,
isSupported: Int => Boolean = _ => true
): JavaOption = new JavaOption(s"-X$name", arguments, isSupported)

def minHeapSize(size: String): JavaOption =
extra("ms", List(size))

def maxHeapSize(size: String): JavaOption =
extra("mx", List(size))

def advanced(
name: String,
arguments: List[String] = List.empty,
isSupported: Int => Boolean = _ => true
): JavaOption = new JavaOption(s"-XX:$name", arguments, isSupported)

def addOpens(module: String, `package`: String, reflectingModule: String) =
new JavaOption("--add-opens", List(s"$module/${`package`}=$reflectingModule"), _ >= 17)
def optionsForVersion(
javaVersion: Int,
proposedJavacOptions: Set[JavaOption]
): Set[JavaOption] =
proposedJavacOptions.filter(_.isSupported(javaVersion))

def tokensForVersion(
javaVersion: Int,
proposedJavacOptions: Set[JavaOption]
): Seq[String] =
optionsForVersion(javaVersion, proposedJavacOptions).toList
.flatMap(opt => opt.option :: opt.args)

def defaults(javaVersion: Int): Seq[String] =
tokensForVersion(
javaVersion,
Set(
property("file.encoding", "UTF-8"),
addOpens("java.base", "java.util", "ALL-UNNAMED"),
addOpens("java.base", "java.lang.invoke", "ALL-UNNAMED"),
addOpens("java.base", "java.lang", "ALL-UNNAMED")
)
)

def testDefaults(javaVersion: Int): Seq[String] =
tokensForVersion(
javaVersion,
Set(
minHeapSize("512m"),
maxHeapSize("2G"),
advanced("UseParallelGC"),
property("scio.ignoreVersionWarning", "true"),
property("org.slf4j.simpleLogger.defaultLogLevel", "info"),
property("org.slf4j.simpleLogger.logFile", "scio.log")
) ++ Set(
"bigquery.project",
"bigquery.secret",
"cloudsql.sqlserver.password"
).flatMap(prop => sys.props.get(prop).map(value => property(prop, value)))
)
}
Loading

0 comments on commit 172c354

Please sign in to comment.