blob: e308ec70d9848a4b448d8d019556f4e50a4c528a [file] [log] [blame]
Primiano Tucci2c5488f2019-06-01 03:27:28 +01001#!/usr/bin/env python
2# Copyright (C) 2019 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# This tool checks for layering violations in the include/ directory.
17# It checks that:
18# - public includes don't end up depending on non-public /ext/ includes.
19# - public includes don't end up depending on private src/ headers.
20# - We use consistently <angle brackets> for other libraries.
21
22import os
23import re
24import subprocess
25import sys
26
27ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
28
Primiano Tucci2c5488f2019-06-01 03:27:28 +010029
30def main():
31 errors = 0
32 include_root = os.path.join(ROOT_DIR, 'include')
33 for root, _, files in os.walk(include_root):
34 for fname in files:
35 fpath = os.path.join(root, fname)
36 rel_path = os.path.relpath(fpath, ROOT_DIR)
37 if not os.path.isfile(fpath):
38 continue
39 if fpath.endswith('.cc'):
Primiano Tucci834fdc72019-10-04 11:33:44 +010040 sys.stderr.write('.cc files not allowed in includes/ : ' + rel_path +
41 '\n')
Primiano Tucci2c5488f2019-06-01 03:27:28 +010042 errors += 1
43 continue
44 if fpath.endswith('.h'):
45 with open(fpath) as f:
46 lines = f.readlines()
47 for line in lines:
Primiano Tucci7e05fc12019-08-27 17:29:47 +020048 if '// no-include-violation-check' in line:
49 continue
Primiano Tucci2c5488f2019-06-01 03:27:28 +010050 m = re.findall(r'^#include "(.*\.h)"', line)
51 if not m:
52 continue
53 incl = m[0]
54
Primiano Tucci355b8c82019-08-29 08:37:51 +020055 # Allow only #include "perfetto/..." or "protos/..." but not "src/".
56 if not (incl.startswith('perfetto/') or incl.startswith('protos/')):
Primiano Tucci2c5488f2019-06-01 03:27:28 +010057 sys.stderr.write(
58 ('Public header %s is trying to include %s which is outside ' +
59 'of include/. If you are trying to include a library use ' +
60 ' <angle brackets> instead\n') % (rel_path, incl))
61 errors += 1
62 continue
63
Primiano Tucci0b651b82019-06-03 17:16:23 +010064 # Ignore lines marked with nogncheck.
65 if '// nogncheck' in line:
66 continue
67
Primiano Tucci2c5488f2019-06-01 03:27:28 +010068 # Public (non-/ext/) headers cannot include /ext/ headers.
69 if (not rel_path.startswith('include/perfetto/ext/') and
Primiano Tucci834fdc72019-10-04 11:33:44 +010070 incl.startswith('perfetto/ext/')):
Primiano Tucci2c5488f2019-06-01 03:27:28 +010071 sys.stderr.write(('Public header %s cannot include the non-public' +
72 '/ext/ header %s.\n') % (rel_path, incl))
73 errors += 1
74 continue
75
76 return 0 if errors == 0 else 1
77
78
79if __name__ == '__main__':
80 sys.exit(main())