mkmf-lite 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,8 @@
1
+ = 0.2.2 - 6-Dec-2011
2
+ * Added the check_sizeof method.
3
+ * If CONFIG['COMMON_HEADERS'] is blank then stdio.h and stdlib.h are
4
+ used instead. On MS Windows the windows.h header is also used.
5
+
1
6
  = 0.2.1 - 21-Jan-2011
2
7
  * Minor platform detection adjustments for MS Windows.
3
8
 
data/lib/mkmf/lite.rb CHANGED
@@ -3,10 +3,16 @@ require 'rbconfig'
3
3
  require 'tmpdir'
4
4
  require 'ptools'
5
5
 
6
+ if File::ALT_SEPARATOR && RUBY_VERSION.to_f < 1.9
7
+ require 'win32/open3'
8
+ else
9
+ require 'open3'
10
+ end
11
+
6
12
  module Mkmf
7
13
  module Lite
8
14
  # The version of the mkmf-lite library
9
- MKMF_LITE_VERSION = '0.2.1'
15
+ MKMF_LITE_VERSION = '0.2.2'
10
16
 
11
17
  @@cpp_command = Config::CONFIG['CC'] || Config::CONFIG['CPP']
12
18
  @@cpp_outfile = Config::CONFIG['CPPOUTFILE'] || "-o conftest.i"
@@ -62,7 +68,7 @@ module Mkmf
62
68
  end
63
69
 
64
70
  # Checks whether or not the struct of type +struct_type+ contains the
65
- # +struct_member+. If it does not, or the struct type cannot be found,
71
+ # +struct_member+. If it does not, or the struct type cannot be found,
66
72
  # then false is returned.
67
73
  #
68
74
  # An optional list of +headers+ may be specified, in addition to the
@@ -76,6 +82,27 @@ module Mkmf
76
82
  try_to_compile(code)
77
83
  end
78
84
 
85
+ # Returns the sizeof +type+ using +headers+, or common headers if no
86
+ # headers are specified.
87
+ #
88
+ # If this method fails an error is raised. This could happen if the type
89
+ # can't be found and/or the header files do not include the indicated type.
90
+ #
91
+ # Example:
92
+ #
93
+ # class Foo
94
+ # include Mkmf::Lite
95
+ # utsname = check_sizeof('struct utsname', 'sys/utsname.h')
96
+ # end
97
+ #
98
+ def check_sizeof(type, headers = [])
99
+ headers = get_header_string(headers)
100
+ erb = ERB.new(read_template('check_sizeof.erb'))
101
+ code = erb.result(binding)
102
+
103
+ try_to_execute(code)
104
+ end
105
+
79
106
  private
80
107
 
81
108
  # Take an array of header file names (or convert it to an array if it's a
@@ -92,8 +119,13 @@ module Mkmf
92
119
 
93
120
  common_headers = Config::CONFIG['COMMON_HEADERS']
94
121
 
95
- unless common_headers.nil? || common_headers.empty?
96
- headers += Config::CONFIG['COMMON_HEADERS'].split
122
+ if common_headers.nil? || common_headers.empty?
123
+ if headers.empty?
124
+ headers = ['stdio.h', 'stdlib.h']
125
+ headers += 'windows.h' if File::ALT_SEPARATOR
126
+ end
127
+ else
128
+ headers += common_headers.split
97
129
  end
98
130
 
99
131
  headers = headers.flatten.uniq
@@ -102,6 +134,49 @@ module Mkmf
102
134
  headers
103
135
  end
104
136
 
137
+ # Create a temporary bit of C source code in the temp directory, and
138
+ # try to compile it. If it succeeds attempt to run the generated code.
139
+ # The code generated is expected to print a number to STDOUT, which
140
+ # is then grabbed and returned as an integer.
141
+ #
142
+ # Note that $stderr is temporarily redirected to the null device because
143
+ # we don't actually care about the reason for failure, though a Ruby
144
+ # error is raised if the compilation step fails.
145
+ #
146
+ def try_to_execute(code)
147
+ begin
148
+ result = 0
149
+
150
+ stderr_orig = $stderr.dup
151
+
152
+ Dir.chdir(Dir.tmpdir){
153
+ File.open(@@cpp_srcfile, 'w'){ |fh| fh.write(code) }
154
+
155
+ command = @@cpp_command + ' '
156
+ command += @@cpp_outfile + ' '
157
+ command += @@cpp_srcfile
158
+
159
+ $stderr.reopen(File.null)
160
+
161
+ if system(command)
162
+ Open3.popen3("./conftest.i") do |stdin, stdout, stderr|
163
+ stdin.close
164
+ stderr.close
165
+ result = stdout.gets.chomp.to_i
166
+ end
167
+ else
168
+ raise "Failed to compile source code:\n===\n" + code + "==="
169
+ end
170
+ }
171
+ ensure
172
+ File.delete(@@cpp_srcfile) if File.exists?(@@cpp_srcfile)
173
+ File.delete(@@cpp_outfile) if File.exists?(@@cpp_outfile)
174
+ $stderr.reopen(stderr_orig)
175
+ end
176
+
177
+ result
178
+ end
179
+
105
180
  # Create a temporary bit of C source code in the temp directory, and
