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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
#
path = File.dirname __FILE__
lib_path = File.join path, '..', 'lib', 'efl', 'native'
#
# header, module name, fct prefix, lib
libraries = [
# HEADER MODUE NAME FCT PREFIX LIB OUTPUT REQUIRES
[ 'eina_types.h', 'Eina', 'eina', 'eina', 'eina_types.rb' ],
[ 'eina_main.h', 'Eina', 'eina', 'eina', 'eina.rb' ],
[ 'eina_xattr.h', 'EinaXattr', 'eina_xattr', 'eina', 'eina_xattr.rb' ],
[ 'eina_log.h', 'EinaLog', 'eina_log', 'eina', 'eina_log.rb' ],
[ 'eina_list.h', 'EinaList', 'eina_list', 'eina', 'eina_list.rb' ],
[ 'eina_hash.h', 'EinaHash', 'eina_hash', 'eina', 'eina_hash.rb' ],
[ 'Eet.h', 'Eet', 'eet', 'eet', 'eet.rb', ['efl/native/eina_xattr'] ],
[ 'Evas.h', 'Evas', 'evas', 'evas', 'evas.rb' ],
# [ 'Evas_GL.h', 'EvasGl', 'evas_gl', 'evas', 'evas/evas_gl.rb' ],
[ 'Edje.h', 'Edje', 'edje', 'edje', 'edje.rb' ],
[ 'Ecore.h', 'Ecore', 'ecore', 'ecore', 'ecore.rb' ],
# [ 'Ecore_Con.h', 'EcoreCon', 'ecore_con', 'ecore', 'ecore/ecore_con.rb' ],
[ 'Ecore_Input.h', 'EcoreInput', 'ecore_event', 'ecore_input', 'ecore_input.rb' ],
[ 'Ecore_Getopt.h', 'EcoreGetopt', 'ecore_getopt', 'ecore', 'ecore_getopt.rb' ],
[ 'Ecore_Evas.h', 'EcoreEvas', 'ecore_evas', 'ecore_evas', 'ecore_evas.rb' ],
# [ 'Ecore_Fb.h', 'EcoreFb', 'ecore', 'ecore', 'ecore/ecore_fb.rb' ],
# [ 'Ecore_File.h', 'EcoreFile', 'ecore', 'ecore', 'ecore/ecore_file.rb' ],
[ 'EMap.h', 'EMap', 'emap', 'emap', 'emap.rb' ],
[ 'Elementary.h', 'Elm', 'elm', 'libelementary-ver-pre-svn-09.so.0', 'elementary.rb' ],
]
#
INDENT=' '*8
WRAP_LEN=150
#
HEADER =<<-EOF
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
#
require 'efl/native'REQUIRES
#
module Efl
#
module MNAME
#
FCT_PREFIX = 'MY_FCT_PREFIX_' unless const_defined? :FCT_PREFIX
#
def self.method_missing meth, *args, &block
sym = Efl::MethodResolver.resolve self, meth, FCT_PREFIX
self.send sym, *args, &block
end
#
end
#
module Native
EOF
FOOTER =<<-EOF
end
end
#
# EOF
EOF
#
TYPES = {
'int' => ':int',
'char' => ':char',
'void' => ':void',
'long' => ':long',
'short' => ':short',
'float' => ':float',
'pid_t' => ':ulong',
'time_t' => ':ulong',
'size_t' => ':ulong',
'ssize_t' => ':long',
'uintptr_t' => ':pointer',
'double' => ':double',
'long int' => ':long',
'long long' => ':long_long',
'unsigned int' => ':uint',
'unsigned char' => ':uchar',
'unsigned short' => ':ushort',
'unsigned long long' => ':ulong_long',
'char *' => ':string', # FIXME ?!?!
'fd_set *' => ':pointer',
'FILE *' => ':pointer',
'va_list' => ':pointer', # FIXME ?!?!
'struct tm *' => ':pointer',
'struct timeval *' => ':pointer',
'struct sockaddr *' => ':pointer',
'Eina_Bool' => ':eina_bool'
}
#
TYPES_USAGE = {}
#
def set_type t, sym
if TYPES[t].nil?
v = ( TYPES[sym].nil? ? ':'+sym.downcase : TYPES[sym] )
TYPES[t] = v
puts " define type : #{t} => #{v}"
return v
else
return TYPES['Eina_Bool'] if t=='Eina_Bool'
puts "ERROR type #{t} => #{sym} alredy exists!"
exit 1
end
end
#
def get_type t
k = t.gsub(/(const|enum|union)/, '').strip
r = TYPES[k]
if r.nil?
return ':pointer' if k=~ /\*/
puts "unknown type >#{k}< #{t}"
exit 1
end
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.strip
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, indent
txt.gsub( /(.{1,#{150}})(?: +|$\n?)|(.{1,#{150}})/,"\\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+)?) \{(.*)\} (\w+)/
puts "FIXME : #{l}\n#{indent}# FIXME"
r << indent+"# #{l}\n#{indent}# FIXME"
next
end
typedef = $1.strip
values = $2.strip
typename = $3.strip
tsym = set_type typename, typename
syms, vals = [], []
values.split(',').each do |define|
s,v = define.gsub(/ /,'').split(/=/)
syms << s
vals << v
end
if vals.count(nil) == vals.length
args = syms.collect{ |sym| ':'+sym.strip.downcase }.join(', ')
else
i=0
h={}
syms.zip(vals) do |s,v|
sym=':'+s.downcase
if h.has_key? v
h[s]=h[v]
elsif v.nil?
h[s]=i
i+=1
else
h[s]=v
i=v.to_i+1
end
end
args = syms.inject(''){|r,s| r+=":#{s.strip.downcase}, #{h[s]}, " }
end
r << indent+"# #{typedef} {...} #{typename};"
r << wrap_text( indent+"enum #{tsym}, [ #{args} ]", indent+' '*4 )
end
r
end
#
def gen_typedefs path, indent
r = []
open(path+'-types','r').readlines.each do |l|
l.strip!
if l=~/typedef (struct|union) _?\w+ (\w+);/
t = $2.strip
sym = 'pointer'
elsif l=~/typedef enum/
# nothing todo
next
elsif l =~/typedef\s+((?:\w+\**\s)+)(\w+);/
t = $2.strip
sym = $1.strip
else
r << indent+"# #{l}\n#{indent}# FIXME"
next
end
tsym = set_type t, sym
r << indent+"# #{l}"
r << indent+"typedef #{tsym}, :#{t.downcase}"
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+(.*)((?:\(\*?\w+\)| \*?\w+))\s*\((.*)\);/
puts "# #{l}\n#{indent}# FIXME"
r << indent+"# #{l}\n#{indent}# FIXME"
next
end
ret = $1.strip
name = $2.strip
args = $3.split(',').collect { |arg| get_type_from_arg arg, l }.join ', '
t = name.sub(/\(/,'').sub(/\)/,'').sub(/\*/,'')
sym = ( t.downcase=~/_cb$/ ? t : t+'_cb' )
tsym = set_type t, sym
r << indent+"# #{l}"
r << wrap_text(indent+"callback #{tsym}, [ #{args} ], #{get_type ret}", indent+' '*4 )
end
r
end
#
def gen_variables path, indent
r = []
open(path+'-variables','r').readlines.each do |l|
l.strip!
if not l=~ /EAPI\s+extern\s+(\w+\s+\*?)(\w+)/
puts "# #{l}\n#{indent}# FIXME"
r << indent+"# #{l}\n#{indent}# FIXME"
next
end
var = $2.strip
t = $1.strip
r << indent+"# #{l}"
r << wrap_text(indent+"attach_variable :#{var}, #{get_type t}", 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,})?/
puts "# #{l}\n#{indent}# FIXME"
r << indent+"# #{l}\n#{indent}# FIXME"
next
end
ret = $1.strip
func = $2.strip.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} ],", indent+' '*4)
end
r << indent+"]"
r
end
#
Dir.mkdir lib_path unless (File.exists? lib_path)
#
libraries.collect do |header,module_name,fct_prefix,lib, output, requires|
base = File.join path, 'api', header
output = File.join lib_path, output
Dir.mkdir File.dirname(output) unless File.exists? File.dirname(output)
puts "parse #{base}-*"
r = [lib, output, module_name, fct_prefix, requires ]
puts " enums..."
r << gen_enums(base, INDENT)
puts " typedefs..."
r << gen_typedefs(base, INDENT)
puts " callbacks..."
r << gen_callbacks(base, INDENT)
puts " variables..."
r << gen_variables(base, INDENT)
puts " functions..."
r << gen_functions(base, INDENT)
puts "done"
r
end.each do |lib, output, module_name, fct_prefix, requires, enums, typedefs, callbacks, variables, functions|
printf "%-60s", "generate #{output}"
open(output,'w:utf-8') do |f|
reqs = ( requires.nil? ? '' : requires.inject('') {|s,e| s+="\nrequire '#{e}'"})
f << HEADER.gsub(/MNAME/,module_name).sub(/MY_FCT_PREFIX/,fct_prefix).sub(/REQUIRES/,reqs)
f << "#{INDENT}#\n#{INDENT}ffi_lib '#{lib}'"
f << "\n#{INDENT}#\n#{INDENT}# ENUMS"
print "enums, "
f << "\n"+enums.collect { |t| ( t.is_a?(Array) ? t[1] : t ) }.compact.join("\n") unless enums.empty?
f << "\n#{INDENT}#\n#{INDENT}# TYPEDEFS"
print "typedefs, "
f << "\n"+typedefs.collect { |t| ( t.is_a?(Array) ? t[1] : t ) }.compact.join("\n") unless typedefs.empty?
f << "\n#{INDENT}#\n#{INDENT}# CALLBACKS"
print "callbacks, "
f << "\n"+callbacks.join("\n") unless callbacks.empty?
f << "\n#{INDENT}#\n#{INDENT}# VARIABLES"
print "variables, "
f << "\n"+variables.join("\n") unless variables.empty?
f << "\n#{INDENT}#\n#{INDENT}# FUNCTIONS"
puts "functions."
f << "\n"+functions.join("\n") unless functions.empty?
f << "\n#{INDENT}#\n#{INDENT}attach_fcts fcts\n#{INDENT}#\n"
f << FOOTER
end
end
#
|