-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate.rb
453 lines (370 loc) · 13.1 KB
/
generate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
if ENV['OS'] == 'Windows_NT'
raise "This script does not work on Windows."
end
def compact_header(header)
name = header[0]
return if name.start_with? "additions/"
external_path = header[1] ? header[1] : "SDL/SDL2/include"
puts "EX = #{external_path}"
style = "{ColumnLimit: 100000, PointerAlignment: Left, SpacesBeforeTrailingComments: 2, AlignTrailingComments: false, AllowShortFunctionsOnASingleLine: None, ContinuationIndentWidth: 2, AlignOperands: DontAlign, AlignAfterOpenBracket: DontAlign}"
puts "Processing #{name}..."
# Thanks to oprypin for the formatting commands!
# Source: https://gist.github.com/oprypin/62c1d428453e21952d31ca59560507c9
system("mkdir -p output")
system("wget -nc https://raw.githubusercontent.com/libsdl-org/#{external_path}/#{name}.h -O output/#{name}.h")
system("gcc -fpreprocessed -dD -E -P output/#{name}.h > output/#{name}1.h")
system("clang-format output/#{name}1.h -style=\"#{style}\" > output/#{name}2.h")
end
def underscore_string(str)
temp_str = str.gsub(/[A-Z]+/, "_\\0").downcase.gsub("__", "_")
temp_str[0] == "_" ? temp_str[1..-1] : temp_str
end
def camelcase_string(str)
str.gsub(/_[a-zA-Z]/, &:upcase).gsub("_", "")
end
def filter_sdl(str)
filtered_str = str
if filtered_str == "start"
"start_point"
elsif filtered_str == "end"
"end_point"
elsif filtered_str[0..3] == "sdl_"
filtered_str[4..-1]
elsif filtered_str[0..3] == "SDL_"
filtered_str[4..-1]
elsif filtered_str[0..2] == "SDL"
filtered_str[3..-1]
else
filtered_str
end
end
def filter_sdl_const(str)
if str[0..3] == "SDL_"
str[4..-1]
elsif str[0..2] == "SDL"
str[3..-1]
else
str
end
end
def type_filter(name)
filters = [
["Point", "§1"], # The 'int' in 'Point' would be parsed, so we want to mask it for now
["Hint", "§2"],
["const ", ""],
["char*", "LibC::Char*"],
["void", "Void"],
["float", "LibC::Float"],
["double", "LibC::Double"],
["unsigned int", "LibC::UInt"],
["size_t", "LibC::SizeT"],
["long", "LibC::Long"],
["Uint64", "UInt64"],
["Uint32", "UInt32"],
["Uint16", "UInt16"],
["Uint8", "UInt8"],
["Sint64", "Int64"],
["Sint32", "Int32"],
["Sint16", "Int16"],
["Sint8", "Int8"],
["int64", "Int64"],
["int32", "Int32"],
["int16", "Int16"],
["int8", "Int8"],
["int", "LibC::Int"],
["char", "LibC::Char"],
["SDL_bool", "SBool"],
["FILE", "Void"], # It is a bit of cheating, but you should rarely use this anyway
["§1", "Point"], # Now we can demask it again
["§2", "Hint"]
]
soft_filter(filters.inject(name) {|text, filter| text.gsub(filter[0], filter[1])})
end
# This filter ignores predefined types
def soft_filter(name)
filter_sdl(camelcase_string(name))
end
def should_struct_be_excluded?(name)
filters = [
"SDL_AudioCVT",
"SDL_GameControllerButtonBind",
"SDL_VirtualJoystickDesc",
"hat",
"SDL_RWops",
"mem",
"hidden",
"androidio",
"SDL_WindowShapeMode",
"WindowShapeMode"
]
filters.index(name)
end
def should_constant_be_excluded?(name)
filters = [
"SDL_AUDIOCVT_PACKED",
"TTF_MAJOR_VERSION",
"TTF_MINOR_VERSION",
"TTF_PATCHLEVEL"
]
filters.index(name)
end
def should_function_be_excluded?(name)
filters = [
"Mix_SetPostMix",
"Mix_HookMusic",
"Mix_HookMusicFinished",
"Mix_ChannelFinished",
"Mix_EachSoundFont"
]
filters.index(name)
end
def process_constant(constant)
constant.gsub(/(\d+)[uU]/, "\\1").gsub(/^([\d\.]+)[fF]$/, "\\1").gsub("\\x1B", "\\e").gsub("\\x7F", "\\u007F").gsub("SDL_", "").gsub("WINDOWPOS_UNDEFINED_DISPLAY(0)", "(LibSDL::WINDOWPOS_UNDEFINED_MASK | 0)").gsub("WINDOWPOS_CENTERED_DISPLAY(0)", "(LibSDL::WINDOWPOS_CENTERED_MASK | 0)").gsub(/VERSIONNUM(([\S]+), ([\S]+), ([\S]+))/, "((\\2)*1000 + (\\3)*100 + (\\4))")
end
$constant_cache = {}
def get_all_functions(filename)
lines = nil
final_functions = []
File.open(filename, "r") do |f|
lines = f.readlines
end
lines.each do |line|
function_matches = line.match(/extern DECLSPEC ([^;]*);/)
if function_matches
function_part = function_matches[1].gsub("SDL_PRINTF_FORMAT_STRING", "").gsub(" SDL_ACQUIRE(SDL_joystick_lock)", "").gsub(" SDL_RELEASE(SDL_joystick_lock)", "").gsub(/SDL_PRINTF_VARARG_FUNC\([\d]+\)/, "")
function_part_pieces = function_part.match(/([\S]+) (?:SDLCALL )*([\S]*)\(([\S ]+)\)/)
function_return_type = function_part_pieces[1]
function_name = function_part_pieces[2]
function_args = function_part_pieces[3].split(",").map{|arg| arg.strip.split}
next if should_function_be_excluded?(function_name.gsub(/\([\S]*/, "")) # The filtering ensures that no weird parentheses are checked (especially for callbacks)
final_functions.push [function_return_type, function_name, function_args]
end
end
final_functions
end
def get_all_structs(filename)
lines = nil
final_typedefs = []
final_structs = []
final_enums = []
File.open(filename, "r") do |f|
lines = f.readlines
end
total_str = lines.join("\n")
single_line_struct_matches = total_str.scan(/typedef struct ([\S]+)[ ]*([\S]*);/)
multi_line_struct_matches = total_str.scan(/typedef struct ([\S]*)[ ]*{((?>[^}])*)}([^;\}]*) ([\S]+);/)
typedefs = total_str.scan(/typedef ((?!struct)\S*) (\S*);/)
enums = total_str.scan(/typedef enum [\S ]*{ ([\S, ]+) } (\S+);/)
single_line_struct_matches.each do |match|
next if should_struct_be_excluded?(match[1])
final_typedefs.push [match[1], "void"]
end
typedefs.each do |match|
next if should_struct_be_excluded?(match[1])
final_typedefs.push [match[1], match[0]]
end
enums.each do |match|
next if should_struct_be_excluded?(match[1])
final_enums.push [match[1], match[0]]
end
multi_line_struct_matches.each do |match|
struct_name_1 = match[0].strip
struct_block = match[1].strip
struct_attributes = match[2].strip # Use these with the Packed annotation if given
struct_name_2 = match[3].strip
next if should_struct_be_excluded?(struct_name_2)
final_structs.push [struct_name_2, struct_attributes, struct_block]
end
[final_structs, final_typedefs, final_enums]
end
def get_all_constants(filename)
lines = nil
final_constants = []
File.open(filename, "r") do |f|
lines = f.readlines
end
total_str = lines.join("\n")
constant_matches = total_str.scan(/#define ([\w_\d]+)(\([\S \,]*\))?[ ]*(.*)[^\\]\n/)
constant_matches.each do |match|
if match[1]
puts "> Skipping macro function: #{match[0]}"
next
end
next if match[2].empty?
if match[0].match(/[a-z]/)
puts "> Skipping macro function: #{match[0]}"
next
end
next if should_constant_be_excluded?(match[0])
next if $constant_cache[match[0]]
$constant_cache[match[0]] = true
final_constants.push [match[0], match[2]]
end
final_constants
end
def transform_functions(functions)
transformed_functions = []
functions.each do |func|
arg_str = func[2].map{|par| par[0] == "void" ? "" : "#{underscore_string(par[-1])}#{par[0] == "..." ? "" : " : "}#{type_filter(par[0..-2].join(" "))}"}.join(", ").strip
transformed_functions.push " fun #{filter_sdl(underscore_string(func[1]))} = #{func[1]}(#{arg_str}) : #{type_filter(func[0])}"
end
transformed_functions
end
def transform_structs(structs)
transformed_structs = []
# Single line typedefs
struct_str = ""
structs[1].each do |struct|
struct_str += " alias #{soft_filter(struct[0])} = #{type_filter(struct[1])}\n"
end
struct_str = "\n#{struct_str}\n" unless struct_str == ""
transformed_structs.push struct_str
struct_str = ""
structs[2].each do |enum|
struct_str += " enum #{soft_filter(enum[0])}\n"
enum_values = enum[1].split(", ")
enum_values.each do |enum_value|
fixed_enum_values = enum_value.split("=")
fixed_enum_values[0].upcase!
fixed_enum_value = fixed_enum_values.join("=")
ord_val = filter_sdl(process_constant(fixed_enum_value.strip)).gsub("' '", ' '.ord.to_s).gsub(/'(\S)+'/) {|val| val[1..-2].gsub("\\r", "\r").gsub("\\e", "\e").gsub("\\b", "\b").gsub("\\t", "\t").gsub("\\u007F", "\u007F").gsub("\\'", "\'").ord}
processed_ord_val = ord_val.gsub(/SCANCODE_TO_KEYCODE\((\S+)\)/) { "Scancode::#{filter_sdl_const(Regexp.last_match[1])} | K_SCANCODE_MASK" }
struct_str += " #{processed_ord_val}\n"
end
struct_str += " end\n\n"
end
transformed_structs.push struct_str if struct_str != ""
structs[0].each do |struct|
struct_str = ""
struct_str += " @[Packed]\n" if struct[1].end_with? "_PACKED" # TODO: Add Crystal guards for platforms where this is not required
struct_str += " struct #{soft_filter(struct[0])}\n"
struct[2].gsub("const ", "").gsub("struct ", "").split(/[\n]+/).each do |struct_part|
array_part = struct_part.match(/\[(.+)\]/)
filtered_part = array_part ? struct_part.gsub(array_part[0], "") : struct_part
reduced_part = filtered_part.strip.gsub(";", "")
array_str = array_part ? "[#{filter_sdl_const(array_part[1])}]" : ""
components = reduced_part.split
components[1..-1].each do |component|
struct_str += " #{filter_sdl(underscore_string(component.gsub(",", "").strip))} : #{type_filter(components[0])}#{array_str}\n"
end
end
struct_str += " end\n\n"
transformed_structs.push struct_str
end
transformed_structs
end
def transform_constants(constants)
transformed_constants = []
c = 0
constants.each do |constant|
filtered_constant = process_constant(constant[1])
transformed_constants.push "#{c == 0 ? "\n" : ""} #{filter_sdl_const(constant[0])} = #{filtered_constant}"
c += 1
end
transformed_constants
end
headers = [
["SDL"],
["SDL_version"],
["additions/helper_types.cr"],
["SDL_scancode"],
["SDL_audio"],
["additions/helper_audio.cr"],
["SDL_blendmode"],
["SDL_clipboard"],
["SDL_error"],
["additions/helper_event.cr"],
["SDL_events"],
["SDL_filesystem"],
["additions/helper_gamecontroller.cr"],
["SDL_gamecontroller"],
["SDL_gesture"],
["SDL_guid"],
["SDL_haptic"],
["additions/helper_haptic.cr"],
["SDL_hints"],
["additions/helper_hints.cr"],
["SDL_joystick"],
["additions/helper_joystick.cr"],
["SDL_keyboard"],
["SDL_keycode"],
["SDL_mouse"],
["SDL_pixels"],
["additions/helper_pixels.cr"],
["SDL_rect"],
["SDL_render"],
["additions/helper_rwops.cr"],
["SDL_rwops"],
["SDL_sensor"],
["additions/helper_shape.cr"],
["SDL_shape"],
["SDL_surface"],
["SDL_touch"],
["additions/helper_video.cr"],
["SDL_video"]
]
img_headers = [
["SDL_image", "SDL_image/SDL2/include"]
]
mix_headers = [
["additions/helper_mixer.cr"],
["SDL_mixer", "SDL_mixer/SDL2/include"]
]
ttf_headers = [
["SDL_ttf", "SDL_ttf/SDL2"]
]
(headers + img_headers + mix_headers + ttf_headers).each {|header| compact_header(header)}
def process_header(f, header)
f.puts " # #{header[0]}\n"
if header[0].start_with? "additions/"
additions = nil
File.open(header[0], "r") do |f|
additions = f.readlines
end
f.puts "\n"
additions.each {|addition| f.puts addition}
f.puts "\n"
else
cc = 0
cs = 0
cf = 0
transform_constants(get_all_constants("output/#{header[0]}2.h")).each do |transformed_constant|
next if !transformed_constant
f.puts "#{transformed_constant}"
cc += 1
end
transform_structs(get_all_structs("output/#{header[0]}2.h")).each do |transformed_struct|
next if !transformed_struct
f.puts "#{transformed_struct}"
cs += 1
end
transform_functions(get_all_functions("output/#{header[0]}2.h")).each do |transformed_function|
next if !transformed_function
f.puts "#{transformed_function}"
cf += 1
end
f.puts "\n" if cf > 0
puts "A total of #{cc} constants, #{cs} structs and #{cf} functions were converted from #{header[0]}."
end
end
def write_bindings_to_file(filename, which_headers, lib_name, macro_file)
File.open(filename, "w") do |f|
f.puts "@[Link(\"#{lib_name}\")]"
f.puts "lib LibSDL"
which_headers.each do |header|
process_header(f, header)
end
f.puts "end"
macros = nil
File.open(macro_file, "r") do |f|
macros = f.readlines
end
f.puts "\n"
macros.each {|macros| f.puts macros}
end
end
system("mkdir -p src")
write_bindings_to_file("src/sdl-crystal-bindings.cr", headers, "SDL2", "additions/macros.cr")
write_bindings_to_file("src/sdl-image-bindings.cr", img_headers, "SDL2_image", "additions/macros_img.cr")
write_bindings_to_file("src/sdl-mixer-bindings.cr", mix_headers, "SDL2_mixer", "additions/macros_mix.cr")
write_bindings_to_file("src/sdl-ttf-bindings.cr", ttf_headers, "SDL2_ttf", "additions/macros_ttf.cr")