summaryrefslogtreecommitdiff
path: root/project/Layers.scala
blob: 6c939d0ff79d48479c6bc6c93690f376dd43bcb2 (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
import sbt._
import Keys._
import com.jsuereth.git.GitKeys.gitRunner
import ScalaBuildKeys.lock

/** This trait stores all the helper methods to generate layers in Scala's layered build. */
trait Layers extends Build {
  // TODO - Clean this up or use a self-type.

  /** Default SBT overrides needed for layered compilation. */
  def settingOverrides: Seq[Setting[_]]
  /** Reference to the jline project */
  def jline: Project
  /** Reference to forkjoin library */
  def forkjoin: Project
  /** Reference to the ASM wrapped project. */
  def asm: Project
  /** A setting that adds some external dependencies. */
  def externalDeps: Setting[_]
  /** The root project. */
  def aaa_root: Project

  /** Creates a reference Scala version that can be used to build other projects.   This takes in the raw
    * library, compiler as well as a string representing the layer name (used for compiling the compile-interface).
    */
  def makeScalaReference(layer: String, library: Project, reflect: Project, compiler: Project) =
     scalaInstance <<= (appConfiguration in library,
                        version in library,
                        (exportedProducts in library in Compile),
                        (exportedProducts in reflect in Compile),
                        (exportedProducts in compiler in Compile),
                        (fullClasspath in jline in Runtime),
                        (exportedProducts in asm in Runtime)) map {
    (app, version: String, lib: Classpath, reflect: Classpath, comp: Classpath, jline: Classpath, asm: Classpath) =>
      val launcher = app.provider.scalaProvider.launcher
      (lib,comp) match {
         case (Seq(libraryJar), Seq(compilerJar)) =>
           ScalaInstance(
             version + "-" + layer + "-",
             libraryJar.data,
             compilerJar.data,
             launcher,
             ((jline.files ++ asm.files ++ reflect.files):_*))
         case _ => error("Cannot build a ScalaReference with more than one classpath element")
      }
  }
  
  /** Creates a "layer" of Scala compilation.  That is, this will build the next version of Scala from a previous version.
   * Returns the library project and compiler project from the next layer.
   * Note:  The library and compiler are not *complete* in the sense that they are missing things like "actors".
   */
  def makeLayer(layer: String, referenceScala: Setting[Task[ScalaInstance]], autoLock: Boolean = false) : (Project, Project, Project) = {
    val autoLockSettings: Seq[Setting[_]] = 
      if(autoLock) Seq(compile in Compile <<= (compile in Compile, lock) apply { (c, l) => 
        c flatMapR { cResult =>
          val result = Result.tryValue(cResult)
          l mapR { tx => result }
        }
      }) 
      else Seq.empty


    val library = Project(layer + "-library", file("."))  settings(settingOverrides: _*) settings(autoLockSettings:_*) settings(
      version := layer,
      // TODO - use depends on.
      unmanagedClasspath in Compile <<= (exportedProducts in forkjoin in Compile).identity,
      managedClasspath in Compile := Seq(),
      scalaSource in Compile <<= (baseDirectory) apply (_ / "src" / "library"),
      resourceDirectory in Compile <<= baseDirectory apply (_ / "src" / "library"),   
      defaultExcludes in unmanagedResources := ("*.scala" | "*.java" | "*.disabled"),
      // TODO - Allow other scalac option settings.
      scalacOptions in Compile <++= (scalaSource in Compile) map (src => Seq("-sourcepath", src.getAbsolutePath)),
      resourceGenerators in Compile <+= (resourceManaged, Versions.scalaVersions, skip in Compile, streams) map Versions.generateVersionPropertiesFile("library.properties"),
      referenceScala
    )

    // Define the reflection
    val reflect = Project(layer + "-reflect", file(".")) settings(settingOverrides:_*) settings(autoLockSettings:_*) settings(
      version := layer,
      scalaSource in Compile <<= (baseDirectory) apply (_ / "src" / "reflect"),
      resourceDirectory in Compile <<= baseDirectory apply (_ / "src" / "reflect"),
      defaultExcludes := ("tests"),
      defaultExcludes in unmanagedResources := "*.scala",
      resourceGenerators in Compile <+= (resourceManaged, Versions.scalaVersions, skip in Compile, streams) map Versions.generateVersionPropertiesFile("reflect.properties"),
      // TODO - Use depends on *and* SBT's magic dependency mechanisms...
      unmanagedClasspath in Compile <<= Seq(forkjoin, library).map(exportedProducts in Compile in _).join.map(_.flatten),
      externalDeps,
      referenceScala
    )

    // Define the compiler
    val compiler = Project(layer + "-compiler", file(".")) settings(settingOverrides:_*) settings(autoLockSettings:_*) settings(
      version := layer,
      scalaSource in Compile <<= (baseDirectory) apply (_ / "src" / "compiler"),
      resourceDirectory in Compile <<= baseDirectory apply (_ / "src" / "compiler"),
      defaultExcludes := ("tests"),
      defaultExcludes in unmanagedResources := "*.scala",
      resourceGenerators in Compile <+= (resourceManaged, Versions.scalaVersions, skip in Compile, streams) map Versions.generateVersionPropertiesFile("compiler.properties"),
      // Note, we might be able to use the default task, but for some reason ant was filtering files out.  Not sure what's up, but we'll
      // stick with that for now.
      unmanagedResources in Compile <<= (baseDirectory) map {
        (bd) =>
          val dirs = Seq(bd / "src" / "compiler")
          dirs.descendentsExcept( ("*.xml" | "*.html" | "*.gif" | "*.png" | "*.js" | "*.css" | "*.tmpl" | "*.swf" | "*.properties" | "*.txt"),"*.scala").get
      },
      // TODO - Use depends on *and* SBT's magic dependency mechanisms...
      unmanagedClasspath in Compile <<= Seq(forkjoin, library, reflect, jline, asm).map(exportedProducts in Compile in _).join.map(_.flatten),
      externalDeps,
      referenceScala
    )

    // Return the generated projects.
    (library, reflect, compiler)
  }

}