3 exception CompileFailure
of string
4 exception LinkFailure
of string
6 let ext = if Dynlink.is_native
then ".cmxs" else ".cma"
7 let has_ocamlfind = ref false
10 match Sys.command cmd
with
14 let check_runtime () =
15 let has_opt = check_cmd (!Flag.ocamlc ^
".opt -version 2>&1 > /dev/null") in
16 let has_c = check_cmd (!Flag.ocamlc ^
" -version 2>&1 > /dev/null") in
17 let has_find = check_cmd (!Flag.ocamlfind ^
" printconf 2>&1 > /dev/null") in
18 has_ocamlfind := has_find;
21 Flag.ocamlc
:= !Flag.ocamlc ^
".opt";
22 Flag.ocamlopt
:= !Flag.ocamlopt ^
".opt";
23 Flag.ocamldep
:= !Flag.ocamldep ^
".opt";
24 Common.pr2
"Using native version of ocamlc/ocamlopt/ocamldep"
28 Common.pr2
"Using bytecode version of ocamlc/ocamlopt/ocamldep"
30 if Dynlink.is_native
then
32 "No OCaml compiler found! Install either ocamlopt or ocamlopt.opt"
35 "No OCaml compiler found! Install either ocamlc or ocamlc.opt"
37 let init_ocamlcocci _
=
40 let print_match ctr nm kind
=
41 let endlet = "| _ -> failwith \"bad value\" in\n" in
45 "let %s = match List.nth args %d with Coccilib.%s x -> x %s"
48 let string_rep_binding ctr
= function
49 (Some nm
,Ast.MetaPosDecl _
) -> print_match ctr nm
"Pos"
50 | (Some nm
,Ast.MetaListlenDecl _
) -> print_match ctr nm
"Int"
51 | (Some nm
,_
) (* strings for everything else *) ->
52 print_match ctr nm
"Str"
55 let ast_rep_binding ctr
= function
56 (Some nm
,Ast.MetaPosDecl _
) ->
58 (Printf.sprintf
"%s: No AST representation for position variables" nm
)
59 | (Some nm
,Ast.MetaIdDecl _
) -> print_match ctr nm
"Str"
60 | (Some nm
,Ast.MetaFreshIdDecl _
) -> print_match ctr nm
"Str"
61 | (Some nm
,Ast.MetaTypeDecl _
) -> print_match ctr nm
"Type"
62 | (Some nm
,Ast.MetaInitDecl _
) -> print_match ctr nm
"Init"
63 | (Some nm
,Ast.MetaListlenDecl _
) ->
65 (Printf.sprintf
"%s: No AST representation for listlen variables" nm
)
66 | (Some nm
,Ast.MetaParamDecl _
) -> print_match ctr nm
"Param"
67 | (Some nm
,Ast.MetaParamListDecl _
) -> print_match ctr nm
"ParamList"
68 | (Some nm
,Ast.MetaConstDecl _
) -> print_match ctr nm
"Expr"
69 | (Some nm
,Ast.MetaErrDecl _
) -> failwith
("not supported: "^nm
)
70 | (Some nm
,Ast.MetaExpDecl _
) -> print_match ctr nm
"Expr"
71 | (Some nm
,Ast.MetaIdExpDecl _
) -> print_match ctr nm
"Expr"
72 | (Some nm
,Ast.MetaLocalIdExpDecl _
) -> print_match ctr nm
"Expr"
73 | (Some nm
,Ast.MetaExpListDecl _
) -> print_match ctr nm
"ExprList"
74 | (Some nm
,Ast.MetaStmDecl _
) -> print_match ctr nm
"Stmt"
75 | (Some nm
,Ast.MetaStmListDecl _
) -> failwith
("not supported: "^nm
)
76 | (Some nm
,Ast.MetaFuncDecl _
) -> print_match ctr nm
"Str"
77 | (Some nm
,Ast.MetaLocalFuncDecl _
) -> print_match ctr nm
"Str"
78 | (Some nm
,Ast.MetaDeclarerDecl _
) -> print_match ctr nm
"Str"
79 | (Some nm
,Ast.MetaIteratorDecl _
) -> print_match ctr nm
"Str"
82 let prepare_rule (name
, metavars
, code
) =
83 let fname = String.concat
"_" (Str.split
(Str.regexp
" ") name
) in
85 let function_header body
=
86 Printf.sprintf
"let %s args =\n %s" fname body
in
88 let build_parameter_list body
=
95 function ((str_nm
,ast_nm
),_
,mv
) ->
96 (* order important; ctr is incremented *)
97 let string_rep = string_rep_binding ctr (str_nm
,mv
) in
98 let ast_rep = ast_rep_binding ctr (ast_nm
,mv
) in
99 ast_rep :: string_rep :: prev
)
102 (* add to hash table *)
105 "%s\nlet _ = Hashtbl.add Coccilib.fcts \"%s\" %s\n" body name
fname in
106 hash_add (function_header (build_parameter_list code
))
108 let prepare coccifile code
=
113 Ast_cocci.InitialScriptRule
(name
,"ocaml",deps
,code
) ->
117 let init_rules = List.rev
init_rules in
122 Ast_cocci.ScriptRule
(name
,"ocaml",deps
,mv
,code
) ->
123 (name
,mv
,code
) :: prev
124 | Ast_cocci.InitialScriptRule
(name
,"ocaml",deps
,code
) -> prev
125 | Ast_cocci.FinalScriptRule
(name
,"ocaml",deps
,code
) ->
126 (name
,[],code
) :: prev
129 let other_rules = List.rev
other_rules in
130 if init_rules = [] && other_rules = []
133 let basefile = Filename.basename
(Filename.chop_extension coccifile
) in
135 String.concat
"_" (Str.split
(Str.regexp
"-") basefile) in
136 let (file
,o
) = Filename.open_temp_file
basefile ".ml" in
137 (* Global initialization *)
138 Printf.fprintf o
"%s" (init_ocamlcocci());
139 (* Semantic patch specific initialization *)
140 Printf.fprintf o
"%s" (String.concat
"\n\n" init_rules);
141 (* Semantic patch rules and finalizer *)
142 let rule_code = List.map
prepare_rule other_rules in
143 Printf.fprintf o
"%s" (String.concat
"\n\n" rule_code);
148 let filter_dep (accld
, accinc
) dep
=
150 (* Built-in and OCaml defaults are filtered out *)
151 "Arg" | "Arith_status" | "Array" | "ArrayLabels" | "Big_int" | "Bigarray"
152 | "Buffer" | "Callback" | "CamlinternalLazy" | "CamlinternalMod"
154 | "Char" | "Complex" | "Condition" | "Digest" | "Dynlink" | "Event"
156 | "Format" | "Gc" | "Genlex" | "GraphicsX11" | "Hashtbl" | "Int32"
158 | "Lazy" | "Lexing" | "List" | "ListLabels" | "Map" | "Marshal"
159 | "MoreLabels" | "Mutex"
160 | "Nativeint" | "Num" | "Obj" | "Oo" | "Parsing" | "Pervasives"
161 | "Printexc" | "Printf"
162 | "Queue" | "Random" | "Scanf" | "Set" | "Sort" | "Stack" | "StdLabels"
164 | "String" | "StringLabels" | "Sys" | "ThreadUnix" | "Unix" | "UnixLabels"
167 (* Coccilib is filtered out too *)
168 | "Coccilib" | "Common" | "Ast_c" | "Visitor_c" | "Lib_parsing_c" ->
171 | "Dbm" -> ("dbm"::accld
, accinc
)
172 | "Graphics" -> ("graphics"::accld
, accinc
)
173 | "Thread" -> ("thread"::accld
, accinc
)
174 | "Tk" -> ("tk"::accld
, accinc
)
177 let l = Char.lowercase
(String.get dep
0)in
182 let inclcmd = !Flag.ocamlfind ^
" query "^p
in
183 let dir = List.hd
(Common.cmd_to_list
inclcmd) in
186 let parse_dep mlfile depout
=
187 let re_colon = Str.regexp_string
":" in
188 match Str.split
re_colon depout
with
190 let deplist = Str.split
(Str.regexp_string
" ") dep
in
191 let (libs
, orderdep
) = List.fold_left
filter_dep ([],[]) deplist in
192 if libs
<> [] || orderdep
<> [] then
195 let packages = List.rev orderdep
in
196 let inclflags = List.map
get_dir packages in
197 let intlib = List.map
get_dir libs
in
198 let alllibs = List.rev_append
intlib inclflags in
200 List.fold_left
(fun acc
(_,p
) -> acc ^
" "^p
) "" alllibs in
202 String.concat
" " (List.map
(fun (d
,_) -> "-I "^d
) inclflags) in
203 if flags <> "" || libs
<> []
207 ("Extra OCaml packages used in the semantic patch:"^
plist);
208 (alllibs (* , inclflags *), flags)
213 ("ocamlfind did not find "^
214 (if (List.length libs
+ List.length orderdep
) = 1
216 else "one of these packages:")^
plist))
219 (CompileFailure
("ocamlfind not found but "^mlfile^
" uses "^dep
))
224 (CompileFailure
("Wrong dependencies for "^mlfile^
" (Got "^depout^
")"))
226 let dep_flag mlfile
=
227 let depcmd = !Flag.ocamldep ^
" -modules "^mlfile
in
228 match Common.cmd_to_list
depcmd with
229 [dep
] -> parse_dep mlfile dep
230 | _ -> raise
(CompileFailure
("Wrong dependencies for "^mlfile
))
232 let compile_bytecode_cmd flags mlfile
=
233 let obj = (Filename.chop_extension mlfile
) ^
".cmo" in
234 (obj, Printf.sprintf
"%s -c %s %s %s" !Flag.ocamlc
obj flags mlfile
)
236 let compile_native_cmd flags mlfile
=
237 let obj = (Filename.chop_extension mlfile
) ^
".cmxs" in
239 Printf.sprintf
"%s -shared -o %s %s %s" !Flag.ocamlopt
obj flags mlfile
)
241 let compile mlfile cmd
=
243 match Sys.command cmd
with
245 | _ -> raise
(CompileFailure mlfile
)
247 let rec load_obj obj =
250 with Dynlink.Error e
->
252 Dynlink.Unsafe_file
->
253 Dynlink.allow_unsafe_modules
true;
256 Common.pr2
(Dynlink.error_message e
);
257 raise
(LinkFailure
obj)
260 let link_lib (dir, name) = name ^ ext
263 String.concat " " (List.map link_lib libs)
266 let load_lib (dir, name
) =
267 let obj = dir ^
"/" ^name ^
ext in
268 Common.pr2
("Loading "^
obj ^
"...");
272 List.iter
load_lib libs
274 let load_file mlfile
=
275 let (ldlibs
(* , lklibs *), inc
) = dep_flag mlfile
in
276 (* let linklibs = link_libs lklibs in *)
279 "-thread -g -dtypes -I /usr/lib/ocaml %s -I %s/ocaml -I %s/parsing_c -I %s/commons "
280 inc
Config.path
Config.path
Config.path
in
283 then compile_native_cmd flags mlfile
284 else compile_bytecode_cmd flags mlfile
in
286 Common.pr2
"Compilation OK!";
288 Common.pr2
"Loading ML code of the SP...";
289 try Dynlink.loadfile
obj
290 with Dynlink.Error e
->
291 Common.pr2
(Dynlink.error_message e
);
292 raise
(LinkFailure
obj)
294 let clean_file mlfile
=
295 let basefile = Filename.chop_extension mlfile
in
297 if Dynlink.is_native
then
307 Sys.remove
(basefile^
".cmi");
308 List.iter
(fun f
-> try Sys.remove f
with _ -> ()) files
311 This function is used in testing.ml.
312 Once the ML file is compiled and loaded,
313 newly available functions are reported here.
318 Common.pr2
("Fct registered: \""^key^
"\"")