Release coccinelle-0.2.0rc1
[bpt/coccinelle.git] / tools / spp.ml
CommitLineData
978fd7e5
C
1open Common
2
3exception WrongArguments
4
5(* could do via a List.filter because cpp flags are simple as it's
6 * "-I/usr/include" not ["-I";"/usr/include"] like in ocaml so no
7 * need to look multiple args.
8 *)
9let rec cpp_flags_filter xs =
10 match xs with
11 | [] -> []
12 | x::xs ->
13 (match x with
14 | s when x =~ "-D.*" ->
15 s::cpp_flags_filter xs
16 | s when s =~ "-I.*" ->
17 s::cpp_flags_filter xs
18 | _ ->
19 cpp_flags_filter xs
20 )
21
22let is_compile_command xs =
23 List.mem "-c" xs
24
25let source_file xs =
26 xs +> List.filter (fun s -> s =~ ".*\\.c$")
27
28let rec fix_args args file =
29 match args with
30 [] -> []
31 | hd::tail ->
32 if hd = file then
33 (hd^".i") :: tail
34 else
35 hd::fix_args tail file
36
37let rec get_outputfile args =
38 match args with
39 [] -> ([],"")
40 | hd::tail ->
41 if hd = "-o" then
42 let (hd',tail') = match tail with
43 hd'::tail' -> (hd',tail')
44 | _ -> raise WrongArguments
45 in
46 (tail', hd')
47 else
48 let (ntail, out) = get_outputfile tail in
49 (hd::ntail, out)
50
51let main () =
52 let args = List.tl (Array.to_list Sys.argv) in
53 (*args +> List.iter pr2;*)
54 if is_compile_command args
55 then begin
56 let file = source_file args in
57 (match file with
58 | [file] ->
59 let cpp_flags = cpp_flags_filter args in
60 let cmd2 =
61 (spf "cpp %s %s > %s.i"
62 (Common.join " " cpp_flags)
63 file
64 file)
65 in
66 pr2 cmd2;
67 let ret2 = Sys.command cmd2 in
68 if ret2 > 0 then exit ret2;
69 let sp_args = fix_args args file in
70 let cmd = "spatch " ^ (Common.join " " sp_args) in
71 pr2 cmd;
72 let ret = Sys.command cmd in
73 exit ret
74
75 | [] -> failwith "could not find name of source file"
76 | x::y::xs -> failwith "multiple source files"
77 );
78 end
79 else
80 begin
81 let (nargs, outfile) = get_outputfile args in
82 let cmd2 =
83 (spf "cat %s > %s"
84 (Common.join " " nargs)
85 outfile)
86 in
87 pr2 cmd2;
88 Sys.command cmd2
89 end
90
91let _ = main ()