1 (* Import a C header file.
2 * Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 (* Parse command line arguments. *)
28 let debug = ref false in
29 let save_temps = ref false in
31 printf "bitmatch-import-c %s" Bitmatch.version;
35 let argspec = Arg.align [
36 "--debug", Arg.Set debug,
38 "-save-temps", Arg.Set save_temps,
39 " Save temporary files";
40 "--version", Arg.Unit version,
41 " Display version and exit";
44 let input_file = ref None in
46 match !input_file with
47 | None -> input_file := Some str
49 eprintf "bitmatch-import-c: only give a single input file\n";
54 bitmatch-import-c: Import C structures and constants and
55 generate bitmatching functions from them. Please see the
56 manual page bitmatch-import-c(1) for more information.
60 Arg.parse argspec anon_fun usage_msg;
63 let save_temps = !save_temps in
65 match !input_file with
68 eprintf "bitmatch-import-c: no input file specified\n";
71 (* Grab the file and pass it to the preprocessor, and then read the
72 * C code into memory using CIL.
77 (* XXX Unavoidable tmp exploit here. Fix? *)
79 if not save_temps then (
80 let tmp = Filename.temp_file (Filename.temp_dir_name) ".i" in
81 tmp, fun () -> try Unix.unlink tmp with Unix.Unix_error _ -> ()
83 let tmp = Filename.chop_extension input_file ^ ".i" in
84 tmp, fun () -> (* -save-temps, so do nothing *) ()
88 sprintf "cpp -include bitmatch-import-prefix.h %s > %s"
89 (Filename.quote input_file) (Filename.quote tmp) in
90 if debug then prerr_endline cmd;
91 if Sys.command cmd <> 0 then (
92 eprintf "%s: command failed\n" cmd;
97 (* Why does Frontc.parse return a continuation ...? *)
98 let file = (Frontc.parse tmp) () in
101 (* Find out which structures, #defines, etc. are to be imported.
102 * (cf. the macros in bitmatch-import-prefix.h)
107 | GVar ({vname = vname; vtype = vtype},
108 { init = Some (SingleInit vinit) },
110 when String.starts_with vname "__bitmatch_constant_" ->
111 let vname = String.sub vname 20 (String.length vname - 20) in
113 (* Do constant folding on the initializer and then calculate
114 * its compile-time value.
117 match isInteger (constFold true vinit) with
121 "%a: non-constant initializer: %a" d_loc loc d_exp vinit;
124 Some (vname, vinit, loc)
130 | GType ({tname = tname; ttype = ttype}, loc)
131 when String.starts_with tname "__bitmatch_import_" ->
132 let tname = String.sub tname 18 (String.length tname - 18) in
133 Some (tname, ttype, loc)
137 if !Errormsg.hadErrors then exit 1;
139 (* If debugging, print out the imports. *)
142 fun (vname, vinit, loc) ->
143 Errormsg.log "%a: import %s as constant 0x%LX\n" d_loc loc vname vinit;
146 fun (tname, ttype, loc) ->
147 Errormsg.log "%a: import %s as %a\n" d_loc loc tname d_type ttype;