Compiling Ruby with MacRuby 0.5b1

MacRuby 0.5b1 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值