therubyracerがインストールできない

あるプロジェクトで、Gemfile.lockによりtherubyracer.gemがバージョン0.12.2、libv8.gemがバージョン3.16.14.7にロックされていた。bundle installしてみるとlibv8.gemのインストールでエラーになった。(macOS 10.12.2 + Homebrew)

$ bundle
Fetching gem metadata from https://rubygems.org/.
Fetching version metadata from https://rubygems.org/.
Installing libv8 3.16.14.7 with native extensions
Installing ref 1.0.5
Using bundler 1.13.7
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /Users/akira/.rbenv/versions/2.2.2/bin/ruby -r ./siteconf20161227-85073-1voqnaj.rb extconf.rb
creating Makefile
Compiling v8 for x64
Using python 2.7.10
Using compiler: /usr/bin/c++ (clang version 8.0.0)
In file included from ../src/conversions.cc:32:
In file included from ../src/conversions-inl.h:43:
../src/scanner.h:444:5: error: unused typedef '__StaticAssertTypedef__444' [-Werror,-Wunused-local-typedef]
    STATIC_ASSERT(kCharacterLookaheadBufferSize == 1);
    ^
../src/checks.h:283:30: note: expanded from macro 'STATIC_ASSERT'
#define STATIC_ASSERT(test)  STATIC_CHECK(test)
                             ^
../src/checks.h:251:5: note: expanded from macro 'STATIC_CHECK'
    SEMI_STATIC_JOIN(__StaticAssertTypedef__, __LINE__)
    ^
../src/checks.h:240:32: note: expanded from macro 'SEMI_STATIC_JOIN'
#define SEMI_STATIC_JOIN(a, b) SEMI_STATIC_JOIN_HELPER(a, b)
                               ^
../src/checks.h:241:39: note: expanded from macro 'SEMI_STATIC_JOIN_HELPER'
#define SEMI_STATIC_JOIN_HELPER(a, b) a##b
                                      ^
