From 9537254533bcbb3e0178e8085bfdaa363b70a97d Mon Sep 17 00:00:00 2001 From: sneeker Date: Sun, 5 Jun 2022 22:51:52 +0000 Subject: [PATCH] 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. --- lib/configh.ml | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lib/configh.ml diff --git a/lib/configh.ml b/lib/configh.ml new file mode 100644 index 0000000..a3992e2 --- /dev/null +++ b/lib/configh.ml @@ -0,0 +1,33 @@ +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)