misc/monkeypatches.rb
changeset 1 09d0d209d06d
parent 0 92d00ff32c56
child 2 0c24586f579a
child 9 4c51ebe6e9b6
equal deleted inserted replaced
0:92d00ff32c56 1:09d0d209d06d
     7 # * The :install task created the target install directory every time instead of just
     7 # * The :install task created the target install directory every time instead of just
     8 #   declaring a dependency on a directory task
     8 #   declaring a dependency on a directory task
     9 
     9 
    10 require 'mkrf/generator'
    10 require 'mkrf/generator'
    11 
    11 
    12 class Mkrf::Generator
    12 RAKEFILE_TEMPLATE = %q{
       
    13 	# Generated by monkeypatched mkrf
       
    14 	require 'rake/clean'
       
    15 	require 'rbconfig'
    13 
    16 
    14     def rakefile_contents # :nodoc:
    17 	include Config
    15       objext = CONFIG['OBJEXT']
       
    16 
    18 
    17       <<-END_RAKEFILE
    19 	SRC = FileList[#{sources.join(',')}]
    18 # Generated by mkrf
    20 	OBJ = SRC.ext('#{objext}')
    19 require 'rake/clean'
    21 	CC = '#{@cc}'
    20 require 'rbconfig'
       
    21 
    22 
    22 include Config
    23 	ADDITIONAL_OBJECTS = '#{objects}'
    23 
    24 
    24 SRC = FileList[#{sources.join(',')}]
    25 	LDSHARED = "#{@available.ldshared_string} #{ldshared}"
    25 OBJ = SRC.ext('#{objext}')
       
    26 CC = '#{@cc}'
       
    27 
    26 
    28 ADDITIONAL_OBJECTS = '#{objects}'
    27 	LIBPATH =  "#{library_path(CONFIG['libdir'])} #{@available.library_paths_compile_string}"
    29 
    28 
    30 LDSHARED = "#{@available.ldshared_string} #{ldshared}"
    29 	INCLUDES = "#{@available.includes_compile_string}"
    31 
    30 
    32 LIBPATH =  "#{library_path(CONFIG['libdir'])} #{@available.library_paths_compile_string}"
    31 	LIBS = "#{@available.library_compile_string}"
    33 
    32 
    34 INCLUDES = "#{@available.includes_compile_string}"
    33 	CFLAGS = "#{cflags} #{defines_compile_string}"
    35 
    34 
    36 LIBS = "#{@available.library_compile_string}"
    35 	RUBYARCHDIR = ENV["RUBYARCHDIR"] || CONFIG['sitearchdir']
       
    36 	LIBRUBYARG_SHARED = "#{CONFIG['LIBRUBYARG_SHARED']}"
       
    37 	EXT = '#{@extension_name}'
    37 
    38 
    38 CFLAGS = "#{cflags} #{defines_compile_string}"
    39 	CLEAN.include( EXT, '*.#{objext}' )
       
    40 	CLOBBER.include( 'mkrf.log' )
    39 
    41 
    40 RUBYARCHDIR = ENV["RUBYARCHDIR"] || CONFIG['sitearchdir']
    42 	task :default => EXT
    41 LIBRUBYARG_SHARED = "#{CONFIG['LIBRUBYARG_SHARED']}"
       
    42 EXT = '#{@extension_name}'
       
    43 
    43 
    44 CLEAN.include( EXT, '*.#{objext}' )
    44 	rule '.#{objext}' => '.#{@source_extension}' do |t|
    45 CLOBBER.include( 'mkrf.log' )
    45 	  sh "\#{CC} \#{CFLAGS} \#{INCLUDES} -c \#{t.source}"
       
    46 	end
    46 
    47 
    47 task :default => EXT
    48 	desc "Build this extension"
    48 
    49 	file EXT => OBJ do
    49 rule '.#{objext}' => '.#{@source_extension}' do |t|
    50 	  sh "\#{LDSHARED} \#{LIBPATH} #{@available.ld_outfile(@extension_name)} \#{OBJ} \#{ADDITIONAL_OBJECTS} \#{LIBS} \#{LIBRUBYARG_SHARED}"
    50   sh "\#{CC} \#{CFLAGS} \#{INCLUDES} -c \#{t.source}"
    51 	end
    51 end
       
    52 
       
    53 desc "Build this extension"
       
    54 file EXT => OBJ do
       
    55   sh "\#{LDSHARED} \#{LIBPATH} #{@available.ld_outfile(@extension_name)} \#{OBJ} \#{ADDITIONAL_OBJECTS} \#{LIBS} \#{LIBRUBYARG_SHARED}"
       
    56 end
       
    57 
    52 
    58 
    53 
    59 directory RUBYARCHDIR
    54 	directory RUBYARCHDIR
    60 
    55 
    61 desc "Install this extension"
    56 	desc "Install this extension"
    62 task :install => [EXT, RUBYARCHDIR] do
    57 	task :install => [EXT, RUBYARCHDIR] do
    63   install EXT, RUBYARCHDIR, :verbose => true
    58 	  install EXT, RUBYARCHDIR, :verbose => true
    64 end
    59 	end
    65 
    60 
    66 #{additional_code}
    61 	#{additional_code}
    67       END_RAKEFILE
    62 }.gsub( /^\t/m, '' )
    68     end
       
    69 
    63 
    70 end
       
    71 
    64 
       
    65 
       
    66 module Mkrf
       
    67 
       
    68 	class Availability
       
    69 		# No-op the stupid squashing of output
       
    70 		def silence_stream( stream )
       
    71 			old_stream = stream.dup
       
    72 			stream.reopen( @logger.instance_variable_get(:@logdev).dev )
       
    73 			stream.sync = true
       
    74 			yield
       
    75 		ensure
       
    76 			stream.reopen( old_stream )
       
    77 		end
       
    78 	end
       
    79 
       
    80 	class Generator
       
    81 
       
    82 		def rakefile_contents # :nodoc:
       
    83 			objext = CONFIG['OBJEXT']
       
    84 			return interpolate( RAKEFILE_TEMPLATE, binding() )
       
    85 		end
       
    86 
       
    87 
       
    88 		### Interpolate any '#{...}' placeholders in the string within the given
       
    89 		### +scope+ (a Binding object).
       
    90 	    def interpolate( string, scope )
       
    91 	        unless scope.is_a?( Binding )
       
    92 	            raise TypeError, "Argument to interpolate must be a Binding, not "\
       
    93 	                "a #{scope.class.name}"
       
    94 	        end
       
    95 
       
    96 			# $stderr.puts ">>> Interpolating '#{self}'..."
       
    97 
       
    98 	        copy = string.gsub( /"/, %q:\": )
       
    99 	        eval( '"' + copy + '"', scope )
       
   100 		rescue Exception => err
       
   101 			nicetrace = err.backtrace.find_all {|frame|
       
   102 				/in `(interpolate|eval)'/i !~ frame
       
   103 			}
       
   104 			Kernel.raise( err, err.message, nicetrace )
       
   105 	    end
       
   106 
       
   107 	end # class Generator
       
   108 
       
   109 end # module Mkrf