`
love~ruby+rails
  • 浏览: 849237 次
  • 性别: Icon_minigender_1
  • 来自: lanzhou
社区版块
存档分类
最新评论

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.

分享到:
评论

相关推荐

    Compiling with Continuations

    《使用延续的编译》这本书是由普林斯顿大学计算机科学系的Andrew W. Appel所著,并于1992年由剑桥大学出版社出版。本书深入探讨了延续传递风格(Continuation-passing style, 简称CPS)在编译过程中的应用及其相关...

    The Theory of Parsing, Translation, and Compiling

    Aho and Ullman - The Theory of Parsing, Translation, and Compiling - Vol. 1 (1972).djvu Aho and Ullman - The Theory of Parsing, Translation, and Compiling - Vol. 2 (1973).djvu

    vs6.0安装及运行 和解决 Compiling... error spawning cl.exe 的问题

    VS6.0 安装及运行解决 Compiling... error spawning cl.exe 问题 本文将详细介绍 VS6.0 的安装与运行过程,并解决 Compiling... error spawning cl.exe 问题,以便帮助初学者更好地理解和应用 VS6.0。 路径的概念 ...

    Advanced C and C++ Compiling 无水印pdf

    Advanced C and C++ Compiling 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...

    Compiling... ,Error spawning cl.exe错误

    ### "Compiling..., Error spawning cl.exe" 错误解析与解决方案 #### 一、问题概述 在使用Visual C++ 6.0进行编程时,不少用户可能会遇到一个常见的错误:“Compiling..., Error spawning cl.exe”。当尝试编译或...

    Advanced C and C++ Compiling

    ### 高级C和C++编译技术概览 #### 引言 本书《高级C和C++编译》深入探讨了C与C++语言在现代操作系统环境下的编译技术,旨在帮助读者深入了解这两个广泛使用的编程语言在多任务操作系统中的工作原理及优化技巧。...

    compiling-tool android反编译工具

    在Android应用开发的世界里,有时候我们可能需要对APK文件进行反编译,以便查看其源代码、资源文件或理解应用程序的工作原理。这通常用于学习、调试或逆向工程目的。在这一过程中,有几款关键的工具是必不可少的,...

    Hosek-Compiling cross-toolchains with CMake and runtimes build.pdf

    ### 关于跨平台工具链与CMake构建的相关知识点 #### 跨平台工具链(Cross-Toolchain) 跨平台工具链是指用于编译针对不同架构或操作系统的代码的工具集。通常,开发人员需要为不同的目标平台创建可执行文件,这时...

    C&C++-compiling process (C++编译器解析)

    C++编译过程详解 C++编程语言的编译过程是一项复杂的系统工程,涉及多个阶段,从源代码到可执行程序的转化过程中,编译器起着至关重要的作用。本资料详细介绍了C++的编译过程,包括预处理、编译、汇编和链接四个...

    Advanced C and C++ Compiling.pdf

    本书《Advanced C and C++ Compiling.pdf》是一本深入探讨C与C++编程语言编译技术和实践的专业书籍。通过本书的学习,读者可以深入了解如何高效地利用这两种语言进行程序设计与优化。 #### 作者简介 该书由一位具有...

    TC.zip_Compiling

    标题中的"TC.zip_Compiling"暗示了这是一个与C++编译相关的压缩包,可能包含一个C++编译器或者是一些编译C++程序所需的工具和资源。描述明确指出是"C++ Compiler for compiling c++ program",意味着这个压缩包的...

    Learning Geospatial Analysis with Python

    This book focuses on pure Python whenever possible to minimize compiling platform-dependent binaries, so that you don't become bogged down in just getting ready to do analysis., 'Learning Geospatial ...

    Hot Reload Edit Code Without Compiling v1.12.5

    Hot Reload Edit Code Without Compiling v1.12.5

    PyBuilder for compiling with py2exe-开源

    PyBuilder是在Windows上用于将python脚本编译为exe格式的构建工具。 它具有易于使用的界面,并使用py2exe进行编译。 不再需要弄乱命令提示符或安装脚本。 PyBuilder会为您完成。

    TMS Pack for FireMonkey2.3.0.1

    Fixed : Issue compiling demo's in trial version Fixed : Issue with LoadFromFile column widths in TTMSFMXGrid v2.1.0.2 Improved : GetTextHeight function in TTMSFMXHTMLText Fixed : Issue with iOS ...

    fundamentals-of-compiling.rar_Fundamentals

    《编译原理》是计算机科学领域的一门基础课程,它主要研究如何将高级程序设计语言转换为机器可执行的指令。这门课程对于理解计算机系统的内部运作至关重要,尤其对于那些致力于软件开发、系统编程或者想要深入研究...

    为Java虚拟机编译Scala(Michel Schinz)Compiling Scala for the Java Virtual Machine (Michel Schinz)

    - **Compiling type expressions**:编译类型表达式以在运行时保留足够的信息,供模式匹配和反射使用。 - **Polymorphic methods and classes**:编译多态方法和类,保留多态性,以便在运行时进行方法的动态绑定。 -...

Global site tag (gtag.js) - Google Analytics