src/Pure/Tools/build.ML
author wenzelm
Tue Mar 12 16:47:24 2013 +0100 (2013-03-12 ago)
changeset 51399 6ac3c29a300e
parent 51398 c3d02b3518c2
child 51423 e5f9a6d9ca82
permissions -rw-r--r--
discontinued "isabelle usedir" option -r (reset session path);
simplified internal session identification: chapter / name;
clarified chapter index (of sessions) vs. session index (of theories);
discontinued "up" links, for improved modularity also wrt. partial browser_info (users can use "back" within the browser);
removed obsolete session parent_path;
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@51228
    83
wenzelm@51228
    84
(* command timings *)
wenzelm@51228
    85
wenzelm@51228
    86
type timings = ((string * Time.time) Inttab.table) Symtab.table;  (*file -> offset -> name * time *)
wenzelm@51228
    87
wenzelm@51228
    88
val empty_timings: timings = Symtab.empty;
wenzelm@51218
    89
wenzelm@51228
    90
fun update_timings props =
wenzelm@51228
    91
  (case Markup.parse_command_timing_properties props of
wenzelm@51228
    92
    SOME ({file, offset, name}, time) =>
wenzelm@51228
    93
      Symtab.map_default (file, Inttab.empty) (Inttab.update (offset, (name, time)))
wenzelm@51228
    94
  | NONE => I);
wenzelm@51218
    95
wenzelm@51228
    96
fun lookup_timings timings tr =
wenzelm@51228
    97
  (case Toplevel.approximative_id tr of
wenzelm@51228
    98
    SOME {file, offset, name} =>
wenzelm@51228
    99
      (case Symtab.lookup timings file of
wenzelm@51228
   100
        SOME offsets =>
wenzelm@51228
   101
          (case Inttab.lookup offsets offset of
wenzelm@51228
   102
            SOME (name', time) => if name = name' then time else Time.zeroTime
wenzelm@51228
   103
          | NONE => Time.zeroTime)
wenzelm@51228
   104
      | NONE => Time.zeroTime)
wenzelm@51228
   105
  | NONE => Time.zeroTime);
wenzelm@51218
   106
wenzelm@48465
   107
in
wenzelm@48465
   108
wenzelm@48681
   109
fun build args_file = Command_Line.tool (fn () =>
wenzelm@48672
   110
    let
wenzelm@51220
   111
      val (command_timings, (do_output, (options, (verbose, (browser_info,
wenzelm@51399
   112
          (parent_name, (chapter, (name, theories)))))))) =
wenzelm@48672
   113
        File.read (Path.explode args_file) |> YXML.parse_body |>
wenzelm@48672
   114
          let open XML.Decode in
wenzelm@51218
   115
            pair (list properties) (pair bool (pair Options.decode (pair bool (pair string
wenzelm@51399
   116
              (pair string (pair string (pair string
wenzelm@51399
   117
                ((list (pair Options.decode (list string)))))))))))
wenzelm@48672
   118
          end;
wenzelm@48418
   119
wenzelm@48804
   120
      val document_variants =
wenzelm@48804
   121
        map Present.read_variant (space_explode ":" (Options.string options "document_variants"));
wenzelm@48672
   122
      val _ =
wenzelm@48805
   123
        (case duplicates (op =) (map fst document_variants) of
wenzelm@48805
   124
          [] => ()
wenzelm@48805
   125
        | dups => error ("Duplicate document variants: " ^ commas_quote dups));
wenzelm@49242
   126
wenzelm@50982
   127
      val _ = writeln ("\fSession.name = " ^ name);
wenzelm@49242
   128
      val _ =
wenzelm@51399
   129
        Session.init do_output
wenzelm@51399
   130
          (Options.bool options "browser_info")
wenzelm@51399
   131
          (Path.explode browser_info)
wenzelm@48672
   132
          (Options.string options "document")
wenzelm@48672
   133
          (Options.bool options "document_graph")
wenzelm@48805
   134
          (Options.string options "document_output")
wenzelm@48804
   135
          document_variants
wenzelm@51399
   136
          parent_name (chapter, name)
wenzelm@51398
   137
          (false, "")
wenzelm@50121
   138
          verbose;
wenzelm@49911
   139
wenzelm@51228
   140
      val last_timing = lookup_timings (fold update_timings command_timings empty_timings);
wenzelm@51218
   141
wenzelm@49911
   142
      val res1 =
wenzelm@48673
   143
        theories |>
wenzelm@51218
   144
          (List.app (use_theories_condition last_timing)
wenzelm@48673
   145
            |> Session.with_timing name verbose
wenzelm@50683
   146
            |> Unsynchronized.setmp Output.Private_Hooks.protocol_message_fn protocol_message
wenzelm@49911
   147
            |> Unsynchronized.setmp Multithreading.max_threads (Options.int options "threads")
wenzelm@49911
   148
            |> Exn.capture);
wenzelm@49911
   149
      val res2 = Exn.capture Session.finish ();
wenzelm@49911
   150
      val _ = Par_Exn.release_all [res1, res2];
wenzelm@48673
   151
wenzelm@48734
   152
      val _ = if do_output then () else exit 0;
wenzelm@48681
   153
    in 0 end);
wenzelm@48418
   154
wenzelm@48418
   155
end;
wenzelm@48465
   156
wenzelm@48465
   157
end;