Skip to content

Commit 08ebda0

Browse files
committed
MainGeneric handles empty -classpath
1 parent 440597f commit 08ebda0

File tree

2 files changed

+108
-87
lines changed

2 files changed

+108
-87
lines changed

compiler/src/dotty/tools/MainGenericCompiler.scala

Lines changed: 98 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import java.io.File
77
import java.lang.Thread
88
import scala.annotation.internal.sharable
99
import dotty.tools.dotc.util.ClasspathFromClassloader
10+
import dotty.tools.dotc.util.chaining.*
1011
import dotty.tools.runner.ObjectRunner
1112
import dotty.tools.dotc.config.Properties.envOrNone
1213
import dotty.tools.io.Jar
@@ -82,66 +83,82 @@ case class CompileSettings(
8283
def withNoColors: CompileSettings =
8384
this.copy(colors = false)
8485
}
86+
object CompileSettings:
87+
@sharable val javaOption = raw"""-J(.*)""".r
88+
@sharable val javaPropOption = raw"""-D(.+?)=(.?)""".r
89+
90+
def from(args: List[String]): CompileSettings =
91+
@tailrec def process(args: List[String], settings: CompileSettings): CompileSettings = args match
92+
case Nil =>
93+
settings
94+
case "--" :: tail =>
95+
process(Nil, settings.withResidualArgs(tail.toList*))
96+
case ("-v" | "-verbose" | "--verbose") :: tail =>
97+
process(tail, settings.withScalaArgs("-verbose"))
98+
case ("-q" | "-quiet") :: tail =>
99+
process(tail, settings.withQuiet)
100+
case "-repl" :: tail =>
101+
process(tail, settings.withCompileMode(CompileMode.Repl))
102+
case "-script" :: targetScript :: tail =>
103+
process(Nil, settings
104+
.withCompileMode(CompileMode.Script)
105+
.withJavaProps("script.path" -> targetScript)
106+
.withTargetScript(targetScript)
107+
.withScriptArgs(tail*))
108+
case "-compile" :: tail =>
109+
process(tail, settings.withCompileMode(CompileMode.Compile))
110+
case "-decompile" :: tail =>
111+
process(tail, settings.withCompileMode(CompileMode.Decompile))
112+
case "-print-tasty" :: tail =>
113+
process(tail, settings.withCompileMode(CompileMode.PrintTasty))
114+
case "-run" :: tail =>
115+
process(tail, settings.withCompileMode(CompileMode.Run))
116+
case "-colors" :: tail =>
117+
process(tail, settings.withColors)
118+
case "-no-colors" :: tail =>
119+
process(tail, settings.withNoColors)
120+
case "-with-compiler" :: tail =>
121+
process(tail, settings.withCompiler)
122+
case ("-cp" | "-classpath" | "--class-path") :: cp :: tail =>
123+
if cp.startsWith("-") then
124+
process(cp :: tail, settings)
125+
else
126+
MainGenericRunner.processClasspath(cp, tail) match
127+
case (tail, newEntries) =>
128+
process(tail, 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+
process(args, new CompileSettings)
144+
end from
145+
end CompileSettings
85146

86147
object MainGenericCompiler {
87148

88149
val classpathSeparator = File.pathSeparator
89150

90-
@sharable val javaOption = raw"""-J(.*)""".r
91-
@sharable val javaPropOption = raw"""-D(.+?)=(.?)""".r
92-
@tailrec
93-
def process(args: List[String], settings: CompileSettings): CompileSettings = args match
94-
case Nil =>
95-
settings
96-
case "--" :: tail =>
97-
process(Nil, settings.withResidualArgs(tail.toList*))
98-
case ("-v" | "-verbose" | "--verbose") :: tail =>
99-
process(tail, settings.withScalaArgs("-verbose"))
100-
case ("-q" | "-quiet") :: tail =>
101-
process(tail, settings.withQuiet)
102-
case "-repl" :: tail =>
103-
process(tail, settings.withCompileMode(CompileMode.Repl))
104-
case "-script" :: targetScript :: tail =>
105-
process(Nil, settings
106-
.withCompileMode(CompileMode.Script)
107-
.withJavaProps("script.path" -> targetScript)
108-
.withTargetScript(targetScript)
109-
.withScriptArgs(tail*))
110-
case "-compile" :: tail =>
111-
process(tail, settings.withCompileMode(CompileMode.Compile))
112-
case "-decompile" :: tail =>
113-
process(tail, settings.withCompileMode(CompileMode.Decompile))
114-
case "-print-tasty" :: tail =>
115-
process(tail, settings.withCompileMode(CompileMode.PrintTasty))
116-
case "-run" :: tail =>
117-
process(tail, settings.withCompileMode(CompileMode.Run))
118-
case "-colors" :: tail =>
119-
process(tail, settings.withColors)
120-
case "-no-colors" :: tail =>
121-
process(tail, settings.withNoColors)
122-
case "-with-compiler" :: tail =>
123-
process(tail, settings.withCompiler)
124-
case ("-cp" | "-classpath" | "--class-path") :: cp :: tail =>
125-
val (tailargs, newEntries) = MainGenericRunner.processClasspath(cp, tail)
126-
process(tailargs, settings.copy(classPath = settings.classPath ++ newEntries.filter(_.nonEmpty)))
127-
case "-Oshort" :: tail =>
128-
// Nothing is to be done here. Request that the user adds the relevant flags manually.
129-
// i.e this has no effect when MainGenericRunner is invoked programatically.
130-
val addTC="-XX:+TieredCompilation"
131-
val tStopAtLvl="-XX:TieredStopAtLevel=1"
132-
println(s"ignoring deprecated -Oshort flag, please add `-J$addTC` and `-J$tStopAtLvl` flags manually")
133-
process(tail, settings)
134-
case javaOption(stripped) :: tail =>
135-
process(tail, settings.withJavaArgs(stripped))
136-
case javaPropOption(opt, value) :: tail =>
137-
process(tail, settings.withJavaProps(opt -> value))
138-
case arg :: tail =>
139-
process(tail, settings.withResidualArgs(arg))
140-
end process
141-
142151
def main(args: Array[String]): Unit =
143-
val settings = process(args.toList, CompileSettings())
144-
if settings.exitCode != 0 then System.exit(settings.exitCode)
152+
import CompileMode.*
153+
import dotc.Main.main as compiler
154+
import dotc.decompiler.Main.main as decompiler
155+
import dotc.core.tasty.TastyPrinter.main as tastyPrinter
156+
import scripting.Main.main as scripting
157+
import repl.Main.main as repl
158+
159+
val settings = CompileSettings.from(args.toList)
160+
.tap: settings =>
161+
if settings.exitCode != 0 then System.exit(settings.exitCode)
145162

146163
def classpathSetting =
147164
if settings.classPath.isEmpty then List()
@@ -153,34 +170,32 @@ object MainGenericCompiler {
153170
def addJavaProps(): Unit =
154171
settings.javaProps.foreach { (k, v) => sys.props(k) = v }
155172

156-
def run(settings: CompileSettings): Unit = settings.compileMode match
157-
case CompileMode.Compile =>
158-
addJavaProps()
159-
val properArgs = reconstructedArgs()
160-
dotty.tools.dotc.Main.main(properArgs.toArray)
161-
case CompileMode.Decompile =>
162-
addJavaProps()
163-
val properArgs = reconstructedArgs()
164-
dotty.tools.dotc.decompiler.Main.main(properArgs.toArray)
165-
case CompileMode.PrintTasty =>
166-
addJavaProps()
167-
val properArgs = reconstructedArgs()
168-
dotty.tools.dotc.core.tasty.TastyPrinter.main(properArgs.toArray)
169-
case CompileMode.Script => // Naive copy from scalac bash script
170-
addJavaProps()
171-
val properArgs =
172-
reconstructedArgs()
173-
++ List("-script", settings.targetScript)
174-
++ settings.scriptArgs
175-
scripting.Main.main(properArgs.toArray)
176-
case CompileMode.Repl | CompileMode.Run =>
177-
addJavaProps()
178-
val properArgs = reconstructedArgs()
179-
repl.Main.main(properArgs.toArray)
180-
case CompileMode.Guess =>
181-
run(settings.withCompileMode(CompileMode.Compile))
182-
end run
183-
184-
run(settings)
173+
settings.compileMode match
174+
case Compile =>
175+
addJavaProps()
176+
val properArgs = reconstructedArgs()
177+
compiler(properArgs.toArray)
178+
case Decompile =>
179+
addJavaProps()
180+
val properArgs = reconstructedArgs()
181+
decompiler(properArgs.toArray)
182+
case PrintTasty =>
183+
addJavaProps()
184+
val properArgs = reconstructedArgs()
185+
tastyPrinter(properArgs.toArray)
186+
case Script => // Naive copy from scalac bash script
187+
addJavaProps()
188+
val properArgs =
189+
reconstructedArgs()
190+
++ List("-script", settings.targetScript)
191+
++ settings.scriptArgs
192+
scripting(properArgs.toArray)
193+
case Repl | Run =>
194+
addJavaProps()
195+
val properArgs = reconstructedArgs()
196+
repl(properArgs.toArray)
197+
case Guess =>
198+
addJavaProps()
199+
compiler(reconstructedArgs().toArray)
185200
end main
186201
}

compiler/src/dotty/tools/MainGenericRunner.scala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ object MainGenericRunner {
9898

9999
val classpathSeparator = File.pathSeparator
100100

101+
// returns (tail, cp.split) modulo globbing
101102
def processClasspath(cp: String, tail: List[String]): (List[String], List[String]) =
102103
val cpEntries = cp.split(classpathSeparator).toList
103104
val singleEntryClasspath: Boolean = cpEntries.take(2).size == 1
@@ -122,8 +123,12 @@ object MainGenericRunner {
122123
case "-run" :: fqName :: tail =>
123124
processArgs(tail, settings.withExecuteMode(ExecuteMode.Run).withTargetToRun(fqName))
124125
case ("-cp" | "-classpath" | "--class-path") :: cp :: tail =>
125-
val (tailargs, newEntries) = processClasspath(cp, tail)
126-
processArgs(tailargs, settings.copy(classPath = settings.classPath ++ newEntries.filter(_.nonEmpty)))
126+
if cp.startsWith("-") then
127+
processArgs(cp :: tail, settings)
128+
else
129+
processClasspath(cp, tail) match
130+
case (tail, newEntries) =>
131+
processArgs(tail, settings.copy(classPath = settings.classPath ++ newEntries.filter(_.nonEmpty)))
127132
case ("-version" | "--version") :: _ =>
128133
settings.copy(
129134
executeMode = ExecuteMode.Repl,
@@ -166,7 +171,9 @@ object MainGenericRunner {
166171
.withTargetScript(arg)
167172
.withScriptArgs(tail*)
168173
else
169-
val newSettings = if arg.startsWith("-") then settings else settings.withPossibleEntryPaths(arg).withModeShouldBePossibleRun
174+
val newSettings =
175+
if arg.startsWith("-") then settings
176+
else settings.withPossibleEntryPaths(arg).withModeShouldBePossibleRun
170177
processArgs(tail, newSettings.withResidualArgs(arg))
171178
end processArgs
172179

@@ -293,5 +300,4 @@ object MainGenericRunner {
293300

294301
def main(args: Array[String]): Unit =
295302
if (!process(args)) System.exit(1)
296-
297303
}

0 commit comments

Comments
 (0)