src/Pure/Tools/build.ML
author wenzelm
Sat Dec 16 21:53:07 2017 +0100 (24 months ago ago)
changeset 67219 81e9804b2014
parent 66873 9953ae603a23
child 67297 86a099f896fc
permissions -rw-r--r--
added document antiquotation @{session name};
renamed protocol function "Prover.session_base" to "Prover.init_session_base" according to the ML/Scala operation;
wenzelm@50686
     1
(*  Title:      Pure/Tools/build.ML
wenzelm@48418
     2
    Author:     Makarius
wenzelm@48418
     3
wenzelm@48418
     4
Build Isabelle sessions.
wenzelm@48418
     5
*)
wenzelm@48418
     6
wenzelm@48418
     7
signature BUILD =
wenzelm@48418
     8
sig
wenzelm@48731
     9
  val build: string -> unit
wenzelm@48418
    10
end;
wenzelm@48418
    11
wenzelm@48418
    12
structure Build: BUILD =
wenzelm@48418
    13
struct
wenzelm@48418
    14
wenzelm@51662
    15
(* command timings *)
wenzelm@51662
    16
wenzelm@56615
    17
type timings = ((string * Time.time) Inttab.table) Symtab.table;  (*file -> offset -> name, time*)
wenzelm@51662
    18
wenzelm@51662
    19
val empty_timings: timings = Symtab.empty;
wenzelm@51662
    20
wenzelm@51662
    21
fun update_timings props =
wenzelm@51662
    22
  (case Markup.parse_command_timing_properties props of
wenzelm@51662
    23
    SOME ({file, offset, name}, time) =>
wenzelm@51666
    24
      Symtab.map_default (file, Inttab.empty)
wenzelm@62826
    25
        (Inttab.map_default (offset, (name, time)) (fn (_, t) => (name, t + time)))
wenzelm@51662
    26
  | NONE => I);
wenzelm@51662
    27
wenzelm@51662
    28
fun approximative_id name pos =
wenzelm@51662
    29
  (case (Position.file_of pos, Position.offset_of pos) of
wenzelm@51662
    30
    (SOME file, SOME offset) =>
wenzelm@51662
    31
      if name = "" then NONE else SOME {file = file, offset = offset, name = name}
wenzelm@51662
    32
  | _ => NONE);
wenzelm@51662
    33
wenzelm@65058
    34
