@@ -7,6 +7,7 @@ import java.io.File
7
7
import java .lang .Thread
8
8
import scala .annotation .internal .sharable
9
9
import dotty .tools .dotc .util .ClasspathFromClassloader
10
+ import dotty .tools .dotc .util .chaining .*
10
11
import dotty .tools .runner .ObjectRunner
11
12
import dotty .tools .dotc .config .Properties .envOrNone
12
13
import dotty .tools .io .Jar
@@ -82,66 +83,82 @@ case class CompileSettings(
82
83
def withNoColors : CompileSettings =
83
84
this .copy(colors = false )
84
85
}
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
85
146
86
147
object MainGenericCompiler {
87
148
88
149
val classpathSeparator = File .pathSeparator
89
150
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
-
142
151
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)
145
162
146
163
def classpathSetting =
147
164
if settings.classPath.isEmpty then List ()
@@ -153,34 +170,32 @@ object MainGenericCompiler {
153
170
def addJavaProps (): Unit =
154
171
settings.javaProps.foreach { (k, v) => sys.props(k) = v }
155
172
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)
185
200
end main
186
201
}
0 commit comments