MacRuby 0.5 beta 1 is out and can be downloaded from here. The beta can only be used on Snow Leopard, which means its Intel-only. They switched from using YARV as the internal engine to using LLVM. The major side effect of this change is that Ruby code can now be compiled.
Compilation is still pretty rough though. Many simple programs won’t work, so at this point it’s more of an example then a useful tool.
In order to get MacRuby to compile code, you need to have LLVM installed. It doesn’t come as part of the MacRuby 0.5b1 download. I found directions at the Hatena::Diary blog. They’re in Japanese. I don’t speak the language and Google Translate butchers it, so I’m recreating it here with some additional notes.
The first thing to do is to download and install MacRuby. It’s a zip file containing an OS/X install package. The package installs the MacRuby binaries to /usr/local/bin. Make sure it’s in your PATH. All of the usual Ruby suspects are here, prefaced with ‘mac’. So you have ‘macruby’, ‘macgem’, ‘macirb’, etc.
After you install it, you can run pretty much any Ruby program with macruby. This is still very much a work in progress, so don’t be surprised when a program doesn’t run or doesn’t run correctly.
You use the ‘macrubyc’ command to compile code. But if you try to run it now, you’ll get an error saying that it can’t find ‘llc’. That’s because you don’t have LLVM installed. And you need a very current copy.
Here’s where Hatena’s directions come into play.
$ svn co -r 82747 https://llvm.org/svn/llvm-project/llvm/trunk llvm-trunk $ cd llvm-trunk $ ./configure --prefix ~/opt $ UNIVERSAL=1 UNIVERSAL_ARCH="i386 x86_64" ENABLE_OPTIMIZED=1 make -j2 $ UNIVERSAL=1 UNIVERSAL_ARCH="i386 x86_64" ENABLE_OPTIMIZED=1 make install
The ‘svn’ command will checkout revision 82747 of LLVM into the directory ‘llvm-trunk’. Make sure you’re in the directory you want the source checked out into when you run it.
The next two commands go down into the source code directory and set it up to install in an ‘opt’ directory under your home, e.g., ‘/Users/ctwise/opt’. This keeps the LLVM install local and won’t conflict with any later system-wide installs. If you want LLVM installed to the same location as macruby then use the command ‘./configure –prefix /usr/local’.
The fourth command compiles LLVM. It’s setup to run two compiles simultaneously. If you’re using a tool that monitors CPU load, you’ll see both CPUs pegged. This compile step takes a long time.
The last command installs LLVM into the location you specified. If you want LLVM installed to ‘/usr/local’, then you’ll need to change the install command slightly to use sudo.
$ sudo env UNIVERSAL=1 UNIVERSAL_ARCH="i386 x86_64" ENABLE_OPTIMIZED=1 make install
Once you add ‘~/opt/bin’ to your PATH, you can start compiling Ruby.
This small test program compiles and runs fine.
puts "Hello, World!" 'abc'.each_char do |str| puts str.upcase end
When run with ‘macruby’, it produces this output:
$ macruby test.rb Hello, World! A B C
To compile the program you use ‘macrubyc’.
$ macrubyc -o test test.rb
This will create an executable named ‘test’ as well as a ‘test.o’ object file.
-rwxr-xr-x 1 ctwise staff 14784568 Oct 8 09:28 test* -rw-r--r-- 1 ctwise staff 2408 Oct 8 09:28 test.o -rw-r--r-- 1 ctwise staff 69 Oct 8 09:27 test.rb
If you want, you can use the ‘file’ command to see what the files are.
$ file test* test: Mach-O 64-bit executable x86_64 test.o: Mach-O 64-bit object x86_64 test.rb: ASCII text $ ./test Hello, World! A B C
And when you run ‘test’, you’ll see the same output as running ‘macruby test.rb’.
When I tried slightly more complex projects using gems, the compile went through cleanly but the resulting executable exited with an error code. YMMV.
The help file for ‘macrubyc’ states that it supports ‘normal’ and ‘full’ compilation. But setting it to ‘full’ gives you a message that ‘full’ mode isn’t supported yet. A more interesting option is the ‘-V’ or ‘–verbose’ option. This will show you the actual commands being run to compile the code.