src/Pure/System/build.scala
author wenzelm
Fri Jul 20 11:52:20 2012 +0200 (2012-07-20)
changeset 48361 63bdba7c1366
parent 48354 aa1e730c3fdd
child 48363 cf081b7042d2
permissions -rw-r--r--
tune;
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
  private case class Session_Entry(
wenzelm@48337
    87
    name: String,
wenzelm@48337
    88
    reset: Boolean,
wenzelm@48349
    89
    order: Int,
wenzelm@48337
    90
    path: Option[String],
wenzelm@48347
    91
    parent: Option[String],
wenzelm@48337
    92
    description: String,
wenzelm@48337
    93
    options: Options,
wenzelm@48337
    94
    theories: List[(Options, List[String])],
wenzelm@48337
    95
    files: List[String])
wenzelm@48337
    96
wenzelm@48334
    97
  private object Parser extends Parse.Parser
wenzelm@48334
    98
  {
wenzelm@48334
    99
    val SESSION = "session"
wenzelm@48334
   100
    val IN = "in"
wenzelm@48334
   101
    val DESCRIPTION = "description"
wenzelm@48334
   102
    val OPTIONS = "options"
wenzelm@48334
   103
    val THEORIES = "theories"
wenzelm@48334
   104
    val FILES = "files"
wenzelm@48334
   105
wenzelm@48334
   106
    val syntax =
wenzelm@48336
   107
      Outer_Syntax.empty + "!" + "(" + ")" + "+" + "," + "=" + "[" + "]" +
wenzelm@48336
   108
        SESSION + IN + DESCRIPTION + OPTIONS + THEORIES + FILES
wenzelm@48334
   109
wenzelm@48337
   110
    val session_entry: Parser[Session_Entry] =
wenzelm@48334
   111
    {
wenzelm@48334
   112
      val session_name = atom("session name", _.is_name)
wenzelm@48334
   113
      val theory_name = atom("theory name", _.is_name)
wenzelm@48280
   114
wenzelm@48334
   115
      val option =
wenzelm@48334
   116
        name ~ opt(keyword("=") ~! name ^^ { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
wenzelm@48337
   117
      val options = keyword("[") ~> repsep(option, keyword(",")) <~ keyword("]")
wenzelm@48334
   118
wenzelm@48334
   119
      val theories =
wenzelm@48334
   120
        keyword(THEORIES) ~! ((options | success(Nil)) ~ rep1(theory_name)) ^^
wenzelm@48334
   121
          { case _ ~ (x ~ y) => (x, y) }
wenzelm@48334
   122
wenzelm@48334
   123
      ((keyword(SESSION) ~! session_name) ^^ { case _ ~ x => x }) ~
wenzelm@48336
   124
        (keyword("!") ^^^ true | success(false)) ~
wenzelm@48349
   125
        (keyword("(") ~! (nat <~ keyword(")")) ^^ { case _ ~ x => x } | success(Integer.MAX_VALUE)) ~
wenzelm@48337
   126
        (opt(keyword(IN) ~! string ^^ { case _ ~ x => x })) ~
wenzelm@48347
   127
        (keyword("=") ~> opt(session_name <~ keyword("+"))) ~
wenzelm@48334
   128
        (keyword(DESCRIPTION) ~! text ^^ { case _ ~ x => x } | success("")) ~
wenzelm@48334
   129
        (keyword(OPTIONS) ~! options ^^ { case _ ~ x => x } | success(Nil)) ~
wenzelm@48347
   130
        rep(theories) ~
wenzelm@48336
   131
        (keyword(FILES) ~! rep1(path) ^^ { case _ ~ x => x } | success(Nil)) ^^
wenzelm@48349
   132
          { case a ~ b ~ c ~ d ~ e ~ f ~ g ~ h ~ i => Session_Entry(a, b, c, d, e, f, g, h, i) }
wenzelm@48334
   133
    }
wenzelm@48334
   134
wenzelm@48337
   135
    def parse_entries(root: File): List[Session_Entry] =
wenzelm@48334
   136
    {
wenzelm@48335
   137
      val toks = syntax.scan(Standard_System.read_file(root))
wenzelm@48337
   138
      parse_all(rep(session_entry), Token.reader(toks, root.toString)) match {
wenzelm@48334
   139
        case Success(result, _) => result
wenzelm@48334
   140
        case bad => error(bad.toString)
wenzelm@48334
   141
      }
wenzelm@48334
   142
    }
wenzelm@48334
   143
  }
wenzelm@48280
   144
wenzelm@48337
   145
wenzelm@48341
   146
  /* find sessions */
wenzelm@48337
   147
wenzelm@48361
   148
  private val ROOT = Path.explode("ROOT")
wenzelm@48361
   149
  private val SESSIONS = Path.explode("etc/sessions")
wenzelm@48361
   150
wenzelm@48361
   151
  private def sessions_root(dir: Path, root: File, queue: Session.Queue): Session.Queue =
wenzelm@48280
   152
  {
wenzelm@48361
   153
    (queue /: Parser.parse_entries(root))((queue1, entry) =>
wenzelm@48337
   154
      try {
wenzelm@48347
   155
        if (entry.name == "") error("Bad session name")
wenzelm@48347
   156
wenzelm@48337
   157
        val full_name =
wenzelm@48347
   158
          if (entry.name == "RAW" || entry.name == "Pure") {
wenzelm@48347
   159
            if (entry.parent.isDefined) error("Illegal parent session")
wenzelm@48347
   160
            else entry.name
wenzelm@48347
   161
          }
wenzelm@48347
   162
          else
wenzelm@48347
   163
            entry.parent match {
wenzelm@48361
   164
              case Some(parent_name) if queue1.defined(parent_name) =>
wenzelm@48347
   165
                if (entry.reset) entry.name
wenzelm@48351
   166
                else parent_name + "-" + entry.name
wenzelm@48351
   167
              case _ => error("Bad parent session")
wenzelm@48347
   168
            }
wenzelm@48339
   169
wenzelm@48337
   170
        val path =
wenzelm@48337
   171
          entry.path match {
wenzelm@48337
   172
            case Some(p) => Path.explode(p)
wenzelm@48337
   173
            case None => Path.basic(entry.name)
wenzelm@48337
   174
          }
wenzelm@48339
   175
wenzelm@48351
   176
        val key = Session.Key(full_name, entry.order)
wenzelm@48351
   177
        val info = Session.Info(dir + path, entry.description, entry.options,
wenzelm@48351
   178
          entry.theories.map({ case (x, ys) => ys.map(y => (x, y)) }).flatten, entry.files)
wenzelm@48351
   179
wenzelm@48361
   180
        queue1 + (key, info, entry.parent)
wenzelm@48337
   181
      }
wenzelm@48337
   182
      catch {
wenzelm@48337
   183
        case ERROR(msg) =>
wenzelm@48337
   184
          error(msg + "\nThe error(s) above occurred in session entry " +
wenzelm@48337
   185
            quote(entry.name) + " (file " + quote(root.toString) + ")")
wenzelm@48352
   186
      })
wenzelm@48352
   187
  }
wenzelm@48352
   188
wenzelm@48361
   189
  private def sessions_dir(strict: Boolean, dir: Path, queue: Session.Queue): Session.Queue =
wenzelm@48352
   190
  {
wenzelm@48361
   191
    val root = Isabelle_System.platform_file(dir + ROOT)
wenzelm@48361
   192
    if (root.isFile) sessions_root(dir, root, queue)
wenzelm@48352
   193
    else if (strict) error("Bad session root file: " + quote(root.toString))
wenzelm@48361
   194
    else queue
wenzelm@48352
   195
  }
wenzelm@48352
   196
wenzelm@48361
   197
  private def sessions_catalog(dir: Path, catalog: File, queue: Session.Queue): Session.Queue =
wenzelm@48352
   198
  {
wenzelm@48354
   199
    val dirs =
wenzelm@48354
   200
      split_lines(Standard_System.read_file(catalog)).
wenzelm@48354
   201
        filterNot(line => line == "" || line.startsWith("#"))
wenzelm@48361
   202
    (queue /: dirs)((queue1, dir1) =>
wenzelm@48352
   203
      try {
wenzelm@48352
   204
        val dir2 = dir + Path.explode(dir1)
wenzelm@48361
   205
        if (Isabelle_System.platform_file(dir2).isDirectory) sessions_dir(true, dir2, queue1)
wenzelm@48352
   206
        else error("Bad session directory: " + dir2.toString)
wenzelm@48337
   207
      }
wenzelm@48352
   208
      catch {
wenzelm@48352
   209
        case ERROR(msg) =>
wenzelm@48352
   210
          error(msg + "\nThe error(s) above occurred in session catalog " + quote(catalog.toString))
wenzelm@48352
   211
      })
wenzelm@48352
   212
  }
wenzelm@48352
   213
wenzelm@48352
   214
  def find_sessions(more_dirs: List[Path]): Session.Queue =
wenzelm@48352
   215
  {
wenzelm@48361
   216
    var queue = Session.Queue.empty
wenzelm@48352
   217
wenzelm@48352
   218
    for (dir <- Isabelle_System.components()) {
wenzelm@48361
   219
      queue = sessions_dir(false, dir, queue)
wenzelm@48352
   220
wenzelm@48361
   221
      val catalog = Isabelle_System.platform_file(dir + SESSIONS)
wenzelm@48352
   222
      if (catalog.isFile)
wenzelm@48361
   223
        queue = sessions_catalog(dir, catalog, queue)
wenzelm@48337
   224
    }
wenzelm@48352
   225
wenzelm@48361
   226
    for (dir <- more_dirs) queue = sessions_dir(true, dir, queue)
wenzelm@48352
   227
wenzelm@48361
   228
    queue
wenzelm@48280
   229
  }
wenzelm@48341
   230
wenzelm@48341
   231
wenzelm@48341
   232
wenzelm@48341
   233
  /** build **/
wenzelm@48341
   234
wenzelm@48341
   235
  def build(all_sessions: Boolean, build_images: Boolean, list_only: Boolean,
wenzelm@48341
   236
    more_dirs: List[Path], options: List[String], sessions: List[String]): Int =
wenzelm@48341
   237
  {
wenzelm@48341
   238
    println("more_dirs = " + more_dirs.toString)
wenzelm@48341
   239
    println("options = " + options.toString)
wenzelm@48341
   240
    println("sessions = " + sessions.toString)
wenzelm@48341
   241
wenzelm@48351
   242
    for ((key, info) <- find_sessions(more_dirs).topological_order)
wenzelm@48351
   243
      println(key.name + " in " + info.dir)
wenzelm@48341
   244
wenzelm@48341
   245
    0
wenzelm@48341
   246
  }
wenzelm@48341
   247
wenzelm@48341
   248
wenzelm@48346
   249
  /* command line entry point */
wenzelm@48341
   250
wenzelm@48341
   251
  def main(args: Array[String])
wenzelm@48341
   252
  {
wenzelm@48346
   253
    Command_Line.tool {
wenzelm@48346
   254
      args.toList match {
wenzelm@48346
   255
        case
wenzelm@48346
   256
          Properties.Value.Boolean(all_sessions) ::
wenzelm@48346
   257
          Properties.Value.Boolean(build_images) ::
wenzelm@48346
   258
          Properties.Value.Boolean(list_only) ::
wenzelm@48346
   259
          Command_Line.Chunks(more_dirs, options, sessions) =>
wenzelm@48346
   260
            build(all_sessions, build_images, list_only,
wenzelm@48346
   261
              more_dirs.map(Path.explode), options, sessions)
wenzelm@48346
   262
        case _ => error("Bad arguments:\n" + cat_lines(args))
wenzelm@48341
   263
      }
wenzelm@48346
   264
    }
wenzelm@48341
   265
  }
wenzelm@48276
   266
}
wenzelm@48276
   267