src/Pure/System/build.scala
author wenzelm
Thu Jul 19 16:09:48 2012 +0200 (2012-07-19)
changeset 48351 a0b95a762abb
parent 48350 09bf3b73e446
child 48352 7fbf98ee265f
permissions -rw-r--r--
less redundant data structures;
wenzelm@48276
     1
/*  Title:      Pure/System/build.scala
wenzelm@48276
     2
    Author:     Makarius
wenzelm@48276
     3
wenzelm@48276
     4
Build and manage Isabelle sessions.
wenzelm@48276
     5
*/
wenzelm@48276
     6
wenzelm@48276
     7
package isabelle
wenzelm@48276
     8
wenzelm@48276
     9
wenzelm@48335
    10
import java.io.File
wenzelm@48335
    11
wenzelm@48337
    12
import scala.collection.mutable
wenzelm@48340
    13
import scala.annotation.tailrec
wenzelm@48337
    14
wenzelm@48335
    15
wenzelm@48276
    16
object Build
wenzelm@48276
    17
{
wenzelm@48337
    18
  /** session information **/
wenzelm@48334
    19
wenzelm@48334
    20
  type Options = List[(String, Option[String])]
wenzelm@48334
    21
wenzelm@48349
    22
  object Session
wenzelm@48349
    23
  {
wenzelm@48349
    24
    object Key
wenzelm@48349
    25
    {
wenzelm@48349
    26
      object Ordering extends scala.math.Ordering[Key]
wenzelm@48349
    27
      {
wenzelm@48349
    28
        def compare(key1: Key, key2: Key): Int =
wenzelm@48350
    29
          key1.order compare key2.order match {
wenzelm@48349
    30
            case 0 => key1.name compare key2.name
wenzelm@48349
    31
            case ord => ord
wenzelm@48349
    32
          }
wenzelm@48349
    33
      }
wenzelm@48349
    34
    }
wenzelm@48349
    35
wenzelm@48349
    36
    sealed case class Key(name: String, order: Int)
wenzelm@48349
    37
    {
wenzelm@48349
    38
      override def toString: String = name
wenzelm@48349
    39
    }
wenzelm@48349
    40
wenzelm@48349
    41
    sealed case class Info(
wenzelm@48349
    42
      dir: Path,
wenzelm@48349
    43
      description: String,
wenzelm@48349
    44
      options: Options,
wenzelm@48349
    45
      theories: List[(Options, String)],
wenzelm@48349
    46
      files: List[String])
wenzelm@48349
    47
wenzelm@48349
    48
    object Queue
wenzelm@48349
    49
    {
wenzelm@48349
    50
      val empty: Queue = new Queue()
wenzelm@48349
    51
    }
wenzelm@48349
    52
wenzelm@48349
    53
    final class Queue private(
wenzelm@48349
    54
      keys: Map[String, Key] = Map.empty,
wenzelm@48349
    55
      graph: Graph[Key, Info] = Graph.empty(Key.Ordering))
wenzelm@48349
    56
    {
wenzelm@48351
    57
      def defined(name: String): Boolean = keys.isDefinedAt(name)
wenzelm@48349
    58
wenzelm@48351
    59
      def + (key: Key, info: Info, parent: Option[String]): Queue =
wenzelm@48349
    60
      {
wenzelm@48349
    61
        val keys1 =
wenzelm@48351
    62
          if (defined(key.name)) error("Duplicate session: " + quote(key.name))
wenzelm@48351
    63
          else keys + (key.name -> key)
wenzelm@48349
    64
wenzelm@48349
    65
        val graph1 =
wenzelm@48349
    66
          try {
wenzelm@48351
    67
            graph.new_node(key, info).add_deps_acyclic(key, parent.toList.map(keys(_)))
wenzelm@48349
    68
          }
wenzelm@48349
    69
          catch {
wenzelm@48349
    70
            case exn: Graph.Cycles[_] =>
wenzelm@48349
    71
              error(cat_lines(exn.cycles.map(cycle =>
wenzelm@48349
    72
                "Cyclic session dependency of " +
wenzelm@48349
    73
                  cycle.map(key => quote(key.toString)).mkString(" via "))))
wenzelm@48349
    74
          }
wenzelm@48349
    75
        new Queue(keys1, graph1)
wenzelm@48349
    76
      }
wenzelm@48349
    77
wenzelm@48351
    78
      def topological_order: List[(Key, Info)] =
wenzelm@48351
    79
        graph.topological_order.map(key => (key, graph.get_node(key)))
wenzelm@48349
    80
    }
wenzelm@48349
    81
  }
wenzelm@48334
    82
wenzelm@48337
    83
wenzelm@48337
    84
  /* parsing */
wenzelm@48337
    85
wenzelm@48337
    86
  val ROOT_NAME = "ROOT"
wenzelm@48337
    87
wenzelm@48337
    88
  private case class Session_Entry(
wenzelm@48337
    89
    name: String,
wenzelm@48337
    90
    reset: Boolean,
wenzelm@48349
    91
    order: Int,
wenzelm@48337
    92
    path: Option[String],
wenzelm@48347
    93
    parent: Option[String],
wenzelm@48337
    94
    description: String,
wenzelm@48337
    95
    options: Options,
wenzelm@48337
    96
    theories: List[(Options, List[String])],
wenzelm@48337
    97
    files: List[String])
wenzelm@48337
    98
wenzelm@48334
    99
  private object Parser extends Parse.Parser
wenzelm@48334
   100
  {
wenzelm@48334
   101
    val SESSION = "session"
wenzelm@48334
   102
    val IN = "in"
wenzelm@48334
   103
    val DESCRIPTION = "description"
wenzelm@48334
   104
    val OPTIONS = "options"
wenzelm@48334
   105
    val THEORIES = "theories"
wenzelm@48334
   106
    val FILES = "files"
wenzelm@48334
   107
wenzelm@48334
   108
    val syntax =
wenzelm@48336
   109
      Outer_Syntax.empty + "!" + "(" + ")" + "+" + "," + "=" + "[" + "]" +
wenzelm@48336
   110
        SESSION + IN + DESCRIPTION + OPTIONS + THEORIES + FILES
wenzelm@48334
   111
wenzelm@48337
   112
    val session_entry: Parser[Session_Entry] =
wenzelm@48334
   113
    {
wenzelm@48334
   114
      val session_name = atom("session name", _.is_name)
wenzelm@48334
   115
      val theory_name = atom("theory name", _.is_name)
wenzelm@48280
   116
wenzelm@48334
   117
      val option =
wenzelm@48334
   118
        name ~ opt(keyword("=") ~! name ^^ { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
wenzelm@48337
   119
      val options = keyword("[") ~> repsep(option, keyword(",")) <~ keyword("]")
wenzelm@48334
   120
wenzelm@48334
   121
      val theories =
wenzelm@48334
   122
        keyword(THEORIES) ~! ((options | success(Nil)) ~ rep1(theory_name)) ^^
wenzelm@48334
   123
          { case _ ~ (x ~ y) => (x, y) }
wenzelm@48334
   124
wenzelm@48334
   125
      ((keyword(SESSION) ~! session_name) ^^ { case _ ~ x => x }) ~
wenzelm@48336
   126
        (keyword("!") ^^^ true | success(false)) ~
wenzelm@48349
   127
        (keyword("(") ~! (nat <~ keyword(")")) ^^ { case _ ~ x => x } | success(Integer.MAX_VALUE)) ~
wenzelm@48337
   128
        (opt(keyword(IN) ~! string ^^ { case _ ~ x => x })) ~
wenzelm@48347
   129
        (keyword("=") ~> opt(session_name <~ keyword("+"))) ~
wenzelm@48334
   130
        (keyword(DESCRIPTION) ~! text ^^ { case _ ~ x => x } | success("")) ~
wenzelm@48334
   131
        (keyword(OPTIONS) ~! options ^^ { case _ ~ x => x } | success(Nil)) ~
wenzelm@48347
   132
        rep(theories) ~
wenzelm@48336
   133
        (keyword(FILES) ~! rep1(path) ^^ { case _ ~ x => x } | success(Nil)) ^^
wenzelm@48349
   134
          { case a ~ b ~ c ~ d ~ e ~ f ~ g ~ h ~ i => Session_Entry(a, b, c, d, e, f, g, h, i) }
wenzelm@48334
   135
    }
wenzelm@48334
   136
wenzelm@48337
   137
    def parse_entries(root: File): List[Session_Entry] =
wenzelm@48334
   138
    {
wenzelm@48335
   139
      val toks = syntax.scan(Standard_System.read_file(root))
wenzelm@48337
   140
      parse_all(rep(session_entry), Token.reader(toks, root.toString)) match {
wenzelm@48334
   141
        case Success(result, _) => result
wenzelm@48334
   142
        case bad => error(bad.toString)
wenzelm@48334
   143
      }
wenzelm@48334
   144
    }
wenzelm@48334
   145
  }
wenzelm@48280
   146
wenzelm@48337
   147
wenzelm@48341
   148
  /* find sessions */
wenzelm@48337
   149
wenzelm@48349
   150
  def find_sessions(more_dirs: List[Path]): Session.Queue =
wenzelm@48280
   151
  {
wenzelm@48349
   152
    var sessions = Session.Queue.empty
wenzelm@48337
   153
wenzelm@48280
   154
    for {
wenzelm@48340
   155
      (dir, strict) <- Isabelle_System.components().map((_, false)) ++ more_dirs.map((_, true))
wenzelm@48280
   156
      root = Isabelle_System.platform_file(dir + Path.basic(ROOT_NAME))
wenzelm@48340
   157
      _ = (strict && !root.isFile && error("Bad session root file: " + quote(root.toString)))
wenzelm@48280
   158
      if root.isFile
wenzelm@48337
   159
      entry <- Parser.parse_entries(root)
wenzelm@48337
   160
    }
wenzelm@48337
   161
    {
wenzelm@48337
   162
      try {
wenzelm@48347
   163
        if (entry.name == "") error("Bad session name")
wenzelm@48347
   164
wenzelm@48337
   165
        val full_name =
wenzelm@48347
   166
          if (entry.name == "RAW" || entry.name == "Pure") {
wenzelm@48347
   167
            if (entry.parent.isDefined) error("Illegal parent session")
wenzelm@48347
   168
            else entry.name
wenzelm@48347
   169
          }
wenzelm@48347
   170
          else
wenzelm@48347
   171
            entry.parent match {
wenzelm@48351
   172
              case Some(parent_name) if sessions.defined(parent_name) =>
wenzelm@48347
   173
                if (entry.reset) entry.name
wenzelm@48351
   174
                else parent_name + "-" + entry.name
wenzelm@48351
   175
              case _ => error("Bad parent session")
wenzelm@48347
   176
            }
wenzelm@48339
   177
wenzelm@48337
   178
        val path =
wenzelm@48337
   179
          entry.path match {
wenzelm@48337
   180
            case Some(p) => Path.explode(p)
wenzelm@48337
   181
            case None => Path.basic(entry.name)
wenzelm@48337
   182
          }
wenzelm@48339
   183
wenzelm@48351
   184
        val key = Session.Key(full_name, entry.order)
wenzelm@48351
   185
        val info = Session.Info(dir + path, entry.description, entry.options,
wenzelm@48351
   186
          entry.theories.map({ case (x, ys) => ys.map(y => (x, y)) }).flatten, entry.files)
wenzelm@48351
   187
wenzelm@48351
   188
        sessions += (key, info, entry.parent)
wenzelm@48337
   189
      }
wenzelm@48337
   190
      catch {
wenzelm@48337
   191
        case ERROR(msg) =>
wenzelm@48337
   192
          error(msg + "\nThe error(s) above occurred in session entry " +
wenzelm@48337
   193
            quote(entry.name) + " (file " + quote(root.toString) + ")")
wenzelm@48337
   194
      }
wenzelm@48337
   195
    }
wenzelm@48349
   196
    sessions
wenzelm@48280
   197
  }
wenzelm@48341
   198
wenzelm@48341
   199
wenzelm@48341
   200
wenzelm@48341
   201
  /** build **/
wenzelm@48341
   202
wenzelm@48341
   203
  def build(all_sessions: Boolean, build_images: Boolean, list_only: Boolean,
wenzelm@48341
   204
    more_dirs: List[Path], options: List[String], sessions: List[String]): Int =
wenzelm@48341
   205
  {
wenzelm@48341
   206
    println("more_dirs = " + more_dirs.toString)
wenzelm@48341
   207
    println("options = " + options.toString)
wenzelm@48341
   208
    println("sessions = " + sessions.toString)
wenzelm@48341
   209
wenzelm@48351
   210
    for ((key, info) <- find_sessions(more_dirs).topological_order)
wenzelm@48351
   211
      println(key.name + " in " + info.dir)
wenzelm@48341
   212
wenzelm@48341
   213
    0
wenzelm@48341
   214
  }
wenzelm@48341
   215
wenzelm@48341
   216
wenzelm@48346
   217
  /* command line entry point */
wenzelm@48341
   218
wenzelm@48341
   219
  def main(args: Array[String])
wenzelm@48341
   220
  {
wenzelm@48346
   221
    Command_Line.tool {
wenzelm@48346
   222
      args.toList match {
wenzelm@48346
   223
        case
wenzelm@48346
   224
          Properties.Value.Boolean(all_sessions) ::
wenzelm@48346
   225
          Properties.Value.Boolean(build_images) ::
wenzelm@48346
   226
          Properties.Value.Boolean(list_only) ::
wenzelm@48346
   227
          Command_Line.Chunks(more_dirs, options, sessions) =>
wenzelm@48346
   228
            build(all_sessions, build_images, list_only,
wenzelm@48346
   229
              more_dirs.map(Path.explode), options, sessions)
wenzelm@48346
   230
        case _ => error("Bad arguments:\n" + cat_lines(args))
wenzelm@48341
   231
      }
wenzelm@48346
   232
    }
wenzelm@48341
   233
  }
wenzelm@48276
   234
}
wenzelm@48276
   235