1#--
2# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
3# All rights reserved.
4# See LICENSE.txt for permissions.
5#++
6
7class Gem::Ext::Builder
8
9  def self.class_name
10    name =~ /Ext::(.*)Builder/
11    $1.downcase
12  end
13
14  def self.make(dest_path, results)
15    unless File.exist? 'Makefile' then
16      raise Gem::InstallError, "Makefile not found:\n\n#{results.join "\n"}"
17    end
18
19    # try to find make program from Ruby configure arguments first
20    RbConfig::CONFIG['configure_args'] =~ /with-make-prog\=(\w+)/
21    make_program = $1 || ENV['MAKE'] || ENV['make']
22    unless make_program then
23      make_program = (/mswin/ =~ RUBY_PLATFORM) ? 'nmake' : 'make'
24    end
25
26    destdir = '"DESTDIR=%s"' % ENV['DESTDIR'] if RUBY_VERSION > '2.0'
27
28    ['', 'install'].each do |target|
29      # Pass DESTDIR via command line to override what's in MAKEFLAGS
30      cmd = [
31        make_program,
32        destdir,
33        target
34      ].join(' ').rstrip
35      run(cmd, results, "make #{target}".rstrip)
36    end
37  end
38
39  def self.redirector
40    '2>&1'
41  end
42
43  def self.run(command, results, command_name = nil)
44    verbose = Gem.configuration.really_verbose
45
46    begin
47      # TODO use Process.spawn when ruby 1.8 support is dropped.
48      rubygems_gemdeps, ENV['RUBYGEMS_GEMDEPS'] = ENV['RUBYGEMS_GEMDEPS'], nil
49      if verbose
50        puts(command)
51        system(command)
52      else
53        results << command
54        results << `#{command} #{redirector}`
55      end
56    ensure
57      ENV['RUBYGEMS_GEMDEPS'] = rubygems_gemdeps
58    end
59
60    unless $?.success? then
61      results << "Building has failed. See above output for more information on the failure." if verbose
62      raise Gem::InstallError, "#{command_name || class_name} failed:\n\n#{results.join "\n"}"
63    end
64  end
65
66end
67
68