Files
scylladb/scripts/merge-compdb.py
Petr Gusev 0443081b0d build: fix merge-compdb.py for CMake 'output' attributes
compile_commands.json is used by LSPs (e.g. `clangd` in VS Code) for
code navigation. `merge-compdb.py`, called by `configure.py`, merges
these files from Scylla, Seastar, and Abseil. The script filters
entries by checking the output attribute against a given prefix. This
is needed because Scylla’s compile_commands.json is generated by Ninja
and includes all build modes, in case the user specified multiple
ones in the call to configure.py. Seastar and Abseil databases,
generated by CMake, used to omit the output attribute, so filtering
did not apply. Starting with `CMake 3.20+`, output attributes are now
included and do not match the expected prefix. For example, they
could be of the form
`absl/synchronization/CMakeFiles/synchronization.dir/internal/futex_waiter.cc.o`.
This causes relevant entries from Seastar and Abseil to be filtered out.

This patch refactors `merge-compdb.py` to allow specifying an
optional prefix per input file, preserving the intent of applying
the output filtering logic only for ninja-generated
Scylla compdb file.

Closes scylladb/scylladb#24211
2025-05-20 08:43:09 +03:00

51 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 ScyllaDB
#
#
# SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
#
#
# Called by configure.py to merge several compdbs into one, so your
# favourite compatible code indexer can present you with a relevant
# maximally-complete unified index.
#
# Usage: merge-compdb.py [input...]
#
# Each input must be a compdb (i.e., a `compile_commands.json`) file.
# Inputs are specified as file names, optionally with an output prefix,
# using the format: <path-to-compdb>[:<output-prefix>]
#
# The script combines the inputs by:
# - Filtering out entries that are not source files (e.g., non-C/C++ files)
# - Excluding entries not built under the specified output prefix (if provided)
# - Writing the filtered and merged result to stdout
#
# `configure.py` calls this script with:
# - The ninja-generated Scylla compdb, with an output prefix for the current build mode
# - Mode-specific Seastar and Abseil compdbs, without output prefixes
import json
import sys
import os.path
inputs = sys.argv[1:]
indexable_exts = {'.c', '.C', '.cc', '.cxx'}
def read_json(fname: str):
with open(fname) as f:
return json.load(f)
output_entries = [e
for f, _, prefix in (i.partition(':') for i in inputs)
for e in read_json(f)
if os.path.splitext(e['file'])[1] in indexable_exts
and (not prefix or e['output'].startswith(prefix))
]
json.dump(output_entries, sys.stdout, indent=True)