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