forked from ocaml-batteries-team/batteries-included
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myocamlbuild.ml
224 lines (192 loc) · 8.14 KB
/
myocamlbuild.ml
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
(* ocamlbuild plugin for building Batteries.
* Copyright (C) 2010 Michael Ekstrand
*
* Portions (hopefully trivial) from build/myocamlbuild.ml and the
* Gallium wiki. *)
open Ocamlbuild_plugin
module Pack = Ocamlbuild_pack
let ocamlfind x = S[A"ocamlfind"; A x]
let packs = "bigarray,num,str"
let doc_intro = "build/intro.text"
let mkconf = "build/mkconf.byte"
let compiler_libs = if Sys.ocaml_version.[0] = '4' then [A"-I"; A"+compiler-libs"] else []
(* removes the trailing newlines in the stdout of s *)
let run_and_read s =
let res = run_and_read s in
String.chomp res
(* Throws exception if bisect not installed - ignore this exception *)
let bisect_dir = try run_and_read "ocamlfind query bisect" with _ -> "."
let bisect_pp = Pathname.concat bisect_dir "bisect_pp.cmo"
let src_bat_ml =
let l = Array.to_list (Pathname.readdir "src") in
let l =
List.filter (fun filename ->
String.is_prefix "bat" filename && String.is_suffix filename ".ml"
) l in
List.map (fun filename -> Pathname.concat "src" filename) l
let _ = dispatch begin function
| Before_options ->
(* Set up to use ocamlfind *)
Options.ocamlc := ocamlfind "ocamlc";
Options.ocamlopt := ocamlfind "ocamlopt";
Options.ocamldep := ocamlfind "ocamldep";
Options.ocamldoc := ocamlfind "ocamldoc";
Options.ocamlmktop := ocamlfind "ocamlmktop"
| Before_rules ->
rule "build shared module"
~prod:"%.cmxs"
~dep:"%.cmxa"
begin fun env build ->
let tags = Tags.union
(tags_of_pathname (env "%.cmxs"))
(tags_of_pathname (env "%.cmxa"))
++ "ocaml" ++ "link" ++ "module"
in
Cmd(S[!Options.ocamlopt; A"-shared"; A"-linkall";
T tags; A"-o"; P(env "%.cmxs"); P(env "%.cmxa")])
end;
rule "process config file"
~prod:"%.ml"
~deps:["%.mlp"; mkconf]
begin fun env build ->
Cmd(S[A"ocamlrun"; P mkconf; P(env "%.mlp"); P(env "%.ml")])
end;
rule "process meta file"
~prod:"META"
~deps:["META.in"; mkconf]
begin fun env build ->
Cmd(S[A"ocamlrun"; P mkconf; P"META.in"; P"META"])
end;
rule "code coverage"
~prod:"coverage/index.html"
~deps:src_bat_ml
begin fun env build ->
List.iter (fun filename ->
tag_file filename ["with_pa_bisect"; "syntax_camlp4o"; "use_bisect"];
) src_bat_ml;
let test_exes = [
"testsuite/main.native";
"qtest/all_tests.native";
] in
List.iter (fun exe -> tag_file exe ["use_bisect"]) test_exes;
List.iter Outcome.ignore_good (build (
List.map (fun exe -> [exe]) test_exes
));
Seq ([
Cmd(S[Sh"rm -f bisect*.out"]);
] @
List.map (fun exe -> Cmd(S[A exe])) test_exes
@ [
Cmd(S[Sh"bisect-report -html coverage bisect*.out"]);
])
end
| After_rules ->
(* use the home-made prefilter preprocessor, which
looks for lines starting with ##Vx##, and delete just the tag or the
whole line depending whether the x matches the ocaml major version
*)
let prefilter_rule ext =
let src = "%." ^ ext ^ "v" in
let tgt = "%." ^ ext in
rule (Printf.sprintf "prefilter: %s --> %s" src tgt)
~prod:tgt
~deps:[src; "build/prefilter.byte"]
(fun env _build ->
Cmd (S [P "build/prefilter.byte"; P (env src); Sh ">"; Px (env tgt)]))
in
prefilter_rule "ml";
prefilter_rule "mli";
(* Rules to create libraries from .mllib instead of .cmo.
We need this because src/batteries.mllib is hidden by src/batteries.ml *)
rule ".mllib --> .cma"
~insert:`top
~prod:"%.cma"
~dep:"%.mllib"
(Pack.Ocaml_compiler.byte_library_link_mllib "%.mllib" "%.cma");
rule ".mllib --> .cmxa"
~insert:`top
~prod:"%.cmxa"
~dep:"%.mllib"
(Pack.Ocaml_compiler.native_library_link_mllib "%.mllib" "%.cmxa");
for n = 1 to 30 do
List.iter (fun symbol ->
flag ["ocaml"; "compile"; Printf.sprintf "warn_%s%d" symbol n]
(S[A"-w"; A (Printf.sprintf "%s%d" symbol n)]);
flag ["ocaml"; "compile"; Printf.sprintf "warn_error_%s%d" symbol n]
(S[A"-warn-error"; A (Printf.sprintf "%s%d" symbol n)])
) ["+"; "-"; "@"]
done;
(* When one links an OCaml program, one should use -linkpkg *)
flag ["ocaml"; "link"; "program"] & A"-linkpkg";
(* Causes build to fail on armel under ocaml 3.10.2
flag ["ocaml"; "native"; "compile"] & A"-annot"; *)
(* A bad idea for future compatibility if OCaml introduces new warnings;
we should use an explicit list of warnings here
flag ["ocaml"; "compile"] & S[A"-warn-error"; A"A"];
*)
flag ["ocaml"; "compile"] & S[A"-package"; A packs];
flag ["ocaml"; "ocamldep"] & S[A"-package"; A packs];
flag ["ocaml"; "doc"] & S[A"-package"; A packs];
flag ["ocaml"; "link"] & S[A"-package"; A packs];
flag ["ocaml"; "infer_interface"] & S[A"-package"; A packs];
List.iter
(fun pkg ->
flag ["ocaml"; "infer_interface"; "pkg_"^pkg] & S[A"-package"; A pkg];
flag ["ocaml"; "ocamldep"; "pkg_"^pkg] & S[A"-package"; A pkg];
flag ["ocaml"; "compile"; "pkg_"^pkg] & S[A"-package"; A pkg];
flag ["ocaml"; "link"; "pkg_"^pkg] & S[A"-package"; A pkg];
())
["oUnit"; "benchmark"];
(* DON'T USE TAG 'thread', USE 'threads'
for compatibility with ocamlbuild *)
flag ["ocaml"; "compile"; "threads"] & A"-thread";
flag ["ocaml"; "link"; "threads"] & A"-thread";
flag ["ocaml"; "doc"; "threads"] & S[A"-I"; A "+threads"];
flag ["ocaml"; "doc"] & S[A"-hide-warnings"; A"-sort"];
flag ["ocaml"; "compile"; "camlp4rf"] &
S[A"-package"; A"camlp4.lib"; A"-pp"; A"camlp4rf"];
flag ["ocaml"; "ocamldep"; "camlp4rf"] &
S[A"-package"; A"camlp4.lib"; A"-pp"; A"camlp4rf"];
flag ["ocaml"; "compile"; "camlp4of"] &
S[A"-package"; A"camlp4.lib"; A"-pp"; A"camlp4of"];
flag ["ocaml"; "ocamldep"; "camlp4of"] &
S[A"-package"; A"camlp4.lib"; A"-pp"; A"camlp4of"];
flag ["ocaml"; "compile"; "syntax_camlp4o"] &
S[A"-syntax"; A"camlp4o"; A"-package"; A"camlp4"];
flag ["ocaml"; "ocamldep"; "syntax_camlp4o"] &
S[A"-syntax"; A"camlp4o"; A"-package"; A"camlp4"];
let flags_pa_bisect =
S[A"-ppopt"; P"str.cma"; A"-ppopt"; P bisect_pp;
A"-ppopt"; A"-disable"; A"-ppopt"; A"b"] in
(* bisect screws up polymorphic recursion without -disable b *)
flag ["ocaml"; "compile"; "with_pa_bisect"] & flags_pa_bisect;
flag ["ocaml"; "ocamldep"; "with_pa_bisect"] & flags_pa_bisect;
ocaml_lib ~extern:true ~dir:bisect_dir "bisect";
ocaml_lib "src/batteries";
ocaml_lib "src/batteriesThread";
flag ["ocaml"; "compile"; "compiler-libs"] & S compiler_libs;
flag ["ocaml"; "link"; "compiler-libs"] & S compiler_libs;
flag ["ocaml"; "ocamldep"; "compiler-libs"] & S compiler_libs;
flag ["ocaml"; "link"; "linkall"] & S[A"-linkall"];
(*
dep ["ocaml"; "link"; "include_tests"; "byte"] &
[Pathname.mk "qtest/test_mods.cma"];
dep ["ocaml"; "link"; "include_tests"; "native"] &
[Pathname.mk "qtest/test_mods.cmxa"]; *)
(* Some .mli files use INCLUDE "foo.mli" to avoid interface duplication;
The problem is that the automatic dependency detector of
ocamlbuild doesn't detect the implicit dependency on the
included .mli, and doesn't copy it into _build before
preprocessing the including file.
Here, we add flags denoting explicit dependencies on the
included .mli. This solution comes from the following
explanation of Xavier Clerc:
http://caml.inria.fr/mantis/print_bug_page.php?bug_id=5162
*)
dep ["ocaml"; "doc"; "extension:html"] & [doc_intro];
flag ["ocaml"; "doc"; "extension:html"] &
(S[A"-t"; A"Batteries user guide";
A"-intro"; P doc_intro;
A"-colorize-code"]);
| _ -> ()
end