Generating Clang Compilation Databases on OpenBSD

Clang compilation databases are a really useful feature if you’re a Vim user and have a code linting plugin like coc. On my OpenBSD workstation, I use NeoVim and the aforementioned plugin, so I needed a way to generate the json files that clangd uses. There are some nice tools in the Linux world that do this for you, but (as usual) they don’t support BSDs and frankly I think they’re overengineered.

In the UNIX spirit, here’s a quick script that I wrote for a project to generate the files needed by clangd. This script assumes that all your .c files live in $PROJECT_ROOT/src. When using the clang compiler to generate these files, they spit out a 1:1 match of json files to source files. Thankfully, a little sed solves that problem.

#!/usr/bin/env sh

set -e

CFLAGS="-I./include -I/usr/local/include"

rm -f compile_commands.json

for f in $(find ./src -type f); do
  n=$(echo $f | grep -o '[^/]*$' | sed 's/c$/json/')
  cc -MJ $n $CFLAGS -c $f

rm *.o
sed -e '1s/^/[/' -e '$s/,$/]/' *.json > compile_commands.out
rm *.json
mv compile_commands.out compile_commands.json