misc/monkeypatches.rb
changeset 1 09d0d209d06d
parent 0 92d00ff32c56
child 2 0c24586f579a
child 9 4c51ebe6e9b6
--- a/misc/monkeypatches.rb	Fri Aug 15 15:43:38 2008 +0000
+++ b/misc/monkeypatches.rb	Fri Aug 15 16:23:03 2008 +0000
@@ -9,63 +9,101 @@
 
 require 'mkrf/generator'
 
-class Mkrf::Generator
-
-    def rakefile_contents # :nodoc:
-      objext = CONFIG['OBJEXT']
+RAKEFILE_TEMPLATE = %q{
+	# Generated by monkeypatched mkrf
+	require 'rake/clean'
+	require 'rbconfig'
 
-      <<-END_RAKEFILE
-# Generated by mkrf
-require 'rake/clean'
-require 'rbconfig'
+	include Config
 
-include Config
+	SRC = FileList[#{sources.join(',')}]
+	OBJ = SRC.ext('#{objext}')
+	CC = '#{@cc}'
 
-SRC = FileList[#{sources.join(',')}]
-OBJ = SRC.ext('#{objext}')
-CC = '#{@cc}'
+	ADDITIONAL_OBJECTS = '#{objects}'
 
-ADDITIONAL_OBJECTS = '#{objects}'
+	LDSHARED = "#{@available.ldshared_string} #{ldshared}"
 
-LDSHARED = "#{@available.ldshared_string} #{ldshared}"
+	LIBPATH =  "#{library_path(CONFIG['libdir'])} #{@available.library_paths_compile_string}"
 
-LIBPATH =  "#{library_path(CONFIG['libdir'])} #{@available.library_paths_compile_string}"
+	INCLUDES = "#{@available.includes_compile_string}"
 
-INCLUDES = "#{@available.includes_compile_string}"
+	LIBS = "#{@available.library_compile_string}"
 
-LIBS = "#{@available.library_compile_string}"
+	CFLAGS = "#{cflags} #{defines_compile_string}"
 
-CFLAGS = "#{cflags} #{defines_compile_string}"
+	RUBYARCHDIR = ENV["RUBYARCHDIR"] || CONFIG['sitearchdir']
+	LIBRUBYARG_SHARED = "#{CONFIG['LIBRUBYARG_SHARED']}"
+	EXT = '#{@extension_name}'
 
-RUBYARCHDIR = ENV["RUBYARCHDIR"] || CONFIG['sitearchdir']
-LIBRUBYARG_SHARED = "#{CONFIG['LIBRUBYARG_SHARED']}"
-EXT = '#{@extension_name}'
+	CLEAN.include( EXT, '*.#{objext}' )
+	CLOBBER.include( 'mkrf.log' )
 
-CLEAN.include( EXT, '*.#{objext}' )
-CLOBBER.include( 'mkrf.log' )
-
-task :default => EXT
+	task :default => EXT
 
-rule '.#{objext}' => '.#{@source_extension}' do |t|
-  sh "\#{CC} \#{CFLAGS} \#{INCLUDES} -c \#{t.source}"
-end
+	rule '.#{objext}' => '.#{@source_extension}' do |t|
+	  sh "\#{CC} \#{CFLAGS} \#{INCLUDES} -c \#{t.source}"
+	end
 
-desc "Build this extension"
-file EXT => OBJ do
-  sh "\#{LDSHARED} \#{LIBPATH} #{@available.ld_outfile(@extension_name)} \#{OBJ} \#{ADDITIONAL_OBJECTS} \#{LIBS} \#{LIBRUBYARG_SHARED}"
-end
+	desc "Build this extension"
+	file EXT => OBJ do
+	  sh "\#{LDSHARED} \#{LIBPATH} #{@available.ld_outfile(@extension_name)} \#{OBJ} \#{ADDITIONAL_OBJECTS} \#{LIBS} \#{LIBRUBYARG_SHARED}"
+	end
 
 
-directory RUBYARCHDIR
+	directory RUBYARCHDIR
+
+	desc "Install this extension"
+	task :install => [EXT, RUBYARCHDIR] do
+	  install EXT, RUBYARCHDIR, :verbose => true
+	end
+
+	#{additional_code}
+}.gsub( /^\t/m, '' )
+
+
+
+module Mkrf
 
-desc "Install this extension"
-task :install => [EXT, RUBYARCHDIR] do
-  install EXT, RUBYARCHDIR, :verbose => true
-end
+	class Availability
+		# No-op the stupid squashing of output
+		def silence_stream( stream )
+			old_stream = stream.dup
+			stream.reopen( @logger.instance_variable_get(:@logdev).dev )
+			stream.sync = true
+			yield
+		ensure
+			stream.reopen( old_stream )
+		end
+	end
+
+	class Generator
 
-#{additional_code}
-      END_RAKEFILE
-    end
+		def rakefile_contents # :nodoc:
+			objext = CONFIG['OBJEXT']
+			return interpolate( RAKEFILE_TEMPLATE, binding() )
+		end
+
+
+		### Interpolate any '#{...}' placeholders in the string within the given
+		### +scope+ (a Binding object).
+	    def interpolate( string, scope )
+	        unless scope.is_a?( Binding )
+	            raise TypeError, "Argument to interpolate must be a Binding, not "\
+	                "a #{scope.class.name}"
+	        end
 
-end
+			# $stderr.puts ">>> Interpolating '#{self}'..."
 
+	        copy = string.gsub( /"/, %q:\": )
+	        eval( '"' + copy + '"', scope )
+		rescue Exception => err
+			nicetrace = err.backtrace.find_all {|frame|
+				/in `(interpolate|eval)'/i !~ frame
+			}
+			Kernel.raise( err, err.message, nicetrace )
+	    end
+
+	end # class Generator
+
+end # module Mkrf
\ No newline at end of file