c2061af
From 111f8422427d78becc9183ae149b2105a16bf327 Mon Sep 17 00:00:00 2001
c2061af
From: Nobuyoshi Nakada <nobu@ruby-lang.org>
c2061af
Date: Tue, 5 Apr 2022 23:24:00 +0900
c2061af
Subject: [PATCH 1/5] Bundled gems are expanded under `.bundle/gems` now
c2061af
c2061af
---
c2061af
 ext/extmk.rb          | 13 +++++++------
c2061af
 template/exts.mk.tmpl |  2 +-
c2061af
 2 files changed, 8 insertions(+), 7 deletions(-)
c2061af
c2061af
diff --git a/ext/extmk.rb b/ext/extmk.rb
c2061af
index 4a087f294ac9..1da9e2704521 100755
c2061af
--- a/ext/extmk.rb
c2061af
+++ b/ext/extmk.rb
c2061af
@@ -146,7 +146,7 @@ def extmake(target, basedir = 'ext', maybestatic = true)
c2061af
     top_srcdir = $top_srcdir
c2061af
     topdir = $topdir
c2061af
     hdrdir = $hdrdir
c2061af
-    prefix = "../" * (target.count("/")+1)
c2061af
+    prefix = "../" * (basedir.count("/")+target.count("/")+1)
c2061af
     $top_srcdir = relative_from(top_srcdir, prefix)
c2061af
     $hdrdir = relative_from(hdrdir, prefix)
c2061af
     $topdir = prefix + $topdir
c2061af
@@ -460,10 +460,11 @@ def $mflags.defined?(var)
c2061af
 end unless $extstatic
c2061af
 
c2061af
 @gemname = nil
c2061af
-if ARGV[0]
c2061af
-  ext_prefix, exts = ARGV.shift.split('/', 2)
c2061af
+if exts = ARGV.shift
c2061af
+  ext_prefix = exts[%r[\A(?>\.bundle/)?[^/]+(?:/(?=(.+)?)|\z)]]
c2061af
+  exts = $1
c2061af
   $extension = [exts] if exts
c2061af
-  if ext_prefix == 'gems'
c2061af
+  if ext_prefix.start_with?('.')
c2061af
     @gemname = exts
c2061af
   elsif exts
c2061af
     $static_ext.delete_if {|t, *| !File.fnmatch(t, exts)}
c2061af
@@ -515,7 +516,7 @@ def $mflags.defined?(var)
c2061af
     exts.delete_if {|d| File.fnmatch?("-*", d)}
c2061af
   end
c2061af
 end
c2061af
-ext_prefix = File.basename(ext_prefix)
c2061af
+ext_prefix = ext_prefix[$top_srcdir.size+1..-2]
c2061af
 
c2061af
 extend Module.new {
c2061af
   def timestamp_file(name, target_prefix = nil)
c2061af
@@ -634,7 +635,7 @@ def initialize(src)
c2061af
   end
c2061af
 }
c2061af
 
c2061af
-Dir.chdir ".."
c2061af
+Dir.chdir dir
c2061af
 unless $destdir.to_s.empty?
c2061af
   $mflags.defined?("DESTDIR") or $mflags << "DESTDIR=#{$destdir}"
c2061af
 end
c2061af
diff --git a/template/exts.mk.tmpl b/template/exts.mk.tmpl
c2061af
index 2f37f4480ac5..964939e365a1 100644
c2061af
--- a/template/exts.mk.tmpl
c2061af
+++ b/template/exts.mk.tmpl
c2061af
@@ -25,7 +25,7 @@ macros["old_extensions"] = []
c2061af
 
c2061af
 contpat = /(?>(?>[^\\\n]|\\.)*\\\n)*(?>[^\\\n]|\\.)*/
c2061af
 Dir.glob("{ext,.bundle/gems}/*/exts.mk") do |e|
c2061af
-  gem = /\Agems(?=\/)/ =~ e
c2061af
+  gem = e.start_with?(".bundle/gems/")
c2061af
   s = File.read(e)
c2061af
   s.scan(/^(extensions|SUBMAKEOPTS|EXT[A-Z]+|MFLAGS|NOTE_[A-Z]+)[ \t]*=[ \t]*(#{contpat})$/o) do |n, v|
c2061af
     v.gsub!(/\\\n[ \t]*/, ' ')
c2061af
c2061af
From 6ea34cac22131d28a9cc50e7875e854aed9bdb88 Mon Sep 17 00:00:00 2001
c2061af
From: Nobuyoshi Nakada <nobu@ruby-lang.org>
c2061af
Date: Wed, 6 Apr 2022 20:25:53 +0900
c2061af
Subject: [PATCH 2/5] Retrieve configured gems info
c2061af
c2061af
---
c2061af
 template/configure-ext.mk.tmpl | 2 +-
c2061af
 template/exts.mk.tmpl          | 4 ++--
c2061af
 2 files changed, 3 insertions(+), 3 deletions(-)
c2061af
c2061af
diff --git a/template/configure-ext.mk.tmpl b/template/configure-ext.mk.tmpl
c2061af
index 6636a7759c54..8ba6b963e3ec 100644
c2061af
--- a/template/configure-ext.mk.tmpl
c2061af
+++ b/template/configure-ext.mk.tmpl
c2061af
@@ -27,7 +27,7 @@ SCRIPT_ARGS = <%=script_args.gsub("#", "\\#")%>
c2061af
 EXTMK_ARGS = $(SCRIPT_ARGS) --gnumake=$(gnumake) --extflags="$(EXTLDFLAGS)" \
c2061af
 	   --make-flags="MINIRUBY='$(MINIRUBY)'"
c2061af
 
c2061af
-all: exts # gems
c2061af
+all: exts gems
c2061af
 exts:
c2061af
 gems:
c2061af
 
c2061af
diff --git a/template/exts.mk.tmpl b/template/exts.mk.tmpl
c2061af
index 964939e365a1..e544c4c88bd7 100644
c2061af
--- a/template/exts.mk.tmpl
c2061af
+++ b/template/exts.mk.tmpl
c2061af
@@ -19,7 +19,7 @@ opt = OptionParser.new do |o|
c2061af
   o.on('--configure-exts=FILE') {|v| confexts = v}
c2061af
   o.order!(ARGV)
c2061af
 end
c2061af
-confexts &&= File.read(confexts).scan(/^exts: (.*\.mk)/).flatten rescue nil
c2061af
+confexts &&= File.read(confexts).scan(/^(?:ext|gem)s: (.*\.mk)/).flatten rescue nil
c2061af
 confexts ||= []
c2061af
 macros["old_extensions"] = []
c2061af
 
c2061af
@@ -30,7 +30,7 @@ Dir.glob("{ext,.bundle/gems}/*/exts.mk") do |e|
c2061af
   s.scan(/^(extensions|SUBMAKEOPTS|EXT[A-Z]+|MFLAGS|NOTE_[A-Z]+)[ \t]*=[ \t]*(#{contpat})$/o) do |n, v|
c2061af
     v.gsub!(/\\\n[ \t]*/, ' ')
c2061af
     next if v.empty?
c2061af
-    next if gem and n != "extensions"
c2061af
+    next if n != "extensions"
c2061af
     n = "old_extensions" if n == "extensions" and !confexts.include?(e)
c2061af
     v = v.split
c2061af
     m = macros[n] ||= []
c2061af
c2061af
From be9d00ee7c72766551ba8c3530f1538034498a6a Mon Sep 17 00:00:00 2001
c2061af
From: Nobuyoshi Nakada <nobu@ruby-lang.org>
c2061af
Date: Wed, 6 Apr 2022 20:28:00 +0900
c2061af
Subject: [PATCH 3/5] Move the target directory of bundled gems like as
c2061af
 rubygems
c2061af
c2061af
---
c2061af
 ext/extmk.rb | 6 +++++-
c2061af
 1 file changed, 5 insertions(+), 1 deletion(-)
c2061af
c2061af
diff --git a/ext/extmk.rb b/ext/extmk.rb
c2061af
index 1da9e2704521..a440af27fc5d 100755
c2061af
--- a/ext/extmk.rb
c2061af
+++ b/ext/extmk.rb
c2061af
@@ -2,6 +2,9 @@
c2061af
 # -*- mode: ruby; coding: us-ascii -*-
c2061af
 # frozen_string_literal: false
c2061af
 
c2061af
+module Gem; end # only needs Gem::Platform
c2061af
+require 'rubygems/platform'
c2061af
+
c2061af
 # :stopdoc:
c2061af
 $extension = nil
c2061af
 $extstatic = nil
c2061af
@@ -535,11 +538,12 @@ def create_makefile(*args, &block)
c2061af
     super(*args) do |conf|
c2061af
       conf.find do |s|
c2061af
         s.sub!(/^(TARGET_SO_DIR *= *)\$\(RUBYARCHDIR\)/) {
c2061af
-          "TARGET_GEM_DIR = $(extout)/gems/$(arch)/#{@gemname}\n"\
c2061af
+          "TARGET_GEM_DIR = $(topdir)/.bundle/extensions/$(gem_platform)/$(ruby_version)/#{@gemname}\n"\
c2061af
           "#{$1}$(TARGET_GEM_DIR)$(target_prefix)"
c2061af
         }
c2061af
       end
c2061af
       conf.any? {|s| /^TARGET *= *\S/ =~ s} and conf << %{
c2061af
+gem_platform = #{Gem::Platform.local}
c2061af
 
c2061af
 # default target
c2061af
 all:
c2061af
c2061af
From c4daf8e445925695c34bab8bf5135dcd1e8575a3 Mon Sep 17 00:00:00 2001
c2061af
From: Nobuyoshi Nakada <nobu@ruby-lang.org>
c2061af
Date: Wed, 6 Apr 2022 22:57:01 +0900
c2061af
Subject: [PATCH 4/5] Obey spec file locations to rubygems
c2061af
c2061af
---
c2061af
 common.mk          | 3 ++-
c2061af
 defs/gmake.mk      | 2 +-
c2061af
 tool/gem-unpack.rb | 5 +++--
c2061af
 3 files changed, 6 insertions(+), 4 deletions(-)
c2061af
c2061af
diff --git a/common.mk b/common.mk
c2061af
index 7c552cba1e04..b4adb2729c0e 100644
c2061af
--- a/common.mk
c2061af
+++ b/common.mk
c2061af
@@ -1359,10 +1359,11 @@ extract-gems$(gnumake:yes=-nongnumake): PHONY
c2061af
 	$(Q) $(RUNRUBY) -C "$(srcdir)" \
c2061af
 	    -Itool -rgem-unpack -answ \
c2061af
 	    -e 'BEGIN {FileUtils.mkdir_p(d = ".bundle/gems")}' \
c2061af
+	    -e 'BEGIN {FileUtils.mkdir_p(s = ".bundle/specifications")}' \
c2061af
 	    -e 'gem, ver = *$$F' \
c2061af
 	    -e 'next if !ver or /^#/=~gem' \
c2061af
 	    -e 'g = "#{gem}-#{ver}"' \
c2061af
-	    -e 'File.directory?("#{d}/#{g}") or Gem.unpack("gems/#{g}.gem", d)' \
c2061af
+	    -e 'File.directory?("#{d}/#{g}") or Gem.unpack("gems/#{g}.gem", d, s)' \
c2061af
 	    gems/bundled_gems
c2061af
 
c2061af
 update-bundled_gems: PHONY
c2061af
diff --git a/defs/gmake.mk b/defs/gmake.mk
c2061af
index a625379a6804..27e3e21cc4d6 100644
c2061af
--- a/defs/gmake.mk
c2061af
+++ b/defs/gmake.mk
c2061af
@@ -290,7 +290,7 @@ extract-gems: | $(patsubst %,.bundle/gems/%,$(bundled-gems))
c2061af
 	$(ECHO) Extracting bundle gem $*...
c2061af
 	$(Q) $(BASERUBY) -C "$(srcdir)" \
c2061af
 	    -Itool -rgem-unpack \
c2061af
-	    -e 'Gem.unpack("gems/$(@F).gem", ".bundle/gems")'
c2061af
+	    -e 'Gem.unpack("gems/$(@F).gem", ".bundle/gems", ".bundle/specifications")'
c2061af
 
c2061af
 $(srcdir)/.bundle/gems:
c2061af
 	$(MAKEDIRS) $@
c2061af
diff --git a/tool/gem-unpack.rb b/tool/gem-unpack.rb
c2061af
index cb05719463f2..fe10b0e420fa 100644
c2061af
--- a/tool/gem-unpack.rb
c2061af
+++ b/tool/gem-unpack.rb
c2061af
@@ -5,13 +5,14 @@
c2061af
 # This library is used by "make extract-gems" to
c2061af
 # unpack bundled gem files.
c2061af
 
c2061af
-def Gem.unpack(file, dir = nil)
c2061af
+def Gem.unpack(file, dir = nil, spec_dir = nil)
c2061af
   pkg = Gem::Package.new(file)
c2061af
   spec = pkg.spec
c2061af
   target = spec.full_name
c2061af
   target = File.join(dir, target) if dir
c2061af
   pkg.extract_files target
c2061af
-  spec_file = File.join(target, "#{spec.name}-#{spec.version}.gemspec")
c2061af
+  FileUtils.mkdir_p(spec_dir ||= target)
c2061af
+  spec_file = File.join(spec_dir, "#{spec.name}-#{spec.version}.gemspec")
c2061af
   open(spec_file, 'wb') do |f|
c2061af
     f.print spec.to_ruby
c2061af
   end
c2061af
c2061af
From 3de652d8198be9cd2998c095903889a80e738275 Mon Sep 17 00:00:00 2001
c2061af
From: Nobuyoshi Nakada <nobu@ruby-lang.org>
c2061af
Date: Thu, 7 Apr 2022 01:44:43 +0900
c2061af
Subject: [PATCH 5/5] Install built gem extension binaries
c2061af
c2061af
---
c2061af
 tool/rbinstall.rb | 56 ++++++++++++++---------------------------------
c2061af
 1 file changed, 16 insertions(+), 40 deletions(-)
c2061af
c2061af
diff --git a/tool/rbinstall.rb b/tool/rbinstall.rb
c2061af
index 9d9b672be472..624961b4eee6 100755
c2061af
--- a/tool/rbinstall.rb
c2061af
+++ b/tool/rbinstall.rb
c2061af
@@ -858,6 +858,9 @@ class UnpackedInstaller < GemInstaller
c2061af
     def write_cache_file
c2061af
     end
c2061af
 
c2061af
+    def build_extensions
c2061af
+    end
c2061af
+
c2061af
     def shebang(bin_file_name)
c2061af
       path = File.join(gem_dir, spec.bindir, bin_file_name)
c2061af
       first_line = File.open(path, "rb") {|file| file.gets}
c2061af
@@ -940,13 +943,12 @@ def ensure_writable_dir(dir)
c2061af
   install_default_gem('ext', srcdir, bindir)
c2061af
 end
c2061af
 
c2061af
-def load_gemspec(file, expanded = false)
c2061af
+def load_gemspec(file, base = nil)
c2061af
   file = File.realpath(file)
c2061af
   code = File.read(file, encoding: "utf-8:-")
c2061af
   code.gsub!(/(?:`git[^\`]*`|%x\[git[^\]]*\])\.split\([^\)]*\)/m) do
c2061af
     files = []
c2061af
-    if expanded
c2061af
-      base = File.dirname(file)
c2061af
+    if base
c2061af
       Dir.glob("**/*", File::FNM_DOTMATCH, base: base) do |n|
c2061af
         case File.basename(n); when ".", ".."; next; end
c2061af
         next if File.directory?(File.join(base, n))
c2061af
@@ -959,7 +961,7 @@ def load_gemspec(file, expanded = false)
c2061af
   unless Gem::Specification === spec
c2061af
     raise TypeError, "[#{file}] isn't a Gem::Specification (#{spec.class} instead)."
c2061af
   end
c2061af
-  spec.loaded_from = file
c2061af
+  spec.loaded_from = base ? File.join(base, File.basename(file)) : file
c2061af
   spec.files.reject! {|n| n.end_with?(".gemspec") or n.start_with?(".git")}
c2061af
 
c2061af
   spec
c2061af
@@ -1014,20 +1016,6 @@ def install_default_gem(dir, srcdir, bindir)
c2061af
 end
c2061af
 
c2061af
 install?(:ext, :comm, :gem, :'bundled-gems') do
c2061af
-  if CONFIG['CROSS_COMPILING'] == 'yes'
c2061af
-    # The following hacky steps set "$ruby = BASERUBY" in tool/fake.rb
c2061af
-    $hdrdir = ''
c2061af
-    $extmk = nil
c2061af
-    $ruby = nil  # ...
c2061af
-    ruby_path = $ruby + " -I#{Dir.pwd}" # $baseruby + " -I#{Dir.pwd}"
c2061af
-  else
c2061af
-    # ruby_path = File.expand_path(with_destdir(File.join(bindir, ruby_install_name)))
c2061af
-    ENV['RUBYLIB'] = nil
c2061af
-    ENV['RUBYOPT'] = nil
c2061af
-    ruby_path = File.expand_path(with_destdir(File.join(bindir, ruby_install_name))) + " --disable=gems -I#{with_destdir(archlibdir)}"
c2061af
-  end
c2061af
-  Gem.instance_variable_set(:@ruby, ruby_path) if Gem.ruby != ruby_path
c2061af
-
c2061af
   gem_dir = Gem.default_dir
c2061af
   install_dir = with_destdir(gem_dir)
c2061af
   prepare "bundled gems", gem_dir
c2061af
@@ -1047,40 +1035,28 @@ def install_default_gem(dir, srcdir, bindir)
c2061af
     :wrappers => true,
c2061af
     :format_executable => true,
c2061af
   }
c2061af
-  gem_ext_dir = "#$extout/gems/#{CONFIG['arch']}"
c2061af
-  extensions_dir = with_destdir(Gem::StubSpecification.gemspec_stub("", gem_dir, gem_dir).extensions_dir)
c2061af
+
c2061af
+  extensions_dir = Gem::StubSpecification.gemspec_stub("", gem_dir, gem_dir).extensions_dir
c2061af
+  specifications_dir = File.join(gem_dir, "specifications")
c2061af
+  build_dir = Gem::StubSpecification.gemspec_stub("", ".bundle", ".bundle").extensions_dir
c2061af
 
c2061af
   File.foreach("#{srcdir}/gems/bundled_gems") do |name|
c2061af
     next if /^\s*(?:#|$)/ =~ name
c2061af
     next unless /^(\S+)\s+(\S+).*/ =~ name
c2061af
     gem_name = "#$1-#$2"
c2061af
-    path = "#{srcdir}/.bundle/gems/#{gem_name}/#{gem_name}.gemspec"
c2061af
-    if File.exist?(path)
c2061af
-      spec = load_gemspec(path)
c2061af
-    else
c2061af
-      path = "#{srcdir}/.bundle/gems/#{gem_name}/#$1.gemspec"
c2061af
-      next unless File.exist?(path)
c2061af
-      spec = load_gemspec(path, true)
c2061af
-    end
c2061af
+    path = "#{srcdir}/.bundle/specifications/#{gem_name}.gemspec"
c2061af
+    next unless File.exist?(path)
c2061af
+    spec = load_gemspec(path, "#{srcdir}/.bundle/gems/#{gem_name}")
c2061af
     next unless spec.platform == Gem::Platform::RUBY
c2061af
     next unless spec.full_name == gem_name
c2061af
-    if !spec.extensions.empty? && CONFIG["EXTSTATIC"] == "static"
c2061af
-      puts "skip installation of #{spec.name} #{spec.version}; bundled gem with an extension library is not supported on --with-static-linked-ext"
c2061af
-      next
c2061af
-    end
c2061af
     spec.extension_dir = "#{extensions_dir}/#{spec.full_name}"
c2061af
-    if File.directory?(ext = "#{gem_ext_dir}/#{spec.full_name}")
c2061af
-      spec.extensions[0] ||= "-"
c2061af
-    end
c2061af
     package = RbInstall::DirPackage.new spec
c2061af
     ins = RbInstall::UnpackedInstaller.new(package, options)
c2061af
     puts "#{INDENT}#{spec.name} #{spec.version}"
c2061af
     ins.install
c2061af
-    unless $dryrun
c2061af
-      File.chmod($data_mode, File.join(install_dir, "specifications", "#{spec.full_name}.gemspec"))
c2061af
-    end
c2061af
-    unless spec.extensions.empty?
c2061af
-      install_recursive(ext, spec.extension_dir)
c2061af
+    install_recursive("#{build_dir}/#{gem_name}", "#{extensions_dir}/#{gem_name}") do |src, dest|
c2061af
+      # puts "#{INDENT}    #{dest[extensions_dir.size+gem_name.size+2..-1]}"
c2061af
+      install src, dest, :mode => (File.executable?(src) ? $prog_mode : $data_mode)
c2061af
     end
c2061af
     installed_gems[spec.full_name] = true
c2061af
   end