src/Pure/Tools/build_job.scala
author wenzelm
Wed, 08 Feb 2023 10:18:30 +0100
changeset 77236 dce91dab1728
parent 77207 d98a99e4eea9
child 77237 f947e045fa61
permissions -rw-r--r--
clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Tools/build_job.scala
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     3
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     4
Build job running prover process, with rudimentary PIDE session.
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     5
*/
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     6
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     8
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     9
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
    10
import scala.collection.mutable
76087
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
    11
import scala.util.matching.Regex
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
    12
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
    13
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
    14
object Build_Job {
75778
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75762
diff changeset
    15
  /* theory markup/messages from session database */
72854
6c660f05f70c clarified signature;
wenzelm
parents: 72853
diff changeset
    16
75778
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75762
diff changeset
    17
  def read_theory(
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75762
diff changeset
    18
    theory_context: Export.Theory_Context,
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75762
diff changeset
    19
    unicode_symbols: Boolean = false
76205
005abcb34849 tuned signature;
wenzelm
parents: 76088
diff changeset
    20
  ): Option[Document.Snapshot] = {
76936
ee785742c694 proper treatment of unicode_symbols;
wenzelm
parents: 76935
diff changeset
    21
    def decode_bytes(bytes: Bytes): String =
ee785742c694 proper treatment of unicode_symbols;
wenzelm
parents: 76935
diff changeset
    22
      Symbol.output(unicode_symbols, UTF8.decode_permissive(bytes))
ee785742c694 proper treatment of unicode_symbols;
wenzelm
parents: 76935
diff changeset
    23
75778
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75762
diff changeset
    24
    def read(name: String): Export.Entry = theory_context(name, permissive = true)
72848
d5d0e36eda16 read theory with PIDE markup from session database;
wenzelm
parents: 72844
diff changeset
    25
d5d0e36eda16 read theory with PIDE markup from session database;
wenzelm
parents: 72844
diff changeset
    26
    def read_xml(name: String): XML.Body =
76936
ee785742c694 proper treatment of unicode_symbols;
wenzelm
parents: 76935
diff changeset
    27
      YXML.parse_body(decode_bytes(read(name).bytes), cache = theory_context.cache)
72848
d5d0e36eda16 read theory with PIDE markup from session database;
wenzelm
parents: 72844
diff changeset
    28
76912
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    29
    def read_source_file(name: String): Sessions.Source_File =
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    30
      theory_context.session_context.source_file(name)
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    31
75733
d3430f302c2e clarified signature;
wenzelm
parents: 75454
diff changeset
    32
    for {
75778
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75762
diff changeset
    33
      id <- theory_context.document_id()
75903
dce94a1d18fd proper permissive = true (amending 475fedc02737)
wenzelm
parents: 75791
diff changeset
    34
      (thy_file, blobs_files) <- theory_context.files(permissive = true)
75733
d3430f302c2e clarified signature;
wenzelm
parents: 75454
diff changeset
    35
    }
d3430f302c2e clarified signature;
wenzelm
parents: 75454
diff changeset
    36
    yield {
76881
b59118d11a46 clarified master_dir: avoid somewhat fragile Document.Node.Name.master_dir_path;
wenzelm
parents: 76879
diff changeset
    37
      val master_dir =
b59118d11a46 clarified master_dir: avoid somewhat fragile Document.Node.Name.master_dir_path;
wenzelm
parents: 76879
diff changeset
    38
        Path.explode(Url.strip_base_name(thy_file).getOrElse(
b59118d11a46 clarified master_dir: avoid somewhat fragile Document.Node.Name.master_dir_path;
wenzelm
parents: 76879
diff changeset
    39
          error("Cannot determine theory master directory: " + quote(thy_file))))
b59118d11a46 clarified master_dir: avoid somewhat fragile Document.Node.Name.master_dir_path;
wenzelm
parents: 76879
diff changeset
    40
75733
d3430f302c2e clarified signature;
wenzelm
parents: 75454
diff changeset
    41
      val blobs =
76912
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    42
        blobs_files.map { name =>
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    43
          val path = Path.explode(name)
76881
b59118d11a46 clarified master_dir: avoid somewhat fragile Document.Node.Name.master_dir_path;
wenzelm
parents: 76879
diff changeset
    44
          val src_path = File.relative_path(master_dir, path).getOrElse(path)
76912
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    45
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    46
          val file = read_source_file(name)
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    47
          val bytes = file.bytes
76936
ee785742c694 proper treatment of unicode_symbols;
wenzelm
parents: 76935
diff changeset
    48
          val text = decode_bytes(bytes)
76912
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    49
          val chunk = Symbol.Text_Chunk(text)
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    50
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    51
          Command.Blob(Document.Node.Name(name), src_path, Some((file.digest, chunk))) ->
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    52
            Document.Blobs.Item(bytes, text, chunk, changed = false)
75733
d3430f302c2e clarified signature;
wenzelm
parents: 75454
diff changeset
    53
        }
76912
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    54
76936
ee785742c694 proper treatment of unicode_symbols;
wenzelm
parents: 76935
diff changeset
    55
      val thy_source = decode_bytes(read_source_file(thy_file).bytes)
76912
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    56
      val thy_xml = read_xml(Export.MARKUP)
75733
d3430f302c2e clarified signature;
wenzelm
parents: 75454
diff changeset
    57
      val blobs_xml =
d3430f302c2e clarified signature;
wenzelm
parents: 75454
diff changeset
    58
        for (i <- (1 to blobs.length).toList)
d3430f302c2e clarified signature;
wenzelm
parents: 75454
diff changeset
    59
          yield read_xml(Export.MARKUP + i)
72865
ebf72a3b8daa clarified file sources: take from build database instead of file-system;
wenzelm
parents: 72864
diff changeset
    60
76912
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    61
      val markups_index = Command.Markup_Index.make(blobs.map(_._1))
75733
d3430f302c2e clarified signature;
wenzelm
parents: 75454
diff changeset
    62
      val markups =
d3430f302c2e clarified signature;
wenzelm
parents: 75454
diff changeset
    63
        Command.Markups.make(
d3430f302c2e clarified signature;
wenzelm
parents: 75454
diff changeset
    64
          for ((index, xml) <- markups_index.zip(thy_xml :: blobs_xml))
d3430f302c2e clarified signature;
wenzelm
parents: 75454
diff changeset
    65
          yield index -> Markup_Tree.from_XML(xml))
d3430f302c2e clarified signature;
wenzelm
parents: 75454
diff changeset
    66
76911
wenzelm
parents: 76908
diff changeset
    67
      val results =
wenzelm
parents: 76908
diff changeset
    68
        Command.Results.make(
wenzelm
parents: 76908
diff changeset
    69
          for (elem @ XML.Elem(Markup(_, Markup.Serial(i)), _) <- read_xml(Export.MESSAGES))
wenzelm
parents: 76908
diff changeset
    70
            yield i -> elem)
wenzelm
parents: 76908
diff changeset
    71
76205
005abcb34849 tuned signature;
wenzelm
parents: 76088
diff changeset
    72
      val command =
76881
b59118d11a46 clarified master_dir: avoid somewhat fragile Document.Node.Name.master_dir_path;
wenzelm
parents: 76879
diff changeset
    73
        Command.unparsed(thy_source, theory = true, id = id,
b59118d11a46 clarified master_dir: avoid somewhat fragile Document.Node.Name.master_dir_path;
wenzelm
parents: 76879
diff changeset
    74
          node_name = Document.Node.Name(thy_file, theory = theory_context.theory),
76913
a8eb5046b05f tuned signature;
wenzelm
parents: 76912
diff changeset
    75
          blobs_info = Command.Blobs_Info.make(blobs),
76912
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    76
          markups = markups, results = results)
76205
005abcb34849 tuned signature;
wenzelm
parents: 76088
diff changeset
    77
76913
a8eb5046b05f tuned signature;
wenzelm
parents: 76912
diff changeset
    78
      val doc_blobs = Document.Blobs.make(blobs)
76912
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    79
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
    80
      Document.State.init.snippet(command, doc_blobs)
72848
d5d0e36eda16 read theory with PIDE markup from session database;
wenzelm
parents: 72844
diff changeset
    81
    }
d5d0e36eda16 read theory with PIDE markup from session database;
wenzelm
parents: 72844
diff changeset
    82
  }
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
    83
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
    84
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
    85
  /* print messages */
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
    86
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
    87
  def print_log(
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
    88
    options: Options,
76088
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
    89
    sessions: List[String],
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
    90
    theories: List[String] = Nil,
76087
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
    91
    message_head: List[Regex] = Nil,
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
    92
    message_body: List[Regex] = Nil,
72878
80465b791f95 clarified messages;
wenzelm
parents: 72876
diff changeset
    93
    verbose: Boolean = false,
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
    94
    progress: Progress = new Progress,
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
    95
    margin: Double = Pretty.default_margin,
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
    96
    breakgain: Double = Pretty.default_breakgain,
72873
0ad513706a27 clarified Pretty.Metric, as for build errors;
wenzelm
parents: 72871
diff changeset
    97
    metric: Pretty.Metric = Symbol.Metric,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
    98
    unicode_symbols: Boolean = false
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
    99
  ): Unit = {
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   100
    val store = Sessions.store(options)
76654
a3177042863d tuned signature;
wenzelm
parents: 76205
diff changeset
   101
    val session = new Session(options, Resources.bootstrap)
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   102
76087
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
   103
    def check(filter: List[Regex], make_string: => String): Boolean =
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
   104
      filter.isEmpty || {
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
   105
        val s = Output.clean_yxml(make_string)
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
   106
        filter.forall(r => r.findFirstIn(Output.clean_yxml(s)).nonEmpty)
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
   107
      }
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
   108
76088
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   109
    def print(session_name: String): Unit = {
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   110
      using(Export.open_session_context0(store, session_name)) { session_context =>
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   111
        val result =
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   112
          for {
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   113
            db <- session_context.session_db()
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   114
            theories = store.read_theories(db, session_name)
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   115
            errors = store.read_errors(db, session_name)
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   116
            info <- store.read_build(db, session_name)
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   117
          } yield (theories, errors, info.return_code)
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   118
        result match {
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   119
          case None => store.error_database(session_name)
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   120
          case Some((used_theories, errors, rc)) =>
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   121
            theories.filterNot(used_theories.toSet) match {
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   122
              case Nil =>
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   123
              case bad => error("Unknown theories " + commas_quote(bad))
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   124
            }
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   125
            val print_theories =
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   126
              if (theories.isEmpty) used_theories else used_theories.filter(theories.toSet)
75778
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75762
diff changeset
   127
76088
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   128
            for (thy <- print_theories) {
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   129
              val thy_heading = "\nTheory " + quote(thy) + " (in " + session_name + ")" + ":"
75778
d18c96b9b955 prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents: 75762
diff changeset
   130
76088
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   131
              read_theory(session_context.theory(thy), unicode_symbols = unicode_symbols) match {
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   132
                case None => progress.echo(thy_heading + " MISSING")
76205
005abcb34849 tuned signature;
wenzelm
parents: 76088
diff changeset
   133
                case Some(snapshot) =>
76088
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   134
                  val rendering = new Rendering(snapshot, options, session)
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   135
                  val messages =
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   136
                    rendering.text_messages(Text.Range.full)
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   137
                      .filter(message => verbose || Protocol.is_exported(message.info))
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   138
                  if (messages.nonEmpty) {
76933
dd53bb198eb1 tuned signature: more uniform operations;
wenzelm
parents: 76930
diff changeset
   139
                    val line_document = Line.Document(snapshot.node.source)
76088
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   140
                    val buffer = new mutable.ListBuffer[String]
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   141
                    for (Text.Info(range, elem) <- messages) {
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   142
                      val line = line_document.position(range.start).line1
76205
005abcb34849 tuned signature;
wenzelm
parents: 76088
diff changeset
   143
                      val pos = Position.Line_File(line, snapshot.node_name.node)
76088
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   144
                      def message_text: String =
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   145
                        Protocol.message_text(elem, heading = true, pos = pos,
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   146
                          margin = margin, breakgain = breakgain, metric = metric)
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   147
                      val ok =
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   148
                        check(message_head, Protocol.message_heading(elem, pos)) &&
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   149
                        check(message_body, XML.content(Pretty.unformatted(List(elem))))
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   150
                      if (ok) buffer += message_text
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   151
                    }
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   152
                    if (buffer.nonEmpty) {
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   153
                      progress.echo(thy_heading)
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   154
                      buffer.foreach(progress.echo)
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   155
                    }
76087
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
   156
                  }
76088
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   157
              }
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   158
            }
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   159
76088
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   160
            if (errors.nonEmpty) {
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   161
              val msg = Symbol.output(unicode_symbols, cat_lines(errors))
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   162
              progress.echo("\nBuild errors:\n" + Output.error_message_text(msg))
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   163
            }
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   164
            if (rc != Process_Result.RC.ok) {
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   165
              progress.echo("\n" + Process_Result.RC.print_long(rc))
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   166
            }
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   167
        }
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   168
      }
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   169
    }
76088
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   170
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   171
    val errors = new mutable.ListBuffer[String]
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   172
    for (session_name <- sessions) {
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   173
      Exn.interruptible_capture(print(session_name)) match {
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   174
        case Exn.Res(_) =>
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   175
        case Exn.Exn(exn) => errors += Exn.message(exn)
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   176
      }
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   177
    }
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   178
    if (errors.nonEmpty) error(cat_lines(errors.toList))
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   179
  }
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   180
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   181
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   182
  /* Isabelle tool wrapper */
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   183
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   184
  val isabelle_tool = Isabelle_Tool("log", "print messages from build database",
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   185
    Scala_Project.here,
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   186
    { args =>
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   187
      /* arguments */
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   188
76087
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
   189
      var message_head = List.empty[Regex]
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
   190
      var message_body = List.empty[Regex]
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   191
      var unicode_symbols = false
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   192
      var theories: List[String] = Nil
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   193
      var margin = Pretty.default_margin
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   194
      var options = Options.init()
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   195
      var verbose = false
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   196
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   197
      val getopts = Getopts("""
76088
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   198
Usage: isabelle log [OPTIONS] [SESSIONS ...]
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   199
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   200
  Options are:
76087
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
   201
    -H REGEX     filter messages by matching against head
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
   202
    -M REGEX     filter messages by matching against body
72876
626fcaebd049 NEWS and documentation for "isabelle log";
wenzelm
parents: 72875
diff changeset
   203
    -T NAME      restrict to given theories (multiple options possible)
72867
7b10b40b1273 clarified output of Isabelle symbols;
wenzelm
parents: 72865
diff changeset
   204
    -U           output Unicode symbols
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   205
    -m MARGIN    margin for pretty printing (default: """ + margin + """)
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   206
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
73837
f72335f6a9ed clarified documentation: tracing messages are not shown here;
wenzelm
parents: 73835
diff changeset
   207
    -v           print all messages, including information etc.
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   208
76088
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   209
  Print messages from the build database of the given sessions, without any
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   210
  checks against current sources nor session structure: results from old
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   211
  sessions or failed builds can be printed as well.
76087
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
   212
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
   213
  Multiple options -H and -M are conjunctive: all given patterns need to
76088
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   214
  match. Patterns match any substring, but ^ or $ may be used to match the
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   215
  start or end explicitly.
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   216
""",
76087
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
   217
        "H:" -> (arg => message_head = message_head ::: List(arg.r)),
c8f5fec36b5c support regex patterns on messages;
wenzelm
parents: 75970
diff changeset
   218
        "M:" -> (arg => message_body = message_body ::: List(arg.r)),
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   219
        "T:" -> (arg => theories = theories ::: List(arg)),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   220
        "U" -> (_ => unicode_symbols = true),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   221
        "m:" -> (arg => margin = Value.Double.parse(arg)),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   222
        "o:" -> (arg => options = options + arg),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   223
        "v" -> (_ => verbose = true))
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   224
76088
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   225
      val sessions = getopts(args)
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   226
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   227
      val progress = new Console_Progress()
72858
cb0c407fbc6e added "isabelle log": print messages from build database;
wenzelm
parents: 72857
diff changeset
   228
76088
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   229
      if (sessions.isEmpty) progress.echo_warning("No sessions to print")
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   230
      else {
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   231
        print_log(options, sessions, theories = theories, message_head = message_head,
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   232
          message_body = message_body, verbose = verbose, margin = margin, progress = progress,
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   233
          unicode_symbols = unicode_symbols)
bec8677d0e5b support multiple sessions, with cumulative errors;
wenzelm
parents: 76087
diff changeset
   234
      }
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   235
    })
72848
d5d0e36eda16 read theory with PIDE markup from session database;
wenzelm
parents: 72844
diff changeset
   236
}
d5d0e36eda16 read theory with PIDE markup from session database;
wenzelm
parents: 72844
diff changeset
   237
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   238
class Build_Job(progress: Progress,
76668
dd03c91cda43 clarified signature;
wenzelm
parents: 76667
diff changeset
   239
  session_background: Sessions.Background,
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   240
  store: Sessions.Store,
77236
dce91dab1728 clarified signature;
wenzelm
parents: 77207
diff changeset
   241
  val do_store: Boolean,
73777
52e43a93d51f clarified system_log: make this work independently of the particular "isabelle build" command-line (e.g. "isabelle mirabelle");
wenzelm
parents: 73774
diff changeset
   242
  log: Logger,
73805
b73777a0c076 allow build session setup, e.g. for protocol handlers;
wenzelm
parents: 73804
diff changeset
   243
  session_setup: (String, Session) => Unit,
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   244
  val numa_node: Option[Int],
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   245
  command_timings0: List[Properties.T]
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   246
) {
76668
dd03c91cda43 clarified signature;
wenzelm
parents: 76667
diff changeset
   247
  def session_name: String = session_background.session_name
dd03c91cda43 clarified signature;
wenzelm
parents: 76667
diff changeset
   248
  val info: Sessions.Info = session_background.sessions_structure(session_name)
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   249
  val options: Options = NUMA.policy_options(info.options, numa_node)
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   250
76873
713eb7f2230e clarified signature: more explicit types;
wenzelm
parents: 76872
diff changeset
   251
  val session_sources: Sessions.Sources =
713eb7f2230e clarified signature: more explicit types;
wenzelm
parents: 76872
diff changeset
   252
    Sessions.Sources.load(session_background.base, cache = store.cache.compress)
76871
a17f9ff37558 clarified session_sources (again, see also 9d0e6ea7aa68);
wenzelm
parents: 76860
diff changeset
   253
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   254
  private val future_result: Future[Process_Result] =
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   255
    Future.thread("build", uninterruptible = true) {
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   256
      val parent = info.parent.getOrElse("")
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   257
75454
295e1c9d2994 tuned signature;
wenzelm
parents: 75440
diff changeset
   258
      val env =
295e1c9d2994 tuned signature;
wenzelm
parents: 75440
diff changeset
   259
        Isabelle_System.settings(
295e1c9d2994 tuned signature;
wenzelm
parents: 75440
diff changeset
   260
          List("ISABELLE_ML_DEBUGGER" -> options.bool("ML_debugger").toString))
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   261
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   262
      val is_pure = Sessions.is_pure(session_name)
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   263
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   264
      val use_prelude = if (is_pure) Thy_Header.ml_roots.map(_._1) else Nil
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   265
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   266
      val eval_store =
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   267
        if (do_store) {
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   268
          (if (info.theories.nonEmpty) List("ML_Heap.share_common_data ()") else Nil) :::
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   269
          List("ML_Heap.save_child " +
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   270
            ML_Syntax.print_string_bytes(File.platform_path(store.output_heap(session_name))))
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   271
        }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   272
        else Nil
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   273
76912
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   274
      def session_blobs(node_name: Document.Node.Name): List[(Command.Blob, Document.Blobs.Item)] =
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   275
        session_background.base.theory_load_commands.get(node_name.theory) match {
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   276
          case None => Nil
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   277
          case Some(spans) =>
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   278
            val syntax = session_background.base.theory_syntax(node_name)
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   279
            val master_dir = Path.explode(node_name.master_dir)
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   280
            for (span <- spans; file <- span.loaded_files(syntax).files)
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   281
              yield {
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   282
                val src_path = Path.explode(file)
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   283
                val blob_name = Document.Node.Name(File.symbolic_path(master_dir + src_path))
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   284
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   285
                val bytes = session_sources(blob_name.node).bytes
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   286
                val text = bytes.text
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   287
                val chunk = Symbol.Text_Chunk(text)
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   288
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   289
                Command.Blob(blob_name, src_path, Some((SHA1.digest(bytes), chunk))) ->
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   290
                  Document.Blobs.Item(bytes, text, chunk, changed = false)
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   291
              }
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   292
        }
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   293
73774
734d5d3fbd9d syslog option for "isabelle build";
wenzelm
parents: 73719
diff changeset
   294
      val resources =
76657
a8d85b4a588c clarified signature;
wenzelm
parents: 76656
diff changeset
   295
        new Resources(session_background, log = log, command_timings = command_timings0)
76912
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   296
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   297
      val session =
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   298
        new Session(options, resources) {
74731
161e84e6b40a just one cache, via HTML_Context, via Sessions.Store or Session;
wenzelm
parents: 74712
diff changeset
   299
          override val cache: Term.Cache = store.cache
72816
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72783
diff changeset
   300
76912
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   301
          override def build_blobs_info(node_name: Document.Node.Name): Command.Blobs_Info =
76913
a8eb5046b05f tuned signature;
wenzelm
parents: 76912
diff changeset
   302
            Command.Blobs_Info.make(session_blobs(node_name))
76912
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   303
ca872f20cf5b clarified session sources: theory and blobs are read from database, instead of physical file-system;
wenzelm
parents: 76911
diff changeset
   304
          override def build_blobs(node_name: Document.Node.Name): Document.Blobs =
76913
a8eb5046b05f tuned signature;
wenzelm
parents: 76912
diff changeset
   305
            Document.Blobs.make(session_blobs(node_name))
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   306
        }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   307
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   308
      object Build_Session_Errors {
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   309
        private val promise: Promise[List[String]] = Future.promise
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   310
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   311
        def result: Exn.Result[List[String]] = promise.join_result
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   312
        def cancel(): Unit = promise.cancel()
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   313
        def apply(errs: List[String]): Unit = {
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   314
          try { promise.fulfill(errs) }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   315
          catch { case _: IllegalStateException => }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   316
        }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   317
      }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   318
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   319
      val export_consumer =
74255
e117e0c29204 more reactive interrupt;
wenzelm
parents: 74144
diff changeset
   320
        Export.consumer(store.open_database(session_name, output = true), store.cache,
e117e0c29204 more reactive interrupt;
wenzelm
parents: 74144
diff changeset
   321
          progress = progress)
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   322
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   323
      val stdout = new StringBuilder(1000)
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   324
      val stderr = new StringBuilder(1000)
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   325
      val command_timings = new mutable.ListBuffer[Properties.T]
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   326
      val theory_timings = new mutable.ListBuffer[Properties.T]
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   327
      val session_timings = new mutable.ListBuffer[Properties.T]
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   328
      val runtime_statistics = new mutable.ListBuffer[Properties.T]
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   329
      val task_statistics = new mutable.ListBuffer[Properties.T]
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   330
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   331
      def fun(
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   332
        name: String,
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   333
        acc: mutable.ListBuffer[Properties.T],
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   334
        unapply: Properties.T => Option[Properties.T]
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   335
      ): (String, Session.Protocol_Function) = {
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   336
        name -> ((msg: Prover.Protocol_Output) =>
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   337
          unapply(msg.properties) match {
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   338
            case Some(props) => acc += props; true
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   339
            case _ => false
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   340
          })
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   341
      }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   342
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   343
      session.init_protocol_handler(new Session.Protocol_Handler {
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   344
          override def exit(): Unit = Build_Session_Errors.cancel()
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   345
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   346
          private def build_session_finished(msg: Prover.Protocol_Output): Boolean = {
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   347
            val (rc, errors) =
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   348
              try {
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   349
                val (rc, errs) = {
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   350
                  import XML.Decode._
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   351
                  pair(int, list(x => x))(Symbol.decode_yxml(msg.text))
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   352
                }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   353
                val errors =
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   354
                  for (err <- errs) yield {
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   355
                    val prt = Protocol_Message.expose_no_reports(err)
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   356
                    Pretty.string_of(prt, metric = Symbol.Metric)
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   357
                  }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   358
                (rc, errors)
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   359
              }
74306
a117c076aa22 clarified signature;
wenzelm
parents: 74259
diff changeset
   360
              catch { case ERROR(err) => (Process_Result.RC.failure, List(err)) }
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   361
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   362
            session.protocol_command("Prover.stop", rc.toString)
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   363
            Build_Session_Errors(errors)
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   364
            true
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   365
          }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   366
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   367
          private def loading_theory(msg: Prover.Protocol_Output): Boolean =
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   368
            msg.properties match {
72692
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 72683
diff changeset
   369
              case Markup.Loading_Theory(Markup.Name(name)) =>
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   370
                progress.theory(Progress.Theory(name, session = session_name))
72692
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 72683
diff changeset
   371
                false
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   372
              case _ => false
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   373
            }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   374
74685
0ab5e35ac964 avoid conflict with future keyword;
wenzelm
parents: 74657
diff changeset
   375
          private def export_(msg: Prover.Protocol_Output): Boolean =
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   376
            msg.properties match {
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   377
              case Protocol.Export(args) =>
75762
985c3a64748c clarified signature: more uniform treatment of empty exports;
wenzelm
parents: 75738
diff changeset
   378
                export_consumer.make_entry(session_name, args, msg.chunk)
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   379
                true
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   380
              case _ => false
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   381
            }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   382
75440
39011d0d2128 tuned signature;
wenzelm
parents: 75438
diff changeset
   383
          override val functions: Session.Protocol_Functions =
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   384
            List(
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   385
              Markup.Build_Session_Finished.name -> build_session_finished,
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   386
              Markup.Loading_Theory.name -> loading_theory,
74685
0ab5e35ac964 avoid conflict with future keyword;
wenzelm
parents: 74657
diff changeset
   387
              Markup.EXPORT -> export_,
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   388
              fun(Markup.Theory_Timing.name, theory_timings, Markup.Theory_Timing.unapply),
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   389
              fun(Markup.Session_Timing.name, session_timings, Markup.Session_Timing.unapply),
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   390
              fun(Markup.Task_Statistics.name, task_statistics, Markup.Task_Statistics.unapply))
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   391
        })
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   392
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   393
      session.command_timings += Session.Consumer("command_timings") {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   394
        case Session.Command_Timing(props) =>
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   395
          for {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   396
            elapsed <- Markup.Elapsed.unapply(props)
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   397
            elapsed_time = Time.seconds(elapsed)
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   398
            if elapsed_time.is_relevant && elapsed_time >= options.seconds("command_timing_threshold")
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   399
          } command_timings += props.filter(Markup.command_timing_property)
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   400
      }
72711
9e89c2e15d36 clarified command_timings protocol;
wenzelm
parents: 72701
diff changeset
   401
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   402
      session.runtime_statistics += Session.Consumer("ML_statistics") {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   403
        case Session.Runtime_Statistics(props) => runtime_statistics += props
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   404
      }
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   405
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   406
      session.finished_theories += Session.Consumer[Document.Snapshot]("finished_theories") {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   407
        case snapshot =>
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   408
          if (!progress.stopped) {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   409
            def export_(name: String, xml: XML.Body, compress: Boolean = true): Unit = {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   410
              if (!progress.stopped) {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   411
                val theory_name = snapshot.node_name.theory
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   412
                val args =
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   413
                  Protocol.Export.Args(theory_name = theory_name, name = name, compress = compress)
75762
985c3a64748c clarified signature: more uniform treatment of empty exports;
wenzelm
parents: 75738
diff changeset
   414
                val body = Bytes(Symbol.encode(YXML.string_of_body(xml)))
985c3a64748c clarified signature: more uniform treatment of empty exports;
wenzelm
parents: 75738
diff changeset
   415
                export_consumer.make_entry(session_name, args, body)
74258
e942eedd9229 more reactive interrupt;
wenzelm
parents: 74255
diff changeset
   416
              }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   417
            }
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   418
            def export_text(name: String, text: String, compress: Boolean = true): Unit =
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   419
              export_(name, List(XML.Text(text)), compress = compress)
72730
01c9b3033036 more exports from rendering;
wenzelm
parents: 72726
diff changeset
   420
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   421
            for (command <- snapshot.snippet_command) {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   422
              export_text(Export.DOCUMENT_ID, command.id.toString, compress = false)
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   423
            }
72844
240c8a0f6337 clarified exports;
wenzelm
parents: 72817
diff changeset
   424
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   425
            export_text(Export.FILES,
76884
a004c5322ea4 clarified modules;
wenzelm
parents: 76881
diff changeset
   426
              cat_lines(snapshot.node_files.map(name => File.symbolic_path(name.path))),
a004c5322ea4 clarified modules;
wenzelm
parents: 76881
diff changeset
   427
              compress = false)
72816
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72783
diff changeset
   428
76930
9ce0aa145d21 more uniform operations;
wenzelm
parents: 76913
diff changeset
   429
            for ((blob_name, i) <- snapshot.node_files.tail.zipWithIndex) {
9ce0aa145d21 more uniform operations;
wenzelm
parents: 76913
diff changeset
   430
              val xml = snapshot.switch(blob_name).xml_markup()
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   431
              export_(Export.MARKUP + (i + 1), xml)
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   432
            }
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   433
            export_(Export.MARKUP, snapshot.xml_markup())
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   434
            export_(Export.MESSAGES, snapshot.messages.map(_._1))
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   435
          }
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   436
      }
72692
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 72683
diff changeset
   437
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   438
      session.all_messages += Session.Consumer[Any]("build_session_output") {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   439
        case msg: Prover.Output =>
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   440
          val message = msg.message
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   441
          if (msg.is_system) resources.log(Protocol.message_text(message))
73774
734d5d3fbd9d syslog option for "isabelle build";
wenzelm
parents: 73719
diff changeset
   442
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   443
          if (msg.is_stdout) {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   444
            stdout ++= Symbol.encode(XML.content(message))
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   445
          }
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   446
          else if (msg.is_stderr) {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   447
            stderr ++= Symbol.encode(XML.content(message))
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   448
          }
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   449
          else if (msg.is_exit) {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   450
            val err =
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   451
              "Prover terminated" +
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   452
                (msg.properties match {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   453
                  case Markup.Process_Result(result) => ": " + result.print_rc
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   454
                  case _ => ""
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   455
                })
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   456
            Build_Session_Errors(List(err))
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   457
          }
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   458
        case _ =>
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   459
      }
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   460
73805
b73777a0c076 allow build session setup, e.g. for protocol handlers;
wenzelm
parents: 73804
diff changeset
   461
      session_setup(session_name, session)
b73777a0c076 allow build session setup, e.g. for protocol handlers;
wenzelm
parents: 73804
diff changeset
   462
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   463
      val eval_main = Command_Line.ML_tool("Isabelle_Process.init_build ()" :: eval_store)
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   464
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   465
      val process =
76729
b045b40a65cc clarified signature;
wenzelm
parents: 76672
diff changeset
   466
        Isabelle_Process.start(store, options, session, session_background,
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   467
          logic = parent, raw_ml_system = is_pure,
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   468
          use_prelude = use_prelude, eval_main = eval_main,
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   469
          cwd = info.dir.file, env = env)
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   470
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   471
      val build_errors =
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   472
        Isabelle_Thread.interrupt_handler(_ => process.terminate()) {
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   473
          Exn.capture { process.await_startup() } match {
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   474
            case Exn.Res(_) =>
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   475
              val resources_yxml = resources.init_session_yxml
74144
f9f6a31cc99c proper prover_options for batch-build;
wenzelm
parents: 74067
diff changeset
   476
              val encode_options: XML.Encode.T[Options] =
f9f6a31cc99c proper prover_options for batch-build;
wenzelm
parents: 74067
diff changeset
   477
                options => session.prover_options(options).encode
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   478
              val args_yxml =
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   479
                YXML.string_of_body(
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   480
                  {
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   481
                    import XML.Encode._
74144
f9f6a31cc99c proper prover_options for batch-build;
wenzelm
parents: 74067
diff changeset
   482
                    pair(string, list(pair(encode_options, list(pair(string, properties)))))(
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   483
                      (session_name, info.theories))
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   484
                  })
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   485
              session.protocol_command("build_session", resources_yxml, args_yxml)
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   486
              Build_Session_Errors.result
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   487
            case Exn.Exn(exn) => Exn.Res(List(Exn.message(exn)))
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   488
          }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   489
        }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   490
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   491
      val process_result =
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   492
        Isabelle_Thread.interrupt_handler(_ => process.terminate()) { process.await_shutdown() }
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   493
72697
e16f85e3c288 more robust;
wenzelm
parents: 72696
diff changeset
   494
      session.stop()
e16f85e3c288 more robust;
wenzelm
parents: 72696
diff changeset
   495
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   496
      val export_errors =
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   497
        export_consumer.shutdown(close = true).map(Output.error_message_text)
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   498
72699
ed59a506998f clarified document_output vs. progress;
wenzelm
parents: 72697
diff changeset
   499
      val (document_output, document_errors) =
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   500
        try {
72881
220a094a42d8 proper else statement;
wenzelm
parents: 72879
diff changeset
   501
          if (build_errors.isInstanceOf[Exn.Res[_]] && process_result.ok && info.documents.nonEmpty) {
75786
ff6c1a82270f clarified modules;
wenzelm
parents: 75782
diff changeset
   502
            using(Export.open_database_context(store)) { database_context =>
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   503
              val documents =
76668
dd03c91cda43 clarified signature;
wenzelm
parents: 76667
diff changeset
   504
                using(database_context.open_session(session_background)) {
75782
dba571dd0ba9 clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents: 75780
diff changeset
   505
                  session_context =>
dba571dd0ba9 clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents: 75780
diff changeset
   506
                    Document_Build.build_documents(
dba571dd0ba9 clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents: 75780
diff changeset
   507
                      Document_Build.context(session_context, progress = progress),
dba571dd0ba9 clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents: 75780
diff changeset
   508
                      output_sources = info.document_output,
dba571dd0ba9 clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents: 75780
diff changeset
   509
                      output_pdf = info.document_output)
dba571dd0ba9 clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents: 75780
diff changeset
   510
                }
75970
b4a04fa01677 tuned signature: more general operations;
wenzelm
parents: 75914
diff changeset
   511
              using(database_context.open_database(session_name, output = true))(session_database =>
75787
f9fcf06aa2eb clarified signature;
wenzelm
parents: 75786
diff changeset
   512
                documents.foreach(_.write(session_database.db, session_name)))
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   513
              (documents.flatMap(_.log_lines), Nil)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   514
            }
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   515
          }
72881
220a094a42d8 proper else statement;
wenzelm
parents: 72879
diff changeset
   516
          else (Nil, Nil)
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   517
        }
72882
1dc2ad97e062 more informative error;
wenzelm
parents: 72881
diff changeset
   518
        catch {
76734
b4a9c907e062 more accurate error messages;
wenzelm
parents: 76729
diff changeset
   519
          case exn: Document_Build.Build_Error => (exn.log_lines, exn.log_errors)
72882
1dc2ad97e062 more informative error;
wenzelm
parents: 72881
diff changeset
   520
          case Exn.Interrupt.ERROR(msg) => (Nil, List(msg))
1dc2ad97e062 more informative error;
wenzelm
parents: 72881
diff changeset
   521
        }
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   522
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   523
      val result = {
72738
a4d7da18ac5c store timings for used_theories in canonical order, with reconstructed store.read_theories;
wenzelm
parents: 72730
diff changeset
   524
        val theory_timing =
75438
wenzelm
parents: 75425
diff changeset
   525
          theory_timings.iterator.flatMap(
75425
b958e053d993 avoid pattern-match warnings, notably in scala3;
wenzelm
parents: 75394
diff changeset
   526
            {
75438
wenzelm
parents: 75425
diff changeset
   527
              case props @ Markup.Name(name) => Some(name -> props)
wenzelm
parents: 75425
diff changeset
   528
              case _ => None
75425
b958e053d993 avoid pattern-match warnings, notably in scala3;
wenzelm
parents: 75394
diff changeset
   529
            }).toMap
72738
a4d7da18ac5c store timings for used_theories in canonical order, with reconstructed store.read_theories;
wenzelm
parents: 72730
diff changeset
   530
        val used_theory_timings =
76668
dd03c91cda43 clarified signature;
wenzelm
parents: 76667
diff changeset
   531
          for { (name, _) <- session_background.base.used_theories }
72738
a4d7da18ac5c store timings for used_theories in canonical order, with reconstructed store.read_theories;
wenzelm
parents: 72730
diff changeset
   532
            yield theory_timing.getOrElse(name.theory, Markup.Name(name.theory))
a4d7da18ac5c store timings for used_theories in canonical order, with reconstructed store.read_theories;
wenzelm
parents: 72730
diff changeset
   533
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   534
        val more_output =
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   535
          Library.trim_line(stdout.toString) ::
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   536
            command_timings.toList.map(Protocol.Command_Timing_Marker.apply) :::
72738
a4d7da18ac5c store timings for used_theories in canonical order, with reconstructed store.read_theories;
wenzelm
parents: 72730
diff changeset
   537
            used_theory_timings.map(Protocol.Theory_Timing_Marker.apply) :::
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   538
            session_timings.toList.map(Protocol.Session_Timing_Marker.apply) :::
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   539
            runtime_statistics.toList.map(Protocol.ML_Statistics_Marker.apply) :::
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   540
            task_statistics.toList.map(Protocol.Task_Statistics_Marker.apply) :::
72699
ed59a506998f clarified document_output vs. progress;
wenzelm
parents: 72697
diff changeset
   541
            document_output
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   542
72726
ec6a27bbdab8 proper return code for more errors (amending d892f6d66402);
wenzelm
parents: 72725
diff changeset
   543
        process_result.output(more_output)
ec6a27bbdab8 proper return code for more errors (amending d892f6d66402);
wenzelm
parents: 72725
diff changeset
   544
          .error(Library.trim_line(stderr.toString))
ec6a27bbdab8 proper return code for more errors (amending d892f6d66402);
wenzelm
parents: 72725
diff changeset
   545
          .errors_rc(export_errors ::: document_errors)
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   546
      }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   547
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   548
      build_errors match {
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   549
        case Exn.Res(build_errs) =>
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   550
          val errs = build_errs ::: document_errors
74259
6d48d6ba58df more robust: progress.stopped means that build has failed;
wenzelm
parents: 74258
diff changeset
   551
          if (errs.nonEmpty) {
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   552
            result.error_rc.output(
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   553
              errs.flatMap(s => split_lines(Output.error_message_text(s))) :::
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   554
                errs.map(Protocol.Error_Message_Marker.apply))
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   555
          }
74306
a117c076aa22 clarified signature;
wenzelm
parents: 74259
diff changeset
   556
          else if (progress.stopped && result.ok) result.copy(rc = Process_Result.RC.interrupt)
74259
6d48d6ba58df more robust: progress.stopped means that build has failed;
wenzelm
parents: 74258
diff changeset
   557
          else result
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   558
        case Exn.Exn(Exn.Interrupt()) =>
74306
a117c076aa22 clarified signature;
wenzelm
parents: 74259
diff changeset
   559
          if (result.ok) result.copy(rc = Process_Result.RC.interrupt)
74259
6d48d6ba58df more robust: progress.stopped means that build has failed;
wenzelm
parents: 74258
diff changeset
   560
          else result
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   561
        case Exn.Exn(exn) => throw exn
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   562
      }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   563
    }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   564
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   565
  def terminate(): Unit = future_result.cancel()
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   566
  def is_finished: Boolean = future_result.is_finished
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   567
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
   568
  private val timeout_request: Option[Event_Timer.Request] = {
73700
908351c8c0b1 check timeout_ignored as in ML, before applying timeout_scale;
wenzelm
parents: 73559
diff changeset
   569
    if (info.timeout_ignored) None
908351c8c0b1 check timeout_ignored as in ML, before applying timeout_scale;
wenzelm
parents: 73559
diff changeset
   570
    else Some(Event_Timer.request(Time.now() + info.timeout) { terminate() })
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   571
  }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   572
77236
dce91dab1728 clarified signature;
wenzelm
parents: 77207
diff changeset
   573
  def join: Process_Result = {
dce91dab1728 clarified signature;
wenzelm
parents: 77207
diff changeset
   574
    val result = future_result.join
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   575
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   576
    val was_timeout =
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   577
      timeout_request match {
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   578
        case None => false
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   579
        case Some(request) => !request.cancel()
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   580
      }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   581
77236
dce91dab1728 clarified signature;
wenzelm
parents: 77207
diff changeset
   582
    if (result.ok) result
dce91dab1728 clarified signature;
wenzelm
parents: 77207
diff changeset
   583
    else if (was_timeout) result.error(Output.error_message_text("Timeout")).timeout_rc
dce91dab1728 clarified signature;
wenzelm
parents: 77207
diff changeset
   584
    else if (result.interrupted) result.error(Output.error_message_text("Interrupt"))
dce91dab1728 clarified signature;
wenzelm
parents: 77207
diff changeset
   585
    else result
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   586
  }
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
   587
}