author | Michael Granger <ged@FaerieMUD.org> |
Thu, 20 Nov 2008 16:43:02 +0000 | |
changeset 3 | 7f6da371e2ce |
parent 2 | 0c24586f579a |
permissions | -rw-r--r-- |
0 | 1 |
### Monkeypatch to work around the broken Rakefile generated by Mkrf <= 0.2.3 |
2 |
||
3 |
# This fixes: |
|
4 |
# * Some weird unnecessary string interpolation |
|
5 |
# * The :install task doesn't work unless you have RUBYARCHDIR in your environment, instead of |
|
6 |
# falling back to CONFIG['sitearchdir']. |
|
7 |
# * The :install task created the target install directory every time instead of just |
|
8 |
# declaring a dependency on a directory task |
|
9 |
||
10 |
require 'mkrf/generator' |
|
11 |
||
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
12 |
RAKEFILE_TEMPLATE = %q{ |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
13 |
# Generated by monkeypatched mkrf |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
14 |
require 'rake/clean' |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
15 |
require 'rbconfig' |
0 | 16 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
17 |
include Config |
0 | 18 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
19 |
SRC = FileList[#{sources.join(',')}] |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
20 |
OBJ = SRC.ext('#{objext}') |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
21 |
CC = '#{@cc}' |
0 | 22 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
23 |
ADDITIONAL_OBJECTS = '#{objects}' |
0 | 24 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
25 |
LDSHARED = "#{@available.ldshared_string} #{ldshared}" |
0 | 26 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
27 |
LIBPATH = "#{library_path(CONFIG['libdir'])} #{@available.library_paths_compile_string}" |
0 | 28 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
29 |
INCLUDES = "#{@available.includes_compile_string}" |
0 | 30 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
31 |
LIBS = "#{@available.library_compile_string}" |
0 | 32 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
33 |
CFLAGS = "#{cflags} #{defines_compile_string}" |
0 | 34 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
35 |
RUBYARCHDIR = ENV["RUBYARCHDIR"] || CONFIG['sitearchdir'] |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
36 |
LIBRUBYARG_SHARED = "#{CONFIG['LIBRUBYARG_SHARED']}" |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
37 |
EXT = '#{@extension_name}' |
0 | 38 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
39 |
CLEAN.include( EXT, '*.#{objext}' ) |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
40 |
CLOBBER.include( 'mkrf.log' ) |
0 | 41 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
42 |
task :default => EXT |
0 | 43 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
44 |
rule '.#{objext}' => '.#{@source_extension}' do |t| |
2 | 45 |
$stderr.puts " building \#{t.name} from \#{t.source}" |
46 |
sh "\#{CC} \#{CFLAGS} \#{INCLUDES} -c \#{t.source}" |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
47 |
end |
0 | 48 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
49 |
desc "Build this extension" |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
50 |
file EXT => OBJ do |
2 | 51 |
$stderr.puts " linking \#{OBJ.join(', ')} into \#{EXT}" |
52 |
sh "\#{LDSHARED} \#{LIBPATH} #{@available.ld_outfile(@extension_name)} \#{OBJ} \#{ADDITIONAL_OBJECTS} \#{LIBS} \#{LIBRUBYARG_SHARED}" |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
53 |
end |
0 | 54 |
|
55 |
||
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
56 |
directory RUBYARCHDIR |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
57 |
|
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
58 |
desc "Install this extension" |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
59 |
task :install => [EXT, RUBYARCHDIR] do |
2 | 60 |
install EXT, RUBYARCHDIR, :verbose => true |
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
61 |
end |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
62 |
|
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
63 |
#{additional_code} |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
64 |
}.gsub( /^\t/m, '' ) |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
65 |
|
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
66 |
|
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
67 |
|
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
68 |
module Mkrf |
0 | 69 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
70 |
class Availability |
2 | 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 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
148 |
def silence_stream( stream ) |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
149 |
old_stream = stream.dup |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
150 |
stream.reopen( @logger.instance_variable_get(:@logdev).dev ) |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
151 |
stream.sync = true |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
152 |
yield |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
153 |
ensure |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
154 |
stream.reopen( old_stream ) |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
155 |
end |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
156 |
end |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
157 |
|
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
158 |
class Generator |
0 | 159 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
160 |
def rakefile_contents # :nodoc: |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
161 |
objext = CONFIG['OBJEXT'] |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
162 |
return interpolate( RAKEFILE_TEMPLATE, binding() ) |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
163 |
end |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
164 |
|
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
165 |
|
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
166 |
### Interpolate any '#{...}' placeholders in the string within the given |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
167 |
### +scope+ (a Binding object). |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
168 |
def interpolate( string, scope ) |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
169 |
unless scope.is_a?( Binding ) |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
170 |
raise TypeError, "Argument to interpolate must be a Binding, not "\ |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
171 |
"a #{scope.class.name}" |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
172 |
end |
0 | 173 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
174 |
# $stderr.puts ">>> Interpolating '#{self}'..." |
0 | 175 |
|
1
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
176 |
copy = string.gsub( /"/, %q:\": ) |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
177 |
eval( '"' + copy + '"', scope ) |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
178 |
rescue Exception => err |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
179 |
nicetrace = err.backtrace.find_all {|frame| |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
180 |
/in `(interpolate|eval)'/i !~ frame |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
181 |
} |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
182 |
Kernel.raise( err, err.message, nicetrace ) |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
183 |
end |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
184 |
|
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
185 |
end # class Generator |
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
186 |
|
09d0d209d06d
Added some stuff to the mkrf monkeypatch to log output from the header/library
Michael Granger <ged@FaerieMUD.org>
parents:
0
diff
changeset
|
187 |
end # module Mkrf |