2016-02-01 15:43:26 +03:00
|
|
|
# frozen_string_literal: true
|
2023-03-17 12:36:42 +03:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
#--
|
|
|
|
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
|
|
|
|
# All rights reserved.
|
|
|
|
# See LICENSE.txt for permissions.
|
|
|
|
#++
|
|
|
|
|
2020-05-16 16:42:27 +03:00
|
|
|
require_relative "../user_interaction"
|
2013-09-14 12:59:02 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
class Gem::Ext::Builder
|
2013-09-14 12:59:02 +04:00
|
|
|
include Gem::UserInteraction
|
|
|
|
|
|
|
|
attr_accessor :build_args # :nodoc:
|
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def self.class_name
|
|
|
|
name =~ /Ext::(.*)Builder/
|
|
|
|
$1.downcase
|
|
|
|
end
|
|
|
|
|
2022-11-30 05:40:02 +03:00
|
|
|
def self.make(dest_path, results, make_dir = Dir.pwd, sitedir = nil, targets = ["clean", "", "install"])
|
2020-12-08 10:33:39 +03:00
|
|
|
unless File.exist? File.join(make_dir, "Makefile")
|
2013-10-16 04:14:16 +04:00
|
|
|
raise Gem::InstallError, "Makefile not found"
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
2011-01-29 02:46:47 +03:00
|
|
|
# try to find make program from Ruby configure arguments first
|
2011-01-19 03:08:49 +03:00
|
|
|
RbConfig::CONFIG["configure_args"] =~ /with-make-prog\=(\w+)/
|
2021-11-06 01:22:35 +03:00
|
|
|
make_program_name = ENV["MAKE"] || ENV["make"] || $1
|
2023-03-16 07:55:04 +03:00
|
|
|
make_program_name ||= RUBY_PLATFORM.include?("mswin") ? "nmake" : "make"
|
2021-11-06 01:22:35 +03:00
|
|
|
make_program = Shellwords.split(make_program_name)
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2021-11-04 07:59:41 +03:00
|
|
|
# The installation of the bundled gems is failed when DESTDIR is empty in mswin platform.
|
2023-03-16 07:52:28 +03:00
|
|
|
destdir = /\bnmake/i !~ make_program_name || ENV["DESTDIR"] && ENV["DESTDIR"] != "" ? "DESTDIR=%s" % ENV["DESTDIR"] : ""
|
2013-07-23 02:46:50 +04:00
|
|
|
|
2022-06-14 16:26:37 +03:00
|
|
|
env = [destdir]
|
|
|
|
|
|
|
|
if sitedir
|
|
|
|
env << "sitearchdir=%s" % sitedir
|
|
|
|
env << "sitelibdir=%s" % sitedir
|
|
|
|
end
|
|
|
|
|
2022-11-30 05:40:02 +03:00
|
|
|
targets.each do |target|
|
2013-06-25 17:28:57 +04:00
|
|
|
# Pass DESTDIR via command line to override what's in MAKEFLAGS
|
|
|
|
cmd = [
|
2021-02-01 18:17:16 +03:00
|
|
|
*make_program,
|
2022-06-14 16:26:37 +03:00
|
|
|
*env,
|
2020-12-08 10:33:39 +03:00
|
|
|
target,
|
2021-01-04 04:09:05 +03:00
|
|
|
].reject(&:empty?)
|
2013-10-16 04:43:14 +04:00
|
|
|
begin
|
2020-12-08 10:33:39 +03:00
|
|
|
run(cmd, results, "make #{target}".rstrip, make_dir)
|
2013-10-16 04:43:14 +04:00
|
|
|
rescue Gem::InstallError
|
|
|
|
raise unless target == "clean" # ignore clean failure
|
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-17 19:48:52 +03:00
|
|
|
def self.ruby
|
|
|
|
require "shellwords"
|
|
|
|
# Gem.ruby is quoted if it contains whitespace
|
|
|
|
cmd = Gem.ruby.shellsplit
|
|
|
|
|
2023-03-17 19:16:03 +03:00
|
|
|
# This load_path is only needed when running rubygems test without a proper installation.
|
|
|
|
# Prepending it in a normal installation will cause problem with order of $LOAD_PATH.
|
|
|
|
# Therefore only add load_path if it is not present in the default $LOAD_PATH.
|
|
|
|
load_path = File.expand_path("../..", __dir__)
|
|
|
|
case load_path
|
|
|
|
when RbConfig::CONFIG["sitelibdir"], RbConfig::CONFIG["vendorlibdir"], RbConfig::CONFIG["rubylibdir"]
|
2023-03-17 19:48:52 +03:00
|
|
|
cmd
|
2023-03-17 19:16:03 +03:00
|
|
|
else
|
2023-03-17 19:48:52 +03:00
|
|
|
cmd << "-I#{load_path}"
|
2023-03-17 19:16:03 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-23 12:22:36 +03:00
|
|
|
def self.run(command, results, command_name = nil, dir = Dir.pwd, env = {})
|
2012-11-29 10:52:18 +04:00
|
|
|
verbose = Gem.configuration.really_verbose
|
|
|
|
|
2013-02-05 06:37:35 +04:00
|
|
|
begin
|
2023-03-16 07:07:05 +03:00
|
|
|
rubygems_gemdeps = ENV["RUBYGEMS_GEMDEPS"]
|
|
|
|
ENV["RUBYGEMS_GEMDEPS"] = nil
|
2013-02-05 06:37:35 +04:00
|
|
|
if verbose
|
2020-12-08 10:33:39 +03:00
|
|
|
puts("current directory: #{dir}")
|
2018-11-04 18:39:00 +03:00
|
|
|
p(command)
|
|
|
|
end
|
2020-12-08 10:33:39 +03:00
|
|
|
results << "current directory: #{dir}"
|
2021-07-25 17:11:18 +03:00
|
|
|
require "shellwords"
|
2021-01-04 04:09:05 +03:00
|
|
|
results << command.shelljoin
|
2018-11-04 18:39:00 +03:00
|
|
|
|
2019-12-13 14:19:08 +03:00
|
|
|
require "open3"
|
2020-03-24 09:39:24 +03:00
|
|
|
# Set $SOURCE_DATE_EPOCH for the subprocess.
|
2022-06-23 12:22:36 +03:00
|
|
|
build_env = { "SOURCE_DATE_EPOCH" => Gem.source_date_epoch_string }.merge(env)
|
2021-01-04 04:09:05 +03:00
|
|
|
output, status = begin
|
2022-06-23 12:22:36 +03:00
|
|
|
Open3.capture2e(build_env, *command, :chdir => dir)
|
2023-03-16 07:47:12 +03:00
|
|
|
rescue StandardError => error
|
2021-01-04 04:09:05 +03:00
|
|
|
raise Gem::InstallError, "#{command_name || class_name} failed#{error.message}"
|
|
|
|
end
|
2019-04-29 10:07:16 +03:00
|
|
|
if verbose
|
|
|
|
puts output
|
|
|
|
else
|
|
|
|
results << output
|
2013-02-05 06:37:35 +04:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
ENV["RUBYGEMS_GEMDEPS"] = rubygems_gemdeps
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2019-04-29 10:07:16 +03:00
|
|
|
unless status.success?
|
2012-11-29 10:52:18 +04:00
|
|
|
results << "Building has failed. See above output for more information on the failure." if verbose
|
2019-04-29 10:07:16 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
yield(status, results) if block_given?
|
2013-10-16 04:14:16 +04:00
|
|
|
|
2019-04-29 10:07:16 +03:00
|
|
|
unless status.success?
|
2013-10-16 04:14:16 +04:00
|
|
|
exit_reason =
|
2019-04-29 10:07:16 +03:00
|
|
|
if status.exited?
|
|
|
|
", exit code #{status.exitstatus}"
|
|
|
|
elsif status.signaled?
|
|
|
|
", uncaught signal #{status.termsig}"
|
2013-10-16 04:14:16 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
raise Gem::InstallError, "#{command_name || class_name} failed#{exit_reason}"
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
2013-10-16 04: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 12:59:02 +04:00
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def initialize(spec, build_args = spec.build_args)
|
2013-09-14 12:59:02 +04:00
|
|
|
@spec = spec
|
|
|
|
@build_args = build_args
|
2013-12-01 03:27:52 +04:00
|
|
|
@gem_dir = spec.full_gem_path
|
2013-09-14 12:59:02 +04:00
|
|
|
|
2019-08-22 18:36:29 +03:00
|
|
|
@ran_rake = false
|
2013-09-14 12:59:02 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Chooses the extension builder class for +extension+
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def builder_for(extension) # :nodoc:
|
2013-09-14 12: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
|
2022-03-16 14:52:46 +03:00
|
|
|
when /Cargo.toml/ then
|
2023-01-23 08:16:12 +03:00
|
|
|
Gem::Ext::CargoBuilder.new
|
2013-09-14 12:59:02 +04:00
|
|
|
else
|
2019-07-28 16:58:03 +03:00
|
|
|
build_error("No builder for extension '#{extension}'")
|
2013-09-14 12:59:02 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2019-07-28 16:50:11 +03:00
|
|
|
# Logs the build +output+, then raises Gem::Ext::BuildError.
|
2013-09-14 12:59:02 +04:00
|
|
|
|
2019-07-28 16:50:11 +03:00
|
|
|
def build_error(output, backtrace = nil) # :nodoc:
|
2013-10-16 04:14:16 +04:00
|
|
|
gem_make_out = write_gem_make_out output
|
2013-09-14 12: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-16 04:14:16 +04:00
|
|
|
raise Gem::Ext::BuildError, message, backtrace
|
2013-09-14 12:59:02 +04:00
|
|
|
end
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def build_extension(extension, dest_path) # :nodoc:
|
2013-09-14 12:59:02 +04:00
|
|
|
results = []
|
|
|
|
|
2019-07-28 17:03:01 +03:00
|
|
|
builder = builder_for(extension)
|
|
|
|
|
2013-10-16 04:14:16 +04:00
|
|
|
extension_dir =
|
2018-10-22 03:27:02 +03:00
|
|
|
File.expand_path File.join(@gem_dir, File.dirname(extension))
|
2013-11-19 04:34:13 +04:00
|
|
|
lib_dir = File.join @spec.full_gem_path, @spec.raw_require_paths.first
|
2013-09-14 12:59:02 +04:00
|
|
|
|
|
|
|
begin
|
|
|
|
FileUtils.mkdir_p dest_path
|
|
|
|
|
2020-12-08 10:33:39 +03:00
|
|
|
results = builder.build(extension, dest_path,
|
|
|
|
results, @build_args, lib_dir, extension_dir)
|
|
|
|
|
|
|
|
verbose { results.join("\n") }
|
2013-10-16 04:14:16 +04:00
|
|
|
|
|
|
|
write_gem_make_out results.join "\n"
|
2023-03-16 07:47:12 +03:00
|
|
|
rescue StandardError => e
|
2013-10-16 04:14:16 +04:00
|
|
|
results << e.message
|
2019-07-28 16:58:03 +03:00
|
|
|
build_error(results.join("\n"), $@)
|
2013-09-14 12: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-08 04:32:18 +03:00
|
|
|
say "Building native extensions. This could take a while..."
|
2013-09-14 12:59:02 +04:00
|
|
|
else
|
2023-03-16 07:00:54 +03:00
|
|
|
say "Building native extensions with: '#{@build_args.join " "}'"
|
2013-09-14 12:59:02 +04:00
|
|
|
say "This could take a while..."
|
|
|
|
end
|
|
|
|
|
2013-12-10 23:54:19 +04:00
|
|
|
dest_path = @spec.extension_dir
|
2013-10-16 04:14:16 +04:00
|
|
|
|
2020-12-08 10:33:39 +03:00
|
|
|
require "fileutils"
|
2013-10-16 04:14:16 +04:00
|
|
|
FileUtils.rm_f @spec.gem_build_complete_path
|
2013-09-14 12:59:02 +04:00
|
|
|
|
|
|
|
@spec.extensions.each do |extension|
|
|
|
|
break if @ran_rake
|
|
|
|
|
|
|
|
build_extension extension, dest_path
|
|
|
|
end
|
2013-10-16 04: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 13:20:47 +03:00
|
|
|
def write_gem_make_out(output) # :nodoc:
|
2013-12-10 23:54:19 +04:00
|
|
|
destination = File.join @spec.extension_dir, "gem_make.out"
|
2013-10-16 04:14:16 +04:00
|
|
|
|
2013-12-10 23:54:19 +04:00
|
|
|
FileUtils.mkdir_p @spec.extension_dir
|
2013-10-16 04:14:16 +04:00
|
|
|
|
2019-02-14 15:59:03 +03:00
|
|
|
File.open destination, "wb" do |io|
|
|
|
|
io.puts output
|
|
|
|
end
|
2013-10-16 04:14:16 +04:00
|
|
|
|
|
|
|
destination
|
2013-09-14 12:59:02 +04:00
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|