| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
########################################################################## |
| 4 |
# # |
| 5 |
# This file is part of Frama-C. # |
| 6 |
# # |
| 7 |
# Copyright (C) 2007-2022 # |
| 8 |
# CEA (Commissariat à l'énergie atomique et aux énergies # |
| 9 |
# alternatives) # |
| 10 |
# # |
| 11 |
# you can redistribute it and/or modify it under the terms of the GNU # |
| 12 |
# Lesser General Public License as published by the Free Software # |
| 13 |
# Foundation, version 2.1. # |
| 14 |
# # |
| 15 |
# It is distributed in the hope that it will be useful, # |
| 16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 18 |
# GNU Lesser General Public License for more details. # |
| 19 |
# # |
| 20 |
# See the GNU Lesser General Public License version 2.1 # |
| 21 |
# for more details (enclosed in the file licenses/LGPLv2.1). # |
| 22 |
# # |
| 23 |
########################################################################## |
| 24 |
|
| 25 |
"""This script removes absolute path references in a JSON Compilation Database.""" |
| 26 |
|
| 27 |
# See: http://clang.llvm.org/docs/JSONCompilationDatabase.html |
| 28 |
|
| 29 |
import sys |
| 30 |
import os |
| 31 |
import json |
| 32 |
from pathlib import Path |
| 33 |
|
| 34 |
if len(sys.argv) < 2: |
| 35 |
# no argument, assume default name |
| 36 |
arg = Path("compile_commands.json") |
| 37 |
else: |
| 38 |
arg = Path(sys.argv[1]) |
| 39 |
|
| 40 |
if not arg.exists(): |
| 41 |
print(f"error: file '{arg}' not found") |
| 42 |
sys.exit(f"usage: {sys.argv[0]} [compile_commands.json]") |
| 43 |
|
| 44 |
with open(arg) as data: |
| 45 |
jcdb_json = json.load(data) |
| 46 |
jcdb_dir = arg.parent |
| 47 |
out_json = {} |
| 48 |
|
| 49 |
replacements = set() |
| 50 |
|
| 51 |
nb_diffs = 0 |
| 52 |
for entry in jcdb_json: |
| 53 |
if "file" in entry and os.path.isabs(entry["file"]): |
| 54 |
old_entry = entry["file"] |
| 55 |
entry["file"] = os.path.relpath(entry["file"], jcdb_dir) |
| 56 |
if old_entry != entry["file"]: |
| 57 |
nb_diffs += 1 |
| 58 |
replacements.add(f"{old_entry} -> {entry['file']}") |
| 59 |
else: |
| 60 |
print(f"warning: absolute path could not be normalized: {entry['file']}") |
| 61 |
elif "directory" in entry and os.path.isabs(entry["directory"]): |
| 62 |
old_entry = entry["directory"] |
| 63 |
entry["directory"] = os.path.relpath(entry["directory"], jcdb_dir) |
| 64 |
if old_entry != entry["directory"]: |
| 65 |
nb_diffs += 1 |
| 66 |
replacements.add(f"{old_entry} -> {entry['directory']}") |
| 67 |
else: |
| 68 |
print(f"warning: absolute path could not be normalized: {entry['directory']}") |
| 69 |
|
| 70 |
if nb_diffs == 0: |
| 71 |
print(f"No changes to be applied to {arg}") |
| 72 |
else: |
| 73 |
replacements_str = "\n".join(sorted(replacements)) |
| 74 |
print(f"Replacements to be made:\n{replacements_str}") |
| 75 |
yn = input(f"{nb_diffs} replacements to be applied. Normalize {arg}? [y/N] ") |
| 76 |
if yn.lower() == "y": |
| 77 |
with open(arg, "w", encoding="utf-8") as outfile: |
| 78 |
json.dump(jcdb_json, outfile, ensure_ascii=False, indent=4) |
| 79 |
print(f"Normalization applied to {arg}") |
| 80 |
else: |
| 81 |
print("Exiting without overwriting.") |