aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/coqProject_file.ml4
blob: 13de731f54a53401fe8bfe30994126c9ed240d64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
(************************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
(* <O___,, *   INRIA - CNRS - LIX - LRI - PPS - Copyright 1999-2017     *)
(*   \VV/  **************************************************************)
(*    //   *      This file is distributed under the terms of the       *)
(*         *       GNU Lesser General Public License Version 2.1        *)
(************************************************************************)

type project = {
  project_file  : string option;
  makefile : string option;
  install_kind  : install option;
  use_ocamlopt : bool;
  bypass_API : bool;

  v_files : string list;
  mli_files : string list;
  ml4_files : string list;
  ml_files : string list;
  mllib_files : string list;
  mlpack_files : string list;

  ml_includes : path list;
  r_includes  : (path * logic_path) list;
  q_includes  : (path * logic_path) list;
  extra_args : string list;
  defs : (string * string) list;
  
  extra_targets : extra_target list;
  subdirs : string list;
}
and extra_target = {
  target : string;
  dependencies : string;
  phony : bool;
  command : string;
}
and logic_path = string
and path = { path : string; canonical_path : string }
and install =
  | NoInstall
  | TraditionalInstall
  | UserInstall

(* TODO generate with PPX *)
let mk_project project_file makefile install_kind use_ocamlopt bypass_API = {
  project_file;
  makefile;
  install_kind;
  use_ocamlopt;
  bypass_API;

  v_files = [];
  mli_files = [];
  ml4_files = [];
  ml_files = [];
  mllib_files = [];
  mlpack_files = [];
  extra_targets = [];
  subdirs = [];
  ml_includes = [];
  r_includes = [];
  q_includes = [];
  extra_args = [];
  defs = [];
}

(********************* utils ********************************************)

let rec post_canonize f =
  if Filename.basename f = Filename.current_dir_name
  then let dir = Filename.dirname f in
    if dir = Filename.current_dir_name then f else post_canonize dir
  else f

(********************* parser *******************************************)

exception Parsing_error of string

let rec parse_string = parser
  | [< '' ' | '\n' | '\t' >] -> ""
  | [< 'c; s >] -> (String.make 1 c)^(parse_string s)
  | [< >] -> ""
and parse_string2 = parser
  | [< ''"' >] -> ""
  | [< 'c; s >] -> (String.make 1 c)^(parse_string2 s)
  | [< >] -> raise (Parsing_error "unterminated string")
and parse_skip_comment = parser
  | [< ''\n'; s >] -> s
  | [< 'c; s >] -> parse_skip_comment s
  | [< >] -> [< >]
and parse_args = parser
  | [< '' ' | '\n' | '\t'; s >] -> parse_args s
  | [< ''#'; s >] -> parse_args (parse_skip_comment s)
  | [< ''"'; str = parse_string2; s >] -> ("" ^ str) :: parse_args s
  | [< 'c; str = parse_string; s >] -> ((String.make 1 c) ^ str) :: (parse_args s)
  | [< >] -> []

let parse f =
  let c = open_in f in
  let res = parse_args (Stream.of_channel c) in
  close_in c;
  res
;;

(* Copy from minisys.ml, since we don't see that file here *)
let exists_dir dir =
  let rec strip_trailing_slash dir =
    let len = String.length dir in
    if len > 0 && (dir.[len-1] = '/' || dir.[len-1] = '\\')
    then strip_trailing_slash (String.sub dir 0 (len-1)) else dir in
  try Sys.is_directory (strip_trailing_slash dir) with Sys_error _ -> false


let process_cmd_line orig_dir proj args =
  let orig_dir = (* avoids turning foo.v in ./foo.v *)
    if orig_dir = "." then "" else orig_dir in
  let error s = Feedback.msg_error (Pp.str (s^".")); exit 1 in
  let mk_path d =
    let p = CUnix.correct_path d orig_dir in
    { path = CUnix.remove_path_dot (post_canonize p);
      canonical_path = CUnix.canonical_path_name p } in
  let rec aux proj = function
  | [] -> proj
  | "-impredicative-set" :: _ ->
    error "Use \"-arg -impredicative-set\" instead of \"-impredicative-set\""
  | "-no-install" :: _ ->
    error "Use \"-install none\" instead of \"-no-install\""
  | "-custom" :: _ ->
    error "Use \"-extra[-phony] target deps command\" instead of \"-custom command deps target\""
  
  | ("-no-opt"|"-byte") :: r -> aux { proj with use_ocamlopt =  false } r
  | ("-full"|"-opt") :: r -> aux { proj with use_ocamlopt =  true } r
  | "-install" :: d :: r ->
    if proj.install_kind <> None then
      Feedback.msg_warning (Pp.str "-install set more than once.");
    let install = match d with
      | "user" -> UserInstall
      | "none" -> NoInstall
      | "global" -> TraditionalInstall
      | _ -> error ("invalid option \""^d^"\" passed to -install") in
    aux { proj with install_kind = Some install } r
  | "-extra" :: target :: dependencies :: command :: r ->
    let tgt = { target; dependencies; phony = false; command } in
    aux { proj with extra_targets = proj.extra_targets @ [tgt] } r
  | "-extra-phony" :: target :: dependencies :: command :: r ->
    let tgt = { target; dependencies; phony = true; command } in
    aux { proj with extra_targets = proj.extra_targets @ [tgt] } r

  | "-Q" :: d :: lp :: r ->
    aux { proj with q_includes = proj.q_includes @ [mk_path d,lp] } r
  | "-I" :: d :: r ->
    aux { proj with ml_includes = proj.ml_includes @ [mk_path d] } r
  | "-R" :: d :: lp :: r ->
    aux { proj with r_includes = proj.r_includes @ [mk_path d,lp] } r

  | "-f" :: file :: r ->
    let file = CUnix.remove_path_dot (CUnix.correct_path file orig_dir) in
    let () = match proj.project_file with
      | None -> ()
      | Some _ -> Feedback.msg_warning (Pp.str
	"Multiple project files are deprecated.")
    in
    let proj = aux { proj with project_file = Some file } (parse file) in
    aux proj r

  | "-o" :: file :: r ->
    if String.contains file '/' then
      error "Output file must be in the current directory";
    if proj.makefile <> None then
      error "Option -o given more than once";
    aux { proj with makefile = Some file } r  
  | v :: "=" :: def :: r ->
    aux { proj with defs = proj.defs @ [v,def] } r
  | "-arg" :: a :: r ->
    aux { proj with extra_args = proj.extra_args @ [a] } r
  | "-bypass-API" :: r ->
    aux { proj with bypass_API = true } r
  | f :: r ->
      let f = CUnix.correct_path f orig_dir in
      let proj =
        if exists_dir f then { proj with subdirs = proj.subdirs @ [f] }
        else match CUnix.get_extension f with
        | ".v" -> { proj with v_files = proj.v_files @ [f] }
        | ".ml" -> { proj with ml_files = proj.ml_files @ [f] }
        | ".ml4" -> { proj with ml4_files = proj.ml4_files @ [f] }
        | ".mli" -> { proj with mli_files = proj.mli_files @ [f] }
        | ".mllib" -> { proj with mllib_files = proj.mllib_files @ [f] }
        | ".mlpack" -> { proj with mlpack_files = proj.mlpack_files @ [f] }
        | _ -> raise (Parsing_error ("Unknown option "^f)) in
      aux proj r
 in
   aux proj args

 (******************************* API ************************************)

let cmdline_args_to_project ~curdir args =
  process_cmd_line curdir (mk_project None None None true false) args

let read_project_file f =
  process_cmd_line (Filename.dirname f)
    (mk_project (Some f) None (Some NoInstall) true false) (parse f)

let rec find_project_file ~from ~projfile_name =
  let fname = Filename.concat from projfile_name in
  if Sys.file_exists fname then Some fname
  else
    let newdir = Filename.dirname from in
    if newdir = "" || newdir = "/" then None
    else find_project_file ~from:newdir ~projfile_name
;;

let coqtop_args_from_project
  { ml_includes; r_includes; q_includes; extra_args }
=
  let map = List.map in
  let args =
    map (fun { canonical_path = i } -> ["-I"; i]) ml_includes @
    map (fun ({ canonical_path = i }, l) -> ["-Q"; i; l]) q_includes @
    map (fun ({ canonical_path = p }, l) -> ["-R"; p; l]) r_includes @
    [extra_args] in
  List.flatten args
;;

(* vim:set ft=ocaml: *)