src/Pure/Tools/build.ML
author wenzelm
Wed May 16 21:36:59 2018 +0200 (14 months ago)
changeset 68198 6710167e17af
parent 68179 da70c9e91135
child 68227 b95a43d8b826
permissions -rw-r--r--
avoid race condition wrt. ISABELLE_TMP, which is removed in Bash.cleanup() before Bash.result(progress_stdout);
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@68088
    97
      else if function = (Markup.functionN, Markup.exportN) andalso
wenzelm@68088
    98
        not (null args) andalso #1 (hd args) = Markup.idN
wenzelm@68088
    99
      then
wenzelm@68148
   100
        let
wenzelm@68198
   101
          val path = Path.expand (Path.explode ("$ISABELLE_EXPORT_TMP/export" ^ serial_string ()));
wenzelm@68148
   102
          val _ = File.open_output (fn out => List.app (File.output out) output) path;
wenzelm@68198
   103
        in inline_message (#2 function) (tl args @ [(Markup.fileN, Path.implode path)]) end
wenzelm@51216
   104
      else
wenzelm@51216
   105
        (case Markup.dest_loading_theory props of
wenzelm@51216
   106
          SOME name => writeln ("\floading_theory = " ^ name)
wenzelm@51661
   107
        | NONE => raise Output.Protocol_Message props)
wenzelm@51661
   108
  | [] => raise Output.Protocol_Message props);
wenzelm@51045
   109
wenzelm@50683
   110
wenzelm@65307
   111
(* build theories *)
wenzelm@50683
   112
wenzelm@67297
   113
fun build_theories symbols bibtex_entries last_timing qualifier master_dir (options, thys) =
wenzelm@59366
   114
  let
wenzelm@68179
   115
    val context =
wenzelm@68179
   116
      {options = options, symbols = symbols, bibtex_entries = bibtex_entries,
wenzelm@68179
   117
        last_timing = last_timing};
wenzelm@59366
   118
    val condition = space_explode "," (Options.string options "condition");
wenzelm@59366
   119
    val conds = filter_out (can getenv_strict) condition;
wenzelm@59366
   120
  in
wenzelm@59366
   121
    if null conds then
wenzelm@63827
   122
      (if Options.bool options "checkpoint" then ML_Heap.share_common_data () else ();
wenzelm@63827
   123
        Options.set_default options;
wenzelm@62714
   124
        Isabelle_Process.init_options ();
wenzelm@64599
   125
        Future.fork I;
wenzelm@68179
   126
        (Thy_Info.use_theories context qualifier master_dir
wenzelm@64308
   127
        |>
wenzelm@64308
   128
          (case Options.string options "profiling" of
wenzelm@64308
   129
            "" => I
wenzelm@64308
   130
          | "time" => profile_time
wenzelm@64308
   131
          | "allocations" => profile_allocations
wenzelm@64308
   132
          | bad => error ("Bad profiling option: " ^ quote bad))
wenzelm@59366
   133
        |> Unsynchronized.setmp print_mode
wenzelm@62714
   134
            (space_explode "," (Options.string options "print_mode") @ print_mode_value ())) thys)
wenzelm@59366
   135
    else
