Import r39558, r39559, r39560, r39572 from ruby

These commits fix extension building in RubyGems to match the new
mkmf.rb output.

Also, 1.8.7 compatibility was restored.