Skip to content

Commit

Permalink
Use common mechanism for mapping scalacOptions. Always set -source:3.…
Browse files Browse the repository at this point in the history
…x-migration and common flags
  • Loading branch information
WojciechMazur committed Jan 3, 2024
1 parent 7817390 commit 521da31
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 84 deletions.
19 changes: 19 additions & 0 deletions project-builder/build-revision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ export OPENCB_SCRIPT_DIR=$scriptDir
$scriptDir/checkout.sh "$repoUrl" "$rev" repo
buildToolFile="build-tool.txt"

scalaBinaryVersion=`echo $scalaVersion | cut -d . -f 1,2`
scalaBinaryVersionMajor=`echo $scalaVersion | cut -d . -f 1`
scalaBinaryVersionMinor=`echo $scalaVersion | cut -d . -f 2`
echo "Scala binary version found: $scalaBinaryVersion"

commonAppendScalacOptions="-source:$scalaBinaryVersion-migration,-Wconf:msg=can be rewritten automatically under -rewrite -source $scalaBinaryVersion-migration:s"
commonRemoveScalacOptions="-deprecation,-feature,-Xfatal-warnings,-Werror,MATCH:.*-Wconf.*any:e,-migration,"
echo "Would try to apply common scalacOption (best-effort, sbt/mill only):"
echo "Append: $commonAppendScalacOptions"
echo "Remove: $commonRemoveScalacOptions"

if [ -z $extraScalacOptions ];then extraScalacOptions="$commonAppendScalacOptions"
else extraScalacOptions="$extraScalacOptions,$commonAppendScalacOptions"
fi

if [ -z $disabledScalacOption ];then disabledScalacOption="$commonRemoveScalacOptions"
else disabledScalacOption="$disabledScalacOption,$commonRemoveScalacOptions";
fi

if [ -f "repo/mill" ] || [ -f "repo/build.sc" ]; then
echo "Mill project found: ${isMillProject}"
echo "mill" > $buildToolFile
Expand Down
23 changes: 6 additions & 17 deletions project-builder/mill/MillCommunityBuild.sc
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,14 @@ private object ScalacOptionsSettings {
.getOrElse(Nil)
val append = parse("communitybuild.appendScalacOptions")
val remove = parse("communitybuild.removeScalacOptions")
val filters = (append ++ remove).distinct.map { setting =>
Seq[String => String](
setting => if (setting.startsWith("--")) setting.tail else setting,
setting => {
setting.indexOf(':') match {
case -1 => setting
case n => setting.substring(0, n + 1) //
}
}
).reduce(_.andThen(_))
.apply(setting)
}
}

def mapScalacOptions(current: Seq[String]): Seq[String] = {
current
.filterNot(s => ScalacOptionsSettings.filters.exists(_.contains(s)))
.appendedAll(ScalacOptionsSettings.append)
}
def mapScalacOptions(current: Seq[String]): Seq[String] =
CommunityBuildCore.Scala3CommunityBuild.Utils.mapScalacOptions(
current = current,
append = ScalacOptionsSettings.append,
remove = ScalacOptionsSettings.remove
)

