def self.generate(options = {})
typedefs = nil
Tempfile.open 'ffi_types_generator' do |io|
io.puts "#include <sys/types.h>\n#include <sys/socket.h>\n#include <sys/resource.h>\n"
io.close
typedefs = `gcc -E -x c #{options[:cppflags]} -D_DARWIN_USE_64_BIT_INODE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -c #{io.path}`
end
code = ""
typedefs.each_line do |type|
next unless type =~ /typedef/
next if type =~ /union|struct/
type.gsub!(/^(.*typedef\s*)/, "")
type.gsub!(/\s*;\s*$/, "")
parts = type.split(/\s+/)
def_type = parts.join(" ")
if type =~ /__attribute__/
if parts.last =~ /__QI__|__HI__|__SI__|__DI__|__word__/
final_type = ""
parts.each do |p|
break if p =~ /__attribute__/
final_type = p
end
else
final_type = parts.pop
end
def_type = case type
when /__QI__/ then "char"
when /__HI__/ then "short"
when /__SI__/ then "int"
when /__DI__/ then "long long"
when /__word__/ then "long"
else "int"
end
def_type = "unsigned #{def_type}" if type =~ /unsigned/
else
final_type = parts.pop
def_type = parts.join(" ")
end
if type = TYPE_MAP[def_type]
code << "rbx.platform.typedef.#{final_type} = #{type}\n"
TYPE_MAP[final_type] = TYPE_MAP[def_type]
else
if def_type =~ /\*/
code << "rbx.platform.typedef.#{final_type} = pointer\n"
TYPE_MAP[final_type] = :pointer
end
end
end
code
end