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