Skip to content

Instantly share code, notes, and snippets.

@ardabbour
Last active December 3, 2021 13:10
Show Gist options
  • Save ardabbour/e4fa114f15d3301db5de40d3243f3741 to your computer and use it in GitHub Desktop.
Save ardabbour/e4fa114f15d3301db5de40d3243f3741 to your computer and use it in GitHub Desktop.
Merges compile_command.json spread across multiple packages under a catkin workspace for debugging using llvm tools.
#!/usr/bin/env python3
"""
Copyright 2021 Abdul Rahman Dabbour
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
CCCM (Catkin Compile Commands Merger): script to merge the compile commands
generated in catkin workspaces.
"""
import pathlib
from argparse import ArgumentParser
import json
def main(ws_path: str, merged_cc_path: str) -> None:
"""
Merges compile_command.json files in catkin workspaces into one for debugging using llvm tools.
Parameters
----------
ws_path : str
Workspace directory path.
merged_cc_path : str
Merged compile_command.json file path.
"""
ws_path = pathlib.Path(ws_path)
pkgs = [f for f in ws_path.joinpath("build").iterdir() if f.is_dir()]
cc_list = []
for pkg in pkgs:
cc_path = pkg.joinpath("compile_commands.json")
if cc_path.is_file():
with open(cc_path) as f:
cc_list.append(json.load(f))
cc_list = [item for sublist in cc_list for item in sublist]
with open(merged_cc_path, "w") as merged_cc_file:
json.dump(cc_list, merged_cc_file, indent=4, sort_keys=True)
if __name__ == "__main__":
PARSER = ArgumentParser()
PARSER.add_argument(
"--workspace", "-w", help="Workspace directory path", default=".", type=str
)
PARSER.add_argument(
"--output",
"-o",
help="Output file path",
default="compile_commands.json",
type=str,
)
ARGS = PARSER.parse_args()
main(ARGS.workspace, ARGS.output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment