[Rubygems-developers] [PATCH] Support extensions built with rake

Tilman Sauerbeck tilman at code-monkey.de
Fri Aug 12 09:58:12 EDT 2005


Hi,
here's a patch that adds support for gems that use Rake to build
extensions.

It's assumed that the default rake target will build the extension(s),
and that the "install" target will install them.
RUBYARCHDIR and RUBYLIBDIR are overridden the same way as it's done for
extconf.rb-based extensions.

Also, the "clean" target will now be invoked for both extconf.rb-based
extensions and for rake-based ones - I don't see why *.o should be
kept around :)

The patch is probably flawed in several ways, but I hope it's good
enough to either merge it or pick it up and improve it until it's good
to go ;)

Regards,
Tilman

-- 
learn to quote: http://www.netmeister.org/news/learn2quote.html
-------------- next part --------------
diff -aur rubygems-0.8.11.orig/lib/rubygems/installer.rb rubygems-0.8.11/lib/rubygems/installer.rb
--- rubygems-0.8.11.orig/lib/rubygems/installer.rb	2005-06-12 23:33:19.000000000 +0200
+++ rubygems-0.8.11/lib/rubygems/installer.rb	2005-08-11 22:19:32.000000000 +0200
@@ -284,29 +284,31 @@
       say "Building native extensions.  This could take a while..."
       start_dir = Dir.pwd
       dest_path = File.join(directory, spec.require_paths[0])
+
       spec.extensions.each do |extension|
-        Dir.chdir File.join(directory, File.dirname(extension))
-        results = ["#{Gem.ruby} #{File.basename(extension)} #{ARGV.join(" ")}"]
-        results << `#{Gem.ruby} #{File.basename(extension)} #{ARGV.join(" ")}`
-        if File.exist?('Makefile')
-          mf = File.read('Makefile')
-          mf = mf.gsub(/^RUBYARCHDIR\s*=\s*\$.*/, "RUBYARCHDIR = #{dest_path}")
-          mf = mf.gsub(/^RUBYLIBDIR\s*=\s*\$.*/, "RUBYLIBDIR = #{dest_path}")
-          File.open('Makefile', 'wb') {|f| f.print mf}
-          make_program = ENV['make']
-          unless make_program
-            make_program = (/mswin/ =~ RUBY_PLATFORM) ? 'nmake' : 'make'
-          end
-          results << "#{make_program}"
-          results << `#{make_program}`
-          results << "#{make_program} install"
-          results << `#{make_program} install`
-          say results.join("\n")
+        if extension.match(/extconf/)
+          builder = ExtExtConfBuilder
+		elsif extension.match(/rakefile/i)
+          builder = ExtRakeBuilder
         else
-          File.open(File.join(Dir.pwd, 'gem_make.out'), 'wb') {|f| f.puts results.join("\n")}
-          raise "ERROR: Failed to build gem native extension.\nGem files will remain installed in #{directory} for inspection.\n  #{results.join('\n')}\n\nResults logged to #{File.join(Dir.pwd, 'gem_make.out')}"
+          builder = nil
+          results = ["No builder for extension '#{extension}'"]
+        end
+
+        begin
+          err = false
+          Dir.chdir File.join(directory, File.dirname(extension))
+          results = builder.build(extension, directory, dest_path)
+        rescue
+          err = true
         end
+
+        say results.join("\n")
         File.open('gem_make.out', 'wb') {|f| f.puts results.join("\n")}
+
+        if err
+          raise "ERROR: Failed to build gem native extension.\nGem files will remain installed in #{directory} for inspection.\n  #{results.join('\n')}\n\nResults logged to #{File.join(Dir.pwd, 'gem_make.out')}"
+		end
       end
       Dir.chdir start_dir
     end
@@ -535,4 +537,40 @@
 
   end  # class Uninstaller
 
+  class ExtExtConfBuilder
+    def ExtExtConfBuilder.build(extension, directory, dest_path)
+      results = ["#{Gem.ruby} #{File.basename(extension)} #{ARGV.join(" ")}"]
+      results << `#{Gem.ruby} #{File.basename(extension)} #{ARGV.join(" ")}`
+
+      raise unless File.exist?('Makefile')
+      mf = File.read('Makefile')
+      mf = mf.gsub(/^RUBYARCHDIR\s*=\s*\$.*/, "RUBYARCHDIR = #{dest_path}")
+      mf = mf.gsub(/^RUBYLIBDIR\s*=\s*\$.*/, "RUBYLIBDIR = #{dest_path}")
+      File.open('Makefile', 'wb') {|f| f.print mf}
+
+      make_program = ENV['make']
+      unless make_program
+        make_program = (/mswin/ =~ RUBY_PLATFORM) ? 'nmake' : 'make'
+      end
+
+      ['', 'install', 'clean'].each do |target|
+        results << "#{make program} #{target}".strip
+        results << `#{make program} #{target}`
+      end
+    end
+  end
+
+  class ExtRakeBuilder
+    def ExtRakeBuilder.build(ext, directory, dest_path)
+      make_program = ENV['rake'] || 'rake'
+      make_program += " RUBYARCHDIR=#{dest_path} RUBYLIBDIR=#{dest_path}"
+
+      results = []
+
+      ['', 'install', 'clean'].each do |target|
+        results << "#{make program} #{target}".strip
+        results << `#{make program} #{target}`
+      end
+    end
+  end
 end  # module Gem


More information about the Rubygems-developers mailing list