From d4e210f33ccd39824738e904063c43734b2e158b Mon Sep 17 00:00:00 2001 From: sneeker Date: Fri, 17 Jun 2022 19:15:48 +0000 Subject: [PATCH] feat(compdb): write compile_commands.json for clangd One entry per translation unit with directory, arguments and file, taken from the compile actions already in the graph. --- lib/compdb.ml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 lib/compdb.ml diff --git a/lib/compdb.ml b/lib/compdb.ml new file mode 100644 index 0000000..d438f5b --- /dev/null +++ b/lib/compdb.ml @@ -0,0 +1,19 @@ +let render (b : Build.t) = + let dir = Sys.getcwd () in + let entries = + Array.to_list b.g.Graph.nodes + |> List.filter (fun (n : Graph.node) -> n.tag = "cc" || n.tag = "c++") + |> List.map (fun (n : Graph.node) -> + Printf.sprintf + " {\n \"directory\": %s,\n \"file\": %s,\n \"output\": %s,\n \"arguments\": %s\n }" + (Json.str dir) (Json.str n.label) + (Json.str (List.hd n.outs)) + (Json.list Json.str (Array.to_list n.cmd))) + in + "[\n" ^ String.concat ",\n" entries ^ "\n]\n" + +let write b path = + Fs.write path (render b); + List.length + (List.filter (fun (n : Graph.node) -> n.Graph.tag = "cc" || n.Graph.tag = "c++") + (Array.to_list b.Build.g.Graph.nodes))