fun get_timings timings tr =
wenzelm@51662
    35
  (case approximative_id (Toplevel.name_of tr) (Toplevel.pos_of tr) of
wenzelm@51662
    36
    SOME {file, offset, name} =>
wenzelm@51662
    37
      (case Symtab.lookup timings file of
wenzelm@51662
    38
        SOME offsets =>
wenzelm@51662
    39
          (case Inttab.lookup offsets offset of
wenzelm@51662
    40
            SOME (name', time) => if name = name' then SOME time else NONE
wenzelm@51662
    41
          | NONE => NONE)
wenzelm@51662
    42
      | NONE => NONE)
wenzelm@65058
    43
  | NONE => NONE)
wenzelm@65058
    44
  |> the_default Time.zeroTime;
wenzelm@51662
    45
wenzelm@51662
    46
wenzelm@52052
    47
(* session timing *)
wenzelm@52052
    48
wenzelm@52052
    49
fun session_timing name verbose f x =
wenzelm@52052
    50
  let
wenzelm@52052
    51
    val start = Timing.start ();
wenzelm@52052
    52
    val y = f x;
wenzelm@52052
    53
    val timing = Timing.result start;
wenzelm@52052
    54
wenzelm@62925
    55
    val threads = string_of_int (Multithreading.max_threads ());
wenzelm@52052
    56
    val factor = Time.toReal (#cpu timing) / Time.toReal (#elapsed timing)
wenzelm@52052
    57
      |> Real.fmt (StringCvt.FIX (SOME 2));
wenzelm@52052
    58
wenzelm@52052
    59
    val timing_props =
wenzelm@52052
    60
      [("threads", threads)] @ Markup.timing_properties timing @ [("factor", factor)];
wenzelm@52052
    61
    val _ = writeln ("\fTiming = " ^ YXML.string_of_body (XML.Encode.properties timing_props));
wenzelm@52052
    62
    val _ =
wenzelm@52052
    63
      if verbose then
wenzelm@52052
    64
        Output.physical_stderr ("Timing " ^ name ^ " (" ^
wenzelm@52052
    65
          threads ^ " threads, " ^ Timing.message timing ^ ", factor " ^ factor ^ ")\n")
wenzelm@52052
    66
      else ();
wenzelm@52052
    67
  in y end;
wenzelm@52052
    68
wenzelm@52052
    69
wenzelm@50683
    70
(* protocol messages *)
wenzelm@50683
    71
wenzelm@51662
    72
fun inline_message a args =
wenzelm@51662
    73
  writeln ("\f" ^ a ^ " = " ^ YXML.string_of_body (XML.Encode.properties args));
wenzelm@50683
    74
wenzelm@50683
    75
fun protocol_message props output =
wenzelm@51216
    76
  (case props of
wenzelm@51216
    77
    function :: args =>
wenzelm@51662
    78
      if function = Markup.ML_statistics orelse function = Markup.task_statistics then
wenzelm@51662
    79
        inline_message (#2 function) args
wenzelm@51662
    80
      else if function = Markup.command_timing then
wenzelm@51662
    81
        let
wenzelm@51662
    82
          val name = the_default "" (Properties.get args Markup.nameN);
wenzelm@51662
    83
          val pos = Position.of_properties args;
wenzelm@51662
    84
          val {elapsed, ...} = Markup.parse_timing_properties args;
wenzelm@62793
    85
          val is_significant =
wenzelm@62793
    86
            Timing.is_relevant_time elapsed andalso
wenzelm@62826
    87
            elapsed >= Options.default_seconds "command_timing_threshold";
wenzelm@51662
    88
        in
wenzelm@62793
    89
          if is_significant then
wenzelm@59149
    90
            (case approximative_id name pos of
wenzelm@59149
    91
              SOME id => inline_message (#2 function) (Markup.command_timing_properties id elapsed)
wenzelm@59149
    92
            | NONE => ())
wenzelm@59149
    93
          else ()
wenzelm@51662
    94
        end
wenzelm@66873
    95
      else if function = Markup.theory_timing then
wenzelm@66873
    96
        inline_message (#2 function) args
wenzelm@51216
    97
      else
wenzelm@51216
    98
        (case Markup.dest_loading_theory props of
wenzelm@51216
    99
          SOME name => writeln ("\floading_theory = " ^ name)
wenzelm@51661
   100
        | NONE => raise Output.Protocol_Message props)
wenzelm@51661
   101
  | [] => raise Output.Protocol_Message props);
wenzelm@51045
   102
wenzelm@50683
   103
wenzelm@65307
   104
(* build theories *)
wenzelm@50683
   105
wenzelm@65445
   106
fun build_theories symbols last_timing qualifier master_dir (options, thys) =
wenzelm@59366
   107
  let
wenzelm@59366
   108
    val condition = space_explode "," (Options.string options "condition");
wenzelm@59366
   109
    val conds = filter_out (can getenv_strict) condition;
wenzelm@59366
   110
  in
wenzelm@59366
   111
    if null conds then
wenzelm@63827
   112
      (if Options.bool options "checkpoint" then ML_Heap.share_common_data () else ();
wenzelm@63827
   113
        Options.set_default options;
wenzelm@62714
   114
        Isabelle_Process.init_options ();
wenzelm@64599
   115
        Future.fork I;
wenzelm@59366
   116
        (Thy_Info.use_theories {
wenzelm@59366
   117
          document = Present.document_enabled (Options.string options "document"),
wenzelm@61381
   118
          symbols = symbols,
wenzelm@59366
   119
          last_timing = last_timing,
wenzelm@65445
   120
          qualifier = qualifier,
wenzelm@59366
   121
          master_dir = master_dir}
wenzelm@64308
   122
        |>
wenzelm@64308
   123
          (case Options.string options "profiling" of
wenzelm@64308
   124
            "" => I
wenzelm@64308
   125
          | "time" => profile_time
wenzelm@64308
   126
          | "allocations" => profile_allocations
wenzelm@64308
   127
          | bad => error ("Bad profiling option: " ^ quote bad))
wenzelm@59366
   128
        |> Unsynchronized.setmp print_mode
wenzelm@62714
   129
            (space_explode "," (Options.string options "print_mode") @ print_mode_value ())) thys)
wenzelm@59366
   130
    else
wenzelm@59366
   131
      Output.physical_stderr ("Skipping theories " ^ commas_quote (map #1 thys) ^
wenzelm@59366
   132
        " (undefined " ^ commas conds ^ ")\n")
wenzelm@59366
   133
  end;
wenzelm@59366
   134
wenzelm@65307
   135
wenzelm@65307
   136
(* build session *)
wenzelm@65307
   137
wenzelm@65307
   138
datatype args = Args of
wenzelm@65307
   139
 {symbol_codes: (string * int) list,
wenzelm@65307
   140
  command_timings: Properties.T list,
wenzelm@65307
   141
  do_output: bool,
wenzelm@65307
   142
  verbose: bool,
wenzelm@65307
   143
  browser_info: Path.T,
wenzelm@65307
   144
  document_files: (Path.T * Path.T) list,
wenzelm@65307
   145
  graph_file: Path.T,
wenzelm@65307
   146
  parent_name: string,
wenzelm@65307
   147
  chapter: string,
wenzelm@65307
   148
  name: string,
wenzelm@65307
   149
  master_dir: Path.T,
wenzelm@65431
   150
  theories: (Options.T * (string * Position.T) list) list,
wenzelm@67219
   151
  sessions: string list,
wenzelm@65457
   152
  global_theories: (string * string) list,
wenzelm@66712
   153
  loaded_theories: string list,
wenzelm@65431
   154
  known_theories: (string * string) list};
wenzelm@65307
   155
wenzelm@65307
   156
fun decode_args yxml =
wenzelm@62630
   157
  let
wenzelm@65307
   158
    open XML.Decode;
wenzelm@65517
   159
    val position = Position.of_properties o properties;
wenzelm@62630
   160
    val (symbol_codes, (command_timings, (do_output, (verbose, (browser_info,
wenzelm@65431
   161
      (document_files, (graph_file, (parent_name, (chapter, (name, (master_dir,
wenzelm@67219
   162
      (theories, (sessions, (global_theories, (loaded_theories, known_theories))))))))))))))) =
wenzelm@65307
   163
      pair (list (pair string int)) (pair (list properties) (pair bool (pair bool (pair string
wenzelm@65307
   164
        (pair (list (pair string string)) (pair string (pair string (pair string (pair string
wenzelm@65431
   165
          (pair string
wenzelm@65517
   166
            (pair (((list (pair Options.decode (list (pair string position))))))
wenzelm@67219
   167
              (pair (list string) (pair (list (pair string string))
wenzelm@67219
   168
                (pair (list string) (list (pair string string))))))))))))))))
wenzelm@65307
   169
      (YXML.parse_body yxml);
wenzelm@65307
   170
  in
wenzelm@65307
   171
    Args {symbol_codes = symbol_codes, command_timings = command_timings, do_output = do_output,
wenzelm@65307
   172
      verbose = verbose, browser_info = Path.explode browser_info,
wenzelm@65307
   173
      document_files = map (apply2 Path.explode) document_files,
wenzelm@65307
   174
      graph_file = Path.explode graph_file, parent_name = parent_name, chapter = chapter,
wenzelm@67219
   175
      name = name, master_dir = Path.explode master_dir, theories = theories, sessions = sessions,
wenzelm@65441
   176
      global_theories = global_theories, loaded_theories = loaded_theories,
wenzelm@65431
   177
      known_theories = known_theories}
wenzelm@65307
   178
  end;
wenzelm@48418
   179
wenzelm@65307
   180
fun build_session (Args {symbol_codes, command_timings, do_output, verbose, browser_info,
wenzelm@67219
   181
    document_files, graph_file, parent_name, chapter, name, master_dir, theories, sessions,
wenzelm@65441
   182
    global_theories, loaded_theories, known_theories}) =
wenzelm@65307
   183
  let
wenzelm@62630
   184
    val symbols = HTML.make_symbols symbol_codes;
wenzelm@51941
   185
wenzelm@65441
   186
    val _ =
wenzelm@65478
   187
      Resources.init_session_base
wenzelm@67219
   188
        {sessions = sessions,
wenzelm@67219
   189
         global_theories = global_theories,
wenzelm@65441
   190
         loaded_theories = loaded_theories,
wenzelm@65441
   191
         known_theories = known_theories};
wenzelm@65431
   192
wenzelm@62630
   193
    val _ =
wenzelm@62630
   194
      Session.init
wenzelm@62630
   195
        symbols
wenzelm@62630
   196
        do_output
wenzelm@62630
   197
        (Options.default_bool "browser_info")
wenzelm@65307
   198
        browser_info
wenzelm@62630
   199
        (Options.default_string "document")
wenzelm@62630
   200
        (Options.default_string "document_output")
wenzelm@62630
   201
        (Present.document_variants (Options.default_string "document_variants"))
wenzelm@65307
   202
        document_files
wenzelm@65307
   203
        graph_file
wenzelm@65307
   204
        parent_name
wenzelm@65307
   205
        (chapter, name)
wenzelm@62630
   206
        verbose;
wenzelm@49911
   207
wenzelm@65058
   208
    val last_timing = get_timings (fold update_timings command_timings empty_timings);
wenzelm@51218
   209
wenzelm@62630
   210
    val res1 =
wenzelm@62630
   211
      theories |>
wenzelm@65445
   212
        (List.app (build_theories symbols last_timing name master_dir)
wenzelm@62630
   213
          |> session_timing name verbose
wenzelm@62630
   214
          |> Exn.capture);
wenzelm@62630
   215
    val res2 = Exn.capture Session.finish ();
wenzelm@65431
   216
wenzelm@65478
   217
    val _ = Resources.finish_session_base ();
wenzelm@62630
   218
    val _ = Par_Exn.release_all [res1, res2];
wenzelm@65307
   219
  in () end;
wenzelm@48673
   220
wenzelm@65307
   221
(*command-line tool*)
wenzelm@65307
   222
fun build args_file =
wenzelm@65307
   223
  let
wenzelm@65307
   224
    val _ = SHA1.test_samples ();
wenzelm@65307
   225
    val _ = Options.load_default ();
wenzelm@65307
   226
    val _ = Isabelle_Process.init_options ();
wenzelm@65936
   227
    val args = decode_args (File.read (Path.explode args_file));
wenzelm@66048
   228
    fun error_message msg = writeln ("\ferror_message = " ^ encode_lines (YXML.content_of msg));
wenzelm@65307
   229
    val _ =
wenzelm@65307
   230
      Unsynchronized.setmp Private_Output.protocol_message_fn protocol_message
wenzelm@65934
   231
        build_session args
wenzelm@65948
   232
      handle exn => (List.app error_message (Runtime.exn_message_list exn); Exn.reraise exn);
wenzelm@62630
   233
    val _ = Options.reset_default ();
wenzelm@62630
   234
  in () end;
wenzelm@48418
   235
wenzelm@65307
   236
(*PIDE version*)
wenzelm@65307
   237
val _ =
wenzelm@65307
   238
  Isabelle_Process.protocol_command "build_session"
wenzelm@65313
   239
    (fn [args_yxml] =>
wenzelm@65307
   240
      let
wenzelm@65313
   241
        val args = decode_args args_yxml;
wenzelm@65307
   242
        val result = (build_session args; "") handle exn =>
wenzelm@65307
   243
          (Runtime.exn_message exn handle _ (*sic!*) =>
wenzelm@65307
   244
            "Exception raised, but failed to print details!");
wenzelm@65313
   245
    in Output.protocol_message Markup.build_session_finished [result] end | _ => raise Match);
wenzelm@59366
   246
wenzelm@48418
   247
end;