summaryrefslogtreecommitdiff
path: root/jni/ruby/lib/rubygems/ext
diff options
context:
space:
mode:
authorJari Vetoniemi <jari.vetoniemi@indooratlas.com>2020-03-16 18:49:26 +0900
committerJari Vetoniemi <jari.vetoniemi@indooratlas.com>2020-03-30 00:39:06 +0900
commitfcbf63e62c627deae76c1b8cb8c0876c536ed811 (patch)
tree64cb17de3f41a2b6fef2368028fbd00349946994 /jni/ruby/lib/rubygems/ext
Fresh start
Diffstat (limited to 'jni/ruby/lib/rubygems/ext')
-rw-r--r--jni/ruby/lib/rubygems/ext/build_error.rb6
-rw-r--r--jni/ruby/lib/rubygems/ext/builder.rb218
-rw-r--r--jni/ruby/lib/rubygems/ext/cmake_builder.rb16
-rw-r--r--jni/ruby/lib/rubygems/ext/configure_builder.rb23
-rw-r--r--jni/ruby/lib/rubygems/ext/ext_conf_builder.rb78
-rw-r--r--jni/ruby/lib/rubygems/ext/rake_builder.rb36
6 files changed, 377 insertions, 0 deletions
diff --git a/jni/ruby/lib/rubygems/ext/build_error.rb b/jni/ruby/lib/rubygems/ext/build_error.rb
new file mode 100644
index 0000000..bfe85ff
--- /dev/null
+++ b/jni/ruby/lib/rubygems/ext/build_error.rb
@@ -0,0 +1,6 @@
+##
+# Raised when there is an error while building extensions.
+
+class Gem::Ext::BuildError < Gem::InstallError
+end
+
diff --git a/jni/ruby/lib/rubygems/ext/builder.rb b/jni/ruby/lib/rubygems/ext/builder.rb
new file mode 100644
index 0000000..548f126
--- /dev/null
+++ b/jni/ruby/lib/rubygems/ext/builder.rb
@@ -0,0 +1,218 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'rubygems/user_interaction'
+require 'thread'
+
+class Gem::Ext::Builder
+
+ 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:
+
+ def self.class_name
+ name =~ /Ext::(.*)Builder/
+ $1.downcase
+ end
+
+ def self.make(dest_path, results)
+ unless File.exist? 'Makefile' then
+ raise Gem::InstallError, 'Makefile not found'
+ end
+
+ # try to find make program from Ruby configure arguments first
+ RbConfig::CONFIG['configure_args'] =~ /with-make-prog\=(\w+)/
+ make_program = ENV['MAKE'] || ENV['make'] || $1
+ unless make_program then
+ make_program = (/mswin/ =~ RUBY_PLATFORM) ? 'nmake' : 'make'
+ end
+
+ destdir = '"DESTDIR=%s"' % ENV['DESTDIR'] if RUBY_VERSION > '2.0'
+
+ ['clean', '', 'install'].each do |target|
+ # Pass DESTDIR via command line to override what's in MAKEFLAGS
+ cmd = [
+ make_program,
+ destdir,
+ target
+ ].join(' ').rstrip
+ begin
+ run(cmd, results, "make #{target}".rstrip)
+ rescue Gem::InstallError
+ raise unless target == 'clean' # ignore clean failure
+ end
+ end
+ end
+
+ def self.redirector
+ '2>&1'
+ end
+
+ def self.run(command, results, command_name = nil)
+ verbose = Gem.configuration.really_verbose
+
+ begin
+ # TODO use Process.spawn when ruby 1.8 support is dropped.
+ rubygems_gemdeps, ENV['RUBYGEMS_GEMDEPS'] = ENV['RUBYGEMS_GEMDEPS'], nil
+ if verbose
+ puts(command)
+ system(command)
+ else
+ results << command
+ results << `#{command} #{redirector}`
+ end
+ ensure
+ ENV['RUBYGEMS_GEMDEPS'] = rubygems_gemdeps
+ end
+
+ unless $?.success? then
+ results << "Building has failed. See above output for more information on the failure." if verbose
+
+ exit_reason =
+ if $?.exited? then
+ ", exit code #{$?.exitstatus}"
+ elsif $?.signaled? then
+ ", uncaught signal #{$?.termsig}"
+ end
+
+ raise Gem::InstallError, "#{command_name || class_name} failed#{exit_reason}"
+ end
+ end
+
+ ##
+ # 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.
+
+ def initialize spec, build_args = spec.build_args
+ @spec = spec
+ @build_args = build_args
+ @gem_dir = spec.full_gem_path
+
+ @ran_rake = nil
+ end
+
+ ##
+ # Chooses the extension builder class for +extension+
+
+ def builder_for extension # :nodoc:
+ 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
+ extension_dir = File.join @gem_dir, File.dirname(extension)
+
+ message = "No builder for extension '#{extension}'"
+ build_error extension_dir, message
+ end
+ end
+
+ ##
+ # Logs the build +output+ in +build_dir+, then raises Gem::Ext::BuildError.
+
+ def build_error build_dir, output, backtrace = nil # :nodoc:
+ gem_make_out = write_gem_make_out output
+
+ 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
+
+ raise Gem::Ext::BuildError, message, backtrace
+ end
+
+ def build_extension extension, dest_path # :nodoc:
+ results = []
+
+ extension ||= '' # I wish I knew why this line existed
+ extension_dir =
+ File.expand_path File.join @gem_dir, File.dirname(extension)
+ lib_dir = File.join @spec.full_gem_path, @spec.raw_require_paths.first
+
+ builder = builder_for extension
+
+ begin
+ FileUtils.mkdir_p dest_path
+
+ CHDIR_MUTEX.synchronize do
+ Dir.chdir extension_dir do
+ results = builder.build(extension, @gem_dir, dest_path,
+ results, @build_args, lib_dir)
+
+ verbose { results.join("\n") }
+ end
+ end
+
+ write_gem_make_out results.join "\n"
+ rescue => e
+ results << e.message
+ build_error extension_dir, results.join("\n"), $@
+ 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?
+ say "Building native extensions. This could take a while..."
+ else
+ say "Building native extensions with: '#{@build_args.join ' '}'"
+ say "This could take a while..."
+ end
+
+ dest_path = @spec.extension_dir
+
+ FileUtils.rm_f @spec.gem_build_complete_path
+
+ @ran_rake = false # only run rake once
+
+ @spec.extensions.each do |extension|
+ break if @ran_rake
+
+ build_extension extension, dest_path
+ end
+
+ FileUtils.touch @spec.gem_build_complete_path
+ end
+
+ ##
+ # Writes +output+ to gem_make.out in the extension install directory.
+
+ def write_gem_make_out output # :nodoc:
+ destination = File.join @spec.extension_dir, 'gem_make.out'
+
+ FileUtils.mkdir_p @spec.extension_dir
+
+ open destination, 'wb' do |io| io.puts output end
+
+ destination
+ end
+
+end
+
diff --git a/jni/ruby/lib/rubygems/ext/cmake_builder.rb b/jni/ruby/lib/rubygems/ext/cmake_builder.rb
new file mode 100644
index 0000000..24531bc
--- /dev/null
+++ b/jni/ruby/lib/rubygems/ext/cmake_builder.rb
@@ -0,0 +1,16 @@
+require 'rubygems/command'
+
+class Gem::Ext::CmakeBuilder < Gem::Ext::Builder
+ def self.build(extension, directory, dest_path, results, args=[], lib_dir=nil)
+ unless File.exist?('Makefile') then
+ cmd = "cmake . -DCMAKE_INSTALL_PREFIX=#{dest_path}"
+ cmd << " #{Gem::Command.build_args.join ' '}" unless Gem::Command.build_args.empty?
+
+ run cmd, results
+ end
+
+ make dest_path, results
+
+ results
+ end
+end
diff --git a/jni/ruby/lib/rubygems/ext/configure_builder.rb b/jni/ruby/lib/rubygems/ext/configure_builder.rb
new file mode 100644
index 0000000..f66e393
--- /dev/null
+++ b/jni/ruby/lib/rubygems/ext/configure_builder.rb
@@ -0,0 +1,23 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+class Gem::Ext::ConfigureBuilder < Gem::Ext::Builder
+
+ def self.build(extension, directory, dest_path, results, args=[], lib_dir=nil)
+ unless File.exist?('Makefile') then
+ cmd = "sh ./configure --prefix=#{dest_path}"
+ cmd << " #{args.join ' '}" unless args.empty?
+
+ run cmd, results
+ end
+
+ make dest_path, results
+
+ results
+ end
+
+end
+
diff --git a/jni/ruby/lib/rubygems/ext/ext_conf_builder.rb b/jni/ruby/lib/rubygems/ext/ext_conf_builder.rb
new file mode 100644
index 0000000..d11d1ac
--- /dev/null
+++ b/jni/ruby/lib/rubygems/ext/ext_conf_builder.rb
@@ -0,0 +1,78 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+require 'fileutils'
+require 'tempfile'
+
+class Gem::Ext::ExtConfBuilder < Gem::Ext::Builder
+ FileEntry = FileUtils::Entry_ # :nodoc:
+
+ def self.build(extension, directory, dest_path, results, args=[], lib_dir=nil)
+ # relative path required as some versions of mktmpdir return an absolute
+ # path which breaks make if it includes a space in the name
+ tmp_dest = get_relative_path(Dir.mktmpdir(".gem.", "."))
+
+ t = nil
+ Tempfile.open %w"siteconf .rb", "." do |siteconf|
+ t = siteconf
+ siteconf.puts "require 'rbconfig'"
+ siteconf.puts "dest_path = #{tmp_dest.dump}"
+ %w[sitearchdir sitelibdir].each do |dir|
+ siteconf.puts "RbConfig::MAKEFILE_CONFIG['#{dir}'] = dest_path"
+ siteconf.puts "RbConfig::CONFIG['#{dir}'] = dest_path"
+ end
+
+ siteconf.flush
+
+ destdir = ENV["DESTDIR"]
+
+ begin
+ cmd = [Gem.ruby, "-r", get_relative_path(siteconf.path), File.basename(extension), *args].join ' '
+
+ begin
+ run cmd, results
+ ensure
+ FileUtils.mv 'mkmf.log', dest_path if File.exist? 'mkmf.log'
+ siteconf.unlink
+ end
+
+ ENV["DESTDIR"] = nil
+
+ make dest_path, results
+
+ if tmp_dest
+ # TODO remove in RubyGems 3
+ if Gem.install_extension_in_lib and lib_dir then
+ FileUtils.mkdir_p lib_dir
+ entries = Dir.entries(tmp_dest) - %w[. ..]
+ entries = entries.map { |entry| File.join tmp_dest, entry }
+ FileUtils.cp_r entries, lib_dir, :remove_destination => true
+ end
+
+ FileEntry.new(tmp_dest).traverse do |ent|
+ destent = ent.class.new(dest_path, ent.rel)
+ destent.exist? or FileUtils.mv(ent.path, destent.path)
+ end
+ end
+ ensure
+ ENV["DESTDIR"] = destdir
+ end
+ end
+ t.unlink if t and t.path
+
+ results
+ ensure
+ FileUtils.rm_rf tmp_dest if tmp_dest
+ end
+
+ private
+ def self.get_relative_path(path)
+ path[0..Dir.pwd.length-1] = '.' if path.start_with?(Dir.pwd)
+ path
+ end
+
+end
+
diff --git a/jni/ruby/lib/rubygems/ext/rake_builder.rb b/jni/ruby/lib/rubygems/ext/rake_builder.rb
new file mode 100644
index 0000000..d5ebf59
--- /dev/null
+++ b/jni/ruby/lib/rubygems/ext/rake_builder.rb
@@ -0,0 +1,36 @@
+#--
+# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
+# All rights reserved.
+# See LICENSE.txt for permissions.
+#++
+
+class Gem::Ext::RakeBuilder < Gem::Ext::Builder
+
+ def self.build(extension, directory, dest_path, results, args=[], lib_dir=nil)
+ if File.basename(extension) =~ /mkrf_conf/i then
+ cmd = "#{Gem.ruby} #{File.basename extension}"
+ cmd << " #{args.join " "}" unless args.empty?
+ run cmd, results
+ end
+
+ # Deal with possible spaces in the path, e.g. C:/Program Files
+ dest_path = '"' + dest_path.to_s + '"' if dest_path.to_s.include?(' ')
+
+ rake = ENV['rake']
+
+ rake ||= begin
+ "#{Gem.ruby} -rubygems #{Gem.bin_path('rake', 'rake')}"
+ rescue Gem::Exception
+ end
+
+ rake ||= Gem.default_exec_format % 'rake'
+
+ cmd = "#{rake} RUBYARCHDIR=#{dest_path} RUBYLIBDIR=#{dest_path}" # ENV is frozen
+
+ run cmd, results
+
+ results
+ end
+
+end
+