README: update with extra disclaimer
[raindrops.git] / ext / raindrops / extconf.rb
blobb1310b0efa0d69dfed89b411dc01b1234083bd99
1 # frozen_string_literal: false
2 require 'mkmf'
3 require 'shellwords'
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'
16 else
17   %w(netinet/tcp.h netinet/tcp_fsm.h).each { |h|
18     have_header(h, headers) and headers << h
19   }
20 end
22 $CPPFLAGS += " -D_BSD_SOURCE "
24 if have_type("struct tcp_info", headers)
25   %w(
26     tcpi_state
27     tcpi_ca_state
28     tcpi_retransmits
29     tcpi_probes
30     tcpi_backoff
31     tcpi_options
32     tcpi_snd_wscale
33     tcpi_rcv_wscale
34     tcpi_rto
35     tcpi_ato
36     tcpi_snd_mss
37     tcpi_rcv_mss
38     tcpi_unacked
39     tcpi_sacked
40     tcpi_lost
41     tcpi_retrans
42     tcpi_fackets
43     tcpi_last_data_sent
44     tcpi_last_ack_sent
45     tcpi_last_data_recv
46     tcpi_last_ack_recv
47     tcpi_pmtu
48     tcpi_rcv_ssthresh
49     tcpi_rtt
50     tcpi_rttvar
51     tcpi_snd_ssthresh
52     tcpi_snd_cwnd
53     tcpi_advmss
54     tcpi_reordering
55     tcpi_rcv_rtt
56     tcpi_rcv_space
57     tcpi_total_retrans
58     tcpi_snd_wnd
59     tcpi_snd_bwnd
60     tcpi_snd_nxt
61     tcpi_rcv_nxt
62     tcpi_toe_tid
63     tcpi_snd_rexmitpack
64     tcpi_rcv_ooopack
65     tcpi_snd_zerowin
66   ).each do |field|
67     cfunc = "tcp_info_#{field}"
68     if have_struct_member('struct tcp_info', field, headers)
69       func_body = <<EOF
70 static VALUE #{cfunc}(VALUE self)
72         struct tcp_info *info = DATA_PTR(self);
73         return UINT2NUM((uint32_t)info->#{field});
75 EOF
76       func_body.delete!("\n")
77       $defs << "-DCFUNC_#{cfunc}=#{Shellwords.shellescape(func_body)}"
78     else
79       func_body = "static inline void #{cfunc}(void) {}"
80       $defs << "-DCFUNC_#{cfunc}=#{Shellwords.shellescape(func_body)}"
81       cfunc = 'rb_f_notimplement'.freeze
82     end
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)])}"
87   end
88   tcp_state_map = {
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),
100   }
101   nstate = 0
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
106       nstate += 1
107     end
108   end
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}"
113     end
114   end
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
125   src = <<SRC
126 int main(int argc, char * const argv[]) {
127         unsigned long i = 0;
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);
133         return 0;
137   if try_link(src)
138     $defs.push(format("-DHAVE_GCC_ATOMIC_BUILTINS"))
139     true
140   else
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 "
145     if try_link(src)
146       $defs.push(format("-DHAVE_GCC_ATOMIC_BUILTINS"))
147       true
148     else
149       $CFLAGS = prev_cflags
150       false
151     end
152   end
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')