<scratch space>:61:1: note: expanded from here
__StaticAssertTypedef__444
^
1 error generated.
make[1]: ***
[/Users/akira/tmp/t/vendor/ruby/2.2.0/gems/libv8-3.16.14.7/vendor/v8/out/x64.release/obj.target/preparser_lib/src/conversions.o] Error 1
make: *** [x64.release] Error 2
/Users/akira/tmp/t/vendor/ruby/2.2.0/gems/libv8-3.16.14.7/ext/libv8/location.rb:36:in `block in verify_installation!': libv8 did not install
properly, expected binary v8 archive
'/Users/akira/tmp/t/vendor/ruby/2.2.0/gems/libv8-3.16.14.7/vendor/v8/out/x64.release/obj.target/tools/gyp/libv8_base.a'to exist, but it was
not found (Libv8::Location::Vendor::ArchiveNotFound)
        from /Users/akira/tmp/t/vendor/ruby/2.2.0/gems/libv8-3.16.14.7/ext/libv8/location.rb:35:in `each'
        from /Users/akira/tmp/t/vendor/ruby/2.2.0/gems/libv8-3.16.14.7/ext/libv8/location.rb:35:in `verify_installation!'
        from /Users/akira/tmp/t/vendor/ruby/2.2.0/gems/libv8-3.16.14.7/ext/libv8/location.rb:26:in `install!'
        from extconf.rb:7:in `<main>'
GYP_GENERATORS=make \
        build/gyp/gyp --generator-output="out" build/all.gyp \
                      -Ibuild/standalone.gypi --depth=. \
                      -Dv8_target_arch=x64 \
-S.x64  -Dv8_enable_backtrace=1 -Dv8_can_use_vfp2_instructions=true -Darm_fpu=vfpv2 -Dv8_can_use_vfp3_instructions=true
-Darm_fpu=vfpv3 -Dwerror=''
  CXX(target) /Users/akira/tmp/t/vendor/ruby/2.2.0/gems/libv8-3.16.14.7/vendor/v8/out/x64.release/obj.target/preparser_lib/src/allocation.o
CXX(target)
/Users/akira/tmp/t/vendor/ruby/2.2.0/gems/libv8-3.16.14.7/vendor/v8/out/x64.release/obj.target/preparser_lib/src/atomicops_internals_x86_gcc.o
  CXX(target) /Users/akira/tmp/t/vendor/ruby/2.2.0/gems/libv8-3.16.14.7/vendor/v8/out/x64.release/obj.target/preparser_lib/src/bignum.o
  CXX(target) /Users/akira/tmp/t/vendor/ruby/2.2.0/gems/libv8-3.16.14.7/vendor/v8/out/x64.release/obj.target/preparser_lib/src/bignum-dtoa.o
CXX(target)
/Users/akira/tmp/t/vendor/ruby/2.2.0/gems/libv8-3.16.14.7/vendor/v8/out/x64.release/obj.target/preparser_lib/src/cached-powers.o
  CXX(target) /Users/akira/tmp/t/vendor/ruby/2.2.0/gems/libv8-3.16.14.7/vendor/v8/out/x64.release/obj.target/preparser_lib/src/conversions.o

extconf failed, exit code 1

Gem files will remain installed in /Users/akira/tmp/t/vendor/ruby/2.2.0/gems/libv8-3.16.14.7 for inspection.
Results logged to /Users/akira/tmp/t/vendor/ruby/2.2.0/extensions/x86_64-darwin-16/2.2.0-static/libv8-3.16.14.7/gem_make.out

An error occurred while installing libv8 (3.16.14.7), and Bundler cannot continue.
Make sure that `gem install libv8 -v '3.16.14.7'` succeeds before bundling.

libv8.gemを個別にインストールする方法を検索してみると--with-system-v8を付けるとよいといったものが出てくる。従ってみればたしかにインストールできる。(apple-gcc42を使え、というのも出てくるが、そちらはエラーになった。)

$ gem install libv8 -v 3.16.14.7 -- --with-system-v8                            
Building native extensions with: '--with-system-v8'
This could take a while...
Successfully installed libv8-3.16.14.7
Parsing documentation for libv8-3.16.14.7
Done installing documentation for libv8 after 0 seconds
1 gem installed

これでlibv8.gemの問題は解決だと再びbundleするとtherubyracer.gemのインスールでエラーになった。

$ bundle
Fetching gem metadata from https://rubygems.org/.
Fetching version metadata from https://rubygems.org/.
Using libv8 3.16.14.7
Using ref 1.0.5
Using bundler 1.13.7
Installing therubyracer 0.12.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /Users/akira/.rbenv/versions/2.2.2/bin/ruby -r ./siteconf20161227-86003-10m326y.rb extconf.rb
checking for main() in -lpthread... yes
checking for main() in -lobjc... yes
checking for v8.h... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/Users/akira/.rbenv/versions/2.2.2/bin/$(RUBY_BASE_NAME)
        --with-pthreadlib
        --without-pthreadlib
        --with-objclib
        --without-objclib
        --enable-debug
        --disable-debug
        --with-v8-dir
        --without-v8-dir
        --with-v8-include
        --without-v8-include=${v8-dir}/include
        --with-v8-lib
        --without-v8-lib=${v8-dir}/lib
/Users/akira/tmp/t/vendor/ruby/2.2.0/gems/libv8-3.16.14.7/ext/libv8/location.rb:50:in `configure': You have chosen to use the version of V8
found on your system (Libv8::Location::System::NotFoundError)
and *not* the one that is bundle with the libv8 rubygem. However,
it could not be located. please make sure you have a version of
v8 that is compatible with 3.16.14.7 installed. You may
need to special --with-v8-dir options if it is in a non-standard
location

