1 require 'lib/ffi/struct_generator'
2 require 'lib/ffi/const_generator'
3 require 'lib/ffi/types_generator'
5 deps = %w[Rakefile] + Dir['lib/ffi/*rb']
7 file 'runtime/platform.conf' => deps do |task|
8 addrinfo = FFI::StructGenerator.new 'addrinfo' do |s|
9 s.include 'sys/socket.h'
11 s.name 'struct addrinfo'
12 s.field :ai_flags, :int
13 s.field :ai_family, :int
14 s.field :ai_socktype, :int
15 s.field :ai_protocol, :int
16 s.field :ai_addrlen, :int
17 s.field :ai_addr, :pointer
18 s.field :ai_canonname, :string
19 s.field :ai_next, :pointer
22 dirent = FFI::StructGenerator.new 'dirent' do |s|
23 s.include "sys/types.h"
25 s.name 'struct dirent'
26 s.field :d_ino, :ino_t
27 s.field :d_reclen, :ushort
28 s.field :d_name, :char_array
31 timeval = FFI::StructGenerator.new 'timeval' do |s|
32 s.include "sys/time.h"
33 s.name 'struct timeval'
34 s.field :tv_sec, :time_t
35 s.field :tv_usec, :suseconds_t
38 sockaddr_in = FFI::StructGenerator.new 'sockaddr_in' do |s|
39 s.include "netinet/in.h"
41 s.include "sys/socket.h"
42 s.include "sys/stat.h"
43 s.name 'struct sockaddr_in'
44 s.field :sin_family, :sa_family_t
45 s.field :sin_port, :ushort
47 s.field :sin_zero, :char_array
50 sockaddr_un = FFI::StructGenerator.new 'sockaddr_un' do |s|
52 s.name 'struct sockaddr_un'
53 s.field :sun_family, :sa_family_t
54 s.field :sun_path, :char_array
57 servent = FFI::StructGenerator.new 'servent' do |s|
59 s.name 'struct servent'
60 s.field :s_name, :pointer
61 s.field :s_aliases, :pointer
63 s.field :s_proto, :pointer
66 stat = FFI::StructGenerator.new 'stat' do |s|
67 s.include "sys/types.h"
68 s.include "sys/stat.h"
70 s.field :st_dev, :dev_t
71 s.field :st_ino, :ino_t
72 s.field :st_mode, :mode_t
73 s.field :st_nlink, :nlink_t
74 s.field :st_uid, :uid_t
75 s.field :st_gid, :gid_t
76 s.field :st_rdev, :dev_t
77 s.field :st_size, :off_t
80 s.field :st_atime, :time_t
81 s.field :st_mtime, :time_t
82 s.field :st_ctime, :time_t
85 rlimit = FFI::StructGenerator.new 'rlimit' do |s|
86 s.include "sys/types.h"
87 s.include "sys/time.h"
88 s.include "sys/resource.h"
89 s.name 'struct rlimit'
90 s.field :rlim_cur, :rlim_t
91 s.field :rlim_max, :rlim_t
94 # FIXME these constants don't have standard names. LOCK_SH == Linux,
95 # O_SHLOCK on Bsd/Darwin, etc. Binary doesn't exist at all in many non-Unix
96 # variants. This should come out of something like config.h
106 file_cg = FFI::ConstGenerator.new 'rbx.platform.file' do |cg|
109 cg.include 'sys/stat.h'
144 file_constants.each { |c| cg.const c }
147 io_cg = FFI::ConstGenerator.new 'rbx.platform.io' do |cg|
156 io_constants.each { |c| cg.const c }
159 # Only constants needed by core are added here
160 fcntl_cg = FFI::ConstGenerator.new 'rbx.platform.fcntl' do |cg|
163 fcntl_constants = %w[
169 fcntl_constants.each { |c| cg.const c }
172 socket_cg = FFI::ConstGenerator.new 'rbx.platform.socket' do |cg|
173 cg.include 'sys/types.h'
174 cg.include 'sys/socket.h'
176 cg.include 'netinet/tcp.h'
177 cg.include 'netinet/in.h'
179 socket_constants = %w[
243 INADDR_ALLHOSTS_GROUP
247 INADDR_MAX_LOCAL_GROUP
275 IP_DEFAULT_MULTICAST_LOOP
276 IP_DEFAULT_MULTICAST_TTL
403 SO_SECURITY_AUTHENTICATION
404 SO_SECURITY_ENCRYPTION_NETWORK
405 SO_SECURITY_ENCRYPTION_TRANSPORT
419 socket_constants.each { |c| cg.const c }
422 process_cg = FFI::ConstGenerator.new 'rbx.platform.process' do |cg|
423 cg.include 'sys/wait.h'
424 cg.include 'sys/resource.h'
426 process_constants = %w{
445 process_constants.each { |c| cg.const c }
447 long_process_constants = %w[
453 long_process_constants.each { |c|
454 cg.const c, "%llu", "(unsigned long long)"
458 # The constants come from MRI's signal.c. This means that some of them might
461 signal_cg = FFI::ConstGenerator.new 'rbx.platform.signal' do |cg|
462 cg.include 'signal.h'
463 cg.include 'sys/signal.h'
465 signal_constants = %w{
512 signal_constants.each { |c| cg.const c }
515 zlib_cg = FFI::ConstGenerator.new 'rbx.platform.zlib' do |cg|
518 zlib_constants = %w[ZLIB_VERSION]
520 zlib_constants.each { |c| cg.const c, "%s", "(char *)" }
523 puts "Generating #{task.name}..." if $verbose
525 File.open task.name, "w" do |f|
526 addrinfo.dump_config f
528 timeval.dump_config f
529 sockaddr_in.dump_config f
530 sockaddr_un.dump_config f if sockaddr_un.found?
531 servent.dump_config f
535 file_cg.dump_constants f
536 io_cg.dump_constants f
537 fcntl_cg.dump_constants f
538 socket_cg.dump_constants f
539 process_cg.dump_constants f
540 signal_cg.dump_constants f
541 zlib_cg.dump_constants f
543 f.puts FFI::TypesGenerator.generate