August 4, 2009

Embedding MacRuby

The actual implementation of MacRuby contains a facility method used by HotCocoa macrake tasks and by the embed MacRuby target in XCode. The method only change the executable to point to the embedded MacRuby in the .app file. But all bundles in MacRuby contain a reference to the system MacRuby installed in /Library/Framework, not the embedded version. This prevents the distribution of a MacRuby application to computers without a system-wide MacRuby install.

Waiting for the next MacRuby release where this problem should be fixed, an easy fix is to run install_name_tool on all the bundles in the directory. From the command line:

    1 mkdir ~/tmp
    2 cd tmp
    3 hotcocoa embedded
    4 cd embedded
    5 macrake deploy
    6 LIB=MacRuby.framework/Versions/Current/usr/lib/libmacruby.dylib
    7 
    8 find Embedded.app/Contents -name "*.bundle" \
    9 -exec install_name_tool -change /Library/Frameworks/$LIB \
   10 @executable_path/../Frameworks/$LIB {} \;
   11 

Sorry for the poor line wrapping to make it readable. You can check the results with:

The first lines are needed just to setup a HotCocoa example project. On line 8 I’m applying install_name_tool to all bundles in the distribution. The same can be done to a MacRuby XCode project created with an Embedded target.

If your project is HotCocoa based, you can add the following macrake task to the Rakefile in the root directory:

As you can see I’m also removing unnecessary files for a MacRuby 0.4 distribution to shrink the application size down to around 15Mb. Not too bad considering an entire Ruby runtime (MacRuby) is included.

Comments (View)
blog comments powered by Disqus