Cross-compiling Ruby extensions for win32: rcovrt
It seems that building Ruby extensions under win32 is quite a daunting task: I've been asked repeatedly to provide win32 binaries for rcovrt (the extension that makes rcov's code coverage analysis run two orders of magnitude faster*1), so I cross-compiled it from the comfort of a non-win32 platform, using mingw.
Binaries built with mingw should be compatible with the ruby-mswin32 distribution, which also means they should work fine with the latest one-click-installer distros (the 1.8.4-16 series), since they are based on the good old ruby-mswin32 builds.
The first thing I need to generate an appropriate Makefile in order to cross-compile rcovrt is a suitable rbconfig.rb. It turns out that the easiest way to create it is cross-compiling ruby itself (it takes under 2 minutes on my fairly old box), obtaining the desired file in the process.
I wrote a simple cross-compile.sh:
#!/bin/sh env ac_cv_func_getpgrp_void=no \ ac_cv_func_setpgrp_void=yes \ rb_cv_negative_time_t=no \ ac_cv_func_memcmp_working=yes \ rb_cv_binary_elf=no \ ./configure \ --host=i586-mingw32msvc \ --target=i386-mingw32 \ --build=i686-linux \ --prefix=/where/to/install/ruby-mingw32 make ruby #make install
The resulting rbconfig.rb contains the information about the build environment mkmf will use when I run extconf.rb.
All that's left is overriding the settings in the rbconfig.rb corresponding to the ruby executable from the platform I'm building on:
$ ruby -I path/to/generated/rbconfig extconf.rb $ make
That way the new rbconfig.rb will be loaded when mkmf require()s it.
It's as simple as that:
$ file rcovrt.so rcovrt.so: MS Windows PE 32-bit Intel 80386 console DLL
However, I don't know if the extension will actually work, not having a win32 box nearby... If you happen to be running ruby-mswin32 or the one click installer (1.8.4-16), please give it a try:
- copy rcovrt.so to the appropriate dir (something like c:\ruby\lib\ruby\site_ruby\1.8\i386-mswin32\)
- run the following:
ruby -v -rrcovrt -e "p Rcov"
It will just print the platform info an then "Rcov" if everything went well. If so, rcov: code coverage for Ruby will now run much faster.
At any rate, please drop a line with the result. Thanks!
Just want to say "thank you" because your article helped me to cross-compile a library of mine :)
(It was still very tedious as I had to link with another C++ library which I had to cross-compile too, ouch!)
dos$ ruby -v -rrcovrt -e "p Rcov" ruby 1.8.4 (2005-12-24) [i386-mswin32] Rcov
Great! Being able to build for win32 without actually having to use it feels great :-) Thank you for the report, Jim.
Worked for me too. Thank you very much. The only issue I am left with is the code duplication in one of my classes.
worked fine: ruby 1.8.4 (2005-12-24) [i386-mswin32]
*1 than other tools based on Kernel#set_trace_func
- 58 http://www.ruby-forum.com/topic/84993
- 49 http://redhanded.hobix.com/inspect/hpricotAndSandboxForWin32.html
- 21 http://www.artima.com/forums/flat.jsp?forum=123&thread=154800
- 19 http://www.ruby-forum.com/topic/61438
- 18 http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/220548
- 15 http://anarchaia.org
- 12 http://planetruby.0x42.net
- 8 http://www.anarchaia.org
- 7 http://www.artima.com/buzz/community.jsp?forum=123
- 5 http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/219783