Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 1 | #!/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 | |
| 22 | import os |
| 23 | import re |
| 24 | import subprocess |
| 25 | import sys |
| 26 | |
| 27 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 28 | |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 29 | |
| 30 | def 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 Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 40 | sys.stderr.write('.cc files not allowed in includes/ : ' + rel_path + |
| 41 | '\n') |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 42 | 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 Tucci | 7e05fc1 | 2019-08-27 17:29:47 +0200 | [diff] [blame] | 48 | if '// no-include-violation-check' in line: |
| 49 | continue |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 50 | m = re.findall(r'^#include "(.*\.h)"', line) |
| 51 | if not m: |
| 52 | continue |
| 53 | incl = m[0] |
| 54 | |
Primiano Tucci | 355b8c8 | 2019-08-29 08:37:51 +0200 | [diff] [blame] | 55 | # Allow only #include "perfetto/..." or "protos/..." but not "src/". |
| 56 | if not (incl.startswith('perfetto/') or incl.startswith('protos/')): |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 57 | 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 Tucci | 0b651b8 | 2019-06-03 17:16:23 +0100 | [diff] [blame] | 64 | # Ignore lines marked with nogncheck. |
| 65 | if '// nogncheck' in line: |
| 66 | continue |
| 67 | |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 68 | # Public (non-/ext/) headers cannot include /ext/ headers. |
| 69 | if (not rel_path.startswith('include/perfetto/ext/') and |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 70 | incl.startswith('perfetto/ext/')): |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 71 | 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 | |
| 79 | if __name__ == '__main__': |
| 80 | sys.exit(main()) |