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.
|
|
|
|
#++
|
|
|
|
|
|
|
|
class Gem::Ext::ExtConfBuilder < Gem::Ext::Builder
|
2020-12-08 02:33:39 -05:00
|
|
|
def self.build(extension, dest_path, results, args=[], lib_dir=nil, extension_dir=Dir.pwd)
|
2020-05-19 06:08:43 -04:00
|
|
|
require 'fileutils'
|
|
|
|
require 'tempfile'
|
|
|
|
|
2020-12-08 02:33:39 -05:00
|
|
|
tmp_dest = Dir.mktmpdir(".gem.", extension_dir)
|
2016-02-01 07:43:26 -05:00
|
|
|
|
|
|
|
# Some versions of `mktmpdir` return absolute paths, which will break make
|
|
|
|
# if the paths contain spaces. However, on Ruby 1.9.x on Windows, relative
|
|
|
|
# paths cause all C extension builds to fail.
|
|
|
|
#
|
|
|
|
# As such, we convert to a relative path unless we are using Ruby 1.9.x on
|
|
|
|
# Windows. This means that when using Ruby 1.9.x on Windows, paths with
|
|
|
|
# spaces do not work.
|
|
|
|
#
|
|
|
|
# Details: https://github.com/rubygems/rubygems/issues/977#issuecomment-171544940
|
2021-05-28 06:47:49 -04:00
|
|
|
tmp_dest_relative = get_relative_path(tmp_dest.clone, extension_dir)
|
2013-03-04 01:33:48 -05:00
|
|
|
|
2020-12-08 02:33:39 -05:00
|
|
|
Tempfile.open %w[siteconf .rb], extension_dir do |siteconf|
|
2013-03-04 22:31:28 -05:00
|
|
|
siteconf.puts "require 'rbconfig'"
|
2021-05-28 06:47:49 -04:00
|
|
|
siteconf.puts "dest_path = #{tmp_dest_relative.dump}"
|
2013-03-02 11:14:32 -05:00
|
|
|
%w[sitearchdir sitelibdir].each do |dir|
|
2013-03-04 22:31:28 -05:00
|
|
|
siteconf.puts "RbConfig::MAKEFILE_CONFIG['#{dir}'] = dest_path"
|
|
|
|
siteconf.puts "RbConfig::CONFIG['#{dir}'] = dest_path"
|
2013-02-28 17:25:55 -05:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2017-05-16 01:47:53 -04:00
|
|
|
siteconf.close
|
2013-03-04 22:31:28 -05:00
|
|
|
|
|
|
|
destdir = ENV["DESTDIR"]
|
2020-08-29 06:49:20 -04:00
|
|
|
|
2013-03-04 22:31:28 -05:00
|
|
|
begin
|
2020-12-08 02:33:39 -05:00
|
|
|
# workaround for https://github.com/oracle/truffleruby/issues/2115
|
|
|
|
siteconf_path = RUBY_ENGINE == "truffleruby" ? siteconf.path.dup : siteconf.path
|
2021-07-25 10:11:18 -04:00
|
|
|
require "shellwords"
|
2018-10-30 23:23:30 -04:00
|
|
|
cmd = Gem.ruby.shellsplit << "-I" << File.expand_path("../../..", __FILE__) <<
|
2020-12-08 02:33:39 -05:00
|
|
|
"-r" << get_relative_path(siteconf_path, extension_dir) << File.basename(extension)
|
2018-10-30 23:23:30 -04:00
|
|
|
cmd.push(*args)
|
2013-03-02 11:14:32 -05:00
|
|
|
|
2014-02-03 19:48:31 -05:00
|
|
|
begin
|
2020-12-08 02:33:39 -05:00
|
|
|
run(cmd, results, class_name, extension_dir) do |s, r|
|
|
|
|
mkmf_log = File.join(extension_dir, 'mkmf.log')
|
|
|
|
if File.exist? mkmf_log
|
2019-07-30 19:54:45 -04:00
|
|
|
unless s.success?
|
|
|
|
r << "To see why this extension failed to compile, please check" \
|
2019-04-29 03:07:16 -04:00
|
|
|
" the mkmf.log which can be found here:\n"
|
2019-07-30 19:54:45 -04:00
|
|
|
r << " " + File.join(dest_path, 'mkmf.log') + "\n"
|
2019-04-29 03:07:16 -04:00
|
|
|
end
|
2020-12-08 02:33:39 -05:00
|
|
|
FileUtils.mv mkmf_log, dest_path
|
2017-01-23 21:38:57 -05:00
|
|
|
end
|
2015-07-01 17:50:14 -04:00
|
|
|
end
|
2014-05-26 10:36:36 -04:00
|
|
|
siteconf.unlink
|
2014-02-03 19:48:31 -05:00
|
|
|
end
|
2013-03-02 11:14:32 -05:00
|
|
|
|
2013-03-04 22:31:28 -05:00
|
|
|
ENV["DESTDIR"] = nil
|
2013-03-04 20:06:08 -05:00
|
|
|
|
2020-12-08 02:33:39 -05:00
|
|
|
make dest_path, results, extension_dir
|
2013-03-02 11:14:32 -05:00
|
|
|
|
2021-05-28 06:47:49 -04:00
|
|
|
if tmp_dest_relative
|
|
|
|
full_tmp_dest = File.join(extension_dir, tmp_dest_relative)
|
2020-12-08 02:33:39 -05:00
|
|
|
|
2013-12-01 15:52:57 -05:00
|
|
|
# TODO remove in RubyGems 3
|
2018-11-21 05:20:47 -05:00
|
|
|
if Gem.install_extension_in_lib and lib_dir
|
2013-12-01 15:52:57 -05:00
|
|
|
FileUtils.mkdir_p lib_dir
|
2020-12-08 02:33:39 -05:00
|
|
|
entries = Dir.entries(full_tmp_dest) - %w[. ..]
|
|
|
|
entries = entries.map {|entry| File.join full_tmp_dest, entry }
|
2014-12-06 19:53:01 -05:00
|
|
|
FileUtils.cp_r entries, lib_dir, :remove_destination => true
|
2013-12-01 15:52:57 -05:00
|
|
|
end
|
2013-11-18 19:34:13 -05:00
|
|
|
|
2020-12-08 02:33:39 -05:00
|
|
|
FileUtils::Entry_.new(full_tmp_dest).traverse do |ent|
|
2013-03-04 22:31:28 -05:00
|
|
|
destent = ent.class.new(dest_path, ent.rel)
|
2014-09-13 23:30:02 -04:00
|
|
|
destent.exist? or FileUtils.mv(ent.path, destent.path)
|
2013-03-04 22:31:28 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
ENV["DESTDIR"] = destdir
|
2020-08-29 06:49:20 -04:00
|
|
|
siteconf.close!
|
2013-03-04 01:33:48 -05:00
|
|
|
end
|
|
|
|
end
|
2013-07-22 18:46:50 -04:00
|
|
|
|
|
|
|
results
|
2013-03-02 11:14:32 -05:00
|
|
|
ensure
|
2013-03-04 01:33:48 -05:00
|
|
|
FileUtils.rm_rf tmp_dest if tmp_dest
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
private
|
2019-06-04 04:21:14 -04:00
|
|
|
|
2020-12-08 02:33:39 -05:00
|
|
|
def self.get_relative_path(path, base)
|
|
|
|
path[0..base.length - 1] = '.' if path.start_with?(base)
|
2014-09-13 23:30:02 -04:00
|
|
|
path
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|