check-header-guards.py (1177B)
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 HBSOURCES = [removeprefix(x) for x in os.getenv("HBSOURCES", "").split()] or [ 23 x for x in os.listdir(".") if x.startswith("hb") and x.endswith((".cc", ".hh")) 24 ] 25 26 27 stat = 0 28 29 for x in HBHEADERS + HBSOURCES: 30 if not x.endswith("h") or x == "hb-gobject-structs.h": 31 continue 32 print(f"Checking {x}") 33 tag = ( 34 x.upper() 35 .replace(".", "_") 36 .replace("-", "_") 37 .replace(os.path.sep, "_") 38 .replace("/", "_") 39 ) 40 with open(x, "r", encoding="utf-8") as f: 41 content = f.read() 42 if len(re.findall(tag + r"\b", content)) != 3: 43 print( 44 "Ouch, header file %s does not have correct preprocessor guards. Expected: %s" 45 % (x, tag) 46 ) 47 stat = 1 48 49 sys.exit(stat)