src/Pure/Tools/build.ML
author wenzelm
Tue Feb 19 13:57:13 2013 +0100 (2013-02-19 ago)
changeset 51218 6425a0d3b7ac
parent 51217 65ab2c4f4c32
child 51220 e140c8fa485a
permissions -rw-r--r--
support for build passing timings from Scala to ML;
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@50683
    15
(* protocol messages *)
wenzelm@50683
    16
wenzelm@51045
    17
local
wenzelm@51045
    18
wenzelm@51216
    19
(* FIXME avoid hardwired stuff!? *)
wenzelm@51216
    20
val protocol_echo = [Markup.ML_statistics, Markup.task_statistics, Markup.command_timing];
wenzelm@50683
    21
wenzelm@51216
    22
fun protocol_undef () = raise Fail "Undefined Output.protocol_message";
wenzelm@51045
    23
wenzelm@51045
    24
in
wenzelm@51045
    25
wenzelm@50683
    26
fun protocol_message props output =
wenzelm@51216
    27
  (case props of
wenzelm@51216
    28
    function :: args =>
wenzelm@51216
    29
      if member (op =) protocol_echo function then
wenzelm@51216
    30
        writeln ("\f" ^ #2 function ^ " = " ^ YXML.string_of_body (XML.Encode.properties args))
wenzelm@51216
    31
      else
wenzelm@51216
    32
        (case Markup.dest_loading_theory props of
wenzelm@51216
    33
          SOME name => writeln ("\floading_theory = " ^ name)
wenzelm@51216
    34
        | NONE => protocol_undef ())
wenzelm@51216
    35
  | [] => protocol_undef ());
wenzelm@50683
    36
wenzelm@51045
    37
end;
wenzelm@51045
    38
wenzelm@50683
    39
wenzelm@50683
    40
(* build *)
wenzelm@50683
    41
wenzelm@48465
    42
local
wenzelm@48465
    43
wenzelm@48500
    44
fun no_document options =
wenzelm@50121
    45
  (case Options.string options "document" of "" => true | "false" => true | _ => false);
wenzelm@48500
    46
wenzelm@51217
    47
fun use_theories last_timing options =
wenzelm@51217
    48
  Thy_Info.use_theories {last_timing = last_timing, master_dir = Path.current}
wenzelm@48457
    49
    |> Unsynchronized.setmp Proofterm.proofs (Options.int options "proofs")
wenzelm@48457
    50
    |> Unsynchronized.setmp print_mode
wenzelm@48457
    51
        (space_explode "," (Options.string options "print_mode") @ print_mode_value ())
wenzelm@48457
    52
    |> Unsynchronized.setmp Goal.parallel_proofs (Options.int options "parallel_proofs")
wenzelm@48457
    53
    |> Unsynchronized.setmp Goal.parallel_proofs_threshold
wenzelm@48457
    54
        (Options.int options "parallel_proofs_threshold")
wenzelm@48457
    55
    |> Unsynchronized.setmp Multithreading.trace (Options.int options "threads_trace")
wenzelm@48460
    56
    |> Unsynchronized.setmp Multithreading.max_threads (Options.int options "threads")
wenzelm@50698
    57
    |> Unsynchronized.setmp Future.ML_statistics true
wenzelm@48500
    58
    |> no_document options ? Present.no_document
wenzelm@48486
    59
    |> Unsynchronized.setmp quick_and_dirty (Options.bool options "quick_and_dirty")
wenzelm@48634
    60
    |> Unsynchronized.setmp Toplevel.skip_proofs (Options.bool options "skip_proofs")
wenzelm@48486
    61
    |> Unsynchronized.setmp Printer.show_question_marks_default
wenzelm@48486
    62
        (Options.bool options "show_question_marks")
wenzelm@48486
    63
    |> Unsynchronized.setmp Name_Space.names_long_default (Options.bool options "names_long")
wenzelm@48486
    64
    |> Unsynchronized.setmp Name_Space.names_short_default (Options.bool options "names_short")
wenzelm@48492
    65
    |> Unsynchronized.setmp Name_Space.names_unique_default (Options.bool options "names_unique")
wenzelm@48520
    66
    |> Unsynchronized.setmp Thy_Output.display_default (Options.bool options "thy_output_display")
wenzelm@48520
    67
    |> Unsynchronized.setmp Thy_Output.quotes_default (Options.bool options "thy_output_quotes")
wenzelm@48520
    68
    |> Unsynchronized.setmp Thy_Output.indent_default (Options.int options "thy_output_indent")
wenzelm@48520
    69
    |> Unsynchronized.setmp Thy_Output.source_default (Options.bool options "thy_output_source")
wenzelm@48520
    70
    |> Unsynchronized.setmp Thy_Output.break_default (Options.bool options "thy_output_break")
wenzelm@48527
    71
    |> Unsynchronized.setmp Pretty.margin_default (Options.int options "pretty_margin")
wenzelm@48492
    72
    |> Unsynchronized.setmp Toplevel.timing (Options.bool options "timing");
wenzelm@48457
    73
wenzelm@51217
    74
fun use_theories_condition last_timing (options, thys) =
wenzelm@48465
    75
  let val condition = space_explode "," (Options.string options "condition") in
wenzelm@48465
    76
    (case filter_out (can getenv_strict) condition of
wenzelm@51217
    77
      [] => use_theories last_timing options (map (rpair Position.none) thys)
wenzelm@48465
    78
    | conds =>
wenzelm@48512
    79
        Output.physical_stderr ("Skipping theories " ^ commas_quote thys ^
wenzelm@48477
    80
          " (undefined " ^ commas conds ^ ")\n"))
wenzelm@48465
    81
  end;
wenzelm@48465
    82
wenzelm@51218
    83
structure Timings =
wenzelm@51218
    84
  Table(type key = Properties.T val ord = dict_ord (prod_ord fast_string_ord fast_string_ord));
wenzelm@51218
    85
wenzelm@51218
    86
fun make_timing props =
wenzelm@51218
    87
  let val (t, id) = List.partition (member (op =) Markup.timing_propertiesN o fst) props
wenzelm@51218
    88
  in (id, Markup.parse_timing_properties t) end;
wenzelm@51218
    89
wenzelm@51218
    90
fun make_timings timings = fold (Timings.update o make_timing) timings Timings.empty;
wenzelm@51218
    91
wenzelm@48465
    92
in
wenzelm@48465
    93
wenzelm@48681
    94
fun build args_file = Command_Line.tool (fn () =>
wenzelm@48672
    95
    let
wenzelm@51218
    96
      val (timings, (do_output, (options, (verbose, (browser_info,
wenzelm@51218
    97
          (parent_name, (name, theories))))))) =
wenzelm@48672
    98
        File.read (Path.explode args_file) |> YXML.parse_body |>
wenzelm@48672
    99
          let open XML.Decode in
wenzelm@51218
   100
            pair (list properties) (pair bool (pair Options.decode (pair bool (pair string
wenzelm@51218
   101
              (pair string (pair string ((list (pair Options.decode (list string))))))))))
wenzelm@48672
   102
          end;
wenzelm@48418
   103
wenzelm@48804
   104
      val document_variants =
wenzelm@48804
   105
        map Present.read_variant (space_explode ":" (Options.string options "document_variants"));
wenzelm@48672
   106
      val _ =
wenzelm@48805
   107
        (case duplicates (op =) (map fst document_variants) of
wenzelm@48805
   108
          [] => ()
wenzelm@48805
   109
        | dups => error ("Duplicate document variants: " ^ commas_quote dups));
wenzelm@49242
   110
wenzelm@50982
   111
      val _ = writeln ("\fSession.name = " ^ name);
wenzelm@49242
   112
      val _ =
wenzelm@50707
   113
        (case Session.path () of
wenzelm@50707
   114
          [] => ()
wenzelm@50707
   115
        | path => writeln ("\fSession.parent_path = " ^ space_implode "/" path));
wenzelm@50707
   116
      val _ =
wenzelm@48672
   117
        Session.init do_output false
wenzelm@48672
   118
          (Options.bool options "browser_info") browser_info
wenzelm@48672
   119
          (Options.string options "document")
wenzelm@48672
   120
          (Options.bool options "document_graph")
wenzelm@48805
   121
          (Options.string options "document_output")
wenzelm@48804
   122
          document_variants
wenzelm@48672
   123
          parent_name name
wenzelm@50121
   124
          (false, "") ""
wenzelm@50121
   125
          verbose;
wenzelm@49911
   126
wenzelm@51218
   127
      val last_timing =
wenzelm@51218
   128
        the_default Timing.zero o
wenzelm@51218
   129
          Timings.lookup (make_timings timings) o Toplevel.approximative_id;
wenzelm@51218
   130
wenzelm@49911
   131
      val res1 =
wenzelm@48673
   132
        theories |>
wenzelm@51218
   133
          (List.app (use_theories_condition last_timing)
wenzelm@48673
   134
            |> Session.with_timing name verbose
wenzelm@50683
   135
            |> Unsynchronized.setmp Output.Private_Hooks.protocol_message_fn protocol_message
wenzelm@49911
   136
            |> Unsynchronized.setmp Multithreading.max_threads (Options.int options "threads")
wenzelm@49911
   137
            |> Exn.capture);
wenzelm@49911
   138
      val res2 = Exn.capture Session.finish ();
wenzelm@49911
   139
      val _ = Par_Exn.release_all [res1, res2];
wenzelm@48673
   140
wenzelm@48734
   141
      val _ = if do_output then () else exit 0;
wenzelm@48681
   142
    in 0 end);
wenzelm@48418
   143
wenzelm@48418
   144
end;
wenzelm@48465
   145
wenzelm@48465
   146
end;