PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/myocamlbuild.ml

https://github.com/heidi-ann/ocaml-re
OCaml | 500 lines | 366 code | 79 blank | 55 comment | 11 complexity | 920e01f92e664067db68b52e3927ddc5 MD5 | raw file
Possible License(s): LGPL-2.1
  1. (* OASIS_START *)
  2. (* DO NOT EDIT (digest: f4c185bbb74d1359f1015550740c693c) *)
  3. module OASISGettext = struct
  4. (* # 21 "/Users/avsm/.opam/system/build/oasis.0.3.0/src/oasis/OASISGettext.ml" *)
  5. let ns_ str =
  6. str
  7. let s_ str =
  8. str
  9. let f_ (str : ('a, 'b, 'c, 'd) format4) =
  10. str
  11. let fn_ fmt1 fmt2 n =
  12. if n = 1 then
  13. fmt1^^""
  14. else
  15. fmt2^^""
  16. let init =
  17. []
  18. end
  19. module OASISExpr = struct
  20. (* # 21 "/Users/avsm/.opam/system/build/oasis.0.3.0/src/oasis/OASISExpr.ml" *)
  21. open OASISGettext
  22. type test = string
  23. type flag = string
  24. type t =
  25. | EBool of bool
  26. | ENot of t
  27. | EAnd of t * t
  28. | EOr of t * t
  29. | EFlag of flag
  30. | ETest of test * string
  31. type 'a choices = (t * 'a) list
  32. let eval var_get t =
  33. let rec eval' =
  34. function
  35. | EBool b ->
  36. b
  37. | ENot e ->
  38. not (eval' e)
  39. | EAnd (e1, e2) ->
  40. (eval' e1) && (eval' e2)
  41. | EOr (e1, e2) ->
  42. (eval' e1) || (eval' e2)
  43. | EFlag nm ->
  44. let v =
  45. var_get nm
  46. in
  47. assert(v = "true" || v = "false");
  48. (v = "true")
  49. | ETest (nm, vl) ->
  50. let v =
  51. var_get nm
  52. in
  53. (v = vl)
  54. in
  55. eval' t
  56. let choose ?printer ?name var_get lst =
  57. let rec choose_aux =
  58. function
  59. | (cond, vl) :: tl ->
  60. if eval var_get cond then
  61. vl
  62. else
  63. choose_aux tl
  64. | [] ->
  65. let str_lst =
  66. if lst = [] then
  67. s_ "<empty>"
  68. else
  69. String.concat
  70. (s_ ", ")
  71. (List.map
  72. (fun (cond, vl) ->
  73. match printer with
  74. | Some p -> p vl
  75. | None -> s_ "<no printer>")
  76. lst)
  77. in
  78. match name with
  79. | Some nm ->
  80. failwith
  81. (Printf.sprintf
  82. (f_ "No result for the choice list '%s': %s")
  83. nm str_lst)
  84. | None ->
  85. failwith
  86. (Printf.sprintf
  87. (f_ "No result for a choice list: %s")
  88. str_lst)
  89. in
  90. choose_aux (List.rev lst)
  91. end
  92. # 117 "myocamlbuild.ml"
  93. module BaseEnvLight = struct
  94. (* # 21 "/Users/avsm/.opam/system/build/oasis.0.3.0/src/base/BaseEnvLight.ml" *)
  95. module MapString = Map.Make(String)
  96. type t = string MapString.t
  97. let default_filename =
  98. Filename.concat
  99. (Sys.getcwd ())
  100. "setup.data"
  101. let load ?(allow_empty=false) ?(filename=default_filename) () =
  102. if Sys.file_exists filename then
  103. begin
  104. let chn =
  105. open_in_bin filename
  106. in
  107. let st =
  108. Stream.of_channel chn
  109. in
  110. let line =
  111. ref 1
  112. in
  113. let st_line =
  114. Stream.from
  115. (fun _ ->
  116. try
  117. match Stream.next st with
  118. | '\n' -> incr line; Some '\n'
  119. | c -> Some c
  120. with Stream.Failure -> None)
  121. in
  122. let lexer =
  123. Genlex.make_lexer ["="] st_line
  124. in
  125. let rec read_file mp =
  126. match Stream.npeek 3 lexer with
  127. | [Genlex.Ident nm; Genlex.Kwd "="; Genlex.String value] ->
  128. Stream.junk lexer;
  129. Stream.junk lexer;
  130. Stream.junk lexer;
  131. read_file (MapString.add nm value mp)
  132. | [] ->
  133. mp
  134. | _ ->
  135. failwith
  136. (Printf.sprintf
  137. "Malformed data file '%s' line %d"
  138. filename !line)
  139. in
  140. let mp =
  141. read_file MapString.empty
  142. in
  143. close_in chn;
  144. mp
  145. end
  146. else if allow_empty then
  147. begin
  148. MapString.empty
  149. end
  150. else
  151. begin
  152. failwith
  153. (Printf.sprintf
  154. "Unable to load environment, the file '%s' doesn't exist."
  155. filename)
  156. end
  157. let var_get name env =
  158. let rec var_expand str =
  159. let buff =
  160. Buffer.create ((String.length str) * 2)
  161. in
  162. Buffer.add_substitute
  163. buff
  164. (fun var ->
  165. try
  166. var_expand (MapString.find var env)
  167. with Not_found ->
  168. failwith
  169. (Printf.sprintf
  170. "No variable %s defined when trying to expand %S."
  171. var
  172. str))
  173. str;
  174. Buffer.contents buff
  175. in
  176. var_expand (MapString.find name env)
  177. let var_choose lst env =
  178. OASISExpr.choose
  179. (fun nm -> var_get nm env)
  180. lst
  181. end
  182. # 215 "myocamlbuild.ml"
  183. module MyOCamlbuildFindlib = struct
  184. (* # 21 "/Users/avsm/.opam/system/build/oasis.0.3.0/src/plugins/ocamlbuild/MyOCamlbuildFindlib.ml" *)
  185. (** OCamlbuild extension, copied from
  186. * http://brion.inria.fr/gallium/index.php/Using_ocamlfind_with_ocamlbuild
  187. * by N. Pouillard and others
  188. *
  189. * Updated on 2009/02/28
  190. *
  191. * Modified by Sylvain Le Gall
  192. *)
  193. open Ocamlbuild_plugin
  194. (* these functions are not really officially exported *)
  195. let run_and_read =
  196. Ocamlbuild_pack.My_unix.run_and_read
  197. let blank_sep_strings =
  198. Ocamlbuild_pack.Lexers.blank_sep_strings
  199. let split s ch =
  200. let x =
  201. ref []
  202. in
  203. let rec go s =
  204. let pos =
  205. String.index s ch
  206. in
  207. x := (String.before s pos)::!x;
  208. go (String.after s (pos + 1))
  209. in
  210. try
  211. go s
  212. with Not_found -> !x
  213. let split_nl s = split s '\n'
  214. let before_space s =
  215. try
  216. String.before s (String.index s ' ')
  217. with Not_found -> s
  218. (* this lists all supported packages *)
  219. let find_packages () =
  220. List.map before_space (split_nl & run_and_read "ocamlfind list")
  221. (* this is supposed to list available syntaxes, but I don't know how to do it. *)
  222. let find_syntaxes () = ["camlp4o"; "camlp4r"]
  223. (* ocamlfind command *)
  224. let ocamlfind x = S[A"ocamlfind"; x]
  225. let dispatch =
  226. function
  227. | Before_options ->
  228. (* by using Before_options one let command line options have an higher priority *)
  229. (* on the contrary using After_options will guarantee to have the higher priority *)
  230. (* override default commands by ocamlfind ones *)
  231. Options.ocamlc := ocamlfind & A"ocamlc";
  232. Options.ocamlopt := ocamlfind & A"ocamlopt";
  233. Options.ocamldep := ocamlfind & A"ocamldep";
  234. Options.ocamldoc := ocamlfind & A"ocamldoc";
  235. Options.ocamlmktop := ocamlfind & A"ocamlmktop"
  236. | After_rules ->
  237. (* When one link an OCaml library/binary/package, one should use -linkpkg *)
  238. flag ["ocaml"; "link"; "program"] & A"-linkpkg";
  239. (* For each ocamlfind package one inject the -package option when
  240. * compiling, computing dependencies, generating documentation and
  241. * linking. *)
  242. List.iter
  243. begin fun pkg ->
  244. flag ["ocaml"; "compile"; "pkg_"^pkg] & S[A"-package"; A pkg];
  245. flag ["ocaml"; "ocamldep"; "pkg_"^pkg] & S[A"-package"; A pkg];
  246. flag ["ocaml"; "doc"; "pkg_"^pkg] & S[A"-package"; A pkg];
  247. flag ["ocaml"; "link"; "pkg_"^pkg] & S[A"-package"; A pkg];
  248. flag ["ocaml"; "infer_interface"; "pkg_"^pkg] & S[A"-package"; A pkg];
  249. end
  250. (find_packages ());
  251. (* Like -package but for extensions syntax. Morover -syntax is useless
  252. * when linking. *)
  253. List.iter begin fun syntax ->
  254. flag ["ocaml"; "compile"; "syntax_"^syntax] & S[A"-syntax"; A syntax];
  255. flag ["ocaml"; "ocamldep"; "syntax_"^syntax] & S[A"-syntax"; A syntax];
  256. flag ["ocaml"; "doc"; "syntax_"^syntax] & S[A"-syntax"; A syntax];
  257. flag ["ocaml"; "infer_interface"; "syntax_"^syntax] & S[A"-syntax"; A syntax];
  258. end (find_syntaxes ());
  259. (* The default "thread" tag is not compatible with ocamlfind.
  260. * Indeed, the default rules add the "threads.cma" or "threads.cmxa"
  261. * options when using this tag. When using the "-linkpkg" option with
  262. * ocamlfind, this module will then be added twice on the command line.
  263. *
  264. * To solve this, one approach is to add the "-thread" option when using
  265. * the "threads" package using the previous plugin.
  266. *)
  267. flag ["ocaml"; "pkg_threads"; "compile"] (S[A "-thread"]);
  268. flag ["ocaml"; "pkg_threads"; "doc"] (S[A "-I"; A "+threads"]);
  269. flag ["ocaml"; "pkg_threads"; "link"] (S[A "-thread"]);
  270. flag ["ocaml"; "pkg_threads"; "infer_interface"] (S[A "-thread"])
  271. | _ ->
  272. ()
  273. end
  274. module MyOCamlbuildBase = struct
  275. (* # 21 "/Users/avsm/.opam/system/build/oasis.0.3.0/src/plugins/ocamlbuild/MyOCamlbuildBase.ml" *)
  276. (** Base functions for writing myocamlbuild.ml
  277. @author Sylvain Le Gall
  278. *)
  279. open Ocamlbuild_plugin
  280. module OC = Ocamlbuild_pack.Ocaml_compiler
  281. type dir = string
  282. type file = string
  283. type name = string
  284. type tag = string
  285. (* # 56 "/Users/avsm/.opam/system/build/oasis.0.3.0/src/plugins/ocamlbuild/MyOCamlbuildBase.ml" *)
  286. type t =
  287. {
  288. lib_ocaml: (name * dir list) list;
  289. lib_c: (name * dir * file list) list;
  290. flags: (tag list * (spec OASISExpr.choices)) list;
  291. (* Replace the 'dir: include' from _tags by a precise interdepends in
  292. * directory.
  293. *)
  294. includes: (dir * dir list) list;
  295. }
  296. let env_filename =
  297. Pathname.basename
  298. BaseEnvLight.default_filename
  299. let dispatch_combine lst =
  300. fun e ->
  301. List.iter
  302. (fun dispatch -> dispatch e)
  303. lst
  304. let tag_libstubs nm =
  305. "use_lib"^nm^"_stubs"
  306. let nm_libstubs nm =
  307. nm^"_stubs"
  308. let dispatch t e =
  309. let env =
  310. BaseEnvLight.load
  311. ~filename:env_filename
  312. ~allow_empty:true
  313. ()
  314. in
  315. match e with
  316. | Before_options ->
  317. let no_trailing_dot s =
  318. if String.length s >= 1 && s.[0] = '.' then
  319. String.sub s 1 ((String.length s) - 1)
  320. else
  321. s
  322. in
  323. List.iter
  324. (fun (opt, var) ->
  325. try
  326. opt := no_trailing_dot (BaseEnvLight.var_get var env)
  327. with Not_found ->
  328. Printf.eprintf "W: Cannot get variable %s" var)
  329. [
  330. Options.ext_obj, "ext_obj";
  331. Options.ext_lib, "ext_lib";
  332. Options.ext_dll, "ext_dll";
  333. ]
  334. | After_rules ->
  335. (* Declare OCaml libraries *)
  336. List.iter
  337. (function
  338. | nm, [] ->
  339. ocaml_lib nm
  340. | nm, dir :: tl ->
  341. ocaml_lib ~dir:dir (dir^"/"^nm);
  342. List.iter
  343. (fun dir ->
  344. List.iter
  345. (fun str ->
  346. flag ["ocaml"; "use_"^nm; str] (S[A"-I"; P dir]))
  347. ["compile"; "infer_interface"; "doc"])
  348. tl)
  349. t.lib_ocaml;
  350. (* Declare directories dependencies, replace "include" in _tags. *)
  351. List.iter
  352. (fun (dir, include_dirs) ->
  353. Pathname.define_context dir include_dirs)
  354. t.includes;
  355. (* Declare C libraries *)
  356. List.iter
  357. (fun (lib, dir, headers) ->
  358. (* Handle C part of library *)
  359. flag ["link"; "library"; "ocaml"; "byte"; tag_libstubs lib]
  360. (S[A"-dllib"; A("-l"^(nm_libstubs lib)); A"-cclib";
  361. A("-l"^(nm_libstubs lib))]);
  362. flag ["link"; "library"; "ocaml"; "native"; tag_libstubs lib]
  363. (S[A"-cclib"; A("-l"^(nm_libstubs lib))]);
  364. flag ["link"; "program"; "ocaml"; "byte"; tag_libstubs lib]
  365. (S[A"-dllib"; A("dll"^(nm_libstubs lib))]);
  366. (* When ocaml link something that use the C library, then one
  367. need that file to be up to date.
  368. *)
  369. dep ["link"; "ocaml"; "program"; tag_libstubs lib]
  370. [dir/"lib"^(nm_libstubs lib)^"."^(!Options.ext_lib)];
  371. dep ["compile"; "ocaml"; "program"; tag_libstubs lib]
  372. [dir/"lib"^(nm_libstubs lib)^"."^(!Options.ext_lib)];
  373. (* TODO: be more specific about what depends on headers *)
  374. (* Depends on .h files *)
  375. dep ["compile"; "c"]
  376. headers;
  377. (* Setup search path for lib *)
  378. flag ["link"; "ocaml"; "use_"^lib]
  379. (S[A"-I"; P(dir)]);
  380. )
  381. t.lib_c;
  382. (* Add flags *)
  383. List.iter
  384. (fun (tags, cond_specs) ->
  385. let spec =
  386. BaseEnvLight.var_choose cond_specs env
  387. in
  388. flag tags & spec)
  389. t.flags
  390. | _ ->
  391. ()
  392. let dispatch_default t =
  393. dispatch_combine
  394. [
  395. dispatch t;
  396. MyOCamlbuildFindlib.dispatch;
  397. ]
  398. end
  399. # 476 "myocamlbuild.ml"
  400. open Ocamlbuild_plugin;;
  401. let package_default =
  402. {
  403. MyOCamlbuildBase.lib_ocaml =
  404. [
  405. ("re", ["lib"]);
  406. ("re_emacs", ["lib"]);
  407. ("re_str", ["lib"]);
  408. ("re_posix", ["lib"]);
  409. ("re_glob", ["lib"]);
  410. ("re_perl", ["lib"]);
  411. ("re_pcre", ["lib"])
  412. ];
  413. lib_c = [];
  414. flags = [];
  415. includes = [("lib_test", ["lib"])];
  416. }
  417. ;;
  418. let dispatch_default = MyOCamlbuildBase.dispatch_default package_default;;
  419. # 499 "myocamlbuild.ml"
  420. (* OASIS_STOP *)
  421. Ocamlbuild_plugin.dispatch dispatch_default;;