wenzelm@59366
   136
      Output.physical_stderr ("Skipping theories " ^ commas_quote (map #1 thys) ^
wenzelm@59366
   137
        " (undefined " ^ commas conds ^ ")\n")
wenzelm@59366
   138
  end;
wenzelm@59366
   139
wenzelm@65307
   140
wenzelm@65307
   141
(* build session *)
wenzelm@65307
   142
wenzelm@65307
   143
datatype args = Args of
wenzelm@65307
   144
 {symbol_codes: (string * int) list,
wenzelm@65307
   145
  command_timings: Properties.T list,
wenzelm@65307
   146
  do_output: bool,
wenzelm@65307
   147
  verbose: bool,
wenzelm@65307
   148
  browser_info: Path.T,
wenzelm@65307
   149
  document_files: (Path.T * Path.T) list,
wenzelm@65307
   150
  graph_file: Path.T,
wenzelm@65307
   151
  parent_name: string,
wenzelm@65307
   152
  chapter: string,
wenzelm@65307
   153
  name: string,
wenzelm@65307
   154
  master_dir: Path.T,
wenzelm@65431
   155
  theories: (Options.T * (string * Position.T) list) list,
wenzelm@67493
   156
  sessions: (string * Properties.T) list,
wenzelm@67471
   157
  doc_names: string list,
wenzelm@65457
   158
  global_theories: (string * string) list,
wenzelm@66712
   159
  loaded_theories: string list,
wenzelm@67297
   160
  known_theories: (string * string) list,
wenzelm@67297
   161
  bibtex_entries: string list};
wenzelm@65307
   162
wenzelm@65307
   163
fun decode_args yxml =
wenzelm@62630
   164
  let
wenzelm@65307
   165
    open XML.Decode;
wenzelm@65517
   166
    val position = Position.of_properties o properties;
wenzelm@62630
   167
    val (symbol_codes, (command_timings, (do_output, (verbose, (browser_info,
wenzelm@65431
   168
      (document_files, (graph_file, (parent_name, (chapter, (name, (master_dir,
wenzelm@67471
   169
      (theories, (sessions, (doc_names, (global_theories, (loaded_theories,
wenzelm@67471
   170
      (known_theories, bibtex_entries))))))))))))))))) =
wenzelm@65307
   171
      pair (list (pair string int)) (pair (list properties) (pair bool (pair bool (pair string
wenzelm@65307
   172
        (pair (list (pair string string)) (pair string (pair string (pair string (pair string
wenzelm@65431
   173
          (pair string
wenzelm@65517
   174
            (pair (((list (pair Options.decode (list (pair string position))))))
wenzelm@67493
   175
              (pair (list (pair string properties)) (pair (list string)
wenzelm@67493
   176
                (pair (list (pair string string)) (pair (list string)
wenzelm@67493
   177
                  (pair (list (pair string string)) (list string)))))))))))))))))
wenzelm@65307
   178
      (YXML.parse_body yxml);
wenzelm@65307
   179
  in
wenzelm@65307
   180
    Args {symbol_codes = symbol_codes, command_timings = command_timings, do_output = do_output,
wenzelm@65307
   181
      verbose = verbose, browser_info = Path.explode browser_info,
wenzelm@65307
   182
      document_files = map (apply2 Path.explode) document_files,
wenzelm@65307
   183
      graph_file = Path.explode graph_file, parent_name = parent_name, chapter = chapter,
wenzelm@67219
   184
      name = name, master_dir = Path.explode master_dir, theories = theories, sessions = sessions,
wenzelm@67471
   185
      doc_names = doc_names, global_theories = global_theories, loaded_theories = loaded_theories,
wenzelm@67297
   186
      known_theories = known_theories, bibtex_entries = bibtex_entries}
wenzelm@65307
   187
  end;
wenzelm@48418
   188
wenzelm@65307
   189
fun build_session (Args {symbol_codes, command_timings, do_output, verbose, browser_info,
wenzelm@67219
   190
    document_files, graph_file, parent_name, chapter, name, master_dir, theories, sessions,
wenzelm@67471
   191
    doc_names, global_theories, loaded_theories, known_theories, bibtex_entries}) =
wenzelm@65307
   192
  let
wenzelm@62630
   193
    val symbols = HTML.make_symbols symbol_codes;
wenzelm@51941
   194
wenzelm@65441
   195
    val _ =
wenzelm@65478
   196
      Resources.init_session_base
wenzelm@67219
   197
        {sessions = sessions,
wenzelm@67493
   198
         docs = doc_names,
wenzelm@67219
   199
         global_theories = global_theories,
wenzelm@65441
   200
         loaded_theories = loaded_theories,
wenzelm@65441
   201
         known_theories = known_theories};
wenzelm@65431
   202
wenzelm@62630
   203
    val _ =
wenzelm@62630
   204
      Session.init
wenzelm@62630
   205
        symbols
wenzelm@62630
   206
        do_output
wenzelm@62630
   207
        (Options.default_bool "browser_info")
wenzelm@65307
   208
        browser_info
wenzelm@62630
   209
        (Options.default_string "document")
wenzelm@62630
   210
        (Options.default_string "document_output")
wenzelm@62630
   211
        (Present.document_variants (Options.default_string "document_variants"))
wenzelm@65307
   212
        document_files
wenzelm@65307
   213
        graph_file
wenzelm@65307
   214
        parent_name
wenzelm@65307
   215
        (chapter, name)
wenzelm@62630
   216
        verbose;
wenzelm@49911
   217
wenzelm@65058
   218
    val last_timing = get_timings (fold update_timings command_timings empty_timings);
wenzelm@51218
   219
wenzelm@62630
   220
    val res1 =
wenzelm@62630
   221
      theories |>
wenzelm@67297
   222
        (List.app (build_theories symbols bibtex_entries last_timing name master_dir)
wenzelm@62630
   223
          |> session_timing name verbose
wenzelm@62630
   224
          |> Exn.capture);
wenzelm@62630
   225
    val res2 = Exn.capture Session.finish ();
wenzelm@65431
   226
wenzelm@65478
   227
    val _ = Resources.finish_session_base ();
wenzelm@62630
   228
    val _ = Par_Exn.release_all [res1, res2];
wenzelm@65307
   229
  in () end;
wenzelm@48673
   230
wenzelm@65307
   231
(*command-line tool*)
wenzelm@65307
   232
fun build args_file =
wenzelm@65307
   233
  let
wenzelm@65307
   234
    val _ = SHA1.test_samples ();
wenzelm@65307
   235
    val _ = Options.load_default ();
wenzelm@65307
   236
    val _ = Isabelle_Process.init_options ();
wenzelm@65936
   237
    val args = decode_args (File.read (Path.explode args_file));
wenzelm@66048
   238
    fun error_message msg = writeln ("\ferror_message = " ^ encode_lines (YXML.content_of msg));
wenzelm@65307
   239
    val _ =
wenzelm@65307
   240
      Unsynchronized.setmp Private_Output.protocol_message_fn protocol_message
wenzelm@65934
   241
        build_session args
wenzelm@65948
   242
      handle exn => (List.app error_message (Runtime.exn_message_list exn); Exn.reraise exn);
wenzelm@62630
   243
    val _ = Options.reset_default ();
wenzelm@62630
   244
  in () end;
wenzelm@48418
   245
wenzelm@65307
   246
(*PIDE version*)
wenzelm@65307
   247
val _ =
wenzelm@65307
   248
  Isabelle_Process.protocol_command "build_session"
wenzelm@65313
   249
    (fn [args_yxml] =>
wenzelm@65307
   250
      let
wenzelm@65313
   251
        val args = decode_args args_yxml;
wenzelm@65307
   252
        val result = (build_session args; "") handle exn =>
wenzelm@65307
   253
          (Runtime.exn_message exn handle _ (*sic!*) =>
wenzelm@65307
   254
            "Exception raised, but failed to print details!");
wenzelm@65313
   255
    in Output.protocol_message Markup.build_session_finished [result] end | _ => raise Match);
wenzelm@59366
   256
wenzelm@48418
   257
end;