| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
import pycparser |
| 22 |
from pycparser import c_generator |
| 23 |
import sys |
| 24 |
import os |
| 25 |
|
| 26 |
def which(program): |
| 27 |
import os |
| 28 |
def is_exe(fpath): |
| 29 |
return os.path.isfile(fpath) and os.access(fpath, os.X_OK) |
| 30 |
|
| 31 |
if sys.platform == 'win32' and not program.endswith(".exe"): |
| 32 |
program += ".exe" |
| 33 |
|
| 34 |
fpath, fname = os.path.split(program) |
| 35 |
if fpath: |
| 36 |
if is_exe(program): |
| 37 |
return program |
| 38 |
else: |
| 39 |
for path in os.environ["PATH"].split(os.pathsep): |
| 40 |
path = path.strip('"') |
| 41 |
exe_file = os.path.join(path, program) |
| 42 |
if is_exe(exe_file): |
| 43 |
return exe_file |
| 44 |
return None |
| 45 |
|
| 46 |
|
| 47 |
PJ_ROOT_PATH = "../../../" |
| 48 |
|
| 49 |
|
| 50 |
CPP_PATH = which("cpp") |
| 51 |
if not CPP_PATH: |
| 52 |
print 'Error: need to have cpp in PATH' |
| 53 |
sys.exit(1) |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
if sys.platform == 'win32': |
| 60 |
PYCPARSER_DIR="D:/work/tool/pycparser-master" |
| 61 |
elif sys.platform == "linux2": |
| 62 |
PYCPARSER_DIR="/home/bennylp/Desktop/opt/src/pycparser-master" |
| 63 |
else: |
| 64 |
PYCPARSER_DIR="/Library/Python/2.7/site-packages/pycparser" |
| 65 |
|
| 66 |
if not os.path.exists(PYCPARSER_DIR + '/utils/fake_libc_include'): |
| 67 |
print "Error: couldn't find pycparser utils in '%s'" % PYCPARSER_DIR |
| 68 |
sys.exit(1) |
| 69 |
|
| 70 |
|
| 71 |
C_HEADING_SECTION = """ |
| 72 |
#define PJ_AUTOCONF 1 |
| 73 |
#define jmp_buf int |
| 74 |
#define __attribute__(x) |
| 75 |
""" |
| 76 |
|
| 77 |
|
| 78 |
CPP_CFLAGS = [ |
| 79 |
'-I' + PYCPARSER_DIR + '/utils/fake_libc_include', |
| 80 |
"-I" + PJ_ROOT_PATH + "pjlib/include", |
| 81 |
"-I" + PJ_ROOT_PATH + "pjlib-util/include", |
| 82 |
"-I" + PJ_ROOT_PATH + "pjnath/include", |
| 83 |
"-I" + PJ_ROOT_PATH + "pjmedia/include", |
| 84 |
"-I" + PJ_ROOT_PATH + "pjsip/include" |
| 85 |
] |
| 86 |
|
| 87 |
|
| 88 |
class SymbolVisitor(pycparser.c_ast.NodeVisitor): |
| 89 |
def __init__(self, names): |
| 90 |
self.nodeDict = {} |
| 91 |
for name in names: |
| 92 |
self.nodeDict[name] = None |
| 93 |
|
| 94 |
def _add(self, node): |
| 95 |
if self.nodeDict.has_key(node.name): |
| 96 |
self.nodeDict[node.name] = node |
| 97 |
|
| 98 |
def visit_Struct(self, node): |
| 99 |
self._add(node) |
| 100 |
|
| 101 |
def visit_Enum(self, node): |
| 102 |
self._add(node) |
| 103 |
|
| 104 |
def visit_Typename(self, node): |
| 105 |
self._add(node) |
| 106 |
|
| 107 |
def visit_Typedef(self, node): |
| 108 |
self._add(node) |
| 109 |
|
| 110 |
|
| 111 |
TEMP_FILE="tmpsrc.h" |
| 112 |
|
| 113 |
class SymbolImporter: |
| 114 |
""" |
| 115 |
Import C selected declarations from C source file and move it |
| 116 |
to another file. |
| 117 |
|
| 118 |
Parameters: |
| 119 |
- listfile Path of file containing list of C source file |
| 120 |
and identifier names to be imported. The format |
| 121 |
of the listfile is: |
| 122 |
|
| 123 |
filename name1 name2 name3 |
| 124 |
|
| 125 |
for example: |
| 126 |
|
| 127 |
pj/sock_qos.h pj_qos_type pj_qos_flag |
| 128 |
pj/types.h pj_status_t PJ_SUCCESS |
| 129 |
""" |
| 130 |
def __init__(self): |
| 131 |
pass |
| 132 |
|
| 133 |
def process(self, listfile, outfile): |
| 134 |
|
| 135 |
|
| 136 |
f = open(listfile) |
| 137 |
lines = f.readlines() |
| 138 |
f.close() |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
f = open(TEMP_FILE, "w") |
| 143 |
f.write(C_HEADING_SECTION) |
| 144 |
names = [] |
| 145 |
fcnt = 0 |
| 146 |
for line in lines: |
| 147 |
spec = line.split() |
| 148 |
if len(spec) < 2: |
| 149 |
continue |
| 150 |
fcnt += 1 |
| 151 |
f.write("#include <%s>\n" % spec[0]) |
| 152 |
names.extend(spec[1:]) |
| 153 |
f.close() |
| 154 |
print 'Parsing %d symbols from %d files..' % (len(names), fcnt) |
| 155 |
|
| 156 |
try: |
| 157 |
|
| 158 |
ast = pycparser.parse_file(TEMP_FILE, use_cpp=True, cpp_path=CPP_PATH, cpp_args=CPP_CFLAGS) |
| 159 |
os.remove(TEMP_FILE) |
| 160 |
|
| 161 |
|
| 162 |
print 'Filtering..' |
| 163 |
visitor = SymbolVisitor(names) |
| 164 |
visitor.visit(ast) |
| 165 |
|
| 166 |
|
| 167 |
print 'Writing declarations..' |
| 168 |
f = open(outfile, 'w') |
| 169 |
f.write("// This file is autogenerated by importsym script, do not modify!\n\n") |
| 170 |
gen = pycparser.c_generator.CGenerator() |
| 171 |
for name in names: |
| 172 |
node = visitor.nodeDict[name] |
| 173 |
if not node: |
| 174 |
print " warning: declaration for '%s' is not found **" % k |
| 175 |
else: |
| 176 |
print " writing '%s'.." % name |
| 177 |
output = gen.visit(node) + ";\n\n" |
| 178 |
f.write(output) |
| 179 |
f.close() |
| 180 |
print "Done." |
| 181 |
|
| 182 |
except Exception as e: |
| 183 |
print e |
| 184 |
|
| 185 |
if __name__ == "__main__": |
| 186 |
print "Importing symbols: 'symbols.lst' --> 'symbols.i'" |
| 187 |
si = SymbolImporter() |
| 188 |
si.process("symbols.lst", "symbols.i") |
| 189 |
try: |
| 190 |
os.remove("lextab.py") |
| 191 |
except OSError: |
| 192 |
pass |
| 193 |
try: |
| 194 |
os.remove("yacctab.py") |
| 195 |
except OSError: |
| 196 |
pass |