check-includes.py (1732B)
1 #!/usr/bin/env python3 2 3 import os 4 import re 5 import sys 6 7 srcdir = sys.argv[1] 8 base_srcdir = sys.argv[2] 9 builddir = sys.argv[3] 10 11 os.chdir(srcdir) 12 13 14 def removeprefix(s): 15 abs_path = os.path.join(base_srcdir, s) 16 return os.path.relpath(abs_path, srcdir) 17 18 19 HBHEADERS = [os.path.basename(x) for x in os.getenv("HBHEADERS", "").split()] or [ 20 x for x in os.listdir(".") if x.startswith("hb") and x.endswith(".h") 21 ] 22 23 HBSOURCES = [removeprefix(x) for x in os.getenv("HBSOURCES", "").split()] or [ 24 x for x in os.listdir(".") if x.startswith("hb") and x.endswith((".cc", ".hh")) 25 ] 26 27 28 stat = 0 29 30 print( 31 'Checking that public header files #include "hb-common.h" or "hb.h" first (or none)' 32 ) 33 for x in HBHEADERS: 34 if x == "hb.h" or x == "hb-common.h": 35 continue 36 print(f"Checking {x}") 37 with open(x, "r", encoding="utf-8") as f: 38 content = f.read() 39 first = re.findall(r"#.*include.*", content)[0] 40 if first not in ['#include "hb.h"', '#include "hb-common.h"']: 41 print("failure on %s" % x) 42 stat = 1 43 44 print("Checking that source files #include a private header first (or none)") 45 for x in HBSOURCES: 46 print(f"Checking {x}") 47 with open(x, "r", encoding="utf-8") as f: 48 content = f.read() 49 includes = re.findall(r"#.*include.*", content) 50 if includes: 51 if not len(re.findall(r'".*\.hh"', includes[0])): 52 print("failure on %s" % x) 53 stat = 1 54 55 print("Checking that there is no #include <hb-*.h>") 56 for x in HBHEADERS + HBSOURCES: 57 print(f"Checking {x}") 58 with open(x, "r", encoding="utf-8") as f: 59 content = f.read() 60 if re.findall("#.*include.*<.*hb", content): 61 print("failure on %s" % x) 62 stat = 1 63 64 sys.exit(stat)