summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/util/PathResolver.scala
blob: 3aca9c3ee08de384a9d5b64f0c1a871509224b73 (plain) (blame)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* NSC -- new Scala compiler
 * Copyright 2006-2010 LAMP/EPFL
 * @author  Paul Phillips
 */

package scala.tools
package util

import java.net.{ URL, MalformedURLException }
import nsc.{ Settings }
import nsc.util.{ ClassPath, JavaClassPath, ScalaClassLoader }
import nsc.io.{ File, Directory, Path }
import ClassPath.DefaultJavaContext
import File.{ pathSeparator }
import PartialFunction.condOpt

// Mostly based on the specification at:
// https://lampsvn.epfl.ch/trac/scala/wiki/Classpath
//

object PathResolver {
  private def propOrElse(name: String, alt: String) = System.getProperty(name, alt)
  private def envOrElse(name: String, alt: String)  = Option(System getenv name) getOrElse alt

  private def fileOpt(f: Path): Option[String]      = f ifFile (_.path)
  private def dirOpt(d: Path): Option[String]       = d ifDirectory (_.path)
  private def expandToPath(p: Path)                 = joincp(ClassPath.expandPath(p.path, true))
  private def expandToContents(p: Path)             = joincp(ClassPath.expandDir(p.path))
  private def joincp(xs: Seq[String]): String       = xs filterNot (_ == "") mkString pathSeparator
  private def splitcp(cp: String): Seq[String]      = cp split pathSeparator filterNot (_ == "") toSeq

  /** pretty print class path */
  def ppcp(s: String) = splitcp(s) match {
    case Nil      => ""
    case Seq(x)   => x
    case xs       => xs map ("\n" + _) mkString
  }

  /** Values found solely by inspecting environment or property variables.
   */
  object Environment {
    private def searchForBootClasspath = {
      import scala.collection.JavaConversions._
      System.getProperties find (_._1 endsWith ".boot.class.path") map (_._2) getOrElse ""
    }

    def classPathEnv        =  envOrElse("CLASSPATH", "")
    def toolPathEnv         =  envOrElse("TOOL_CLASSPATH", "")
    def classPathProp       = propOrElse("java.class.path", "")
    def javaBootClassPath   = propOrElse("sun.boot.class.path", searchForBootClasspath)
    def javaExtDirs         = propOrElse("java.ext.dirs", "")
    def userHome            = propOrElse("user.home", "")
    def scalaHome           = propOrElse("scala.home", "")

    def classPath           = List(classPathProp, classPathEnv) find (_ != "") getOrElse "."

    override def toString = """
      |object Environment {
      |  classPathEnv       = %s
      |  toolPathEnv        = %s
      |  classPathProp      = %s
      |  javaBootClassPath  = %s
      |  javaExtDirs        = %s
      |  userHome           = %s
      |  scalaHome          = %s
      |}""".trim.stripMargin.format(
      classPathEnv, toolPathEnv, ppcp(classPathProp), ppcp(javaBootClassPath),
      javaExtDirs, userHome, scalaHome
    )
  }

  /** Default values based on those in Environment as interpretered according
   *  to the path resolution specification.
   */
  object Defaults {
    private lazy val guessedScalaHome = {
      for (url <- ScalaClassLoader originOfClass classOf[ScalaObject] ; if url.getProtocol == "file") yield
        File(url.getFile).parent.path
    } getOrElse ""

    private def translateScalaHome(s: String) = s.replaceAll("""${SCALA_HOME}""", scalaHome)

    def scalaHome         = Environment.scalaHome match { case "" => guessedScalaHome ; case x => x }
    def scalaHomeDir      = Directory(scalaHome)
    def scalaLibDir       = Directory(scalaHomeDir / "lib")
    def scalaClassesDir   = Directory(scalaHomeDir / "classes")

    def scalaLibJar       = File(scalaLibDir / "scala-library.jar")
    def scalaLibClassDir  = Directory(scalaClassesDir / "library")
    def scalaLibFound: Option[Directory] =
      if (scalaLibJar.isFile) Some(scalaLibDir)
      else if (scalaLibClassDir.isDirectory) Some(scalaClassesDir)
      else None
    def scalaLibPath      = scalaLibFound map (_.path) getOrElse ""

