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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
#
path = File.dirname __FILE__
lib_path = File.join path, '..', 'lib', 'e17'
#
libraries = [
[ 'eina_types.h', 'eina'],
[ 'Eet.h', 'eet'],
[ 'Evas.h', 'evas'],
[ 'Evas_GL.h', 'evas'],
[ 'Edje.h', 'edje'],
[ 'Ecore.h', 'ecore'],
[ 'Ecore_Con.h', 'ecore'],
[ 'Ecore_Input.h', 'ecore'],
[ 'Ecore_Getopt.h', 'ecore'],
[ 'Ecore_Evas.h', 'ecore'],
[ 'Ecore_Fb.h', 'ecore'],
[ 'Ecore_File.h', 'ecore'],
[ 'Elementary.h', 'libelementary-ver-pre-svn-09.so.0' ],
]
#
INDENT=' '*8
#
HEADER =<<-EOF
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
#
require 'e17/ffi'
#
module E17
#
module MNAME
def self.method_missing m, *args, &block
return E17::API.send 'MBASE_'+m.to_s, *args, &block
end
end
#
module API
#
EOF
FOOTER =<<-EOF
end
end
#
# EOF
EOF
#
TYPES = {
'int' => ':int',
'char' => ':char',
'void' => ':void',
'long' => ':long',
'short' => ':short',
'float' => ':float',
'pid_t' => ':ulong',
'size_t' => ':ulong',
'double' => ':double',
'long int' => ':long',
'long long' => ':long_long',
'unsigned int' => ':uint',
'unsigned char' => ':uchar',
'unsigned short' => ':ushort',
'unsigned long long' => ':ulong_long',
'int *' => ':int_p',
'void *' => ':void_p',
'short *' => ':short_p',
'float *' => ':float_p',
'size_t *' => ':ulong_p',
'double *' => ':double_p',
'unsigned int *' => ':uint_p',
'unsigned char *' => ':uchar_p',
'char *' => ':string', # FIXME ?!?!
'char **' => ':string_array', # FIXME ?!?!
'char ***' => ':string_array_p', # FIXME ?!?!
'fd_set *' => ':pointer',
'FILE *' => ':pointer',
'va_list' => ':pointer', # FIXME ?!?!
'struct tm *' => ':pointer',
'struct timeval *' => ':pointer',
'struct sockaddr *' => ':pointer',
# E17 BASE TYPES
'Eina_Bool' => ':eina_bool',
'Eina_Bool *' => ':eina_bool_p',
'Eina_List' => ':eina_list',
'Eina_List *' => ':eina_list_p',
'Eina_Hash' => ':eina_hash',
'Eina_Hash *' => ':eina_hash_p',
'Eina_Iterator' => ':eina_iterator',
'Eina_Iterator *' => ':eina_iterator_p',
'Eina_Accessor' => ':eina_accessor',
'Eina_Accessor *' => ':eina_accessor_p',
}
#
TYPES_USAGE = {}
#
def set_type t, v, cb=false
return 'bool' if t =~/Eina_Bool/
v = v.downcase.gsub(/(const|enum|union)/,'').strip
if not TYPES[t].nil?
puts "type already exists >#{t}< #{v}"
exit 1
end
r = TYPES[v]
v = r[1..-1] if not r.nil?
TYPES[t]=':'+v
if cb
TYPES[t+' *']=':'+v
else
TYPES[t+' *']=':'+v+'_p'
TYPES[t+' **']=':'+v+'_pp'
TYPES[t+' ***']=':'+v+'_ppp'
end
v
end
#
def get_type t
k = t.gsub(/(const|enum|union)/, '').strip
k.sub!(/\*/,' *') if k=~/\w+\*/
r = TYPES[k]
if r.nil?
puts "unknown type >#{k}< #{t}"
exit 1
end
TYPES_USAGE[k]||=true
r
end
#
def get_type_from_arg arg, l
if arg=~ /^\s*void\s*$/
return ''
end
if arg =~ /\.\.\./
return ':varargs'
end
k = arg.gsub(/const/,'').gsub(/\s{2,}/,' ').strip
if k=~/(.*?)(\w+)$/
return get_type $1
end
# try with unchanged argument string
t = get_type k
if t.nil?
puts "wrong arg >#{k}< #{arg} (#{l})"
exit 1
end
t
end
#
def wrap_text txt, col, indent
txt.gsub( /(.{1,#{col}})(?: +|$\n?)|(.{1,#{col}})/,"\\1\\2\n#{indent}").sub(/\n\s+$/,'')
end
#
def gen_enums path, indent
r = []
open(path+'-enums','r').readlines.each do |l|
l.strip!
if not l=~/(typedef enum(?: \w+)?) {([A-Z0-9_ (\s*=\s*-?[\d+]),]+)} (\w+);/
r << indent+"# #{l}\n#{indent}# FIXME"
next
end
typedef = $1
values = $2
typename = $3
v = set_type typename, typename
args = values.split(',').collect { |cst| ':'+cst.strip.downcase }.join(', ').gsub(/=/,',').gsub(/ ,/,',')
r << indent+"# #{typedef} {...} #{typename};"
r << wrap_text( indent+"enum :#{v}, [ #{args} ]", 150, indent+' '*4 )
r << [ typename+' *', indent+"typedef :pointer, :#{v}_p" ]
r << [ typename+' **', indent+"typedef :pointer, :#{v}_pp" ]
r << [ typename+' ***', indent+"typedef :pointer, :#{v}_ppp" ]
end
r
end
#
def gen_typedefs path, indent
r = []
open(path+'-types','r').readlines.each do |l|
l.strip!
if l=~/typedef (struct|enum|union) _\w+ (\w+);/
t = $2
v = 'pointer'
set_type t, t
elsif l =~/typedef\s+((?:\w+\**\s)+)(\w+);/
t = $2
v = $1
v = set_type t, v
else
r << indent+"# #{l}\n#{indent}# FIXME"
next
end
r << indent+"# #{l}"
r << indent+"typedef :#{v}, :#{t.downcase}"
r << [ t+' *', indent+"typedef :pointer, :#{t.downcase}_p" ]
r << [ t+' **', indent+"typedef :pointer, :#{t.downcase}_pp" ]
r << [ t+' ***', indent+"typedef :pointer, :#{t.downcase}_ppp" ]
end
r
end
#
def gen_callbacks path, indent
r = []
open(path+'-callbacks','r').readlines.each do |l|
l.strip!
if not l=~/^\s*typedef\s+([a-zA-Z0-9_\* ]+?\s+\**)((?:\(\*)?\w+\)?)\s*\((.*)\);\s*$/
r << indent+"# #{l}\n#{indent}# FIXME"
next
end
ret = $1
name = $2
args = $3.split(',').collect { |arg| get_type_from_arg arg, l }.join ', '
k = name.sub(/\(/,'').sub(/\)/,'').sub(/\*/,'')
t = name.downcase.sub(/\(/,'').sub(/\)/,'').sub(/\*/,'')
t = set_type k, t, true
r << indent+"# #{l}"
r << wrap_text(indent+"callback :#{t}, [ #{args} ], #{get_type ret}", 150, indent+' '*4 )
end
r
end
#
def gen_functions path, indent
r = []
r << indent+"fcts = ["
open(path+'-functions','r').readlines.each do |l|
l.strip!
if not l=~ /EAPI ([a-zA-Z0-9_\* ]+?)(\w+) ?\(([a-zA-Z0-9_ \*,\.]+)\)( *[A-Z]{2,})?/
r << indent+"# #{l}\n#{indent}# FIXME"
next
end
ret = $1
func = $2.downcase
args = $3.split(',').collect { |arg| get_type_from_arg arg, l }.join ', '
r << indent+"# #{l}"
r << wrap_text(indent+"[ :#{func}, [ #{args} ], #{get_type ret} ],", 150, indent+' '*4)
end
r << indent+"]"
r
end
#
libraries.collect do |header,lib|
module_name = header[0..-3].sub(/_/,'').capitalize
base = File.join path, 'api', header
dir = File.join lib_path, header[0..-3].split('_').first.downcase
Dir.mkdir dir unless Dir.exists? dir
output = File.join dir, "#{header[0..-3].downcase}-ffi.rb"
puts "parse #{base}-*"
r = [lib, output, module_name, header[0..-3].downcase ]
r << gen_enums(base, INDENT)
r << gen_typedefs(base, INDENT)
r << gen_callbacks(base, INDENT)
r << gen_functions(base, INDENT)
r
end.each do |lib, output, module_name, module_base, enums, typedefs, callbacks, functions|
puts "generate #{output}"
open(output,'w:utf-8') do |f|
f << HEADER.sub(/MNAME/,module_name).sub(/MBASE/,module_base)
f << "#{INDENT}#\n#{INDENT}ffi_lib '#{lib}'"
f << "\n#{INDENT}#\n#{INDENT}# ENUMS"
f << "\n"+enums.collect { |t| ( t.is_a?(Array) ? ( TYPES_USAGE[t[0]] ? t[1] : nil ) : t ) }.compact.join("\n") unless enums.empty?
f << "\n#{INDENT}#\n#{INDENT}# TYPEDEFS"
f << "\n"+typedefs.collect { |t| ( t.is_a?(Array) ? ( TYPES_USAGE[t[0]] ? t[1] : nil ) : t ) }.compact.join("\n") unless typedefs.empty?
f << "\n#{INDENT}#\n#{INDENT}# CALLBACKS"
f << "\n"+callbacks.join("\n") unless callbacks.empty?
f << "\n#{INDENT}#\n#{INDENT}# FUNCTIONS"
f << "\n"+functions.join("\n") unless functions.empty?
f << "\n#{INDENT}#\n#{INDENT}attach_fcts fcts\n"
f << FOOTER
end
end
#
|