    # File lib/rubygems/installer.rb, line 384
      def build_extensions
        return if @spec.extensions.empty?
        say "Building native extensions.  This could take a while..."
        start_dir = Dir.pwd
        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

          begin
            Dir.chdir File.join(@gem_dir, File.dirname(extension))
            results = builder.build(extension, @gem_dir, dest_path, results)

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

          rescue => ex
            results = results.join "\n"

            File.open('gem_make.out', 'wb') { |f| f.puts results }

            message = "ERROR: Failed to build gem native extension.\n\n\#{results}\n\nGem files will remain installed in \#{@gem_dir} for inspection.\nResults logged to \#{File.join(Dir.pwd, 'gem_make.out')}\n"

            raise ExtensionBuildError, message
          ensure
            Dir.chdir start_dir
          end
        end
      end