summaryrefslogtreecommitdiff
path: root/scripts/tartan-json
blob: 560413a0834f0ea28233b53ddfd3239264508284 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
# coding: utf8
#
# This file is part of Tartan.
# Copyright © 2019 Philip Chimento
#
# Tartan is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Tartan is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Tartan.  If not, see <http://www.gnu.org/licenses/>.

# Use this script to analyze a whole project by passing it the compilation
# database file generated by Meson or CMake. An example with Meson would be:
#
#  $ meson _build
#  $ tartan-json _build/compile_commands.json

import argparse
import json
import os
import shlex
import subprocess
import sys

parser = argparse.ArgumentParser(
    description='''Run the Tartan analyzer on a whole compilation database, in
the compile_commands.json format such as generated by Meson or CMake.''')
parser.add_argument('json', type=argparse.FileType('r'), metavar='FILE',
                    help='path to compile_commands.json')
parser.add_argument('-q', '--quiet', action='store_true',
                    help="don't print progress messages")
parser.add_argument('tartan_options', nargs=argparse.REMAINDER, metavar='...',
                    help='extra options to pass on to Tartan')

args = parser.parse_args()

compile_db = json.load(args.json)
had_error = False

for index, entry in enumerate(compile_db):
    if not args.quiet:
        full_path = os.path.normpath(os.path.join(entry['directory'],
                                                  entry['file']))
        print('[{}/{}] Processing {}'.format(index + 1, len(compile_db),
                                             full_path))

    new_env = dict(os.environ)
    existing_tartan_options = new_env.get('TARTAN_OPTIONS', '')
    new_env['TARTAN_OPTIONS'] = (existing_tartan_options + ' ' +
                                 ' '.join(args.tartan_options))
    # -Wno-unused-command-line-argument is because linker arguments will be
    # ignored and produce a warning with --analyze.
    invocation = (['tartan', '--analyze', '-Wno-unused-command-line-argument']
                  + shlex.split(entry['command']))
    result = subprocess.run(invocation, cwd=entry['directory'], env=new_env)
    if result.returncode != 0:
        had_error = True

sys.exit(1 if had_error else 0)