misc/monkeypatches.rb
branchmahlon-misc
changeset 9 4c51ebe6e9b6
parent 1 09d0d209d06d
child 10 b1426511fb64
equal deleted inserted replaced
8:296a03a08be2 9:4c51ebe6e9b6
    64 
    64 
    65 
    65 
    66 module Mkrf
    66 module Mkrf
    67 
    67 
    68 	class Availability
    68 	class Availability
    69 		# No-op the stupid squashing of output
    69 
       
    70 		# Create a new Availability instance.
       
    71 		#
       
    72 		# Valid keys for the options hash include:
       
    73 		# * <tt>:loaded_libs</tt> -- libraries to load by default
       
    74 		# * <tt>:library_paths</tt> -- libraries paths to include by default
       
    75 		# * <tt>:headers</tt> -- headers to load by default
       
    76 		# * <tt>:compiler</tt> -- which compiler to use when determining availability
       
    77 		# * <tt>:includes</tt> -- directories that should be searched for include files
       
    78 		def initialize(options = {})      
       
    79 			@loaded_libs = options[:loaded_libs] || []
       
    80 			@loaded_libs.flatten!
       
    81 
       
    82 			@library_paths = [(options[:library_paths] || [])].flatten
       
    83 			# Not sure what COMMON_HEADERS looks like when populated
       
    84 			@headers = options[:headers] || [] # Config::CONFIG["COMMON_HEADERS"]
       
    85 			@compiler = options[:compiler] || Config::CONFIG["CC"]
       
    86 			@includes = [(options[:includes] || DEFAULT_INCLUDES)].flatten
       
    87 			@logger = Logger.new('mkrf.log')
       
    88 			@defines = []
       
    89 		end
       
    90 
       
    91 
       
    92 		def can_link?( function_body )
       
    93 			silence_command_line do
       
    94 				create_source(function_body)
       
    95 				cmd = link_command()
       
    96 				@logger.debug "Running link command: #{cmd}"
       
    97 				system( cmd )
       
    98 			end
       
    99 		ensure
       
   100 			FileUtils.rm_f TEMP_SOURCE_FILE
       
   101 			FileUtils.rm_f TEMP_EXECUTABLE
       
   102 		end
       
   103 
       
   104 
       
   105 		# Add the LIBRUBYARG_SHARED setting to the library paths for non-windows boxen. This is
       
   106 		# necessary if Ruby is installed in a directory that isn't in the default library
       
   107 		# search path (e.g., on FreeBSD where Ruby is /usr/local/bin/ruby).
       
   108 		def library_paths_compile_string
       
   109 			if RUBY_PLATFORM =~ /mswin/
       
   110 				@library_paths.collect {|l| "/libpath:#{l}"}.join(' ')
       
   111 			else
       
   112 				Config::CONFIG['LIBRUBYARG_SHARED'] + @library_paths.collect {|l| "-L#{l}"}.join(' ')
       
   113 			end
       
   114 		end
       
   115 
       
   116 
       
   117 		# Separate includes with a newline instead of a literal '\n'
       
   118 		def header_include_string
       
   119 			@headers.collect {|header| "#include <#{header}>"}.join( "\n" )
       
   120 		end
       
   121 
       
   122 
       
   123 		# Log the created source to the logfile
       
   124 		def create_source( src )
       
   125 			@logger.debug "Creating source file:\n#{src}"
       
   126 			File.open( TEMP_SOURCE_FILE, "w+" ) do |f|
       
   127 				f.write( src )
       
   128 			end
       
   129 		end
       
   130 
       
   131 		# Prepend the LIBS string directly to the @loaded_libs, as not all arguments in
       
   132 		# it 
       
   133 		def library_compile_string
       
   134 			added_libs = nil
       
   135 			if RUBY_PLATFORM =~ /mswin/
       
   136 				added_libs = @loaded_libs.join(' ')
       
   137 			else
       
   138 				added_libs = @loaded_libs.collect {|l| "-l#{l}"}.join(' ')
       
   139 			end
       
   140 			
       
   141 			return Config::CONFIG["LIBS"] + ' ' + added_libs
       
   142 		end
       
   143 
       
   144 
       
   145 		# Redirect to the mkrf log instead of /dev/null
    70 		def silence_stream( stream )
   146 		def silence_stream( stream )
    71 			old_stream = stream.dup
   147 			old_stream = stream.dup
    72 			stream.reopen( @logger.instance_variable_get(:@logdev).dev )
   148 			stream.reopen( @logger.instance_variable_get(:@logdev).dev )
    73 			stream.sync = true
   149 			stream.sync = true
    74 			yield
   150 			yield