src/Pure/Tools/build.ML
author wenzelm
Sat, 11 Jul 2020 16:58:38 +0200
changeset 72019 940195fbb282
parent 72012 c81e58a81b8c
child 72103 7b318273a4aa
permissions -rw-r--r--
clarified messages;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50686
d703e3aafa8c moved files;
wenzelm
parents: 50683
diff changeset
     1
(*  Title:      Pure/Tools/build.ML
48418
1a634f9614fb some actual build function on ML side;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
1a634f9614fb some actual build function on ML side;
wenzelm
parents:
diff changeset
     3
1a634f9614fb some actual build function on ML side;
wenzelm
parents:
diff changeset
     4
Build Isabelle sessions.
1a634f9614fb some actual build function on ML side;
wenzelm
parents:
diff changeset
     5
*)
1a634f9614fb some actual build function on ML side;
wenzelm
parents:
diff changeset
     6
1a634f9614fb some actual build function on ML side;
wenzelm
parents:
diff changeset
     7
signature BUILD =
1a634f9614fb some actual build function on ML side;
wenzelm
parents:
diff changeset
     8
sig
48731
a45ba78abcc1 more casual exit back to ML toplevel, to accomodate commit in SML/NJ which continues at the saved point;
wenzelm
parents: 48681
diff changeset
     9
  val build: string -> unit
48418
1a634f9614fb some actual build function on ML side;
wenzelm
parents:
diff changeset
    10
end;
1a634f9614fb some actual build function on ML side;
wenzelm
parents:
diff changeset
    11
1a634f9614fb some actual build function on ML side;
wenzelm
parents:
diff changeset
    12
structure Build: BUILD =
1a634f9614fb some actual build function on ML side;
wenzelm
parents:
diff changeset
    13
struct
1a634f9614fb some actual build function on ML side;
wenzelm
parents:
diff changeset
    14
51662
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    15
(* command timings *)
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    16
56615
47c1dbeec36a tuned comments;
wenzelm
parents: 56614
diff changeset
    17
type timings = ((string * Time.time) Inttab.table) Symtab.table;  (*file -> offset -> name, time*)
51662
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    18
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    19
val empty_timings: timings = Symtab.empty;
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    20
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    21
fun update_timings props =
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    22
  (case Markup.parse_command_timing_properties props of
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    23
    SOME ({file, offset, name}, time) =>
51666
b97aeb018900 add command timings (like document command status);
wenzelm
parents: 51662
diff changeset
    24
      Symtab.map_default (file, Inttab.empty)
62826
eb94e570c1a4 prefer infix operations;
wenzelm
parents: 62793
diff changeset
    25
        (Inttab.map_default (offset, (name, time)) (fn (_, t) => (name, t + time)))
51662
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    26
  | NONE => I);
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    27
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    28
fun approximative_id name pos =
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    29
  (case (Position.file_of pos, Position.offset_of pos) of
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    30
    (SOME file, SOME offset) =>
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    31
      if name = "" then NONE else SOME {file = file, offset = offset, name = name}
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    32
  | _ => NONE);
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    33
65058
3e9f382fb67e absent timing information means zero, according to 0070053570c4, f235646b1b73;
wenzelm
parents: 64599
diff changeset
    34
