I am currently having the same need, linked in my case to the generation of the clangd and kythe index.
my solution was to patch ninja to have a compdb linked only to the target of my interest, you can probably get what you need from there.
here you can find the patch, the command to run is
ninja -C <output-dir> -a chrome_public_apk -t compdb cc cxx objc objcxx >compile_commands.json
alternatively you can try to use
ninjatracing and extract the information
ninjatracing .ninja_log >ninja_log_trace.json
third_party/catapult/tracing/bin/trace2html ninja_log_trace.json
uazo