Files
meowc/lib/configh.ml
T
milner 0259325381 feat(configh): generate a guarded C header from probe results
Emits the project name and the HAVE_ and SIZEOF_ defines from what the probes
actually found, behind an include guard derived from the output path.
2022-06-05 22:51:52 +00:00

34 lines
1.2 KiB
OCaml

open Types
let guard p =
let base = Ident.cident (Filename.basename (Option.value p.config_header ~default:"config.h")) in
Printf.sprintf "MEOWC_%s_%s" (Ident.cident p.pname) base
let render p =
let b = Buffer.create 1024 in
let g = guard p in
Buffer.add_string b "/* Generated by meowc. Edits will be overwritten. */\n";
Buffer.add_string b (Printf.sprintf "#ifndef %s\n#define %s\n\n" g g);
Buffer.add_string b (Printf.sprintf "#define %s_NAME %S\n\n" (Ident.cident p.pname) p.pname);
let seen = Hashtbl.create 32 in
List.iter
(fun d ->
if not (Hashtbl.mem seen d.dname) then begin
Hashtbl.add seen d.dname ();
match d.dvalue with
| Some v -> Buffer.add_string b (Printf.sprintf "#define %s %s\n" d.dname v)
| None -> Buffer.add_string b (Printf.sprintf "/* #undef %s */\n" d.dname)
end)
p.defines;
Buffer.add_string b (Printf.sprintf "\n#endif /* %s */\n" g);
Buffer.contents b
let emit p =
match p.config_header with
| None -> None
| Some path ->
let text = render p in
let changed = match Fs.read path with exception Sys_error _ -> true | old -> old <> text in
if changed then Fs.write path text;
Some (path, changed)