fun get_timings timings tr =
51662
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    35
  (case approximative_id (Toplevel.name_of tr) (Toplevel.pos_of tr) of
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    36
    SOME {file, offset, name} =>
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    37
      (case Symtab.lookup timings file of
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    38
        SOME offsets =>
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    39
          (case Inttab.lookup offsets offset of
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    40
            SOME (name', time) => if name = name' then SOME time else NONE
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    41
          | NONE => NONE)
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    42
      | NONE => NONE)
65058
3e9f382fb67e absent timing information means zero, according to 0070053570c4, f235646b1b73;
wenzelm
parents: 64599
diff changeset
    43
  | NONE => NONE)
3e9f382fb67e absent timing information means zero, according to 0070053570c4, f235646b1b73;
wenzelm
parents: 64599
diff changeset
    44
  |> the_default Time.zeroTime;
51662
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    45
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    46
52052
892061142ba6 discontinued obsolete isabelle usedir, mkdir, make;
wenzelm
parents: 52041
diff changeset
    47
(* session timing *)
892061142ba6 discontinued obsolete isabelle usedir, mkdir, make;
wenzelm
parents: 52041
diff changeset
    48
72019
940195fbb282 clarified messages;
wenzelm
parents: 72012
diff changeset
    49
fun session_timing f x =
52052
892061142ba6 discontinued obsolete isabelle usedir, mkdir, make;
wenzelm
parents: 52041
diff changeset
    50
  let
892061142ba6 discontinued obsolete isabelle usedir, mkdir, make;
wenzelm
parents: 52041
diff changeset
    51
    val start = Timing.start ();
892061142ba6 discontinued obsolete isabelle usedir, mkdir, make;
wenzelm
parents: 52041
diff changeset
    52
    val y = f x;
892061142ba6 discontinued obsolete isabelle usedir, mkdir, make;
wenzelm
parents: 52041
diff changeset
    53
    val timing = Timing.result start;
892061142ba6 discontinued obsolete isabelle usedir, mkdir, make;
wenzelm
parents: 52041
diff changeset
    54
62925
f1bdf10f95d8 tuned signature;
wenzelm
parents: 62884
diff changeset
    55
    val threads = string_of_int (Multithreading.max_threads ());
72019
940195fbb282 clarified messages;
wenzelm
parents: 72012
diff changeset
    56
    val props = [("threads", threads)] @ Markup.timing_properties timing;
940195fbb282 clarified messages;
wenzelm
parents: 72012
diff changeset
    57
    val _ = Output.protocol_message (Markup.session_timing :: props) [];
52052
892061142ba6 discontinued obsolete isabelle usedir, mkdir, make;
wenzelm
parents: 52041
diff changeset
    58
  in y end;
892061142ba6 discontinued obsolete isabelle usedir, mkdir, make;
wenzelm
parents: 52041
diff changeset
    59
892061142ba6 discontinued obsolete isabelle usedir, mkdir, make;
wenzelm
parents: 52041
diff changeset
    60
50683
34b109c5324c inline ML statistics into build log;
wenzelm
parents: 50280
diff changeset
    61
(* protocol messages *)
34b109c5324c inline ML statistics into build log;
wenzelm
parents: 50280
diff changeset
    62
34b109c5324c inline ML statistics into build log;
wenzelm
parents: 50280
diff changeset
    63
fun protocol_message props output =
51216
e6e7685fc8f8 emit command_timing properties into build log;
wenzelm
parents: 51045
diff changeset
    64
  (case props of
e6e7685fc8f8 emit command_timing properties into build log;
wenzelm
parents: 51045
diff changeset
    65
    function :: args =>
51662
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    66
      if function = Markup.ML_statistics orelse function = Markup.task_statistics then
71622
ab5009192ebb tuned signature -- follow Scala;
wenzelm
parents: 71619
diff changeset
    67
        Protocol_Message.marker (#2 function) args
51662
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    68
      else if function = Markup.command_timing then
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    69
        let
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    70
          val name = the_default "" (Properties.get args Markup.nameN);
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    71
          val pos = Position.of_properties args;
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    72
          val {elapsed, ...} = Markup.parse_timing_properties args;
62793
f235646b1b73 less bulky timing information, e.g. HOL 56913 ~> 672;
wenzelm
parents: 62715
diff changeset
    73
          val is_significant =
f235646b1b73 less bulky timing information, e.g. HOL 56913 ~> 672;
wenzelm
parents: 62715
diff changeset
    74
            Timing.is_relevant_time elapsed andalso
62826
eb94e570c1a4 prefer infix operations;
wenzelm
parents: 62793
diff changeset
    75
            elapsed >= Options.default_seconds "command_timing_threshold";
51662
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    76
        in
62793
f235646b1b73 less bulky timing information, e.g. HOL 56913 ~> 672;
wenzelm
parents: 62715
diff changeset
    77
          if is_significant then
59149
0070053570c4 suppress irrelevant timing messages (the majority);
wenzelm
parents: 59058
diff changeset
    78
            (case approximative_id name pos of
70907
7e3f25a0cee4 proper protocol_message for bootstrap proofs;
wenzelm
parents: 70712
diff changeset
    79
              SOME id =>
71622
ab5009192ebb tuned signature -- follow Scala;
wenzelm
parents: 71619
diff changeset
    80
                Protocol_Message.marker (#2 function)
71619
e33f6e5f86b6 clarified protocol messages: explicitly use physical_writeln, always encode_lines;
wenzelm
parents: 71618
diff changeset
    81
                  (Markup.command_timing_properties id elapsed)
59149
0070053570c4 suppress irrelevant timing messages (the majority);
wenzelm
parents: 59058
diff changeset
    82
            | NONE => ())
0070053570c4 suppress irrelevant timing messages (the majority);
wenzelm
parents: 59058
diff changeset
    83
          else ()
51662
3391a493f39a just one timing protocol function, with 3 implementations: TTY/PG, PIDE/document, build;
wenzelm
parents: 51661
diff changeset
    84
        end
72012
c81e58a81b8c clarified inlined protocol messages;
wenzelm
parents: 72011
diff changeset
    85
      else if function = Markup.theory_timing orelse function = Markup.session_timing then
71622
ab5009192ebb tuned signature -- follow Scala;
wenzelm
parents: 71619
diff changeset
    86
        Protocol_Message.marker (#2 function) args
51216
e6e7685fc8f8 emit command_timing properties into build log;
wenzelm
parents: 51045
diff changeset
    87
      else
e6e7685fc8f8 emit command_timing properties into build log;
wenzelm
parents: 51045
diff changeset
    88
        (case Markup.dest_loading_theory props of
71622
ab5009192ebb tuned signature -- follow Scala;
wenzelm
parents: 71619
diff changeset
    89
          SOME name => Protocol_Message.marker_text "loading_theory" name
70907
7e3f25a0cee4 proper protocol_message for bootstrap proofs;
wenzelm
parents: 70712
diff changeset
    90
        | NONE => Export.protocol_message props output)
51661
92e58b76dbb1 clarified protocol_message undefinedness;
wenzelm
parents: 51553
diff changeset
    91
  | [] => raise Output.Protocol_Message props);
51045
630c0895d9d1 more efficient inlined properties, especially relevant for voluminous tasks trace;
wenzelm
parents: 50982
diff changeset
    92
50683
34b109c5324c inline ML statistics into build log;
wenzelm
parents: 50280
diff changeset
    93
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
    94
(* build theories *)
50683
34b109c5324c inline ML statistics into build log;
wenzelm
parents: 50280
diff changeset
    95
67297
86a099f896fc formal check of @{cite} bibtex entries -- only in batch-mode session builds;
wenzelm
parents: 67219
diff changeset
    96
fun build_theories symbols bibtex_entries last_timing qualifier master_dir (options, thys) =
59366
e94df7f6b608 clarified build_theories: proper protocol handler;
wenzelm
parents: 59364
diff changeset
    97
  let
68179
da70c9e91135 clarified signature: more explicit type "context" with full options;
wenzelm
parents: 68148
diff changeset
    98
    val context =
da70c9e91135 clarified signature: more explicit type "context" with full options;
wenzelm
parents: 68148
diff changeset
    99
      {options = options, symbols = symbols, bibtex_entries = bibtex_entries,
da70c9e91135 clarified signature: more explicit type "context" with full options;
wenzelm
parents: 68148
diff changeset
   100
        last_timing = last_timing};
59366
e94df7f6b608 clarified build_theories: proper protocol handler;
wenzelm
parents: 59364
diff changeset
   101
    val condition = space_explode "," (Options.string options "condition");
e94df7f6b608 clarified build_theories: proper protocol handler;
wenzelm
parents: 59364
diff changeset
   102
    val conds = filter_out (can getenv_strict) condition;
e94df7f6b608 clarified build_theories: proper protocol handler;
wenzelm
parents: 59364
diff changeset
   103
  in
e94df7f6b608 clarified build_theories: proper protocol handler;
wenzelm
parents: 59364
diff changeset
   104
    if null conds then
69755
2fc85ce1f557 discontinued obsolete option "checkpoint";
wenzelm
parents: 68511
diff changeset
   105
      (Options.set_default options;
62714
63888e5f668b clarified use of options;
wenzelm
parents: 62713
diff changeset
   106
        Isabelle_Process.init_options ();
64599
80ef54198f44 dummy fork to produce ML_statistics even in sequential mode (e.g. for heap size);
wenzelm
parents: 64308
diff changeset
   107
        Future.fork I;
68179
da70c9e91135 clarified signature: more explicit type "context" with full options;
wenzelm
parents: 68148
diff changeset
   108
        (Thy_Info.use_theories context qualifier master_dir
64308
b00508facb4f added system option "profiling";
wenzelm
parents: 63827
diff changeset
   109
        |>
b00508facb4f added system option "profiling";
wenzelm
parents: 63827
diff changeset
   110
          (case Options.string options "profiling" of
b00508facb4f added system option "profiling";
wenzelm
parents: 63827
diff changeset
   111
            "" => I
b00508facb4f added system option "profiling";
wenzelm
parents: 63827
diff changeset
   112
          | "time" => profile_time
b00508facb4f added system option "profiling";
wenzelm
parents: 63827
diff changeset
   113
          | "allocations" => profile_allocations
b00508facb4f added system option "profiling";
wenzelm
parents: 63827
diff changeset
   114
          | bad => error ("Bad profiling option: " ^ quote bad))
59366
e94df7f6b608 clarified build_theories: proper protocol handler;
wenzelm
parents: 59364
diff changeset
   115
        |> Unsynchronized.setmp print_mode
62714
63888e5f668b clarified use of options;
wenzelm
parents: 62713
diff changeset
   116
            (space_explode "," (Options.string options "print_mode") @ print_mode_value ())) thys)
59366
e94df7f6b608 clarified build_theories: proper protocol handler;
wenzelm
parents: 59364
diff changeset
   117
    else
e94df7f6b608 clarified build_theories: proper protocol handler;
wenzelm
parents: 59364
diff changeset
   118
      Output.physical_stderr ("Skipping theories " ^ commas_quote (map #1 thys) ^
e94df7f6b608 clarified build_theories: proper protocol handler;
wenzelm
parents: 59364
diff changeset
   119
        " (undefined " ^ commas conds ^ ")\n")
e94df7f6b608 clarified build_theories: proper protocol handler;
wenzelm
parents: 59364
diff changeset
   120
  end;
e94df7f6b608 clarified build_theories: proper protocol handler;
wenzelm
parents: 59364
diff changeset
   121
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   122
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   123
(* build session *)
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   124
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   125
datatype args = Args of
71876
ad063ac1f617 more robust: explicit check for PIDE session;
wenzelm
parents: 71875
diff changeset
   126
 {pide: bool,
ad063ac1f617 more robust: explicit check for PIDE session;
wenzelm
parents: 71875
diff changeset
   127
  symbol_codes: (string * int) list,
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   128
  command_timings: Properties.T list,
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   129
  verbose: bool,
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   130
  browser_info: Path.T,
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   131
  document_files: (Path.T * Path.T) list,
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   132
  graph_file: Path.T,
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   133
  parent_name: string,
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   134
  chapter: string,
71669
12ebd8d0deee tuned signature;
wenzelm
parents: 71667
diff changeset
   135
  session_name: string,
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   136
  master_dir: Path.T,
65431
4a3e6cda3b94 provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents: 65318
diff changeset
   137
  theories: (Options.T * (string * Position.T) list) list,
70683
8c7706b053c7 find theory files via session structure: much faster Prover IDE startup;
wenzelm
parents: 69755
diff changeset
   138
  session_positions: (string * Properties.T) list,
8c7706b053c7 find theory files via session structure: much faster Prover IDE startup;
wenzelm
parents: 69755
diff changeset
   139
  session_directories: (string * string) list,
67471
bddfa23a4ea9 formal treatment of documentation names;
wenzelm
parents: 67297
diff changeset
   140
  doc_names: string list,
65457
2bf0d2fcd506 proper import qualifier for global theories;
wenzelm
parents: 65445
diff changeset
   141
  global_theories: (string * string) list,
66712
4c98c929a12a session-qualified theory names are mandatory;
wenzelm
parents: 66048
diff changeset
   142
  loaded_theories: string list,
67297
86a099f896fc formal check of @{cite} bibtex entries -- only in batch-mode session builds;
wenzelm
parents: 67219
diff changeset
   143
  bibtex_entries: string list};
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   144
71876
ad063ac1f617 more robust: explicit check for PIDE session;
wenzelm
parents: 71875
diff changeset
   145
fun decode_args pide yxml =
62630
bc772694cfbd ML save_state under control of Isabelle/Scala;
wenzelm
parents: 62599
diff changeset
   146
  let
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   147
    open XML.Decode;
65517
1544e61e5314 more position information;
wenzelm
parents: 65478
diff changeset
   148
    val position = Position.of_properties o properties;
71611
fb6953e77000 eliminated pointless flag (see also 6533ceee4cd7);
wenzelm
parents: 70991
diff changeset
   149
    val (symbol_codes, (command_timings, (verbose, (browser_info,
71669
12ebd8d0deee tuned signature;
wenzelm
parents: 71667
diff changeset
   150
      (document_files, (graph_file, (parent_name, (chapter, (session_name, (master_dir,
70683
8c7706b053c7 find theory files via session structure: much faster Prover IDE startup;
wenzelm
parents: 69755
diff changeset
   151
      (theories, (session_positions, (session_directories, (doc_names, (global_theories,
71611
fb6953e77000 eliminated pointless flag (see also 6533ceee4cd7);
wenzelm
parents: 70991
diff changeset
   152
      (loaded_theories, bibtex_entries)))))))))))))))) =
fb6953e77000 eliminated pointless flag (see also 6533ceee4cd7);
wenzelm
parents: 70991
diff changeset
   153
      pair (list (pair string int)) (pair (list properties) (pair bool (pair string
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   154
        (pair (list (pair string string)) (pair string (pair string (pair string (pair string
65431
4a3e6cda3b94 provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents: 65318
diff changeset
   155
          (pair string
65517
1544e61e5314 more position information;
wenzelm
parents: 65478
diff changeset
   156
            (pair (((list (pair Options.decode (list (pair string position))))))
70683
8c7706b053c7 find theory files via session structure: much faster Prover IDE startup;
wenzelm
parents: 69755
diff changeset
   157
              (pair (list (pair string properties))
67493
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67471
diff changeset
   158
                (pair (list (pair string string)) (pair (list string)
71611
fb6953e77000 eliminated pointless flag (see also 6533ceee4cd7);
wenzelm
parents: 70991
diff changeset
   159
                  (pair (list (pair string string)) (pair (list string) (list string))))))))))))))))
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   160
      (YXML.parse_body yxml);
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   161
  in
71876
ad063ac1f617 more robust: explicit check for PIDE session;
wenzelm
parents: 71875
diff changeset
   162
    Args {pide = pide, symbol_codes = symbol_codes, command_timings = command_timings,
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   163
      verbose = verbose, browser_info = Path.explode browser_info,
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   164
      document_files = map (apply2 Path.explode) document_files,
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   165
      graph_file = Path.explode graph_file, parent_name = parent_name, chapter = chapter,
71669
12ebd8d0deee tuned signature;
wenzelm
parents: 71667
diff changeset
   166
      session_name = session_name, master_dir = Path.explode master_dir, theories = theories,
70683
8c7706b053c7 find theory files via session structure: much faster Prover IDE startup;
wenzelm
parents: 69755
diff changeset
   167
      session_positions = session_positions, session_directories = session_directories,
67471
bddfa23a4ea9 formal treatment of documentation names;
wenzelm
parents: 67297
diff changeset
   168
      doc_names = doc_names, global_theories = global_theories, loaded_theories = loaded_theories,
70712
a3cfe859d915 find theories via session directories only -- ignore known_theories;
wenzelm
parents: 70683
diff changeset
   169
      bibtex_entries = bibtex_entries}
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   170
  end;
48418
1a634f9614fb some actual build function on ML side;
wenzelm
parents:
diff changeset
   171
71876
ad063ac1f617 more robust: explicit check for PIDE session;
wenzelm
parents: 71875
diff changeset
   172
fun build_session (Args {pide, symbol_codes, command_timings, verbose, browser_info, document_files,
71669
12ebd8d0deee tuned signature;
wenzelm
parents: 71667
diff changeset
   173
    graph_file, parent_name, chapter, session_name, master_dir, theories, session_positions,
70712
a3cfe859d915 find theories via session directories only -- ignore known_theories;
wenzelm
parents: 70683
diff changeset
   174
    session_directories, doc_names, global_theories, loaded_theories, bibtex_entries}) =
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   175
  let
62630
bc772694cfbd ML save_state under control of Isabelle/Scala;
wenzelm
parents: 62599
diff changeset
   176
    val symbols = HTML.make_symbols symbol_codes;
51941
ead4248aef3b full default options for Isabelle_Process and Build;
wenzelm
parents: 51666
diff changeset
   177
65441
9425e4d8bdb6 more session_base information in ML;
wenzelm
parents: 65431
diff changeset
   178
    val _ =
71875
aaa984499d36 tuned signature;
wenzelm
parents: 71669
diff changeset
   179
      Resources.init_session
71876
ad063ac1f617 more robust: explicit check for PIDE session;
wenzelm
parents: 71875
diff changeset
   180
        {pide = pide,
ad063ac1f617 more robust: explicit check for PIDE session;
wenzelm
parents: 71875
diff changeset
   181
         session_positions = session_positions,
70683
8c7706b053c7 find theory files via session structure: much faster Prover IDE startup;
wenzelm
parents: 69755
diff changeset
   182
         session_directories = session_directories,
67493
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67471
diff changeset
   183
         docs = doc_names,
67219
81e9804b2014 added document antiquotation @{session name};
wenzelm
parents: 66873
diff changeset
   184
         global_theories = global_theories,
70712
a3cfe859d915 find theories via session directories only -- ignore known_theories;
wenzelm
parents: 70683
diff changeset
   185
         loaded_theories = loaded_theories};
65431
4a3e6cda3b94 provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents: 65318
diff changeset
   186
62630
bc772694cfbd ML save_state under control of Isabelle/Scala;
wenzelm
parents: 62599
diff changeset
   187
    val _ =
bc772694cfbd ML save_state under control of Isabelle/Scala;
wenzelm
parents: 62599
diff changeset
   188
      Session.init
bc772694cfbd ML save_state under control of Isabelle/Scala;
wenzelm
parents: 62599
diff changeset
   189
        symbols
bc772694cfbd ML save_state under control of Isabelle/Scala;
wenzelm
parents: 62599
diff changeset
   190
        (Options.default_bool "browser_info")
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   191
        browser_info
62630
bc772694cfbd ML save_state under control of Isabelle/Scala;
wenzelm
parents: 62599
diff changeset
   192
        (Options.default_string "document")
bc772694cfbd ML save_state under control of Isabelle/Scala;
wenzelm
parents: 62599
diff changeset
   193
        (Options.default_string "document_output")
68511
c6626358bf21 tuned signature;
wenzelm
parents: 68227
diff changeset
   194
        (Present.document_variants (Options.default ()))
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   195
        document_files
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   196
        graph_file
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   197
        parent_name
71669
12ebd8d0deee tuned signature;
wenzelm
parents: 71667
diff changeset
   198
        (chapter, session_name)
62630
bc772694cfbd ML save_state under control of Isabelle/Scala;
wenzelm
parents: 62599
diff changeset
   199
        verbose;
49911
262c36fd5f26 collective errors from use_thys and Session.finish/Goal.finish_futures -- avoid uninformative interrupts stemming from failure of goal forks that are not registered in the theory (e.g. unnamed theorems);
wenzelm
parents: 49242
diff changeset
   200
65058
3e9f382fb67e absent timing information means zero, according to 0070053570c4, f235646b1b73;
wenzelm
parents: 64599
diff changeset
   201
    val last_timing = get_timings (fold update_timings command_timings empty_timings);
51218
6425a0d3b7ac support for build passing timings from Scala to ML;
wenzelm
parents: 51217
diff changeset
   202
62630
bc772694cfbd ML save_state under control of Isabelle/Scala;
wenzelm
parents: 62599
diff changeset
   203
    val res1 =
bc772694cfbd ML save_state under control of Isabelle/Scala;
wenzelm
parents: 62599
diff changeset
   204
      theories |>
71669
12ebd8d0deee tuned signature;
wenzelm
parents: 71667
diff changeset
   205
        (List.app (build_theories symbols bibtex_entries last_timing session_name master_dir)
72019
940195fbb282 clarified messages;
wenzelm
parents: 72012
diff changeset
   206
          |> session_timing
62630
bc772694cfbd ML save_state under control of Isabelle/Scala;
wenzelm
parents: 62599
diff changeset
   207
          |> Exn.capture);
bc772694cfbd ML save_state under control of Isabelle/Scala;
wenzelm
parents: 62599
diff changeset
   208
    val res2 = Exn.capture Session.finish ();
65431
4a3e6cda3b94 provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents: 65318
diff changeset
   209
65478
7c40477e0a87 clarified init_session_base / finish_session_base: retain some information for plain "isabelle process", without rechecking dependencies as in "isabelle console";
wenzelm
parents: 65457
diff changeset
   210
    val _ = Resources.finish_session_base ();
62630
bc772694cfbd ML save_state under control of Isabelle/Scala;
wenzelm
parents: 62599
diff changeset
   211
    val _ = Par_Exn.release_all [res1, res2];
71613
wenzelm
parents: 71611
diff changeset
   212
    val _ =
71669
12ebd8d0deee tuned signature;
wenzelm
parents: 71667
diff changeset
   213
      if session_name = Context.PureN
12ebd8d0deee tuned signature;
wenzelm
parents: 71667
diff changeset
   214
      then Theory.install_pure (Thy_Info.get_theory Context.PureN) else ();
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   215
  in () end;
48673
b2b09970c571 let with_timing report overall number of threads;
wenzelm
parents: 48672
diff changeset
   216
71631
3f02bc5a5a03 more accurate treatment of errors;
wenzelm
parents: 71622
diff changeset
   217
3f02bc5a5a03 more accurate treatment of errors;
wenzelm
parents: 71622
diff changeset
   218
(* command-line tool *)
3f02bc5a5a03 more accurate treatment of errors;
wenzelm
parents: 71622
diff changeset
   219
71618
wenzelm
parents: 71613
diff changeset
   220
fun inline_errors exn =
wenzelm
parents: 71613
diff changeset
   221
  Runtime.exn_message_list exn
71622
ab5009192ebb tuned signature -- follow Scala;
wenzelm
parents: 71619
diff changeset
   222
  |> List.app (fn msg => Protocol_Message.marker_text "error_message" (YXML.content_of msg));
71618
wenzelm
parents: 71613
diff changeset
   223
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   224
fun build args_file =
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   225
  let
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   226
    val _ = SHA1.test_samples ();
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   227
    val _ = Options.load_default ();
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   228
    val _ = Isabelle_Process.init_options ();
71876
ad063ac1f617 more robust: explicit check for PIDE session;
wenzelm
parents: 71875
diff changeset
   229
    val args = decode_args false (File.read (Path.explode args_file));
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   230
    val _ =
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   231
      Unsynchronized.setmp Private_Output.protocol_message_fn protocol_message
65934
5f202ba9f590 store errors in build db;
wenzelm
parents: 65532
diff changeset
   232
        build_session args
71618
wenzelm
parents: 71613
diff changeset
   233
      handle exn => (inline_errors exn; Exn.reraise exn);
70907
7e3f25a0cee4 proper protocol_message for bootstrap proofs;
wenzelm
parents: 70712
diff changeset
   234
    val _ = Private_Output.protocol_message_fn := Output.protocol_message_undefined;
62630
bc772694cfbd ML save_state under control of Isabelle/Scala;
wenzelm
parents: 62599
diff changeset
   235
    val _ = Options.reset_default ();
bc772694cfbd ML save_state under control of Isabelle/Scala;
wenzelm
parents: 62599
diff changeset
   236
  in () end;
48418
1a634f9614fb some actual build function on ML side;
wenzelm
parents:
diff changeset
   237
71631
3f02bc5a5a03 more accurate treatment of errors;
wenzelm
parents: 71622
diff changeset
   238
3f02bc5a5a03 more accurate treatment of errors;
wenzelm
parents: 71622
diff changeset
   239
(* PIDE version *)
3f02bc5a5a03 more accurate treatment of errors;
wenzelm
parents: 71622
diff changeset
   240
65307
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   241
val _ =
c1ba192b4f96 more explicit build_session args;
wenzelm
parents: 65306
diff changeset
   242
  Isabelle_Process.protocol_command "build_session"
65313
347ed6219dab more realistic PIDE build session;
wenzelm
parents: 65307
diff changeset
   243
    (fn [args_yxml] =>
70991
f9f7c34b7dd4 more scalable protocol_message: use XML.body directly (Output.output hook is not required);
wenzelm
parents: 70907
diff changeset
   244
        let
71876
ad063ac1f617 more robust: explicit check for PIDE session;
wenzelm
parents: 71875
diff changeset
   245
          val args = decode_args true args_yxml;
71880
0ca353521753 asynchronous build_session: notably for Scala.fulfill protocol commands during run;
wenzelm
parents: 71879
diff changeset
   246
          fun exec e =
0ca353521753 asynchronous build_session: notably for Scala.fulfill protocol commands during run;
wenzelm
parents: 71879
diff changeset
   247
            if can Theory.get_pure () then
0ca353521753 asynchronous build_session: notably for Scala.fulfill protocol commands during run;
wenzelm
parents: 71879
diff changeset
   248
              Isabelle_Thread.fork
71884
2bf0283fc975 proper stack_limit;
wenzelm
parents: 71880
diff changeset
   249
                {name = "build_session", stack_limit = Isabelle_Thread.stack_limit (),
2bf0283fc975 proper stack_limit;
wenzelm
parents: 71880
diff changeset
   250
                  interrupts = false} e
71880
0ca353521753 asynchronous build_session: notably for Scala.fulfill protocol commands during run;
wenzelm
parents: 71879
diff changeset
   251
              |> ignore
0ca353521753 asynchronous build_session: notably for Scala.fulfill protocol commands during run;
wenzelm
parents: 71879
diff changeset
   252
            else e ();
71667
4d2de35214c5 proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
wenzelm
parents: 71656
diff changeset
   253
        in
71880
0ca353521753 asynchronous build_session: notably for Scala.fulfill protocol commands during run;
wenzelm
parents: 71879
diff changeset
   254
          exec (fn () =>
0ca353521753 asynchronous build_session: notably for Scala.fulfill protocol commands during run;
wenzelm
parents: 71879
diff changeset
   255
            (Future.interruptible_task (fn () => (build_session args; (0, []))) () handle exn =>
0ca353521753 asynchronous build_session: notably for Scala.fulfill protocol commands during run;
wenzelm
parents: 71879
diff changeset
   256
              ((1, Runtime.exn_message_list exn) handle _ (*sic!*) => (2, ["CRASHED"])))
71879
fe7ee970c425 clarified build_session protocol;
wenzelm
parents: 71878
diff changeset
   257
          |> let open XML.Encode in pair int (list string) end
71880
0ca353521753 asynchronous build_session: notably for Scala.fulfill protocol commands during run;
wenzelm
parents: 71879
diff changeset
   258
          |> Output.protocol_message Markup.build_session_finished)
71667
4d2de35214c5 proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
wenzelm
parents: 71656
diff changeset
   259
        end
70991
f9f7c34b7dd4 more scalable protocol_message: use XML.body directly (Output.output hook is not required);
wenzelm
parents: 70907
diff changeset
   260
      | _ => raise Match);
59366
e94df7f6b608 clarified build_theories: proper protocol handler;
wenzelm
parents: 59364
diff changeset
   261
48418
1a634f9614fb some actual build function on ML side;
wenzelm
parents:
diff changeset
   262
end;