1 # frozen_string_literal: false
5 $CFLAGS += ' -O0 ' # faster checks
6 dir_config('atomic_ops')
7 have_func('mmap', 'sys/mman.h') or abort 'mmap() not found'
8 have_func('munmap', 'sys/mman.h') or abort 'munmap() not found'
9 have_func('rb_io_descriptor')
11 $CPPFLAGS += " -D_GNU_SOURCE "
12 have_func('mremap', 'sys/mman.h')
13 headers = %w(sys/types.h netdb.h string.h sys/socket.h netinet/in.h)
14 if have_header('linux/tcp.h')
15 headers << 'linux/tcp.h'
17 %w(netinet/tcp.h netinet/tcp_fsm.h).each { |h|
18 have_header(h, headers) and headers << h
22 $CPPFLAGS += " -D_BSD_SOURCE "
24 if have_type("struct tcp_info", headers)
67 cfunc = "tcp_info_#{field}"
68 if have_struct_member('struct tcp_info', field, headers)
70 static VALUE #{cfunc}(VALUE self)
72 struct tcp_info *info = DATA_PTR(self);
73 return UINT2NUM((uint32_t)info->#{field});
76 func_body.delete!("\n")
77 $defs << "-DCFUNC_#{cfunc}=#{Shellwords.shellescape(func_body)}"
79 func_body = "static inline void #{cfunc}(void) {}"
80 $defs << "-DCFUNC_#{cfunc}=#{Shellwords.shellescape(func_body)}"
81 cfunc = 'rb_f_notimplement'.freeze
83 rbmethod = %Q("#{field.sub(/\Atcpi_/, ''.freeze)}")
84 $defs << "-DDEFINE_METHOD_tcp_info_#{field}=" \
85 "#{Shellwords.shellescape(
86 %Q[rb_define_method(cTCP_Info,#{rbmethod},#{cfunc},0)])}"
89 ESTABLISHED: %w(TCP_ESTABLISHED TCPS_ESTABLISHED),
90 SYN_SENT: %w(TCP_SYN_SENT TCPS_SYN_SENT),
91 SYN_RECV: %w(TCP_SYN_RECV TCPS_SYN_RECEIVED),
92 FIN_WAIT1: %w(TCP_FIN_WAIT1 TCPS_FIN_WAIT_1),
93 FIN_WAIT2: %w(TCP_FIN_WAIT2 TCPS_FIN_WAIT_2),
94 TIME_WAIT: %w(TCP_TIME_WAIT TCPS_TIME_WAIT),
95 CLOSE: %w(TCP_CLOSE TCPS_CLOSED),
96 CLOSE_WAIT: %w(TCP_CLOSE_WAIT TCPS_CLOSE_WAIT),
97 LAST_ACK: %w(TCP_LAST_ACK TCPS_LAST_ACK),
98 LISTEN: %w(TCP_LISTEN TCPS_LISTEN),
99 CLOSING: %w(TCP_CLOSING TCPS_CLOSING),
102 tcp_state_map.each do |state, try|
103 try.each do |os_name|
104 have_const(os_name, headers) or next
105 tcp_state_map[state] = os_name
109 if nstate == tcp_state_map.size
110 $defs << '-DRAINDROPS_TCP_STATES_ALL_KNOWN=1'
111 tcp_state_map.each do |state, name|
112 $defs << "-DRAINDROPS_TCP_#{state}=#{name}"
117 have_func("getpagesize", "unistd.h")
118 have_func('rb_thread_call_without_gvl')
119 have_func('rb_thread_blocking_region')
120 have_func('rb_thread_io_blocking_region')
122 checking_for "GCC 4+ atomic builtins" do
123 # we test CMPXCHG anyways even though we don't need it to filter out
124 # ancient i386-only targets without CMPXCHG
126 int main(int argc, char * const argv[]) {
128 __sync_lock_test_and_set(&i, 0);
129 __sync_lock_test_and_set(&i, 1);
130 __sync_bool_compare_and_swap(&i, 0, 1);
131 __sync_add_and_fetch(&i, argc);
132 __sync_sub_and_fetch(&i, argc);
138 $defs.push(format("-DHAVE_GCC_ATOMIC_BUILTINS"))
141 # some compilers still target 386 by default, but we need at least 486
142 # to run atomic builtins.
143 prev_cflags = $CFLAGS
144 $CFLAGS += " -march=i486 "
146 $defs.push(format("-DHAVE_GCC_ATOMIC_BUILTINS"))
149 $CFLAGS = prev_cflags
153 end or have_header('atomic_ops.h') or abort <<-SRC
155 libatomic_ops is required if GCC 4+ is not used.
156 See https://github.com/ivmai/libatomic_ops
158 Users of Debian-based distros may run:
160 apt-get install libatomic-ops-dev
162 create_header # generate extconf.h to avoid excessively long command-line
163 $CFLAGS.sub!(/ -O0 /, '')
164 create_makefile('raindrops_ext')