Debian Codegraph @

Xpjproject/pjsip-apps/src/swig/importsym.py

raw symbols SCIP
1 #
2 # importsym.py: Import C symbol decls (structs, enums, etc) and write them
3 # to another file
4 #
5 # Copyright (C)2013 Teluu Inc. (http://www.teluu.com)
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 # CPP is needed by pycparser.
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 # Hardcoded!
56 # Note for win32:
57 # - use Windows Command Prompt app, not msys/mingw
58 # - temporarily comment "#include <pj/compat/socket.h>" in pj/sock.h (line ~29)
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 # Heading, to be placed before the source files
71 C_HEADING_SECTION = """
72 #define PJ_AUTOCONF 1
73 #define jmp_buf int
74 #define __attribute__(x)
75 """
76
77 # CPP (C preprocessor) settings
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 # Read listfile
136 f = open(listfile)
137 lines = f.readlines()
138 f.close()
139
140 # Process each line in list file, while generating the
141 # temporary C file to be processed by pycparser
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 # Parse the temporary C file
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 # Filter the declarations that we wanted
162 print 'Filtering..'
163 visitor = SymbolVisitor(names)
164 visitor.visit(ast)
165
166 # Print symbol declarations to outfile
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