Debian Codegraph @

ocamlbuild_js_of_ocaml.ml

raw symbols SCIP
1 (* Js_of_ocaml library
2 * http://www.ocsigen.org/js_of_ocaml/
3 * Copyright (C) 2013 Jacques-Pascal Deplaix
4 * Laboratoire PPS - CNRS Université Paris Diderot
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, with linking exception;
9 * either version 2.1 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *)
20
21 open Ocamlbuild_plugin
22 module Pack = Ocamlbuild_pack
23
24 let fold f =
25 let l = ref [] in
26 (try
27 while true do
28 match f () with
29 | None -> ()
30 | Some x -> l := x :: !l
31 done
32 with _ -> ());
33 List.rev !l
34
35 let split_comma = Str.split_delim (Str.regexp " *[, ] *")
36
37 let fold_pflag scan =
38 List.fold_left
39 (fun acc x -> try split_comma (scan x (fun x -> x)) @ acc with _ -> acc)
40 []
41
42 let ocamlfind cmd f =
43 let p = Printf.sprintf in
44 let cmd = List.map (p "\"%s\"") cmd in
45 let cmd = p "ocamlfind query %s" (String.concat " " cmd) in
46 Pack.My_unix.run_and_open cmd (fun ic -> fold (fun () -> f ic))
47
48 let packages_and_predicates prod =
49 let all_pkgs, predicates =
50 let tags = Tags.elements (tags_of_pathname prod) in
51 let pkgs = fold_pflag (fun x -> Scanf.sscanf x "package(%[^)])") tags in
52 let predicates = fold_pflag (fun x -> Scanf.sscanf x "predicate(%[^)])") tags in
53 "js_of_ocaml" :: pkgs, predicates
54 in
55 (* Findlib usually set pkg_* predicate for all selected packages *)
56 (* It doesn't do it with 'query' command, we have to it manually. *)
57 let cmd = "-format" :: "pkg_%p" :: "-r" :: all_pkgs in
58 let predicates_pkgs = ocamlfind cmd (fun ic -> Some (input_line ic)) in
59 all_pkgs, predicates @ predicates_pkgs
60
61 let old_linkopts prod =
62 let all_pkgs, all_predicates = packages_and_predicates prod in
63 let predicates = String.concat "," ("javascript" :: all_predicates) in
64 (* query findlib for linking option *)
65 let cmd = "-o-format" :: "-r" :: "-predicates" :: predicates :: all_pkgs in
66 ocamlfind cmd (fun ic ->
67 let s = String.trim (input_line ic) in
68 if String.length s = 0 then None else Some s)
69
70 let runtime_files prod =
71 let all_pkgs, all_predicates = packages_and_predicates prod in
72 let predicates = String.concat "," all_predicates in
73 (* query findlib for jsoo runtime files *)
74 let cmd =
75 "-format" :: "%+(jsoo_runtime)" :: "-r" :: "-predicates" :: predicates :: all_pkgs
76 in
77 ocamlfind cmd (fun ic ->
78 let s = String.trim (input_line ic) in
79 if String.length s = 0 then None else Some s)
80
81 let init mode =
82 let dep = "%.byte" in
83 let prod = "%.js" in
84 let f env _ =
85 let dep = env dep in
86 let prod = env prod in
87 let link_opts =
88 match mode with
89 | `Default -> List.map (fun x -> P x) (runtime_files prod)
90 | `Legacy -> List.map (fun x -> A x) (old_linkopts prod)
91 in
92 let tags = tags_of_pathname prod ++ "js_of_ocaml" in
93 Cmd (S [ A "js_of_ocaml"; T tags; S link_opts; A "-o"; Px prod; P dep ])
94 in
95 rule "js_of_ocaml: .byte -> .js" ~dep ~prod f;
96 flag [ "js_of_ocaml"; "debug" ] (S [ A "--pretty"; A "--debug-info"; A "--source-map" ]);
97 flag [ "js_of_ocaml"; "pretty" ] (A "--pretty");
98 flag [ "js_of_ocaml"; "debuginfo" ] (A "--debug-info");
99 flag [ "js_of_ocaml"; "noinline" ] (A "--no-inline");
100 flag [ "js_of_ocaml"; "sourcemap" ] (A "--source-map");
101 pflag [ "js_of_ocaml" ] "opt" (fun n -> S [ A "--opt"; A n ]);
102 pflag [ "js_of_ocaml" ] "set" (fun n -> S [ A "--set"; A n ])
103
104 let oasis_support ~executables =
105 let aux x = if List.mem x executables then Pathname.update_extension "js" x else x in
106 Options.targets := List.map aux !Options.targets
107
108 let dispatcher ?(mode = `Default) ?(oasis_executables = []) = function
109 | After_rules -> init mode
110 | After_options -> oasis_support ~executables:oasis_executables
111 | _ -> ()