File: check_includes.py

package info (click to toggle)
radare2 6.0.7%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 78,360 kB
  • sloc: ansic: 903,263; sh: 8,137; javascript: 7,911; makefile: 5,503; python: 2,730; cpp: 789; perl: 404; lisp: 122; sed: 85; asm: 57; cs: 37; xml: 32; ruby: 29; java: 21
file content (106 lines) | stat: -rwxr-xr-x 3,387 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3

import glob
import os
import re
import sys

def extract_list(content, list_name):
    pattern = rf'{list_name} (\+?=) \[(.*?)\]'
    matches = re.findall(pattern, content, re.DOTALL)
    files = []
    for op, items in matches:
        # Split by comma, but handle multiline
        lines = items.split('\n')
        for line in lines:
            line = line.strip()
            if line.startswith("'") and (line.endswith("',") or line.endswith("'")):
                if line.endswith("',"):
                    files.append(line[1:-2])
                else:
                    files.append(line[1:-1])
            elif line.startswith('"') and (line.endswith('",') or line.endswith('"')):
                if line.endswith('",'):
                    files.append(line[1:-2])
                else:
                    files.append(line[1:-1])
            elif line.startswith('join_paths('):
                # Handle join_paths
                # For simplicity, assume it's join_paths('include', 'sflib', arch, file) or similar
                # But for common, it's join_paths('include/sflib/common/sftypes.h')
                # Actually, for sflib_common_files, it's join_paths('include/sflib/common/sftypes.h')
                # So, extract the string inside
                inner = line[11:-1]  # remove join_paths(
                if inner.endswith(')'):
                    inner = inner[:-1]
                # Assume it's a single string
                if inner.startswith("'") and inner.endswith("'"):
                    files.append(inner[1:-1])
    return files

def main():
    meson_file = 'libr/meson.build'
    if not os.path.exists(meson_file):
        print(f"Error: {meson_file} not found")
        sys.exit(1)

    with open(meson_file, 'r') as f:
        content = f.read()

    lists = [
        'include_files',
        'r_util_files',
        'r_muta_files',
        'r_anal_files',
        'r_esil_files',
    ]

    all_files = []
    for lst in lists:
        all_files.extend(extract_list(content, lst))

    # Get all actual header files
    all_headers = glob.glob('libr/include/**/*.h', recursive=True)
    actual = set()
    for h in all_headers:
        rel_path = h[len('libr/'):]
        if not rel_path.startswith('include/sdb/') and not rel_path.startswith('include/sflib/') and rel_path not in ('include/r_userconf.h', 'include/r_version.h'):
            actual.add(rel_path)

    listed = set(all_files)

    # Check for listed files that don't exist
    nonexistent = []
    for file in all_files:
        path = f'libr/{file}'
        if not os.path.exists(path):
            nonexistent.append(file)

    # Check for headers not listed
    missing = actual - listed

    # Check for extra listed headers
    extras = listed - actual

    if nonexistent:
        print("Listed include files that do not exist:")
        for m in sorted(nonexistent):
            print(f"  {m}")
        sys.exit(1)

    if missing:
        print("Headers in libr/include not listed in meson.build:")
        for m in sorted(missing):
            print(f"  {m}")
        sys.exit(1)

    if extras:
        print("Extra headers listed in meson.build:")
        for e in sorted(extras):
            print(f"  {e}")
        sys.exit(1)

    print("All include files are present and correctly listed.")

if __name__ == '__main__':
    main()