-
Notifications
You must be signed in to change notification settings - Fork 6
/
create_f77_zmq_h.py
executable file
·169 lines (143 loc) · 4.72 KB
/
create_f77_zmq_h.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
#
# f77_zmq : Fortran 77 bindings for the ZeroMQ library
# Copyright (C) 2014 Anthony Scemama
#
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
# USA
#
# Anthony Scemama <scemama@irsamc.ups-tlse.fr>
# Laboratoire de Chimie et Physique Quantiques - UMR5626
# Universite Paul Sabatier - Bat. 3R1b4, 118 route de Narbonne
# 31062 Toulouse Cedex 09, France
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import re
import os
import sys
import ctypes
def create_lines(f):
result = f.read()
result = result.replace('\\\n', '')
result = result.split('\n')
return result
def create_dict_of_defines(lines,file_out):
"""lines is a list of lines coming from the zmq.h"""
# Fetch all parameters in zmq.h
d = {}
for line in lines:
if line.startswith("#define"):
buffer = line.split()
key = buffer[1]
try:
value = int(eval(" ".join(buffer[2:]).strip()))
except:
continue
if key[0] == '_' or '(' in key:
continue
d[key] = value
command = "%(key)s=%(value)d\nd['%(key)s']=%(key)s"%locals()
command = re.sub("/\*.*?\*/", "", command)
exec(command, locals())
# Add the version number:
d['ZMQ_VERSION'] = int(d['ZMQ_VERSION_MAJOR'])*10000 + int(d['ZMQ_VERSION_MINOR'])*100 + int(d['ZMQ_VERSION_PATCH'])
d['ZMQ_PTR'] = ctypes.sizeof(ctypes.c_voidp)
print("===========================================")
print("ZMQ_PTR set to %d (for %d-bit architectures)"%(d['ZMQ_PTR'],d['ZMQ_PTR']*8))
print("===========================================")
# Print to file
keys = list( d.keys() )
keys.sort()
for k in keys:
print(" integer %s"%(k), file=file_out)
for k in keys:
buffer = " parameter(%s=%s)"%(k, d[k])
if len(buffer) > 72:
buffer = " parameter(\n & %s=%s)"%(k, d[k])
print(buffer, file=file_out)
return None
def create_prototypes(lines,file_out):
"""lines is a list of lines coming from the f77_zmq.c file"""
typ_conv = {
'long' : 'integer*8' ,
'int' : 'integer' ,
'float' : 'real',
'char*' : 'character*(64)',
'double' : 'double precision',
'void*' : 'integer*%d'%(ctypes.sizeof(ctypes.c_voidp)),
'void' : None
}
# Get all the functions of the f77_zmq.c file
d = {}
for line in lines:
if line == "":
continue
if line[0] in " #{}/":
continue
buffer = line.replace('_(','_ (').lower().split()
typ = typ_conv[buffer[0]]
if typ is None:
continue
name = buffer[1][:-1].split("(")[1]
d[name] = typ
# Print to file
keys = list( d.keys() )
keys.sort()
for k in keys:
print(" %-20s %s"%(d[k],k), file=file_out)
print(" %-20s %s"%("external",k), file=file_out)
return None
def find_ZMQ_H():
ZMQ_H = os.environ.get("ZMQ_H")
if ZMQ_H is not None:
print("ZMQ_H defined as {0}".format(ZMQ_H))
return ZMQ_H
else:
v = os.environ.get("CPATH")
if v is None:
v = os.environ.get("C_INCLUDE_PATH")
if v is None:
v = "/usr/include:/usr/local/include"
for d in v.split(':'):
if d and "zmq.h" in os.listdir(d):
return ("{0}/zmq.h".format(d))
return None
def main():
ZMQ_H = find_ZMQ_H()
if ZMQ_H is None:
print("Error: zmq.h not found. You can specify it with the ZMQ_H environment variable")
sys.exit(1)
else:
print("Using {0}".format(ZMQ_H))
os.system("cp {0} .".format(ZMQ_H))
file_out = open('f77_zmq.h','w')
file_in = open( ZMQ_H, 'r' )
lines = create_lines(file_in)
file_in.close()
create_dict_of_defines(lines,file_out)
file_in = open(sys.argv[1], 'r' )
lines = create_lines(file_in)
file_in.close()
create_prototypes(lines,file_out)
file_out.close()
file_in = open('f77_zmq.h','r')
file_out = open('f77_zmq_free.h','w')
file_out.write(file_in.read().replace('\n &',' &\n '))
file_in.close()
file_out.close()
if __name__ == '__main__':
main()