3
'''Generate the wapi_counts.c source from the ga-papi.h header.'''
7
def get_signatures(header):
8
# first, gather all function signatures from ga-papi.h aka argv[1]
11
current_signature = ''
14
for line in open(header):
15
line = line.strip() # remove whitespace before and after line
17
continue # skip blank lines
18
if EXTERN in line and SEMICOLON in line:
19
signatures.append(line)
21
current_signature = line
23
elif SEMICOLON in line and accumulating:
24
current_signature += line
25
signatures.append(current_signature)
28
current_signature += line
31
class FunctionArgument(object):
32
def __init__(self, signature):
33
self.pointer = signature.count('*')
34
self.array = '[' in signature
35
signature = signature.replace('*','').strip()
36
signature = signature.replace('[','').strip()
37
signature = signature.replace(']','').strip()
38
self.type,self.name = signature.split()
43
for p in range(self.pointer):
50
class Function(object):
51
def __init__(self, signature):
52
signature = signature.replace('extern','').strip()
53
self.return_type,signature = signature.split(None,1)
54
self.return_type = self.return_type.strip()
55
signature = signature.strip()
56
self.name,signature = signature.split('(',1)
57
self.name = self.name.strip()
58
signature = signature.replace(')','').strip()
59
signature = signature.replace(';','').strip()
62
for arg in signature.split(','):
63
self.args.append(FunctionArgument(arg.strip()))
65
def get_call(self, name=None):
76
sig = sig[:-2] # remove last ', '
80
def get_signature(self, name=None):
81
sig = self.return_type[:]
92
sig = sig[:-2] # remove last ', '
97
return self.get_signature()
99
if __name__ == '__main__':
100
if len(sys.argv) != 2:
101
print 'incorrect number of arguments'
102
print 'usage: wapigen_counts.py <ga-papi.h> > <wapi_counts.c>'
103
sys.exit(len(sys.argv))
112
#include "typesf2c.h"
116
# parse signatures into the Function class
117
for sig in get_signatures(sys.argv[1]):
118
function = Function(sig)
119
functions[function.name] = function
121
# for each function, generate a static count
122
for name in sorted(functions):
123
print 'static long count_%s = 0;' % name
126
# now process the functions
127
for name in sorted(functions):
128
func = functions[name]
129
if 'terminate' in name:
132
if 'void' not in func.return_type:
133
maybe_return = 'return '
134
func = functions[name]
135
wnga_name = name.replace('pnga_','wnga_')
142
''' % (func.get_signature(wnga_name), name, maybe_return, func.get_call())
144
# prepare to output the terminate function
145
name = 'pnga_terminate'
146
wnga_name = name.replace('pnga_','wnga_')
147
func = functions[name]
149
# establish 'the_code' to use in the body of terminate
150
# it's printing the count of each function if it was called at least once
151
for name in sorted(functions):
154
printf("%s %%ld\\n", count_%s);
156
''' % (name, name, name)
157
# output the terminate function
160
++count_pnga_terminate;
162
/* don't dump info if terminate more than once */
163
if (1 == count_pnga_terminate) {
167
''' % (func.get_signature(wnga_name), func.get_call(), the_code)