src/Pure/Tools/build.scala
author wenzelm
Mon Apr 03 23:31:31 2017 +0200 (2017-04-03)
changeset 65365 d32e702d7ab8
parent 65360 3ff88fece1f6
child 65372 b722ee40c26c
permissions -rw-r--r--
tuned message;
     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               val theories = info.theories.map(x => (x._2, x._3))
   204               import XML.Encode._
   205               pair(list(pair(string, int)), pair(list(properties), pair(bool, pair(bool,
   206                 pair(Path.encode, pair(list(pair(Path.encode, Path.encode)), pair(string,
   207                 pair(string, pair(string, pair(string, pair(Path.encode,
   208                 list(pair(Options.encode, list(Path.encode))))))))))))))(
   209               (Symbol.codes, (command_timings, (do_output, (verbose,
   210                 (store.browser_info, (info.document_files, (File.standard_path(graph_file),
   211                 (parent, (info.chapter, (name, (Path.current,
   212                 theories))))))))))))
   213             })
   214 
   215         val env =
   216           Isabelle_System.settings() +
   217             ("ISABELLE_ML_DEBUGGER" -> options.bool("ML_debugger").toString)
   218 
   219         def save_heap: String =
   220           "ML_Heap.share_common_data (); ML_Heap.save_child " +
   221             ML_Syntax.print_string0(File.platform_path(output))
   222 
   223         if (pide && !Sessions.is_pure(name)) {
   224           val resources = new Resources(name, deps(parent))
   225           val session = new Session(options, resources)
   226           val handler = new Handler(progress, session, name)
   227           session.init_protocol_handler(handler)
   228 
   229           val session_result = Future.promise[Process_Result]
   230 
   231           Isabelle_Process.start(session, options, logic = parent,
   232             cwd = info.dir.file, env = env, tree = Some(tree), store = store,
   233             phase_changed =
   234             {
   235               case Session.Ready => session.protocol_command("build_session", args_yxml)
   236               case Session.Terminated(result) => session_result.fulfill(result)
   237               case _ =>
   238             })
   239 
   240           val result = session_result.join
   241           handler.result_error.join match {
   242             case "" => result
   243             case msg =>
   244               result.copy(
   245                 rc = result.rc max 1,
   246                 out_lines = result.out_lines ::: split_lines(Output.error_text(msg)))
   247           }
   248         }
   249         else {
   250           val args_file = Isabelle_System.tmp_file("build")
   251           File.write(args_file, args_yxml)
   252 
   253           val eval =
   254             "Command_Line.tool0 (fn () => (" +
   255             "Build.build " + ML_Syntax.print_string0(File.standard_path(args_file)) +
   256             (if (do_output) "; " + save_heap else "") + "));"
   257 
   258           val process =
   259             if (Sessions.is_pure(name)) {
   260               ML_Process(options, raw_ml_system = true, cwd = info.dir.file,
   261                 args =
   262                   (for ((root, _) <- Thy_Header.ml_roots) yield List("--use", root)).flatten :::
   263                   List("--eval", eval),
   264                 env = env, tree = Some(tree), store = store, cleanup = () => args_file.delete)
   265             }
   266             else {
   267               ML_Process(options, parent, List("--eval", eval), cwd = info.dir.file,
   268                 env = env, tree = Some(tree), store = store, cleanup = () => args_file.delete)
   269             }
   270 
   271           process.result(
   272             progress_stdout = (line: String) =>
   273               Library.try_unprefix("\floading_theory = ", line) match {
   274                 case Some(theory) => progress.theory(name, theory)
   275                 case None =>
   276               },
   277             progress_limit =
   278               options.int("process_output_limit") match {
   279                 case 0 => None
   280                 case m => Some(m * 1000000L)
   281               },
   282             strict = false)
   283         }
   284       }
   285 
   286     def terminate: Unit = future_result.cancel
   287     def is_finished: Boolean = future_result.is_finished
   288 
   289     @volatile private var was_timeout = false
   290     private val timeout_request: Option[Event_Timer.Request] =
   291     {
   292       if (info.timeout > Time.zero)
   293         Some(Event_Timer.request(Time.now() + info.timeout) { terminate; was_timeout = true })
   294       else None
   295     }
   296 
   297     def join: Process_Result =
   298     {
   299       val result = future_result.join
   300 
   301       if (result.ok)
   302         Present.finish(progress, store.browser_info, graph_file, info, name)
   303 
   304       graph_file.delete
   305       timeout_request.foreach(_.cancel)
   306 
   307       if (result.interrupted) {
   308         if (was_timeout) result.error(Output.error_text("Timeout")).was_timeout
   309         else result.error(Output.error_text("Interrupt"))
   310       }
   311       else result
   312     }
   313   }
   314 
   315 
   316 
   317   /** build with results **/
   318 
   319   class Results private[Build](results: Map[String, (Option[Process_Result], Sessions.Info)])
   320   {
   321     def sessions: Set[String] = results.keySet
   322     def cancelled(name: String): Boolean = results(name)._1.isEmpty
   323     def apply(name: String): Process_Result = results(name)._1.getOrElse(Process_Result(1))
   324     def info(name: String): Sessions.Info = results(name)._2
   325     val rc =
   326       (0 /: results.iterator.map(
   327         { case (_, (Some(r), _)) => r.rc case (_, (None, _)) => 1 }))(_ max _)
   328     def ok: Boolean = rc == 0
   329 
   330     override def toString: String = rc.toString
   331   }
   332 
   333   def build(
   334     options: Options,
   335     progress: Progress = No_Progress,
   336     build_heap: Boolean = false,
   337     clean_build: Boolean = false,
   338     dirs: List[Path] = Nil,
   339     select_dirs: List[Path] = Nil,
   340     numa_shuffling: Boolean = false,
   341     max_jobs: Int = 1,
   342     list_files: Boolean = false,
   343     check_keywords: Set[String] = Set.empty,
   344     no_build: Boolean = false,
   345     system_mode: Boolean = false,
   346     verbose: Boolean = false,
   347     pide: Boolean = false,
   348     requirements: Boolean = false,
   349     all_sessions: Boolean = false,
   350     exclude_session_groups: List[String] = Nil,
   351     exclude_sessions: List[String] = Nil,
   352     session_groups: List[String] = Nil,
   353     sessions: List[String] = Nil): Results =
   354   {
   355     build_selection(
   356       options = options,
   357       progress = progress,
   358       build_heap = build_heap,
   359       clean_build = clean_build,
   360       dirs = dirs,
   361       select_dirs = select_dirs,
   362       numa_shuffling = numa_shuffling,
   363       max_jobs = max_jobs,
   364       list_files = list_files,
   365       check_keywords = check_keywords,
   366       no_build = no_build,
   367       system_mode = system_mode,
   368       verbose = verbose,
   369       pide = pide,
   370       selection = { full_tree =>
   371         full_tree.selection(requirements, all_sessions,
   372           exclude_session_groups, exclude_sessions, session_groups, sessions) })
   373   }
   374 
   375   def build_selection(
   376     options: Options,
   377     progress: Progress = No_Progress,
   378     build_heap: Boolean = false,
   379     clean_build: Boolean = false,
   380     dirs: List[Path] = Nil,
   381     select_dirs: List[Path] = Nil,
   382     numa_shuffling: Boolean = false,
   383     max_jobs: Int = 1,
   384     list_files: Boolean = false,
   385     check_keywords: Set[String] = Set.empty,
   386     no_build: Boolean = false,
   387     system_mode: Boolean = false,
   388     verbose: Boolean = false,
   389     pide: Boolean = false,
   390     selection: Sessions.Tree => (List[String], Sessions.Tree) =
   391       (_.selection(all_sessions = true))): Results =
   392   {
   393     /* session selection and dependencies */
   394 
   395     val build_options = options.int.update("completion_limit", 0).bool.update("ML_statistics", true)
   396     val full_tree = Sessions.load(build_options, dirs, select_dirs)
   397     val (selected, selected_tree) = selection(full_tree)
   398     val deps =
   399       Sessions.dependencies(progress, true, verbose, list_files, check_keywords, selected_tree)
   400 
   401     def sources_stamp(name: String): List[String] =
   402       (selected_tree(name).meta_digest :: deps.sources(name)).map(_.toString).sorted
   403 
   404 
   405     /* main build process */
   406 
   407     val store = Sessions.store(system_mode)
   408     val queue = Queue(selected_tree, store)
   409 
   410     store.prepare_output()
   411 
   412     // optional cleanup
   413     if (clean_build) {
   414       for (name <- full_tree.graph.all_succs(selected)) {
   415         val files =
   416           List(Path.basic(name), store.database(name), store.log(name), store.log_gz(name)).
   417             map(store.output_dir + _).filter(_.is_file)
   418         if (files.nonEmpty) progress.echo("Cleaning " + name + " ...")
   419         if (!files.forall(p => p.file.delete)) progress.echo(name + " FAILED to delete")
   420       }
   421     }
   422 
   423     // scheduler loop
   424     case class Result(
   425       current: Boolean, heap_stamp: Option[String],
   426       process: Option[Process_Result], info: Sessions.Info)
   427     {
   428       def ok: Boolean =
   429         process match {
   430           case None => false
   431           case Some(res) => res.rc == 0
   432         }
   433     }
   434 
   435     def sleep()
   436     {
   437       try { Thread.sleep(500) }
   438       catch { case Exn.Interrupt() => Exn.Interrupt.impose() }
   439     }
   440 
   441     val numa_nodes = new NUMA.Nodes(numa_shuffling)
   442 
   443     @tailrec def loop(
   444       pending: Queue,
   445       running: Map[String, (List[String], Job)],
   446       results: Map[String, Result]): Map[String, Result] =
   447     {
   448       def used_node(i: Int): Boolean =
   449         running.iterator.exists(
   450           { case (_, (_, job)) => job.numa_node.isDefined && job.numa_node.get == i })
   451 
   452       if (pending.is_empty) results
   453       else {
   454         if (progress.stopped)
   455           for ((_, (_, job)) <- running) job.terminate
   456 
   457         running.find({ case (_, (_, job)) => job.is_finished }) match {
   458           case Some((name, (input_heaps, job))) =>
   459             //{{{ finish job
   460 
   461             val process_result = job.join
   462             process_result.err_lines.foreach(progress.echo(_))
   463             if (process_result.ok)
   464               progress.echo("Finished " + name + " (" + process_result.timing.message_resources + ")")
   465 
   466             val log_lines = process_result.out_lines.filterNot(_.startsWith("\f"))
   467             val process_result_tail =
   468             {
   469               val tail = job.info.options.int("process_output_tail")
   470               process_result.copy(
   471                 out_lines =
   472                   "(see also " + (store.output_dir + store.log(name)).file.toString + ")" ::
   473                   (if (tail == 0) log_lines else log_lines.drop(log_lines.length - tail max 0)))
   474             }
   475 
   476             val heap_stamp =
   477               if (process_result.ok) {
   478                 (store.output_dir + store.log(name)).file.delete
   479                 val heap_stamp =
   480                   for (path <- job.output_path if path.is_file)
   481                     yield Sessions.write_heap_digest(path)
   482 
   483                 File.write_gzip(store.output_dir + store.log_gz(name), terminate_lines(log_lines))
   484 
   485                 heap_stamp
   486               }
   487               else {
   488                 (store.output_dir + Path.basic(name)).file.delete
   489                 (store.output_dir + store.log_gz(name)).file.delete
   490 
   491                 File.write(store.output_dir + store.log(name), terminate_lines(log_lines))
   492                 progress.echo(name + " FAILED")
   493                 if (!process_result.interrupted) progress.echo(process_result_tail.out)
   494 
   495                 None
   496               }
   497 
   498             // write database
   499             {
   500               val database = store.output_dir + store.database(name)
   501               database.file.delete
   502 
   503               using(SQLite.open_database(database))(db =>
   504                 store.write_session_info(db, name,
   505                   build_log =
   506                     Build_Log.Log_File(name, process_result.out_lines).
   507                       parse_session_info(
   508                         command_timings = true, ml_statistics = true, task_statistics = true),
   509                   build =
   510                     Session_Info(sources_stamp(name), input_heaps, heap_stamp, process_result.rc)))
   511             }
   512 
   513             loop(pending - name, running - name,
   514               results + (name -> Result(false, heap_stamp, Some(process_result_tail), job.info)))
   515             //}}}
   516           case None if running.size < (max_jobs max 1) =>
   517             //{{{ check/start next job
   518             pending.dequeue(running.isDefinedAt(_)) match {
   519               case Some((name, info)) =>
   520                 val ancestor_results = selected_tree.ancestors(name).map(results(_))
   521                 val ancestor_heaps = ancestor_results.flatMap(_.heap_stamp)
   522 
   523                 val do_output = build_heap || Sessions.is_pure(name) || queue.is_inner(name)
   524 
   525                 val (current, heap_stamp) =
   526                 {
   527                   store.find_database_heap(name) match {
   528                     case Some((database, heap_stamp)) =>
   529                       using(SQLite.open_database(database))(store.read_build(_, name)) match {
   530                         case Some(build) =>
   531                           val current =
   532                             build.sources == sources_stamp(name) &&
   533                             build.input_heaps == ancestor_heaps &&
   534                             build.output_heap == heap_stamp &&
   535                             !(do_output && heap_stamp.isEmpty) &&
   536                             build.return_code == 0
   537                           (current, heap_stamp)
   538                         case None => (false, None)
   539                       }
   540                     case None => (false, None)
   541                   }
   542                 }
   543                 val all_current = current && ancestor_results.forall(_.current)
   544 
   545                 if (all_current)
   546                   loop(pending - name, running,
   547                     results + (name -> Result(true, heap_stamp, Some(Process_Result(0)), info)))
   548                 else if (no_build) {
   549                   if (verbose) progress.echo("Skipping " + name + " ...")
   550                   loop(pending - name, running,
   551                     results + (name -> Result(false, heap_stamp, Some(Process_Result(1)), info)))
   552                 }
   553                 else if (ancestor_results.forall(_.ok) && !progress.stopped) {
   554                   val numa_node = numa_nodes.next(used_node(_))
   555                   progress.echo((if (do_output) "Building " else "Running ") + name + " ...")
   556                   val job =
   557                     new Job(progress, name, info, selected_tree, deps, store, do_output, verbose,
   558                       pide, numa_node, queue.command_timings(name))
   559                   loop(pending, running + (name -> (ancestor_heaps, job)), results)
   560                 }
   561                 else {
   562                   progress.echo(name + " CANCELLED")
   563                   loop(pending - name, running,
   564                     results + (name -> Result(false, heap_stamp, None, info)))
   565                 }
   566               case None => sleep(); loop(pending, running, results)
   567             }
   568             ///}}}
   569           case None => sleep(); loop(pending, running, results)
   570         }
   571       }
   572     }
   573 
   574 
   575     /* build results */
   576 
   577     val results0 =
   578       if (deps.is_empty) {
   579         progress.echo(Output.warning_text("Nothing to build"))
   580         Map.empty[String, Result]
   581       }
   582       else loop(queue, Map.empty, Map.empty)
   583 
   584     val results =
   585       new Results(
   586         (for ((name, result) <- results0.iterator)
   587           yield (name, (result.process, result.info))).toMap)
   588 
   589     if (results.rc != 0 && (verbose || !no_build)) {
   590       val unfinished =
   591         (for {
   592           name <- results.sessions.iterator
   593           if !results(name).ok
   594          } yield name).toList.sorted
   595       progress.echo("Unfinished session(s): " + commas(unfinished))
   596     }
   597 
   598 
   599     /* global browser info */
   600 
   601     if (!no_build) {
   602       val browser_chapters =
   603         (for {
   604           (name, result) <- results0.iterator
   605           if result.ok
   606           info = full_tree(name)
   607           if info.options.bool("browser_info")
   608         } yield (info.chapter, (name, info.description))).toList.groupBy(_._1).
   609             map({ case (chapter, es) => (chapter, es.map(_._2)) }).filterNot(_._2.isEmpty)
   610 
   611       for ((chapter, entries) <- browser_chapters)
   612         Present.update_chapter_index(store.browser_info, chapter, entries)
   613 
   614       if (browser_chapters.nonEmpty) Present.make_global_index(store.browser_info)
   615     }
   616 
   617     results
   618   }
   619 
   620 
   621   /* Isabelle tool wrapper */
   622 
   623   val isabelle_tool = Isabelle_Tool("build", "build and manage Isabelle sessions", args =>
   624   {
   625     val build_options = Word.explode(Isabelle_System.getenv("ISABELLE_BUILD_OPTIONS"))
   626 
   627     var select_dirs: List[Path] = Nil
   628     var numa_shuffling = false
   629     var pide = false
   630     var requirements = false
   631     var exclude_session_groups: List[String] = Nil
   632     var all_sessions = false
   633     var build_heap = false
   634     var clean_build = false
   635     var dirs: List[Path] = Nil
   636     var session_groups: List[String] = Nil
   637     var max_jobs = 1
   638     var check_keywords: Set[String] = Set.empty
   639     var list_files = false
   640     var no_build = false
   641     var options = (Options.init() /: build_options)(_ + _)
   642     var system_mode = false
   643     var verbose = false
   644     var exclude_sessions: List[String] = Nil
   645 
   646     val getopts = Getopts("""
   647 Usage: isabelle build [OPTIONS] [SESSIONS ...]
   648 
   649   Options are:
   650     -D DIR       include session directory and select its sessions
   651     -N           cyclic shuffling of NUMA CPU nodes (performance tuning)
   652     -P           build via PIDE protocol
   653     -R           operate on requirements of selected sessions
   654     -X NAME      exclude sessions from group NAME and all descendants
   655     -a           select all sessions
   656     -b           build heap images
   657     -c           clean build
   658     -d DIR       include session directory
   659     -g NAME      select session group NAME
   660     -j INT       maximum number of parallel jobs (default 1)
   661     -k KEYWORD   check theory sources for conflicts with proposed keywords
   662     -l           list session source files
   663     -n           no build -- test dependencies only
   664     -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
   665     -s           system build mode: produce output in ISABELLE_HOME
   666     -v           verbose
   667     -x NAME      exclude session NAME and all descendants
   668 
   669   Build and manage Isabelle sessions, depending on implicit settings:
   670 
   671 """ + Library.prefix_lines("  ", Build_Log.Settings.show()) + "\n",
   672       "D:" -> (arg => select_dirs = select_dirs ::: List(Path.explode(arg))),
   673       "N" -> (_ => numa_shuffling = true),
   674       "P" -> (_ => pide = true),
   675       "R" -> (_ => requirements = true),
   676       "X:" -> (arg => exclude_session_groups = exclude_session_groups ::: List(arg)),
   677       "a" -> (_ => all_sessions = true),
   678       "b" -> (_ => build_heap = true),
   679       "c" -> (_ => clean_build = true),
   680       "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
   681       "g:" -> (arg => session_groups = session_groups ::: List(arg)),
   682       "j:" -> (arg => max_jobs = Value.Int.parse(arg)),
   683       "k:" -> (arg => check_keywords = check_keywords + arg),
   684       "l" -> (_ => list_files = true),
   685       "n" -> (_ => no_build = true),
   686       "o:" -> (arg => options = options + arg),
   687       "s" -> (_ => system_mode = true),
   688       "v" -> (_ => verbose = true),
   689       "x:" -> (arg => exclude_sessions = exclude_sessions ::: List(arg)))
   690 
   691     val sessions = getopts(args)
   692 
   693     val progress = new Console_Progress(verbose = verbose)
   694 
   695     val start_date = Date.now()
   696 
   697     if (verbose) {
   698       progress.echo(
   699         "Started at " + Build_Log.print_date(start_date) +
   700           " (" + Isabelle_System.getenv("ML_IDENTIFIER") + " on " + Isabelle_System.hostname() +")")
   701       progress.echo(Build_Log.Settings.show() + "\n")
   702     }
   703 
   704     val results =
   705       progress.interrupt_handler {
   706         build(options, progress,
   707           build_heap = build_heap,
   708           clean_build = clean_build,
   709           dirs = dirs,
   710           select_dirs = select_dirs,
   711           numa_shuffling = NUMA.enabled_warning(numa_shuffling),
   712           max_jobs = max_jobs,
   713           list_files = list_files,
   714           check_keywords = check_keywords,
   715           no_build = no_build,
   716           system_mode = system_mode,
   717           verbose = verbose,
   718           pide = pide,
   719           requirements = requirements,
   720           all_sessions = all_sessions,
   721           exclude_session_groups = exclude_session_groups,
   722           exclude_sessions = exclude_sessions,
   723           session_groups = session_groups,
   724           sessions = sessions)
   725       }
   726     val end_date = Date.now()
   727     val elapsed_time = end_date.time - start_date.time
   728 
   729     if (verbose) {
   730       progress.echo("\nFinished at " + Build_Log.print_date(end_date))
   731     }
   732 
   733     val total_timing =
   734       (Timing.zero /: results.sessions.iterator.map(a => results(a).timing))(_ + _).
   735         copy(elapsed = elapsed_time)
   736     progress.echo(total_timing.message_resources)
   737 
   738     sys.exit(results.rc)
   739   })
   740 }