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 22import os 23import re 24import subprocess 25import sys 26 27ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 28 29 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'): 40 sys.stderr.write('.cc files not allowed in includes/ : ' + rel_path + 41 '\n') 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: 48 if '// no-include-violation-check' in line: 49 continue 50 m = re.findall(r'^#include "(.*\.h)"', line) 51 if not m: 52 continue 53 incl = m[0] 54 55 # Allow only #include "perfetto/..." or "protos/..." but not "src/". 56 if not (incl.startswith('perfetto/') or incl.startswith('protos/')): 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 64 # Ignore lines marked with nogncheck. 65 if '// nogncheck' in line: 66 continue 67 68 # Public (non-/ext/) headers cannot include /ext/ headers. 69 if (not rel_path.startswith('include/perfetto/ext/') and 70 incl.startswith('perfetto/ext/')): 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 79if __name__ == '__main__': 80 sys.exit(main()) 81