prepare-code-coverage-artifact.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. '''Prepare a code coverage artifact.
  4. - Collate raw profiles into one indexed profile.
  5. - Generate html reports for the given binaries.
  6. Caution: The positional arguments to this script must be specified before any
  7. optional arguments, such as --restrict.
  8. '''
  9. import argparse
  10. import glob
  11. import os
  12. import subprocess
  13. import sys
  14. def merge_raw_profiles(host_llvm_profdata, profile_data_dir, preserve_profiles):
  15. print(':: Merging raw profiles...', end='')
  16. sys.stdout.flush()
  17. raw_profiles = glob.glob(os.path.join(profile_data_dir, '*.profraw'))
  18. manifest_path = os.path.join(profile_data_dir, 'profiles.manifest')
  19. profdata_path = os.path.join(profile_data_dir, 'Coverage.profdata')
  20. with open(manifest_path, 'w') as manifest:
  21. manifest.write('\n'.join(raw_profiles))
  22. subprocess.check_call([host_llvm_profdata, 'merge', '-sparse', '-f',
  23. manifest_path, '-o', profdata_path])
  24. if not preserve_profiles:
  25. for raw_profile in raw_profiles:
  26. os.remove(raw_profile)
  27. os.remove(manifest_path)
  28. print('Done!')
  29. return profdata_path
  30. def prepare_html_report(host_llvm_cov, profile, report_dir, binaries,
  31. restricted_dirs):
  32. print(':: Preparing html report for {0}...'.format(binaries), end='')
  33. sys.stdout.flush()
  34. objects = []
  35. for i, binary in enumerate(binaries):
  36. if i == 0:
  37. objects.append(binary)
  38. else:
  39. objects.extend(('-object', binary))
  40. invocation = [host_llvm_cov, 'show'] + objects + ['-format', 'html',
  41. '-instr-profile', profile, '-o', report_dir,
  42. '-show-line-counts-or-regions', '-Xdemangler', 'c++filt',
  43. '-Xdemangler', '-n'] + restricted_dirs
  44. subprocess.check_call(invocation)
  45. with open(os.path.join(report_dir, 'summary.txt'), 'wb') as Summary:
  46. subprocess.check_call([host_llvm_cov, 'report'] + objects +
  47. ['-instr-profile', profile] + restricted_dirs,
  48. stdout=Summary)
  49. print('Done!')
  50. def prepare_html_reports(host_llvm_cov, profdata_path, report_dir, binaries,
  51. unified_report, restricted_dirs):
  52. if unified_report:
  53. prepare_html_report(host_llvm_cov, profdata_path, report_dir, binaries,
  54. restricted_dirs)
  55. else:
  56. for binary in binaries:
  57. binary_report_dir = os.path.join(report_dir,
  58. os.path.basename(binary))
  59. prepare_html_report(host_llvm_cov, profdata_path, binary_report_dir,
  60. [binary], restricted_dirs)
  61. if __name__ == '__main__':
  62. parser = argparse.ArgumentParser(description=__doc__)
  63. parser.add_argument('host_llvm_profdata', help='Path to llvm-profdata')
  64. parser.add_argument('host_llvm_cov', help='Path to llvm-cov')
  65. parser.add_argument('profile_data_dir',
  66. help='Path to the directory containing the raw profiles')
  67. parser.add_argument('report_dir',
  68. help='Path to the output directory for html reports')
  69. parser.add_argument('binaries', metavar='B', type=str, nargs='*',
  70. help='Path to an instrumented binary')
  71. parser.add_argument('--only-merge', action='store_true',
  72. help='Only merge raw profiles together, skip report '
  73. 'generation')
  74. parser.add_argument('--preserve-profiles',
  75. help='Do not delete raw profiles', action='store_true')
  76. parser.add_argument('--use-existing-profdata',
  77. help='Specify an existing indexed profile to use')
  78. parser.add_argument('--unified-report', action='store_true',
  79. help='Emit a unified report for all binaries')
  80. parser.add_argument('--restrict', metavar='R', type=str, nargs='*',
  81. default=[],
  82. help='Restrict the reporting to the given source paths'
  83. ' (must be specified after all other positional arguments)')
  84. args = parser.parse_args()
  85. if args.use_existing_profdata and args.only_merge:
  86. print('--use-existing-profdata and --only-merge are incompatible')
  87. exit(1)
  88. if args.use_existing_profdata:
  89. profdata_path = args.use_existing_profdata
  90. else:
  91. profdata_path = merge_raw_profiles(args.host_llvm_profdata,
  92. args.profile_data_dir,
  93. args.preserve_profiles)
  94. if not len(args.binaries):
  95. print('No binaries specified, no work to do!')
  96. exit(1)
  97. if not args.only_merge:
  98. prepare_html_reports(args.host_llvm_cov, profdata_path, args.report_dir,
  99. args.binaries, args.unified_report, args.restrict)