xref: /aosp_15_r20/external/skia/tools/check-headers-self-sufficient (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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
8import multiprocessing
9import os
10import re
11import subprocess
12import sys
13
14'''
15If called with arguments, this script will verify that those headers are
16self-sufficient and idempotent.
17
18Otherwise, test all checked-in headers except for those in the ignore list.
19'''
20
21ignore = 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/sk_app/.*',
59    r'tools/viewer/.*',
60    ]))
61
62
63# test header for self-sufficiency and idempotency.
64# Returns a string containing errors, or None iff there are no errors.
65def compile_header(header):
66    cmd = ['c++', '--std=c++14', '-I.', '-o', '/dev/null', '-c', '-x', 'c++', '-']
67    proc = subprocess.Popen(cmd, stdin=subprocess.PIPE,
68                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
69    proc.stdin.write('#include "%s"\n#include "%s"\n' % (header, header))
70    proc.stdin.close()
71    errors = proc.stdout.read().strip()
72    if proc.wait() != 0 or len(errors) > 0:
73        return '\n\033[7m ERROR: %s \033[0m\n%s\n\n' % (header, errors)
74    return None
75
76
77#     for h in headers:
78#         compile_header(h)
79# ...Except use a multiprocessing pool.
80# Exit at first error.
81def compile_headers(headers):
82    class N: good = True
83    # N.good is a global scoped to this function to make a print_and_exit_if() a closure
84    pool = multiprocessing.Pool()
85    def print_and_exit_if(r):
86        if r is not None:
87            sys.stdout.write(r)
88            N.good = False
89            pool.terminate()
90    for path in headers:
91        assert os.path.exists(path)
92        pool.apply_async(compile_header, args=(path, ), callback=print_and_exit_if)
93    pool.close()
94    pool.join()
95    if N.good:
96        sys.stdout.write('all good :)\n')
97    else:
98        exit(1)
99
100
101def main(argv):
102    skia_dir = os.path.join(os.path.dirname(__file__), os.pardir)
103    if len(argv) > 1:
104        paths = [os.path.relpath(os.path.abspath(arg), skia_dir) for arg in argv[1:]]
105        os.chdir(skia_dir)
106    else:
107        os.chdir(skia_dir)
108        paths = [path for path in subprocess.check_output(['git', 'ls-files']).splitlines()
109                 if path.endswith('.h') and not ignore.match(path)]
110    compile_headers(paths)
111
112
113if __name__ == '__main__':
114    main(sys.argv)
115