PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/benchmarks/rdoc/ruby_trunk/ext/socket/extconf.rb

http://github.com/acangiano/ruby-benchmark-suite
Ruby | 352 lines | 293 code | 34 blank | 25 comment | 62 complexity | 0ed2652cc90ed5e258f23e0b14cad871 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, CC-BY-SA-3.0, LGPL-2.0, ISC, LGPL-2.1, GPL-2.0
  1. require 'mkmf'
  2. case RUBY_PLATFORM
  3. when /(ms|bcc)win32|mingw/
  4. test_func = "WSACleanup"
  5. have_library("ws2_32", "WSACleanup")
  6. when /cygwin/
  7. test_func = "socket"
  8. when /beos/
  9. test_func = "socket"
  10. have_library("net", "socket")
  11. when /haiku/
  12. test_func = "socket"
  13. have_library("network", "socket")
  14. when /i386-os2_emx/
  15. test_func = "socket"
  16. have_library("socket", "socket")
  17. else
  18. test_func = "socket"
  19. have_library("nsl", "t_open")
  20. have_library("socket", "socket")
  21. end
  22. unless $mswin or $bccwin or $mingw
  23. headers = %w<sys/types.h netdb.h string.h sys/socket.h netinet/in.h>
  24. end
  25. if /solaris/ =~ RUBY_PLATFORM and !try_compile("")
  26. # bug of gcc 3.0 on Solaris 8 ?
  27. headers << "sys/feature_tests.h"
  28. end
  29. if have_header("arpa/inet.h")
  30. headers << "arpa/inet.h"
  31. end
  32. ipv6 = false
  33. default_ipv6 = /mswin|cygwin|beos|haiku/ !~ RUBY_PLATFORM
  34. if enable_config("ipv6", default_ipv6)
  35. if checking_for("ipv6") {try_link(<<EOF)}
  36. #include <sys/types.h>
  37. #ifndef _WIN32
  38. #include <sys/socket.h>
  39. #endif
  40. int
  41. main()
  42. {
  43. socket(AF_INET6, SOCK_STREAM, 0);
  44. }
  45. EOF
  46. $defs << "-DENABLE_IPV6" << "-DINET6"
  47. ipv6 = true
  48. end
  49. end
  50. if ipv6
  51. if $mingw
  52. $CPPFLAGS << " -D_WIN32_WINNT=0x501"
  53. end
  54. ipv6lib = nil
  55. class << (fmt = "unknown")
  56. def %(s) s || self end
  57. end
  58. idirs, ldirs = dir_config("inet6", %w[/usr/inet6 /usr/local/v6].find {|d| File.directory?(d)})
  59. checking_for("ipv6 type", fmt) do
  60. if have_macro("IPV6_INRIA_VERSION", "netinet/in.h")
  61. "inria"
  62. elsif have_macro("__KAME__", "netinet/in.h")
  63. have_library(ipv6lib = "inet6")
  64. "kame"
  65. elsif have_macro("_TOSHIBA_INET6", "sys/param.h")
  66. have_library(ipv6lib = "inet6") and "toshiba"
  67. elsif have_macro("__V6D__", "sys/v6config.h")
  68. have_library(ipv6lib = "v6") and "v6d"
  69. elsif have_macro("_ZETA_MINAMI_INET6", "sys/param.h")
  70. have_library(ipv6lib = "inet6") and "zeta"
  71. elsif ipv6lib = with_config("ipv6-lib")
  72. warn <<EOS
  73. --with-ipv6-lib and --with-ipv6-libdir option will be obsolete, use
  74. --with-inet6lib and --with-inet6-{include,lib} options instead.
  75. EOS
  76. find_library(ipv6lib, nil, with_config("ipv6-libdir", ldirs)) and
  77. ipv6lib
  78. elsif have_library("inet6")
  79. "inet6"
  80. end
  81. end or not ipv6lib or abort <<EOS
  82. Fatal: no #{ipv6lib} library found. cannot continue.
  83. You need to fetch lib#{ipv6lib}.a from appropriate
  84. ipv6 kit and compile beforehand.
  85. EOS
  86. end
  87. if have_struct_member("struct sockaddr_in", "sin_len", headers)
  88. $defs[-1] = "-DHAVE_SIN_LEN"
  89. end
  90. # doug's fix, NOW add -Dss_family... only if required!
  91. doug = proc {have_struct_member("struct sockaddr_storage", "ss_family", headers)}
  92. if (doug[] or
  93. with_cppflags($CPPFLAGS + " -Dss_family=__ss_family", &doug))
  94. $defs[-1] = "-DHAVE_SOCKADDR_STORAGE"
  95. doug = proc {have_struct_member("struct sockaddr_storage", "ss_len", headers)}
  96. doug[] or with_cppflags($CPPFLAGS + " -Dss_len=__ss_len", &doug)
  97. end
  98. if have_struct_member("struct sockaddr", "sa_len", headers)
  99. $defs[-1] = "-DHAVE_SA_LEN "
  100. end
  101. have_header("netinet/tcp.h") if not /cygwin/ =~ RUBY_PLATFORM # for cygwin 1.1.5
  102. have_header("netinet/udp.h")
  103. if have_func("sendmsg") | have_func("recvmsg")
  104. have_struct_member('struct msghdr', 'msg_control', ['sys/types.h', 'sys/socket.h'])
  105. have_struct_member('struct msghdr', 'msg_accrights', ['sys/types.h', 'sys/socket.h'])
  106. end
  107. getaddr_info_ok = enable_config("wide-getaddrinfo") do
  108. checking_for("wide getaddrinfo") {try_run(<<EOF)}
  109. #{cpp_include(headers)}
  110. #include <stdlib.h>
  111. #ifndef EXIT_SUCCESS
  112. #define EXIT_SUCCESS 0
  113. #endif
  114. #ifndef EXIT_FAILURE
  115. #define EXIT_FAILURE 1
  116. #endif
  117. #ifndef AF_LOCAL
  118. #define AF_LOCAL AF_UNIX
  119. #endif
  120. int
  121. main()
  122. {
  123. int passive, gaierr, inet4 = 0, inet6 = 0;
  124. struct addrinfo hints, *ai, *aitop;
  125. char straddr[INET6_ADDRSTRLEN], strport[16];
  126. #ifdef _WIN32
  127. WSADATA retdata;
  128. WSAStartup(MAKEWORD(2, 0), &retdata);
  129. #endif
  130. for (passive = 0; passive <= 1; passive++) {
  131. memset(&hints, 0, sizeof(hints));
  132. hints.ai_family = AF_UNSPEC;
  133. hints.ai_protocol = IPPROTO_TCP;
  134. hints.ai_flags = passive ? AI_PASSIVE : 0;
  135. hints.ai_socktype = SOCK_STREAM;
  136. if ((gaierr = getaddrinfo(NULL, "54321", &hints, &aitop)) != 0) {
  137. (void)gai_strerror(gaierr);
  138. goto bad;
  139. }
  140. for (ai = aitop; ai; ai = ai->ai_next) {
  141. if (ai->ai_family == AF_LOCAL) continue;
  142. if (ai->ai_addr == NULL)
  143. goto bad;
  144. #if defined(_AIX)
  145. if (ai->ai_family == AF_INET6 && passive) {
  146. inet6++;
  147. continue;
  148. }
  149. ai->ai_addr->sa_len = ai->ai_addrlen;
  150. ai->ai_addr->sa_family = ai->ai_family;
  151. #endif
  152. if (ai->ai_addrlen == 0 ||
  153. getnameinfo(ai->ai_addr, ai->ai_addrlen,
  154. straddr, sizeof(straddr), strport, sizeof(strport),
  155. NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
  156. goto bad;
  157. }
  158. if (strcmp(strport, "54321") != 0) {
  159. goto bad;
  160. }
  161. switch (ai->ai_family) {
  162. case AF_INET:
  163. if (passive) {
  164. if (strcmp(straddr, "0.0.0.0") != 0) {
  165. goto bad;
  166. }
  167. } else {
  168. if (strcmp(straddr, "127.0.0.1") != 0) {
  169. goto bad;
  170. }
  171. }
  172. inet4++;
  173. break;
  174. case AF_INET6:
  175. if (passive) {
  176. if (strcmp(straddr, "::") != 0) {
  177. goto bad;
  178. }
  179. } else {
  180. if (strcmp(straddr, "::1") != 0) {
  181. goto bad;
  182. }
  183. }
  184. inet6++;
  185. break;
  186. case AF_UNSPEC:
  187. goto bad;
  188. break;
  189. default:
  190. /* another family support? */
  191. break;
  192. }
  193. }
  194. }
  195. if (!(inet4 == 0 || inet4 == 2))
  196. goto bad;
  197. if (!(inet6 == 0 || inet6 == 2))
  198. goto bad;
  199. if (aitop)
  200. freeaddrinfo(aitop);
  201. exit(EXIT_SUCCESS);
  202. bad:
  203. if (aitop)
  204. freeaddrinfo(aitop);
  205. exit(EXIT_FAILURE);
  206. }
  207. EOF
  208. end
  209. if ipv6 and not getaddr_info_ok
  210. abort <<EOS
  211. Fatal: --enable-ipv6 is specified, and your OS seems to support IPv6 feature.
  212. But your getaddrinfo() and getnameinfo() are appeared to be broken. Sorry,
  213. you cannot compile IPv6 socket classes with broken these functions.
  214. You can try --enable-wide-getaddrinfo.
  215. EOS
  216. end
  217. case with_config("lookup-order-hack", "UNSPEC")
  218. when "INET"
  219. $defs << "-DLOOKUP_ORDER_HACK_INET"
  220. when "INET6"
  221. $defs << "-DLOOKUP_ORDER_HACK_INET6"
  222. when "UNSPEC"
  223. # nothing special
  224. else
  225. abort <<EOS
  226. Fatal: invalid value for --with-lookup-order-hack (expected INET, INET6 or UNSPEC)
  227. EOS
  228. end
  229. $objs = [
  230. "init.#{$OBJEXT}",
  231. "constants.#{$OBJEXT}",
  232. "basicsocket.#{$OBJEXT}",
  233. "socket.#{$OBJEXT}",
  234. "ipsocket.#{$OBJEXT}",
  235. "tcpsocket.#{$OBJEXT}",
  236. "tcpserver.#{$OBJEXT}",
  237. "sockssocket.#{$OBJEXT}",
  238. "udpsocket.#{$OBJEXT}",
  239. "unixsocket.#{$OBJEXT}",
  240. "unixserver.#{$OBJEXT}",
  241. "option.#{$OBJEXT}",
  242. "ancdata.#{$OBJEXT}",
  243. "raddrinfo.#{$OBJEXT}"
  244. ]
  245. unless getaddr_info_ok and have_func("getnameinfo", headers) and have_func("getaddrinfo", headers)
  246. if have_struct_member("struct in6_addr", "s6_addr8", headers)
  247. $defs[-1] = "-DHAVE_ADDR8"
  248. end
  249. $CPPFLAGS="-I. "+$CPPFLAGS
  250. $objs += ["getaddrinfo.#{$OBJEXT}"]
  251. $objs += ["getnameinfo.#{$OBJEXT}"]
  252. $defs << "-DGETADDRINFO_EMU"
  253. have_func("inet_ntop") or have_func("inet_ntoa")
  254. have_func("inet_pton") or have_func("inet_aton")
  255. have_func("getservbyport")
  256. if have_func("gai_strerror")
  257. unless checking_for("gai_strerror() returns const pointer") {!try_compile(<<EOF)}
  258. #{cpp_include(headers)}
  259. #include <stdlib.h>
  260. void
  261. conftest_gai_strerror_is_const()
  262. {
  263. *gai_strerror(0) = 0;
  264. }
  265. EOF
  266. $defs << "-DGAI_STRERROR_CONST"
  267. end
  268. end
  269. have_header("arpa/nameser.h")
  270. have_header("resolv.h")
  271. end
  272. have_header("ifaddrs.h")
  273. have_func("getifaddrs")
  274. have_header("sys/ioctl.h")
  275. have_header("sys/sockio.h")
  276. have_header("net/if.h", headers)
  277. have_header("sys/param.h", headers)
  278. have_header("sys/ucred.h", headers)
  279. unless have_type("socklen_t", headers)
  280. $defs << "-Dsocklen_t=int"
  281. end
  282. have_header("sys/un.h")
  283. have_header("sys/uio.h")
  284. have_type("struct in_pktinfo", headers) {|src|
  285. src.sub(%r'^/\*top\*/', '\1'"\n#if defined(IPPROTO_IP) && defined(IP_PKTINFO)") <<
  286. "#else\n" << "#error\n" << ">>>>>> no in_pktinfo <<<<<<\n" << "#endif\n"
  287. }
  288. have_type("struct in6_pktinfo", headers) {|src|
  289. src.sub(%r'^/\*top\*/', '\1'"\n#if defined(IPPROTO_IPV6) && defined(IPV6_PKTINFO)") <<
  290. "#else\n" << "#error\n" << ">>>>>> no in6_pktinfo <<<<<<\n" << "#endif\n"
  291. }
  292. if have_struct_member("struct in_pktinfo", "ipi_spec_dst", headers)
  293. $defs[-1] = "-DHAVE_IPI_SPEC_DST"
  294. end
  295. have_type("struct sockcred", headers)
  296. have_type("struct cmsgcred", headers)
  297. have_func("getpeereid")
  298. have_header("ucred.h", headers)
  299. have_func("getpeerucred")
  300. # workaround for recent Windows SDK
  301. $defs << "-DIPPROTO_IPV6=IPPROTO_IPV6" if have_const("IPPROTO_IPV6") && !have_macro("IPPROTO_IPV6")
  302. $distcleanfiles << "constants.h" << "constdefs.*"
  303. if have_func(test_func)
  304. have_func("hsterror")
  305. have_func("getipnodebyname") or have_func("gethostbyname2")
  306. have_func("socketpair")
  307. unless have_func("gethostname")
  308. have_func("uname")
  309. end
  310. if enable_config("socks", ENV["SOCKS_SERVER"])
  311. if have_library("socks5", "SOCKSinit")
  312. $defs << "-DSOCKS5" << "-DSOCKS"
  313. elsif have_library("socks", "Rconnect")
  314. $defs << "-DSOCKS"
  315. end
  316. end
  317. create_makefile("socket")
  318. end