Skip to content

Commit 24124d5

Browse files
committed
Add setting to restrict macro classpath
For better cacheability
1 parent ec669bf commit 24124d5

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/compiler/scala/tools/nsc/settings/ScalaSettings.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ trait ScalaSettings extends AbsScalaSettings
265265
val YcachePluginClassLoader = CachePolicy.setting("plugin", "compiler plugins")
266266
val YcacheMacroClassLoader = CachePolicy.setting("macro", "macros")
267267

268+
val YmacroClasspath = PathSetting ("-Ymacro-classpath", "The classpath used to reflectively load macro implementations", "")
269+
268270
val exposeEmptyPackage = BooleanSetting ("-Yexpose-empty-package", "Internal only: expose the empty package.").internalOnly()
269271
val Ydelambdafy = ChoiceSetting ("-Ydelambdafy", "strategy", "Strategy used for translating lambdas into JVM code.", List("inline", "method"), "method")
270272

src/compiler/scala/tools/nsc/typechecker/Macros.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ package scala.tools.nsc
1414
package typechecker
1515

1616
import java.lang.Math.min
17+
import java.net.URL
18+
1719
import symtab.Flags._
1820
import scala.reflect.internal.util.ScalaClassLoader
1921
import scala.reflect.runtime.ReflectionUtils
@@ -22,6 +24,7 @@ import scala.reflect.internal.TypesStats
2224
import scala.reflect.macros.util._
2325
import scala.util.control.ControlThrowable
2426
import scala.reflect.internal.util.ListOfNil
27+
import scala.reflect.io.AbstractFile
2528
import scala.reflect.macros.runtime.{AbortMacroException, MacroRuntimes}
2629
import scala.reflect.macros.compiler.DefaultMacroCompiler
2730
import scala.tools.reflect.FastTrack
@@ -75,7 +78,12 @@ trait Macros extends MacroRuntimes with Traces with Helpers {
7578
* Mirrors with runtime definitions (e.g. Repl) need to adjust this method.
7679
*/
7780
protected def findMacroClassLoader(): ClassLoader = {
78-
val classpath = global.classPath.asURLs
81+
val classpath: Seq[URL] = if (settings.YmacroClasspath.isSetByUser) {
82+
for {
83+
file <- scala.tools.nsc.util.ClassPath.expandPath(settings.YmacroClasspath.value, true)
84+
af <- Option(AbstractFile getDirectory file)
85+
} yield af.file.toURI.toURL
86+
} else global.classPath.asURLs
7987
def newLoader = () => {
8088
macroLogVerbose("macro classloader: initializing from -cp: %s".format(classpath))
8189
ScalaClassLoader.fromURLs(classpath, self.getClass.getClassLoader)

0 commit comments

Comments
 (0)