aboutsummaryrefslogtreecommitdiff
path: root/deprecated/verify_compiler.py
diff options
context:
space:
mode:
authorTiancong Wang <tcwang@google.com>2020-02-13 21:08:49 +0000
committerTiancong Wang <tcwang@google.com>2020-02-13 21:08:49 +0000
commitb75f321fc8978b92ce3db6886ccb966768f0c7a8 (patch)
tree35fa0fbaeaaddd9cc2a126a05eee3527b51e83a8 /deprecated/verify_compiler.py
parentcddd960b0ba2eb62c372c0d3176c75f0bd05d5e8 (diff)
parente617e3393dd24003aa976ece5050bb291070041c (diff)
downloadtoolchain-utils-b75f321fc8978b92ce3db6886ccb966768f0c7a8.tar.gz
Merging 18 commit(s) from Chromium's toolchain-utils am: 0ae38c8498 am: 2a19d36a82 am: e617e3393dr_aml_301500702android-mainline-12.0.0_r55android-mainline-11.0.0_r9android-mainline-11.0.0_r8android-mainline-11.0.0_r7android-mainline-11.0.0_r6android-mainline-11.0.0_r5android-mainline-11.0.0_r45android-mainline-11.0.0_r44android-mainline-11.0.0_r43android-mainline-11.0.0_r42android-mainline-11.0.0_r41android-mainline-11.0.0_r40android-mainline-11.0.0_r4android-mainline-11.0.0_r39android-mainline-11.0.0_r38android-mainline-11.0.0_r37android-mainline-11.0.0_r36android-mainline-11.0.0_r35android-mainline-11.0.0_r34android-mainline-11.0.0_r33android-mainline-11.0.0_r32android-mainline-11.0.0_r31android-mainline-11.0.0_r30android-mainline-11.0.0_r3android-mainline-11.0.0_r29android-mainline-11.0.0_r28android-mainline-11.0.0_r27android-mainline-11.0.0_r26android-mainline-11.0.0_r25android-mainline-11.0.0_r24android-mainline-11.0.0_r23android-mainline-11.0.0_r22android-mainline-11.0.0_r21android-mainline-11.0.0_r20android-mainline-11.0.0_r2android-mainline-11.0.0_r19android-mainline-11.0.0_r18android-mainline-11.0.0_r17android-mainline-11.0.0_r16android-mainline-11.0.0_r15android-mainline-11.0.0_r14android-mainline-11.0.0_r13android-mainline-11.0.0_r12android-mainline-11.0.0_r10android-mainline-11.0.0_r1android-11.0.0_r48android-11.0.0_r47android-11.0.0_r46android-11.0.0_r45android-11.0.0_r44android-11.0.0_r43android-11.0.0_r42android-11.0.0_r41android-11.0.0_r40android-11.0.0_r39android-11.0.0_r38android-11.0.0_r37android-11.0.0_r36android-11.0.0_r35android-11.0.0_r34android-11.0.0_r33android-11.0.0_r32android-11.0.0_r31android-11.0.0_r30android-11.0.0_r29android-11.0.0_r28android-11.0.0_r27android-11.0.0_r26android-11.0.0_r24android-11.0.0_r23android-11.0.0_r22android-11.0.0_r21android-11.0.0_r20android-11.0.0_r19android-11.0.0_r18android-11.0.0_r16android11-qpr3-s1-releaseandroid11-qpr3-releaseandroid11-qpr2-releaseandroid11-qpr1-s2-releaseandroid11-qpr1-s1-releaseandroid11-qpr1-releaseandroid11-qpr1-d-s1-releaseandroid11-qpr1-d-releaseandroid11-qpr1-c-releaseandroid11-mainline-tethering-releaseandroid11-mainline-sparse-2021-jan-releaseandroid11-mainline-sparse-2020-dec-releaseandroid11-mainline-releaseandroid11-mainline-permission-releaseandroid11-mainline-os-statsd-releaseandroid11-mainline-networkstack-releaseandroid11-mainline-media-swcodec-releaseandroid11-mainline-media-releaseandroid11-mainline-extservices-releaseandroid11-mainline-documentsui-releaseandroid11-mainline-conscrypt-releaseandroid11-mainline-cellbroadcast-releaseandroid11-mainline-captiveportallogin-releaseandroid11-devandroid11-d2-releaseandroid11-d1-b-release
Change-Id: I3f25c7ee034b2e20e37ed941b8eae24eec7043eb
Diffstat (limited to 'deprecated/verify_compiler.py')
-rwxr-xr-xdeprecated/verify_compiler.py233
1 files changed, 233 insertions, 0 deletions
diff --git a/deprecated/verify_compiler.py b/deprecated/verify_compiler.py
new file mode 100755
index 00000000..b70c1257
--- /dev/null
+++ b/deprecated/verify_compiler.py
@@ -0,0 +1,233 @@
+#!/usr/bin/env python2
+"""Verify that a ChromeOS sub-tree was built with a particular compiler"""
+
+from __future__ import print_function
+
+import argparse
+import fnmatch
+import os
+import sys
+
+from cros_utils import command_executer
+
+COMPILERS = ['gcc', 'clang']
+
+COMPILER_STRINGS = {'gcc': 'GNU C', 'clang': 'clang version'}
+
+ERR_NO_DEBUG_INFO = 1024
+
+
+def UsageError(parser, message):
+ """Output error message and help/usage info."""
+
+ print('ERROR: %s' % message)
+ parser.print_help()
+ sys.exit(0)
+
+
+def CreateTmpDwarfFile(filename, dwarf_file, cmd_executer):
+ """Create temporary dwarfdump file, to be parsed later."""
+
+ cmd = ('readelf --debug-dump=info %s | grep -A5 DW_AT_producer > %s' %
+ (filename, dwarf_file))
+ retval = cmd_executer.RunCommand(cmd, print_to_console=False)
+ return retval
+
+
+def FindAllFiles(root_dir):
+ """Create a list of all the *.debug and *.dwp files to be checked."""
+
+ file_list = []
+ tmp_list = [
+ os.path.join(dirpath, f)
+ for dirpath, _, files in os.walk(root_dir)
+ for f in fnmatch.filter(files, '*.debug')
+ ]
+ for f in tmp_list:
+ if 'build-id' not in f:
+ file_list.append(f)
+ tmp_list = [
+ os.path.join(dirpath, f)
+ for dirpath, _, files in os.walk(root_dir)
+ for f in fnmatch.filter(files, '*.dwp')
+ ]
+ file_list += tmp_list
+ return file_list
+
+
+def VerifyArgs(compiler, filename, tmp_dir, root_dir, options, parser):
+ """Verify that the option values and combinations are valid."""
+
+ if options.filename and options.all_files:
+ UsageError(parser, 'Cannot use both --file and --all_files.')
+ if options.filename and options.root_dir:
+ UsageError(parser, 'Cannot use both --file and --root_dir.')
+ if options.all_files and not options.root_dir:
+ UsageError(parser, 'Missing --root_dir option.')
+ if options.root_dir and not options.all_files:
+ UsageError(parser, 'Missing --all_files option.')
+ if not options.filename and not options.all_files:
+ UsageError(parser, 'Must specify either --file or --all_files.')
+
+ # Verify that the values specified are valid.
+ if filename:
+ if not os.path.exists(filename):
+ UsageError(parser, 'Cannot find %s' % filename)
+ compiler = options.compiler.lower()
+ if compiler not in COMPILERS:
+ UsageError(parser, '%s is not a valid compiler (gcc or clang).' % compiler)
+ if root_dir and not os.path.exists(root_dir):
+ UsageError(parser, '%s does not exist.' % root_dir)
+ if not os.path.exists(tmp_dir):
+ os.makedirs(tmp_dir)
+
+
+def CheckFile(filename, compiler, tmp_dir, options, cmd_executer):
+ """Verify the information in a single file."""
+
+ print('Checking %s' % filename)
+ # Verify that file contains debug information.
+ cmd = 'readelf -SW %s | grep debug_info' % filename
+ retval = cmd_executer.RunCommand(cmd, print_to_console=False)
+ if retval != 0:
+ print('No debug info in this file. Unable to verify compiler.')
+ # There's no debug info in this file, so skip it.
+ return ERR_NO_DEBUG_INFO
+
+ tmp_name = os.path.basename(filename) + '.dwarf'
+ dwarf_file = os.path.join(tmp_dir, tmp_name)
+ status = CreateTmpDwarfFile(filename, dwarf_file, cmd_executer)
+
+ if status != 0:
+ print('Unable to create dwarf file for %s (status: %d).' % (filename,
+ status))
+ return status
+
+ comp_str = COMPILER_STRINGS[compiler]
+
+ retval = 0
+ with open(dwarf_file, 'r') as in_file:
+ lines = in_file.readlines()
+ looking_for_name = False
+ for line in lines:
+ if 'DW_AT_producer' in line:
+ looking_for_name = False
+ if 'GNU AS' in line:
+ continue
+ if comp_str not in line:
+ looking_for_name = True
+ retval = 1
+ elif looking_for_name:
+ if 'DW_AT_name' in line:
+ words = line.split(':')
+ bad_file = words[-1]
+ print('FAIL: %s was not compiled with %s.' % (bad_file.rstrip(),
+ compiler))
+ looking_for_name = False
+ elif 'DW_TAG_' in line:
+ looking_for_name = False
+
+ if not options.keep_file:
+ os.remove(dwarf_file)
+
+ return retval
+
+
+def Main(argv):
+
+ cmd_executer = command_executer.GetCommandExecuter()
+ parser = argparse.ArgumentParser()
+ parser.add_argument(
+ '--file', dest='filename', help='Name of file to be verified.')
+ parser.add_argument(
+ '--compiler',
+ dest='compiler',
+ required=True,
+ help='Desired compiler (gcc or clang)')
+ parser.add_argument(
+ '--tmp_dir',
+ dest='tmp_dir',
+ help='Directory in which to put dwarf dump file.'
+ ' Defaults to /tmp')
+ parser.add_argument(
+ '--keep_file',
+ dest='keep_file',
+ default=False,
+ action='store_true',
+ help='Do not delete dwarf file when done.')
+ parser.add_argument(
+ '--all_files',
+ dest='all_files',
+ default=False,
+ action='store_true',
+ help='Find and check ALL .debug/.dwp files '
+ 'in subtree. Must be used with --root_dir '
+ '(and NOT with --file).')
+ parser.add_argument(
+ '--root_dir',
+ dest='root_dir',
+ help='Root of subtree in which to look for '
+ 'files. Must be used with --all_files, and'
+ ' not with --file.')
+
+ options = parser.parse_args(argv)
+
+ compiler = options.compiler
+ filename = None
+ if options.filename:
+ filename = os.path.realpath(os.path.expanduser(options.filename))
+ tmp_dir = '/tmp'
+ if options.tmp_dir:
+ tmp_dir = os.path.realpath(os.path.expanduser(options.tmp_dir))
+ root_dir = None
+ if options.root_dir:
+ root_dir = os.path.realpath(os.path.expanduser(options.root_dir))
+
+ VerifyArgs(compiler, filename, tmp_dir, root_dir, options, parser)
+
+ file_list = []
+ if filename:
+ file_list.append(filename)
+ else:
+ file_list = FindAllFiles(root_dir)
+
+ bad_files = []
+ unknown_files = []
+ all_pass = True
+ for f in file_list:
+ result = CheckFile(f, compiler, tmp_dir, options, cmd_executer)
+ if result == ERR_NO_DEBUG_INFO:
+ unknown_files.append(f)
+ all_pass = False
+ elif result != 0:
+ bad_files.append(f)
+ all_pass = False
+
+ if all_pass:
+ print('\n\nSUCCESS: All compilation units were compiled with %s.\n' %
+ compiler)
+ return 0
+ else:
+ if len(bad_files) == 0:
+ print(
+ '\n\n*Mostly* SUCCESS: All files that could be checked were compiled '
+ 'with %s.' % compiler)
+ print(
+ '\n\nUnable to verify the following files (no debug info in them):\n')
+ for f in unknown_files:
+ print(f)
+ else:
+ print('\n\nFAILED: The following files were not compiled with %s:\n' %
+ compiler)
+ for f in bad_files:
+ print(f)
+ if len(unknown_files) > 0:
+ print('\n\nUnable to verify the following files (no debug info in '
+ 'them):\n')
+ for f in unknown_files:
+ print(f)
+ return 1
+
+
+if __name__ == '__main__':
+ sys.exit(Main(sys.argv[1:]))