Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 591 lines (531 sloc) 12.705 kb
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 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
require File.expand_path('../../lib/ffi/generators', __FILE__)

directory "runtime"

deps = %w[Rakefile rakelib/platform.rake runtime] + Dir['lib/ffi/*rb']

file 'runtime/platform.conf' => deps do |task|
  puts "GEN runtime/platform.conf"

  File.open task.name, "wb" do |f|
    FFI::Generators::Structures.new 'addrinfo' do |s|
      if BUILD_CONFIG[:windows]
        s.include "ws2tcpip.h"
      else
        s.include 'sys/socket.h'
        s.include 'netdb.h'
      end
      s.name 'struct addrinfo'
      s.field :ai_flags, :int
      s.field :ai_family, :int
      s.field :ai_socktype, :int
      s.field :ai_protocol, :int
      s.field :ai_addrlen, :int
      s.field :ai_addr, :pointer
      s.field :ai_canonname, :string
      s.field :ai_next, :pointer
    end.write_config(f)

    FFI::Generators::Structures.new 'dirent' do |s|
      s.include "sys/types.h"
      s.include "dirent.h"
      s.name 'struct dirent'
      s.field :d_ino, :ino_t
      s.field :d_reclen, :ushort
      s.field :d_name, :char_array
    end.write_config(f)

    FFI::Generators::Structures.new 'timeval' do |s|
      s.include "sys/time.h"
      s.name 'struct timeval'
      s.field :tv_sec, :time_t
      s.field :tv_usec, :suseconds_t
    end.write_config(f)

    FFI::Generators::Structures.new 'sockaddr_in' do |s|
      if BUILD_CONFIG[:windows]
        s.include "ws2tcpip.h"
      else
        s.include "netinet/in.h"
        s.include "sys/socket.h"
      end
      s.include "fcntl.h"
      s.include "sys/stat.h"
      s.name 'struct sockaddr_in'
      s.field :sin_family, :sa_family_t
      s.field :sin_port, :ushort
      s.field :sin_addr
      s.field :sin_zero, :char_array
    end.write_config(f)

    unless BUILD_CONFIG[:windows]
      sockaddr_un = FFI::Generators::Structures.new 'sockaddr_un' do |s|
        s.include "sys/un.h"
        s.name 'struct sockaddr_un'
        s.field :sun_family, :sa_family_t
        s.field :sun_path, :char_array
      end

      sockaddr_un.write_config f if sockaddr_un.found?
    end

    FFI::Generators::Structures.new 'servent' do |s|
      if BUILD_CONFIG[:windows]
        s.include "winsock2.h"
      else
        s.include "netdb.h"
      end
      s.name 'struct servent'
      s.field :s_name, :pointer
      s.field :s_aliases, :pointer
      s.field :s_port, :int
      s.field :s_proto, :pointer
    end.write_config(f)

    FFI::Generators::Structures.new 'stat' do |s|
      unless BUILD_CONFIG[:windows]
        s.include "sys/types.h"
        s.include "unistd.h"
        s.include "time.h"
      end
      s.include "sys/stat.h"
      s.name 'struct stat'
      s.field :st_dev, :dev_t
      s.field :st_ino, :ino_t
      s.field :st_mode, :mode_t
      if BUILD_CONFIG[:windows]
        s.field :st_nlink, :short
        s.field :st_uid, :short
        s.field :st_gid, :short
      else
        s.field :st_nlink, :nlink_t
        s.field :st_uid, :uid_t
        s.field :st_gid, :gid_t
      end
      s.field :st_rdev, :dev_t
      s.field :st_size, :off_t
      unless BUILD_CONFIG[:windows]
        s.field :st_blksize
        s.field :st_blocks
      end
      s.field :st_atime, :time_t
      s.field :st_mtime, :time_t
      s.field :st_ctime, :time_t
    end.write_config(f)

    unless BUILD_CONFIG[:windows]
      FFI::Generators::Structures.new 'rlimit' do |s|
        s.include "sys/types.h"
        s.include "sys/time.h"
        s.include "sys/resource.h"
        s.name 'struct rlimit'
        s.field :rlim_cur, :rlim_t
        s.field :rlim_max, :rlim_t
      end.write_config(f)
    end

    # FIXME these constants don't have standard names. LOCK_SH == Linux,
    # O_SHLOCK on Bsd/Darwin, etc. Binary doesn't exist at all in many non-Unix
    # variants. This should come out of something like config.h

    # fixme_constants = %w{
    # LOCK_SH
    # LOCK_EX
    # LOCK_NB
    # LOCK_UN
    # BINARY
    # }

    FFI::Generators::Constants.new 'rbx.platform.file' do |cg|
      cg.include 'stdio.h'
      cg.include 'fcntl.h'
      cg.include 'sys/stat.h'

      file_constants = %w[
O_RDONLY
O_WRONLY
O_RDWR
O_CREAT
O_EXCL
O_NOCTTY
O_TRUNC
O_APPEND
O_NONBLOCK
O_SYNC
S_IRUSR
S_IWUSR
S_IXUSR
S_IRGRP
S_IWGRP
S_IXGRP
S_IROTH
S_IWOTH
S_IXOTH
S_IFMT
S_IFIFO
S_IFCHR
S_IFDIR
S_IFBLK
S_IFREG
S_IFLNK
S_IFSOCK
S_IFWHT
S_ISUID
S_ISGID
S_ISVTX
]

      file_constants.each { |c| cg.const c }
    end.write_constants(f)

    FFI::Generators::Constants.new 'rbx.platform.io' do |cg|
      cg.include 'stdio.h'

      io_constants = %w[
SEEK_SET
SEEK_CUR
SEEK_END
]

      io_constants.each { |c| cg.const c }
    end.write_constants(f)

    # Only constants needed by core are added here
    FFI::Generators::Constants.new 'rbx.platform.fcntl' do |cg|
      cg.include 'fcntl.h'

      fcntl_constants = %w[
F_GETFL
F_SETFL
O_ACCMODE
F_GETFD
F_SETFD
FD_CLOEXEC
]

      fcntl_constants.each { |c| cg.const c }
    end.write_constants(f)

    FFI::Generators::Constants.new 'rbx.platform.socket' do |cg|
      cg.include 'sys/types.h'
      if BUILD_CONFIG[:windows]
        cg.include "winsock2.h"
      else
        cg.include 'sys/socket.h'
        cg.include 'netdb.h'
        cg.include 'netinet/tcp.h'
        cg.include 'netinet/in.h'
      end

      socket_constants = %w[
AF_APPLETALK
AF_ATM
AF_AX25
AF_CCITT
AF_CHAOS
AF_CNT
AF_COIP
AF_DATAKIT
AF_DEC
AF_DLI
AF_E164
AF_ECMA
AF_HYLINK
AF_IMPLINK
AF_INET
AF_INET6
AF_IPX
AF_ISDN
AF_ISO
AF_LAT
AF_LINK
AF_LOCAL
AF_MAX
AF_NATM
AF_NDRV
AF_NETBIOS
AF_NETGRAPH
AF_NS
AF_OSI
AF_PPP
AF_PUP
AF_ROUTE
AF_SIP
AF_SNA
AF_SYSTEM
AF_UNIX
AF_UNSPEC

AI_ADDRCONFIG
AI_ALL
AI_CANONNAME
AI_DEFAULT
AI_MASK
AI_NUMERICHOST
AI_PASSIVE
AI_V4MAPPED
AI_V4MAPPED_CFG

EAI_ADDRFAMILY
EAI_AGAIN
EAI_BADFLAGS
EAI_BADHINTS
EAI_FAIL
EAI_FAMILY
EAI_MAX
EAI_MEMORY
EAI_NODATA
EAI_NONAME
EAI_PROTOCOL
EAI_SERVICE
EAI_SOCKTYPE
EAI_SYSTEM

INADDR_ALLHOSTS_GROUP
INADDR_ANY
INADDR_BROADCAST
INADDR_LOOPBACK
INADDR_MAX_LOCAL_GROUP
INADDR_NONE
INADDR_UNSPEC_GROUP

IPPORT_RESERVED
IPPORT_USERRESERVED

IPPROTO_BIP
IPPROTO_EGP
IPPROTO_EON
IPPROTO_GGP
IPPROTO_HELLO
IPPROTO_ICMP
IPPROTO_IDP
IPPROTO_IGMP
IPPROTO_IP
IPPROTO_MAX
IPPROTO_ND
IPPROTO_PUP
IPPROTO_RAW
IPPROTO_TCP
IPPROTO_TP
IPPROTO_UDP
IPPROTO_XTP

IPX_TYPE

IP_ADD_MEMBERSHIP
IP_DEFAULT_MULTICAST_LOOP
IP_DEFAULT_MULTICAST_TTL
IP_DROP_MEMBERSHIP
IP_HDRINCL
IP_MAX_MEMBERSHIPS
IP_MULTICAST_IF
IP_MULTICAST_LOOP
IP_MULTICAST_TTL
IP_OPTIONS
IP_RECVDSTADDR
IP_RECVOPTS
IP_RECVRETOPTS
IP_RETOPTS
IP_TOS
IP_TTL

MSG_COMPAT
MSG_CTRUNC
MSG_DONTROUTE
MSG_DONTWAIT
MSG_EOF
MSG_EOR
MSG_FLUSH
MSG_HAVEMORE
MSG_HOLD
MSG_OOB
MSG_PEEK
MSG_RCVMORE
MSG_SEND
MSG_TRUNC
MSG_WAITALL

NI_DGRAM
NI_MAXHOST
NI_MAXSERV
NI_NAMEREQD
NI_NOFQDN
NI_NUMERICHOST
NI_NUMERICSERV

PF_APPLETALK
PF_AX25
PF_CCITT
PF_CHAOS
PF_CNT
PF_COIP
PF_DATAKIT
PF_DLI
PF_ECMA
PF_HYLINK
PF_IMPLINK
PF_INET
PF_INET6
PF_IPX
PF_ISDN
PF_ISO
PF_KEY
PF_LAT
PF_LINK
PF_LOCAL
PF_MAX
PF_NATM
PF_NDRV
PF_NETBIOS
PF_NETGRAPH
PF_NS
PF_OSI
PF_PIP
PF_PPP
PF_PUP
PF_ROUTE
PF_RTIP
PF_SIP
PF_SNA
PF_SYSTEM
PF_UNIX
PF_UNSPEC
PF_XTP

SHUT_RD
SHUT_RDWR
SHUT_WR

SOCK_DGRAM
SOCK_PACKET
SOCK_RAW
SOCK_RDM
SOCK_SEQPACKET
SOCK_STREAM

SOL_ATALK
SOL_AX25
SOL_IP
SOL_IPX
SOL_SOCKET
SOL_TCP
SOL_UDP

SOPRI_BACKGROUND
SOPRI_INTERACTIVE
SOPRI_NORMAL

SO_ACCEPTCONN
SO_ACCEPTFILTER
SO_ATTACH_FILTER
SO_BINDTODEVICE
SO_BROADCAST
SO_DEBUG
SO_DETACH_FILTER
SO_DONTROUTE
SO_DONTTRUNC
SO_ERROR
SO_KEEPALIVE
SO_LINGER
SO_NKE
SO_NOSIGPIPE
SO_NO_CHECK
SO_NREAD
SO_OOBINLINE
SO_PASSCRED
SO_PEERCRED
SO_PEERNAME
SO_PRIORITY
SO_RCVBUF
SO_RCVLOWAT
SO_RCVTIMEO
SO_REUSEADDR
SO_REUSEPORT
SO_SECURITY_AUTHENTICATION
SO_SECURITY_ENCRYPTION_NETWORK
SO_SECURITY_ENCRYPTION_TRANSPORT
SO_SNDBUF
SO_SNDLOWAT
SO_SNDTIMEO
SO_TIMESTAMP
SO_TYPE
SO_USELOOPBACK
SO_WANTMORE
SO_WANTOOBFLAG

TCP_MAXSEG
TCP_NODELAY
]

      socket_constants.each { |c| cg.const c }
    end.write_constants(f)

    unless BUILD_CONFIG[:windows]
      FFI::Generators::Constants.new 'rbx.platform.process' do |cg|
        cg.include 'sys/wait.h'
        cg.include 'sys/resource.h'
        cg.include 'stdlib.h'

        process_constants = %w{
EXIT_SUCCESS
EXIT_FAILURE

WNOHANG
WUNTRACED
PRIO_PROCESS
PRIO_PGRP
PRIO_USER
RLIMIT_CPU
RLIMIT_FSIZE
RLIMIT_DATA
RLIMIT_STACK
RLIMIT_CORE
RLIMIT_RSS
RLIMIT_NPROC
RLIMIT_NOFILE
RLIMIT_MEMLOCK
RLIMIT_AS
RLIMIT_SBSIZE
}

        process_constants.each { |c| cg.const c }

        long_process_constants = %w[
RLIM_INFINITY
RLIM_SAVED_MAX
RLIM_SAVED_CUR
]

        long_process_constants.each { |c|
          cg.const c, "%llu", "(unsigned long long)"
        }
      end.write_constants(f)
    end

    # The constants come from MRI's signal.c. This means that some of them might
    # be missing.

    FFI::Generators::Constants.new 'rbx.platform.signal' do |cg|
      cg.include 'signal.h'
      unless BUILD_CONFIG[:windows]
        cg.include 'sys/signal.h'
      end

      signal_constants = %w{
SIGHUP
SIGINT
SIGQUIT
SIGILL
SIGTRAP
SIGIOT
SIGABRT
SIGEMT
SIGFPE
SIGKILL
SIGBUS
SIGSEGV
SIGSYS
SIGPIPE
SIGALRM
SIGTERM
SIGURG
SIGSTOP
SIGTSTP
SIGCONT
SIGCHLD
SIGCLD
SIGCHLD
SIGTTIN
SIGTTOU
SIGIO
SIGXCPU
SIGXFSZ
SIGVTALRM
SIGPROF
SIGWINCH
SIGUSR1
SIGUSR2
SIGLOST
SIGMSG
SIGPWR
SIGPOLL
SIGDANGER
SIGMIGRATE
SIGPRE
SIGGRANT
SIGRETRACT
SIGSOUND
SIGINFO
}

      signal_constants.each { |c| cg.const c }
    end.write_constants(f)

    FFI::Generators::Constants.new 'rbx.platform.zlib' do |cg|
      cg.include_dir 'vendor/zlib' if BUILD_CONFIG[:vendor_zlib]
      cg.include 'zlib.h'

      zlib_constants = %w[ZLIB_VERSION]

      zlib_constants.each { |c| cg.const c, "%s", "(char *)" }
    end.write_constants(f)

    unless BUILD_CONFIG[:windows]
      FFI::Generators::Constants.new 'rbx.platform.dlopen' do |cg|
        cg.include 'dlfcn.h'

        dlopen_constants = %w[
RTLD_LAZY
RTLD_NOW
RTLD_LOCAL
RTLD_GLOBAL
]

        dlopen_constants.each { |c| cg.const c }
      end.write_constants(f)
    end

    f.puts FFI::Generators::Types.new.generate
  end
end
Something went wrong with that request. Please try again.