def build_extensions
    return if spec.extensions.empty?
    say "Building native extensions.  This could take a while..."
    dest_path = File.join gem_dir, spec.require_paths.first
    ran_rake = false # only run rake once

    spec.extensions.each do |extension|
      break if ran_rake
      results = []

      builder = case extension
                when /extconf/ then
                  Gem::Ext::ExtConfBuilder
                when /configure/ then
                  Gem::Ext::ConfigureBuilder
                when /rakefile/i, /mkrf_conf/i then
                  ran_rake = true
                  Gem::Ext::RakeBuilder
                else
                  results = ["No builder for extension '#{extension}'"]
                  nil
                end


      extension_dir = begin
                        File.join gem_dir, File.dirname(extension)
                      rescue TypeError # extension == nil
                        gem_dir
                      end


      begin
        Dir.chdir extension_dir do
          results = builder.build(extension, gem_dir, dest_path, results)

          say results.join("\n") if Gem.configuration.really_verbose
        end
      rescue
        results = results.join "\n"

        gem_make_out = File.join extension_dir, 'gem_make.out'

        open gem_make_out, 'wb' do |io| io.puts results end

        message = <<-EOF
ERROR: Failed to build gem native extension.

        #{results}

Gem files will remain installed in #{gem_dir} for inspection.
Results logged to #{gem_make_out}
EOF

        raise ExtensionBuildError, message
      end
    end
  end
#
Builds extensions.  Valid types of extensions are extconf.rb files,
configure scripts and rakefiles or mkrf_conf files.