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