|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Tweaks a compile_commands.json file: for every build command that has a |
| 4 | +# --sysroot argument, each existing -isystem argument gets a matching extra |
| 5 | +# -isystem argument pointing into the sysroot. This is useful when a |
| 6 | +# compiler resolves -isystem paths relative to --sysroot internally (as |
| 7 | +# part of its built-in search path handling) but a tool consuming |
| 8 | +# compile_commands.json (such as Cppcheck) does not, so the sysroot-relative |
| 9 | +# path needs to be spelled out explicitly. |
| 10 | +# |
| 11 | +# Example: |
| 12 | +# --sysroot /a/b -isystem /opt/x |
| 13 | +# => |
| 14 | +# --sysroot /a/b -isystem /opt/x -isystem /a/b/opt/x |
| 15 | +# |
| 16 | +# Optionally, --isystem-to-i converts -isystem arguments to -I, which can be |
| 17 | +# useful since Cppcheck otherwise treats -isystem headers as "system" |
| 18 | +# headers and skips some checks in them. Paths whose folder name matches one |
| 19 | +# of the --exclude-folder values are left as -isystem. For example, with |
| 20 | +# --isystem-to-i --exclude-folder lib1: |
| 21 | +# -isystem /opt/x -isystem /path/lib1/include |
| 22 | +# => |
| 23 | +# -I /opt/x -isystem /path/lib1/include |
| 24 | +# |
| 25 | +# Optionally, --remove-include-path removes -I arguments whose path contains |
| 26 | +# the given path. For example, with --remove-include-path /path/lib1: |
| 27 | +# -I /opt/x -I /path/lib1/include |
| 28 | +# => |
| 29 | +# -I /opt/x |
| 30 | +# |
| 31 | +# Usage: |
| 32 | +# tools/tweak-compile-commands.py compile_commands.json -o out.json |
| 33 | +# tools/tweak-compile-commands.py -i compile_commands.json |
| 34 | +# tools/tweak-compile-commands.py -i compile_commands.json --isystem-to-i --exclude-folder lib1 |
| 35 | +# tools/tweak-compile-commands.py -i compile_commands.json --remove-include-path /path/lib1 |
| 36 | +# |
| 37 | +# With neither -o nor -i, the result is written to stdout. |
| 38 | + |
| 39 | +import argparse |
| 40 | +import json |
| 41 | +import shlex |
| 42 | +import sys |
| 43 | + |
| 44 | + |
| 45 | +def find_sysroot(tokens): |
| 46 | + for i, tok in enumerate(tokens): |
| 47 | + if tok == '--sysroot' and i + 1 < len(tokens): |
| 48 | + return tokens[i + 1] |
| 49 | + if tok.startswith('--sysroot='): |
| 50 | + return tok[len('--sysroot='):] |
| 51 | + return None |
| 52 | + |
| 53 | + |
| 54 | +def join_sysroot(sysroot, path): |
| 55 | + return sysroot.rstrip('/') + '/' + path.lstrip('/') |
| 56 | + |
| 57 | + |
| 58 | +def add_isystem_sysroot(tokens, sysroot): |
| 59 | + result = [] |
| 60 | + i = 0 |
| 61 | + n = len(tokens) |
| 62 | + while i < n: |
| 63 | + tok = tokens[i] |
| 64 | + if tok == '-isystem' and i + 1 < n: |
| 65 | + path = tokens[i + 1] |
| 66 | + result.append(tok) |
| 67 | + result.append(path) |
| 68 | + result.append('-isystem') |
| 69 | + result.append(join_sysroot(sysroot, path)) |
| 70 | + i += 2 |
| 71 | + continue |
| 72 | + if tok.startswith('-isystem') and tok != '-isystem': |
| 73 | + path = tok[len('-isystem'):] |
| 74 | + result.append(tok) |
| 75 | + result.append('-isystem' + join_sysroot(sysroot, path)) |
| 76 | + i += 1 |
| 77 | + continue |
| 78 | + result.append(tok) |
| 79 | + i += 1 |
| 80 | + return result |
| 81 | + |
| 82 | + |
| 83 | +def is_excluded(path, exclude_folders): |
| 84 | + parts = path.replace('\\', '/').split('/') |
| 85 | + return any(part in exclude_folders for part in parts if part) |
| 86 | + |
| 87 | + |
| 88 | +def convert_isystem_to_i(tokens, exclude_folders): |
| 89 | + result = [] |
| 90 | + i = 0 |
| 91 | + n = len(tokens) |
| 92 | + while i < n: |
| 93 | + tok = tokens[i] |
| 94 | + if tok == '-isystem' and i + 1 < n: |
| 95 | + path = tokens[i + 1] |
| 96 | + result.append(tok if is_excluded(path, exclude_folders) else '-I') |
| 97 | + result.append(path) |
| 98 | + i += 2 |
| 99 | + continue |
| 100 | + if tok.startswith('-isystem') and tok != '-isystem': |
| 101 | + path = tok[len('-isystem'):] |
| 102 | + result.append(tok if is_excluded(path, exclude_folders) else '-I' + path) |
| 103 | + i += 1 |
| 104 | + continue |
| 105 | + result.append(tok) |
| 106 | + i += 1 |
| 107 | + return result |
| 108 | + |
| 109 | + |
| 110 | +def matches_remove_path(path, remove_paths): |
| 111 | + normalized = path.replace('\\', '/') |
| 112 | + return any(remove_path.replace('\\', '/') in normalized for remove_path in remove_paths) |
| 113 | + |
| 114 | + |
| 115 | +def remove_include_paths(tokens, remove_paths): |
| 116 | + result = [] |
| 117 | + i = 0 |
| 118 | + n = len(tokens) |
| 119 | + while i < n: |
| 120 | + tok = tokens[i] |
| 121 | + if tok == '-I' and i + 1 < n: |
| 122 | + path = tokens[i + 1] |
| 123 | + if matches_remove_path(path, remove_paths): |
| 124 | + i += 2 |
| 125 | + continue |
| 126 | + result.append(tok) |
| 127 | + result.append(path) |
| 128 | + i += 2 |
| 129 | + continue |
| 130 | + if tok.startswith('-I') and tok != '-I': |
| 131 | + path = tok[len('-I'):] |
| 132 | + if matches_remove_path(path, remove_paths): |
| 133 | + i += 1 |
| 134 | + continue |
| 135 | + result.append(tok) |
| 136 | + i += 1 |
| 137 | + continue |
| 138 | + result.append(tok) |
| 139 | + i += 1 |
| 140 | + return result |
| 141 | + |
| 142 | + |
| 143 | +def tweak_entry(entry, isystem_to_i, exclude_folders, remove_include_path): |
| 144 | + if 'command' in entry: |
| 145 | + tokens = shlex.split(entry['command']) |
| 146 | + elif 'arguments' in entry: |
| 147 | + tokens = entry['arguments'] |
| 148 | + else: |
| 149 | + return False |
| 150 | + |
| 151 | + changed = False |
| 152 | + |
| 153 | + sysroot = find_sysroot(tokens) |
| 154 | + if sysroot is not None: |
| 155 | + tokens = add_isystem_sysroot(tokens, sysroot) |
| 156 | + changed = True |
| 157 | + |
| 158 | + if isystem_to_i: |
| 159 | + new_tokens = convert_isystem_to_i(tokens, exclude_folders) |
| 160 | + if new_tokens != tokens: |
| 161 | + tokens = new_tokens |
| 162 | + changed = True |
| 163 | + |
| 164 | + if remove_include_path: |
| 165 | + new_tokens = remove_include_paths(tokens, remove_include_path) |
| 166 | + if new_tokens != tokens: |
| 167 | + tokens = new_tokens |
| 168 | + changed = True |
| 169 | + |
| 170 | + if not changed: |
| 171 | + return False |
| 172 | + |
| 173 | + if 'command' in entry: |
| 174 | + entry['command'] = shlex.join(tokens) |
| 175 | + else: |
| 176 | + entry['arguments'] = tokens |
| 177 | + return True |
| 178 | + |
| 179 | + |
| 180 | +def main(): |
| 181 | + parser = argparse.ArgumentParser( |
| 182 | + description='Add sysroot-relative -isystem arguments to build commands in a compile_commands.json file.') |
| 183 | + parser.add_argument('compile_commands', help='path to the compile_commands.json file to read') |
| 184 | + group = parser.add_mutually_exclusive_group() |
| 185 | + group.add_argument('-o', '--output', help='write the result to this file instead of stdout') |
| 186 | + group.add_argument('-i', '--in-place', action='store_true', help='overwrite the input file with the result') |
| 187 | + parser.add_argument('--isystem-to-i', action='store_true', |
| 188 | + help='convert -isystem arguments to -I (except excluded folders)') |
| 189 | + parser.add_argument('--exclude-folder', action='append', default=[], metavar='FOLDER', |
| 190 | + help='folder name to keep as -isystem when using --isystem-to-i; can be given multiple times') |
| 191 | + parser.add_argument('--remove-include-path', action='append', default=[], metavar='PATH', |
| 192 | + help='remove -I arguments whose path contains PATH; can be given multiple times') |
| 193 | + args = parser.parse_args() |
| 194 | + |
| 195 | + with open(args.compile_commands, encoding='utf-8') as f: |
| 196 | + entries = json.load(f) |
| 197 | + |
| 198 | + changed = 0 |
| 199 | + for entry in entries: |
| 200 | + if tweak_entry(entry, args.isystem_to_i, args.exclude_folder, args.remove_include_path): |
| 201 | + changed += 1 |
| 202 | + |
| 203 | + out = json.dumps(entries, indent=2) + '\n' |
| 204 | + |
| 205 | + if args.in_place: |
| 206 | + with open(args.compile_commands, 'w', encoding='utf-8') as f: |
| 207 | + f.write(out) |
| 208 | + elif args.output: |
| 209 | + with open(args.output, 'w', encoding='utf-8') as f: |
| 210 | + f.write(out) |
| 211 | + else: |
| 212 | + sys.stdout.write(out) |
| 213 | + |
| 214 | + print('tweaked {} of {} entries'.format(changed, len(entries)), file=sys.stderr) |
| 215 | + |
| 216 | + |
| 217 | +if __name__ == '__main__': |
| 218 | + main() |
0 commit comments