|
| 1 | +package dotty.tools |
| 2 | + |
| 3 | +import scala.language.unsafeNulls |
| 4 | + |
| 5 | +import scala.annotation.tailrec |
| 6 | +import scala.io.Source |
| 7 | +import scala.util.Try |
| 8 | +import java.io.File |
| 9 | +import java.lang.Thread |
| 10 | +import scala.annotation.internal.sharable |
| 11 | +import dotty.tools.dotc.util.ClasspathFromClassloader |
| 12 | +import dotty.tools.runner.ObjectRunner |
| 13 | +import dotty.tools.dotc.config.Properties.envOrNone |
| 14 | +import dotty.tools.io.Jar |
| 15 | +import dotty.tools.runner.ScalaClassLoader |
| 16 | +import java.nio.file.Paths |
| 17 | +import dotty.tools.dotc.config.CommandLineParser |
| 18 | +import dotty.tools.scripting.StringDriver |
| 19 | + |
| 20 | +enum CompileMode: |
| 21 | + case Guess |
| 22 | + case Compile |
| 23 | + case Decompile |
| 24 | + case PrintTasty |
| 25 | + case Script |
| 26 | + case Repl |
| 27 | + case Run |
| 28 | + |
| 29 | +case class CompileSettings( |
| 30 | + verbose: Boolean = false, |
| 31 | + classPath: List[String] = List.empty, |
| 32 | + compileMode: CompileMode = CompileMode.Guess, |
| 33 | + exitCode: Int = 0, |
| 34 | + javaArgs: List[String] = List.empty, |
| 35 | + javaProps: List[(String, String)] = List.empty, |
| 36 | + scalaArgs: List[String] = List.empty, |
| 37 | + residualArgs: List[String] = List.empty, |
| 38 | + scriptArgs: List[String] = List.empty, |
| 39 | + targetScript: String = "", |
| 40 | + compiler: Boolean = false, |
| 41 | + quiet: Boolean = false, |
| 42 | + colors: Boolean = false, |
| 43 | +) { |
| 44 | + def withCompileMode(em: CompileMode): CompileSettings = this.compileMode match |
| 45 | + case CompileMode.Guess => |
| 46 | + this.copy(compileMode = em) |
| 47 | + case _ => |
| 48 | + println(s"compile_mode==[$compileMode], attempted overwrite by [$em]") |
| 49 | + this.copy(exitCode = 1) |
| 50 | + end withCompileMode |
| 51 | + |
| 52 | + def withScalaArgs(args: String*): CompileSettings = |
| 53 | + this.copy(scalaArgs = scalaArgs.appendedAll(args.toList.filter(_.nonEmpty))) |
| 54 | + |
| 55 | + def withJavaArgs(args: String*): CompileSettings = |
| 56 | + this.copy(javaArgs = javaArgs.appendedAll(args.toList.filter(_.nonEmpty))) |
| 57 | + |
| 58 | + def withJavaProps(args: (String, String)*): CompileSettings = |
| 59 | + this.copy(javaProps = javaProps.appendedAll(args.toList)) |
| 60 | + |
| 61 | + def withResidualArgs(args: String*): CompileSettings = |
| 62 | + this.copy(residualArgs = residualArgs.appendedAll(args.toList.filter(_.nonEmpty))) |
| 63 | + |
| 64 | + def withScriptArgs(args: String*): CompileSettings = |
| 65 | + this.copy(scriptArgs = scriptArgs.appendedAll(args.toList.filter(_.nonEmpty))) |
| 66 | + |
| 67 | + def withTargetScript(file: String): CompileSettings = |
| 68 | + Try(Source.fromFile(file)).toOption match |
| 69 | + case Some(_) => this.copy(targetScript = file) |
| 70 | + case None => |
| 71 | + println(s"not found $file") |
| 72 | + this.copy(exitCode = 2) |
| 73 | + end withTargetScript |
| 74 | + |
| 75 | + def withCompiler: CompileSettings = |
| 76 | + this.copy(compiler = true) |
| 77 | + |
| 78 | + def withQuiet: CompileSettings = |
| 79 | + this.copy(quiet = true) |
| 80 | + |
| 81 | + def withColors: CompileSettings = |
| 82 | + this.copy(colors = true) |
| 83 | + |
| 84 | + def withNoColors: CompileSettings = |
| 85 | + this.copy(colors = false) |
| 86 | +} |
| 87 | + |
| 88 | +object MainGenericCompiler { |
| 89 | + |
| 90 | + val classpathSeparator = File.pathSeparator |
| 91 | + |
| 92 | + @sharable val javaOption = raw"""-J(.*)""".r |
| 93 | + @sharable val javaPropOption = raw"""-D(.+?)=(.?)""".r |
| 94 | + @tailrec |
| 95 | + def process(args: List[String], settings: CompileSettings): CompileSettings = args match |
| 96 | + case Nil => |
| 97 | + settings |
| 98 | + case "--" :: tail => |
| 99 | + process(Nil, settings.withResidualArgs(tail.toList*)) |
| 100 | + case ("-v" | "-verbose" | "--verbose") :: tail => |
| 101 | + process(tail, settings.withScalaArgs("-verbose")) |
| 102 | + case ("-q" | "-quiet") :: tail => |
| 103 | + process(tail, settings.withQuiet) |
| 104 | + case "-repl" :: tail => |
| 105 | + process(tail, settings.withCompileMode(CompileMode.Repl)) |
| 106 | + case "-script" :: targetScript :: tail => |
| 107 | + process(Nil, settings |
| 108 | + .withCompileMode(CompileMode.Script) |
| 109 | + .withJavaProps("script.path" -> targetScript) |
| 110 | + .withTargetScript(targetScript) |
| 111 | + .withScriptArgs(tail*)) |
| 112 | + case "-compile" :: tail => |
| 113 | + process(tail, settings.withCompileMode(CompileMode.Compile)) |
| 114 | + case "-decompile" :: tail => |
| 115 | + process(tail, settings.withCompileMode(CompileMode.Decompile)) |
| 116 | + case "-print-tasty" :: tail => |
| 117 | + process(tail, settings.withCompileMode(CompileMode.PrintTasty)) |
| 118 | + case "-run" :: tail => |
| 119 | + process(tail, settings.withCompileMode(CompileMode.Run)) |
| 120 | + case "-colors" :: tail => |
| 121 | + process(tail, settings.withColors) |
| 122 | + case "-no-colors" :: tail => |
| 123 | + process(tail, settings.withNoColors) |
| 124 | + case "-with-compiler" :: tail => |
| 125 | + process(tail, settings.withCompiler) |
| 126 | + case ("-cp" | "-classpath" | "--class-path") :: cp :: tail => |
| 127 | + val (tailargs, newEntries) = MainGenericRunner.processClasspath(cp, tail) |
| 128 | + process(tailargs, settings.copy(classPath = settings.classPath ++ newEntries.filter(_.nonEmpty))) |
| 129 | + case "-Oshort" :: tail => |
| 130 | + // Nothing is to be done here. Request that the user adds the relevant flags manually. |
| 131 | + // i.e this has no effect when MainGenericRunner is invoked programatically. |
| 132 | + val addTC="-XX:+TieredCompilation" |
| 133 | + val tStopAtLvl="-XX:TieredStopAtLevel=1" |
| 134 | + println(s"ignoring deprecated -Oshort flag, please add `-J$addTC` and `-J$tStopAtLvl` flags manually") |
| 135 | + process(tail, settings) |
| 136 | + case javaOption(stripped) :: tail => |
| 137 | + process(tail, settings.withJavaArgs(stripped)) |
| 138 | + case javaPropOption(opt, value) :: tail => |
| 139 | + process(tail, settings.withJavaProps(opt -> value)) |
| 140 | + case arg :: tail => |
| 141 | + process(tail, settings.withResidualArgs(arg)) |
| 142 | + end process |
| 143 | + |
| 144 | + def main(args: Array[String]): Unit = |
| 145 | + val settings = process(args.toList, CompileSettings()) |
| 146 | + if settings.exitCode != 0 then System.exit(settings.exitCode) |
| 147 | + |
| 148 | + def classpathSetting = |
| 149 | + if settings.classPath.isEmpty then List() |
| 150 | + else List("-classpath", settings.classPath.mkString(classpathSeparator)) |
| 151 | + |
| 152 | + def reconstructedArgs() = |
| 153 | + classpathSetting ++ settings.scalaArgs ++ settings.residualArgs |
| 154 | + |
| 155 | + def addJavaProps(): Unit = |
| 156 | + settings.javaProps.foreach { (k, v) => sys.props(k) = v } |
| 157 | + |
| 158 | + def run(settings: CompileSettings): Unit = settings.compileMode match |
| 159 | + case CompileMode.Compile => |
| 160 | + addJavaProps() |
| 161 | + val properArgs = reconstructedArgs() |
| 162 | + dotty.tools.dotc.Main.main(properArgs.toArray) |
| 163 | + case CompileMode.Decompile => |
| 164 | + addJavaProps() |
| 165 | + val properArgs = reconstructedArgs() |
| 166 | + dotty.tools.dotc.decompiler.Main.main(properArgs.toArray) |
| 167 | + case CompileMode.PrintTasty => |
| 168 | + addJavaProps() |
| 169 | + val properArgs = reconstructedArgs() |
| 170 | + dotty.tools.dotc.core.tasty.TastyPrinter.main(properArgs.toArray) |
| 171 | + case CompileMode.Script => // Naive copy from scalac bash script |
| 172 | + addJavaProps() |
| 173 | + val properArgs = |
| 174 | + reconstructedArgs() |
| 175 | + ++ List("-script", settings.targetScript) |
| 176 | + ++ settings.scriptArgs |
| 177 | + scripting.Main.main(properArgs.toArray) |
| 178 | + case CompileMode.Repl | CompileMode.Run => |
| 179 | + addJavaProps() |
| 180 | + val properArgs = reconstructedArgs() |
| 181 | + repl.Main.main(properArgs.toArray) |
| 182 | + case CompileMode.Guess => |
| 183 | + run(settings.withCompileMode(CompileMode.Compile)) |
| 184 | + end run |
| 185 | + |
| 186 | + run(settings) |
| 187 | + end main |
| 188 | +} |
0 commit comments