summaryrefslogtreecommitdiff
path: root/scripts/tartan-json
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/tartan-json')
-rwxr-xr-xscripts/tartan-json67
1 files changed, 67 insertions, 0 deletions
diff --git a/scripts/tartan-json b/scripts/tartan-json
new file mode 100755
index 0000000..560413a
--- /dev/null
+++ b/scripts/tartan-json
@@ -0,0 +1,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)