src/Pure/Tools/build.ML
author wenzelm
Fri Apr 07 19:35:39 2017 +0200 (2017-04-07 ago)
changeset 65431 4a3e6cda3b94
parent 65318 342efc382558
child 65441 9425e4d8bdb6
permissions -rw-r--r--
provide session base for "isabelle build" and "isabelle console" ML process;
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@51216
    95
      else
wenzelm@51216
    96
        (case Markup.dest_loading_theory props of
wenzelm@51216
    97
          SOME name => writeln ("\floading_theory = " ^ name)
wenzelm@51661
    98
        | NONE => raise Output.Protocol_Message props)
wenzelm@51661
    99
  | [] => raise Output.Protocol_Message props);
wenzelm@51045
   100
wenzelm@50683
   101
wenzelm@65307
   102
(* build theories *)
wenzelm@50683
   103
wenzelm@61381
   104
fun build_theories symbols last_timing master_dir (options, thys) =
wenzelm@59366
   105
  let
wenzelm@59366
   106
    val condition = space_explode "," (Options.string options "condition");
wenzelm@59366
   107
    val conds = filter_out (can getenv_strict) condition;
wenzelm@59366
   108
  in
wenzelm@59366
   109
    if null conds then
wenzelm@63827
   110
      (if Options.bool options "checkpoint" then ML_Heap.share_common_data () else ();
wenzelm@63827
   111
        Options.set_default options;
wenzelm@62714
   112
        Isabelle_Process.init_options ();
wenzelm@64599
   113
        Future.fork I;
wenzelm@59366
   114
        (Thy_Info.use_theories {
wenzelm@59366
   115
          document = Present.document_enabled (Options.string options "document"),
wenzelm@61381
   116
          symbols = symbols,
wenzelm@59366
   117
          last_timing = last_timing,
wenzelm@59366
   118
          master_dir = master_dir}
wenzelm@64308
   119
        |>
wenzelm@64308
   120
          (case Options.string options "profiling" of
wenzelm@64308
   121
            "" => I
wenzelm@64308
   122
          | "time" => profile_time
wenzelm@64308
   123
          | "allocations" => profile_allocations
wenzelm@64308
   124
          | bad => error ("Bad profiling option: " ^ quote bad))
wenzelm@59366
   125
        |> Unsynchronized.setmp print_mode
wenzelm@62714
   126
            (space_explode "," (Options.string options "print_mode") @ print_mode_value ())) thys)
wenzelm@59366
   127
    else
