3 exception CompileFailure
of string
4 exception LinkFailure
of string
6 let ext = if Dynlink.is_native
then ".cmxs" else ".cma"
9 let sysdircmd = !Flag.ocamlfind ^
" printconf stdlib" in
10 match Common.cmd_to_list
sysdircmd with
12 | _
-> raise
(CompileFailure
(sysdircmd ^
" has failed"))
15 let (_
,stat
) = Common.cmd_to_list_and_status cmd
in
17 Unix.WEXITED
0 -> true
20 let check_runtime () =
21 let has_opt = check_cmd (!Flag.ocamlc ^
".opt -version 2>&1 > /dev/null") in
22 let has_c = check_cmd (!Flag.ocamlc ^
" -version 2>&1 > /dev/null") in
25 Flag.ocamlc
:= !Flag.ocamlc ^
".opt";
26 Flag.ocamlopt
:= !Flag.ocamlopt ^
".opt";
27 Flag.ocamldep
:= !Flag.ocamldep ^
".opt";
28 Common.pr2
"Using native version of ocamlc/ocamlopt/ocamldep"
32 Common.pr2
"Using bytecode version of ocamlc/ocamlopt/ocamldep"
34 if Dynlink.is_native
then
36 "No OCaml compiler found! Install either ocamlopt or ocamlopt.opt"
39 "No OCaml compiler found! Install either ocamlc or ocamlc.opt"
41 let init_ocamlcocci _
=
44 let print_match ctr nm kind
=
45 let endlet = "| _ -> failwith \"bad value\" in\n" in
49 "let %s = match List.nth args %d with Coccilib.%s x -> x %s"
52 let string_rep_binding ctr
= function
53 (Some nm
,Ast.MetaPosDecl _
) -> print_match ctr nm
"Pos"
54 | (Some nm
,Ast.MetaListlenDecl _
) -> print_match ctr nm
"Int"
55 | (Some nm
,_
) (* strings for everything else *) ->
56 print_match ctr nm
"Str"
59 let ast_rep_binding ctr
= function
60 (Some nm
,Ast.MetaPosDecl _
) ->
62 (Printf.sprintf
"%s: No AST representation for position variables" nm
)
63 | (Some nm
,Ast.MetaMetaDecl _
) ->
66 "%s: No AST representation for metavariables declared as \"%s\""
68 | (Some nm
,Ast.MetaIdDecl _
) -> print_match ctr nm
"Str"
69 | (Some nm
,Ast.MetaFreshIdDecl _
) -> print_match ctr nm
"Str"
70 | (Some nm
,Ast.MetaTypeDecl _
) -> print_match ctr nm
"Type"
71 | (Some nm
,Ast.MetaInitDecl _
) -> print_match ctr nm
"Init"
72 | (Some nm
,Ast.MetaListlenDecl _
) ->
74 (Printf.sprintf
"%s: No AST representation for listlen variables" nm
)
75 | (Some nm
,Ast.MetaParamDecl _
) -> print_match ctr nm
"Param"
76 | (Some nm
,Ast.MetaParamListDecl _
) -> print_match ctr nm
"ParamList"
77 | (Some nm
,Ast.MetaConstDecl _
) -> print_match ctr nm
"Expr"
78 | (Some nm
,Ast.MetaErrDecl _
) -> failwith
("not supported: "^nm
)
79 | (Some nm
,Ast.MetaExpDecl _
) -> print_match ctr nm
"Expr"
80 | (Some nm
,Ast.MetaIdExpDecl _
) -> print_match ctr nm
"Expr"
81 | (Some nm
,Ast.MetaLocalIdExpDecl _
) -> print_match ctr nm
"Expr"
82 | (Some nm
,Ast.MetaExpListDecl _
) -> print_match ctr nm
"ExprList"
83 | (Some nm
,Ast.MetaDeclDecl _
) -> print_match ctr nm
"Decl"
84 | (Some nm
,Ast.MetaFieldDecl _
) -> print_match ctr nm
"Field"
85 | (Some nm
,Ast.MetaFieldListDecl _
) -> print_match ctr nm
"FieldList"
86 | (Some nm
,Ast.MetaStmDecl _
) -> print_match ctr nm
"Stmt"
87 | (Some nm
,Ast.MetaStmListDecl _
) -> failwith
("not supported: "^nm
)
88 | (Some nm
,Ast.MetaFuncDecl _
) -> print_match ctr nm
"Str"
89 | (Some nm
,Ast.MetaLocalFuncDecl _
) -> print_match ctr nm
"Str"
90 | (Some nm
,Ast.MetaDeclarerDecl _
) -> print_match ctr nm
"Str"
91 | (Some nm
,Ast.MetaIteratorDecl _
) -> print_match ctr nm
"Str"
94 let manage_script_vars script_vars
=
95 let rec loop n
= function
98 (Printf.sprintf
"let %s = List.nth script_args %d in\n" x n
) ^
102 (* ---------------------------------------------------------------------- *)
103 (* Iteration management *)
105 let print_iteration_code o
=
107 String.concat
"\n | "
109 (function x
-> Printf.sprintf
"%s -> \"%s\""
110 (String.capitalize x
) x
)
112 let add_virt_rules_method =
113 match !Iteration.parsed_virtual_rules
with
117 method add_virtual_rule r =
118 let r = match r with %s in
119 virtual_rules <- Common.union_set [r] virtual_rules\n"
121 let add_virt_ids_method =
122 match !Iteration.parsed_virtual_identifiers
with
126 method add_virtual_identifier i v =
127 let i = match i with %s in
129 let v1 = List.assoc i virtual_identifiers in
131 then failwith (\"multiple values specified for \"^i)
133 virtual_identifiers <- (i,v) :: virtual_identifiers"
138 val mutable files = None
139 val mutable files_changed = false
140 val mutable virtual_rules = ([] : string list)
141 val mutable virtual_identifiers = ([] : (string * string) list)
142 method set_files f = files <- Some f
145 Iteration.add_pending_instance (files,virtual_rules,virtual_identifiers)
146 end\n\n" add_virt_rules_method add_virt_ids_method
148 (* ---------------------------------------------------------------------- *)
150 let prepare_rule (name
, metavars
, script_vars
, code
) =
151 let fname = String.concat
"_" (Str.split
(Str.regexp
" ") name
) in
152 (* function header *)
153 let function_header body
=
154 Printf.sprintf
"let %s args script_args =\n %s" fname body
in
156 let build_parameter_list body
=
163 function ((str_nm
,ast_nm
),_
,mv
) ->
164 (* order important; ctr is incremented *)
165 let string_rep = string_rep_binding ctr (str_nm
,mv
) in
166 let ast_rep = ast_rep_binding ctr (ast_nm
,mv
) in
167 ast_rep :: string_rep :: prev
)
169 lets ^
(manage_script_vars script_vars
) ^ body
in
170 (* add to hash table *)
173 "%s\nlet _ = Hashtbl.add Coccilib.fcts \"%s\" %s\n" body name
fname in
174 hash_add (function_header (build_parameter_list code
))
176 let prepare coccifile code
=
181 Ast_cocci.InitialScriptRule
(name
,"ocaml",deps
,code
) ->
185 let init_rules = List.rev
init_rules in
190 Ast_cocci.ScriptRule
(name
,"ocaml",deps
,mv
,script_vars
,code
) ->
191 (name
,mv
,script_vars
,code
) :: prev
192 | Ast_cocci.InitialScriptRule
(name
,"ocaml",deps
,code
) -> prev
193 | Ast_cocci.FinalScriptRule
(name
,"ocaml",deps
,code
) ->
194 (name
,[],[],code
) :: prev
197 let other_rules = List.rev
other_rules in
198 if init_rules = [] && other_rules = []
202 let basefile = Filename.basename
(Filename.chop_extension coccifile
) in
204 String.concat
"_" (Str.split
(Str.regexp
"-") basefile) in
205 let (file
,o
) = Filename.open_temp_file
basefile ".ml" in
206 (* Global initialization *)
207 Printf.fprintf o
"%s\n" (init_ocamlcocci());
208 (* virtual rules and identifiers *)
209 (if !Iteration.parsed_virtual_rules
!= []
211 Printf.fprintf o
"type __virtual_rules__ = %s\n\n"
213 (List.map
String.capitalize
!Iteration.parsed_virtual_rules
)));
214 (if !Iteration.parsed_virtual_identifiers
!= []
216 Printf.fprintf o
"type __virtual_identifiers__ = %s\n\n"
219 (function x
-> Printf.sprintf
"%s" x
)
220 (List.map
String.capitalize
221 !Iteration.parsed_virtual_identifiers
))));
222 print_iteration_code o
;
223 (* Semantic patch specific initialization *)
224 Printf.fprintf o
"%s" (String.concat
"\n\n" init_rules);
225 (* Semantic patch rules and finalizer *)
226 let rule_code = List.map
prepare_rule other_rules in
227 Printf.fprintf o
"%s" (String.concat
"\n\n" rule_code);
233 let filter_dep (accld
, accinc
) dep
=
235 (* Built-in and OCaml defaults are filtered out *)
236 "Arg" | "Arith_status" | "Array" | "ArrayLabels" | "Big_int" | "Bigarray"
237 | "Buffer" | "Callback" | "CamlinternalLazy" | "CamlinternalMod"
239 | "Char" | "Complex" | "Condition" | "Digest" | "Dynlink" | "Event"
241 | "Format" | "Gc" | "Genlex" | "GraphicsX11" | "Hashtbl" | "Int32"
243 | "Lazy" | "Lexing" | "List" | "ListLabels" | "Map" | "Marshal"
244 | "MoreLabels" | "Mutex"
245 | "Nativeint" | "Num" | "Obj" | "Oo" | "Parsing" | "Pervasives"
246 | "Printexc" | "Printf"
247 | "Queue" | "Random" | "Scanf" | "Set" | "Sort" | "Stack" | "StdLabels"
249 | "String" | "StringLabels" | "Sys" | "ThreadUnix" | "Unix" | "UnixLabels"
252 (* Coccilib is filtered out too *)
253 | "Coccilib" | "Common" | "Ast_c" | "Visitor_c" | "Lib_parsing_c"
257 | "Dbm" -> ("dbm"::accld
, accinc
)
258 | "Graphics" -> ("graphics"::accld
, accinc
)
259 | "Thread" -> ("thread"::accld
, accinc
)
260 | "Tk" -> ("tk"::accld
, accinc
)
263 let l = Char.lowercase
(String.get dep
0)in
268 let inclcmd = !Flag.ocamlfind ^
" query "^p
in
269 let dir = List.hd
(Common.cmd_to_list
inclcmd) in
272 let parse_dep mlfile depout
=
273 let re_colon = Str.regexp_string
":" in
274 match Str.split
re_colon depout
with
276 let deplist = Str.split
(Str.regexp_string
" ") dep
in
277 let (libs
, orderdep
) = List.fold_left
filter_dep ([],[]) deplist in
278 if libs
<> [] || orderdep
<> [] then
280 if check_cmd (!Flag.ocamlfind ^
" printconf 2>&1 > /dev/null")
282 let packages = List.rev orderdep
in
283 let inclflags = List.map
get_dir packages in
284 let intlib = List.map
get_dir libs
in
285 let alllibs = List.rev_append
intlib inclflags in
287 List.fold_left
(fun acc
(_,p
) -> acc ^
" "^p
) "" alllibs in
289 String.concat
" " (List.map
(fun (d
,_) -> "-I "^d
) inclflags) in
290 if flags <> "" || libs
<> []
294 ("Extra OCaml packages used in the semantic patch:"^
plist);
295 (alllibs (* , inclflags *), flags)
300 ("ocamlfind did not find "^
301 (if (List.length libs
+ List.length orderdep
) = 1
303 else "one of these packages:")^
plist))
306 (CompileFailure
("ocamlfind not found but "^mlfile^
" uses "^dep
))
312 (CompileFailure
("Wrong dependencies for "^mlfile^
" (Got "^depout^
")"))
314 let dep_flag mlfile
=
315 let depcmd = !Flag.ocamldep ^
" -modules "^mlfile
in
316 match Common.cmd_to_list
depcmd with
317 [dep
] -> parse_dep mlfile dep
319 List.iter
(function x
-> Common.pr2
(x^
"\n")) err
;
320 raise
(CompileFailure
("Failed ocamldep for "^mlfile
))
322 let compile_bytecode_cmd flags mlfile
=
323 let obj = (Filename.chop_extension mlfile
) ^
".cmo" in
324 (obj, Printf.sprintf
"%s -c %s %s %s" !Flag.ocamlc
obj flags mlfile
)
326 let compile_native_cmd flags mlfile
=
327 let obj = (Filename.chop_extension mlfile
) ^
".cmxs" in
329 Printf.sprintf
"%s -shared -o %s %s %s" !Flag.ocamlopt
obj flags mlfile
)
331 let compile mlfile cmd
=
333 match Sys.command cmd
with
335 | _ -> raise
(CompileFailure mlfile
)
337 let rec load_obj obj =
340 with Dynlink.Error e
->
342 Dynlink.Unsafe_file
->
343 Dynlink.allow_unsafe_modules
true;
346 Common.pr2
(Dynlink.error_message e
);
347 raise
(LinkFailure
obj)
350 let link_lib (dir, name) = name ^ ext
353 String.concat " " (List.map link_lib libs)
356 let load_lib (dir, name
) =
357 let obj = dir ^
"/" ^name ^
ext in
358 Common.pr2
("Loading "^
obj ^
"...");
362 List.iter
load_lib libs
364 let load_file mlfile
=
365 let (ldlibs
(* , lklibs *), inc
) = dep_flag mlfile
in
366 (* let linklibs = link_libs lklibs in *)
369 "-thread -g -dtypes -I %s %s -I %s/globals -I %s/ocaml -I %s/parsing_c -I %s/commons "
370 sysdir inc
Config.path
Config.path
Config.path
Config.path
in
373 then compile_native_cmd flags mlfile
374 else compile_bytecode_cmd flags mlfile
in
376 Common.pr2
"Compilation OK!";
378 Common.pr2
"Loading ML code of the SP...";
379 try Dynlink.loadfile
obj
380 with Dynlink.Error e
->
381 Common.pr2
(Dynlink.error_message e
);
382 raise
(LinkFailure
obj)
384 let clean_file mlfile
=
385 let basefile = Filename.chop_extension mlfile
in
387 if Dynlink.is_native
then
397 Sys.remove
(basefile^
".cmi");
398 List.iter
(fun f
-> try Sys.remove f
with _ -> ()) files
401 This function is used in testing.ml.
402 Once the ML file is compiled and loaded,
403 newly available functions are reported here.
408 Common.pr2
("Fct registered: \""^key^
"\"")