    def scalaExtDirs          = scalaLibFound map (_.path) getOrElse ""
    def expandedScalaExtDirs  = if (scalaExtDirs == "") "" else expandToContents(scalaExtDirs)
    def expandedClassPath     = expandToPath(Environment.classPath)

    val pluginSearchPath    = List("misc", "scala-devel", "plugins")
    def scalaPluginDir      = pluginSearchPath map (scalaHomeDir / _) find (_.isDirectory) map (_.path) getOrElse ""

    // The class path that a runner script uses to interpret a program is called the “execution class path”.
    // The execution class path is the concatenation of the following sub-path.
    // If a class is available in multiple locations, it must be loaded from that with the lowest number.
    def executionPath = List(
      // 1. The Java bootstrap class path.
      Environment.javaBootClassPath,
      // 2. The Java extension class path.
      Environment.javaExtDirs,
      // 3. The first available path below.
      //    * The fixed class path (TOOL_CLASSPATH) set in the runner script when it was generated
      //      (which can be set as the "classpath" attribute when using the scalatool Ant task).
      //      This path may contain absolute locations, or locations relative to Scala's home by using
      //      the shell variable ${SCALA_HOME} in the path.
      //    * The class path formed by all JAR and ZIP files and all folders in Scala's home lib folder.
      Environment.toolPathEnv match {
        case "" => Defaults.expandedScalaExtDirs
        case x  => expandToPath(translateScalaHome(x))
      }
    )

    override def toString = """
      |object Defaults {
      |  scalaHome            = %s
      |  scalaLibFound       = %s
      |  scalaPluginDir       = %s
      |  expandedClassPath    = %s
      |}""".trim.stripMargin.format(
        scalaHome, scalaLibFound, scalaPluginDir,
        ppcp(expandedScalaExtDirs), ppcp(expandedClassPath)
      )
  }

  def executionPath = joincp(Defaults.executionPath)

  /** The original logic of MainGenericRunner.
   */
  def basicScalaClassPath(includeCwd: Boolean): String = {
    // this is to make the interpreter work when running without the scala script
    // (e.g. from eclipse). Before, "java.class.path" was added to the user classpath
    // in Settings; this was changed to match the behavior of Sun's javac.
    def maincp =
      if (Environment.scalaHome == "") Defaults.expandedClassPath
      else Defaults.expandedScalaExtDirs
    def dotcp = if (includeCwd) "." else ""

    joincp(Seq(maincp, dotcp))
  }

  /** XXX not yet used.
   */
  def toJavaClassPath(settings: Settings): JavaClassPath = {
    new JavaClassPath(
      settings.bootclasspath.value, settings.extdirs.value,
      settings.classpath.value, settings.sourcepath.value,
      settings.Xcodebase.value, DefaultJavaContext
    )
  }

  /** With no arguments, show the interesting values in Environment and Defaults.
   *  If there are arguments, show those in Calculated as if those options had been
   *  given to a scala runner.
   */
  def main(args: Array[String]): Unit = {
    if (args.isEmpty) {
      println(Environment)
      println(Defaults)
    }
    else {
      val settings = new Settings()
      val rest = settings.processArguments(args.toList)._2
      val pr = new PathResolver(settings)
      println(" COMMAND: 'scala %s'".format(args.mkString(" ")))
      println("RESIDUAL: 'scala %s'\n".format(rest.mkString(" ")))
      println(pr.Calculated)
    }
  }

