src/Pure/Tools/build.scala
author wenzelm
Sun May 06 23:03:08 2018 +0200 (12 months ago)
changeset 68092 888d35a19866
parent 68088 0763d4eb3ebc
child 68148 fb661e4c4717
permissions -rw-r--r--
store exports in session database, with asynchronous / parallel compression;
wenzelm@50686
     1
/*  Title:      Pure/Tools/build.scala
wenzelm@48276
     2
    Author:     Makarius
wenzelm@57923
     3
    Options:    :folding=explicit:
wenzelm@48276
     4
wenzelm@48276
     5
Build and manage Isabelle sessions.
wenzelm@48276
     6
*/
wenzelm@48276
     7
wenzelm@48276
     8
package isabelle
wenzelm@48276
     9
wenzelm@48276
    10
wenzelm@48676
    11
import scala.collection.SortedSet
wenzelm@48340
    12
import scala.annotation.tailrec
wenzelm@48337
    13
wenzelm@48335
    14
wenzelm@48276
    15
object Build
wenzelm@48276
    16
{
wenzelm@62631
    17
  /** auxiliary **/
wenzelm@48424
    18
wenzelm@65291
    19
  /* persistent build info */
wenzelm@65291
    20
wenzelm@65291
    21
  sealed case class Session_Info(
wenzelm@66749
    22
    sources: String,
wenzelm@65291
    23
    input_heaps: List[String],
wenzelm@65291
    24
    output_heap: Option[String],
wenzelm@65291
    25
    return_code: Int)
wenzelm@66747
    26
  {
wenzelm@66747
    27
    def ok: Boolean = return_code == 0
wenzelm@66747
    28
  }
wenzelm@65291
    29
wenzelm@65291
    30
wenzelm@65289
    31
  /* queue with scheduling information */
wenzelm@48676
    32
wenzelm@62631
    33
  private object Queue
wenzelm@48676
    34
  {
wenzelm@68086
    35
    type Timings = (List[Properties.T], Double)
wenzelm@68086
    36
wenzelm@68086
    37
    def load_timings(progress: Progress, store: Sessions.Store, name: String): Timings =
wenzelm@65289
    38
    {
wenzelm@68086
    39
      val no_timings: Timings = (Nil, 0.0)
wenzelm@65289
    40
wenzelm@65291
    41
      store.find_database(name) match {
wenzelm@65291
    42
        case None => no_timings
wenzelm@65291
    43
        case Some(database) =>
wenzelm@65291
    44
          def ignore_error(msg: String) =
wenzelm@65291
    45
          {
wenzelm@65831
    46
            progress.echo_warning("Ignoring bad database: " +
wenzelm@65365
    47
              database.expand + (if (msg == "") "" else "\n" + msg))
wenzelm@65291
    48
            no_timings
wenzelm@65291
    49
          }
wenzelm@65291
    50
          try {
wenzelm@65291
    51
            using(SQLite.open_database(database))(db =>
wenzelm@65291
    52
            {
wenzelm@65935
    53
              val command_timings = store.read_command_timings(db, name)
wenzelm@65935
    54
              val session_timing =
wenzelm@65935
    55
                store.read_session_timing(db, name) match {
wenzelm@65935
    56
                  case Markup.Elapsed(t) => t
wenzelm@65935
    57
                  case _ => 0.0
wenzelm@65935
    58
                }
wenzelm@65935
    59
              (command_timings, session_timing)
wenzelm@65291
    60
            })
wenzelm@65291
    61
          }
wenzelm@65291
    62
          catch {
wenzelm@65291
    63
            case ERROR(msg) => ignore_error(msg)
wenzelm@65291
    64
            case exn: java.lang.Error => ignore_error(Exn.message(exn))
wenzelm@65291
    65
            case _: XML.Error => ignore_error("")
wenzelm@65291
    66
          }
wenzelm@65289
    67
      }
wenzelm@65289
    68
    }
wenzelm@65289
    69
wenzelm@67052
    70
    def apply(progress: Progress, sessions: Sessions.Structure, store: Sessions.Store): Queue =
wenzelm@48676
    71
    {
wenzelm@65420
    72
      val graph = sessions.build_graph
wenzelm@65415
    73
      val names = graph.keys
wenzelm@51220
    74
wenzelm@65831
    75
      val timings = names.map(name => (name, load_timings(progress, store, name)))
wenzelm@51220
    76
      val command_timings =
wenzelm@51220
    77
        Map(timings.map({ case (name, (ts, _)) => (name, ts) }): _*).withDefaultValue(Nil)
wenzelm@51220
    78
      val session_timing =
wenzelm@51220
    79
        Map(timings.map({ case (name, (_, t)) => (name, t) }): _*).withDefaultValue(0.0)
wenzelm@48678
    80
wenzelm@48678
    81
      def outdegree(name: String): Int = graph.imm_succs(name).size
wenzelm@48678
    82
wenzelm@48678
    83
      object Ordering extends scala.math.Ordering[String]
wenzelm@48678
    84
      {
wenzelm@51227
    85
        def compare_timing(name1: String, name2: String): Int =
wenzelm@51227
    86
        {
wenzelm@51227
    87
          val t1 = session_timing(name1)
wenzelm@51227
    88
          val t2 = session_timing(name2)
wenzelm@51227
    89
          if (t1 == 0.0 || t2 == 0.0) 0
wenzelm@51227
    90
          else t1 compare t2
wenzelm@51227
    91
        }
wenzelm@51227
    92
wenzelm@48678
    93
        def compare(name1: String, name2: String): Int =
wenzelm@51229
    94
          outdegree(name2) compare outdegree(name1) match {
wenzelm@48678
    95
            case 0 =>
wenzelm@51229
    96
              compare_timing(name2, name1) match {
wenzelm@51220
    97
                case 0 =>
wenzelm@65415
    98
                  sessions(name2).timeout compare sessions(name1).timeout match {
wenzelm@51220
    99
                    case 0 => name1 compare name2
wenzelm@51220
   100
                    case ord => ord
wenzelm@51220
   101
                  }
wenzelm@48678
   102
                case ord => ord
wenzelm@48678
   103
              }
wenzelm@48678
   104
            case ord => ord
wenzelm@48678
   105
          }
wenzelm@48678
   106
      }
wenzelm@48678
   107
wenzelm@65415
   108
      new Queue(graph, SortedSet(names: _*)(Ordering), command_timings)
wenzelm@48676
   109
    }
wenzelm@48676
   110
  }
wenzelm@48676
   111
wenzelm@65314
   112
  private class Queue(
wenzelm@62631
   113
    graph: Graph[String, Sessions.Info],
wenzelm@51220
   114
    order: SortedSet[String],
wenzelm@51220
   115
    val command_timings: String => List[Properties.T])
wenzelm@48676
   116
  {
wenzelm@48676
   117
    def is_inner(name: String): Boolean = !graph.is_maximal(name)
wenzelm@48676
   118
wenzelm@48676
   119
    def is_empty: Boolean = graph.is_empty
wenzelm@48676
   120
wenzelm@51227
   121
    def - (name: String): Queue =
wenzelm@51227
   122
      new Queue(graph.del_node(name),
wenzelm@67011
   123
        order - name,  // FIXME scala-2.10.0 .. 2.12.4 TreeSet problem!?
wenzelm@51227
   124
        command_timings)
wenzelm@48676
   125
wenzelm@62631
   126
    def dequeue(skip: String => Boolean): Option[(String, Sessions.Info)] =
wenzelm@48676
   127
    {
wenzelm@51227
   128
      val it = order.iterator.dropWhile(name =>
wenzelm@51227
   129
        skip(name)
wenzelm@67011
   130
          || !graph.defined(name)  // FIXME scala-2.10.0 .. 2.12.4 TreeSet problem!?
wenzelm@51227
   131
          || !graph.is_minimal(name))
wenzelm@48680
   132
      if (it.hasNext) { val name = it.next; Some((name, graph.get_node(name))) }
wenzelm@48676
   133
      else None
wenzelm@48676
   134
    }
wenzelm@48676
   135
  }
wenzelm@48676
   136
wenzelm@48676
   137
wenzelm@65313
   138
  /* PIDE protocol handler */
wenzelm@65313
   139
wenzelm@65313
   140
  class Handler(progress: Progress, session: Session, session_name: String)
wenzelm@65313
   141
    extends Session.Protocol_Handler
wenzelm@65313
   142
  {
wenzelm@65313
   143
    val result_error: Promise[String] = Future.promise
wenzelm@65313
   144
wenzelm@65313
   145
    override def exit() { result_error.cancel }
wenzelm@65313
   146
wenzelm@65313
   147
    private def build_session_finished(msg: Prover.Protocol_Output): Boolean =
wenzelm@65313
   148
    {
wenzelm@65313
   149
      val error_message =
wenzelm@65344
   150
        try { Pretty.string_of(Symbol.decode_yxml(msg.text)) }
wenzelm@65313
   151
        catch { case ERROR(msg) => msg }
wenzelm@65313
   152
      result_error.fulfill(error_message)
wenzelm@65313
   153
      session.send_stop()
wenzelm@65313
   154
      true
wenzelm@65313
   155
    }
wenzelm@65313
   156
wenzelm@65313
   157
    private def loading_theory(msg: Prover.Protocol_Output): Boolean =
wenzelm@65313
   158
      msg.properties match {
wenzelm@65313
   159
        case Markup.Loading_Theory(name) =>
wenzelm@65313
   160
          progress.theory(session_name, name)
wenzelm@65313
   161
          true
wenzelm@65313
   162
        case _ => false
wenzelm@65313
   163
      }
wenzelm@65313
   164
wenzelm@65313
   165
    val functions =
wenzelm@65313
   166
      List(
wenzelm@65313
   167
        Markup.BUILD_SESSION_FINISHED -> build_session_finished _,
wenzelm@65313
   168
        Markup.LOADING_THEORY -> loading_theory _)
wenzelm@65313
   169
  }
wenzelm@65313
   170
wenzelm@65313
   171
wenzelm@65308
   172
  /* job: running prover process */
wenzelm@48341
   173
wenzelm@65308
   174
  private class Job(progress: Progress,
wenzelm@65308
   175
    name: String,
wenzelm@65308
   176
    val info: Sessions.Info,
wenzelm@67052
   177
    sessions: Sessions.Structure,
wenzelm@65313
   178
    deps: Sessions.Deps,
wenzelm@65308
   179
    store: Sessions.Store,
wenzelm@65308
   180
    do_output: Boolean,
wenzelm@65308
   181
    verbose: Boolean,
wenzelm@65308
   182
    pide: Boolean,
wenzelm@65308
   183
    val numa_node: Option[Int],
wenzelm@65308
   184
    command_timings: List[Properties.T])
wenzelm@48418
   185
  {
wenzelm@62633
   186
    val output = store.output_dir + Path.basic(name)
wenzelm@48674
   187
    def output_path: Option[Path] = if (do_output) Some(output) else None
wenzelm@48674
   188
wenzelm@65312
   189
    val options =
wenzelm@65312
   190
      numa_node match {
wenzelm@65312
   191
        case None => info.options
wenzelm@65312
   192
        case Some(n) => info.options.string("ML_process_policy") = NUMA.policy(n)
wenzelm@65312
   193
      }
wenzelm@65312
   194
wenzelm@59445
   195
    private val graph_file = Isabelle_System.tmp_file("session_graph", "pdf")
wenzelm@66822
   196
    isabelle.graphview.Graph_File.write(options, graph_file, deps(name).session_graph_display)
wenzelm@59445
   197
wenzelm@68092
   198
    private val export_consumer = Export.consumer(SQLite.open_database(store.output_database(name)))
wenzelm@68092
   199
wenzelm@62573
   200
    private val future_result: Future[Process_Result] =
wenzelm@61559
   201
      Future.thread("build") {
wenzelm@65308
   202
        val parent = info.parent.getOrElse("")
wenzelm@65441
   203
        val base = deps(name)
wenzelm@65308
   204
        val args_yxml =
wenzelm@65308
   205
          YXML.string_of_body(
wenzelm@62944
   206
            {
wenzelm@62944
   207
              import XML.Encode._
wenzelm@62944
   208
              pair(list(pair(string, int)), pair(list(properties), pair(bool, pair(bool,
wenzelm@62944
   209
                pair(Path.encode, pair(list(pair(Path.encode, Path.encode)), pair(string,
wenzelm@65307
   210
                pair(string, pair(string, pair(string, pair(Path.encode,
wenzelm@65517
   211
                pair(list(pair(Options.encode, list(pair(string, properties)))),
wenzelm@67493
   212
                pair(list(pair(string, properties)), pair(list(string),
wenzelm@67493
   213
                pair(list(pair(string, string)),
wenzelm@67471
   214
                pair(list(string), pair(list(pair(string, string)), list(string))))))))))))))))))(
wenzelm@62944
   215
              (Symbol.codes, (command_timings, (do_output, (verbose,
wenzelm@62944
   216
                (store.browser_info, (info.document_files, (File.standard_path(graph_file),
wenzelm@65307
   217
                (parent, (info.chapter, (name, (Path.current,
wenzelm@67471
   218
                (info.theories, (base.known.sessions.toList, (base.doc_names,
wenzelm@67471
   219
                (base.global_theories.toList, (base.loaded_theories.keys, (base.dest_known_theories,
wenzelm@67471
   220
                info.bibtex_entries.map(_.info)))))))))))))))))))
wenzelm@65308
   221
            })
wenzelm@65308
   222
wenzelm@65308
   223
        val env =
wenzelm@65308
   224
          Isabelle_System.settings() +
wenzelm@65312
   225
            ("ISABELLE_ML_DEBUGGER" -> options.bool("ML_debugger").toString)
wenzelm@65308
   226
wenzelm@65308
   227
        def save_heap: String =
wenzelm@66972
   228
          (if (info.theories.isEmpty) "" else "ML_Heap.share_common_data (); ") +
wenzelm@66972
   229
            "ML_Heap.save_child " + ML_Syntax.print_string_bytes(File.platform_path(output))
wenzelm@62944
   230
wenzelm@65360
   231
        if (pide && !Sessions.is_pure(name)) {
wenzelm@65532
   232
          val resources = new Resources(deps(parent))
wenzelm@65313
   233
          val session = new Session(options, resources)
wenzelm@65313
   234
          val handler = new Handler(progress, session, name)
wenzelm@65315
   235
          session.init_protocol_handler(handler)
wenzelm@65313
   236
wenzelm@65317
   237
          val session_result = Future.promise[Process_Result]
wenzelm@65313
   238
wenzelm@65313
   239
          Isabelle_Process.start(session, options, logic = parent,
wenzelm@65415
   240
            cwd = info.dir.file, env = env, sessions = Some(sessions), store = store,
wenzelm@65313
   241
            phase_changed =
wenzelm@65313
   242
            {
wenzelm@65313
   243
              case Session.Ready => session.protocol_command("build_session", args_yxml)
wenzelm@65317
   244
              case Session.Terminated(result) => session_result.fulfill(result)
wenzelm@65313
   245
              case _ =>
wenzelm@65313
   246
            })
wenzelm@65313
   247
wenzelm@65317
   248
          val result = session_result.join
wenzelm@65313
   249
          handler.result_error.join match {
wenzelm@65317
   250
            case "" => result
wenzelm@65317
   251
            case msg =>
wenzelm@65317
   252
              result.copy(
wenzelm@65317
   253
                rc = result.rc max 1,
wenzelm@65828
   254
                out_lines = result.out_lines ::: split_lines(Output.error_message_text(msg)))
wenzelm@65313
   255
          }
wenzelm@65308
   256
        }
wenzelm@65308
   257
        else {
wenzelm@65308
   258
          val args_file = Isabelle_System.tmp_file("build")
wenzelm@65308
   259
          File.write(args_file, args_yxml)
wenzelm@65308
   260
wenzelm@65308
   261
          val eval =
wenzelm@65308
   262
            "Command_Line.tool0 (fn () => (" +
wenzelm@66782
   263
            "Build.build " + ML_Syntax.print_string_bytes(File.standard_path(args_file)) +
wenzelm@67380
   264
            (if (Sessions.is_pure(name)) "; Theory.install_pure (Thy_Info.get_theory Context.PureN)"
wenzelm@67380
   265
             else "") + (if (do_output) "; " + save_heap else "") + "));"
wenzelm@64265
   266
wenzelm@65308
   267
          val process =
wenzelm@65360
   268
            if (Sessions.is_pure(name)) {
wenzelm@65312
   269
              ML_Process(options, raw_ml_system = true, cwd = info.dir.file,
wenzelm@65308
   270
                args =
wenzelm@65308
   271
                  (for ((root, _) <- Thy_Header.ml_roots) yield List("--use", root)).flatten :::
wenzelm@65308
   272
                  List("--eval", eval),
wenzelm@65415
   273
                env = env, sessions = Some(sessions), store = store,
wenzelm@65415
   274
                cleanup = () => args_file.delete)
wenzelm@65308
   275
            }
wenzelm@65308
   276
            else {
wenzelm@65312
   277
              ML_Process(options, parent, List("--eval", eval), cwd = info.dir.file,
wenzelm@65415
   278
                env = env, sessions = Some(sessions), store = store,
wenzelm@65415
   279
                cleanup = () => args_file.delete)
wenzelm@65308
   280
            }
wenzelm@64265
   281
wenzelm@65308
   282
          process.result(
wenzelm@65308
   283
            progress_stdout = (line: String) =>
wenzelm@65308
   284
              Library.try_unprefix("\floading_theory = ", line) match {
wenzelm@65308
   285
                case Some(theory) => progress.theory(name, theory)
wenzelm@65308
   286
                case None =>
wenzelm@68088
   287
                  for {
wenzelm@68088
   288
                    text <- Library.try_unprefix("\fexport = ", line)
wenzelm@68088
   289
                    (args, body) <-
wenzelm@68088
   290
                      Markup.Export.dest_inline(XML.Decode.properties(YXML.parse_body(text)))
wenzelm@68092
   291
                  } { export_consumer(name, args, body) }
wenzelm@65308
   292
              },
wenzelm@65308
   293
            progress_limit =
wenzelm@65312
   294
              options.int("process_output_limit") match {
wenzelm@65308
   295
                case 0 => None
wenzelm@65308
   296
                case m => Some(m * 1000000L)
wenzelm@65308
   297
              },
wenzelm@65308
   298
            strict = false)
wenzelm@65308
   299
        }
wenzelm@50845
   300
      }
wenzelm@48674
   301
wenzelm@62572
   302
    def terminate: Unit = future_result.cancel
wenzelm@62572
   303
    def is_finished: Boolean = future_result.is_finished
wenzelm@48674
   304
wenzelm@56779
   305
    private val timeout_request: Option[Event_Timer.Request] =
wenzelm@56779
   306
    {
wenzelm@62569
   307
      if (info.timeout > Time.zero)
wenzelm@66943
   308
        Some(Event_Timer.request(Time.now() + info.timeout) { terminate })
wenzelm@48674
   309
      else None
wenzelm@56779
   310
    }
wenzelm@48674
   311
wenzelm@62400
   312
    def join: Process_Result =
wenzelm@50845
   313
    {
wenzelm@62572
   314
      val result = future_result.join
wenzelm@68092
   315
      val export_result =
wenzelm@68092
   316
        export_consumer.shutdown(close = true).map(Output.error_message_text(_)) match {
wenzelm@68092
   317
          case Nil => result
wenzelm@68092
   318
          case errs if result.ok => result.copy(rc = 1).errors(errs)
wenzelm@68092
   319
          case errs => result.errors(errs)
wenzelm@68092
   320
        }
wenzelm@50845
   321
wenzelm@68092
   322
      if (export_result.ok)
wenzelm@62633
   323
        Present.finish(progress, store.browser_info, graph_file, info, name)
wenzelm@61372
   324
wenzelm@59445
   325
      graph_file.delete
wenzelm@66943
   326
wenzelm@66943
   327
      val was_timeout =
wenzelm@66943
   328
        timeout_request match {
wenzelm@66943
   329
          case None => false
wenzelm@66943
   330
          case Some(request) => !request.cancel
wenzelm@66943
   331
        }
wenzelm@48674
   332
wenzelm@68092
   333
      if (export_result.interrupted) {
wenzelm@68092
   334
        if (was_timeout) export_result.error(Output.error_message_text("Timeout")).was_timeout
wenzelm@68092
   335
        else export_result.error(Output.error_message_text("Interrupt"))
wenzelm@52063
   336
      }
wenzelm@68092
   337
      else export_result
wenzelm@48674
   338
    }
wenzelm@48364
   339
  }
wenzelm@48364
   340
wenzelm@48424
   341
wenzelm@62631
   342
wenzelm@62641
   343
  /** build with results **/
wenzelm@48424
   344
wenzelm@63996
   345
  class Results private[Build](results: Map[String, (Option[Process_Result], Sessions.Info)])
wenzelm@62403
   346
  {
wenzelm@62403
   347
    def sessions: Set[String] = results.keySet
lars@63082
   348
    def cancelled(name: String): Boolean = results(name)._1.isEmpty
lars@63082
   349
    def apply(name: String): Process_Result = results(name)._1.getOrElse(Process_Result(1))
lars@63082
   350
    def info(name: String): Sessions.Info = results(name)._2
wenzelm@65253
   351
    val rc =
wenzelm@65253
   352
      (0 /: results.iterator.map(
wenzelm@65253
   353
        { case (_, (Some(r), _)) => r.rc case (_, (None, _)) => 1 }))(_ max _)
wenzelm@62641
   354
    def ok: Boolean = rc == 0
wenzelm@62406
   355
wenzelm@62406
   356
    override def toString: String = rc.toString
wenzelm@62403
   357
  }
wenzelm@62403
   358
wenzelm@62641
   359
  def build(
wenzelm@50404
   360
    options: Options,
wenzelm@64909
   361
    progress: Progress = No_Progress,
wenzelm@65832
   362
    check_unknown_files: Boolean = false,
wenzelm@48511
   363
    build_heap: Boolean = false,
wenzelm@48595
   364
    clean_build: Boolean = false,
wenzelm@56890
   365
    dirs: List[Path] = Nil,
wenzelm@56890
   366
    select_dirs: List[Path] = Nil,
wenzelm@66968
   367
    infos: List[Sessions.Info] = Nil,
wenzelm@64265
   368
    numa_shuffling: Boolean = false,
wenzelm@48509
   369
    max_jobs: Int = 1,
wenzelm@48903
   370
    list_files: Boolean = false,
wenzelm@59891
   371
    check_keywords: Set[String] = Set.empty,
wenzelm@66841
   372
    fresh_build: Boolean = false,
wenzelm@48509
   373
    no_build: Boolean = false,
wenzelm@66745
   374
    soft_build: Boolean = false,
wenzelm@48509
   375
    system_mode: Boolean = false,
wenzelm@48509
   376
    verbose: Boolean = false,
wenzelm@65308
   377
    pide: Boolean = false,
wenzelm@63224
   378
    requirements: Boolean = false,
wenzelm@63224
   379
    all_sessions: Boolean = false,
wenzelm@66737
   380
    base_sessions: List[String] = Nil,
wenzelm@63224
   381
    exclude_session_groups: List[String] = Nil,
wenzelm@59892
   382
    exclude_sessions: List[String] = Nil,
wenzelm@63224
   383
    session_groups: List[String] = Nil,
wenzelm@65422
   384
    sessions: List[String] = Nil,
wenzelm@65422
   385
    selection: Sessions.Selection = Sessions.Selection.empty): Results =
wenzelm@63224
   386
  {
wenzelm@66745
   387
    val build_options = options.int.update("completion_limit", 0).bool.update("ML_statistics", true)
wenzelm@51220
   388
wenzelm@66745
   389
    val store = Sessions.store(system_mode)
wenzelm@66745
   390
wenzelm@66745
   391
wenzelm@66745
   392
    /* session selection and dependencies */
wenzelm@65422
   393
wenzelm@66961
   394
    val full_sessions =
wenzelm@67026
   395
      Sessions.load_structure(build_options, dirs = dirs, select_dirs = select_dirs, infos = infos)
wenzelm@65422
   396
wenzelm@66749
   397
    def sources_stamp(deps: Sessions.Deps, name: String): String =
wenzelm@66749
   398
    {
wenzelm@66749
   399
      val digests =
wenzelm@66749
   400
        full_sessions(name).meta_digest :: deps.sources(name) ::: deps.imported_sources(name)
wenzelm@66749
   401
      SHA1.digest(cat_lines(digests.map(_.toString).sorted)).toString
wenzelm@66749
   402
    }
wenzelm@66745
   403
wenzelm@67030
   404
    val selection1 =
wenzelm@67030
   405
      Sessions.Selection(requirements, all_sessions, base_sessions, exclude_session_groups,
wenzelm@67030
   406
        exclude_sessions, session_groups, sessions) ++ selection
wenzelm@67030
   407
wenzelm@67025
   408
    val (selected_sessions, deps) =
wenzelm@66745
   409
    {
wenzelm@67030
   410
      val selected_sessions0 = full_sessions.selection(selection1)
wenzelm@66745
   411
      val deps0 =
wenzelm@66962
   412
        Sessions.deps(selected_sessions0, full_sessions.global_theories,
wenzelm@66962
   413
          progress = progress, inlined_files = true, verbose = verbose,
wenzelm@66962
   414
          list_files = list_files, check_keywords = check_keywords).check_errors
wenzelm@66745
   415
wenzelm@66841
   416
      if (soft_build && !fresh_build) {
wenzelm@66745
   417
        val outdated =
wenzelm@67025
   418
          selected_sessions0.build_topological_order.flatMap(name =>
wenzelm@66745
   419
            store.find_database(name) match {
wenzelm@66745
   420
              case Some(database) =>
wenzelm@66745
   421
                using(SQLite.open_database(database))(store.read_build(_, name)) match {
wenzelm@66745
   422
                  case Some(build)
wenzelm@66747
   423
                  if build.ok && build.sources == sources_stamp(deps0, name) => None
wenzelm@66745
   424
                  case _ => Some(name)
wenzelm@66745
   425
                }
wenzelm@66745
   426
              case None => Some(name)
wenzelm@66745
   427
            })
wenzelm@67025
   428
        val selected_sessions =
wenzelm@66745
   429
          full_sessions.selection(Sessions.Selection(sessions = outdated))
wenzelm@66745
   430
        val deps =
wenzelm@67852
   431
          Sessions.deps(selected_sessions, full_sessions.global_theories,
wenzelm@67852
   432
            progress = progress, inlined_files = true).check_errors
wenzelm@67025
   433
        (selected_sessions, deps)
wenzelm@66745
   434
      }
wenzelm@67025
   435
      else (selected_sessions0, deps0)
wenzelm@66745
   436
    }
wenzelm@66745
   437
wenzelm@66745
   438
wenzelm@66745
   439
    /* check unknown files */
wenzelm@48504
   440
wenzelm@65832
   441
    if (check_unknown_files) {
wenzelm@65832
   442
      val source_files =
wenzelm@65832
   443
        (for {
wenzelm@65832
   444
          (_, base) <- deps.session_bases.iterator
wenzelm@65832
   445
          (path, _) <- base.sources.iterator
wenzelm@65832
   446
        } yield path).toList
wenzelm@67098
   447
      val exclude_files = List(Path.explode("$POLYML_EXE")).map(_.canonical_file)
wenzelm@67098
   448
      val unknown_files =
wenzelm@67782
   449
        Mercurial.check_files(source_files)._2.
wenzelm@67098
   450
          filterNot(path => exclude_files.contains(path.canonical_file))
wenzelm@65832
   451
      if (unknown_files.nonEmpty) {
wenzelm@67782
   452
        progress.echo_warning("Unknown files (not part of the underlying Mercurial repository):" +
wenzelm@65832
   453
          unknown_files.map(path => path.expand.implode).sorted.mkString("\n  ", "\n  ", ""))
wenzelm@65832
   454
      }
wenzelm@65832
   455
    }
wenzelm@65832
   456
wenzelm@51220
   457
wenzelm@51220
   458
    /* main build process */
wenzelm@51220
   459
wenzelm@65831
   460
    val queue = Queue(progress, selected_sessions, store)
wenzelm@65289
   461
wenzelm@62632
   462
    store.prepare_output()
wenzelm@48373
   463
wenzelm@68086
   464
    // cleanup
wenzelm@68086
   465
    def cleanup(name: String, echo: Boolean = false)
wenzelm@68086
   466
    {
wenzelm@68086
   467
      val files =
wenzelm@68086
   468
        List(Path.basic(name), store.database(name), store.log(name), store.log_gz(name)).
wenzelm@68086
   469
          map(store.output_dir + _).filter(_.is_file)
wenzelm@68086
   470
      if (files.nonEmpty) progress.echo_if(echo, "Cleaning " + name + " ...")
wenzelm@68086
   471
      val res = files.map(p => p.file.delete)
wenzelm@68086
   472
      if (!res.forall(ok => ok)) progress.echo_if(echo, name + " FAILED to delete")
wenzelm@68086
   473
    }
wenzelm@48595
   474
    if (clean_build) {
wenzelm@67030
   475
      for (name <- full_sessions.build_descendants(full_sessions.build_selection(selection1))) {
wenzelm@68086
   476
        cleanup(name, echo = true)
wenzelm@48595
   477
      }
wenzelm@48595
   478
    }
wenzelm@48595
   479
wenzelm@48425
   480
    // scheduler loop
lars@63082
   481
    case class Result(
wenzelm@66594
   482
      current: Boolean,
wenzelm@66594
   483
      heap_stamp: Option[String],
wenzelm@66594
   484
      process: Option[Process_Result],
wenzelm@66594
   485
      info: Sessions.Info)
wenzelm@62402
   486
    {
wenzelm@62402
   487
      def ok: Boolean =
wenzelm@62402
   488
        process match {
wenzelm@62402
   489
          case None => false
wenzelm@62402
   490
          case Some(res) => res.rc == 0
wenzelm@62402
   491
        }
wenzelm@62402
   492
    }
wenzelm@48639
   493
wenzelm@56837
   494
    def sleep()
wenzelm@56837
   495
    {
wenzelm@56837
   496
      try { Thread.sleep(500) }
wenzelm@56861
   497
      catch { case Exn.Interrupt() => Exn.Interrupt.impose() }
wenzelm@56837
   498
    }
wenzelm@50366
   499
wenzelm@64265
   500
    val numa_nodes = new NUMA.Nodes(numa_shuffling)
wenzelm@64265
   501
wenzelm@48425
   502
    @tailrec def loop(
wenzelm@48676
   503
      pending: Queue,
wenzelm@62628
   504
      running: Map[String, (List[String], Job)],
wenzelm@48639
   505
      results: Map[String, Result]): Map[String, Result] =
wenzelm@48425
   506
    {
wenzelm@64265
   507
      def used_node(i: Int): Boolean =
wenzelm@64265
   508
        running.iterator.exists(
wenzelm@64265
   509
          { case (_, (_, job)) => job.numa_node.isDefined && job.numa_node.get == i })
wenzelm@64265
   510
wenzelm@48425
   511
      if (pending.is_empty) results
wenzelm@51253
   512
      else {
wenzelm@51253
   513
        if (progress.stopped)
wenzelm@51253
   514
          for ((_, (_, job)) <- running) job.terminate
wenzelm@51253
   515
wenzelm@48674
   516
        running.find({ case (_, (_, job)) => job.is_finished }) match {
wenzelm@62628
   517
          case Some((name, (input_heaps, job))) =>
wenzelm@50367
   518
            //{{{ finish job
wenzelm@48424
   519
wenzelm@62401
   520
            val process_result = job.join
wenzelm@48373
   521
wenzelm@65294
   522
            val log_lines = process_result.out_lines.filterNot(_.startsWith("\f"))
wenzelm@62404
   523
            val process_result_tail =
wenzelm@62404
   524
            {
wenzelm@62409
   525
              val tail = job.info.options.int("process_output_tail")
wenzelm@62632
   526
              process_result.copy(
wenzelm@62632
   527
                out_lines =
wenzelm@68086
   528
                  "(see also " + store.output_log(name).file.toString + ")" ::
wenzelm@65294
   529
                  (if (tail == 0) log_lines else log_lines.drop(log_lines.length - tail max 0)))
wenzelm@62404
   530
            }
wenzelm@62404
   531
wenzelm@66666
   532
wenzelm@66666
   533
            // write log file
wenzelm@62636
   534
            val heap_stamp =
wenzelm@62401
   535
              if (process_result.ok) {
wenzelm@62636
   536
                val heap_stamp =
wenzelm@62704
   537
                  for (path <- job.output_path if path.is_file)
wenzelm@62704
   538
                    yield Sessions.write_heap_digest(path)
wenzelm@48639
   539
wenzelm@68086
   540
                File.write_gzip(store.output_log_gz(name), terminate_lines(log_lines))
wenzelm@48639
   541
wenzelm@62636
   542
                heap_stamp
wenzelm@48639
   543
              }
wenzelm@48639
   544
              else {
wenzelm@68086
   545
                File.write(store.output_log(name), terminate_lines(log_lines))
wenzelm@48639
   546
wenzelm@62636
   547
                None
wenzelm@48639
   548
              }
wenzelm@65291
   549
wenzelm@65291
   550
            // write database
wenzelm@65291
   551
            {
wenzelm@66944
   552
              val build_log =
wenzelm@66944
   553
                Build_Log.Log_File(name, process_result.out_lines).
wenzelm@66944
   554
                  parse_session_info(
wenzelm@66944
   555
                    command_timings = true,
wenzelm@66944
   556
                    theory_timings = true,
wenzelm@66944
   557
                    ml_statistics = true,
wenzelm@66944
   558
                    task_statistics = true)
wenzelm@66944
   559
wenzelm@68086
   560
              using(SQLite.open_database(store.output_database(name)))(db =>
wenzelm@65318
   561
                store.write_session_info(db, name,
wenzelm@65291
   562
                  build_log =
wenzelm@66944
   563
                    if (process_result.timeout) build_log.error("Timeout") else build_log,
wenzelm@65291
   564
                  build =
wenzelm@66746
   565
                    Session_Info(sources_stamp(deps, name), input_heaps, heap_stamp,
wenzelm@66744
   566
                      process_result.rc)))
wenzelm@65291
   567
            }
wenzelm@65291
   568
wenzelm@66666
   569
            // messages
wenzelm@66666
   570
            process_result.err_lines.foreach(progress.echo(_))
wenzelm@66666
   571
wenzelm@66666
   572
            if (process_result.ok)
wenzelm@66666
   573
              progress.echo("Finished " + name + " (" + process_result.timing.message_resources + ")")
wenzelm@66666
   574
            else {
wenzelm@66666
   575
              progress.echo(name + " FAILED")
wenzelm@66666
   576
              if (!process_result.interrupted) progress.echo(process_result_tail.out)
wenzelm@66666
   577
            }
wenzelm@66666
   578
wenzelm@50707
   579
            loop(pending - name, running - name,
lars@63082
   580
              results + (name -> Result(false, heap_stamp, Some(process_result_tail), job.info)))
wenzelm@50367
   581
            //}}}
wenzelm@60215
   582
          case None if running.size < (max_jobs max 1) =>
wenzelm@50367
   583
            //{{{ check/start next job
wenzelm@48547
   584
            pending.dequeue(running.isDefinedAt(_)) match {
wenzelm@48547
   585
              case Some((name, info)) =>
wenzelm@66848
   586
                val ancestor_results =
wenzelm@66848
   587
                  selected_sessions.build_requirements(List(name)).filterNot(_ == name).
wenzelm@66848
   588
                    map(results(_))
wenzelm@62636
   589
                val ancestor_heaps = ancestor_results.flatMap(_.heap_stamp)
wenzelm@62628
   590
wenzelm@65360
   591
                val do_output = build_heap || Sessions.is_pure(name) || queue.is_inner(name)
wenzelm@48547
   592
wenzelm@62636
   593
                val (current, heap_stamp) =
wenzelm@48547
   594
                {
wenzelm@65291
   595
                  store.find_database_heap(name) match {
wenzelm@65291
   596
                    case Some((database, heap_stamp)) =>
wenzelm@65320
   597
                      using(SQLite.open_database(database))(store.read_build(_, name)) match {
wenzelm@65291
   598
                        case Some(build) =>
wenzelm@62628
   599
                          val current =
wenzelm@66841
   600
                            !fresh_build &&
wenzelm@66747
   601
                            build.ok &&
wenzelm@66745
   602
                            build.sources == sources_stamp(deps, name) &&
wenzelm@65291
   603
                            build.input_heaps == ancestor_heaps &&
wenzelm@65291
   604
                            build.output_heap == heap_stamp &&
wenzelm@66595
   605
                            !(do_output && heap_stamp.isEmpty)
wenzelm@62636
   606
                          (current, heap_stamp)
wenzelm@62636
   607
                        case None => (false, None)
wenzelm@48547
   608
                      }
wenzelm@62636
   609
                    case None => (false, None)
wenzelm@48504
   610
                  }
wenzelm@48547
   611
                }
wenzelm@62628
   612
                val all_current = current && ancestor_results.forall(_.current)
wenzelm@48528
   613
wenzelm@48547
   614
                if (all_current)
wenzelm@62402
   615
                  loop(pending - name, running,
lars@63082
   616
                    results + (name -> Result(true, heap_stamp, Some(Process_Result(0)), info)))
wenzelm@48678
   617
                else if (no_build) {
wenzelm@50366
   618
                  if (verbose) progress.echo("Skipping " + name + " ...")
wenzelm@62402
   619
                  loop(pending - name, running,
lars@63082
   620
                    results + (name -> Result(false, heap_stamp, Some(Process_Result(1)), info)))
wenzelm@48678
   621
                }
wenzelm@62628
   622
                else if (ancestor_results.forall(_.ok) && !progress.stopped) {
wenzelm@68086
   623
                  progress.echo((if (do_output) "Building " else "Running ") + name + " ...")
wenzelm@68086
   624
wenzelm@68086
   625
                  cleanup(name)
wenzelm@68086
   626
                  using(SQLite.open_database(store.output_database(name)))(db =>
wenzelm@68086
   627
                    store.init_session_info(db, name))
wenzelm@68086
   628
wenzelm@64265
   629
                  val numa_node = numa_nodes.next(used_node(_))
wenzelm@51220
   630
                  val job =
wenzelm@65415
   631
                    new Job(progress, name, info, selected_sessions, deps, store, do_output,
wenzelm@65415
   632
                      verbose, pide, numa_node, queue.command_timings(name))
wenzelm@62628
   633
                  loop(pending, running + (name -> (ancestor_heaps, job)), results)
wenzelm@48547
   634
                }
wenzelm@48547
   635
                else {
wenzelm@50366
   636
                  progress.echo(name + " CANCELLED")
wenzelm@65253
   637
                  loop(pending - name, running,
wenzelm@65253
   638
                    results + (name -> Result(false, heap_stamp, None, info)))
wenzelm@48547
   639
                }
wenzelm@48547
   640
              case None => sleep(); loop(pending, running, results)
wenzelm@48425
   641
            }
wenzelm@50367
   642
            ///}}}
wenzelm@48425
   643
          case None => sleep(); loop(pending, running, results)
wenzelm@48373
   644
        }
wenzelm@51253
   645
      }
wenzelm@48425
   646
    }
wenzelm@48425
   647
wenzelm@51220
   648
wenzelm@51220
   649
    /* build results */
wenzelm@51220
   650
wenzelm@62641
   651
    val results0 =
wenzelm@48583
   652
      if (deps.is_empty) {
wenzelm@65827
   653
        progress.echo_warning("Nothing to build")
wenzelm@50707
   654
        Map.empty[String, Result]
wenzelm@48583
   655
      }
wenzelm@48583
   656
      else loop(queue, Map.empty, Map.empty)
wenzelm@48583
   657
wenzelm@62641
   658
    val results =
wenzelm@64265
   659
      new Results(
wenzelm@64265
   660
        (for ((name, result) <- results0.iterator)
wenzelm@64265
   661
          yield (name, (result.process, result.info))).toMap)
wenzelm@62641
   662
wenzelm@62641
   663
    if (results.rc != 0 && (verbose || !no_build)) {
wenzelm@62641
   664
      val unfinished =
wenzelm@62641
   665
        (for {
wenzelm@62641
   666
          name <- results.sessions.iterator
wenzelm@62641
   667
          if !results(name).ok
wenzelm@62641
   668
         } yield name).toList.sorted
wenzelm@62641
   669
      progress.echo("Unfinished session(s): " + commas(unfinished))
wenzelm@62641
   670
    }
wenzelm@62641
   671
wenzelm@51418
   672
wenzelm@51418
   673
    /* global browser info */
wenzelm@51418
   674
wenzelm@51418
   675
    if (!no_build) {
wenzelm@51418
   676
      val browser_chapters =
wenzelm@51418
   677
        (for {
wenzelm@62641
   678
          (name, result) <- results0.iterator
wenzelm@62944
   679
          if result.ok
wenzelm@65415
   680
          info = full_sessions(name)
wenzelm@51418
   681
          if info.options.bool("browser_info")
wenzelm@51418
   682
        } yield (info.chapter, (name, info.description))).toList.groupBy(_._1).
wenzelm@51418
   683
            map({ case (chapter, es) => (chapter, es.map(_._2)) }).filterNot(_._2.isEmpty)
wenzelm@51418
   684
wenzelm@51418
   685
      for ((chapter, entries) <- browser_chapters)
wenzelm@62632
   686
        Present.update_chapter_index(store.browser_info, chapter, entries)
wenzelm@51418
   687
wenzelm@62632
   688
      if (browser_chapters.nonEmpty) Present.make_global_index(store.browser_info)
wenzelm@51418
   689
    }
wenzelm@51418
   690
wenzelm@62641
   691
    results
wenzelm@48341
   692
  }
wenzelm@48341
   693
wenzelm@48341
   694
wenzelm@62833
   695
  /* Isabelle tool wrapper */
wenzelm@48341
   696
wenzelm@62833
   697
  val isabelle_tool = Isabelle_Tool("build", "build and manage Isabelle sessions", args =>
wenzelm@48341
   698
  {
wenzelm@62833
   699
    val build_options = Word.explode(Isabelle_System.getenv("ISABELLE_BUILD_OPTIONS"))
wenzelm@62590
   700
wenzelm@66737
   701
    var base_sessions: List[String] = Nil
wenzelm@62833
   702
    var select_dirs: List[Path] = Nil
wenzelm@64265
   703
    var numa_shuffling = false
wenzelm@65308
   704
    var pide = false
wenzelm@62833
   705
    var requirements = false
wenzelm@66745
   706
    var soft_build = false
wenzelm@62833
   707
    var exclude_session_groups: List[String] = Nil
wenzelm@62833
   708
    var all_sessions = false
wenzelm@62833
   709
    var build_heap = false
wenzelm@62833
   710
    var clean_build = false
wenzelm@62833
   711
    var dirs: List[Path] = Nil
wenzelm@66841
   712
    var fresh_build = false
wenzelm@62833
   713
    var session_groups: List[String] = Nil
wenzelm@62833
   714
    var max_jobs = 1
wenzelm@62833
   715
    var check_keywords: Set[String] = Set.empty
wenzelm@62833
   716
    var list_files = false
wenzelm@62833
   717
    var no_build = false
wenzelm@67847
   718
    var options = Options.init(opts = build_options)
wenzelm@62833
   719
    var system_mode = false
wenzelm@62833
   720
    var verbose = false
wenzelm@62833
   721
    var exclude_sessions: List[String] = Nil
wenzelm@62590
   722
wenzelm@62833
   723
    val getopts = Getopts("""
wenzelm@62590
   724
Usage: isabelle build [OPTIONS] [SESSIONS ...]
wenzelm@62590
   725
wenzelm@62590
   726
  Options are:
wenzelm@66737
   727
    -B NAME      include session NAME and all descendants
wenzelm@62590
   728
    -D DIR       include session directory and select its sessions
wenzelm@64265
   729
    -N           cyclic shuffling of NUMA CPU nodes (performance tuning)
wenzelm@65308
   730
    -P           build via PIDE protocol
wenzelm@62590
   731
    -R           operate on requirements of selected sessions
wenzelm@66745
   732
    -S           soft build: only observe changes of sources, not heap images
wenzelm@62590
   733
    -X NAME      exclude sessions from group NAME and all descendants
wenzelm@62590
   734
    -a           select all sessions
wenzelm@62590
   735
    -b           build heap images
wenzelm@62590
   736
    -c           clean build
wenzelm@62590
   737
    -d DIR       include session directory
wenzelm@66841
   738
    -f           fresh build
wenzelm@62590
   739
    -g NAME      select session group NAME
wenzelm@62590
   740
    -j INT       maximum number of parallel jobs (default 1)
wenzelm@62590
   741
    -k KEYWORD   check theory sources for conflicts with proposed keywords
wenzelm@62590
   742
    -l           list session source files
wenzelm@62590
   743
    -n           no build -- test dependencies only
wenzelm@62590
   744
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
wenzelm@62590
   745
    -s           system build mode: produce output in ISABELLE_HOME
wenzelm@62590
   746
    -v           verbose
wenzelm@62590
   747
    -x NAME      exclude session NAME and all descendants
wenzelm@62590
   748
wenzelm@62596
   749
  Build and manage Isabelle sessions, depending on implicit settings:
wenzelm@62596
   750
wenzelm@64455
   751
""" + Library.prefix_lines("  ", Build_Log.Settings.show()) + "\n",
wenzelm@66737
   752
      "B:" -> (arg => base_sessions = base_sessions ::: List(arg)),
wenzelm@62833
   753
      "D:" -> (arg => select_dirs = select_dirs ::: List(Path.explode(arg))),
wenzelm@64265
   754
      "N" -> (_ => numa_shuffling = true),
wenzelm@65308
   755
      "P" -> (_ => pide = true),
wenzelm@62833
   756
      "R" -> (_ => requirements = true),
wenzelm@66745
   757
      "S" -> (_ => soft_build = true),
wenzelm@62833
   758
      "X:" -> (arg => exclude_session_groups = exclude_session_groups ::: List(arg)),
wenzelm@62833
   759
      "a" -> (_ => all_sessions = true),
wenzelm@62833
   760
      "b" -> (_ => build_heap = true),
wenzelm@62833
   761
      "c" -> (_ => clean_build = true),
wenzelm@62833
   762
      "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
wenzelm@66841
   763
      "f" -> (_ => fresh_build = true),
wenzelm@62833
   764
      "g:" -> (arg => session_groups = session_groups ::: List(arg)),
wenzelm@63805
   765
      "j:" -> (arg => max_jobs = Value.Int.parse(arg)),
wenzelm@62833
   766
      "k:" -> (arg => check_keywords = check_keywords + arg),
wenzelm@62833
   767
      "l" -> (_ => list_files = true),
wenzelm@62833
   768
      "n" -> (_ => no_build = true),
wenzelm@62833
   769
      "o:" -> (arg => options = options + arg),
wenzelm@62833
   770
      "s" -> (_ => system_mode = true),
wenzelm@62833
   771
      "v" -> (_ => verbose = true),
wenzelm@62833
   772
      "x:" -> (arg => exclude_sessions = exclude_sessions ::: List(arg)))
wenzelm@62590
   773
wenzelm@62833
   774
    val sessions = getopts(args)
wenzelm@62590
   775
wenzelm@64115
   776
    val progress = new Console_Progress(verbose = verbose)
wenzelm@62590
   777
wenzelm@64140
   778
    val start_date = Date.now()
wenzelm@64140
   779
wenzelm@62833
   780
    if (verbose) {
wenzelm@62833
   781
      progress.echo(
wenzelm@64155
   782
        "Started at " + Build_Log.print_date(start_date) +
wenzelm@64140
   783
          " (" + Isabelle_System.getenv("ML_IDENTIFIER") + " on " + Isabelle_System.hostname() +")")
wenzelm@64081
   784
      progress.echo(Build_Log.Settings.show() + "\n")
wenzelm@62833
   785
    }
wenzelm@62590
   786
wenzelm@62833
   787
    val results =
wenzelm@62833
   788
      progress.interrupt_handler {
wenzelm@67846
   789
        build(options,
wenzelm@67846
   790
          progress = progress,
wenzelm@65832
   791
          check_unknown_files = Mercurial.is_repository(Path.explode("~~")),
wenzelm@63224
   792
          build_heap = build_heap,
wenzelm@63224
   793
          clean_build = clean_build,
wenzelm@63224
   794
          dirs = dirs,
wenzelm@63224
   795
          select_dirs = select_dirs,
wenzelm@65831
   796
          numa_shuffling = NUMA.enabled_warning(progress, numa_shuffling),
wenzelm@63224
   797
          max_jobs = max_jobs,
wenzelm@63224
   798
          list_files = list_files,
wenzelm@63224
   799
          check_keywords = check_keywords,
wenzelm@66841
   800
          fresh_build = fresh_build,
wenzelm@63224
   801
          no_build = no_build,
wenzelm@66745
   802
          soft_build = soft_build,
wenzelm@63224
   803
          system_mode = system_mode,
wenzelm@63224
   804
          verbose = verbose,
wenzelm@65308
   805
          pide = pide,
wenzelm@63224
   806
          requirements = requirements,
wenzelm@63224
   807
          all_sessions = all_sessions,
wenzelm@66737
   808
          base_sessions = base_sessions,
wenzelm@63224
   809
          exclude_session_groups = exclude_session_groups,
wenzelm@63224
   810
          exclude_sessions = exclude_sessions,
wenzelm@63224
   811
          session_groups = session_groups,
wenzelm@63224
   812
          sessions = sessions)
wenzelm@62833
   813
      }
wenzelm@64140
   814
    val end_date = Date.now()
wenzelm@64140
   815
    val elapsed_time = end_date.time - start_date.time
wenzelm@62590
   816
wenzelm@62833
   817
    if (verbose) {
wenzelm@64155
   818
      progress.echo("\nFinished at " + Build_Log.print_date(end_date))
wenzelm@62833
   819
    }
wenzelm@62590
   820
wenzelm@62833
   821
    val total_timing =
wenzelm@62833
   822
      (Timing.zero /: results.sessions.iterator.map(a => results(a).timing))(_ + _).
wenzelm@62833
   823
        copy(elapsed = elapsed_time)
wenzelm@62833
   824
    progress.echo(total_timing.message_resources)
wenzelm@62590
   825
wenzelm@62833
   826
    sys.exit(results.rc)
wenzelm@62833
   827
  })
wenzelm@48276
   828
}