eigenclass logo
MAIN  Index  Search  Changes  PageRank  Login

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.

Cross-compiling ruby

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:

    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 \
    make ruby
    #make install

The resulting rbconfig.rb contains the information about the build environment mkmf will use when I run extconf.rb.

Cross-compiling the extension

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:

  1. copy rcovrt.so to the appropriate dir (something like c:\ruby\lib\ruby\site_ruby\1.8\i386-mswin32\)
  2. 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!

Thank you - Robin Stocker (2006-05-20 (Sat) 20:39:49)

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!)

Worked for me - Jim Weirich (2006-04-03 (Mon) 10:58:28)

 dos$ ruby -v -rrcovrt -e "p Rcov"
 ruby 1.8.4 (2005-12-24) [i386-mswin32]

mfp 2006-04-03 (Mon) 13:26:18

Great! Being able to build for win32 without actually having to use it feels great :-) Thank you for the report, Jim.

eric 2006-04-04 (Mardi) 11:02:49

Worked for me too. Thank you very much. The only issue I am left with is the code duplication in one of my classes.

Ingo 2006-04-06 (Thr) 09:15:09

worked fine: ruby 1.8.4 (2005-12-24) [i386-mswin32]

*1 than other tools based on Kernel#set_trace_func