2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2007-11-10 02:48:56 -05:00
|
|
|
#--
|
|
|
|
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
|
|
|
|
# All rights reserved.
|
|
|
|
# See LICENSE.txt for permissions.
|
|
|
|
#++
|
|
|
|
|
2020-05-16 09:42:27 -04:00
|
|
|
require_relative '../user_interaction'
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
class Gem::Ext::Builder
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
include Gem::UserInteraction
|
|
|
|
|
|
|
|
##
|
|
|
|
# The builder shells-out to run various commands after changing the
|
|
|
|
# directory. This means multiple installations cannot be allowed to build
|
|
|
|
# extensions in parallel as they may change each other's directories leading
|
|
|
|
# to broken extensions or failed installations.
|
|
|
|
|
|
|
|
CHDIR_MUTEX = Mutex.new # :nodoc:
|
|
|
|
|
|
|
|
attr_accessor :build_args # :nodoc:
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def self.class_name
|
|
|
|
name =~ /Ext::(.*)Builder/
|
|
|
|
$1.downcase
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.make(dest_path, results)
|
2018-11-21 05:20:47 -05:00
|
|
|
unless File.exist? 'Makefile'
|
2013-10-15 20:14:16 -04:00
|
|
|
raise Gem::InstallError, 'Makefile not found'
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2011-01-28 18:46:47 -05:00
|
|
|
# try to find make program from Ruby configure arguments first
|
2011-01-18 19:08:49 -05:00
|
|
|
RbConfig::CONFIG['configure_args'] =~ /with-make-prog\=(\w+)/
|
2013-09-14 04:59:02 -04:00
|
|
|
make_program = ENV['MAKE'] || ENV['make'] || $1
|
2018-11-21 05:20:47 -05:00
|
|
|
unless make_program
|
2007-11-10 02:48:56 -05:00
|
|
|
make_program = (/mswin/ =~ RUBY_PLATFORM) ? 'nmake' : 'make'
|
|
|
|
end
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
destdir = '"DESTDIR=%s"' % ENV['DESTDIR']
|
2013-07-22 18:46:50 -04:00
|
|
|
|
2013-10-15 20:43:14 -04:00
|
|
|
['clean', '', 'install'].each do |target|
|
2013-06-25 09:28:57 -04:00
|
|
|
# Pass DESTDIR via command line to override what's in MAKEFLAGS
|
|
|
|
cmd = [
|
|
|
|
make_program,
|
2013-07-22 18:46:50 -04:00
|
|
|
destdir,
|
2013-06-25 09:28:57 -04:00
|
|
|
target
|
|
|
|
].join(' ').rstrip
|
2013-10-15 20:43:14 -04:00
|
|
|
begin
|
|
|
|
run(cmd, results, "make #{target}".rstrip)
|
|
|
|
rescue Gem::InstallError
|
|
|
|
raise unless target == 'clean' # ignore clean failure
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def self.run(command, results, command_name = nil)
|
|
|
|
verbose = Gem.configuration.really_verbose
|
|
|
|
|
2013-02-04 21:37:35 -05:00
|
|
|
begin
|
|
|
|
rubygems_gemdeps, ENV['RUBYGEMS_GEMDEPS'] = ENV['RUBYGEMS_GEMDEPS'], nil
|
|
|
|
if verbose
|
2015-07-01 17:50:14 -04:00
|
|
|
puts("current directory: #{Dir.pwd}")
|
2018-11-04 10:39:00 -05:00
|
|
|
p(command)
|
|
|
|
end
|
|
|
|
results << "current directory: #{Dir.pwd}"
|
|
|
|
results << (command.respond_to?(:shelljoin) ? command.shelljoin : command)
|
|
|
|
|
2019-12-13 06:19:08 -05:00
|
|
|
require "open3"
|
2020-03-24 02:39:24 -04:00
|
|
|
# Set $SOURCE_DATE_EPOCH for the subprocess.
|
|
|
|
env = {'SOURCE_DATE_EPOCH' => Gem.source_date_epoch_string}
|
|
|
|
output, status = Open3.capture2e(env, *command)
|
2019-04-29 03:07:16 -04:00
|
|
|
if verbose
|
|
|
|
puts output
|
|
|
|
else
|
|
|
|
results << output
|
2013-02-04 21:37:35 -05:00
|
|
|
end
|
2018-10-30 23:23:30 -04:00
|
|
|
rescue => error
|
|
|
|
raise Gem::InstallError, "#{command_name || class_name} failed#{error.message}"
|
2013-02-04 21:37:35 -05:00
|
|
|
ensure
|
|
|
|
ENV['RUBYGEMS_GEMDEPS'] = rubygems_gemdeps
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2019-04-29 03:07:16 -04:00
|
|
|
unless status.success?
|
2012-11-29 01:52:18 -05:00
|
|
|
results << "Building has failed. See above output for more information on the failure." if verbose
|
2019-04-29 03:07:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
yield(status, results) if block_given?
|
2013-10-15 20:14:16 -04:00
|
|
|
|
2019-04-29 03:07:16 -04:00
|
|
|
unless status.success?
|
2013-10-15 20:14:16 -04:00
|
|
|
exit_reason =
|
2019-04-29 03:07:16 -04:00
|
|
|
if status.exited?
|
|
|
|
", exit code #{status.exitstatus}"
|
|
|
|
elsif status.signaled?
|
|
|
|
", uncaught signal #{status.termsig}"
|
2013-10-15 20:14:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
raise Gem::InstallError, "#{command_name || class_name} failed#{exit_reason}"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
##
|
2013-10-15 20:14:16 -04:00
|
|
|
# Creates a new extension builder for +spec+. If the +spec+ does not yet
|
|
|
|
# have build arguments, saved, set +build_args+ which is an ARGV-style
|
|
|
|
# array.
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def initialize(spec, build_args = spec.build_args)
|
2013-09-14 04:59:02 -04:00
|
|
|
@spec = spec
|
|
|
|
@build_args = build_args
|
2013-11-30 18:27:52 -05:00
|
|
|
@gem_dir = spec.full_gem_path
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2019-08-22 11:36:29 -04:00
|
|
|
@ran_rake = false
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Chooses the extension builder class for +extension+
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def builder_for(extension) # :nodoc:
|
2013-09-14 04:59:02 -04:00
|
|
|
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
|
|
|
|
when /CMakeLists.txt/ then
|
|
|
|
Gem::Ext::CmakeBuilder
|
|
|
|
else
|
2019-07-28 09:58:03 -04:00
|
|
|
build_error("No builder for extension '#{extension}'")
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2019-07-28 09:50:11 -04:00
|
|
|
# Logs the build +output+, then raises Gem::Ext::BuildError.
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2019-07-28 09:50:11 -04:00
|
|
|
def build_error(output, backtrace = nil) # :nodoc:
|
2013-10-15 20:14:16 -04:00
|
|
|
gem_make_out = write_gem_make_out output
|
2013-09-14 04:59:02 -04:00
|
|
|
|
|
|
|
message = <<-EOF
|
|
|
|
ERROR: Failed to build gem native extension.
|
|
|
|
|
|
|
|
#{output}
|
|
|
|
|
|
|
|
Gem files will remain installed in #{@gem_dir} for inspection.
|
|
|
|
Results logged to #{gem_make_out}
|
|
|
|
EOF
|
|
|
|
|
2013-10-15 20:14:16 -04:00
|
|
|
raise Gem::Ext::BuildError, message, backtrace
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def build_extension(extension, dest_path) # :nodoc:
|
2013-09-14 04:59:02 -04:00
|
|
|
results = []
|
|
|
|
|
2019-07-28 10:03:01 -04:00
|
|
|
builder = builder_for(extension)
|
|
|
|
|
2013-10-15 20:14:16 -04:00
|
|
|
extension_dir =
|
2018-10-21 20:27:02 -04:00
|
|
|
File.expand_path File.join(@gem_dir, File.dirname(extension))
|
2013-11-18 19:34:13 -05:00
|
|
|
lib_dir = File.join @spec.full_gem_path, @spec.raw_require_paths.first
|
2013-09-14 04:59:02 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
FileUtils.mkdir_p dest_path
|
|
|
|
|
|
|
|
CHDIR_MUTEX.synchronize do
|
2018-05-30 09:01:35 -04:00
|
|
|
pwd = Dir.getwd
|
|
|
|
Dir.chdir extension_dir
|
|
|
|
begin
|
|
|
|
results = builder.build(extension, dest_path,
|
2013-11-18 19:34:13 -05:00
|
|
|
results, @build_args, lib_dir)
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
verbose { results.join("\n") }
|
2018-05-30 09:01:35 -04:00
|
|
|
ensure
|
|
|
|
begin
|
|
|
|
Dir.chdir pwd
|
|
|
|
rescue SystemCallError
|
|
|
|
Dir.chdir dest_path
|
|
|
|
end
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
end
|
2013-10-15 20:14:16 -04:00
|
|
|
|
|
|
|
write_gem_make_out results.join "\n"
|
|
|
|
rescue => e
|
|
|
|
results << e.message
|
2019-07-28 09:58:03 -04:00
|
|
|
build_error(results.join("\n"), $@)
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Builds extensions. Valid types of extensions are extconf.rb files,
|
|
|
|
# configure scripts and rakefiles or mkrf_conf files.
|
|
|
|
|
|
|
|
def build_extensions
|
|
|
|
return if @spec.extensions.empty?
|
|
|
|
|
|
|
|
if @build_args.empty?
|
2017-10-07 21:32:18 -04:00
|
|
|
say "Building native extensions. This could take a while..."
|
2013-09-14 04:59:02 -04:00
|
|
|
else
|
|
|
|
say "Building native extensions with: '#{@build_args.join ' '}'"
|
|
|
|
say "This could take a while..."
|
|
|
|
end
|
|
|
|
|
2013-12-10 14:54:19 -05:00
|
|
|
dest_path = @spec.extension_dir
|
2013-10-15 20:14:16 -04:00
|
|
|
|
|
|
|
FileUtils.rm_f @spec.gem_build_complete_path
|
2013-09-14 04:59:02 -04:00
|
|
|
|
|
|
|
@spec.extensions.each do |extension|
|
|
|
|
break if @ran_rake
|
|
|
|
|
|
|
|
build_extension extension, dest_path
|
|
|
|
end
|
2013-10-15 20:14:16 -04:00
|
|
|
|
|
|
|
FileUtils.touch @spec.gem_build_complete_path
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Writes +output+ to gem_make.out in the extension install directory.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def write_gem_make_out(output) # :nodoc:
|
2013-12-10 14:54:19 -05:00
|
|
|
destination = File.join @spec.extension_dir, 'gem_make.out'
|
2013-10-15 20:14:16 -04:00
|
|
|
|
2013-12-10 14:54:19 -05:00
|
|
|
FileUtils.mkdir_p @spec.extension_dir
|
2013-10-15 20:14:16 -04:00
|
|
|
|
2019-02-14 07:59:03 -05:00
|
|
|
File.open destination, 'wb' do |io|
|
|
|
|
io.puts output
|
|
|
|
end
|
2013-10-15 20:14:16 -04:00
|
|
|
|
|
|
|
destination
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|