-
Notifications
You must be signed in to change notification settings - Fork 17
/
myocamlbuild.ml
214 lines (180 loc) · 6.48 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
(* ocamlbuild script of ofuzz *)
open Ocamlbuild_plugin
open Ocamlbuild_pack
(* these functions are not really officially exported *)
let run_and_read = Ocamlbuild_pack.My_unix.run_and_read
let blank_sep_strings = Ocamlbuild_pack.Lexers.blank_sep_strings
let split s ch =
let x = ref [] in
let rec go s =
let pos = String.index s ch in
x := (String.before s pos)::!x;
go (String.after s (pos + 1))
in
try
go s
with Not_found -> !x
let split_nl s = split s '\n'
let before_space s =
try
String.before s (String.index s ' ')
with Not_found -> s
(* this lists all supported packages *)
let find_packages () =
List.map before_space (split_nl & run_and_read "ocamlfind list")
let find_syntaxes () = ["camlp4o"; "camlp4r"]
(* ocamlfind command *)
let ocamlfind x = S[A"ocamlfind"; x]
(* camlidl command *)
let camlidl = S([A"camlidl"; A"-header"])
let getline_from_cmd cmd =
let ch = Unix.open_process_in cmd in
let line = input_line ch in
ignore (Unix.close_process_in ch);
line
(* ocaml path *)
let ocamlpath =
getline_from_cmd "ocamlfind printconf path"
let camlidlpath =
getline_from_cmd "ocamlfind query camlidl"
let get_os_type () =
getline_from_cmd "uname"
let _ = dispatch begin function
| Before_options ->
(* override default commands by ocamlfind ones *)
Options.ocamlc := ocamlfind & A"ocamlc";
Options.ocamlopt := ocamlfind & A"ocamlopt";
Options.ocamldep := ocamlfind & A"ocamldep";
Options.ocamldoc := ocamlfind & A"ocamldoc";
Options.ocamlmktop := ocamlfind & A"ocamlmktop";
(* taggings *)
tag_any
["pkg_str";
"pkg_unix";
"pkg_yojson";
"pkg_camlidl";
"pkg_batteries";
"pkg_mysql";
"pkg_bz2";
"pkg_curses";
];
tag_file "src/libfast_stubs.c" ["stubs"];
tag_file "src/libfuzz_stubs.c" ["stubs"];
tag_file "src/libcomb_stubs.c" ["stubs"];
tag_file "src/libprob_stubs.c" ["stubs"];
tag_file "src/minimizer.ml" ["pkg_threads"];
tag_file "src/minimizer.native" ["pkg_threads"];
| After_rules ->
(* When one link an OCaml library/binary/package, one should use -linkpkg *)
flag ["ocaml"; "link"; "program"] & A"-linkpkg";
(* For each ocamlfind package one inject the -package option when
* compiling, computing dependencies, generating documentation and
* linking. *)
List.iter begin fun pkg ->
flag ["ocaml"; "compile"; "pkg_"^pkg] & S[A"-package"; A pkg];
flag ["ocaml"; "ocamldep"; "pkg_"^pkg] & S[A"-package"; A pkg];
flag ["ocaml"; "doc"; "pkg_"^pkg] & S[A"-package"; A pkg];
flag ["ocaml"; "link"; "pkg_"^pkg] & S[A"-package"; A pkg];
flag ["ocaml"; "infer_interface"; "pkg_"^pkg] & S[A"-package"; A pkg];
end (find_packages ());
(* Like -package but for extensions syntax. Morover -syntax is useless
* when linking. *)
List.iter begin fun syntax ->
flag ["ocaml"; "compile"; "syntax_"^syntax]
& S[A"-syntax"; A syntax];
flag ["ocaml"; "ocamldep"; "syntax_"^syntax]
& S[A"-syntax"; A syntax];
flag ["ocaml"; "doc"; "syntax_"^syntax]
& S[A"-syntax"; A syntax];
flag ["ocaml"; "infer_interface"; "syntax_"^syntax]
& S[A"-syntax"; A syntax];
end (find_syntaxes ());
(* The default "thread" tag is not compatible with ocamlfind.
Indeed, the default rules add the "threads.cma" or "threads.cmxa"
options when using this tag. When using the "-linkpkg" option with
ocamlfind, this module will then be added twice on the command line.
To solve this, one approach is to add the "-thread" option when using
the "threads" package using the previous plugin.
*)
flag ["ocaml"; "pkg_threads"; "compile"] (S[A "-thread"]);
flag ["ocaml"; "pkg_threads"; "link"] (S[A "-thread"]);
flag ["ocaml"; "pkg_threads"; "infer_interface"] (S[A "-thread"]);
flag ["ocaml"; "pkg_threads"; "doc"] (S[A "-thread"]);
(* debugging info *)
flag ["ocaml"; "compile"]
(S[A"-g"]);
flag ["ocaml"; "link"]
(S[A"-g"]);
flag ["ocaml"; "compile"; "native"]
(S[A"-inline";A"10"]);
(* c stub generated from camlidl *)
flag ["c"; "compile"; "stubs"]
(S[A"-ccopt";A("-I"^camlidlpath);]);
flag ["cpp"; "compile"; "stubs"]
(S[A("-I"^ocamlpath^"/ocaml");A("-I"^camlidlpath);]);
(* compile dependencies *)
dep
["ocaml"; "compile"]
[
"libfast_stubs.a";
"libfuzz_stubs.a";
"libcomb_stubs.a";
"libprob_stubs.a";
];
dep ["file:src/ofuzz.native"]
[
"libfast_stubs.a";
"libfuzz_stubs.a";
"libcomb_stubs.a";
"libprob_stubs.a"
];
flag ["ocaml"; "link"; "native"]
(S[
A"-inline"; A"10";
A"-cclib"; A"-L.";
A"-cclib"; A"-lfast_stubs";
A"-cclib"; A"-lfuzz_stubs";
A"-cclib"; A"-lcomb_stubs";
A"-cclib"; A"-lprob_stubs";
A"-cclib"; A"-lboost_system";
A"-cclib"; A"-lboost_filesystem";
A"-cclib"; (if get_os_type() = "Darwin" then A"-lc++" else A"-lstdc++");
A"-cclib"; A"-lgmp";
A"-cclib"; A"-lmpfr";
A"-cclib"; A"-lcamlidl";
]);
(* interested in the dependency graph *)
(* flag ["ocaml"; "doc"; "docdir"]
(S[
A"-dot";
]); *)
(* camlidl rules starts here *)
rule "camlidl"
~prods:["%.mli"; "%.ml"; "%_stubs.c"]
~deps:["%.idl"]
begin fun env _build ->
let idl = env "%.idl" in
let tags = tags_of_pathname idl ++ "compile" ++ "camlidl" in
let cmd = Cmd( S[camlidl; T tags; P idl] ) in
Seq [cmd]
end;
(* define c++ ruels here *)
rule "cpp"
~prods:["%.o";]
~deps:["%.cpp"]
begin fun env _build ->
let file = env "%.cpp" in
let target = env "%.o" in
let tags = tags_of_pathname file ++ "compile" ++ "cpp" in
let cmd = Cmd( S[A"g++"; A"-g"; A"-c"; A"-O3";
A("-I../"^Pathname.dirname file);
A("-I/usr/local/include");
A("-fPIC");
A"-o"; A target; T tags; P file] )
in
Seq [cmd]
end;
flag ["ocamlmklib"; "c"]
(S[A"-L."])
| _ -> ()
end