From 521da31089d488592fadb2a7f2a82d1519f3935c Mon Sep 17 00:00:00 2001 From: Wojciech Mazur Date: Wed, 3 Jan 2024 21:16:28 +0100 Subject: [PATCH] Use common mechanism for mapping scalacOptions. Always set -source:3.x-migration and common flags --- project-builder/build-revision.sh | 19 +++++++ project-builder/mill/MillCommunityBuild.sc | 23 +++----- project-builder/mill/prepare-project.sh | 3 +- .../sbt/CommunityBuildPlugin.scala | 52 ++++--------------- project-builder/sbt/build.sh | 28 ++-------- .../shared/CommunityBuildCore.scala | 26 ++++++++++ 6 files changed, 67 insertions(+), 84 deletions(-) diff --git a/project-builder/build-revision.sh b/project-builder/build-revision.sh index caa7f1aa..b57dd0e8 100755 --- a/project-builder/build-revision.sh +++ b/project-builder/build-revision.sh @@ -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 diff --git a/project-builder/mill/MillCommunityBuild.sc b/project-builder/mill/MillCommunityBuild.sc index f438c0fb..bb6b62b5 100644 --- a/project-builder/mill/MillCommunityBuild.sc +++ b/project-builder/mill/MillCommunityBuild.sc @@ -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" diff --git a/project-builder/mill/prepare-project.sh b/project-builder/mill/prepare-project.sh index 4ea3e090..e4a7daf8 100755 --- a/project-builder/mill/prepare-project.sh +++ b/project-builder/mill/prepare-project.sh @@ -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 diff --git a/project-builder/sbt/CommunityBuildPlugin.scala b/project-builder/sbt/CommunityBuildPlugin.scala index 1314545b..94a14dfc 100644 --- a/project-builder/sbt/CommunityBuildPlugin.scala +++ b/project-builder/sbt/CommunityBuildPlugin.scala @@ -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 `++ ` command for defined crossScalaVersions, @@ -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 @@ -243,12 +214,10 @@ object CommunityBuildPlugin extends AutoPlugin { } val commands = Seq( - enableMigrationMode, disableFatalWarnings, setPublishVersion, setCrossScalaVersions, - appendScalacOptions, - removeScalacOptions, + mapScalacOptions, excludeLibraryDependency, removeScalacOptionsStartingWith ) @@ -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 { diff --git a/project-builder/sbt/build.sh b/project-builder/sbt/build.sh index f06f104f..95e1fba9 100755 --- a/project-builder/sbt/build.sh +++ b/project-builder/sbt/build.sh @@ -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 @@ -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 @@ -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 diff --git a/project-builder/shared/CommunityBuildCore.scala b/project-builder/shared/CommunityBuildCore.scala index a98defe4..84521b40 100644 --- a/project-builder/shared/CommunityBuildCore.scala +++ b/project-builder/shared/CommunityBuildCore.scala @@ -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 + + } + } }