case class ModuleInfo(org: String, name: String, module: Module) {
val targetName = s"$org%$name"
Expand Down
3 changes: 1 addition & 2 deletions project-builder/mill/prepare-project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ millVersion=
if [[ -f .mill-version ]];then
millVersion=`cat .mill-version`
echo "Found explicit mill version $millVersion"
el
else
echo "No .mill-version file found, detecting compatible mill version"
if [[ -f ./mill ]];then
millVersion=`./mill -v resolve _ | grep "[Mm]ill.*version" | grep -E -o "(\d+\.?){3}"`
else
for v in $MILL_0_11 $MILL_0_10 $MILL_0_9; do
if ${scriptDir}/millw --mill-version $v $RESOLVE; then
if ${scriptDir}/millw --mill-version $v $RESOLVE > /dev/null 2>/dev/null; then
millVersion=$v
break
fi
Expand Down
52 changes: 11 additions & 41 deletions project-builder/sbt/CommunityBuildPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,38 +127,6 @@ object CommunityBuildPlugin extends AutoPlugin {
(_: Scope, currentSettings: Seq[String]) => currentSettings.filterNot(flags.contains)
}

/** Helper command used to filter scalacOptions and set source migration flags
*/
val enableMigrationMode = keyTransformCommand("enableMigrationMode", Keys.scalacOptions) {
(args, extracted) =>
val argSourceVersion = args.headOption.filter(_.nonEmpty)
def resolveSourceVersion(scalaVersion: String) =
CrossVersion.partialVersion(scalaVersion).collect {
case (3, 0) => "3.0-migration"
case (3, 1) => "3.0-migration"
case (3, 2) => "3.2-migration"
case (3, _) => "future-migration"
}

(scope: Scope, currentSettings: Seq[String]) => {
val scalaVersion = extracted.get(scope / Keys.scalaVersion)
if (!scalaVersion.startsWith("3.")) currentSettings
else
argSourceVersion
.orElse(resolveSourceVersion(scalaVersion))
.fold(currentSettings) { sourceVersion =>
val newEntries = Seq(s"-source:$sourceVersion")
println(s"Setting migration mode ${newEntries.mkString(" ")} in ${scope}")
// -Xfatal-warnings or -Wconf:any:e are don't allow to perform -source update
val filteredSettings =
Seq("-rewrite", "-source", "-migration", "-Xfatal-warnings")
currentSettings.filterNot { setting =>
filteredSettings.exists(setting.contains(_)) ||
setting.matches(".*-Wconf.*any:e")
} ++ newEntries
}
}
}

/** Helper command used to update crossScalaVersion It's needed for sbt 1.7.x, which does force
* exact match in `++ <scalaVersion>` command for defined crossScalaVersions,
Expand Down Expand Up @@ -208,14 +176,17 @@ object CommunityBuildPlugin extends AutoPlugin {
}
}

val appendScalacOptions = keyTransformCommand("appendScalacOptions", Keys.scalacOptions) {
val mapScalacOptions = keyTransformCommand("mapScalacOptions", Keys.scalacOptions) {
(args, _) => (_: Scope, currentScalacOptions: Seq[String]) =>
currentScalacOptions ++ args.filterNot(currentScalacOptions.contains)
}
val safeArgs = args.map(_.split(",").toList.filter(_.nonEmpty))
val append = safeArgs.lift(0).getOrElse(Nil)
val remove = safeArgs.lift(1).getOrElse(Nil)
Scala3CommunityBuild.Utils.mapScalacOptions(
current = currentScalacOptions,
append = append,
remove = remove
)

val removeScalacOptions = keyTransformCommand("removeScalacOptions", Keys.scalacOptions) {
(args, _) => (_: Scope, currentScalacOptions: Seq[String]) =>
currentScalacOptions.filterNot(args.contains)
}

import sbt.librarymanagement.InclExclRule
Expand Down Expand Up @@ -243,12 +214,10 @@ object CommunityBuildPlugin extends AutoPlugin {
}

val commands = Seq(
enableMigrationMode,
disableFatalWarnings,
setPublishVersion,
setCrossScalaVersions,
appendScalacOptions,
removeScalacOptions,
mapScalacOptions,
excludeLibraryDependency,
removeScalacOptionsStartingWith
)
Expand Down Expand Up @@ -503,6 +472,7 @@ object CommunityBuildPlugin extends AutoPlugin {
case EvalResult.Value(settings, _) => settings
case _ => Nil
}
println(s"Compile scalacOptions: ${scalacOptions}")
val compileResult = eval(Compile / compile)

val shouldBuildDocs = eval(Compile / doc / skip) match {
Expand Down
28 changes: 4 additions & 24 deletions project-builder/sbt/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ logFile=build.log

shouldRetry=false
forceScalaVersion=false
enableMigrationMode=false
sourceVersionToUseForMigration=""
appendScalacOptions="${extraScalacOptions}"
removeScalacOptions="${disabledScalacOption}"

function runSbt() {
# Use `setPublishVersion` instead of `every version`, as it might overrte Jmh/Jcstress versions
Expand All @@ -69,20 +69,13 @@ function runSbt() {
echo "Would force Scala version $scalaVersion"
setScalaVersionCmd="++$scalaVersion!"
fi
enableMigrationModeCmd=""
if [[ "$enableMigrationMode" == "true" ]]; then
echo "Would enable migration mode $scalaVersion"
enableMigrationModeCmd="enableMigrationMode"
fi
tq='"""'
sbt ${sbtSettings[@]} \
"setCrossScalaVersions $scalaVersion" \
"$setScalaVersionCmd -v" \
"removeScalacOptions -deprecation -feature -Xfatal-warnings -Werror ${disabledScalacOption}" \
"appendScalacOptions ${extraScalacOptions}" \
"mapScalacOptions \"$appendScalacOptions\" \"$removeScalacOptions\"" \
"set every credentials := Nil" \
"$setVersionCmd" \
"$enableMigrationModeCmd $sourceVersionToUseForMigration" \
"$customCommands" \
"moduleMappings" \
"runBuild ${scalaVersion} ${tq}${projectConfig}${tq} $targetsString" | tee $logFile
Expand All @@ -102,23 +95,10 @@ function checkLogsForRetry() {
shouldRetry=true
fi
fi

if [ "$enableMigrationMode" = false ]; then
if grep -P --max-count=1 'can be rewritten automatically under -rewrite -source ([\w\.]+-migration)' "$logFile"; then
# Don't pass file to grep or it will deadlock in pipes :<
# List all possible patches
# Take last workd (.*-migration)
# Sort by number of occurences and pick the most common one
# Spliting it into multiple lines caused errors
sourceVersionToUseForMigration=$(cat $logFile | grep -oP 'can be rewritten automatically under -rewrite -source ([\w\.]+-migration)' | awk '{print $NF}' | sort | uniq -c | sort -nr | cut -c9- | head -n 1)
enableMigrationMode=true
shouldRetry=true
fi
fi
}

retry=0
maxRetries=2 # 1 retry for each: missing mappings (force scala version) and source migration
maxRetries=1 # 1 retry for each: missing mappings (force scala version)

function retryBuild() {
while [[ $retry -lt $maxRetries ]]; do
Expand Down
26 changes: 26 additions & 0 deletions project-builder/shared/CommunityBuildCore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,31 @@ object Scala3CommunityBuild {
}
}
}

def mapScalacOptions(
current: Seq[String],
append: Seq[String],
remove: Seq[String]
): Seq[String] = {
val matching = remove.filter { _.startsWith("MATCH:") }.map(_.stripPrefix("MATCH:"))
val filters = (append ++ remove).distinct.map { setting =>
Seq[String => String](
setting => if (setting.startsWith("--")) setting.tail else setting,
setting => {
setting.indexOf(':') match {
case -1 => setting
case n => setting.substring(0, n)
}
}
).reduce(_.andThen(_))
.apply(setting)
}
current
.filterNot { s =>
filters.exists(_.contains(s)) || matching.exists(_.matches(s))
} ++ append.distinct

}

}
}

0 comments on commit 521da31

Please sign in to comment.