  /**
   * Split command line parameters by space, properly process quoted parameter
   */
  def splitParams(line: String, errorFn: String => Unit): List[String] = {
    def parse(from: Int, i: Int, args: List[String]): List[String] = {
      if (i < line.length) {
        line.charAt(i) match {
          case ' ' =>
            val args1 = fetchArg(from, i) :: args
            val j = skipS(i + 1)
            if (j >= 0) {
              parse(j, j, args1)
            } else args1
          case '"' =>
            val j = skipTillQuote(i + 1)
            if (j > 0) {
              parse(from, j + 1, args)
            } else {
              errorFn("Parameters '" + line + "' with unmatched quote at " + i + ".")
              Nil
            }
          case _ => parse(from, i + 1, args)
        }
      } else { // done
        if (i > from) {
          fetchArg(from, i) :: args
        } else args
      }
    }

    def fetchArg(from: Int, until: Int) = {
      if (line.charAt(from) == '"') {
        line.substring(from + 1, until - 1)
      } else {
        line.substring(from, until)
      }
    }

    def skipTillQuote(i: Int): Int = {
      if (i < line.length) {
        line.charAt(i) match {
          case '"' => i
          case _ => skipTillQuote(i + 1)
        }
      } else -1
    }

    def skipS(i: Int): Int = {
      if (i < line.length) {
        line.charAt(i) match {
          case ' ' => skipS(i + 1)
          case _ => i
        }
      } else -1
    }

    // begin split
    val j = skipS(0)
    if (j >= 0) {
      parse(j, j, Nil).reverse
    } else Nil
  }
}
import PathResolver.{ Defaults, Environment, joincp, splitcp, ppcp }

class PathResolver(settings: Settings) {
  private def cmdLineOrElse(name: String, alt: String) = {
    (commandLineFor(name) match {
      case Some("") => None
      case x        => x
    }) getOrElse alt
  }

  private def commandLineFor(s: String): Option[String] = condOpt(s) {
    case "javabootclasspath"  => settings.javabootclasspath.value
    case "javaextdirs"        => settings.javaextdirs.value
    case "bootclasspath"      => settings.bootclasspath.value
    case "extdirs"            => settings.extdirs.value
    case "classpath" | "cp"   => settings.classpath.value
    case "sourcepath"         => settings.sourcepath.value
    case "Ycodebase"          => settings.Xcodebase.value
  }

  /** Calculated values based on any given command line options, falling back on
   *  those in Defaults.
   */
  object Calculated {
    def javaBootClassPath   = cmdLineOrElse("javabootclasspath", Environment.javaBootClassPath)
    def javaExtDirs         = cmdLineOrElse("javaextdirs", Environment.javaExtDirs)
    def scalaBootClassPath  = cmdLineOrElse("bootclasspath", Defaults.scalaLibPath)
    def scalaExtDirs        = cmdLineOrElse("extdirs", Defaults.scalaExtDirs)
    def sourcePath          = cmdLineOrElse("sourcepath", "")
    def codeBase            = cmdLineOrElse("Ycodebase", "")   // XXX
    def classPath           = cmdLineOrElse("cp", Environment.classPath)

    def referencePath = List(
      // 1. The value of -javabootclasspath if it is set, or the Java bootstrap class path.
      javaBootClassPath,
      // 2. The value of -bootclasspath if it is set,
      //    or the lib/scala-library.jar file of Scala's home if it is available,
      //    or the classes/library folder of Scala's home if it is available.
      scalaBootClassPath,
      // 3. All JAR and ZIP files present in any folder listed by the value of -javaextdirs, if it is set,
      //    or the Java extension class path.
      javaExtDirs,
      // 4. All JAR and ZIP files present in any folder listed by the value of -extdirs, if it is set.
      scalaExtDirs,
      // 5. The first available path below.
      //    * The value of -classpath or -cp.
      //    * The value of the CLASSPATH environment variable.
      //    * The current directory (that is the location of ".").
      //
      // XXX doesn't mention java.class.path
      classPath
    )

    override def toString = """
      |object Calculated {
      |  javaBootClassPath    = %s
      |  javaExtDirs          = %s
      |  scalaBootClassPath   = %s
      |  scalaExtDirs         = %s
      |  sourcePath           = %s
      |  codeBase             = %s
      |  classPath            = %s
      |}""".trim.stripMargin.format(
        ppcp(javaBootClassPath), ppcp(javaExtDirs),
        ppcp(scalaBootClassPath), ppcp(scalaExtDirs),
        ppcp(sourcePath), codeBase, ppcp(classPath)
      )
  }

  def referencePath = joincp(Calculated.referencePath)
}