thanks,
The Mgmt

        from /Users/akira/tmp/t/vendor/ruby/2.2.0/gems/libv8-3.16.14.7/lib/libv8.rb:7:in `configure_makefile'
        from extconf.rb:32:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in /Users/akira/tmp/t/vendor/ruby/2.2.0/gems/therubyracer-0.12.2 for inspection.
Results logged to /Users/akira/tmp/t/vendor/ruby/2.2.0/extensions/x86_64-darwin-16/2.2.0-static/therubyracer-0.12.2/gem_make.out

An error occurred while installing therubyracer (0.12.2), and Bundler cannot continue.
Make sure that `gem install therubyracer -v '0.12.2'` succeeds before bundling.

これも検索すると--with-v8-dirを付けてやればよいと出てくる。しかし実際にやってみると状況は変わらず、やはりエラーとなる。

ここで、いったんlibv8.gemを削除して、therubyracer.gemを先にインストールしてみると、なんとこれが成功してしまう:

$ gem uninstall libv8 therubyracer                                      
Successfully uninstalled libv8-3.16.14.7

$ gem install therubyracer 
Fetching: libv8-3.16.14.17-x86_64-darwin-16.gem (100%)
Successfully installed libv8-3.16.14.17-x86_64-darwin-16
Building native extensions.  This could take a while...
Successfully installed therubyracer-0.12.2
Parsing documentation for libv8-3.16.14.17-x86_64-darwin-16
Installing ri documentation for libv8-3.16.14.17-x86_64-darwin-16
Parsing documentation for therubyracer-0.12.2
Installing ri documentation for therubyracer-0.12.2
Done installing documentation for libv8, therubyracer after 0 seconds
2 gems installed

もちろん動作する。

$ ruby -rubygems -e 'require "v8"; p V8::Context.new.eval("1+1")'
2

だが、よく見るとtherubyracer.gemの依存関係によりlibv8-3.16.14.17の、しかもバイナリがインストールされていることがわかる。したがって、Gemfile.lockの指定を満たせない。

$ bundle check
The following gems are missing
 * libv8 (3.16.14.7)
Install missing gems with `bundle install`

ここで再び、必要とされるバージョンのlibv8.gemをインストールすると、Gemfile.lockの指定を満たせる。

$ gem install libv8 -v 3.16.14.7 -- --with-system-v8
Fetching: libv8-3.16.14.7.gem (100%)
Building native extensions with: '--with-system-v8'
This could take a while...
Successfully installed libv8-3.16.14.7
Parsing documentation for libv8-3.16.14.7
Installing ri documentation for libv8-3.16.14.7
Done installing documentation for libv8 after 0 seconds
1 gem installed

$ bundle check                                                                           
The Gemfile's dependencies are satisfied

この状態で動作するのかというと、問題なく動作する。

$ bundle exec ruby -e 'require "v8"; p V8::Context.new.eval("1+1")'
2

結局、libv8.gemはなんなのかというと、少なくともこの件の範囲では、therubyracer.gemをインストールするための材料の一つだといえる。

このケースでは、therubyracer.gemの中の拡張ライブラリは、libv8.gemの中のlibv8ライブラリをリンクする。静的にリンクしていれば実行時にはlibv8.gemはまったく使用されない。そういうものである。

ところでtherubyracer.gemのインストールに--with-v8-dirを指定してもエラーになる件だが、これはどうやらlibv8のバージョンによるものらしい。以下のように3.15系を指定してインストールを行うとエラーは起きない。

$ brew install v8-315

$ bundle config --local build.libv8 --with-system-v8                          
You are replacing the current local value of build.libv8, which is currently nil

$ bundle config --local build.therubyracer --with-v8-dir=/usr/local/opt/v8-315                          
You are replacing the current local value of build.therubyracer, which is currently nil

$ bundle
Fetching gem metadata from https://rubygems.org/.
Fetching version metadata from https://rubygems.org/.
Installing libv8 3.16.14.7 with native extensions
Installing ref 1.0.5
Using bundler 1.13.7
Installing therubyracer 0.12.2 with native extensions
Bundle complete! 1 Gemfile dependency, 4 gems now installed.
Bundled gems are installed into ./vendor.