src/Pure/System/build.scala
author wenzelm
Mon Jul 23 15:59:14 2012 +0200 (2012-07-23)
changeset 48447 ef600ce4559c
parent 48425 0d95980e9aae
child 48457 fd9e28d5a143
permissions -rw-r--r--
added system build mode: produce output in ISABELLE_HOME;
determine output location more explicitly;
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@48409
    10
import java.io.{File => JFile}
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@48349
    20
  object Session
wenzelm@48349
    21
  {
wenzelm@48421
    22
    /* Key */
wenzelm@48421
    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@48421
    41
wenzelm@48421
    42
    /* Info */
wenzelm@48421
    43
wenzelm@48349
    44
    sealed case class Info(
wenzelm@48349
    45
      dir: Path,
wenzelm@48418
    46
      parent: Option[String],
wenzelm@48349
    47
      description: String,
wenzelm@48349
    48
      options: Options,
wenzelm@48421
    49
      theories: List[(Options, List[Path])],
wenzelm@48421
    50
      files: List[Path],
wenzelm@48424
    51
      digest: SHA1.Digest)
wenzelm@48421
    52
wenzelm@48421
    53
wenzelm@48421
    54
    /* Queue */
wenzelm@48349
    55
wenzelm@48349
    56
    object Queue
wenzelm@48349
    57
    {
wenzelm@48349
    58
      val empty: Queue = new Queue()
wenzelm@48349
    59
    }
wenzelm@48349
    60
wenzelm@48349
    61
    final class Queue private(
wenzelm@48349
    62
      keys: Map[String, Key] = Map.empty,
wenzelm@48349
    63
      graph: Graph[Key, Info] = Graph.empty(Key.Ordering))
wenzelm@48349
    64
    {
wenzelm@48425
    65
      def is_empty: Boolean = graph.is_empty
wenzelm@48425
    66
wenzelm@48425
    67
      def apply(name: String): Info = graph.get_node(keys(name))
wenzelm@48351
    68
      def defined(name: String): Boolean = keys.isDefinedAt(name)
wenzelm@48419
    69
      def is_inner(name: String): Boolean = !graph.is_maximal(keys(name))
wenzelm@48419
    70
wenzelm@48418
    71
      def + (key: Key, info: Info): Queue =
wenzelm@48349
    72
      {
wenzelm@48349
    73
        val keys1 =
wenzelm@48351
    74
          if (defined(key.name)) error("Duplicate session: " + quote(key.name))
wenzelm@48351
    75
          else keys + (key.name -> key)
wenzelm@48349
    76
wenzelm@48349
    77
        val graph1 =
wenzelm@48349
    78
          try {
wenzelm@48418
    79
            graph.new_node(key, info).add_deps_acyclic(key, info.parent.toList.map(keys(_)))
wenzelm@48349
    80
          }
wenzelm@48349
    81
          catch {
wenzelm@48349
    82
            case exn: Graph.Cycles[_] =>
wenzelm@48349
    83
              error(cat_lines(exn.cycles.map(cycle =>
wenzelm@48349
    84
                "Cyclic session dependency of " +
wenzelm@48349
    85
                  cycle.map(key => quote(key.toString)).mkString(" via "))))
wenzelm@48349
    86
          }
wenzelm@48349
    87
        new Queue(keys1, graph1)
wenzelm@48349
    88
      }
wenzelm@48349
    89
wenzelm@48425
    90
      def - (name: String): Queue = new Queue(keys - name, graph.del_node(keys(name)))
wenzelm@48425
    91
wenzelm@48363
    92
      def required(names: List[String]): Queue =
wenzelm@48363
    93
      {
wenzelm@48363
    94
        val req = graph.all_preds(names.map(keys(_))).map(_.name).toSet
wenzelm@48363
    95
        val keys1 = keys -- keys.keySet.filter(name => !req(name))
wenzelm@48363
    96
        val graph1 = graph.restrict(key => keys1.isDefinedAt(key.name))
wenzelm@48363
    97
        new Queue(keys1, graph1)
wenzelm@48363
    98
      }
wenzelm@48363
    99
wenzelm@48425
   100
      def dequeue(skip: String => Boolean): Option[(String, Info)] =
wenzelm@48425
   101
      {
wenzelm@48425
   102
        val it = graph.entries.dropWhile(
wenzelm@48425
   103
          { case (key, (_, (deps, _))) => !deps.isEmpty || skip(key.name) })
wenzelm@48425
   104
        if (it.hasNext) { val (key, (info, _)) = it.next; Some((key.name, info)) }
wenzelm@48425
   105
        else None
wenzelm@48425
   106
      }
wenzelm@48425
   107
wenzelm@48422
   108
      def topological_order: List[(String, Info)] =
wenzelm@48422
   109
        graph.topological_order.map(key => (key.name, graph.get_node(key)))
wenzelm@48349
   110
    }
wenzelm@48349
   111
  }