106
181
  # try to compile it. If it succeeds, return true. Otherwise, return
107
182
  # false.
@@ -0,0 +1,10 @@
1
+ #include <stdio.h>
2
+
3
+ <%= headers %>
4
+
5
+ int conftest_const = (int)(sizeof(<%= type %>));
6
+
7
+ int main(){
8
+ printf("%d\n", conftest_const);
9
+ return 0;
10
+ }
data/mkmf-lite.gemspec CHANGED
@@ -3,7 +3,7 @@ require 'rubygems'
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'mkmf-lite'
5
5
  spec.summary = 'A lighter version of mkmf designed for use as a library'
6
- spec.version = '0.2.1'
6
+ spec.version = '0.2.2'
7
7
  spec.author = 'Daniel J. Berger'
8
8
  spec.license = 'Artistic 2.0'
9
9
  spec.email = 'djberg96@gmail.com'
@@ -22,7 +22,7 @@ class TC_Mkmf_Lite < Test::Unit::TestCase
22
22
  end
23
23
 
24
24
  test "version information" do
25
- assert_equal('0.2.1', MKMF_LITE_VERSION)
25
+ assert_equal('0.2.2', MKMF_LITE_VERSION)
26
26
  end
27
27
 
28
28
  test "have_header basic functionality" do
@@ -82,6 +82,31 @@ class TC_Mkmf_Lite < Test::Unit::TestCase
82
82
  }
83
83
  end
84
84
 
85
+ test "check_sizeof basic functionality" do
86
+ assert_respond_to(self, :check_sizeof)
87
+ assert_nothing_raised{ check_sizeof('struct passwd', 'pwd.h') }
88
+ end
89
+
90
+ test "check_sizeof requires at least one argument" do
91
+ assert_raise(ArgumentError){ check_sizeof }
92
+ assert_raise(ArgumentError){ check_sizeof('struct passwd', 'pw_name', 1) }
93
+ end
94
+
95
+ test "check_sizeof accepts a maximum of two arguments" do
96
+ assert_raise(ArgumentError){ check_sizeof('div_t', 'stdlib.h', 1) }
97
+ end
98
+
99
+ test "check_sizeof works with one or two arguments" do
100
+ assert_nothing_raised{ check_sizeof('div_t') }
101
+ assert_nothing_raised{ check_sizeof('div_t', 'stdlib.h') }
102
+ end
103
+
104
+ test "check_sizeof returns an integer value" do
105
+ size = check_sizeof('struct passwd', 'pwd.h')
106
+ assert_kind_of(Integer, size)
107
+ assert_true(size > 0)
108
+ end
109
+
85
110
  def teardown
86
111
  @st_type = nil
87
112
  @st_member = nil
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mkmf-lite
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
5
- prerelease: false
4
+ hash: 19
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Daniel J. Berger
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-21 00:00:00 -07:00
19
- default_executable:
18
+ date: 2011-12-06 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: ptools
@@ -61,6 +60,7 @@ extra_rdoc_files:
61
60
  files:
62
61
  - CHANGES
63
62
  - lib/mkmf/lite.rb
63
+ - lib/mkmf/templates/check_sizeof.erb
64
64
  - lib/mkmf/templates/have_func.erb
65
65
  - lib/mkmf/templates/have_func_pointer.erb
66
66
  - lib/mkmf/templates/have_header.erb
@@ -70,7 +70,6 @@ files:
70
70
  - Rakefile
71
71
  - README
72
72
  - test/test_mkmf_lite.rb
73
- has_rdoc: true
74
73
  homepage: http://www.rubyforge.org/projects/shards
75
74
  licenses:
76
75
  - Artistic 2.0
@@ -100,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
99
  requirements: []
101
100
 
102
101
  rubyforge_project: shards
103
- rubygems_version: 1.3.7
102
+ rubygems_version: 1.8.10
104
103
  signing_key:
105
104
  specification_version: 3
106
105
  summary: A lighter version of mkmf designed for use as a library