src/Pure/Tools/build.ML
author wenzelm
Wed, 20 Feb 2013 15:22:22 +0100
changeset 51228 dff3471dd8bc
parent 51220 e140c8fa485a
child 51398 c3d02b3518c2
permissions -rw-r--r--
more tight representation of command timing; tuned signatures; tuned;

(*  Title:      Pure/Tools/build.ML
    Author:     Makarius

Build Isabelle sessions.
*)

signature BUILD =
sig
  val build: string -> unit
end;

structure Build: BUILD =
struct

(* protocol messages *)

local

(* FIXME avoid hardwired stuff!? *)
val protocol_echo = [Markup.ML_statistics, Markup.task_statistics, Markup.command_timing];

fun protocol_undef () = raise Fail "Undefined Output.protocol_message";

in

fun protocol_message props output =
  (case props of
    function :: args =>
      if member (op =) protocol_echo function then
        writeln ("\f" ^ #2 function ^ " = " ^ YXML.string_of_body (XML.Encode.properties args))
      else
        (case Markup.dest_loading_theory props of
          SOME name => writeln ("\floading_theory = " ^ name)
        | NONE => protocol_undef ())
  | [] => protocol_undef ());

end;


(* build *)

local

fun no_document options =
  (case Options.string options "document" of "" => true | "false" => true | _ => false);

fun use_theories last_timing options =
  Thy_Info.use_theories {last_timing = last_timing, master_dir = Path.current}
    |> Unsynchronized.setmp Proofterm.proofs (Options.int options "proofs")
    |> Unsynchronized.setmp print_mode
        (space_explode "," (Options.string options "print_mode") @ print_mode_value ())
    |> Unsynchronized.setmp Goal.parallel_proofs (Options.int options "parallel_proofs")
    |> Unsynchronized.setmp Goal.parallel_proofs_threshold
        (Options.int options "parallel_proofs_threshold")
    |> Unsynchronized.setmp Multithreading.trace (Options.int options "threads_trace")
    |> Unsynchronized.setmp Multithreading.max_threads (Options.int options "threads")
    |> Unsynchronized.setmp Future.ML_statistics true
    |> no_document options ? Present.no_document
    |> Unsynchronized.setmp quick_and_dirty (Options.bool options "quick_and_dirty")
    |> Unsynchronized.setmp Toplevel.skip_proofs (Options.bool options "skip_proofs")
    |> Unsynchronized.setmp Printer.show_question_marks_default
        (Options.bool options "show_question_marks")
    |> Unsynchronized.setmp Name_Space.names_long_default (Options.bool options "names_long")
    |> Unsynchronized.setmp Name_Space.names_short_default (Options.bool options "names_short")
    |> Unsynchronized.setmp Name_Space.names_unique_default (Options.bool options "names_unique")
    |> Unsynchronized.setmp Thy_Output.display_default (Options.bool options "thy_output_display")
    |> Unsynchronized.setmp Thy_Output.quotes_default (Options.bool options "thy_output_quotes")
    |> Unsynchronized.setmp Thy_Output.indent_default (Options.int options "thy_output_indent")
    |> Unsynchronized.setmp Thy_Output.source_default (Options.bool options "thy_output_source")
    |> Unsynchronized.setmp Thy_Output.break_default (Options.bool options "thy_output_break")
    |> Unsynchronized.setmp Pretty.margin_default (Options.int options "pretty_margin")
    |> Unsynchronized.setmp Toplevel.timing (Options.bool options "timing");

fun use_theories_condition last_timing (options, thys) =
  let val condition = space_explode "," (Options.string options "condition") in
    (case filter_out (can getenv_strict) condition of
      [] => use_theories last_timing options (map (rpair Position.none) thys)
    | conds =>
        Output.physical_stderr ("Skipping theories " ^ commas_quote thys ^
          " (undefined " ^ commas conds ^ ")\n"))
  end;


(* command timings *)

type timings = ((string * Time.time) Inttab.table) Symtab.table;  (*file -> offset -> name * time *)

val empty_timings: timings = Symtab.empty;

fun update_timings props =
  (case Markup.parse_command_timing_properties props of
    SOME ({file, offset, name}, time) =>
      Symtab.map_default (file, Inttab.empty) (Inttab.update (offset, (name, time)))
  | NONE => I);

fun lookup_timings timings tr =
  (case Toplevel.approximative_id tr of
    SOME {file, offset, name} =>
      (case Symtab.lookup timings file of
        SOME offsets =>
          (case Inttab.lookup offsets offset of
            SOME (name', time) => if name = name' then time else Time.zeroTime
          | NONE => Time.zeroTime)
      | NONE => Time.zeroTime)
  | NONE => Time.zeroTime);

in

fun build args_file = Command_Line.tool (fn () =>
    let
      val (command_timings, (do_output, (options, (verbose, (browser_info,
          (parent_name, (name, theories))))))) =
        File.read (Path.explode args_file) |> YXML.parse_body |>
          let open XML.Decode in
            pair (list properties) (pair bool (pair Options.decode (pair bool (pair string
              (pair string (pair string ((list (pair Options.decode (list string))))))))))
          end;

      val document_variants =
        map Present.read_variant (space_explode ":" (Options.string options "document_variants"));
      val _ =
        (case duplicates (op =) (map fst document_variants) of
          [] => ()
        | dups => error ("Duplicate document variants: " ^ commas_quote dups));

      val _ = writeln ("\fSession.name = " ^ name);
      val _ =
        (case Session.path () of
          [] => ()
        | path => writeln ("\fSession.parent_path = " ^ space_implode "/" path));
      val _ =
        Session.init do_output false
          (Options.bool options "browser_info") browser_info
          (Options.string options "document")
          (Options.bool options "document_graph")
          (Options.string options "document_output")
          document_variants
          parent_name name
          (false, "") ""
          verbose;

      val last_timing = lookup_timings (fold update_timings command_timings empty_timings);

      val res1 =
        theories |>
          (List.app (use_theories_condition last_timing)
            |> Session.with_timing name verbose
            |> Unsynchronized.setmp Output.Private_Hooks.protocol_message_fn protocol_message
            |> Unsynchronized.setmp Multithreading.max_threads (Options.int options "threads")
            |> Exn.capture);
      val res2 = Exn.capture Session.finish ();
      val _ = Par_Exn.release_all [res1, res2];

      val _ = if do_output then () else exit 0;
    in 0 end);

end;

end;