forked from BinaryAnalysisPlatform/bap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myocamlbuild.ml.in
114 lines (96 loc) · 3.03 KB
/
myocamlbuild.ml.in
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
let cxx_rule () =
let deps = ["%.hpp"; "%.cpp"; "%.h"] and prod = "%.o" in
let action env _ =
let src = env "%.cpp" and obj = env "%.o" in
let cxx = expand "${cxx} ${cc_optimization} ${cxxflags}" in
let tags = tags_of_pathname src ++ "c++" ++ "compile" in
Cmd (S [Sh cxx; T tags; A "-c"; P src; A "-o"; Px obj]) in
rule "cxx: hpp & cpp & h -> o" ~deps ~prod action
let () = Rules.add cxx_rule
let mark_tags () =
let module Ocamlbuild_compat = struct
let mark_tag_used = ignore
include Ocamlbuild_plugin
end in
let open Ocamlbuild_compat in
List.iter mark_tag_used [
"pkg_core_bench";
"tests";
"pkg_piqirun";
"pkg_core";
]
(* we will pass this manually, util it is fixed *)
let pr_6184_hack = function
| After_rules ->
(* Pass -predicates to ocamldep *)
pflag ["ocaml"; "ocamldep"] "predicate" (fun s -> S [A "-predicates"; A s]);
| _ -> ()
let pass_pp_to_link = function
| After_rules ->
let set acts = pflag acts "pp" (fun s -> S [A "-pp"; A s]) in
set ["link"];
set ["c"; "compile"];
| _ -> ()
let chop_extension file =
try Filename.chop_extension file
with exn -> invalid_arg ("Failed to chop extension from '" ^ file ^ "'")
let unit_of_package pkg =
let open MyOCamlbuildFindlib in
let ocamlfind s =
run_and_read (exec_from_conf "ocamlfind" ^ " " ^ s) in
let archive = ocamlfind ("query -format %a -predicates byte " ^ pkg) in
if String.length archive > 3
then Some (chop_extension archive)
else None
let predicate_used_packages = function
| After_rules ->
MyOCamlbuildFindlib.find_packages () |>
List.iter (fun pkg -> match unit_of_package pkg with
| None -> ()
| Some lib ->
flag ["ocaml"; "link"; "pkg_"^pkg]
(S [A "-predicates"; A ("used_"^lib)]))
| _ -> ()
let predicate_used_libs = function
| After_rules ->
package_default.MyOCamlbuildBase.lib_ocaml |>
List.iter (fun (lib,_,_) ->
flag ["ocaml"; "link"; "use_"^lib]
(S [A "-predicates"; A ("used_"^lib)]))
| _ -> ()
let disable_crossmodule_optimization = function
| After_rules ->
if bool_of_string @@ expand "$development"
then flag ["ocaml"; "compile"] (A "-opaque")
| _ -> ()
let link_shared_native ~cmxa ~dst =
Cmd (S [
!Options.ocamlopt;
A "-shared";
A "-linkall";
A "-I";
A (Filename.dirname cmxa);
P cmxa; A "-o"; Px dst])
let register_cmxs_of_cmxa_rule () =
rule "bap: cmxa -> cmxs"
~prods:["%.cmxs"]
~deps:["%.cmxa"; "%.a"]
(fun env _ ->
link_shared_native
~cmxa:(env "%.cmxa")
~dst:(env "%.cmxs"))
let override_broken_cmxs_rule = function
| Before_rules ->
register_cmxs_of_cmxa_rule ()
| _ -> ()
let () =
mark_tags ();
Ocamlbuild_plugin.dispatch (fun stage ->
Rules.dispatch stage;
dispatch_default stage;
override_broken_cmxs_rule stage;
pr_6184_hack stage;
pass_pp_to_link stage;
predicate_used_libs stage;
disable_crossmodule_optimization stage;
predicate_used_packages stage)