src/Pure/System/build.scala
author wenzelm
Thu Jul 19 19:12:58 2012 +0200 (2012-07-19)
changeset 48352 7fbf98ee265f
parent 48351 a0b95a762abb
child 48354 aa1e730c3fdd
permissions -rw-r--r--
include COMPONENT/etc/sessions as catalog for more directories, for improved scalability with hundreds of entries (notably AFP);
misc tuning and simplification;
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@48352
   148
  private def sessions_root(dir: Path, root: File, sessions: Session.Queue): Session.Queue =
wenzelm@48280
   149
  {
wenzelm@48352
   150
    (sessions /: Parser.parse_entries(root))((sessions1, entry) =>
wenzelm@48337
   151
      try {
wenzelm@48347
   152
        if (entry.name == "") error("Bad session name")
wenzelm@48347
   153
wenzelm@48337
   154
        val full_name =
wenzelm@48347
   155
          if (entry.name == "RAW" || entry.name == "Pure") {
wenzelm@48347
   156
            if (entry.parent.isDefined) error("Illegal parent session")
wenzelm@48347
   157
            else entry.name
wenzelm@48347
   158
          }
wenzelm@48347
   159
          else
wenzelm@48347
   160
            entry.parent match {
wenzelm@48352
   161
              case Some(parent_name) if sessions1.defined(parent_name) =>
wenzelm@48347
   162
                if (entry.reset) entry.name
wenzelm@48351
   163
                else parent_name + "-" + entry.name
wenzelm@48351
   164
              case _ => error("Bad parent session")
wenzelm@48347
   165
            }
wenzelm@48339
   166
wenzelm@48337
   167
        val path =
wenzelm@48337
   168
          entry.path match {
wenzelm@48337
   169
            case Some(p) => Path.explode(p)
wenzelm@48337
   170
            case None => Path.basic(entry.name)
wenzelm@48337
   171
          }
wenzelm@48339
   172
wenzelm@48351
   173
        val key = Session.Key(full_name, entry.order)
wenzelm@48351
   174
        val info = Session.Info(dir + path, entry.description, entry.options,
wenzelm@48351
   175
          entry.theories.map({ case (x, ys) => ys.map(y => (x, y)) }).flatten, entry.files)
wenzelm@48351
   176
wenzelm@48352
   177
        sessions1 + (key, info, entry.parent)
wenzelm@48337
   178
      }
wenzelm@48337
   179
      catch {
wenzelm@48337
   180
        case ERROR(msg) =>
wenzelm@48337
   181
          error(msg + "\nThe error(s) above occurred in session entry " +
wenzelm@48337
   182
            quote(entry.name) + " (file " + quote(root.toString) + ")")
wenzelm@48352
   183
      })
wenzelm@48352
   184
  }
wenzelm@48352
   185
wenzelm@48352
   186
  private def sessions_dir(strict: Boolean, dir: Path, sessions: Session.Queue): Session.Queue =
wenzelm@48352
   187
  {
wenzelm@48352
   188
    val root = Isabelle_System.platform_file(dir + Path.basic("ROOT"))
wenzelm@48352
   189
    if (root.isFile) sessions_root(dir, root, sessions)
wenzelm@48352
   190
    else if (strict) error("Bad session root file: " + quote(root.toString))
wenzelm@48352
   191
    else sessions
wenzelm@48352
   192
  }
wenzelm@48352
   193
wenzelm@48352
   194
  private def sessions_catalog(dir: Path, catalog: File, sessions: Session.Queue): Session.Queue =
wenzelm@48352
   195
  {
wenzelm@48352
   196
    val dirs = split_lines(Standard_System.read_file(catalog)).filter(_ != "")
wenzelm@48352
   197
    (sessions /: dirs)((sessions1, dir1) =>
wenzelm@48352
   198
      try {
wenzelm@48352
   199
        val dir2 = dir + Path.explode(dir1)
wenzelm@48352
   200
        if (Isabelle_System.platform_file(dir2).isDirectory) sessions_dir(true, dir2, sessions1)
wenzelm@48352
   201
        else error("Bad session directory: " + dir2.toString)
wenzelm@48337
   202
      }
wenzelm@48352
   203
      catch {
wenzelm@48352
   204
        case ERROR(msg) =>
wenzelm@48352
   205
          error(msg + "\nThe error(s) above occurred in session catalog " + quote(catalog.toString))
wenzelm@48352
   206
      })
wenzelm@48352
   207
  }
wenzelm@48352
   208
wenzelm@48352
   209
  def find_sessions(more_dirs: List[Path]): Session.Queue =
wenzelm@48352
   210
  {
wenzelm@48352
   211
    var sessions = Session.Queue.empty
wenzelm@48352
   212
wenzelm@48352
   213
    for (dir <- Isabelle_System.components()) {
wenzelm@48352
   214
      sessions = sessions_dir(false, dir, sessions)
wenzelm@48352
   215
wenzelm@48352
   216
      val catalog = Isabelle_System.platform_file(dir + Path.explode("etc/sessions"))
wenzelm@48352
   217
      if (catalog.isFile)
wenzelm@48352
   218
        sessions = sessions_catalog(dir, catalog, sessions)
wenzelm@48337
   219
    }
wenzelm@48352
   220
wenzelm@48352
   221
    for (dir <- more_dirs) sessions = sessions_dir(true, dir, sessions)
wenzelm@48352
   222
wenzelm@48349
   223
    sessions
wenzelm@48280
   224
  }
wenzelm@48341
   225
wenzelm@48341
   226
wenzelm@48341
   227
wenzelm@48341
   228
  /** build **/
wenzelm@48341
   229
wenzelm@48341
   230
  def build(all_sessions: Boolean, build_images: Boolean, list_only: Boolean,
wenzelm@48341
   231
    more_dirs: List[Path], options: List[String], sessions: List[String]): Int =
wenzelm@48341
   232
  {
wenzelm@48341
   233
    println("more_dirs = " + more_dirs.toString)
wenzelm@48341
   234
    println("options = " + options.toString)
wenzelm@48341
   235
    println("sessions = " + sessions.toString)
wenzelm@48341
   236
wenzelm@48351
   237
    for ((key, info) <- find_sessions(more_dirs).topological_order)
wenzelm@48351
   238
      println(key.name + " in " + info.dir)
wenzelm@48341
   239
wenzelm@48341
   240
    0
wenzelm@48341
   241
  }
wenzelm@48341
   242
wenzelm@48341
   243
wenzelm@48346
   244
  /* command line entry point */
wenzelm@48341
   245
wenzelm@48341
   246
  def main(args: Array[String])
wenzelm@48341
   247
  {
wenzelm@48346
   248
    Command_Line.tool {
wenzelm@48346
   249
      args.toList match {
wenzelm@48346
   250
        case
wenzelm@48346
   251
          Properties.Value.Boolean(all_sessions) ::
wenzelm@48346
   252
          Properties.Value.Boolean(build_images) ::
wenzelm@48346
   253
          Properties.Value.Boolean(list_only) ::
wenzelm@48346
   254
          Command_Line.Chunks(more_dirs, options, sessions) =>
wenzelm@48346
   255
            build(all_sessions, build_images, list_only,
wenzelm@48346
   256
              more_dirs.map(Path.explode), options, sessions)
wenzelm@48346
   257
        case _ => error("Bad arguments:\n" + cat_lines(args))
wenzelm@48341
   258
      }
wenzelm@48346
   259
    }
wenzelm@48341
   260
  }
wenzelm@48276
   261
}
wenzelm@48276
   262