| author | wenzelm | 
| Fri, 27 Mar 2020 12:03:20 +0100 | |
| changeset 71594 | 8a298184f3f9 | 
| parent 71383 | 8313dca6dee9 | 
| child 71598 | 269dc4bf1f40 | 
| permissions | -rw-r--r-- | 
| 65477 | 1  | 
/* Title: Pure/ML/ml_process.scala  | 
| 62544 | 2  | 
Author: Makarius  | 
3  | 
||
| 62586 | 4  | 
The raw ML process.  | 
| 62544 | 5  | 
*/  | 
6  | 
||
7  | 
package isabelle  | 
|
8  | 
||
9  | 
||
| 62573 | 10  | 
import java.io.{File => JFile}
 | 
11  | 
||
12  | 
||
| 62544 | 13  | 
object ML_Process  | 
14  | 
{
 | 
|
15  | 
def apply(options: Options,  | 
|
| 71594 | 16  | 
sessions_structure: Sessions.Structure,  | 
| 
62634
 
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
 
wenzelm 
parents: 
62633 
diff
changeset
 | 
17  | 
logic: String = "",  | 
| 62544 | 18  | 
args: List[String] = Nil,  | 
| 
62556
 
c115e69f457f
more abstract Session.start, without prover command-line;
 
wenzelm 
parents: 
62548 
diff
changeset
 | 
19  | 
modes: List[String] = Nil,  | 
| 62643 | 20  | 
raw_ml_system: Boolean = false,  | 
| 62573 | 21  | 
cwd: JFile = null,  | 
| 
62610
 
4c89504c76fb
more uniform signature for various process invocations;
 
wenzelm 
parents: 
62606 
diff
changeset
 | 
22  | 
env: Map[String, String] = Isabelle_System.settings(),  | 
| 62557 | 23  | 
redirect: Boolean = false,  | 
| 62603 | 24  | 
cleanup: () => Unit = () => (),  | 
| 
65431
 
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
 
wenzelm 
parents: 
65420 
diff
changeset
 | 
25  | 
session_base: Option[Sessions.Base] = None,  | 
| 68209 | 26  | 
store: Option[Sessions.Store] = None): Bash.Process =  | 
| 62544 | 27  | 
  {
 | 
| 
62637
 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 
wenzelm 
parents: 
62634 
diff
changeset
 | 
28  | 
val logic_name = Isabelle_System.default_logic(logic)  | 
| 68211 | 29  | 
val _store = store.getOrElse(Sessions.store(options))  | 
| 68209 | 30  | 
|
| 
62637
 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 
wenzelm 
parents: 
62634 
diff
changeset
 | 
31  | 
val heaps: List[String] =  | 
| 62643 | 32  | 
if (raw_ml_system) Nil  | 
| 
62637
 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 
wenzelm 
parents: 
62634 
diff
changeset
 | 
33  | 
      else {
 | 
| 71594 | 34  | 
sessions_structure.selection(Sessions.Selection.session(logic_name)).  | 
| 
70683
 
8c7706b053c7
find theory files via session structure: much faster Prover IDE startup;
 
wenzelm 
parents: 
69831 
diff
changeset
 | 
35  | 
build_requirements(List(logic_name)).  | 
| 68221 | 36  | 
map(a => File.platform_path(_store.the_heap(a)))  | 
| 62544 | 37  | 
}  | 
38  | 
||
| 
62637
 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 
wenzelm 
parents: 
62634 
diff
changeset
 | 
39  | 
val eval_init =  | 
| 
62634
 
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
 
wenzelm 
parents: 
62633 
diff
changeset
 | 
40  | 
      if (heaps.isEmpty) {
 | 
| 62544 | 41  | 
List(  | 
| 62912 | 42  | 
"""  | 
43  | 
fun chapter (_: string) = ();  | 
|
44  | 
fun section (_: string) = ();  | 
|
45  | 
fun subsection (_: string) = ();  | 
|
46  | 
fun subsubsection (_: string) = ();  | 
|
47  | 
fun paragraph (_: string) = ();  | 
|
48  | 
fun subparagraph (_: string) = ();  | 
|
49  | 
val ML_file = PolyML.use;  | 
|
50  | 
""",  | 
|
| 62544 | 51  | 
if (Platform.is_windows)  | 
52  | 
"fun exit 0 = OS.Process.exit OS.Process.success" +  | 
|
53  | 
" | exit 1 = OS.Process.exit OS.Process.failure" +  | 
|
54  | 
" | exit rc = OS.Process.exit (RunCall.unsafeCast (Word8.fromInt rc))"  | 
|
| 62560 | 55  | 
else  | 
56  | 
"fun exit rc = Posix.Process.exit (Word8.fromInt rc)",  | 
|
| 
62629
 
1815513a57f1
clarified prompt: "ML" usually means Isabelle/ML;
 
wenzelm 
parents: 
62614 
diff
changeset
 | 
57  | 
"PolyML.Compiler.prompt1 := \"Poly/ML> \"",  | 
| 
 
1815513a57f1
clarified prompt: "ML" usually means Isabelle/ML;
 
wenzelm 
parents: 
62614 
diff
changeset
 | 
58  | 
"PolyML.Compiler.prompt2 := \"Poly/ML# \"")  | 
| 62544 | 59  | 
}  | 
| 
62637
 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 
wenzelm 
parents: 
62634 
diff
changeset
 | 
60  | 
else  | 
| 
 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 
wenzelm 
parents: 
62634 
diff
changeset
 | 
61  | 
List(  | 
| 
 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 
wenzelm 
parents: 
62634 
diff
changeset
 | 
62  | 
"(PolyML.SaveState.loadHierarchy " +  | 
| 66782 | 63  | 
ML_Syntax.print_list(ML_Syntax.print_string_bytes)(heaps) +  | 
| 
62637
 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 
wenzelm 
parents: 
62634 
diff
changeset
 | 
64  | 
"; PolyML.print_depth 0) handle exn => (TextIO.output (TextIO.stdErr, General.exnMessage exn ^ " +  | 
| 66782 | 65  | 
          ML_Syntax.print_string_bytes(": " + logic_name + "\n") +
 | 
| 
62637
 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 
wenzelm 
parents: 
62634 
diff
changeset
 | 
66  | 
"); OS.Process.exit OS.Process.failure)")  | 
| 62544 | 67  | 
|
68  | 
val eval_modes =  | 
|
69  | 
if (modes.isEmpty) Nil  | 
|
| 66782 | 70  | 
      else List("Print_Mode.add_modes " + ML_Syntax.print_list(ML_Syntax.print_string_bytes)(modes))
 | 
| 62544 | 71  | 
|
72  | 
// options  | 
|
73  | 
    val isabelle_process_options = Isabelle_System.tmp_file("options")
 | 
|
| 71114 | 74  | 
    Isabelle_System.chmod("600", File.path(isabelle_process_options))
 | 
| 62544 | 75  | 
File.write(isabelle_process_options, YXML.string_of_body(options.encode))  | 
| 62573 | 76  | 
    val env_options = Map("ISABELLE_PROCESS_OPTIONS" -> File.standard_path(isabelle_process_options))
 | 
| 
62634
 
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
 
wenzelm 
parents: 
62633 
diff
changeset
 | 
77  | 
    val eval_options = if (heaps.isEmpty) Nil else List("Options.load_default ()")
 | 
| 62544 | 78  | 
|
| 
65431
 
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
 
wenzelm 
parents: 
65420 
diff
changeset
 | 
79  | 
// session base  | 
| 
 
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
 
wenzelm 
parents: 
65420 
diff
changeset
 | 
80  | 
val eval_session_base =  | 
| 
 
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
 
wenzelm 
parents: 
65420 
diff
changeset
 | 
81  | 
      session_base match {
 | 
| 
 
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
 
wenzelm 
parents: 
65420 
diff
changeset
 | 
82  | 
case None => Nil  | 
| 
 
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
 
wenzelm 
parents: 
65420 
diff
changeset
 | 
83  | 
case Some(base) =>  | 
| 65441 | 84  | 
def print_table(table: List[(String, String)]): String =  | 
| 
65431
 
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
 
wenzelm 
parents: 
65420 
diff
changeset
 | 
85  | 
ML_Syntax.print_list(  | 
| 
 
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
 
wenzelm 
parents: 
65420 
diff
changeset
 | 
86  | 
ML_Syntax.print_pair(  | 
| 66782 | 87  | 
ML_Syntax.print_string_bytes, ML_Syntax.print_string_bytes))(table)  | 
| 66712 | 88  | 
def print_list(list: List[String]): String =  | 
| 66782 | 89  | 
ML_Syntax.print_list(ML_Syntax.print_string_bytes)(list)  | 
| 
67493
 
c4e9e0c50487
treat sessions as entities with defining position;
 
wenzelm 
parents: 
67471 
diff
changeset
 | 
90  | 
def print_sessions(list: List[(String, Position.T)]): String =  | 
| 
 
c4e9e0c50487
treat sessions as entities with defining position;
 
wenzelm 
parents: 
67471 
diff
changeset
 | 
91  | 
ML_Syntax.print_list(  | 
| 
 
c4e9e0c50487
treat sessions as entities with defining position;
 
wenzelm 
parents: 
67471 
diff
changeset
 | 
92  | 
ML_Syntax.print_pair(ML_Syntax.print_string_bytes, ML_Syntax.print_properties))(list)  | 
| 
 
c4e9e0c50487
treat sessions as entities with defining position;
 
wenzelm 
parents: 
67471 
diff
changeset
 | 
93  | 
|
| 65532 | 94  | 
          List("Resources.init_session_base" +
 | 
| 71594 | 95  | 
            " {session_positions = " + print_sessions(sessions_structure.session_positions) +
 | 
96  | 
", session_directories = " + print_table(sessions_structure.dest_session_directories) +  | 
|
| 67520 | 97  | 
", docs = " + print_list(base.doc_names) +  | 
| 67219 | 98  | 
", global_theories = " + print_table(base.global_theories.toList) +  | 
| 
70712
 
a3cfe859d915
find theories via session directories only -- ignore known_theories;
 
wenzelm 
parents: 
70683 
diff
changeset
 | 
99  | 
", loaded_theories = " + print_list(base.loaded_theories.keys) + "}")  | 
| 
65431
 
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
 
wenzelm 
parents: 
65420 
diff
changeset
 | 
100  | 
}  | 
| 
 
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
 
wenzelm 
parents: 
65420 
diff
changeset
 | 
101  | 
|
| 
 
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
 
wenzelm 
parents: 
65420 
diff
changeset
 | 
102  | 
// process  | 
| 62544 | 103  | 
val eval_process =  | 
| 
62634
 
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
 
wenzelm 
parents: 
62633 
diff
changeset
 | 
104  | 
if (heaps.isEmpty)  | 
| 62712 | 105  | 
        List("PolyML.print_depth " + ML_Syntax.print_int(options.int("ML_print_depth")))
 | 
| 
69572
 
09a6a7c04b45
more robust system channel via options that are private to the user;
 
wenzelm 
parents: 
68221 
diff
changeset
 | 
106  | 
      else List("Isabelle_Process.init ()")
 | 
| 62544 | 107  | 
|
| 
62601
 
a937889f0086
create ISABELLE_TMP in Scala (despite odd/obsolete chmod in d84b4d39bce1);
 
wenzelm 
parents: 
62600 
diff
changeset
 | 
108  | 
// ISABELLE_TMP  | 
| 
 
a937889f0086
create ISABELLE_TMP in Scala (despite odd/obsolete chmod in d84b4d39bce1);
 
wenzelm 
parents: 
62600 
diff
changeset
 | 
109  | 
    val isabelle_tmp = Isabelle_System.tmp_dir("process")
 | 
| 
 
a937889f0086
create ISABELLE_TMP in Scala (despite odd/obsolete chmod in d84b4d39bce1);
 
wenzelm 
parents: 
62600 
diff
changeset
 | 
110  | 
    val env_tmp = Map("ISABELLE_TMP" -> File.standard_path(isabelle_tmp))
 | 
| 
 
a937889f0086
create ISABELLE_TMP in Scala (despite odd/obsolete chmod in d84b4d39bce1);
 
wenzelm 
parents: 
62600 
diff
changeset
 | 
111  | 
|
| 64274 | 112  | 
val ml_runtime_options =  | 
113  | 
    {
 | 
|
114  | 
      val ml_options = Word.explode(Isabelle_System.getenv("ML_OPTIONS"))
 | 
|
| 69702 | 115  | 
val ml_options1 =  | 
116  | 
        if (ml_options.exists(_.containsSlice("gcthreads"))) ml_options
 | 
|
117  | 
        else ml_options ::: List("--gcthreads", options.int("threads").toString)
 | 
|
118  | 
val ml_options2 =  | 
|
119  | 
        if (!Platform.is_windows || ml_options.exists(_.containsSlice("codepage"))) ml_options1
 | 
|
120  | 
        else ml_options1 ::: List("--codepage", "utf8")
 | 
|
121  | 
ml_options2  | 
|
| 64274 | 122  | 
}  | 
123  | 
||
| 
62545
 
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
 
wenzelm 
parents: 
62544 
diff
changeset
 | 
124  | 
// bash  | 
| 
 
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
 
wenzelm 
parents: 
62544 
diff
changeset
 | 
125  | 
val bash_args =  | 
| 64274 | 126  | 
ml_runtime_options :::  | 
| 71383 | 127  | 
(eval_init ::: eval_modes ::: eval_options ::: eval_session_base ::: eval_process)  | 
128  | 
        .flatMap(eval => List("--eval", eval)) ::: args
 | 
|
| 62546 | 129  | 
|
| 
63986
 
c7a4b03727ae
options for process policy, notably for multiprocessor machines;
 
wenzelm 
parents: 
62912 
diff
changeset
 | 
130  | 
Bash.process(  | 
| 
 
c7a4b03727ae
options for process policy, notably for multiprocessor machines;
 
wenzelm 
parents: 
62912 
diff
changeset
 | 
131  | 
      "exec " + options.string("ML_process_policy") + """ "$ML_HOME/poly" -q """ +
 | 
| 64304 | 132  | 
Bash.strings(bash_args),  | 
| 62614 | 133  | 
cwd = cwd,  | 
| 
67586
 
8b19a8a7f029
more robust: avoid global change of LD_LIBRARY_PATH (e.g. relevant for subprocesses);
 
wenzelm 
parents: 
67520 
diff
changeset
 | 
134  | 
env = env ++ env_options ++ env_tmp,  | 
| 62614 | 135  | 
redirect = redirect,  | 
| 62602 | 136  | 
cleanup = () =>  | 
137  | 
        {
 | 
|
138  | 
isabelle_process_options.delete  | 
|
139  | 
Isabelle_System.rm_tree(isabelle_tmp)  | 
|
| 62603 | 140  | 
cleanup()  | 
| 62602 | 141  | 
})  | 
| 62544 | 142  | 
}  | 
| 62586 | 143  | 
|
144  | 
||
| 
62835
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
145  | 
/* Isabelle tool wrapper */  | 
| 62586 | 146  | 
|
| 
62835
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
147  | 
  val isabelle_tool = Isabelle_Tool("process", "raw ML process (batch mode)", args =>
 | 
| 62586 | 148  | 
  {
 | 
| 
62835
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
149  | 
var dirs: List[Path] = Nil  | 
| 
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
150  | 
var eval_args: List[String] = Nil  | 
| 
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
151  | 
    var logic = Isabelle_System.getenv("ISABELLE_LOGIC")
 | 
| 
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
152  | 
var modes: List[String] = Nil  | 
| 
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
153  | 
var options = Options.init()  | 
| 62586 | 154  | 
|
| 
62835
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
155  | 
    val getopts = Getopts("""
 | 
| 
62634
 
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
 
wenzelm 
parents: 
62633 
diff
changeset
 | 
156  | 
Usage: isabelle process [OPTIONS]  | 
| 62586 | 157  | 
|
158  | 
Options are:  | 
|
| 62677 | 159  | 
-T THEORY load theory  | 
| 62639 | 160  | 
-d DIR include session directory  | 
| 62586 | 161  | 
-e ML_EXPR evaluate ML expression on startup  | 
162  | 
-f ML_FILE evaluate ML file on startup  | 
|
| 
62634
 
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
 
wenzelm 
parents: 
62633 
diff
changeset
 | 
163  | 
-l NAME logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """)  | 
| 62586 | 164  | 
-m MODE add print mode for output  | 
165  | 
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME)  | 
|
166  | 
||
| 
62634
 
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
 
wenzelm 
parents: 
62633 
diff
changeset
 | 
167  | 
Run the raw Isabelle ML process in batch mode.  | 
| 62586 | 168  | 
""",  | 
| 
62835
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
169  | 
"T:" -> (arg =>  | 
| 66782 | 170  | 
        eval_args = eval_args ::: List("--eval", "use_thy " + ML_Syntax.print_string_bytes(arg))),
 | 
| 
62835
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
171  | 
"d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),  | 
| 
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
172  | 
      "e:" -> (arg => eval_args = eval_args ::: List("--eval", arg)),
 | 
| 
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
173  | 
      "f:" -> (arg => eval_args = eval_args ::: List("--use", arg)),
 | 
| 
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
174  | 
"l:" -> (arg => logic = arg),  | 
| 
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
175  | 
"m:" -> (arg => modes = arg :: modes),  | 
| 
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
176  | 
"o:" -> (arg => options = options + arg))  | 
| 62586 | 177  | 
|
| 
62835
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
178  | 
val more_args = getopts(args)  | 
| 71383 | 179  | 
if (args.isEmpty || more_args.nonEmpty) getopts.usage()  | 
| 62586 | 180  | 
|
| 71594 | 181  | 
val sessions_structure = Sessions.load_structure(options, dirs = dirs)  | 
182  | 
||
183  | 
val rc =  | 
|
184  | 
ML_Process(options, sessions_structure, logic = logic, args = eval_args, modes = modes)  | 
|
185  | 
.result().print_stdout.rc  | 
|
| 62888 | 186  | 
sys.exit(rc)  | 
| 
62835
 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 
wenzelm 
parents: 
62712 
diff
changeset
 | 
187  | 
})  | 
| 62544 | 188  | 
}  |