| 1 |
#!/usr/bin/env python3 |
| 2 |
import subprocess |
| 3 |
import sys |
| 4 |
from typing import Tuple |
| 5 |
|
| 6 |
ALLOWLIST: Tuple[str, ...] = ( |
| 7 |
# system headers |
| 8 |
"pthread.h", |
| 9 |
"stdarg.h", |
| 10 |
"stdbool.h", |
| 11 |
"stddef.h", |
| 12 |
"stdint.h", |
| 13 |
"time.h", # time_t used in Messenger.h TODO(iphydf): maybe don't? |
| 14 |
# toxav stuff, maybe not worth abstracting away |
| 15 |
"opus.h", |
| 16 |
"vpx/vp8cx.h", |
| 17 |
"vpx/vp8dx.h", |
| 18 |
"vpx/vpx_decoder.h", |
| 19 |
"vpx/vpx_encoder.h", |
| 20 |
"vpx/vpx_image.h", |
| 21 |
) |
| 22 |
|
| 23 |
out = (subprocess.run( |
| 24 |
[ |
| 25 |
"grep", "-R", "^#include <.*>", "other", "toxav", "toxcore", |
| 26 |
"toxencryptsave" |
| 27 |
], |
| 28 |
check=True, |
| 29 |
capture_output=True, |
| 30 |
).stdout.decode("utf-8").rstrip()) |
| 31 |
|
| 32 |
errors = 0 |
| 33 |
for line in out.split("\n"): |
| 34 |
# other/fun and mallocfail can do what they want. |
| 35 |
if "/fun/" in line or "/mallocfail/" in line: |
| 36 |
continue |
| 37 |
filename, include = line.split(":", 1) |
| 38 |
# We only check headers. |
| 39 |
if not filename.endswith(".h"): |
| 40 |
continue |
| 41 |
# ccompat.h can include some things we don't really want elsewhere. |
| 42 |
allowlist = ALLOWLIST |
| 43 |
if filename == "toxcore/ccompat.h": |
| 44 |
allowlist += ("assert.h", "alloca.h", "malloc.h", "stdlib.h") |
| 45 |
header = include[include.index("<") + 1:include.index(">")] |
| 46 |
if header not in allowlist: |
| 47 |
source = filename[:-2] + ".c" |
| 48 |
print( |
| 49 |
f"{filename}: includes system header <{header}>, which is not allowed in .h files" |
| 50 |
) |
| 51 |
print( |
| 52 |
" " * len(filename) + |
| 53 |
f" consider including it in {source} and exporting an abstraction, instead" |
| 54 |
) |
| 55 |
errors += 1 |
| 56 |
|
| 57 |
if errors: |
| 58 |
sys.exit(1) |