src/Pure/Tools/build.scala
author wenzelm
Sat Mar 18 21:24:54 2017 +0100 (2017-03-18)
changeset 65315 c7097ccbffb7
parent 65314 944758d6462e
child 65317 b9f5cd845616
permissions -rw-r--r--
clarified signature;
     1 /*  Title:      Pure/Tools/build.scala
     2     Author:     Makarius
     3     Options:    :folding=explicit:
     4 
     5 Build and manage Isabelle sessions.
     6 */
     7 
     8 package isabelle
     9 
    10 
    11 import java.io.{BufferedInputStream, FileInputStream,
    12   BufferedReader, InputStreamReader, IOException}
    13 import java.util.zip.GZIPInputStream
    14 
    15 import scala.collection.SortedSet
    16 import scala.collection.mutable
    17 import scala.annotation.tailrec
    18 
    19 
    20 object Build
    21 {
    22   /** auxiliary **/
    23 
    24   /* persistent build info */
    25 
    26   sealed case class Session_Info(
    27     sources: List[String],
    28     input_heaps: List[String],
    29     output_heap: Option[String],
    30     return_code: Int)
    31 
    32 
    33   /* queue with scheduling information */
    34 
    35   private object Queue
    36   {
    37     def load_timings(store: Sessions.Store, name: String): (List[Properties.T], Double) =
    38     {
    39       val no_timings: (List[Properties.T], Double) = (Nil, 0.0)
    40 
    41       store.find_database(name) match {
    42         case None => no_timings
    43         case Some(database) =>
    44           def ignore_error(msg: String) =
    45           {
    46             Output.warning("Ignoring bad database: " + database + (if (msg == "") "" else "\n" + msg))
    47             no_timings
    48           }
    49           try {
    50             using(SQLite.open_database(database))(db =>
    51             {
    52               val build_log = store.read_build_log(db, name, command_timings = true)
    53               val session_timing = Markup.Elapsed.unapply(build_log.session_timing) getOrElse 0.0
    54               (build_log.command_timings, session_timing)
    55             })
    56           }
    57           catch {
    58             case ERROR(msg) => ignore_error(msg)
    59             case exn: java.lang.Error => ignore_error(Exn.message(exn))
    60             case _: XML.Error => ignore_error("")
    61           }
    62       }
    63     }
    64 
    65     def apply(tree: Sessions.Tree, store: Sessions.Store): Queue =
    66     {
    67       val graph = tree.graph
    68       val sessions = graph.keys
    69 
    70       val timings = sessions.map(name => (name, load_timings(store, name)))
    71       val command_timings =
    72         Map(timings.map({ case (name, (ts, _)) => (name, ts) }): _*).withDefaultValue(Nil)
    73       val session_timing =
    74         Map(timings.map({ case (name, (_, t)) => (name, t) }): _*).withDefaultValue(0.0)
    75 
    76       def outdegree(name: String): Int = graph.imm_succs(name).size
    77 
    78       object Ordering extends scala.math.Ordering[String]
    79       {
    80         def compare_timing(name1: String, name2: String): Int =
    81         {
    82           val t1 = session_timing(name1)
    83           val t2 = session_timing(name2)
    84           if (t1 == 0.0 || t2 == 0.0) 0
    85           else t1 compare t2
    86         }
    87 
    88         def compare(name1: String, name2: String): Int =
    89           outdegree(name2) compare outdegree(name1) match {
    90             case 0 =>
    91               compare_timing(name2, name1) match {
    92                 case 0 =>
    93                   tree(name2).timeout compare tree(name1).timeout match {
    94                     case 0 => name1 compare name2
    95                     case ord => ord
    96                   }
    97                 case ord => ord
    98               }
    99             case ord => ord
   100           }
   101       }
   102 
   103       new Queue(graph, SortedSet(sessions: _*)(Ordering), command_timings)
   104     }
   105   }
   106 
   107   private class Queue(
   108     graph: Graph[String, Sessions.Info],
   109     order: SortedSet[String],
   110     val command_timings: String => List[Properties.T])
   111   {
   112     def is_inner(name: String): Boolean = !graph.is_maximal(name)
   113 
   114     def is_empty: Boolean = graph.is_empty
   115 
   116     def - (name: String): Queue =
   117       new Queue(graph.del_node(name),
   118         order - name,  // FIXME scala-2.10.0 TreeSet problem!?
   119         command_timings)
   120 
   121     def dequeue(skip: String => Boolean): Option[(String, Sessions.Info)] =
   122     {
   123       val it = order.iterator.dropWhile(name =>
   124         skip(name)
   125           || !graph.defined(name)  // FIXME scala-2.10.0 TreeSet problem!?
   126           || !graph.is_minimal(name))
   127       if (it.hasNext) { val name = it.next; Some((name, graph.get_node(name))) }
   128       else None
   129     }
   130   }
   131 
   132 
   133   /* PIDE protocol handler */
   134 
   135   class Handler(progress: Progress, session: Session, session_name: String)
   136     extends Session.Protocol_Handler
   137   {
   138     val result_error: Promise[String] = Future.promise
   139 
   140     override def exit() { result_error.cancel }
   141 
   142     private def build_session_finished(msg: Prover.Protocol_Output): Boolean =
   143     {
   144       val error_message =
   145         try { Pretty.string_of(YXML.parse_body(Symbol.decode(msg.text))) }
   146         catch { case ERROR(msg) => msg }
   147       result_error.fulfill(error_message)
   148       session.send_stop()
   149       true
   150     }
   151 
   152     private def loading_theory(msg: Prover.Protocol_Output): Boolean =
   153       msg.properties match {
   154         case Markup.Loading_Theory(name) =>
   155           progress.theory(session_name, name)
   156           true
   157         case _ => false
   158       }
   159 
   160     val functions =
   161       List(
   162         Markup.BUILD_SESSION_FINISHED -> build_session_finished _,
   163         Markup.LOADING_THEORY -> loading_theory _)
   164   }
   165 
   166 
   167   /* job: running prover process */
   168 
   169   private class Job(progress: Progress,
   170     name: String,
   171     val info: Sessions.Info,
   172     tree: Sessions.Tree,
   173     deps: Sessions.Deps,
   174     store: Sessions.Store,
   175     do_output: Boolean,
   176     verbose: Boolean,
   177     pide: Boolean,
   178     val numa_node: Option[Int],
   179     command_timings: List[Properties.T])
   180   {
   181     val output = store.output_dir + Path.basic(name)
   182     def output_path: Option[Path] = if (do_output) Some(output) else None
   183     output.file.delete
   184 
   185     val options =
   186       numa_node match {
   187         case None => info.options
   188         case Some(n) => info.options.string("ML_process_policy") = NUMA.policy(n)
   189       }
   190 
   191     private val graph_file = Isabelle_System.tmp_file("session_graph", "pdf")
   192     try { isabelle.graphview.Graph_File.write(options, graph_file, deps(name).session_graph) }
   193     catch { case ERROR(_) => /*error should be exposed in ML*/ }
   194 
   195     private val future_result: Future[Process_Result] =
   196       Future.thread("build") {
   197         val parent = info.parent.getOrElse("")
   198 
   199         val args_yxml =
   200           YXML.string_of_body(
   201             {
   202               val theories = info.theories.map(x => (x._2, x._3))
   203               import XML.Encode._
   204               pair(list(pair(string, int)), pair(list(properties), pair(bool, pair(bool,
   205                 pair(Path.encode, pair(list(pair(Path.encode, Path.encode)), pair(string,
   206                 pair(string, pair(string, pair(string, pair(Path.encode,
   207                 list(pair(Options.encode, list(Path.encode))))))))))))))(
   208               (Symbol.codes, (command_timings, (do_output, (verbose,
   209                 (store.browser_info, (info.document_files, (File.standard_path(graph_file),
   210                 (parent, (info.chapter, (name, (Path.current,
   211                 theories))))))))))))
   212             })
   213 
   214         val env =
   215           Isabelle_System.settings() +
   216             ("ISABELLE_ML_DEBUGGER" -> options.bool("ML_debugger").toString)
   217 
   218         def save_heap: String =
   219           "ML_Heap.share_common_data (); ML_Heap.save_child " +
   220             ML_Syntax.print_string0(File.platform_path(output))
   221 
   222         if (pide && !Sessions.pure_name(name)) {
   223           val resources = new Resources(deps(parent))
   224           val session = new Session(options, resources)
   225           val handler = new Handler(progress, session, name)
   226           session.init_protocol_handler(handler)
   227 
   228           val session_result = Future.promise[Int]
   229 
   230           Isabelle_Process.start(session, options, logic = parent,
   231             cwd = info.dir.file, env = env, tree = Some(tree), store = store,
   232             phase_changed =
   233             {
   234               case Session.Ready => session.protocol_command("build_session", args_yxml)
   235               case Session.Terminated(rc) => session_result.fulfill(rc)
   236               case _ =>
   237             })
   238 
   239           val rc = session_result.join
   240 
   241           handler.result_error.join match {
   242             case "" => Process_Result(rc)
   243             case msg => Process_Result(rc max 1, out_lines = split_lines(Output.error_text(msg)))
   244           }
   245         }
   246         else {
   247           val args_file = Isabelle_System.tmp_file("build")
   248           File.write(args_file, args_yxml)
   249 
   250           val eval =
   251             "Command_Line.tool0 (fn () => (" +
   252             "Build.build " + ML_Syntax.print_string0(File.standard_path(args_file)) +
   253             (if (do_output) "; " + save_heap else "") + "));"
   254 
   255           val process =
   256             if (Sessions.pure_name(name)) {
   257               ML_Process(options, raw_ml_system = true, cwd = info.dir.file,
   258                 args =
   259                   (for ((root, _) <- Thy_Header.ml_roots) yield List("--use", root)).flatten :::
   260                   List("--eval", eval),
   261                 env = env, tree = Some(tree), store = store, cleanup = () => args_file.delete)
   262             }
   263             else {
   264               ML_Process(options, parent, List("--eval", eval), cwd = info.dir.file,
   265                 env = env, tree = Some(tree), store = store, cleanup = () => args_file.delete)
   266             }
   267 
   268           process.result(
   269             progress_stdout = (line: String) =>
   270               Library.try_unprefix("\floading_theory = ", line) match {
   271                 case Some(theory) => progress.theory(name, theory)
   272                 case None =>
   273               },
   274             progress_limit =
   275               options.int("process_output_limit") match {
   276                 case 0 => None
   277                 case m => Some(m * 1000000L)
   278               },
   279             strict = false)
   280         }
   281       }
   282 
   283     def terminate: Unit = future_result.cancel
   284     def is_finished: Boolean = future_result.is_finished
   285 
   286     @volatile private var was_timeout = false
   287     private val timeout_request: Option[Event_Timer.Request] =
   288     {
   289       if (info.timeout > Time.zero)
   290         Some(Event_Timer.request(Time.now() + info.timeout) { terminate; was_timeout = true })
   291       else None
   292     }
   293 
   294     def join: Process_Result =
   295     {
   296       val result = future_result.join
   297 
   298       if (result.ok)
   299         Present.finish(progress, store.browser_info, graph_file, info, name)
   300 
   301       graph_file.delete
   302       timeout_request.foreach(_.cancel)
   303 
   304       if (result.interrupted) {
   305         if (was_timeout) result.error(Output.error_text("Timeout")).was_timeout
   306         else result.error(Output.error_text("Interrupt"))
   307       }
   308       else result
   309     }
   310   }
   311 
   312 
   313 
   314   /** build with results **/
   315 
   316   class Results private[Build](results: Map[String, (Option[Process_Result], Sessions.Info)])
   317   {
   318     def sessions: Set[String] = results.keySet
   319     def cancelled(name: String): Boolean = results(name)._1.isEmpty
   320     def apply(name: String): Process_Result = results(name)._1.getOrElse(Process_Result(1))
   321     def info(name: String): Sessions.Info = results(name)._2
   322     val rc =
   323       (0 /: results.iterator.map(
   324         { case (_, (Some(r), _)) => r.rc case (_, (None, _)) => 1 }))(_ max _)
   325     def ok: Boolean = rc == 0
   326 
   327     override def toString: String = rc.toString
   328   }
   329 
   330   def build(
   331     options: Options,
   332     progress: Progress = No_Progress,
   333     build_heap: Boolean = false,
   334     clean_build: Boolean = false,
   335     dirs: List[Path] = Nil,
   336     select_dirs: List[Path] = Nil,
   337     numa_shuffling: Boolean = false,
   338     max_jobs: Int = 1,
   339     list_files: Boolean = false,
   340     check_keywords: Set[String] = Set.empty,
   341     no_build: Boolean = false,
   342     system_mode: Boolean = false,
   343     verbose: Boolean = false,
   344     pide: Boolean = false,
   345     requirements: Boolean = false,
   346     all_sessions: Boolean = false,
   347     exclude_session_groups: List[String] = Nil,
   348     exclude_sessions: List[String] = Nil,
   349     session_groups: List[String] = Nil,
   350     sessions: List[String] = Nil): Results =
   351   {
   352     build_selection(
   353       options = options,
   354       progress = progress,
   355       build_heap = build_heap,
   356       clean_build = clean_build,
   357       dirs = dirs,
   358       select_dirs = select_dirs,
   359       numa_shuffling = numa_shuffling,
   360       max_jobs = max_jobs,
   361       list_files = list_files,
   362       check_keywords = check_keywords,
   363       no_build = no_build,
   364       system_mode = system_mode,
   365       verbose = verbose,
   366       pide = pide,
   367       selection = { full_tree =>
   368         full_tree.selection(requirements, all_sessions,
   369           exclude_session_groups, exclude_sessions, session_groups, sessions) })
   370   }
   371 
   372   def build_selection(
   373     options: Options,
   374     progress: Progress = No_Progress,
   375     build_heap: Boolean = false,
   376     clean_build: Boolean = false,
   377     dirs: List[Path] = Nil,
   378     select_dirs: List[Path] = Nil,
   379     numa_shuffling: Boolean = false,
   380     max_jobs: Int = 1,
   381     list_files: Boolean = false,
   382     check_keywords: Set[String] = Set.empty,
   383     no_build: Boolean = false,
   384     system_mode: Boolean = false,
   385     verbose: Boolean = false,
   386     pide: Boolean = false,
   387     selection: Sessions.Tree => (List[String], Sessions.Tree) =
   388       (_.selection(all_sessions = true))): Results =
   389   {
   390     /* session selection and dependencies */
   391 
   392     val build_options = options.int.update("completion_limit", 0).bool.update("ML_statistics", true)
   393     val full_tree = Sessions.load(build_options, dirs, select_dirs)
   394     val (selected, selected_tree) = selection(full_tree)
   395     val deps =
   396       Sessions.dependencies(progress, true, verbose, list_files, check_keywords, selected_tree)
   397 
   398     def sources_stamp(name: String): List[String] =
   399       (selected_tree(name).meta_digest :: deps.sources(name)).map(_.toString).sorted
   400 
   401 
   402     /* main build process */
   403 
   404     val store = Sessions.store(system_mode)
   405     val queue = Queue(selected_tree, store)
   406 
   407     store.prepare_output()
   408 
   409     // optional cleanup
   410     if (clean_build) {
   411       for (name <- full_tree.graph.all_succs(selected)) {
   412         val files =
   413           List(Path.basic(name), store.database(name), store.log(name), store.log_gz(name)).
   414             map(store.output_dir + _).filter(_.is_file)
   415         if (files.nonEmpty) progress.echo("Cleaning " + name + " ...")
   416         if (!files.forall(p => p.file.delete)) progress.echo(name + " FAILED to delete")
   417       }
   418     }
   419 
   420     // scheduler loop
   421     case class Result(
   422       current: Boolean, heap_stamp: Option[String],
   423       process: Option[Process_Result], info: Sessions.Info)
   424     {
   425       def ok: Boolean =
   426         process match {
   427           case None => false
   428           case Some(res) => res.rc == 0
   429         }
   430     }
   431 
   432     def sleep()
   433     {
   434       try { Thread.sleep(500) }
   435       catch { case Exn.Interrupt() => Exn.Interrupt.impose() }
   436     }
   437 
   438     val numa_nodes = new NUMA.Nodes(numa_shuffling)
   439 
   440     @tailrec def loop(
   441       pending: Queue,
   442       running: Map[String, (List[String], Job)],
   443       results: Map[String, Result]): Map[String, Result] =
   444     {
   445       def used_node(i: Int): Boolean =
   446         running.iterator.exists(
   447           { case (_, (_, job)) => job.numa_node.isDefined && job.numa_node.get == i })
   448 
   449       if (pending.is_empty) results
   450       else {
   451         if (progress.stopped)
   452           for ((_, (_, job)) <- running) job.terminate
   453 
   454         running.find({ case (_, (_, job)) => job.is_finished }) match {
   455           case Some((name, (input_heaps, job))) =>
   456             //{{{ finish job
   457 
   458             val process_result = job.join
   459             process_result.err_lines.foreach(progress.echo(_))
   460             if (process_result.ok)
   461               progress.echo("Finished " + name + " (" + process_result.timing.message_resources + ")")
   462 
   463             val log_lines = process_result.out_lines.filterNot(_.startsWith("\f"))
   464             val process_result_tail =
   465             {
   466               val tail = job.info.options.int("process_output_tail")
   467               process_result.copy(
   468                 out_lines =
   469                   "(see also " + (store.output_dir + store.log(name)).file.toString + ")" ::
   470                   (if (tail == 0) log_lines else log_lines.drop(log_lines.length - tail max 0)))
   471             }
   472 
   473             val heap_stamp =
   474               if (process_result.ok) {
   475                 (store.output_dir + store.log(name)).file.delete
   476                 val heap_stamp =
   477                   for (path <- job.output_path if path.is_file)
   478                     yield Sessions.write_heap_digest(path)
   479 
   480                 File.write_gzip(store.output_dir + store.log_gz(name), terminate_lines(log_lines))
   481 
   482                 heap_stamp
   483               }
   484               else {
   485                 (store.output_dir + Path.basic(name)).file.delete
   486                 (store.output_dir + store.log_gz(name)).file.delete
   487 
   488                 File.write(store.output_dir + store.log(name), terminate_lines(log_lines))
   489                 progress.echo(name + " FAILED")
   490                 if (!process_result.interrupted) progress.echo(process_result_tail.out)
   491 
   492                 None
   493               }
   494 
   495             // write database
   496             {
   497               val database = store.output_dir + store.database(name)
   498               database.file.delete
   499 
   500               using(SQLite.open_database(database))(db =>
   501                 store.write_session_info(db,
   502                   build_log =
   503                     Build_Log.Log_File(name, process_result.out_lines).
   504                       parse_session_info(name,
   505                         command_timings = true, ml_statistics = true, task_statistics = true),
   506                   build =
   507                     Session_Info(sources_stamp(name), input_heaps, heap_stamp, process_result.rc)))
   508             }
   509 
   510             loop(pending - name, running - name,
   511               results + (name -> Result(false, heap_stamp, Some(process_result_tail), job.info)))
   512             //}}}
   513           case None if running.size < (max_jobs max 1) =>
   514             //{{{ check/start next job
   515             pending.dequeue(running.isDefinedAt(_)) match {
   516               case Some((name, info)) =>
   517                 val ancestor_results = selected_tree.ancestors(name).map(results(_))
   518                 val ancestor_heaps = ancestor_results.flatMap(_.heap_stamp)
   519 
   520                 val do_output = build_heap || Sessions.pure_name(name) || queue.is_inner(name)
   521 
   522                 val (current, heap_stamp) =
   523                 {
   524                   store.find_database_heap(name) match {
   525                     case Some((database, heap_stamp)) =>
   526                       using(SQLite.open_database(database))(store.read_build(_)) match {
   527                         case Some(build) =>
   528                           val current =
   529                             build.sources == sources_stamp(name) &&
   530                             build.input_heaps == ancestor_heaps &&
   531                             build.output_heap == heap_stamp &&
   532                             !(do_output && heap_stamp.isEmpty) &&
   533                             build.return_code == 0
   534                           (current, heap_stamp)
   535                         case None => (false, None)
   536                       }
   537                     case None => (false, None)
   538                   }
   539                 }
   540                 val all_current = current && ancestor_results.forall(_.current)
   541 
   542                 if (all_current)
   543                   loop(pending - name, running,
   544                     results + (name -> Result(true, heap_stamp, Some(Process_Result(0)), info)))
   545                 else if (no_build) {
   546                   if (verbose) progress.echo("Skipping " + name + " ...")
   547                   loop(pending - name, running,
   548                     results + (name -> Result(false, heap_stamp, Some(Process_Result(1)), info)))
   549                 }
   550                 else if (ancestor_results.forall(_.ok) && !progress.stopped) {
   551                   val numa_node = numa_nodes.next(used_node(_))
   552                   progress.echo((if (do_output) "Building " else "Running ") + name + " ...")
   553                   val job =
   554                     new Job(progress, name, info, selected_tree, deps, store, do_output, verbose,
   555                       pide, numa_node, queue.command_timings(name))
   556                   loop(pending, running + (name -> (ancestor_heaps, job)), results)
   557                 }
   558                 else {
   559                   progress.echo(name + " CANCELLED")
   560                   loop(pending - name, running,
   561                     results + (name -> Result(false, heap_stamp, None, info)))
   562                 }
   563               case None => sleep(); loop(pending, running, results)
   564             }
   565             ///}}}
   566           case None => sleep(); loop(pending, running, results)
   567         }
   568       }
   569     }
   570 
   571 
   572     /* build results */
   573 
   574     val results0 =
   575       if (deps.is_empty) {
   576         progress.echo(Output.warning_text("Nothing to build"))
   577         Map.empty[String, Result]
   578       }
   579       else loop(queue, Map.empty, Map.empty)
   580 
   581     val results =
   582       new Results(
   583         (for ((name, result) <- results0.iterator)
   584           yield (name, (result.process, result.info))).toMap)
   585 
   586     if (results.rc != 0 && (verbose || !no_build)) {
   587       val unfinished =
   588         (for {
   589           name <- results.sessions.iterator
   590           if !results(name).ok
   591          } yield name).toList.sorted
   592       progress.echo("Unfinished session(s): " + commas(unfinished))
   593     }
   594 
   595 
   596     /* global browser info */
   597 
   598     if (!no_build) {
   599       val browser_chapters =
   600         (for {
   601           (name, result) <- results0.iterator
   602           if result.ok
   603           info = full_tree(name)
   604           if info.options.bool("browser_info")
   605         } yield (info.chapter, (name, info.description))).toList.groupBy(_._1).
   606             map({ case (chapter, es) => (chapter, es.map(_._2)) }).filterNot(_._2.isEmpty)
   607 
   608       for ((chapter, entries) <- browser_chapters)
   609         Present.update_chapter_index(store.browser_info, chapter, entries)
   610 
   611       if (browser_chapters.nonEmpty) Present.make_global_index(store.browser_info)
   612     }
   613 
   614     results
   615   }
   616 
   617 
   618   /* Isabelle tool wrapper */
   619 
   620   val isabelle_tool = Isabelle_Tool("build", "build and manage Isabelle sessions", args =>
   621   {
   622     val build_options = Word.explode(Isabelle_System.getenv("ISABELLE_BUILD_OPTIONS"))
   623 
   624     var select_dirs: List[Path] = Nil
   625     var numa_shuffling = false
   626     var pide = false
   627     var requirements = false
   628     var exclude_session_groups: List[String] = Nil
   629     var all_sessions = false
   630     var build_heap = false
   631     var clean_build = false
   632     var dirs: List[Path] = Nil
   633     var session_groups: List[String] = Nil
   634     var max_jobs = 1
   635     var check_keywords: Set[String] = Set.empty
   636     var list_files = false
   637     var no_build = false
   638     var options = (Options.init() /: build_options)(_ + _)
   639     var system_mode = false
   640     var verbose = false
   641     var exclude_sessions: List[String] = Nil
   642 
   643     val getopts = Getopts("""
   644 Usage: isabelle build [OPTIONS] [SESSIONS ...]
   645 
   646   Options are:
   647     -D DIR       include session directory and select its sessions
   648     -N           cyclic shuffling of NUMA CPU nodes (performance tuning)
   649     -P           build via PIDE protocol
   650     -R           operate on requirements of selected sessions
   651     -X NAME      exclude sessions from group NAME and all descendants
   652     -a           select all sessions
   653     -b           build heap images
   654     -c           clean build
   655     -d DIR       include session directory
   656     -g NAME      select session group NAME
   657     -j INT       maximum number of parallel jobs (default 1)
   658     -k KEYWORD   check theory sources for conflicts with proposed keywords
   659     -l           list session source files
   660     -n           no build -- test dependencies only
   661     -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
   662     -s           system build mode: produce output in ISABELLE_HOME
   663     -v           verbose
   664     -x NAME      exclude session NAME and all descendants
   665 
   666   Build and manage Isabelle sessions, depending on implicit settings:
   667 
   668 """ + Library.prefix_lines("  ", Build_Log.Settings.show()) + "\n",
   669       "D:" -> (arg => select_dirs = select_dirs ::: List(Path.explode(arg))),
   670       "N" -> (_ => numa_shuffling = true),
   671       "P" -> (_ => pide = true),
   672       "R" -> (_ => requirements = true),
   673       "X:" -> (arg => exclude_session_groups = exclude_session_groups ::: List(arg)),
   674       "a" -> (_ => all_sessions = true),
   675       "b" -> (_ => build_heap = true),
   676       "c" -> (_ => clean_build = true),
   677       "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
   678       "g:" -> (arg => session_groups = session_groups ::: List(arg)),
   679       "j:" -> (arg => max_jobs = Value.Int.parse(arg)),
   680       "k:" -> (arg => check_keywords = check_keywords + arg),
   681       "l" -> (_ => list_files = true),
   682       "n" -> (_ => no_build = true),
   683       "o:" -> (arg => options = options + arg),
   684       "s" -> (_ => system_mode = true),
   685       "v" -> (_ => verbose = true),
   686       "x:" -> (arg => exclude_sessions = exclude_sessions ::: List(arg)))
   687 
   688     val sessions = getopts(args)
   689 
   690     val progress = new Console_Progress(verbose = verbose)
   691 
   692     val start_date = Date.now()
   693 
   694     if (verbose) {
   695       progress.echo(
   696         "Started at " + Build_Log.print_date(start_date) +
   697           " (" + Isabelle_System.getenv("ML_IDENTIFIER") + " on " + Isabelle_System.hostname() +")")
   698       progress.echo(Build_Log.Settings.show() + "\n")
   699     }
   700 
   701     val results =
   702       progress.interrupt_handler {
   703         build(options, progress,
   704           build_heap = build_heap,
   705           clean_build = clean_build,
   706           dirs = dirs,
   707           select_dirs = select_dirs,
   708           numa_shuffling = NUMA.enabled_warning(numa_shuffling),
   709           max_jobs = max_jobs,
   710           list_files = list_files,
   711           check_keywords = check_keywords,
   712           no_build = no_build,
   713           system_mode = system_mode,
   714           verbose = verbose,
   715           pide = pide,
   716           requirements = requirements,
   717           all_sessions = all_sessions,
   718           exclude_session_groups = exclude_session_groups,
   719           exclude_sessions = exclude_sessions,
   720           session_groups = session_groups,
   721           sessions = sessions)
   722       }
   723     val end_date = Date.now()
   724     val elapsed_time = end_date.time - start_date.time
   725 
   726     if (verbose) {
   727       progress.echo("\nFinished at " + Build_Log.print_date(end_date))
   728     }
   729 
   730     val total_timing =
   731       (Timing.zero /: results.sessions.iterator.map(a => results(a).timing))(_ + _).
   732         copy(elapsed = elapsed_time)
   733     progress.echo(total_timing.message_resources)
   734 
   735     sys.exit(results.rc)
   736   })
   737 }