PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/external/ocamlcryptokit/myocamlbuild.ml

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