wenzelm@48334
   112
wenzelm@48337
   113
wenzelm@48337
   114
  /* parsing */
wenzelm@48337
   115
wenzelm@48337
   116
  private case class Session_Entry(
wenzelm@48337
   117
    name: String,
wenzelm@48337
   118
    reset: Boolean,
wenzelm@48349
   119
    order: Int,
wenzelm@48337
   120
    path: Option[String],
wenzelm@48347
   121
    parent: Option[String],
wenzelm@48337
   122
    description: String,
wenzelm@48421
   123
    options: List[Options.Spec],
wenzelm@48421
   124
    theories: List[(List[Options.Spec], List[String])],
wenzelm@48337
   125
    files: List[String])
wenzelm@48337
   126
wenzelm@48334
   127
  private object Parser extends Parse.Parser
wenzelm@48334
   128
  {
wenzelm@48334
   129
    val SESSION = "session"
wenzelm@48334
   130
    val IN = "in"
wenzelm@48334
   131
    val DESCRIPTION = "description"
wenzelm@48334
   132
    val OPTIONS = "options"
wenzelm@48334
   133
    val THEORIES = "theories"
wenzelm@48334
   134
    val FILES = "files"
wenzelm@48334
   135
wenzelm@48334
   136
    val syntax =
wenzelm@48336
   137
      Outer_Syntax.empty + "!" + "(" + ")" + "+" + "," + "=" + "[" + "]" +
wenzelm@48336
   138
        SESSION + IN + DESCRIPTION + OPTIONS + THEORIES + FILES
wenzelm@48334
   139
wenzelm@48337
   140
    val session_entry: Parser[Session_Entry] =
wenzelm@48334
   141
    {
wenzelm@48334
   142
      val session_name = atom("session name", _.is_name)
wenzelm@48334
   143
      val theory_name = atom("theory name", _.is_name)
wenzelm@48280
   144
wenzelm@48334
   145
      val option =
wenzelm@48334
   146
        name ~ opt(keyword("=") ~! name ^^ { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
wenzelm@48337
   147
      val options = keyword("[") ~> repsep(option, keyword(",")) <~ keyword("]")
wenzelm@48334
   148
wenzelm@48334
   149
      val theories =
wenzelm@48334
   150
        keyword(THEORIES) ~! ((options | success(Nil)) ~ rep1(theory_name)) ^^
wenzelm@48334
   151
          { case _ ~ (x ~ y) => (x, y) }
wenzelm@48334
   152
wenzelm@48334
   153
      ((keyword(SESSION) ~! session_name) ^^ { case _ ~ x => x }) ~
wenzelm@48336
   154
        (keyword("!") ^^^ true | success(false)) ~
wenzelm@48349
   155
        (keyword("(") ~! (nat <~ keyword(")")) ^^ { case _ ~ x => x } | success(Integer.MAX_VALUE)) ~
wenzelm@48337
   156
        (opt(keyword(IN) ~! string ^^ { case _ ~ x => x })) ~
wenzelm@48347
   157
        (keyword("=") ~> opt(session_name <~ keyword("+"))) ~
wenzelm@48334
   158
        (keyword(DESCRIPTION) ~! text ^^ { case _ ~ x => x } | success("")) ~
wenzelm@48334
   159
        (keyword(OPTIONS) ~! options ^^ { case _ ~ x => x } | success(Nil)) ~
wenzelm@48347
   160
        rep(theories) ~
wenzelm@48336
   161
        (keyword(FILES) ~! rep1(path) ^^ { case _ ~ x => x } | success(Nil)) ^^
wenzelm@48349
   162
          { case a ~ b ~ c ~ d ~ e ~ f ~ g ~ h ~ i => Session_Entry(a, b, c, d, e, f, g, h, i) }
wenzelm@48334
   163
    }
wenzelm@48334
   164
wenzelm@48409
   165
    def parse_entries(root: JFile): List[Session_Entry] =
wenzelm@48334
   166
    {
wenzelm@48411
   167
      val toks = syntax.scan(File.read(root))
wenzelm@48337
   168
      parse_all(rep(session_entry), Token.reader(toks, root.toString)) match {
wenzelm@48334
   169
        case Success(result, _) => result
wenzelm@48334
   170
        case bad => error(bad.toString)
wenzelm@48334
   171
      }
wenzelm@48334
   172
    }
wenzelm@48334
   173
  }
wenzelm@48280
   174
wenzelm@48337
   175
wenzelm@48341
   176
  /* find sessions */
wenzelm@48337
   177
wenzelm@48361
   178
  private val ROOT = Path.explode("ROOT")
wenzelm@48361
   179
  private val SESSIONS = Path.explode("etc/sessions")
wenzelm@48361
   180
wenzelm@48364
   181
  private def is_pure(name: String): Boolean = name == "RAW" || name == "Pure"
wenzelm@48364
   182
wenzelm@48421
   183
  private def sessions_root(options: Options, dir: Path, root: JFile, queue: Session.Queue)
wenzelm@48421
   184
    : Session.Queue =
wenzelm@48280
   185
  {
wenzelm@48361
   186
    (queue /: Parser.parse_entries(root))((queue1, entry) =>
wenzelm@48337
   187
      try {
wenzelm@48347
   188
        if (entry.name == "") error("Bad session name")
wenzelm@48347
   189
wenzelm@48337
   190
        val full_name =
wenzelm@48364
   191
          if (is_pure(entry.name)) {
wenzelm@48347
   192
            if (entry.parent.isDefined) error("Illegal parent session")
wenzelm@48347
   193
            else entry.name
wenzelm@48347
   194
          }
wenzelm@48347
   195
          else
wenzelm@48347
   196
            entry.parent match {
wenzelm@48361
   197
              case Some(parent_name) if queue1.defined(parent_name) =>
wenzelm@48347
   198
                if (entry.reset) entry.name
wenzelm@48351
   199
                else parent_name + "-" + entry.name
wenzelm@48351
   200
              case _ => error("Bad parent session")
wenzelm@48347
   201
            }
wenzelm@48339
   202
wenzelm@48337
   203
        val path =
wenzelm@48337
   204
          entry.path match {
wenzelm@48337
   205
            case Some(p) => Path.explode(p)
wenzelm@48337
   206
            case None => Path.basic(entry.name)
wenzelm@48337
   207
          }
wenzelm@48339
   208
wenzelm@48351
   209
        val key = Session.Key(full_name, entry.order)
wenzelm@48421
   210
wenzelm@48421
   211
        val theories =
wenzelm@48421
   212
          entry.theories.map({ case (opts, thys) => (options ++ opts, thys.map(Path.explode(_))) })
wenzelm@48421
   213
        val files = entry.files.map(Path.explode(_))
wenzelm@48423
   214
        val digest = SHA1.digest((full_name, entry.parent, entry.options, entry.theories).toString)
wenzelm@48423
   215
wenzelm@48418
   216
        val info =
wenzelm@48418
   217
          Session.Info(dir + path, entry.parent,
wenzelm@48423
   218
            entry.description, options ++ entry.options, theories, files, digest)
wenzelm@48351
   219
wenzelm@48418
   220
        queue1 + (key, info)
wenzelm@48337
   221
      }
wenzelm@48337
   222
      catch {
wenzelm@48337
   223
        case ERROR(msg) =>
wenzelm@48337
   224
          error(msg + "\nThe error(s) above occurred in session entry " +
wenzelm@48370
   225
            quote(entry.name) + Position.str_of(Position.file(root)))
wenzelm@48352
   226
      })
wenzelm@48352
   227
  }
wenzelm@48352
   228
wenzelm@48421
   229
  private def sessions_dir(options: Options, strict: Boolean, dir: Path, queue: Session.Queue)
wenzelm@48421
   230
    : Session.Queue =
wenzelm@48352
   231
  {
wenzelm@48373
   232
    val root = (dir + ROOT).file
wenzelm@48421
   233
    if (root.isFile) sessions_root(options, dir, root, queue)
wenzelm@48352
   234
    else if (strict) error("Bad session root file: " + quote(root.toString))
wenzelm@48361
   235
    else queue
wenzelm@48352
   236
  }
wenzelm@48352
   237
wenzelm@48421
   238
  private def sessions_catalog(options: Options, dir: Path, catalog: JFile, queue: Session.Queue)
wenzelm@48421
   239
    : Session.Queue =
wenzelm@48352
   240
  {
wenzelm@48354
   241
    val dirs =
wenzelm@48411
   242
      split_lines(File.read(catalog)).filterNot(line => line == "" || line.startsWith("#"))
wenzelm@48361
   243
    (queue /: dirs)((queue1, dir1) =>
wenzelm@48352
   244
      try {
wenzelm@48352
   245
        val dir2 = dir + Path.explode(dir1)
wenzelm@48421
   246
        if (dir2.file.isDirectory) sessions_dir(options, true, dir2, queue1)
wenzelm@48352
   247
        else error("Bad session directory: " + dir2.toString)
wenzelm@48337
   248
      }
wenzelm@48352
   249
      catch {
wenzelm@48352
   250
        case ERROR(msg) =>
wenzelm@48352
   251
          error(msg + "\nThe error(s) above occurred in session catalog " + quote(catalog.toString))
wenzelm@48352
   252
      })
wenzelm@48352
   253
  }
wenzelm@48352
   254
wenzelm@48421
   255
  def find_sessions(options: Options, all_sessions: Boolean, sessions: List[String],
wenzelm@48419
   256
    more_dirs: List[Path]): Session.Queue =
wenzelm@48352
   257
  {
wenzelm@48361
   258
    var queue = Session.Queue.empty
wenzelm@48352
   259
wenzelm@48352
   260
    for (dir <- Isabelle_System.components()) {
wenzelm@48421
   261
      queue = sessions_dir(options, false, dir, queue)
wenzelm@48352
   262
wenzelm@48373
   263
      val catalog = (dir + SESSIONS).file
wenzelm@48352
   264
      if (catalog.isFile)
wenzelm@48421
   265
        queue = sessions_catalog(options, dir, catalog, queue)
wenzelm@48337
   266
    }
wenzelm@48352
   267
wenzelm@48421
   268
    for (dir <- more_dirs) queue = sessions_dir(options, true, dir, queue)
wenzelm@48352
   269
wenzelm@48419
   270
    sessions.filter(name => !queue.defined(name)) match {
wenzelm@48419
   271
      case Nil =>
wenzelm@48419
   272
      case bad => error("Undefined session(s): " + commas_quote(bad))
wenzelm@48419
   273
    }
wenzelm@48419
   274
wenzelm@48419
   275
    if (all_sessions) queue else queue.required(sessions)
wenzelm@48280
   276
  }
wenzelm@48341
   277
wenzelm@48341
   278
wenzelm@48424
   279
wenzelm@48424
   280
  /** build **/
wenzelm@48424
   281
wenzelm@48422
   282
  /* dependencies */
wenzelm@48422
   283
wenzelm@48422
   284
  sealed case class Node(
wenzelm@48422
   285
    loaded_theories: Set[String],
wenzelm@48423
   286
    sources: List[(Path, SHA1.Digest)])
wenzelm@48422
   287
wenzelm@48423
   288
  sealed case class Deps(deps: Map[String, Node])
wenzelm@48423
   289
  {
wenzelm@48423
   290
    def sources(name: String): List[(Path, SHA1.Digest)] = deps(name).sources
wenzelm@48423
   291
  }
wenzelm@48423
   292
wenzelm@48423
   293
  def dependencies(queue: Session.Queue): Deps =
wenzelm@48423
   294
    Deps((Map.empty[String, Node] /: queue.topological_order)(
wenzelm@48422
   295
      { case (deps, (name, info)) =>
wenzelm@48422
   296
          val preloaded =
wenzelm@48422
   297
            info.parent match {
wenzelm@48422
   298
              case None => Set.empty[String]
wenzelm@48422
   299
              case Some(parent) => deps(parent).loaded_theories
wenzelm@48422
   300
            }
wenzelm@48422
   301
          val thy_info = new Thy_Info(new Thy_Load(preloaded))
wenzelm@48422
   302
wenzelm@48422
   303
          val thy_deps =
wenzelm@48422
   304
            thy_info.dependencies(
wenzelm@48422
   305
              info.theories.map(_._2).flatten.
wenzelm@48422
   306
                map(thy => Document.Node.Name(info.dir + Thy_Load.thy_path(thy))))
wenzelm@48422
   307
wenzelm@48422
   308
          val loaded_theories = preloaded ++ thy_deps.map(_._1.theory)
wenzelm@48423
   309
wenzelm@48423
   310
          val all_files =
wenzelm@48422
   311
            thy_deps.map({ case (n, h) =>
wenzelm@48422
   312
              val thy = Path.explode(n.node).expand
wenzelm@48422
   313
              val uses =
wenzelm@48422
   314
                h match {
wenzelm@48422
   315
                  case Exn.Res(d) =>
wenzelm@48422
   316
                    d.uses.map(p => (Path.explode(n.dir) + Path.explode(p._1)).expand)
wenzelm@48422
   317
                  case _ => Nil
wenzelm@48422
   318
                }
wenzelm@48422
   319
              thy :: uses
wenzelm@48422
   320
            }).flatten ::: info.files.map(file => info.dir + file)
wenzelm@48423
   321
          val sources = all_files.par.map(p => (p, SHA1.digest(p))).toList
wenzelm@48422
   322
wenzelm@48422
   323
          deps + (name -> Node(loaded_theories, sources))
wenzelm@48423
   324
      }))
wenzelm@48422
   325
wenzelm@48422
   326
wenzelm@48424
   327
  /* jobs */
wenzelm@48341
   328
wenzelm@48424
   329
  private class Job(cwd: JFile, env: Map[String, String], script: String, args: String)
wenzelm@48364
   330
  {
wenzelm@48418
   331
    private val args_file = File.tmp_file("args")
wenzelm@48418
   332
    private val env1 = env + ("ARGS_FILE" -> Isabelle_System.posix_path(args_file.getPath))
wenzelm@48418
   333
    File.write(args_file, args)
wenzelm@48418
   334
wenzelm@48424
   335
    private val (thread, result) =
wenzelm@48424
   336
      Simple_Thread.future("build") { Isabelle_System.bash_env(cwd, env1, script) }
wenzelm@48418
   337
wenzelm@48418
   338
    def terminate: Unit = thread.interrupt
wenzelm@48418
   339
    def is_finished: Boolean = result.is_finished
wenzelm@48425
   340
    def join: (String, String, Int) = { val res = result.join; args_file.delete; res }
wenzelm@48418
   341
  }
wenzelm@48418
   342
wenzelm@48447
   343
  private def start_job(name: String, info: Session.Info, output: Option[String]): Job =
wenzelm@48418
   344
  {
wenzelm@48418
   345
    val parent = info.parent.getOrElse("")
wenzelm@48418
   346
wenzelm@48373
   347
    val cwd = info.dir.file
wenzelm@48447
   348
    val env = Map("INPUT" -> parent, "TARGET" -> name, "OUTPUT" -> output.getOrElse(""))
wenzelm@48364
   349
    val script =
wenzelm@48447
   350
      if (is_pure(name)) "./build " + name + " \"$OUTPUT\""
wenzelm@48418
   351
      else {
wenzelm@48418
   352
        """
wenzelm@48418
   353
        . "$ISABELLE_HOME/lib/scripts/timestart.bash"
wenzelm@48418
   354
        """ +
wenzelm@48447
   355
          (if (output.isDefined)
wenzelm@48447
   356
            """ "$ISABELLE_PROCESS" -e "Build.build \"$ARGS_FILE\";" -q -w "$INPUT" "$OUTPUT" """
wenzelm@48418
   357
          else
wenzelm@48418
   358
            """ "$ISABELLE_PROCESS" -e "Build.build \"$ARGS_FILE\";" -r -q "$INPUT" """) +
wenzelm@48418
   359
        """
wenzelm@48418
   360
        RC="$?"
wenzelm@48418
   361
wenzelm@48418
   362
        . "$ISABELLE_HOME/lib/scripts/timestop.bash"
wenzelm@48418
   363
wenzelm@48418
   364
        if [ "$RC" -eq 0 ]; then
wenzelm@48418
   365
          echo "Finished $TARGET ($TIMES_REPORT)" >&2
wenzelm@48418
   366
        fi
wenzelm@48418
   367
wenzelm@48418
   368
        exit "$RC"
wenzelm@48418
   369
        """
wenzelm@48418
   370
      }
wenzelm@48418
   371
    val args_xml =
wenzelm@48418
   372
    {
wenzelm@48418
   373
      import XML.Encode._
wenzelm@48421
   374
      pair(bool, pair(string, pair(string, list(string))))(
wenzelm@48447
   375
        output.isDefined, (parent, (name, info.theories.map(_._2).flatten.map(_.implode))))
wenzelm@48418
   376
    }
wenzelm@48424
   377
    new Job(cwd, env, script, YXML.string_of_body(args_xml))
wenzelm@48364
   378
  }
wenzelm@48364
   379
wenzelm@48424
   380
wenzelm@48425
   381
  /* build */
wenzelm@48424
   382
wenzelm@48424
   383
  private def echo(msg: String) { java.lang.System.out.println(msg) }
wenzelm@48425
   384
  private def sleep(): Unit = Thread.sleep(500)
wenzelm@48424
   385
wenzelm@48425
   386
  def build(all_sessions: Boolean, build_images: Boolean, max_jobs: Int,
wenzelm@48447
   387
    list_only: Boolean, system_mode: Boolean, verbose: Boolean,
wenzelm@48419
   388
    more_dirs: List[Path], more_options: List[String], sessions: List[String]): Int =
wenzelm@48341
   389
  {
wenzelm@48419
   390
    val options = (Options.init() /: more_options)(_.define_simple(_))
wenzelm@48421
   391
    val queue = find_sessions(options, all_sessions, sessions, more_dirs)
wenzelm@48423
   392
    val deps = dependencies(queue)
wenzelm@48368
   393
wenzelm@48447
   394
    val (output_dir, browser_info) =
wenzelm@48447
   395
      if (system_mode) (Path.explode("~~/heaps/$ML_IDENTIFIER"), Path.explode("~~/browser_info"))
wenzelm@48447
   396
      else (Path.explode("$ISABELLE_OUTPUT"), Path.explode("$ISABELLE_BROWSER_INFO"))
wenzelm@48363
   397
wenzelm@48373
   398
    // prepare browser info dir
wenzelm@48447
   399
    if (options.bool("browser_info") && !(browser_info + Path.explode("index.html")).file.isFile)
wenzelm@48373
   400
    {
wenzelm@48447
   401
      browser_info.file.mkdirs()
wenzelm@48447
   402
      File.copy(Path.explode("~~/lib/logo/isabelle.gif"),
wenzelm@48447
   403
        browser_info + Path.explode("isabelle.gif"))
wenzelm@48447
   404
      File.write(browser_info + Path.explode("index.html"),
wenzelm@48447
   405
        File.read(Path.explode("~~/lib/html/library_index_header.template")) +
wenzelm@48447
   406
        File.read(Path.explode("~~/lib/html/library_index_content.template")) +
wenzelm@48447
   407
        File.read(Path.explode("~~/lib/html/library_index_footer.template")))
wenzelm@48364
   408
    }
wenzelm@48341
   409
wenzelm@48373
   410
    // prepare log dir
wenzelm@48447
   411
    val log_dir = output_dir + Path.explode("log")
wenzelm@48373
   412
    log_dir.file.mkdirs()
wenzelm@48373
   413
wenzelm@48425
   414
    // scheduler loop
wenzelm@48425
   415
    @tailrec def loop(
wenzelm@48425
   416
      pending: Session.Queue,
wenzelm@48425
   417
      running: Map[String, Job],
wenzelm@48425
   418
      results: Map[String, Int]): Map[String, Int] =
wenzelm@48425
   419
    {
wenzelm@48425
   420
      if (pending.is_empty) results
wenzelm@48425
   421
      else if (running.exists({ case (_, job) => job.is_finished })) {
wenzelm@48425
   422
        val (name, job) = running.find({ case (_, job) => job.is_finished }).get
wenzelm@48424
   423
wenzelm@48425
   424
        val (out, err, rc) = job.join
wenzelm@48425
   425
        echo(Library.trim_line(err))
wenzelm@48373
   426
wenzelm@48425
   427
        val log = log_dir + Path.basic(name)
wenzelm@48425
   428
        if (rc == 0) {
wenzelm@48425
   429
          val sources =
wenzelm@48425
   430
            (queue(name).digest :: deps.sources(name).map(_._2)).map(_.toString).sorted
wenzelm@48425
   431
              .mkString("sources: ", " ", "\n")
wenzelm@48425
   432
          File.write_zip(log.ext("gz"), sources + out)
wenzelm@48425
   433
        }
wenzelm@48425
   434
        else {
wenzelm@48425
   435
          File.write(log, out)
wenzelm@48425
   436
          echo(name + " FAILED")
wenzelm@48425
   437
          echo("(see also " + log.file + ")")
wenzelm@48425
   438
          val lines = split_lines(out)
wenzelm@48425
   439
          val tail = lines.drop(lines.length - 20 max 0)
wenzelm@48425
   440
          echo("\n" + cat_lines(tail))
wenzelm@48425
   441
        }
wenzelm@48425
   442
        loop(pending - name, running - name, results + (name -> rc))
wenzelm@48425
   443
      }
wenzelm@48425
   444
      else if (running.size < (max_jobs max 1)) {
wenzelm@48425
   445
        pending.dequeue(running.isDefinedAt(_)) match {
wenzelm@48425
   446
          case Some((name, info)) =>
wenzelm@48425
   447
            if (list_only) {
wenzelm@48425
   448
              echo(name + " in " + info.dir)
wenzelm@48425
   449
              loop(pending - name, running, results + (name -> 0))
wenzelm@48425
   450
            }
wenzelm@48425
   451
            else if (info.parent.map(results(_)).forall(_ == 0)) {
wenzelm@48447
   452
              val output =
wenzelm@48447
   453
                if (build_images || queue.is_inner(name))
wenzelm@48447
   454
                  Some(Isabelle_System.standard_path(output_dir + Path.basic(name)))
wenzelm@48447
   455
                else None
wenzelm@48447
   456
              echo((if (output.isDefined) "Building " else "Running ") + name + " ...")
wenzelm@48447
   457
              val job = start_job(name, info, output)
wenzelm@48425
   458
              loop(pending, running + (name -> job), results)
wenzelm@48425
   459
            }
wenzelm@48425
   460
            else {
wenzelm@48425
   461
              echo(name + " CANCELLED")
wenzelm@48425
   462
              loop(pending - name, running, results + (name -> 1))
wenzelm@48425
   463
            }
wenzelm@48425
   464
          case None => sleep(); loop(pending, running, results)
wenzelm@48373
   465
        }
wenzelm@48373
   466
      }
wenzelm@48425
   467
      else { sleep(); loop(pending, running, results) }
wenzelm@48425
   468
    }
wenzelm@48425
   469
wenzelm@48425
   470
    (0 /: loop(queue, Map.empty, Map.empty))({ case (rc1, (_, rc2)) => rc1 max rc2 })
wenzelm@48341
   471
  }
wenzelm@48341
   472
wenzelm@48341
   473
wenzelm@48346
   474
  /* command line entry point */
wenzelm@48341
   475
wenzelm@48341
   476
  def main(args: Array[String])
wenzelm@48341
   477
  {
wenzelm@48346
   478
    Command_Line.tool {
wenzelm@48346
   479
      args.toList match {
wenzelm@48346
   480
        case
wenzelm@48346
   481
          Properties.Value.Boolean(all_sessions) ::
wenzelm@48346
   482
          Properties.Value.Boolean(build_images) ::
wenzelm@48425
   483
          Properties.Value.Int(max_jobs) ::
wenzelm@48346
   484
          Properties.Value.Boolean(list_only) ::
wenzelm@48447
   485
          Properties.Value.Boolean(system_mode) ::
wenzelm@48425
   486
          Properties.Value.Boolean(verbose) ::
wenzelm@48346
   487
          Command_Line.Chunks(more_dirs, options, sessions) =>
wenzelm@48447
   488
            build(all_sessions, build_images, max_jobs, list_only, system_mode,
wenzelm@48447
   489
              verbose, more_dirs.map(Path.explode), options, sessions)
wenzelm@48346
   490
        case _ => error("Bad arguments:\n" + cat_lines(args))
wenzelm@48341
   491
      }
wenzelm@48346
   492
    }
wenzelm@48341
   493
  }
wenzelm@48276
   494
}
wenzelm@48276
   495