Mercurial > projects > dstep
annotate scripts/dstepgen.rb @ 4:5a1a6afbfe3a
Added support for function pointers and variadic function pointers to dgen and dstepgen
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Sun, 05 Jul 2009 21:52:04 +0200 |
parents | d0162d8ca0f2 |
children | c0cfd40362ee |
rev | line source |
---|---|
1 | 1 #!/usr/bin/env ruby |
2 | |
3 # Copyright (c) 2008-2009, Jacob Carlborg. All rights reserved. | |
4 # Copyright (c) 2006-2007, Apple Inc. All rights reserved. | |
5 # Copyright (c) 2005-2006 FUJIMOTO Hisakuni | |
6 # | |
7 # Redistribution and use in source and binary forms, with or without | |
8 # modification, are permitted provided that the following conditions | |
9 # are met: | |
10 # 1. Redistributions of source code must retain the above copyright | |
11 # notice, this list of conditions and the following disclaimer. | |
12 # 2. Redistributions in binary form must reproduce the above copyright | |
13 # notice, this list of conditions and the following disclaimer in the | |
14 # documentation and/or other materials provided with the distribution. | |
15 # 3. Neither the name of Apple Inc. ("Apple") nor the names of | |
16 # its contributors may be used to endorse or promote products derived | |
17 # from this software without specific prior written permission. | |
18 # | |
19 # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND | |
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 # ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR | |
23 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
27 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
28 # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
29 # POSSIBILITY OF SUCH DAMAGE. | |
30 | |
31 require "rubygems" | |
32 gem "builder", "~> 2.0" | |
33 require "builder" | |
34 require "tmpdir" | |
35 require "optparse" | |
36 include Builder | |
37 | |
38 $KCODE = "UTF8" | |
39 | |
40 # Extensions that adds support for member access syntax | |
41 class Hash | |
42 def type | |
43 result = self["type"] | |
44 return result unless result.nil? | |
45 self[:type] | |
46 end | |
47 | |
48 def type= (type) | |
49 self[:type] = type | |
50 end | |
51 | |
52 def id | |
53 result = self["id"] | |
54 return result unless result.nil? | |
55 self[:id] | |
56 end | |
57 | |
58 def id= (id) | |
59 self[:id] = id | |
60 end | |
61 | |
62 def methods | |
63 result = self["methods"] | |
64 return result unless result.nil? | |
65 self[:methods] | |
66 end | |
67 | |
68 def methods= (methods) | |
69 self[:methods] = methods | |
70 end | |
71 | |
72 def method_missing (method, *args) | |
73 self.class.instance_eval do | |
74 define_method(method) do |*args| | |
75 if args.length > 0 | |
3
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
76 #self[method[0 ... -1]] = args[0] |
1 | 77 self[eval(":#{method}"[0 ... -1])] = args[0] |
78 else | |
79 result = self[method] | |
80 return result unless result.nil? | |
81 self[eval(":#{method}")] | |
82 end | |
83 end | |
84 end | |
85 | |
86 if (method = method.id2name) =~ /=/ | |
87 eval("self.#{method} (args.length < 2 ? args[0] : args)") | |
88 else | |
89 eval("self.#{method}") | |
90 end | |
91 end | |
92 end | |
93 | |
94 # This Struct represents an Objective-C Framework | |
95 Framework = Struct.new(:name, :headers, :path) do | |
96 def initialize | |
97 self.headers = [] | |
98 self.name = "" | |
99 self.path = "" | |
100 end | |
101 end | |
102 | |
103 # This Struct represents a C/Objective-C header | |
3
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
104 HeaderFile = Struct.new(:name, :framework, :cftypes, :constants, :defines, :enums, :externs, :functions, :function_pointers, |
1 | 105 :imports, :inline_functions, :opaques, :path, :structs, :typedefs) do |
106 def initialize | |
107 self.name = "" | |
108 self.cftypes = [] | |
109 self.constants = [] | |
110 self.defines = [] | |
111 self.enums = [] | |
112 self.externs = [] | |
113 self.framework = "" | |
114 self.functions = [] | |
3
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
115 self.function_pointers = {} |
1 | 116 self.imports = [] |
117 self.inline_functions = [] | |
118 self.opaques = [] | |
119 self.path = "" | |
120 self.structs = [] | |
121 self.typedefs = [] | |
122 end | |
123 end | |
124 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
125 Category = Struct.new(:name, :class) do |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
126 def initialize |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
127 self.name = "" |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
128 self.class = "" |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
129 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
130 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
131 |
1 | 132 # This class scans the headers |
133 class HeaderScaner | |
134 CPP = ['/usr/bin/cpp-4.0', '/usr/bin/cpp-3.3', '/usr/bin/cpp3'].find { |x| File.exist?(x) } | |
135 raise "cpp not found" if CPP.nil? | |
136 CPPFLAGS = "-D__APPLE_CPP__ -include /usr/include/AvailabilityMacros.h" | |
137 CPPFLAGS << "-D__GNUC__" unless /\Acpp-4/.match(File.basename(CPP)) | |
138 | |
139 attr_accessor :frameworks, :headers, :do_64bit | |
140 | |
141 def initialize | |
142 @extern_name = 'extern' | |
143 @frameworks = [] | |
144 @file_content = nil | |
145 @headers = [] | |
146 @classes = {} | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
147 @protocols = {} |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
148 @categories = {} |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
149 #@informal_protocols = {} |
3
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
150 #@function_pointers = {} |
1 | 151 @do_64bit = false |
152 end | |
153 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
154 def get_classes |
1 | 155 @classes |
156 end | |
157 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
158 def get_protocols |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
159 @protocols |
1 | 160 end |
161 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
162 def get_categories |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
163 @categories |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
164 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
165 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
166 # def get_informal_protocols |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
167 # @informal_protocols |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
168 # end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
169 |
1 | 170 def cftypes (header) |
171 re = /typedef\s+(const\s+)?(struct\s*\w+\s*\*\s*)([^\s]+Ref)\s*;/ | |
172 @cpp_result.scan(re).each do |m| | |
173 header.cftypes << { :name => m[2], :type => m[1], :const => m[0] =~ /const/ ? true : false} | |
174 end | |
175 end | |
176 | |
177 def constants (header) | |
178 tmp = header.externs.map do |i| | |
179 constant?(i, true) | |
180 end | |
181 | |
182 header.constants = tmp.flatten.compact | |
183 end | |
184 | |
185 def defines (header) | |
186 re = /#define\s+([^\s]+)\s+(\([^)]+\)|[^\s]+)\s*$/ | |
187 @file_content.scan(re).each do |m| | |
188 next unless !m[0].include?('(') && m[1] != '\\' | |
189 header.defines << { :name => m[0], :value => m[1] } | |
190 end | |
191 end | |
192 | |
193 def enums (header) | |
194 re = /\benum\b\s*(\w+\s+)?\{([^}]*)\}/ | |
195 @cpp_result.scan(re).each do |m| | |
196 enum = { :name => m[0], :members => [] } | |
197 | |
198 m[1].split(",").map do |i| | |
199 name, value = i.split("=", 2).map do |x| | |
200 x.strip | |
201 end | |
202 | |
203 enum[:members] << { :name => name, :value => value } unless name.empty? || name[0] == ?# | |
204 end | |
205 | |
206 header.enums << enum | |
207 end | |
208 end | |
209 | |
3
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
210 def function_pointers (header) |
4
5a1a6afbfe3a
Added support for function pointers and variadic function pointers to dgen and dstepgen
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
211 re = /typedef\s+([\w\s\*]+)\s*\(\s*\*\s*(\w+)\s*\)\s*\(([^)]*)\)\s*;/ |
1 | 212 data = @cpp_result.scan(re) |
4
5a1a6afbfe3a
Added support for function pointers and variadic function pointers to dgen and dstepgen
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
213 re = /typedef\s+([\w\s\*]+)\s*\(([^)]+)\)\s*;/ |
1 | 214 data |= @cpp_result.scan(re).map do |m| |
215 ary = m[0].split(/(\w+)$/) | |
216 ary[1] << " *" | |
217 ary << m[1] | |
218 ary | |
219 end | |
220 | |
221 data.each do |m| | |
4
5a1a6afbfe3a
Added support for function pointers and variadic function pointers to dgen and dstepgen
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
222 variadic = false |
1 | 223 name = m[1] |
224 args = m[2].split(",").map do |x| | |
4
5a1a6afbfe3a
Added support for function pointers and variadic function pointers to dgen and dstepgen
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
225 variadic = x =~ /\.\.\./ ? true : false |
1 | 226 if x.include?(" ") |
227 ptr = x.sub!(/\[\]\s*$/, "") | |
228 x = x.sub(/\w+\s*$/, "").strip | |
229 ptr ? x + "*" : x | |
230 else | |
231 x.strip | |
232 end | |
233 end | |
234 | |
4
5a1a6afbfe3a
Added support for function pointers and variadic function pointers to dgen and dstepgen
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
235 args.delete("...") do |
5a1a6afbfe3a
Added support for function pointers and variadic function pointers to dgen and dstepgen
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
236 args |
5a1a6afbfe3a
Added support for function pointers and variadic function pointers to dgen and dstepgen
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
237 end |
5a1a6afbfe3a
Added support for function pointers and variadic function pointers to dgen and dstepgen
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
238 |
1 | 239 type = "#{m[0]}(*)(#{args.join(', ')})" |
3
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
240 header.function_pointers[name] = get_function_pointer(type) |
4
5a1a6afbfe3a
Added support for function pointers and variadic function pointers to dgen and dstepgen
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
241 header.function_pointers[name].variadic = variadic |
1 | 242 end |
243 end | |
244 | |
3
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
245 def get_function_pointer (fp) |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
246 type = {} |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
247 re = /(.+)\(\*\)\((.+)\)/ |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
248 |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
249 if fp =~ /(.+)\(\*\)\((.+)\)/ |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
250 m1 = $1 |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
251 m2 = $2 |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
252 |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
253 type.const = m1 =~ /const/ ? true : false |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
254 m1.gsub!(/const/, "") |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
255 type.return_type = m1.strip |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
256 type.args = [] |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
257 |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
258 args = m2.split(",") |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
259 |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
260 args.each do |arg| |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
261 const = arg =~ /const/ ? true : false |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
262 arg.gsub!(/const/, "") |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
263 arg.strip! |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
264 arg.gsub!(/\s*\*/, "*") |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
265 type.args << {:type => arg, :const => const} |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
266 end |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
267 end |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
268 |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
269 type |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
270 end |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
271 |
1 | 272 def typedefs (header) |
273 re = /^\s*typedef\s+(.+)\s+([\w\*]+)\s*;$/ | |
274 data = @cpp_result | |
275 data.scan(re).each do |m| | |
276 var = get_var_info(m[0] + " " + m[1]) | |
277 header.typedefs << get_var_info(m[0] + " " + m[1]) if var | |
278 end | |
279 end | |
280 | |
281 def externs (header) | |
282 re = /^\s*#{@extern_name}\s+\b(.*)\s*;.*$/ | |
283 header.externs = @cpp_result.scan(re).map do |m| | |
284 m[0].strip | |
285 end | |
286 end | |
287 | |
288 def imports (header) | |
289 tmp = [] | |
290 | |
291 @file_content.each do |line| | |
292 if line =~ /#(include|import) <(.+)\.h>/ | |
293 next if $2 == header.name | |
294 tmp << $2 | |
295 elsif line =~ /@class(\s+)(\w+)(,|;)/ | |
296 next if $2 == header.name | |
297 | |
298 if $3 == ";" | |
299 tmp << header.framework + "/" + $2 unless header.framework == "" | |
300 tmp << $2 if header.framework == "" | |
301 elsif | |
302 str = line[6 + $1.length ... -2] | |
303 str.gsub!(" ", "") | |
304 arr = str.split(",") | |
305 | |
306 arr.each do |s| | |
307 tmp << header.framework + "/" + s unless header.framework == "" | |
308 tmp << s if header.framework == "" | |
309 end | |
310 end | |
311 end | |
312 end | |
313 | |
314 header.imports = tmp.compact.uniq | |
315 end | |
316 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
317 # def informal_protocols (header) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
318 # methods(header, /^@(interface)\s+(\w+)\s*:?\s*(\w*)\s*(\([^)]+\))?/, @protocols, false) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
319 # end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
320 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
321 def classes (header) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
322 methods(header, /^@(interface)\s+(\w+)\s*:?\s*(\w*)\s*(\<([\w,\s]+)>)?$/, @classes, false) |
1 | 323 end |
324 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
325 def protocols (header) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
326 methods(header, /^@(protocol)\s+(\w+)\s*:?\s*(\w*)\s*(\<([\w,\s]+)>)?(\([^)]+\))?/, @protocols, true) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
327 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
328 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
329 def categories (header) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
330 methods(header, /^@(interface)\s+(\w+)\s*:?\s*(\w*)\s*(\<([\w,\s]+)>)?(\([^)]+\))/, @categories, false, true) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
331 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
332 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
333 def methods (header, regex, array, protocol, category = false) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
334 interface_re = regex |
1 | 335 |
336 end_re = /^@end/ | |
337 body_re = /^[-+]\s*(\([^)]+\))?\s*([^:\s;]+)/ | |
338 args_re = /\w+\s*:/ | |
339 prop_re = /^@property\s*(\([^)]+\))?\s*([^;]+);$/ | |
340 current_interface = current_category = nil | |
341 i = 0 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
342 parent = nil |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
343 protocols = nil |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
344 cat = nil |
1 | 345 |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
346 @cpp_result.each_line do |line| |
1 | 347 size = line.size |
348 line.strip! | |
349 | |
350 if md = interface_re.match(line) | |
351 parent = nil | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
352 protocols = nil |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
353 current_category = md[6].delete("()").strip if md[6] |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
354 current_interface = md[2] unless category |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
355 current_interface = current_category if category |
1 | 356 parent = md[3] unless md[3] == "" |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
357 protocols = md[5] if md[5] |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
358 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
359 if category |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
360 cat = Category.new if category |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
361 cat.name = current_category |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
362 cat.class = md[2] |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
363 end |
1 | 364 |
365 elsif end_re.match(line) | |
366 current_interface = current_category = nil | |
367 | |
368 elsif current_interface && md = prop_re.match(line) | |
369 # Parsing Objective-C 2.0 properties | |
370 if (a = md[2].split(/\s/)).length >= 2 && /^\w+$/.match(name = a[-1]) && (type = a[0 .. -2].join(" ")).index(",").nil? | |
371 getter, setter = name, "set#{name[0].chr.upcase + name[1 .. -1]}" | |
372 readonly = false | |
373 | |
374 if attributes = md[1] | |
375 if md = /getter\s*=\s*(\w+)/.match(attributes) | |
376 getter = md[1] | |
377 end | |
378 | |
379 if md = /setter\s*=\s*(\w+)/.match(attributes) | |
380 setter = md[1] | |
381 end | |
382 | |
383 readonly = true if attributes.index("readonly") | |
384 end | |
385 | |
386 typeinfo = VarInfo.new(type, "", "") | |
387 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
388 array[current_interface] ||= {} unless category |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
389 array[cat] ||= {} if category |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
390 methods = (array[current_interface].methods ||= []) unless category |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
391 methods = (array[cat].methods ||= []) if category |
1 | 392 methods << MethodInfo.new(typeinfo, getter, false, [], line) |
393 | |
394 unless readonly | |
395 methods << MethodInfo.new(VarInfo.new("void", "", ""), setter + ":", false, [typeinfo], line) | |
396 end | |
397 end | |
398 | |
399 elsif current_interface && (line[0] == ?+ || line[0] == ?-) | |
400 mtype = line[0] | |
401 data = @cpp_result[i .. -1] | |
402 body_md = body_re.match(data) | |
403 | |
404 next if body_md.nil? | |
405 | |
406 rettype = body_md[1] ? body_md[1].delete("()") : "id" | |
407 retval = VarInfo.new(rettype, "", "") | |
408 args = [] | |
409 selector = "" | |
410 data = data[0 .. data.index(";")] | |
411 args_data = [] | |
412 | |
413 data.scan(args_re) do |x| | |
414 args_data << [$`, x, $'] | |
415 end | |
416 | |
417 variadic = false | |
418 args_data.each_with_index do |ary, n| | |
419 before, argname, argtype = ary | |
420 arg_nameless = (n > 0 && /\)\s*$/.match(before)) | |
421 argname = ":" if arg_nameless | |
422 realargname = nil | |
423 | |
424 if n < args_data.length - 1 | |
425 argtype.sub!(args_data[n + 1][2], "") | |
426 | |
427 if arg_nameless | |
428 argtype.sub!(/(\w+\s*)?\w+\s*:\s*$/, "") | |
429 else | |
430 unless argtype.sub!(/(\w+)\s+\w+:\s*$/) { |s| realargname = $1; "" } | |
431 # maybe the next argument is nameless | |
432 argtype.sub!(/\w+\s*:\s*$/, "") | |
433 end | |
434 end | |
435 else | |
436 argtype.sub!(/\s+__attribute__\(\(.+\)\)/, "") | |
437 | |
438 if arg_nameless | |
439 argtype.sub!(/\w+\s*;$/, "") | |
440 else | |
441 unless argtype.sub!(/(\w+)\s*;$/) { |s| realargname = $1; "" } | |
442 variadic = argtype.sub!(/,\s*\.\.\.\s*;/, "") != nil | |
443 argtype.sub!(/\w+\s*$/, "") if variadic | |
444 end | |
445 end | |
446 end | |
447 | |
448 selector << argname | |
449 realargname ||= argname.sub(/:/, "") | |
450 args << VarInfo.new(argtype, realargname, "") unless argtype.empty? | |
451 end | |
452 | |
453 selector = body_md[2] if selector.empty? | |
454 args << VarInfo.new("...", "vararg", "") if variadic | |
455 method = MethodInfo.new(retval, selector, line[0] == ?+, args, data) | |
456 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
457 # if protocol && current_category && current_interface == "NSObject" |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
458 # (@informal_protocols[current_category] ||= []) << method |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
459 # end |
1 | 460 |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
461 array[current_interface] ||= {} unless category |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
462 array[cat] ||= {} if category |
1 | 463 |
464 if header.name == current_interface | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
465 array[current_interface].file = header.name unless category |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
466 array[cat].file = header.name if category |
1 | 467 else |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
468 array[current_interface].file ||= header.name unless category |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
469 array[cat].file ||= header.name if category |
1 | 470 end |
471 | |
472 unless parent == current_interface || parent =~ /\s+/ || parent.nil? | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
473 array[current_interface].parent = parent unless category |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
474 array[cat].parent = parent if category |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
475 end |
1 | 476 |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
477 unless protocols.nil? |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
478 protocols.gsub!(/ /, "") |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
479 array[current_interface].protocols = protocols unless category |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
480 array[cat].protocols = protocols if category |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
481 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
482 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
483 (array[current_interface].methods ||= []) << method unless category |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
484 (array[cat].methods ||= []) << method if category |
1 | 485 end |
486 i += size | |
487 end | |
488 end | |
489 | |
490 def structs (header) | |
491 re = /typedef\s+struct\s*\w*\s*((\w+)|\{([^{}]*(\{[^}]+\})?)*\}\s*([^\s]+))\s*(__attribute__\(.+\))?\s*;/ | |
492 i = 0 | |
493 body = nil | |
494 @cpp_result.scan(re).each do |m| | |
495 struct = { :name => m[4], :members => [] } | |
496 | |
497 unless struct[:name].nil? | |
498 if struct[:name][0, 1] == "*" | |
499 struct[:name].sub!("*", "") | |
500 end | |
501 end | |
502 | |
503 return_type = nil | |
504 stripped_return_type = nil | |
505 body = m[2] | |
506 | |
507 if m[2] | |
508 m[2].split(/,|;/).map do |i| | |
509 str, bytes = i.split(":", 2).map do |x| | |
510 x.strip | |
511 end | |
512 | |
513 var = get_var_info(str, true) | |
514 | |
515 if var | |
516 if var.return_type == "***dummy***" | |
517 var.return_type = return_type | |
518 var.stripped_return_type = stripped_return_type | |
519 else | |
520 return_type = var.return_type | |
521 stripped_return_type = var.stripped_return_type | |
522 end | |
523 | |
524 struct[:members] << { :name => var.name, :bytes => bytes, :declaredType => var.return_type, :type => "", :type64 => "" } unless str.empty? || str[0] == ?# | |
525 | |
526 names = [] | |
527 | |
528 tmp = struct[:members].collect do |member| | |
529 unless names.include?(member[:name]) | |
530 names << member[:name] | |
531 member | |
532 end | |
533 end | |
534 | |
535 struct[:members] = tmp.compact | |
536 end | |
537 end | |
538 end | |
539 | |
540 header.structs << struct if body | |
541 end | |
542 end | |
543 | |
544 def functions (header, inline = false) | |
545 if inline | |
546 inline_func_re = /(inline|__inline__)\s+((__attribute__\(\([^)]*\)\)\s+)?([\w\s\*<>]+)\s*\(([^)]*)\)\s*)\{/ | |
547 res = @cpp_result.scan(inline_func_re) | |
548 res.each do |x| | |
549 x.delete_at(0) | |
550 x.delete_at(1) | |
551 end | |
552 else | |
553 skip_inline_re = /(static)?\s(inline|__inline__)[^{;]+(;|\{([^{}]*(\{[^}]+\})?)*\})\s*/ | |
554 func_re = /(^([\w\s\*<>]+)\s*\(([^)]*)\)\s*)(__attribute__[^;]+)?;/ | |
555 res = @cpp_result.gsub(skip_inline_re, '').scan(func_re) | |
556 end | |
557 | |
558 funcs = res.map do |m| | |
559 orig, base, args = m | |
560 base.sub!(/^.*extern\s/, "") | |
561 func = constant?(base) | |
562 | |
563 if func | |
564 args = args.strip.split(",").map do |i| | |
565 constant?(i) | |
566 end | |
567 | |
568 next if args.any? do |x| | |
569 x.nil? | |
570 end | |
571 | |
572 args = [] if args.size == 1 && args[0].return_type == "void" | |
573 FunctionInfo.new(func, args, orig, inline) | |
574 end | |
575 end.compact | |
576 | |
577 if inline | |
578 header.inline_functions = funcs | |
579 else | |
580 header.functions = funcs | |
581 end | |
582 end | |
583 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
584 def prepare (path) |
1 | 585 @file_content = File.read(path) |
586 @file_content.gsub!(%r{(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)}, ""); | |
587 @complete_cpp_result, @cpp_result = do_cpp(path, false, true, "") | |
588 end | |
589 | |
590 def scan (frameworks, headers) | |
591 @frameworks = frameworks | |
592 @headers = headers | |
593 | |
594 @frameworks.each do |framework| | |
595 framework.headers.each do |header| | |
596 prepare(header.path) | |
597 | |
598 imports(header) | |
599 cftypes(header) | |
600 externs(header) | |
601 constants(header) | |
602 enums(header) | |
603 structs(header) | |
604 typedefs(header) | |
605 functions(header) | |
606 functions(header, true) | |
607 defines(header) | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
608 classes(header) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
609 protocols(header) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
610 #informal_protocols(header) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
611 categories(header) |
3
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
612 function_pointers(header) |
1 | 613 end |
614 end | |
615 | |
616 @headers.each do |header| | |
617 prepare(header.path) | |
618 | |
619 imports(header) | |
620 cftypes(header) | |
621 externs(header) | |
622 constants(header) | |
623 enums(header) | |
624 structs(header) | |
625 typedefs(header) | |
626 functions(header) | |
627 functions(header, true) | |
628 defines(header) | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
629 classes(header) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
630 protocols(header) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
631 #informal_protocols(header) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
632 categories(header) |
3
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
633 function_pointers(header) |
1 | 634 end |
635 end | |
636 | |
637 def get_var_info (str, multi = false) | |
638 str.strip! | |
639 | |
640 return nil if str.empty? | |
641 | |
642 if str == "..." | |
643 VarInfo.new("...", "...", str) | |
644 else | |
645 str = "***dummy*** " + str if str[-1].chr == '*' || str.index(/\s/).nil? | |
646 tokens = multi ? str.split(',') : [str] | |
647 part = tokens.first | |
648 re = /^([^()]*)\b(\w+)\b\s*(\[[^\]]*\])*$/ | |
649 m = re.match(part) | |
650 | |
651 if m | |
652 return nil if m[1].split(/\s+/).any? do |x| | |
653 ['end', 'typedef'].include?(x) | |
654 end | |
655 | |
656 m = m.to_a[1..-1].compact.map do |i| | |
657 i.strip | |
658 end | |
659 | |
660 m[0] += m[2] if m.size == 3 | |
661 m[0] = 'void' if m[1] == 'void' | |
662 | |
663 var = begin | |
664 VarInfo.new(m[0], m[1], part) | |
665 rescue | |
666 return nil | |
667 end | |
668 | |
669 if tokens.size > 1 | |
670 [var, *tokens[1..-1].map { |x| constant?(m[0] + x.strip.sub(/^\*+/, '')) }] | |
671 else | |
672 var | |
673 end | |
674 end | |
675 end | |
676 end | |
677 | |
678 def constant? (str, multi = false) | |
679 str.strip! | |
680 return nil if str.empty? | |
681 | |
682 if str == '...' | |
683 VarInfo.new('...', '...', str) | |
684 else | |
685 str << " dummy" if str[-1].chr == '*' or str.index(/\s/).nil? | |
686 tokens = multi ? str.split(',') : [str] | |
687 part = tokens.first | |
688 re = /^([^()]*)\b(\w+)\b\s*(\[[^\]]*\])*$/ | |
689 m = re.match(part) | |
690 | |
691 if m | |
692 return nil if m[1].split(/\s+/).any? do |x| | |
693 ['end', 'typedef'].include?(x) | |
694 end | |
695 | |
696 m = m.to_a[1..-1].compact.map do |i| | |
697 i.strip | |
698 end | |
699 | |
700 m[0] += m[2] if m.size == 3 | |
701 m[0] = 'void' if m[1] == 'void' | |
702 | |
703 var = begin | |
704 VarInfo.new(m[0], m[1], part) | |
705 rescue | |
706 return nil | |
707 end | |
708 | |
709 if tokens.size > 1 | |
710 [var, *tokens[1..-1].map { |x| constant?(m[0] + x.strip.sub(/^\*+/, '')) }] | |
711 else | |
712 var | |
713 end | |
714 end | |
715 end | |
716 end | |
717 | |
718 class VarInfo | |
719 | |
720 attr_reader :name, :orig, :const, :type_modifier | |
721 attr_accessor :octype, :resolved_type, :resolved_type64, :return_type, :stripped_return_type | |
722 | |
723 def initialize (type, name, orig) | |
724 @return_type = type.clone | |
725 @name = name | |
726 @orig = orig | |
727 @const = false | |
728 @type_modifier = "" | |
729 | |
730 @return_type.gsub!(/\[[^\]]*\]/, "*") | |
731 | |
732 if @return_type =~ /\bconst\b/ | |
733 @const = true | |
734 @return_type.gsub!("const", "") | |
735 end | |
736 | |
737 if @return_type =~ /\b(in|out|inout|oneway|bycopy|byref)\b/ | |
738 | |
739 case $1 | |
740 when "in" | |
741 @type_modifier << "n" | |
742 when "out" | |
743 @type_modifier << "o" | |
744 when "inout" | |
745 @type_modifier << "N" | |
746 when "oneway" | |
747 @type_modifier << "w" | |
748 when "bycopy" | |
749 @type_modifier << "c" | |
750 when "byref" | |
751 @type_modifier << "r" | |
752 end | |
753 | |
754 @return_type.gsub!("#{$1}", "") | |
755 end | |
756 | |
757 @return_type.gsub!(/\s?\*\s?/, "*") | |
758 @return_type.gsub!(/^\(|\)$/, "") | |
759 @return_type.strip! | |
760 | |
761 t = type.gsub(/\b(__)?const\b/, "") | |
762 t.gsub!(/<[^>]*>/, '') | |
763 t.gsub!(/\b(in|out|inout|oneway|const)\b/, "") | |
764 t.gsub!(/\b__private_extern__\b/, "") | |
765 t.gsub!(/^\s*\(?\s*/, "") | |
766 t.gsub!(/\s*\)?\s*$/, "") | |
767 | |
768 raise "Empty type (was '#{type}')" if t.empty? | |
769 | |
770 @stripped_return_type = t | |
771 end | |
772 | |
3
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
773 # def function_pointer? (function_pointers) |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
774 # type = @function_pointers[@stripped_return_type] || @stripped_return_type |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
775 # @function_pointer ||= FunctionPointerInfo.new_from_type(type) |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
776 # end |
1 | 777 |
778 def <=>(x) | |
779 self.name <=> x.name | |
780 end | |
781 | |
782 def hash | |
783 @name.hash | |
784 end | |
785 | |
786 def eql? (o) | |
787 @name == o.name | |
788 end | |
789 end | |
790 | |
791 class FunctionInfo < VarInfo | |
792 | |
793 attr_reader :args, :argc | |
794 | |
795 def initialize (function, args, orig, inline = false) | |
796 super(function.return_type, function.name, orig) | |
797 | |
798 @args = args | |
799 @argc = @args.size | |
800 @variadic = false | |
801 | |
802 if @args[-1] && @args[-1].return_type == "..." | |
803 @argc -= 1 | |
804 @variadic = true | |
805 @args.pop | |
806 end | |
807 | |
808 @inline = inline | |
809 self | |
810 end | |
811 | |
812 def variadic? | |
813 @variadic | |
814 end | |
815 | |
816 def inline? | |
817 @inline | |
818 end | |
819 end | |
820 | |
821 class FunctionPointerInfo < FunctionInfo | |
822 def initialize (return_type, arg_types, orig) | |
823 args = arg_types.map do |x| | |
824 VarInfo.new(x, "", "") | |
825 end | |
826 | |
827 super(VarInfo.new(return_type, "", ""), args, orig) | |
828 end | |
829 | |
830 def self.new_from_type (type) | |
831 @cache ||= {} | |
832 info = @cache[type] | |
833 | |
834 return info if info | |
835 | |
836 tokens = type.split(/\(\*\)/) | |
837 return nil if tokens.size != 2 | |
838 | |
839 return_type = tokens.first.strip | |
840 rest = tokens.last.sub(/^\s*\(\s*/, "").sub(/\s*\)\s*$/, "") | |
841 | |
842 arg_types = rest.split(/,/).map do |x| | |
843 x.strip | |
844 end | |
845 | |
846 @cache[type] = self.new(return_type, arg_types, type) | |
847 end | |
848 end | |
849 | |
850 class MethodInfo < FunctionInfo | |
851 | |
852 attr_reader :selector | |
853 | |
854 def initialize (method, selector, is_class, args, orig) | |
855 super(method, args, orig) | |
856 | |
857 @selector = selector | |
858 @is_class = is_class | |
859 self | |
860 end | |
861 | |
862 def class_method? | |
863 @is_class | |
864 end | |
865 | |
866 def <=>(o) | |
867 @selector <=> o.selector | |
868 end | |
869 | |
870 def hash | |
871 @selector.hash | |
872 end | |
873 | |
874 def eql? (o) | |
875 @selector == o.selector | |
876 end | |
877 end | |
878 | |
879 def do_cpp (path, fails_on_error = true, do_64 = true, flags = "") | |
880 f_on = false | |
881 err_file = '/tmp/.cpp.err' | |
882 cpp_line = "#{CPP} #{CPPFLAGS} #{flags} #{do_64 ? '-D__LP64__' : ''} \"#{path}\" 2>#{err_file}" | |
883 complete_result = `#{cpp_line}` | |
884 | |
885 if $?.to_i != 0 && fails_on_error | |
886 $stderr.puts File.read(err_file) | |
887 File.unlink(err_file) | |
888 raise "#{CPP} returned #{$?.to_int/256} exit status\nline was: #{cpp_line}" | |
889 end | |
890 | |
891 result = complete_result.select do |s| | |
892 # First pass to only grab non-empty lines and the pre-processed lines | |
893 # only from the target header (and not the entire pre-processing result). | |
894 next if s.strip.empty? | |
895 m = %r{^#\s*\d+\s+"([^"]+)"}.match(s) | |
896 f_on = (File.basename(m[1]) == File.basename(path)) if m | |
897 f_on | |
898 end.select do |s| | |
899 # Second pass to ignore all pro-processor comments that were left. | |
900 /^#/.match(s) == nil | |
901 end.join | |
902 | |
903 File.unlink(err_file) | |
904 return [complete_result, result] | |
905 end | |
906 end | |
907 | |
908 class DStepGenerator | |
909 | |
910 VERSION = 1.0 | |
911 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
912 attr_accessor :out_file, :scaner, :code_to_inject, :dependencies_switch |
1 | 913 |
914 def initialize | |
915 @do_64bit = false | |
916 @frameworks = [] | |
917 @framework_paths = [] | |
918 @headers = [] | |
919 @import_directives = "#import <Foundation/Foundation.h>\n" | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
920 #@informal_protocols = [] |
1 | 921 @classes = [] |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
922 @protocols = [] |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
923 @categories = [] |
1 | 924 @scaner = HeaderScaner.new |
925 @scaner.do_64bit = @do_64bit | |
926 @umbrella_framework = nil | |
927 @handled_dependencies = [] | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
928 @dependencies_switch = false |
1 | 929 |
930 # link to foundation framework by default | |
931 @compiler_flags = "-framework Foundation" | |
932 end | |
933 | |
934 def do_64bit | |
935 return @do_64bit | |
936 end | |
937 | |
938 def do_64bit= (do_64bit) | |
939 @do_64bit = do_64bit | |
940 @scaner.do_64bit = do_64bit | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
941 end |
1 | 942 |
943 def collect | |
944 scaner.scan(@frameworks, @headers) | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
945 @classes = scaner.get_classes |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
946 @protocols = scaner.get_protocols |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
947 @categories = scaner.get_categories |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
948 #@informal_protocols = scaner.get_informal_protocols |
1 | 949 end |
950 | |
951 def handle_framework (framework, sub_framework = false, parent_framework = nil) | |
952 val = framework.name | |
953 path = framework_path(val) | |
954 | |
955 raise "Can't locate framework '#{val}'" if path.nil? | |
956 @framework_paths << File.dirname(path) | |
957 raise "Can't find framework '#{val}'" if path.nil? | |
958 | |
959 parent_path, name = path.scan(/^(.+)\/(\w+)\.framework\/?$/)[0] | |
960 | |
961 if @private | |
962 headers_path = File.join(path, "PrivateHeaders") | |
963 raise "Can't locate private framework headers at '#{headers_path}'" unless File.exist?(headers_path) | |
964 | |
965 headers = Dir.glob(File.join(headers_path, "**", "*.h")) | |
966 public_headers_path = File.join(path, "Headers") | |
967 public_headers = if File.exist?(public_headers_path) | |
968 HeaderScaner::CPPFLAGS << " -I#{public_headers_path} " | |
969 Dir.glob(File.join8(headers_path, "**", "*.h")) | |
970 else | |
971 [] | |
972 end | |
973 else | |
974 headers_path = File.join(path, "Headers") | |
975 raise "Can't locate public framework headers at '#{headers_path}'" unless File.exist?(headers_path) | |
976 public_headers = headers = Dir.glob(File.join(headers_path, "**", "*.h")) | |
977 end | |
978 | |
979 # We can't just "#import <x/x.h>" as the main Framework header might not include _all_ headers. | |
980 # So we are tricking this by importing the main header first, then all headers. | |
981 header_basenames = (headers | public_headers).map do |x| | |
982 x.sub(/#{headers_path}\/*/, "") | |
983 end | |
984 | |
985 if idx = header_basenames.index("#{name}.h") | |
986 header_basenames.delete_at(idx) | |
987 header_basenames.unshift("#{name}.h") | |
988 end | |
989 | |
990 if sub_framework | |
991 pp = framework_path(parent_framework.name) | |
992 tmp, pname = pp.scan(/^(.+)\/(\w+)\.framework\/?$/)[0] | |
993 @import_directives << "#import <#{pname}/#{pname}.h>" | |
994 else | |
995 @import_directives << header_basenames.map do |x| | |
996 "#import <#{name}/#{x}>" | |
997 end.join("\n") | |
998 end | |
999 | |
1000 @import_directives << "\n" | |
1001 | |
1002 @compiler_flags ||= "-F\"#{parent_path}\" -framework #{name}" | |
1003 @cpp_flags ||= "" | |
1004 @cpp_flags << "-F\"#{parent_path}\" " | |
1005 | |
1006 headers.each do |header| | |
1007 header_file = HeaderFile.new | |
1008 header_file.path = header | |
1009 header_file.name = File.basename(header, File.extname(header)) | |
1010 header_file.framework = framework.name | |
1011 | |
1012 framework.headers << header_file | |
1013 end | |
1014 | |
1015 # Memorize the dependencies. | |
1016 @dependencies = DStepGenerator.dependencies_of_framework(path) | |
1017 | |
1018 handle_sub_frameworks(framework) | |
1019 end | |
1020 | |
1021 def handle_sub_frameworks (framework) | |
1022 path = framework_path(framework.name) | |
1023 frameworks_path = File.join(path, "Frameworks") | |
1024 frameworks = Dir.glob(File.join(frameworks_path, "*.framework")) | |
1025 | |
1026 frameworks.each do |f| | |
1027 add_framework(f, true, framework) | |
1028 end | |
1029 end | |
1030 | |
1031 def framework_path (val) | |
1032 return val if File.exist?(val) | |
1033 | |
1034 val += ".framework" unless /\.framework$/.match(val) | |
1035 paths = ["/System/Library/Frameworks", "/Library/Frameworks", "#{ENV['HOME']}/Library/Frameworks"] | |
1036 paths << "/System/Library/PrivateFrameworks" if @private | |
1037 | |
1038 paths.each do |dir| | |
1039 path = File.join(dir, val) | |
1040 return path if File.exist?(path) | |
1041 end | |
1042 | |
1043 return nil | |
1044 end | |
1045 | |
1046 def umbrella_framework (val) | |
1047 | |
1048 unless val == "" || val.nil? | |
1049 @umbrella_framework = val | |
1050 | |
1051 unless @compiler_flags.nil? || @compiler_flags == "" | |
1052 i = @compiler_flags.index("-framework") | |
1053 @compiler_flags = @compiler_flags[0 .. i - 1] + "-framework #{val}" | |
1054 else | |
1055 @compiler_flags = "-framework #{val}" | |
1056 end | |
1057 end | |
1058 end | |
1059 | |
1060 def self.dependencies_of_framework (path) | |
1061 @dependencies ||= {} | |
1062 name = File.basename(path, ".framework") | |
1063 path = File.join(path, name) | |
1064 deps = @dependencies[path] | |
1065 | |
1066 if deps.nil? | |
1067 deps = `otool -L "#{path}"`.scan(/\t([^\s]+)/).map do |m| | |
1068 dpath = m[0] | |
1069 next if File.basename(dpath) == name | |
1070 next if dpath.include?("PrivateFrameworks") | |
1071 next unless dpath.sub!(/\.framework\/Versions\/\w+\/\w+$/, "") | |
1072 dpath + ".framework" | |
1073 end.compact | |
1074 | |
1075 @dependencies[path] = deps | |
1076 end | |
1077 | |
1078 return deps | |
1079 end | |
1080 | |
1081 def compile_and_execute (code, enable_64bit = false, clean_when_fail = false) | |
1082 compiler_line = "gcc " | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1083 src = File.new(unique_tmp_path("src", ".m"), "w") |
1 | 1084 src << code |
1085 src.close | |
1086 | |
1087 arch_flag = if enable_64bit | |
1088 "-arch x86_64 -arch ppc64" | |
1089 else | |
1090 "-arch i386 -arch ppc" | |
1091 end | |
1092 | |
1093 compiler_line << arch_flag | |
1094 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1095 bin = unique_tmp_path "bin", "" |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1096 log = unique_tmp_path "log", ".txt" |
1 | 1097 |
1098 line = "#{compiler_line} -o #{bin} #{src.path} #{@compiler_flags} 2>#{log}" | |
1099 | |
1100 unless system(line) | |
1101 msg = "Cannot compile Objective-C code ...aborting\nCommand was: #{line}\n\nLog:\n#{File.read(log)}\n\n" | |
1102 $stderr << msg | |
1103 File.delete src.path if clean_when_fail | |
1104 raise msg | |
1105 end | |
1106 | |
1107 result = `#{bin}` | |
1108 | |
1109 unless $?.success? | |
1110 raise "Cannot execute compiled Objective-C code ... aborting\nCommand was: #{line}\nBinary is: #{bin}" | |
1111 end | |
1112 | |
1113 File.delete bin | |
1114 File.delete log | |
1115 File.delete src.path | |
1116 | |
1117 return result | |
1118 end | |
1119 | |
1120 def unique_tmp_path (base, extension = "", dir = Dir.tmpdir) | |
1121 i = 0 | |
1122 loop do | |
1123 path = File.join(dir, "#{base}-#{Process.pid}-#{i}#{extension}") | |
1124 return path unless File.exists?(path) | |
1125 i += 1 | |
1126 end | |
1127 end | |
1128 | |
1129 def add_header (path) | |
1130 header = HeaderFile.new | |
1131 header.path = path | |
1132 header.name = File.basename(path, File.extname(path)) | |
1133 @import_directives << "#include <#{path}>\n" | |
1134 @headers << header | |
1135 end | |
1136 | |
1137 def add_framework (name, sub_framework = false, parent_framework = nil) | |
1138 framework = Framework.new | |
1139 framework.name = name | |
1140 handle_framework(framework, sub_framework, parent_framework) | |
1141 #@import_directives << "#import <#{framework.name}/#{framework.name}.h>\n" | |
1142 @frameworks << framework | |
1143 end | |
1144 | |
1145 def collect_header_types (header, enable_64bit) | |
1146 types = [] | |
1147 | |
1148 header.cftypes.each do |cftype| | |
1149 types << cftype[:type] | |
1150 end | |
1151 | |
1152 header.constants.each do |constant| | |
1153 types << constant.stripped_return_type | |
1154 end | |
1155 | |
1156 header.structs.each do |struct| | |
1157 types << struct[:name] | |
1158 | |
1159 struct[:members].each do |member| | |
1160 types << member[:declaredType] | |
1161 end | |
1162 end | |
1163 | |
1164 header.typedefs.each do |typedef| | |
1165 types << typedef.stripped_return_type | |
1166 end | |
1167 | |
1168 header.functions.each do |function| | |
1169 types << function.stripped_return_type | |
1170 | |
1171 function.args.each do |arg| | |
1172 types << arg.stripped_return_type | |
1173 end | |
1174 end | |
1175 | |
3
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1176 header.function_pointers.each do |fp, value| |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1177 types << value.return_type |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1178 |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1179 value.args.each do |arg| |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1180 types << arg.type |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1181 end |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1182 end |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1183 |
1 | 1184 types |
1185 end | |
1186 | |
1187 def collect_classes_types (enable_64bit) | |
1188 types = [] | |
1189 | |
1190 @classes.each do |clazz, value| | |
1191 value.methods.each do |method| | |
1192 types << method.stripped_return_type | |
1193 | |
1194 method.args.each do |arg| | |
1195 types << arg.stripped_return_type | |
1196 end | |
1197 end | |
1198 end | |
1199 | |
1200 types | |
1201 end | |
1202 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1203 def collect_protocols_types (enable_64bit) |
1 | 1204 types = [] |
1205 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1206 @protocols.each do |protocol, value| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1207 value.methods.each do |method| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1208 types << method.stripped_return_type |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1209 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1210 method.args.each do |arg| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1211 types << arg.stripped_return_type |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1212 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1213 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1214 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1215 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1216 types |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1217 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1218 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1219 # def collect_informal_protocols_types (enable_64bit) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1220 # types = [] |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1221 # |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1222 # @informal_protocols.each do |name, methods| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1223 # methods.each do |method| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1224 # types << method.stripped_return_type |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1225 # |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1226 # method.args.each do |arg| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1227 # types << arg.stripped_return_type |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1228 # end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1229 # end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1230 # end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1231 # |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1232 # types |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1233 # end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1234 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1235 def collect_categories_types (enable_64bit) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1236 types = [] |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1237 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1238 @categories.each do |category, value| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1239 value.methods.each do |method| |
1 | 1240 types << method.stripped_return_type |
1241 | |
1242 method.args.each do |arg| | |
1243 types << arg.stripped_return_type | |
1244 end | |
1245 end | |
1246 end | |
1247 | |
1248 types | |
1249 end | |
1250 | |
1251 def resolve_header_types (header, enable_64bit, resolved_types, x) | |
1252 i = x | |
1253 | |
1254 header.cftypes.each do |cftype| | |
1255 cftype[enable_64bit ? :type64 : :type] = resolved_types[i] | |
1256 i += 1 | |
1257 end | |
1258 | |
1259 header.constants.each do |constant| | |
1260 constant.resolved_type = resolved_types[i] unless enable_64bit | |
1261 constant.resolved_type64 = resolved_types[i] if enable_64bit | |
1262 i += 1 | |
1263 end | |
1264 | |
1265 header.structs.each do |struct| | |
1266 struct[enable_64bit ? :type64 : :type] = resolved_types[i] | |
1267 i += 1 | |
1268 | |
1269 struct[:members].each do |member| | |
1270 member[enable_64bit ? :type64 : :type] = resolved_types[i] | |
1271 i += 1 | |
1272 end | |
1273 end | |
1274 | |
1275 header.typedefs.each do |typedef| | |
1276 typedef.resolved_type = resolved_types[i] unless enable_64bit | |
1277 typedef.resolved_type64 = resolved_types[i] if enable_64bit | |
1278 i += 1 | |
1279 end | |
1280 | |
1281 header.functions.each do |function| | |
1282 function.resolved_type = resolved_types[i] unless enable_64bit | |
1283 function.resolved_type64 = resolved_types[i] if enable_64bit | |
1284 i += 1 | |
1285 | |
1286 function.args.each do |arg| | |
1287 arg.resolved_type = resolved_types[i] unless enable_64bit | |
1288 arg.resolved_type64 = resolved_types[i] if enable_64bit | |
1289 i += 1 | |
1290 end | |
1291 end | |
1292 | |
3
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1293 header.function_pointers.each do |fp, value| |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1294 value.resolved_type = resolved_types[i] unless enable_64bit |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1295 value.resolved_type64 = resolved_types[i] if enable_64bit |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1296 i += 1 |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1297 |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1298 value.args.each do |arg| |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1299 arg.resolved_type = resolved_types[i] unless enable_64bit |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1300 arg.resolved_type64 = resolved_types[i] if enable_64bit |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1301 i += 1 |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1302 end |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1303 end |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1304 |
1 | 1305 i |
1306 end | |
1307 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1308 def resolve_classes_types (enable_64bit, resolved_types, x) |
1 | 1309 i = x |
1310 | |
1311 @classes.each do |clazz, value| | |
1312 value.methods.each do |method| | |
1313 method.resolved_type = resolved_types[i] unless enable_64bit | |
1314 method.resolved_type64 = resolved_types[i] if enable_64bit | |
1315 i += 1 | |
1316 | |
1317 method.args.each do |arg| | |
1318 arg.resolved_type = resolved_types[i] unless enable_64bit | |
1319 arg.resolved_type64 = resolved_types[i] if enable_64bit | |
1320 i += 1 | |
1321 end | |
1322 end | |
1323 end | |
1324 | |
1325 i | |
1326 end | |
1327 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1328 def resolve_protocols_types (enable_64bit, resolved_types, x) |
1 | 1329 i = x |
1330 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1331 @protocols.each do |protocol, value| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1332 value.methods.each do |method| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1333 method.resolved_type = resolved_types[i] unless enable_64bit |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1334 method.resolved_type64 = resolved_types[i] if enable_64bit |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1335 i += 1 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1336 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1337 method.args.each do |arg| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1338 arg.resolved_type = resolved_types[i] unless enable_64bit |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1339 arg.resolved_type64 = resolved_types[i] if enable_64bit |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1340 i += 1 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1341 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1342 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1343 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1344 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1345 i |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1346 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1347 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1348 # def resolve_informal_protocols_types (enable_64bit, resolved_types, x) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1349 # i = x |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1350 # |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1351 # @informal_protocols.each do |name, methods| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1352 # methods.each do |method| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1353 # method.resolved_type = resolved_types[i] unless enable_64bit |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1354 # method.resolved_type64 = resolved_types[i] if enable_64bit |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1355 # i += 1 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1356 # |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1357 # method.args.each do |arg| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1358 # arg.resolved_type = resolved_types[i] unless enable_64bit |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1359 # arg.resolved_type64 = resolved_types[i] if enable_64bit |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1360 # i += 1 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1361 # end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1362 # end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1363 # end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1364 # |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1365 # i |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1366 # end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1367 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1368 def resolve_categories_types (enable_64bit, resolved_types, x) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1369 i = x |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1370 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1371 @categories.each do |category, value| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1372 value.methods.each do |method| |
1 | 1373 method.resolved_type = resolved_types[i] unless enable_64bit |
1374 method.resolved_type64 = resolved_types[i] if enable_64bit | |
1375 i += 1 | |
1376 | |
1377 method.args.each do |arg| | |
1378 arg.resolved_type = resolved_types[i] unless enable_64bit | |
1379 arg.resolved_type64 = resolved_types[i] if enable_64bit | |
1380 i += 1 | |
1381 end | |
1382 end | |
1383 end | |
1384 | |
1385 i | |
1386 end | |
1387 | |
1388 def resolve_types (enable_64bit = false) | |
1389 code = "#include <stdio.h>\n" | |
1390 code << "#import <objc/objc-class.h>\n" | |
1391 | |
1392 # @dependencies.each do |f| | |
1393 # # get the framework name | |
1394 # i = f.index('.') | |
1395 # str = f[0 ... i] | |
1396 # i = str.rindex('/') | |
1397 # str = str[i + 1 .. -1] | |
1398 # | |
1399 # code << "#import <#{str}/#{str}.h>\n" | |
1400 # end | |
1401 | |
1402 code << @import_directives | |
1403 types = [] | |
1404 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1405 code << @code_to_inject + "\n" unless @code_to_inject.nil? |
1 | 1406 |
1407 @frameworks.each do |framework| | |
1408 framework.headers.each do |header| | |
1409 | |
1410 header.imports do |import| | |
1411 code << "#import <#{import}.h>\n" | |
1412 end | |
1413 | |
1414 types << collect_header_types(header, enable_64bit) | |
1415 end | |
1416 end | |
1417 | |
1418 @headers.each do |header| | |
1419 types << collect_header_types(header, enable_64bit) | |
1420 end | |
1421 | |
1422 types << collect_classes_types(enable_64bit) | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1423 types << collect_protocols_types(enable_64bit) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1424 #types << collect_informal_protocols_types(enable_64bit) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1425 types << collect_categories_types(enable_64bit) |
1 | 1426 |
1427 code << "int main ()\n{\n" | |
1428 types.flatten! | |
1429 | |
1430 types.each do |type| | |
1431 code << ' printf("%s\n", ' + "@encode(#{type}));\n" | |
1432 end | |
1433 | |
1434 code << " return 0;\n}" | |
1435 | |
1436 resolved_types = [] | |
1437 | |
1438 compile_and_execute(code, enable_64bit).split("\n").each do |line| | |
1439 resolved_types << line | |
1440 end | |
1441 | |
1442 i = 0 | |
1443 | |
1444 @frameworks.each do |framework| | |
1445 framework.headers.each do |header| | |
1446 i = resolve_header_types(header, enable_64bit, resolved_types, i) | |
1447 end | |
1448 end | |
1449 | |
1450 @headers.each do |header| | |
1451 i = resolve_header_types(header, enable_64bit, resolved_types, i) | |
1452 end | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1453 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1454 i = resolve_classes_types(enable_64bit, resolved_types, i) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1455 i = resolve_protocols_types(enable_64bit, resolved_types, i) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1456 #i = resolve_informal_protocols_types(enable_64bit, resolved_types, i) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1457 i = resolve_categories_types(enable_64bit, resolved_types, i) |
1 | 1458 end |
1459 | |
1460 def generate_header (xml, header) | |
1461 xml.file :name => header.name do | |
1462 header.imports.each do |import| | |
1463 xml.import import | |
1464 end | |
1465 | |
1466 header.defines.each do |define| | |
1467 xml.define define | |
1468 end | |
1469 | |
1470 header.cftypes.each do |cftype| | |
1471 xml.cftype cftype | |
1472 end | |
1473 | |
1474 header.constants.each do |constant| | |
1475 xml.constant :name => constant.name, :declaredType => constant.return_type, :type => constant.resolved_type, :type64 => constant.resolved_type64, :const => constant.const | |
1476 end | |
1477 | |
1478 header.enums.each do |enum| | |
1479 xml.enum :name => enum[:name] do | |
1480 enum[:members].each do |member| | |
1481 xml.member member | |
1482 end | |
1483 end | |
1484 end | |
1485 | |
1486 header.structs.each do |struct| | |
1487 xml.struct :name => struct[:name], :type => struct[:type], :type64 => struct[:type64] do | |
1488 struct[:members].each do |member| | |
1489 xml.member member | |
1490 end | |
1491 end | |
1492 end | |
1493 | |
1494 header.typedefs.each do |typedef| | |
1495 xml.typedef :name => typedef.name, :declaredType => typedef.return_type, :type => typedef.resolved_type, :type64 => typedef.resolved_type64, :const => typedef.const | |
1496 end | |
1497 | |
1498 header.functions.each do |function| | |
1499 xml.function :name => function.name, :inline => function.inline?, :variadic => function.variadic? do | |
1500 function.args.each do |arg| | |
1501 xml.arg :name => arg.name, :declaredType => arg.return_type, :type => arg.resolved_type, :type64 => arg.resolved_type64, :const => arg.const, :typeModifier => arg.type_modifier | |
1502 end | |
1503 | |
1504 xml.returnValue :declaredType => function.return_type, :type => function.resolved_type, :type64 => function.resolved_type64, :const => function.const, :typeModifier => function.type_modifier | |
1505 end | |
1506 end | |
3
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1507 |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1508 header.function_pointers.each do |fp, value| |
4
5a1a6afbfe3a
Added support for function pointers and variadic function pointers to dgen and dstepgen
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
1509 xml.functionPointer :name => fp, :variadic => value.variadic do |
3
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1510 value.args.each do |arg| |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1511 xml.arg :declaredType => arg.type, :type => arg.resolved_type, :type64 => arg.resolved_type64, :const => arg.const |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1512 end |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1513 |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1514 xml.returnValue :declaredType => value.return_type, :type => value.resolved_type, :type64 => value.resolved_type64, :const => value.const |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1515 end |
d0162d8ca0f2
Added support for function pointers in dstepgen
Jacob Carlborg <doob@me.com>
parents:
2
diff
changeset
|
1516 end |
1 | 1517 end |
1518 end | |
1519 | |
1520 def generate_classes (xml) | |
1521 @classes.each do |clazz, value| | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1522 xml.class :name => clazz, :parent => value.parent, :protocols => value.protocols, :file => value.file do |
1 | 1523 value.methods.each do |method| |
1524 xml.method :selector => method.selector, :classMethod => method.class_method?, :variadic => method.variadic? do | |
1525 method.args.each do |arg| | |
1526 xml.arg :name => arg.name, :declaredType => arg.return_type, :type => arg.resolved_type, :type64 => arg.resolved_type64, :const => arg.const, :typeModifier => arg.type_modifier | |
1527 end | |
1528 | |
1529 xml.returnValue :declaredType => method.return_type, :type => method.resolved_type, :type64 => method.resolved_type64, :const => method.const, :typeModifier => method.type_modifier | |
1530 end | |
1531 end | |
1532 end | |
1533 end | |
1534 end | |
1535 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1536 def generate_protocols (xml) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1537 @protocols.each do |protocol, value| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1538 xml.protocol :name => protocol, :parent => value.parent, :protocols => value.protocols, :file => value.file do |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1539 value.methods.each do |method| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1540 xml.method :selector => method.selector, :classMethod => method.class_method?, :variadic => method.variadic? do |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1541 method.args.each do |arg| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1542 xml.arg :name => arg.name, :declaredType => arg.return_type, :type => arg.resolved_type, :type64 => arg.resolved_type64, :const => arg.const, :typeModifier => arg.type_modifier |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1543 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1544 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1545 xml.returnValue :declaredType => method.return_type, :type => method.resolved_type, :type64 => method.resolved_type64, :const => method.const, :typeModifier => method.type_modifier |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1546 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1547 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1548 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1549 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1550 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1551 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1552 # def generate_informal_protocols (xml) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1553 # @informal_protocols.each do |name, methods| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1554 # xml.informalProtocol :name => name do |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1555 # methods.each do |method| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1556 # xml.method :selector => method.selector, :classMethod => method.class_method?, :variadic => method.variadic? do |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1557 # method.args.each do |arg| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1558 # xml.arg :name => arg.name, :declaredType => arg.return_type, :type => arg.resolved_type, :type64 => arg.resolved_type64, :const => arg.const, :typeModifier => arg.type_modifier |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1559 # end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1560 # |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1561 # xml.returnValue :declaredType => method.return_type, :type => method.resolved_type, :type64 => method.resolved_type64, :const => method.const, :typeModifier => method.type_modifier |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1562 # end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1563 # end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1564 # end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1565 # end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1566 # end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1567 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1568 def generate_categories (xml) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1569 @categories.each do |category, value| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1570 xml.category :name => category.name, :class => category.class, :parent => value.parent, :protocols => value.protocols, :file => value.file do |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1571 value.methods.each do |method| |
1 | 1572 xml.method :selector => method.selector, :classMethod => method.class_method?, :variadic => method.variadic? do |
1573 method.args.each do |arg| | |
1574 xml.arg :name => arg.name, :declaredType => arg.return_type, :type => arg.resolved_type, :type64 => arg.resolved_type64, :const => arg.const, :typeModifier => arg.type_modifier | |
1575 end | |
1576 | |
1577 xml.returnValue :declaredType => method.return_type, :type => method.resolved_type, :type64 => method.resolved_type64, :const => method.const, :typeModifier => method.type_modifier | |
1578 end | |
1579 end | |
1580 end | |
1581 end | |
1582 end | |
1583 | |
1584 def generate | |
1585 resolve_types | |
1586 resolve_types(true) if @do_64bit | |
1587 | |
1588 file = STDOUT if @out_file == nil | |
1589 file = File.open @out_file, "w" unless @out_file == nil | |
1590 | |
1591 xml = XmlMarkup.new(:target => file, :indent => 4) | |
1592 xml.instruct! | |
1593 | |
1594 xml.dstep :xmlns => "http://www.dsource.org/projects/dstep", "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation" => "http://www.dsource.org/projects/dstep/trunk/scripts/dstep.xsd" do | |
1595 @frameworks.each do |framework| | |
1596 xml.framework :name => framework.name do | |
1597 framework.headers.each do |header| | |
1598 generate_header(xml, header) | |
1599 end | |
1600 end | |
1601 end | |
1602 | |
1603 @headers.each do |header| | |
1604 generate_header(xml, header) | |
1605 end | |
1606 | |
1607 generate_classes(xml) | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1608 generate_protocols(xml) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1609 #generate_informal_protocols(xml) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1610 generate_categories(xml) |
1 | 1611 end |
1612 | |
1613 file.close unless file == STDOUT | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1614 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1615 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1616 def write_dependencies |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1617 file = STDOUT if @out_file == nil |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1618 file = File.open @out_file, "w" unless @out_file == nil |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1619 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1620 @dependencies.each do |dep| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1621 file << dep + "\n" |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1622 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1623 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1624 file.close unless file == STDOUT |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1625 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1626 exit 0 |
1 | 1627 end |
1628 end | |
1629 | |
1630 def die (*msg) | |
1631 $stderr.puts msg | |
1632 exit 1 | |
1633 end | |
1634 | |
1635 if __FILE__ == $0 | |
1636 dstep_gen = DStepGenerator.new | |
1637 | |
1638 OptionParser.new do |opts| | |
1639 opts.banner = "Usage: #{File.basename(__FILE__)} [options] <headers...>" | |
1640 opts.separator "" | |
1641 opts.separator "Options:" | |
1642 | |
1643 opts.on("-f", "--framework FRAMEWORK", "Generate metadata for the given framework.") do |opt| | |
1644 dstep_gen.add_framework(opt) | |
1645 end | |
1646 | |
1647 opts.on("-u", "--umbrella FRAMEWORK", "Link againts the given umbrella framework.") do |opt| | |
1648 dstep_gen.umbrella_framework(opt) | |
1649 end | |
1650 | |
1651 opts.on(nil, "--64-bit", "Write 64-bit annotations.") do | |
1652 dstep_gen.do_64bit = true | |
1653 end | |
1654 | |
1655 opts.on("-o", "--output FILE", "Write output to the given file.") do |opt| | |
1656 die "Output file can't be specified more than once" if dstep_gen.out_file | |
1657 dstep_gen.out_file = opt | |
1658 end | |
1659 | |
1660 # opts.on("-d", "--output-dir PATH", "Write ouptut to the given paht, use this with the --framework option") do |opt| | |
1661 # die "Output directory can't be specified more than once" if dstep_gen.out_dir | |
1662 # dstep_gen.out_dir = opt | |
1663 # end | |
1664 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1665 opts.on("-c", "--code CODE", "Add code") do |opt| |
1 | 1666 dstep_gen.code_to_inject = opt |
1667 end | |
1668 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1669 opts.on("-d", "--dependencies", "Write framework dependencies and exit") do |opt| |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1670 dstep_gen.dependencies_switch = true |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1671 end |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1672 |
1 | 1673 help_msg = "Use the `-h' flag or for help." |
1674 | |
1675 opts.on("-h", "--help", "Show this message.") do | |
1676 puts opts, help_msg | |
1677 exit | |
1678 end | |
1679 | |
1680 opts.on('-v', '--version', 'Show version.') do | |
1681 puts DStepGenerator::VERSION | |
1682 exit | |
1683 end | |
1684 | |
1685 opts.separator "" | |
1686 | |
1687 if ARGV.empty? | |
1688 die opts.banner | |
1689 else | |
1690 #begin | |
1691 opts.parse!(ARGV) | |
1692 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1693 dstep_gen.write_dependencies if dstep_gen.dependencies_switch |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1694 |
1 | 1695 ARGV.each do |header| |
1696 dstep_gen.add_header(header) | |
1697 end | |
1698 | |
1699 dstep_gen.collect | |
1700 dstep_gen.generate | |
1701 # rescue => e | |
1702 # msg = e.message | |
1703 # msg = "Internal error" if msg.empty? | |
1704 # | |
1705 # die msg, opts.banner, help_msg | |
1706 # end | |
1707 end | |
1708 end | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
1709 end |