1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
/* __ *\
** ________ ___ / / ___ Scala Ant Tasks **
** / __/ __// _ | / / / _ | (c) 2005-2013, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
package scala.tools.ant
import org.apache.tools.ant.AntClassLoader
import org.apache.tools.ant.taskdefs.Java
import org.apache.tools.ant.types.Path
import scala.tools.nsc.Settings
import scala.tools.nsc.io.File
import scala.tools.nsc.settings.FscSettings
import scala.reflect.internal.util.ScalaClassLoader
/** An Ant task to compile with the fast Scala compiler (`fsc`).
*
* In addition to the attributes shared with the `Scalac` task, this task
* also accepts the following attributes:
* - `reset`
* - `server`
* - `shutdown`
* - `ipv4`
* - `maxIdle`
*
* @author Stephane Micheloud
*/
class FastScalac extends Scalac {
private var resetCaches: Boolean = false
private var serverAddr: Option[String] = None
private var shutdownServer: Boolean = false
private var useIPv4: Boolean = false
private var idleMinutes: Option[Int] = None
/*============================================================================*\
** Properties setters **
\*============================================================================*/
/** Sets the `reset` attribute. Used by [[http://ant.apache.org Ant]].
*
* @param input The value for `reset`.
*/
def setReset(input: Boolean) { resetCaches = input }
/** Sets the `server` attribute. Used by [[http://ant.apache.org Ant]].
*
* @param input The value for `server`.
*/
def setServer(input: String) { serverAddr = Some(input) }
/** Sets the `shutdown` attribute. Used by [[http://ant.apache.org Ant]].
*
* @param input The value for `shutdown`.
*/
def setShutdown(input: Boolean) { shutdownServer = input }
/** Sets the `ipv4` attribute. Used by [[http://ant.apache.org Ant]].
*
* @param input The value for `ipv4`.
*/
def setIPv4(input: Boolean) { useIPv4 = input }
/** Sets the `maxIdle` attribute. Used by [[http://ant.apache.org Ant]].
*
* @param input The value for `maxIdle`.
*/
def setMaxIdle(input: Int) { if (0 <= input) idleMinutes = Some(input) }
/*============================================================================*\
** The execute method **
\*============================================================================*/
override protected def newSettings(error: String=>Unit): Settings =
new FscSettings(error)
/** Performs the compilation. */
override def execute() {
val (settings, sourceFiles, javaOnly) = initialize
if (sourceFiles.isEmpty || javaOnly)
return
// initialize fsc specific settings
val s = settings.asInstanceOf[FscSettings] // safe (newSettings)
s.reset.value = resetCaches
if (!serverAddr.isEmpty) s.server.value = serverAddr.get
s.shutdown.value = shutdownServer
s.preferIPv4.value = useIPv4
if (!idleMinutes.isEmpty) s.idleMins.value = idleMinutes.get
val stringSettings =
List(
/*scalac*/
s.bootclasspath, s.classpath, s.extdirs, s.dependencyfile, s.encoding,
s.outdir, s.sourcepath,
/*fsc*/
s.server
) filter (_.value != "") flatMap (x => List(x.name, x.value))
val choiceSettings =
List(
/*scalac*/
s.debuginfo, s.target
) filter (x => x.value != x.default) map (x => "%s:%s".format(x.name, x.value))
val booleanSettings =
List(
/*scalac*/
s.debug, s.deprecation, s.explaintypes, s.nospecialization, s.nowarn,
s.optimise, s.unchecked, s.usejavacp, s.verbose,
/*fsc*/
s.preferIPv4, s.reset, s.shutdown
) filter (_.value) map (_.name)
val intSettings =
List(
/*fsc*/
s.idleMins
) filter (x => x.value != x.default) flatMap (x => List(x.name, x.value.toString))
val phaseSetting = {
val s = settings.log
if (s.value.isEmpty) Nil
else List("%s:%s".format(s.name, s.value.mkString(",")))
}
val fscOptions =
stringSettings ::: choiceSettings ::: booleanSettings ::: intSettings ::: phaseSetting
val java = new Java(this)
java setFork true
// use same default memory options as in fsc script
java.createJvmarg() setValue "-Xmx256M"
java.createJvmarg() setValue "-Xms32M"
val scalacPath: Path = {
val path = new Path(getProject)
if (compilerPath.isDefined) path add compilerPath.get
else getClass.getClassLoader match {
case cl: AntClassLoader =>
path add new Path(getProject, cl.getClasspath)
case _ =>
buildError("Compilation failed because of an internal compiler error;"+
" see the error output for details.")
}
path
}
java.createJvmarg() setValue ("-Xbootclasspath/a:"+scalacPath)
s.jvmargs.value foreach (java.createJvmarg() setValue _)
val scalaHome: String = try {
val url = ScalaClassLoader.originOfClass(classOf[FastScalac]).get
File(url.getFile).jfile.getParentFile.getParentFile.getAbsolutePath
} catch {
case _: Throwable =>
buildError("Compilation failed because of an internal compiler error;"+
" couldn't determine value for -Dscala.home=<value>")
}
java.createJvmarg() setValue "-Dscala.usejavacp=true"
java.createJvmarg() setValue ("-Dscala.home="+scalaHome)
s.defines.value foreach (java.createJvmarg() setValue _)
java setClassname "scala.tools.nsc.MainGenericRunner"
java.createArg() setValue "scala.tools.nsc.CompileClient"
// Encode scalac/javac args for use in a file to be read back via "@file.txt"
def encodeScalacArgsFile(t: Traversable[String]) = t map { s =>
if(s.find(c => c <= ' ' || "\"'\\".contains(c)).isDefined)
"\"" + s.flatMap(c => (if(c == '"' || c == '\\') "\\" else "") + c ) + "\""
else s
} mkString "\n"
// dump the arguments to a file and do "java @file"
val tempArgFile = File.makeTemp("fastscalac")
val tokens = fscOptions ++ (sourceFiles map (_.getPath))
tempArgFile writeAll encodeScalacArgsFile(tokens)
val paths = List(Some(tempArgFile.toAbsolute.path), argfile).flatten map (_.toString)
val res = execWithArgFiles(java, paths)
if (failonerror && res != 0)
buildError("Compilation failed because of an internal compiler error;"+
" see the error output for details.")
}
}
|