Sierraでruby-buildで1.8.7をビルドする

今更ながらrbenv使ってRuby-1.8.7をインストールすべく、 ruby-buildで1.8.7をインストールしようとしたところ、 次のようなエラーが出ました。

ERROR: This package must be compiled with GCC, but ruby-build couldn't
find a suitable `gcc` executable on your system. Please install GCC
and try again.

DETAILS: Apple no longer includes the official GCC compiler with Xcode
as of version 4.2. Instead, the `gcc` executable is a symlink to
`llvm-gcc`, a modified version of GCC which outputs LLVM bytecode.

For most programs the `llvm-gcc` compiler works fine. However,
versions of Ruby older than 1.9.3-p125 are incompatible with
`llvm-gcc`. To build older versions of Ruby you must have the official
GCC compiler installed on your system.

TO FIX THE PROBLEM: Install Homebrew's apple-gcc42 package with this
command: brew tap homebrew/dupes ; brew install apple-gcc42

You will need to install the official GCC compiler to build older
versions of Ruby even if you have installed Apple's Command Line Tools
for Xcode package. The Command Line Tools for Xcode package only
includes `llvm-gcc`.

BUILD FAILED (OS X 10.12.4 using ruby-build 20170405-2-g3b15693)

素直にbrewapple-gcc42を入れようとして、brew install apple-gcc42したところ、 今度は次のようなエラーになりました。

% brew install apple-gcc42
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (caskroom/cask).
No changes to formulae.

apple-gcc42: This formula either does not compile or function as expected on macOS
versions newer than Mavericks due to an upstream incompatibility.
Error: An unsatisfied requirement failed this build.

むーん、なんでこんな状態になっているんだろうと思いつつ、 Xcodegccで1.8.7-p374をコンパイルしてみたところ、 warningはたくさん出るものの、コンパイルできてしまいました。 なので、ruby-buildを改良してSierraだったらXcodegccを使って コンパイルするようにしたパッチが以下です。

--- a/bin/ruby-build
+++ b/bin/ruby-build
@@ -761,6 +761,12 @@ require_java7() {
 require_gcc() {
   local gcc="$(locate_gcc || true)"

+  local osx_version="$(osx_version)"
+  if [ $osx_version = "1012" ]; then
+    return 0
+  fi
+
+
   if [ -z "$gcc" ]; then
     { echo
       colorize 1 "ERROR"

この状態にして、opensslとreadlineとtkのオプションを渡してインストールします。 brewであらかじめopensslとreadlineを入れておいて、

% CONFIGURE_OPTS="--with-readline-dir=/usr/local --with-openssl-dir=`brew --prefix openssl`" RUBY_CONFIGURE_OPTS="--with-openssl-dir=`brew --prefix openssl`" rbenv install 1.8.7-p374

ふう。なんとかインストールできました。

Downloading ruby-1.8.7-p374.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p374.tar.bz2
Installing ruby-1.8.7-p374...

WARNING: ruby-1.8.7-p374 is past its end of life and is now unsupported.
It no longer receives bug fixes or critical security updates.

ruby-build: use readline from homebrew
Installed ruby-1.8.7-p374 to /Users/fujioka/.rbenv/versions/1.8.7-p374

Downloading rubygems-1.6.2.tgz...
-> https://dqw8nmjcqpjn7.cloudfront.net/cb5261818b931b5ea2cb54bc1d583c47823543fcf9682f0d6298849091c1cea7
Installing rubygems-1.6.2...
Installed rubygems-1.6.2 to /Users/fujioka/.rbenv/versions/1.8.7-p374