CLOVER🍀

That was when it all began.

scala/scalac/fscなどのコマンドのオプションを、Scalaコンパイラのソースから確認する

先ほどまで、とあるライブラリを使ったコードのコンパイルがうまくいかなくて、Scalaコンパイラのコードとかを眺めていたわけですが、その時にbuild.gradleに

compileScala {
  compileScala.scalaCompileOptions.additionalParameters= ['-Ydebug']
}

みたいなデバッグオプションを付けてコンパイルしていました。

このオプションをどうやって見つけたかですが、Scalaコンパイラのソースから見つけました。

コケていた「scala/tools/nsc/symtab/classfile/ClassfileParser.scala」に

// Code from scala/tools/nsc/symtab/classfile/ClassfileParser.scala...
  private def handleError(e: Exception) = {
    if (settings.debug.value) e.printStackTrace()
    throw new IOException(s"class file '${in.file}' is broken\n(${e.getClass}/${e.getMessage})")
  }

みたいなことが書いてあったので、なんとかして

    if (settings.debug.value) e.printStackTrace()

を有効にしてスタックトレースを出したいというところからきています。

で、実際これを有効にするためのオプションが定義してあったクラスを含めて、scala/scalac/fscなどの各種コマンドで使えるオプションを確認するには、以下のトレイトやクラスを見るとよさそうです。

scala.tools.nsc.settings.Warnings
scala.tools.nsc.settings.StandardScalaSettings
scala.tools.nsc.settings.ScalaSettings
scala.tools.nsc.settings.FscSettings
scala.tools.nsc.GenericRunnerSettings

…一応、トレイトの順番とかは考えて並べてあります。

例えば、今回使った「-Ydebug」オプションですが、scala.tools.nsc.settings.ScalaSettingsに定義してありました。

  val debug           = BooleanSetting    ("-Ydebug", "Increase the quantity of debugging output.")

「-X」なオプションとかも、定義してありました。

  val Xprint        = PhasesSetting     ("-Xprint", "Print out program after")
  val writeICode    = PhasesSetting     ("-Xprint-icode", "Log internal icode to *.icode files after", "icode")
  val Xprintpos     = BooleanSetting    ("-Xprint-pos", "Print tree positions, as offsets.")
  val printtypes    = BooleanSetting    ("-Xprint-types", "Print tree types (debugging option).")

オーソドックスなオプションは、StandardScalaSettingsを見ればいいみたいです。

trait StandardScalaSettings {
  self: AbsScalaSettings =>

  /** Path related settings.
   */
  val bootclasspath =     PathSetting ("-bootclasspath", "Override location of bootstrap class files.", Defaults.scalaBootClassPath)
  val classpath:          PathSetting // is mutated directly in various places (thus inspiring this very effort)
  val d:                OutputSetting // depends on mutable OutputDirs class
  val extdirs =           PathSetting ("-extdirs", "Override location of installed extensions.", Defaults.scalaExtDirs)
  val javabootclasspath = PathSetting ("-javabootclasspath", "Override java boot classpath.", Defaults.javaBootClassPath)
  val javaextdirs =       PathSetting ("-javaextdirs", "Override java extdirs classpath.", Defaults.javaExtDirs)
  val sourcepath =        PathSetting ("-sourcepath", "Specify location(s) of source files.", "") // Defaults.scalaSourcePath

  /** Other settings.
   */
  val dependencyfile =  StringSetting ("-dependencyfile", "file", "Set dependency tracking file.", ".scala_dependencies")
  val deprecation =    BooleanSetting ("-deprecation", "Emit warning and location for usages of deprecated APIs.")
  val encoding =        StringSetting ("-encoding", "encoding", "Specify character encoding used by source files.", Properties.sourceEncoding)
  val explaintypes =   BooleanSetting ("-explaintypes", "Explain type errors in more detail.")
  val feature =        BooleanSetting ("-feature", "Emit warning and location for usages of features that should be imported explicitly.")
  val g =               ChoiceSetting ("-g", "level", "Set level of generated debugging info.", List("none", "source", "line", "vars", "notailcalls"), "vars")
  val help =           BooleanSetting ("-help", "Print a synopsis of standard options")
  val make =            ChoiceSetting ("-make", "policy", "Recompilation detection policy", List("all", "changed", "immediate", "transitive", "transitivenocp"), "all")
                        . withDeprecationMessage ("this option is unmaintained.  Use sbt or an IDE for selective recompilation.")
  val nowarn =         BooleanSetting ("-nowarn", "Generate no warnings.")
  val optimise:        BooleanSetting // depends on post hook which mutates other settings
  val print =          BooleanSetting ("-print", "Print program with Scala-specific features removed.")
  val target =          ChoiceSetting ("-target", "target", "Target platform for object files. All JVM 1.5 targets are deprecated.",
                                       List("jvm-1.5", "jvm-1.5-fjbg", "jvm-1.5-asm", "jvm-1.6", "jvm-1.7", "msil"),
                                       "jvm-1.6")
  val unchecked =      BooleanSetting ("-unchecked", "Enable additional warnings where generated code depends on assumptions.")
  val uniqid =         BooleanSetting ("-uniqid", "Uniquely tag all identifiers in debugging output.")
  val usejavacp =      BooleanSetting ("-usejavacp", "Utilize the java.class.path in classpath resolution.")
  val verbose =        BooleanSetting ("-verbose", "Output messages about what the compiler is doing.")
  val version =        BooleanSetting ("-version", "Print product version and exit.")

  /** These are @<file> and -Dkey=val style settings, which don't
   *  nicely map to identifiers.
   */
  val argfiles: BooleanSetting  // exists only to echo help message, should be done differently
}

ScalaSettingsは、StandardScalaSettingsとWarningsをミックスインしたトレイト、FscSettingsとGenericRunnerSettingsは上位でScalaSettingsをミックスインしているSettingsクラスを継承しているクラスになります。

だいたい、コマンドとの対応がつきそうな感じです。

scalacコマンドに特化したものがありませんが、scalaコマンドの説明で

PARAMETERS


Any scalac option. See scalac(1).

とあるので、scalaコマンドと定義がそれなりに被ってるってことですね。

scalaコマンドのオプションは、ScalaSettingsのオプションにGenericRunnerSettingsで定義したオプションを加えたものだと思います。そして、scalacコマンドで使えるのはScalaSettingsで定義したところまで、ということでしょうね。