aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/check-headers-self-sufficient
blob: 0513aeff47455257ac32e6cf421eb1963fcf7acf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python

# Copyright 2017 Google Inc.
#
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import fnmatch
import multiprocessing
import os
import subprocess
import sys


public_header_args = [
    '-Iinclude/core',
    '-Iinclude/config',
    '-Iinclude/android',
    '-Iinclude/codec',
    '-Iinclude/effects',
    '-Iinclude/gpu',
    '-Iinclude/gpu/gl',
    '-Iinclude/pathops',
    '-Iinclude/ports',
    '-Iinclude/private',
    '-Iinclude/svg',
    '-Iinclude/utils',
    '-Iinclude/utils/mac',
    '-Iinclude/views',
]

all_header_args = [
    '-Iinclude/core',
    '-Iinclude/config',
    '-Iinclude/android',
    '-Iinclude/c',
    '-Iinclude/codec',
    '-Iinclude/effects',
    '-Iinclude/gpu',
    '-Iinclude/gpu/gl',
    '-Iinclude/pathops',
    '-Iinclude/ports',
    '-Iinclude/private',
    '-Iinclude/svg',
    '-Iinclude/utils',
    '-Iinclude/utils/mac',
    '-Iinclude/views',
    '-Isrc/codec',
    '-Isrc/core',
    '-Isrc/effects',
    '-Isrc/effects/gradients',
    '-Isrc/fonts',
    '-Isrc/gpu',
    '-Isrc/image',
    '-Isrc/images',
    '-Isrc/lazy',
    '-Isrc/opts',
    '-Isrc/pathops',
    '-Isrc/ports',
    '-Isrc/sfnt',
    '-Isrc/sksl',
    '-Isrc/utils',
    '-Isrc/utils/win',
    '-Isrc/xml',
    '-Igm',
    '-Itests',
    '-Itools',
    '-Itools/debugger',
    '-Itools/flags',
    '-Itools/gpu',
    '-Itools/timer',
    '-Ithird_party/etc1',
    '-Ithird_party/externals/jsoncpp/include',
    '-Ithird_party/externals/libjpeg-turbo',
    '-Ithird_party/externals/sfntly/cpp/src',
    '-Ithird_party/externals/zlib',
    '-Ithird_party/gif',
]

ignore = [
    '*/lex.*.h',
    '*/osmesa_wrapper.h',
    'debugger/QT/*',
    'example/*',
    'experimental/*',
    'include/config/*',
    'include/core/SkPostConfig.h',
    'include/gpu/vk/*',
    'include/ports/SkFontMgr_android.h',
    'include/ports/SkFontMgr_fontconfig.h',
    'include/ports/SkTypeface_win.h',
    'include/private/*_impl.h',
    'include/utils/mac/SkCGUtils.h',
    'include/views/SkOSWindow_*.h',
    'src/c/sk_c_from_to.h',
    'src/core/*Template.h',
    'src/core/SkBitmapProcState_*.h',
    'src/core/SkFDot6Constants.h',
    'src/core/SkLinearBitmapPipeline.h',
    'src/core/SkLinearBitmapPipeline_*.h',
    'src/core/SkUnPreMultiplyPriv.h',
    'src/gpu/vk/*.h',
    'src/opts/*_SSE2.h',
    'src/opts/*_SSSE3.h',
    'src/opts/*_neon.h',
    'src/opts/*_sse.h',
    'src/opts/Sk4px_*.h',
    'src/ports/*',
    'src/utils/*_win.h',
    'src/utils/win/*',
    'src/views/*',
    'third_party/*',
    'tools/fiddle/*',
    'tools/viewer/*',
]

# test header for self-sufficiency and idempotency.
# Returns a string containing errors, or None iff there are no errors.
def compile_header(header):
    args = ([]                 if fnmatch.fnmatch(header, 'include/c/*') else
            public_header_args if fnmatch.fnmatch(header, 'include/*')   else
            all_header_args)
    cmd = ['c++', '--std=c++11'] + args + [ '-o', '/dev/null', '-c', '-x', 'c++', '-']
    proc = subprocess.Popen(cmd, stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    proc.stdin.write('#include "%s"\n#include "%s"\n' % (header, header))
    proc.stdin.close()
    errors = proc.stdout.read().strip()
    if proc.wait() != 0 or len(errors) > 0:
        return '\n\033[7m ERROR: %s \033[0m\n%s\n\n' % (header, errors)
    return None

def main():
    class N: good = True
    # N.good is a global scoped to main() to make a print_and_exit_if() a closure
    pool = multiprocessing.Pool()
    def print_and_exit_if(r):
        if r is not None:
            sys.stdout.write(r)
            N.good = False
            pool.terminate()

    os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
    for path in subprocess.check_output(['git', 'ls-files']).splitlines():
        if path.endswith('.h') and not any(fnmatch.fnmatch(path, pattern) for pattern in ignore):
            pool.apply_async(compile_header, args=(path, ), callback=print_and_exit_if)
    pool.close()
    pool.join()
    if N.good:
        sys.stdout.write('all good :)\n')
    else:
        exit(1)

if __name__ == '__main__':
    main()