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