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