wenzelm@59366
   128
      Output.physical_stderr ("Skipping theories " ^ commas_quote (map #1 thys) ^
wenzelm@59366
   129
        " (undefined " ^ commas conds ^ ")\n")
wenzelm@59366
   130
  end;
wenzelm@59366
   131
wenzelm@65307
   132
wenzelm@65307
   133
(* build session *)
wenzelm@65307
   134
wenzelm@65307
   135
datatype args = Args of
wenzelm@65307
   136
 {symbol_codes: (string * int) list,
wenzelm@65307
   137
  command_timings: Properties.T list,
wenzelm@65307
   138
  do_output: bool,
wenzelm@65307
   139
  verbose: bool,
wenzelm@65307
   140
  browser_info: Path.T,
wenzelm@65307
   141
  document_files: (Path.T * Path.T) list,
wenzelm@65307
   142
  graph_file: Path.T,
wenzelm@65307
   143
  parent_name: string,
wenzelm@65307
   144
  chapter: string,
wenzelm@65307
   145
  name: string,
wenzelm@65307
   146
  master_dir: Path.T,
wenzelm@65431
   147
  theories: (Options.T * (string * Position.T) list) list,
wenzelm@65431
   148
  known_theories: (string * string) list};
wenzelm@65307
   149
wenzelm@65307
   150
fun decode_args yxml =
wenzelm@62630
   151
  let
wenzelm@65307
   152
    open XML.Decode;
wenzelm@62630
   153
    val (symbol_codes, (command_timings, (do_output, (verbose, (browser_info,
wenzelm@65431
   154
      (document_files, (graph_file, (parent_name, (chapter, (name, (master_dir,
wenzelm@65431
   155
      (theories, known_theories)))))))))))) =
wenzelm@65307
   156
      pair (list (pair string int)) (pair (list properties) (pair bool (pair bool (pair string
wenzelm@65307
   157
        (pair (list (pair string string)) (pair string (pair string (pair string (pair string
wenzelm@65431
   158
          (pair string
wenzelm@65431
   159
            (pair (((list (pair Options.decode (list (string #> rpair Position.none))))))
wenzelm@65431
   160
              (list (pair string string)))))))))))))
wenzelm@65307
   161
      (YXML.parse_body yxml);
wenzelm@65307
   162
  in
wenzelm@65307
   163
    Args {symbol_codes = symbol_codes, command_timings = command_timings, do_output = do_output,
wenzelm@65307
   164
      verbose = verbose, browser_info = Path.explode browser_info,
wenzelm@65307
   165
      document_files = map (apply2 Path.explode) document_files,
wenzelm@65307
   166
      graph_file = Path.explode graph_file, parent_name = parent_name, chapter = chapter,
wenzelm@65431
   167
      name = name, master_dir = Path.explode master_dir, theories = theories,
wenzelm@65431
   168
      known_theories = known_theories}
wenzelm@65307
   169
  end;
wenzelm@48418
   170
wenzelm@65307
   171
fun build_session (Args {symbol_codes, command_timings, do_output, verbose, browser_info,
wenzelm@65431
   172
    document_files, graph_file, parent_name, chapter, name, master_dir, theories, known_theories}) =
wenzelm@65307
   173
  let
wenzelm@62630
   174
    val symbols = HTML.make_symbols symbol_codes;
wenzelm@51941
   175
wenzelm@65431
   176
    val _ = Resources.set_session_base {known_theories = known_theories};
wenzelm@65431
   177
wenzelm@62630
   178
    val _ =
wenzelm@62630
   179
      Session.init
wenzelm@62630
   180
        symbols
wenzelm@62630
   181
        do_output
wenzelm@62630
   182
        (Options.default_bool "browser_info")
wenzelm@65307
   183
        browser_info
wenzelm@62630
   184
        (Options.default_string "document")
wenzelm@62630
   185
        (Options.default_string "document_output")
wenzelm@62630
   186
        (Present.document_variants (Options.default_string "document_variants"))
wenzelm@65307
   187
        document_files
wenzelm@65307
   188
        graph_file
wenzelm@65307
   189
        parent_name
wenzelm@65307
   190
        (chapter, name)
wenzelm@62630
   191
        verbose;
wenzelm@49911
   192
wenzelm@65058
   193
    val last_timing = get_timings (fold update_timings command_timings empty_timings);
wenzelm@51218
   194
wenzelm@62630
   195
    val res1 =
wenzelm@62630
   196
      theories |>
wenzelm@65307
   197
        (List.app (build_theories symbols last_timing master_dir)
wenzelm@62630
   198
          |> session_timing name verbose
wenzelm@62630
   199
          |> Exn.capture);
wenzelm@62630
   200
    val res2 = Exn.capture Session.finish ();
wenzelm@65431
   201
wenzelm@65431
   202
    val _ = Resources.reset_session_base ();
wenzelm@62630
   203
    val _ = Par_Exn.release_all [res1, res2];
wenzelm@65307
   204
  in () end;
wenzelm@48673
   205
wenzelm@65307
   206
(*command-line tool*)
wenzelm@65307
   207
fun build args_file =
wenzelm@65307
   208
  let
wenzelm@65307
   209
    val _ = SHA1.test_samples ();
wenzelm@65307
   210
    val _ = Options.load_default ();
wenzelm@65307
   211
    val _ = Isabelle_Process.init_options ();
wenzelm@65307
   212
    val args = decode_args (File.read (Path.explode args_file));
wenzelm@65307
   213
    val _ =
wenzelm@65307
   214
      Unsynchronized.setmp Private_Output.protocol_message_fn protocol_message
wenzelm@65307
   215
        build_session args;
wenzelm@62630
   216
    val _ = Options.reset_default ();
wenzelm@62630
   217
  in () end;
wenzelm@48418
   218
wenzelm@65307
   219
(*PIDE version*)
wenzelm@65307
   220
val _ =
wenzelm@65307
   221
  Isabelle_Process.protocol_command "build_session"
wenzelm@65313
   222
    (fn [args_yxml] =>
wenzelm@65307
   223
      let
wenzelm@65313
   224
        val args = decode_args args_yxml;
wenzelm@65307
   225
        val result = (build_session args; "") handle exn =>
wenzelm@65307
   226
          (Runtime.exn_message exn handle _ (*sic!*) =>
wenzelm@65307
   227
            "Exception raised, but failed to print details!");
wenzelm@65313
   228
    in Output.protocol_message Markup.build_session_finished [result] end | _ => raise Match);
wenzelm@59366
   229
wenzelm@48418
   230
end;