Debian Codegraph @

tools/check-headers-self-sufficient

raw
1 #!/usr/bin/env python
2
3 # Copyright 2017 Google Inc.
4 #
5 # Use of this source code is governed by a BSD-style license that can be
6 # found in the LICENSE file.
7
8 import multiprocessing
9 import os
10 import re
11 import subprocess
12 import sys
13
14 '''
15 If called with arguments, this script will verify that those headers are
16 self-sufficient and idempotent.
17
18 Otherwise, test all checked-in headers except for those in the ignore list.
19 '''
20
21 ignore = re.compile('|'.join([
22 r'debugger/QT/.*',
23 r'example/.*',
24 r'experimental/.*',
25 r'include/config/.*',
26 r'include/gpu/mtl/.*',
27 r'include/gpu/vk/.*',
28 r'include/ports/SkFontMgr_android\.h',
29 r'include/ports/SkFontMgr_fontconfig\.h',
30 r'include/ports/SkFontMgr_fuchsia\.h',
31 r'include/ports/SkTypeface_win\.h',
32 r'include/private/.*_impl\.h',
33 r'include/private/.*_neon\.h',
34 r'include/private/.*_sse\.h',
35 r'include/third_party/vulkan/.*',
36 r'include/utils/mac/SkCGUtils\.h',
37 r'include/views/SkOSWindow_.*\.h',
38 r'modules/.*',
39 r'platform_tools/.*',
40 r'src/c/sk_c_from_to\.h',
41 r'src/core/.*Template\.h',
42 r'src/core/SkBitmapProcState_.*\.h',
43 r'src/core/SkLinearBitmapPipeline\.h',
44 r'src/core/SkLinearBitmapPipeline_.*\.h',
45 r'src/gpu/mtl/.*',
46 r'src/gpu/vk/.*',
47 r'src/opts/.*_SSE2\.h',
48 r'src/opts/.*_SSSE3\.h',
49 r'src/opts/.*_neon\.h',
50 r'src/opts/.*_sse\.h',
51 r'src/ports/.*',
52 r'src/utils/.*_win\.h',
53 r'src/utils/win/.*',
54 r'src/views/.*',
55 r'third_party/.*',
56 r'tools/fiddle/.*',
57 r'tools/gpu/vk/.*',
58 r'tools/ganesh/vk/.*',
59 r'tools/sk_app/.*',
60 r'tools/viewer/.*',
61 ]))
62
63
64 # test header for self-sufficiency and idempotency.
65 # Returns a string containing errors, or None iff there are no errors.
66 def compile_header(header):
67 cmd = ['c++', '--std=c++20', '-I.', '-o', '/dev/null', '-c', '-x', 'c++', '-']
68 proc = subprocess.Popen(cmd, stdin=subprocess.PIPE,
69 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
70 proc.stdin.write('#include "%s"\n#include "%s"\n' % (header, header))
71 proc.stdin.close()
72 errors = proc.stdout.read().strip()
73 if proc.wait() != 0 or len(errors) > 0:
74 return '\n\033[7m ERROR: %s \033[0m\n%s\n\n' % (header, errors)
75 return None
76
77
78 # for h in headers:
79 # compile_header(h)
80 # ...Except use a multiprocessing pool.
81 # Exit at first error.
82 def compile_headers(headers):
83 class N: good = True
84 # N.good is a global scoped to this function to make a print_and_exit_if() a closure
85 pool = multiprocessing.Pool()
86 def print_and_exit_if(r):
87 if r is not None:
88 sys.stdout.write(r)
89 N.good = False
90 pool.terminate()
91 for path in headers:
92 assert os.path.exists(path)
93 pool.apply_async(compile_header, args=(path, ), callback=print_and_exit_if)
94 pool.close()
95 pool.join()
96 if N.good:
97 sys.stdout.write('all good :)\n')
98 else:
99 exit(1)
100
101
102 def main(argv):
103 skia_dir = os.path.join(os.path.dirname(__file__), os.pardir)
104 if len(argv) > 1:
105 paths = [os.path.relpath(os.path.abspath(arg), skia_dir) for arg in argv[1:]]
106 os.chdir(skia_dir)
107 else:
108 os.chdir(skia_dir)
109 paths = [path for path in subprocess.check_output(['git', 'ls-files']).splitlines()
110 if path.endswith('.h') and not ignore.match(path)]
111 compile_headers(paths)
112
113
114 if __name__ == '__main__':
115 main(sys.argv)