misc/monkeypatches.rb
changeset 2 0c24586f579a
parent 1 09d0d209d06d
equal deleted inserted replaced
1:09d0d209d06d 2:0c24586f579a
    40 	CLOBBER.include( 'mkrf.log' )
    40 	CLOBBER.include( 'mkrf.log' )
    41 
    41 
    42 	task :default => EXT
    42 	task :default => EXT
    43 
    43 
    44 	rule '.#{objext}' => '.#{@source_extension}' do |t|
    44 	rule '.#{objext}' => '.#{@source_extension}' do |t|
    45 	  sh "\#{CC} \#{CFLAGS} \#{INCLUDES} -c \#{t.source}"
    45 		$stderr.puts "  building \#{t.name} from \#{t.source}"
       
    46 		sh "\#{CC} \#{CFLAGS} \#{INCLUDES} -c \#{t.source}"
    46 	end
    47 	end
    47 
    48 
    48 	desc "Build this extension"
    49 	desc "Build this extension"
    49 	file EXT => OBJ do
    50 	file EXT => OBJ do
    50 	  sh "\#{LDSHARED} \#{LIBPATH} #{@available.ld_outfile(@extension_name)} \#{OBJ} \#{ADDITIONAL_OBJECTS} \#{LIBS} \#{LIBRUBYARG_SHARED}"
    51 		$stderr.puts "  linking \#{OBJ.join(', ')} into \#{EXT}"
       
    52 		sh "\#{LDSHARED} \#{LIBPATH} #{@available.ld_outfile(@extension_name)} \#{OBJ} \#{ADDITIONAL_OBJECTS} \#{LIBS} \#{LIBRUBYARG_SHARED}"
    51 	end
    53 	end
    52 
    54 
    53 
    55 
    54 	directory RUBYARCHDIR
    56 	directory RUBYARCHDIR
    55 
    57 
    56 	desc "Install this extension"
    58 	desc "Install this extension"
    57 	task :install => [EXT, RUBYARCHDIR] do
    59 	task :install => [EXT, RUBYARCHDIR] do
    58 	  install EXT, RUBYARCHDIR, :verbose => true
    60 		install EXT, RUBYARCHDIR, :verbose => true
    59 	end
    61 	end
    60 
    62 
    61 	#{additional_code}
    63 	#{additional_code}
    62 }.gsub( /^\t/m, '' )
    64 }.gsub( /^\t/m, '' )
    63 
    65 
    64 
    66 
    65 
    67 
    66 module Mkrf
    68 module Mkrf
    67 
    69 
    68 	class Availability
    70 	class Availability
    69 		# No-op the stupid squashing of output
    71 
       
    72 		# Create a new Availability instance.
       
    73 		#
       
    74 		# Valid keys for the options hash include:
       
    75 		# * <tt>:loaded_libs</tt> -- libraries to load by default
       
    76 		# * <tt>:library_paths</tt> -- libraries paths to include by default
       
    77 		# * <tt>:headers</tt> -- headers to load by default
       
    78 		# * <tt>:compiler</tt> -- which compiler to use when determining availability
       
    79 		# * <tt>:includes</tt> -- directories that should be searched for include files
       
    80 		def initialize(options = {})      
       
    81 			@loaded_libs = options[:loaded_libs] || []
       
    82 			@loaded_libs.flatten!
       
    83 
       
    84 			@library_paths = [(options[:library_paths] || [])].flatten
       
    85 			# Not sure what COMMON_HEADERS looks like when populated
       
    86 			@headers = options[:headers] || [] # Config::CONFIG["COMMON_HEADERS"]
       
    87 			@compiler = options[:compiler] || Config::CONFIG["CC"]
       
    88 			@includes = [(options[:includes] || DEFAULT_INCLUDES)].flatten
       
    89 			@logger = Logger.new('mkrf.log')
       
    90 			@defines = []
       
    91 		end
       
    92 
       
    93 
       
    94 		def can_link?( function_body )
       
    95 			silence_command_line do
       
    96 				create_source(function_body)
       
    97 				cmd = link_command()
       
    98 				@logger.debug "Running link command: #{cmd}"
       
    99 				system( cmd )
       
   100 			end
       
   101 		ensure
       
   102 			FileUtils.rm_f TEMP_SOURCE_FILE
       
   103 			FileUtils.rm_f TEMP_EXECUTABLE
       
   104 		end
       
   105 
       
   106 
       
   107 		# Add the LIBRUBYARG_SHARED setting to the library paths for non-windows boxen. This is
       
   108 		# necessary if Ruby is installed in a directory that isn't in the default library
       
   109 		# search path (e.g., on FreeBSD where Ruby is /usr/local/bin/ruby).
       
   110 		def library_paths_compile_string
       
   111 			if RUBY_PLATFORM =~ /mswin/
       
   112 				@library_paths.collect {|l| "/libpath:#{l}"}.join(' ')
       
   113 			else
       
   114 				Config::CONFIG['LIBRUBYARG_SHARED'] + @library_paths.collect {|l| "-L#{l}"}.join(' ')
       
   115 			end
       
   116 		end
       
   117 
       
   118 
       
   119 		# Separate includes with a newline instead of a literal '\n'
       
   120 		def header_include_string
       
   121 			@headers.collect {|header| "#include <#{header}>"}.join( "\n" )
       
   122 		end
       
   123 
       
   124 
       
   125 		# Log the created source to the logfile
       
   126 		def create_source( src )
       
   127 			@logger.debug "Creating source file:\n#{src}"
       
   128 			File.open( TEMP_SOURCE_FILE, "w+" ) do |f|
       
   129 				f.write( src )
       
   130 			end
       
   131 		end
       
   132 
       
   133 		# Prepend the LIBS string directly to the @loaded_libs, as not all arguments in
       
   134 		# it 
       
   135 		def library_compile_string
       
   136 			added_libs = nil
       
   137 			if RUBY_PLATFORM =~ /mswin/
       
   138 				added_libs = @loaded_libs.join(' ')
       
   139 			else
       
   140 				added_libs = @loaded_libs.collect {|l| "-l#{l}"}.join(' ')
       
   141 			end
       
   142 			
       
   143 			return Config::CONFIG["LIBS"] + ' ' + added_libs
       
   144 		end
       
   145 
       
   146 
       
   147 		# Redirect to the mkrf log instead of /dev/null
    70 		def silence_stream( stream )
   148 		def silence_stream( stream )
    71 			old_stream = stream.dup
   149 			old_stream = stream.dup
    72 			stream.reopen( @logger.instance_variable_get(:@logdev).dev )
   150 			stream.reopen( @logger.instance_variable_get(:@logdev).dev )
    73 			stream.sync = true
   151 			stream.sync = true
    74 			yield
   152 			yield