Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implemented paths option to has_library?

 o Updated example to show how it can be called


git-svn-id: http://mkrf.rubyforge.org/svn/trunk@51 6e48df83-f314-0410-b1e7-e69642b30866
  • Loading branch information...
commit 14c81399622d1e1e585ea7aa6dd160ab6a8f235a 1 parent 5dff8d2
hoanga authored
4 examples/trivial/ext/trivial/mkrf_conf.rb
... ... @@ -1,4 +1,6 @@
1 1 require 'rubygems'
2 2 require 'mkrf'
3 3
4   -Mkrf::Generator.new('trivial')
  4 +Mkrf::Generator.new('trivial') do |g|
  5 + g.include_library('', '', '/usr/local/lib')
  6 +end
40 lib/mkrf/availability.rb
@@ -22,12 +22,14 @@ class Availability
22 22 #
23 23 # Valid keys for the options hash include:
24 24 # * <tt>:loaded_libs</tt> -- libraries to load by default
  25 + # * <tt>:library_paths</tt> -- libraries paths to include by default
25 26 # * <tt>:headers</tt> -- headers to load by default
26 27 # * <tt>:compiler</tt> -- which compiler to use when determining availability
27 28 # * <tt>:includes</tt> -- directories that should be searched for include files
28 29 def initialize(options = {})
29 30
30 31 @loaded_libs = (options[:loaded_libs] || Config::CONFIG["LIBS"].gsub('-l', '').split).to_a
  32 + @library_paths = (options[:library_paths] || "").to_a
31 33 # Not sure what COMMON_HEADERS looks like when populated
32 34 @headers = options[:headers] || [] # Config::CONFIG["COMMON_HEADERS"]
33 35 @compiler = options[:compiler] || Config::CONFIG["CC"]
@@ -42,7 +44,11 @@ def initialize(options = {})
42 44 # Params:
43 45 # * <tt>library</tt> -- the library to be included as a string.
44 46 # * <tt>function</tt> -- a method to base the inclusion of the library on. +main+ by default.
45   - def include_library(library, function = "main")
  47 + # * <tt>paths</tt> -- an optional list of search paths if the library is not found in the default paths.
  48 + def include_library(library, function = "main", *paths)
  49 + paths.each do |library_dir|
  50 + @library_paths << library_dir
  51 + end
46 52 @loaded_libs << library if has_library?(library, function)
47 53 end
48 54
@@ -64,9 +70,12 @@ def include_header(header, *paths)
64 70 # Params:
65 71 # * <tt>library</tt> -- the library to be included as a string
66 72 # * <tt>function</tt> -- a method to base the inclusion of the library on. +main+ by default.
67   - def has_library?(library, function = "main")
  73 + # * <tt>paths</tt> -- an optional list of search paths if the library is not found in the default paths
  74 + def has_library?(library, function = "main", *paths)
68 75 logger.info "Checking for library: #{library}"
69 76 return true if library_already_loaded?(library)
  77 + # Should this be only found_library? or a specialized version with
  78 + # path searching?
70 79 found_library?(library, function)
71 80 end
72 81
@@ -133,6 +142,11 @@ def library_compile_string
133 142 @loaded_libs.collect {|l| "-l#{l}"}.join(' ')
134 143 end
135 144
  145 + # Returns a string of libraries directories formatted for compilation
  146 + def library_paths_compile_string
  147 + @library_paths.collect {|l| "-L#{l}"}.join(' ')
  148 + end
  149 +
136 150 # Returns a string of include directories formatted for compilation
137 151 def includes_compile_string
138 152 @includes.collect {|i| "-I#{i}"}.join(' ')
@@ -192,6 +206,19 @@ def header_already_loaded?(header)
192 206 return false
193 207 end
194 208
  209 +# def library_found_in_paths?(library, paths)
  210 +# paths.each do |include_path|
  211 +#
  212 +# if with_libs(include_path) { library_can_link?(header) }
  213 +# @libspath << include_path
  214 +# return true
  215 +# end
  216 +# end
  217 +#
  218 +# return false
  219 +#
  220 +# end
  221 +
195 222 def header_found_in_paths?(header, paths)
196 223 paths.each do |include_path|
197 224 if with_includes(include_path) { header_can_link?(header) }
@@ -218,8 +245,11 @@ def header_include_string
218 245 end
219 246
220 247 def link_command
221   - "#{@compiler} -o #{TEMP_EXECUTABLE} #{library_compile_string} " +
222   - "#{includes_compile_string} #{TEMP_SOURCE_FILE}"
  248 + # This current implementation just splats the library_paths in
  249 + # unconditionally. Is this problematic?
  250 + "#{@compiler} -o #{TEMP_EXECUTABLE} #{library_paths_compile_string}" +
  251 + " #{library_compile_string} #{includes_compile_string}" +
  252 + " #{TEMP_SOURCE_FILE}"
223 253 end
224 254
225 255 # Creates a temporary source file with the string passed
@@ -284,4 +314,4 @@ def silence_stream(stream)
284 314 end
285 315
286 316 end
287   -end
  317 +end
8 lib/mkrf/generator.rb
@@ -32,6 +32,12 @@ module Mkrf
32 32 # '/usr/include/libxml2')
33 33 # end
34 34 #
  35 + # It is also possible to specify the library paths in
  36 + # include_library
  37 + # Mkrf::Generator.new('libxml') do |g|
  38 + # g.include_library('socket','socket', '/usr/local/lib/libxml')
  39 + # end
  40 + #
35 41 class Generator
36 42 include Rake
37 43
@@ -164,7 +170,7 @@ def rakefile_contents # :nodoc:
164 170
165 171 LDSHARED = "#{CONFIG['LDSHARED']} #{ldshared}"
166 172
167   -LIBPATH = '-L"#{CONFIG['rubylibdir']}"'
  173 +LIBPATH = "-L#{CONFIG['rubylibdir']} #{@available.library_paths_compile_string}"
168 174
169 175 INCLUDES = "#{@available.includes_compile_string}"
170 176

0 comments on commit 14c8139

Please sign in to comment.
Something went wrong with that request. Please try again.