Sooey

2013-12-04 22:41:43 +0900

Bundlerでgemに固有のビルドオプションを指定する方法。

libv8を利用しているRailsアプリケーションで、Mavericksにしてから改めてgemのビルド&インストールを行おうとすると(Mountain Lion時代にビルドしたgemが使えている限りはおそらく問題に遭遇しない)、libv8のビルドがこんな感じで失敗する。

Installing libv8 (3.11.8.13)
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /Users/juno/.rbenv/versions/1.9.3-p484/bin/ruby extconf.rb
creating Makefile
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Unable to find a compiler officially supported by v8.
It is recommended to use GCC v4.4 or higher
Using compiler: g++

(snip)

Gem files will remain installed in /Users/juno/src/railsapp/vendor/bundle/ruby/1.9.1/gems/libv8-3.11.8.13 for inspection.
Results logged to /Users/juno/src/railsapp/vendor/bundle/ruby/1.9.1/gems/libv8-3.11.8.13/ext/libv8/gem_make.out

Mavericks上でlibv8のビルドを成功させるには、

$ gem install libv8 -v '3.11.8.17' -- --with-system-v8

というように--with-system-v8オプションを指定すればいいのだけど、Railsアプリでbundle installした時のインストール先をbundle install --path vendor/bundleのようにプロジェクトローカルなパスに指定している場合、普通にgem installしたgemは参照することができない。

そこで、bundle installした時にもgemに固有のビルドオプションを指定する方法を調べていたところ、あらかじめbundle configコマンドで設定しておけばよいことがわかった。

最終的に、Mavericksでlibv8をbundle installで正しくビルド&インストールするには以下のようにすればOKだった。

$ bundle config build.libv8 --with-system-v8
$ bundle install --path vendor/bundle