3 let environment = Unix.environment ()
5 let bindir = ref Coq_config.bindir
6 let binary = "coqtop.byte"
8 let xml_theory_library_root = ref (
10 Sys.getenv "XML_THEORY_LIBRARY_ROOT"
14 (* the $COQBIN environment variable has priority over the Coq_config value *)
17 let c = Sys.getenv "COQBIN" in
18 if c <> "" then bindir := c
21 (* coq_v2theoryxml options *)
25 (* Verifies that a string do not contains others caracters than letters,
28 let check_module_name s =
31 "Modules names must only contain letters, digits, or underscores\n";
33 "and must begin with a letter\n";
36 match String.get s 0 with
37 | 'a' .. 'z' | 'A' .. 'Z' ->
38 for i = 1 to (String.length s)-1 do
39 match String.get s i with
40 | 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '_' -> ()
45 (* compilation of a file [file] with command [command] and args [args] *)
47 let compile command args file =
48 let dirname = Filename.dirname file in
49 let basename = Filename.basename file in
51 if Filename.check_suffix basename ".vo" then
52 Filename.chop_suffix basename ".vo"
56 check_module_name modulename;
57 let tmpfile = Filename.temp_file "coq_v2theoryxml" ".v" in
59 command :: "-batch" :: "-silent" :: "-is" :: "barestate" :: args
60 @ ["-load-vernac-source"; tmpfile] in
62 if Sys.os_type = "Unix" then
63 Unix.openfile "/dev/null" [] 0o777
67 let oc = open_out tmpfile in
68 Printf.fprintf oc "Require XmlTheory.\n" ;
69 Printf.fprintf oc "XmlTheory Begin %s \"%s\".\n" modulename
70 !xml_theory_library_root ;
71 Printf.fprintf oc "Load %s.\n" modulename;
72 Printf.fprintf oc "XmlTheory End.\n" ;
77 Unix.create_process_env command
78 (Array.of_list args') environment devnull Unix.stdout Unix.stderr in
79 let status = Unix.waitpid [] pid in
80 if not !keep then Sys.remove tmpfile ;
82 | _, Unix.WEXITED 0 -> ()
83 | _, Unix.WEXITED 127 ->
84 Printf.printf "Cannot execute %s\n" command;
86 | _, Unix.WEXITED c -> exit c
89 if not !keep then Sys.remove tmpfile; exit 1
91 (* parsing of the command line
93 * special treatment for -bindir and -i.
94 * other options are passed to coqtop *)
98 "Usage: coq_v2theoryxml <options> <Coq options> module...\n
100 -xml-theory-library-root d specify the path to the root of the XML library
101 (overrides $XML_THEORY_LIBRARY_ROOT)
102 -image f specify an alternative executable for Coq
103 -t keep temporary files\n\n" ;
108 let rec parse (cfiles,args) = function
110 List.rev cfiles, List.rev args
111 | "-xml-theory-library-root" :: v :: rem ->
112 xml_theory_library_root := v ; parse (cfiles,args) rem
114 keep := true ; parse (cfiles,args) rem
116 bindir:= Filename.concat Coq_config.coqtop "bin";
117 parse (cfiles, "-boot"::args) rem
118 | "-bindir" :: d :: rem ->
119 bindir := d ; parse (cfiles,args) rem
123 parse (cfiles,args) rem
125 raise (Failure "To load ML modules, only -byte is allowed")
126 | "-image" :: f :: rem ->
127 image := f; parse (cfiles,args) rem
130 | ("-?"|"-h"|"-H"|"-help"|"--help") :: _ -> usage ()
131 | ("-libdir"|"-outputstate"|"-I"|"-include"
132 |"-inputstate"|"-is"|"-load-vernac-source"|"-load-vernac-object"
133 |"-load-ml-source"|"-require"|"-load-ml-object"|"-user"
134 |"-init-file" as o) :: rem ->
137 | s :: rem' -> parse (cfiles,s::o::args) rem'
140 | "-R" as o :: s :: t :: rem -> parse (cfiles,t::s::o::args) rem
141 | ("-notactics"|"-debug"|"-db"|"-debugger"|"-nolib"|"-batch"|"-nois"
142 |"-q"|"-full"|"-profile"|"-just-parsing"|"-echo" |"-unsafe"|"-quiet"
143 |"-silent"|"-m" as o) :: rem ->
144 parse (cfiles,o::args) rem
145 | ("-v"|"--version") :: _ ->
148 print_endline Coq_config.coqlib; exit 0
149 | f :: rem -> parse (f::cfiles,args) rem
151 parse ([],[]) (List.tl (Array.to_list Sys.argv))
153 (* main: we parse the command line, define the command to compile files
154 * and then call the compilation on each file *)
157 let cfiles, args = parse_args () in
158 if cfiles = [] then begin
159 prerr_endline "coq_v2theoryxml: too few arguments" ;
163 if !image <> "" then !image else Filename.concat !bindir (binary ^ Coq_config.exec_extension)
165 if !xml_theory_library_root = "" then
167 prerr_endline "coq_v2theoryxml: you must either set $XML_THEORY_LIBRARY_ROOT or use -xml-theory-library-root";
171 List.iter (compile coqtopname args) cfiles ;
173 ("\nWARNING: all the URIs in the generated XML files are broken." ^
174 "\n See the README in the XML contrib to learn how to fix them.\n")
176 let _ = Printexc.print main (); exit 0