Coccinelle release 1.0.0-rc13
[bpt/coccinelle.git] / tools / spp.ml
CommitLineData
f537ebc4 1(*
17ba0788
C
2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
f537ebc4
C
5 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
6 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
d6ce1786
C
7 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
8 * This file is part of Coccinelle.
9 *
10 * Coccinelle is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, according to version 2 of the License.
13 *
14 * Coccinelle is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
24 *)
25
26
27# 0 "./spp.ml"
28(*
29 * Copyright 2012, INRIA
30 * Julia Lawall, Gilles Muller
31 * Copyright 2010-2011, INRIA, University of Copenhagen
32 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
33 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
f537ebc4
C
34 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
35 * This file is part of Coccinelle.
36 *
37 * Coccinelle is free software: you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation, according to version 2 of the License.
40 *
41 * Coccinelle is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Public License for more details.
45 *
46 * You should have received a copy of the GNU General Public License
47 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
48 *
49 * The authors reserve the right to distribute this or future versions of
50 * Coccinelle under other licenses.
51 *)
52
53
feec80c3 54# 0 "./spp.ml"
978fd7e5
C
55open Common
56
57exception WrongArguments
58
59(* could do via a List.filter because cpp flags are simple as it's
60 * "-I/usr/include" not ["-I";"/usr/include"] like in ocaml so no
61 * need to look multiple args.
62 *)
63let rec cpp_flags_filter xs =
64 match xs with
65 | [] -> []
66 | x::xs ->
67 (match x with
68 | s when x =~ "-D.*" ->
69 s::cpp_flags_filter xs
70 | s when s =~ "-I.*" ->
71 s::cpp_flags_filter xs
72 | _ ->
73 cpp_flags_filter xs
74 )
75
76let is_compile_command xs =
77 List.mem "-c" xs
78
79let source_file xs =
80 xs +> List.filter (fun s -> s =~ ".*\\.c$")
81
82let rec fix_args args file =
83 match args with
84 [] -> []
85 | hd::tail ->
86 if hd = file then
87 (hd^".i") :: tail
88 else
89 hd::fix_args tail file
90
91let rec get_outputfile args =
92 match args with
93 [] -> ([],"")
94 | hd::tail ->
95 if hd = "-o" then
96 let (hd',tail') = match tail with
97 hd'::tail' -> (hd',tail')
98 | _ -> raise WrongArguments
99 in
100 (tail', hd')
101 else
102 let (ntail, out) = get_outputfile tail in
103 (hd::ntail, out)
104
105let main () =
106 let args = List.tl (Array.to_list Sys.argv) in
107 (*args +> List.iter pr2;*)
108 if is_compile_command args
109 then begin
110 let file = source_file args in
111 (match file with
112 | [file] ->
113 let cpp_flags = cpp_flags_filter args in
114 let cmd2 =
115 (spf "cpp %s %s > %s.i"
116 (Common.join " " cpp_flags)
117 file
118 file)
119 in
120 pr2 cmd2;
121 let ret2 = Sys.command cmd2 in
122 if ret2 > 0 then exit ret2;
123 let sp_args = fix_args args file in
124 let cmd = "spatch " ^ (Common.join " " sp_args) in
125 pr2 cmd;
126 let ret = Sys.command cmd in
127 exit ret
128
129 | [] -> failwith "could not find name of source file"
130 | x::y::xs -> failwith "multiple source files"
131 );
132 end
133 else
134 begin
135 let (nargs, outfile) = get_outputfile args in
136 let cmd2 =
137 (spf "cat %s > %s"
138 (Common.join " " nargs)
139 outfile)
140 in
141 pr2 cmd2;
142 Sys.command cmd2
143 end
144
145let _ = main ()