src/Pure/Tools/build.ML
author wenzelm
Fri May 17 18:19:42 2013 +0200 (2013-05-17 ago)
changeset 52052 892061142ba6
parent 52041 80e001b85332
child 52059 2f970c7f722b
permissions -rw-r--r--
discontinued obsolete isabelle usedir, mkdir, make;
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@51662
    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@51666
    25
        (Inttab.map_default (offset, (name, time)) (fn (_, t) => (name, Time.+ (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@51662
    34
fun lookup_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@51662
    43
  | NONE => NONE);
wenzelm@51662
    44
wenzelm@51662
    45
wenzelm@52052
    46
(* session timing *)
wenzelm@52052
    47
wenzelm@52052
    48
fun session_timing name verbose f x =
wenzelm@52052
    49
  let
wenzelm@52052
    50
    val start = Timing.start ();
wenzelm@52052
    51
    val y = f x;
wenzelm@52052
    52
    val timing = Timing.result start;
wenzelm@52052
    53
wenzelm@52052
    54
    val threads = string_of_int (Multithreading.max_threads_value ());
wenzelm@52052
    55
    val factor = Time.toReal (#cpu timing) / Time.toReal (#elapsed timing)
wenzelm@52052
    56
      |> Real.fmt (StringCvt.FIX (SOME 2));
wenzelm@52052
    57
wenzelm@52052
    58
    val timing_props =
wenzelm@52052
    59
      [("threads", threads)] @ Markup.timing_properties timing @ [("factor", factor)];
wenzelm@52052
    60
    val _ = writeln ("\fTiming = " ^ YXML.string_of_body (XML.Encode.properties timing_props));
wenzelm@52052
    61
    val _ =
wenzelm@52052
    62
      if verbose then
wenzelm@52052
    63
        Output.physical_stderr ("Timing " ^ name ^ " (" ^
wenzelm@52052
    64
          threads ^ " threads, " ^ Timing.message timing ^ ", factor " ^ factor ^ ")\n")
wenzelm@52052
    65
      else ();
wenzelm@52052
    66
  in y end;
wenzelm@52052
    67
wenzelm@52052
    68
wenzelm@50683
    69
(* protocol messages *)
wenzelm@50683
    70
wenzelm@51662
    71
fun inline_message a args =
wenzelm@51662
    72
  writeln ("\f" ^ a ^ " = " ^ YXML.string_of_body (XML.Encode.properties args));
wenzelm@50683
    73
wenzelm@50683
    74
fun protocol_message props output =
wenzelm@51216
    75
  (case props of
wenzelm@51216
    76
    function :: args =>
wenzelm@51662
    77
      if function = Markup.ML_statistics orelse function = Markup.task_statistics then
wenzelm@51662
    78
        inline_message (#2 function) args
wenzelm@51662
    79
      else if function = Markup.command_timing then
wenzelm@51662
    80
        let
wenzelm@51662
    81
          val name = the_default "" (Properties.get args Markup.nameN);
wenzelm@51662
    82
          val pos = Position.of_properties args;
wenzelm@51662
    83
          val {elapsed, ...} = Markup.parse_timing_properties args;
wenzelm@51662
    84
        in
wenzelm@51662
    85
          (case approximative_id name pos of
wenzelm@51662
    86
            SOME id => inline_message (#2 function) (Markup.command_timing_properties id elapsed)
wenzelm@51662
    87
          | NONE => ())
wenzelm@51662
    88
        end
wenzelm@51216
    89
      else
wenzelm@51216
    90
        (case Markup.dest_loading_theory props of
wenzelm@51216
    91
          SOME name => writeln ("\floading_theory = " ^ name)
wenzelm@51661
    92
        | NONE => raise Output.Protocol_Message props)
wenzelm@51661
    93
  | [] => raise Output.Protocol_Message props);
wenzelm@51045
    94
wenzelm@50683
    95
wenzelm@50683
    96
(* build *)
wenzelm@50683
    97
wenzelm@48465
    98
local
wenzelm@48465
    99
wenzelm@48500
   100
fun no_document options =
wenzelm@50121
   101
  (case Options.string options "document" of "" => true | "false" => true | _ => false);
wenzelm@48500
   102
wenzelm@51217
   103
fun use_theories last_timing options =
wenzelm@51217
   104
  Thy_Info.use_theories {last_timing = last_timing, master_dir = Path.current}
wenzelm@48457
   105
    |> Unsynchronized.setmp Proofterm.proofs (Options.int options "proofs")
wenzelm@48457
   106
    |> Unsynchronized.setmp print_mode
wenzelm@48457
   107
        (space_explode "," (Options.string options "print_mode") @ print_mode_value ())
wenzelm@48457
   108
    |> Unsynchronized.setmp Goal.parallel_proofs (Options.int options "parallel_proofs")
wenzelm@51423
   109
    |> Unsynchronized.setmp Goal.parallel_subproofs_saturation
wenzelm@51423
   110
        (Options.int options "parallel_subproofs_saturation")
wenzelm@51423
   111
    |> Unsynchronized.setmp Goal.parallel_subproofs_threshold
wenzelm@51423
   112
        (Options.real options "parallel_subproofs_threshold")
wenzelm@48457
   113
    |> Unsynchronized.setmp Multithreading.trace (Options.int options "threads_trace")
wenzelm@48460
   114
    |> Unsynchronized.setmp Multithreading.max_threads (Options.int options "threads")
wenzelm@50698
   115
    |> Unsynchronized.setmp Future.ML_statistics true
wenzelm@48500
   116
    |> no_document options ? Present.no_document
wenzelm@48486
   117
    |> Unsynchronized.setmp quick_and_dirty (Options.bool options "quick_and_dirty")
wenzelm@51553
   118
    |> Unsynchronized.setmp Goal.skip_proofs (Options.bool options "skip_proofs")
wenzelm@48527
   119
    |> Unsynchronized.setmp Pretty.margin_default (Options.int options "pretty_margin")
wenzelm@48492
   120
    |> Unsynchronized.setmp Toplevel.timing (Options.bool options "timing");
wenzelm@48457
   121
wenzelm@51217
   122
fun use_theories_condition last_timing (options, thys) =
wenzelm@48465
   123
  let val condition = space_explode "," (Options.string options "condition") in
wenzelm@48465
   124
    (case filter_out (can getenv_strict) condition of
wenzelm@51941
   125
      [] =>
wenzelm@51941
   126
        (Options.set_default options;
wenzelm@51941
   127
         use_theories last_timing options (map (rpair Position.none) thys))
wenzelm@48465
   128
    | conds =>
wenzelm@48512
   129
        Output.physical_stderr ("Skipping theories " ^ commas_quote thys ^
wenzelm@48477
   130
          " (undefined " ^ commas conds ^ ")\n"))
wenzelm@48465
   131
  end;
wenzelm@48465
   132
wenzelm@48465
   133
in
wenzelm@48465
   134
wenzelm@48681
   135
fun build args_file = Command_Line.tool (fn () =>
wenzelm@48672
   136
    let
wenzelm@51220
   137
      val (command_timings, (do_output, (options, (verbose, (browser_info,
wenzelm@51399
   138
          (parent_name, (chapter, (name, theories)))))))) =
wenzelm@48672
   139
        File.read (Path.explode args_file) |> YXML.parse_body |>
wenzelm@48672
   140
          let open XML.Decode in
wenzelm@51218
   141
            pair (list properties) (pair bool (pair Options.decode (pair bool (pair string
wenzelm@51399
   142
              (pair string (pair string (pair string
wenzelm@51399
   143
                ((list (pair Options.decode (list string)))))))))))
wenzelm@48672
   144
          end;
wenzelm@48418
   145
wenzelm@51941
   146
      val _ = Options.set_default options;
wenzelm@51941
   147
wenzelm@48804
   148
      val document_variants =
wenzelm@48804
   149
        map Present.read_variant (space_explode ":" (Options.string options "document_variants"));
wenzelm@48672
   150
      val _ =
wenzelm@48805
   151
        (case duplicates (op =) (map fst document_variants) of
wenzelm@48805
   152
          [] => ()
wenzelm@48805
   153
        | dups => error ("Duplicate document variants: " ^ commas_quote dups));
wenzelm@49242
   154
wenzelm@50982
   155
      val _ = writeln ("\fSession.name = " ^ name);
wenzelm@49242
   156
      val _ =
wenzelm@51399
   157
        Session.init do_output
wenzelm@51399
   158
          (Options.bool options "browser_info")
wenzelm@51399
   159
          (Path.explode browser_info)
wenzelm@48672
   160
          (Options.string options "document")
wenzelm@48672
   161
          (Options.bool options "document_graph")
wenzelm@48805
   162
          (Options.string options "document_output")
wenzelm@48804
   163
          document_variants
wenzelm@51399
   164
          parent_name (chapter, name)
wenzelm@51398
   165
          (false, "")
wenzelm@50121
   166
          verbose;
wenzelm@49911
   167
wenzelm@51228
   168
      val last_timing = lookup_timings (fold update_timings command_timings empty_timings);
wenzelm@51218
   169
wenzelm@49911
   170
      val res1 =
wenzelm@48673
   171
        theories |>
wenzelm@51218
   172
          (List.app (use_theories_condition last_timing)
wenzelm@52052
   173
            |> session_timing name verbose
wenzelm@50683
   174
            |> Unsynchronized.setmp Output.Private_Hooks.protocol_message_fn protocol_message
wenzelm@49911
   175
            |> Unsynchronized.setmp Multithreading.max_threads (Options.int options "threads")
wenzelm@49911
   176
            |> Exn.capture);
wenzelm@49911
   177
      val res2 = Exn.capture Session.finish ();
wenzelm@49911
   178
      val _ = Par_Exn.release_all [res1, res2];
wenzelm@48673
   179
wenzelm@51941
   180
      val _ = Options.reset_default ();
wenzelm@48734
   181
      val _ = if do_output then () else exit 0;
wenzelm@48681
   182
    in 0 end);
wenzelm@48418
   183
wenzelm@48418
   184
end;
wenzelm@48465
   185
wenzelm@48465
   186
end;