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/opts/Sk4px_.*\.h',
52    r'src/ports/.*',
53    r'src/utils/.*_win\.h',
54    r'src/utils/win/.*',
55    r'src/views/.*',
56    r'third_party/.*',
57    r'tools/fiddle/.*',
58    r'tools/gpu/vk/.*',
59    r'tools/mdbviz/.*',
60    r'tools/sk_app/.*',
61    r'tools/viewer/.*',
62    ]))
63
64
65# test header for self-sufficiency and idempotency.
66# Returns a string containing errors, or None iff there are no errors.
67def compile_header(header):
68    cmd = ['c++', '--std=c++14', '-I.', '-o', '/dev/null', '-c', '-x', 'c++', '-']
69    proc = subprocess.Popen(cmd, stdin=subprocess.PIPE,
70                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
71    proc.stdin.write('#include "%s"\n#include "%s"\n' % (header, header))
72    proc.stdin.close()
73    errors = proc.stdout.read().strip()
74    if proc.wait() != 0 or len(errors) > 0:
75        return '\n\033[7m ERROR: %s \033[0m\n%s\n\n' % (header, errors)
76    return None
77
78
79#     for h in headers:
80#         compile_header(h)
81# ...Except use a multiprocessing pool.
82# Exit at first error.
83def compile_headers(headers):
84    class N: good = True
85    # N.good is a global scoped to this function to make a print_and_exit_if() a closure
86    pool = multiprocessing.Pool()
87    def print_and_exit_if(r):
88        if r is not None:
89            sys.stdout.write(r)
90            N.good = False
91            pool.terminate()
92    for path in headers:
93        assert os.path.exists(path)
94        pool.apply_async(compile_header, args=(path, ), callback=print_and_exit_if)
95    pool.close()
96    pool.join()
97    if N.good:
98        sys.stdout.write('all good :)\n')
99    else:
100        exit(1)
101
102
103def main(argv):
104    skia_dir = os.path.join(os.path.dirname(__file__), os.pardir)
105    if len(argv) > 1:
106        paths = [os.path.relpath(os.path.abspath(arg), skia_dir) for arg in argv[1:]]
107        os.chdir(skia_dir)
108    else:
109        os.chdir(skia_dir)
110        paths = [path for path in subprocess.check_output(['git', 'ls-files']).splitlines()
111                 if path.endswith('.h') and not ignore.match(path)]
112    compile_headers(paths)
113
114
115if __name__ == '__main__':
116    main(sys.argv)
117
118