| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
import subprocess |
| 24 |
import shutil |
| 25 |
import sys |
| 26 |
import os |
| 27 |
import re |
| 28 |
import tempfile |
| 29 |
|
| 30 |
PJ_ROOT_PATH = "../../../" |
| 31 |
|
| 32 |
INCLUDE_DIRS = [ |
| 33 |
PJ_ROOT_PATH + "pjlib/include", |
| 34 |
PJ_ROOT_PATH + "pjlib-util/include", |
| 35 |
PJ_ROOT_PATH + "pjnath/include", |
| 36 |
PJ_ROOT_PATH + "pjmedia/include", |
| 37 |
PJ_ROOT_PATH + "pjsip/include", |
| 38 |
] |
| 39 |
|
| 40 |
|
| 41 |
def find_cpp(): |
| 42 |
"""Find the C preprocessor.""" |
| 43 |
cpp = shutil.which("cpp") |
| 44 |
if cpp: |
| 45 |
return cpp |
| 46 |
|
| 47 |
gcc = shutil.which("gcc") |
| 48 |
if gcc: |
| 49 |
return gcc |
| 50 |
cc = shutil.which("cc") |
| 51 |
if cc: |
| 52 |
return cc |
| 53 |
return None |
| 54 |
|
| 55 |
|
| 56 |
def preprocess(header_files): |
| 57 |
"""Run the C preprocessor on the given header files and return the |
| 58 |
preprocessed output as a string. |
| 59 |
""" |
| 60 |
cpp = find_cpp() |
| 61 |
if not cpp: |
| 62 |
print("Error: need cpp, gcc, or cc in PATH") |
| 63 |
sys.exit(1) |
| 64 |
|
| 65 |
|
| 66 |
content = "#define PJ_AUTOCONF 1\n" |
| 67 |
content += "#define __attribute__(x)\n" |
| 68 |
content += "#define PJ_DEPRECATED(x)\n" |
| 69 |
content += "#define PJ_ATTR_DEPRECATED\n" |
| 70 |
for hf in header_files: |
| 71 |
content += '#include <%s>\n' % hf |
| 72 |
|
| 73 |
|
| 74 |
fd, tmpfile = tempfile.mkstemp(suffix='.h') |
| 75 |
try: |
| 76 |
os.write(fd, content.encode()) |
| 77 |
os.close(fd) |
| 78 |
|
| 79 |
|
| 80 |
cmd = [cpp] |
| 81 |
if 'gcc' in os.path.basename(cpp) or 'cc' == os.path.basename(cpp): |
| 82 |
cmd.append('-E') |
| 83 |
for d in INCLUDE_DIRS: |
| 84 |
cmd.extend(['-I', d]) |
| 85 |
cmd.append(tmpfile) |
| 86 |
|
| 87 |
result = subprocess.run(cmd, capture_output=True, text=True) |
| 88 |
if result.returncode != 0: |
| 89 |
print("Preprocessor error:") |
| 90 |
print(result.stderr) |
| 91 |
sys.exit(1) |
| 92 |
return result.stdout |
| 93 |
finally: |
| 94 |
os.unlink(tmpfile) |
| 95 |
|
| 96 |
|
| 97 |
def find_matching_brace(source, start): |
| 98 |
"""Find the position of the closing '}' that matches the '{' at start, |
| 99 |
handling nested braces. Returns the index of the closing '}'. |
| 100 |
""" |
| 101 |
depth = 0 |
| 102 |
i = start |
| 103 |
while i < len(source): |
| 104 |
if source[i] == '{': |
| 105 |
depth += 1 |
| 106 |
elif source[i] == '}': |
| 107 |
depth -= 1 |
| 108 |
if depth == 0: |
| 109 |
return i |
| 110 |
i += 1 |
| 111 |
return -1 |
| 112 |
|
| 113 |
|
| 114 |
def extract_declarations(preprocessed, names): |
| 115 |
"""Extract enum, struct, and typedef declarations for the given names |
| 116 |
from preprocessed C source. |
| 117 |
|
| 118 |
Returns a dict mapping name -> declaration string. |
| 119 |
""" |
| 120 |
|
| 121 |
lines = [] |
| 122 |
for line in preprocessed.split('\n'): |
| 123 |
if line.startswith('#'): |
| 124 |
continue |
| 125 |
lines.append(line) |
| 126 |
source = '\n'.join(lines) |
| 127 |
|
| 128 |
|
| 129 |
source = re.sub(r'\n{3,}', '\n\n', source) |
| 130 |
|
| 131 |
results = {} |
| 132 |
name_set = set(names) |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
for m in re.finditer( |
| 137 |
r'typedef\s+(enum|struct)\s+(\w+)\s*\{', source): |
| 138 |
kind = m.group(1) |
| 139 |
tag_name = m.group(2) |
| 140 |
brace_start = m.end() - 1 |
| 141 |
brace_end = find_matching_brace(source, brace_start) |
| 142 |
if brace_end < 0: |
| 143 |
continue |
| 144 |
|
| 145 |
rest = source[brace_end+1:brace_end+100] |
| 146 |
alias_m = re.match(r'\s*(\w+)\s*;', rest) |
| 147 |
if not alias_m: |
| 148 |
continue |
| 149 |
alias = alias_m.group(1) |
| 150 |
full_decl = source[m.start():brace_end+1+alias_m.end()] |
| 151 |
if tag_name in name_set or alias in name_set: |
| 152 |
key = tag_name if tag_name in name_set else alias |
| 153 |
if key not in results: |
| 154 |
results[key] = clean_decl(full_decl) |
| 155 |
|
| 156 |
|
| 157 |
for m in re.finditer(r'(?<!\w)enum\s+(\w+)\s*\{', source): |
| 158 |
enum_name = m.group(1) |
| 159 |
if enum_name not in name_set or enum_name in results: |
| 160 |
continue |
| 161 |
|
| 162 |
prefix = source[max(0, m.start()-20):m.start()] |
| 163 |
if 'typedef' in prefix: |
| 164 |
continue |
| 165 |
brace_start = m.end() - 1 |
| 166 |
brace_end = find_matching_brace(source, brace_start) |
| 167 |
if brace_end < 0: |
| 168 |
continue |
| 169 |
rest = source[brace_end+1:brace_end+20] |
| 170 |
semi_m = re.match(r'\s*;', rest) |
| 171 |
if not semi_m: |
| 172 |
continue |
| 173 |
full_decl = source[m.start():brace_end+1+semi_m.end()] |
| 174 |
results[enum_name] = clean_decl(full_decl) |
| 175 |
|
| 176 |
|
| 177 |
for m in re.finditer(r'typedef\s+(\w[\w\s]*?)\s+(\w+)\s*;', source): |
| 178 |
alias = m.group(2) |
| 179 |
if alias in name_set and alias not in results: |
| 180 |
results[alias] = clean_decl(m.group(0)) |
| 181 |
|
| 182 |
return results |
| 183 |
|
| 184 |
|
| 185 |
def clean_decl(decl): |
| 186 |
"""Clean up a declaration: normalize whitespace, fix indentation, |
| 187 |
remove blank lines inside braces. |
| 188 |
""" |
| 189 |
lines = decl.split('\n') |
| 190 |
cleaned = [] |
| 191 |
for line in lines: |
| 192 |
line = line.rstrip() |
| 193 |
|
| 194 |
if line == '' and cleaned and cleaned[-1] != '': |
| 195 |
|
| 196 |
in_braces = any('{' in l for l in cleaned) and \ |
| 197 |
not any('}' in l for l in cleaned) |
| 198 |
if in_braces: |
| 199 |
continue |
| 200 |
cleaned.append(line) |
| 201 |
|
| 202 |
|
| 203 |
while len(cleaned) > 1 and cleaned[-2] == '': |
| 204 |
cleaned.pop(-2) |
| 205 |
|
| 206 |
result = '\n'.join(cleaned).strip() |
| 207 |
return result |
| 208 |
|
| 209 |
|
| 210 |
def reformat_enum(decl): |
| 211 |
"""Reformat an enum declaration for clean output.""" |
| 212 |
|
| 213 |
return decl |
| 214 |
|
| 215 |
|
| 216 |
def process(listfile, outfile): |
| 217 |
"""Main processing: read listfile, preprocess headers, extract symbols, |
| 218 |
write output. |
| 219 |
""" |
| 220 |
|
| 221 |
with open(listfile) as f: |
| 222 |
lines = f.readlines() |
| 223 |
|
| 224 |
|
| 225 |
header_files = [] |
| 226 |
names = [] |
| 227 |
for line in lines: |
| 228 |
spec = line.split() |
| 229 |
if len(spec) < 2: |
| 230 |
continue |
| 231 |
header_files.append(spec[0]) |
| 232 |
names.extend(spec[1:]) |
| 233 |
|
| 234 |
print('Preprocessing %d files for %d symbols..' % |
| 235 |
(len(header_files), len(names))) |
| 236 |
|
| 237 |
|
| 238 |
preprocessed = preprocess(header_files) |
| 239 |
|
| 240 |
|
| 241 |
print('Extracting declarations..') |
| 242 |
decls = extract_declarations(preprocessed, names) |
| 243 |
|
| 244 |
|
| 245 |
print('Writing declarations..') |
| 246 |
with open(outfile, 'w') as f: |
| 247 |
f.write("// This file is autogenerated by importsym script, " |
| 248 |
"do not modify!\n\n") |
| 249 |
for name in names: |
| 250 |
if name not in decls: |
| 251 |
print(" warning: declaration for '%s' is not found **" % name) |
| 252 |
else: |
| 253 |
print(" writing '%s'.." % name) |
| 254 |
decl = decls[name] |
| 255 |
|
| 256 |
if decl.endswith(';'): |
| 257 |
f.write(decl + '\n\n') |
| 258 |
else: |
| 259 |
f.write(decl + ';\n\n') |
| 260 |
|
| 261 |
print("Done. Output written to '%s'" % outfile) |
| 262 |
|
| 263 |
|
| 264 |
if __name__ == "__main__": |
| 265 |
listfile = "symbols.lst" |
| 266 |
outfile = "symbols.i" |
| 267 |
|
| 268 |
if len(sys.argv) >= 3: |
| 269 |
listfile = sys.argv[1] |
| 270 |
outfile = sys.argv[2] |
| 271 |
|
| 272 |
print("Importing symbols: '%s' --> '%s'" % (listfile, outfile)) |
| 273 |
process(listfile, outfile) |