autorake 2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,59 @@
1
+ #
2
+ # autorake/configure.rb -- Configure scripts
3
+ #
4
+
5
+ require "autorake/directories"
6
+ require "yaml"
7
+
8
+ module Autorake
9
+
10
+ class Configuration
11
+
12
+ CONFIG_FILE = ".configure"
13
+
14
+ attr_reader :directories
15
+ attr_reader :features, :parameters
16
+ attr_reader :incdirs, :headers, :macros, :libdirs, :libs
17
+
18
+ def initialize environment, directories
19
+ @environment = {}
20
+ environment.each { |k,v| @environment[ k] = v }
21
+ @directories = directories
22
+ @features = {}
23
+ @parameters = {}
24
+ @incdirs = []
25
+ @headers = []
26
+ @macros = {}
27
+ @libdirs = []
28
+ @libs = []
29
+ end
30
+
31
+ def do_env
32
+ @environment.each { |k,v| ENV[ k] = v }
33
+ end
34
+
35
+ def dump
36
+ puts "Environment:"
37
+ @environment.each { |k,v| puts " #{k}=#{v}" }
38
+ puts "Directories:"
39
+ @directories.keys.each { |k| puts " #{k}=#{@directories.expanded k}" }
40
+ puts "Features:"
41
+ @features.each { |k,v| puts " #{k}=#{v}" }
42
+ puts "Parameters:"
43
+ @parameters.each { |k,v| puts " #{k}=#{v}" }
44
+ puts "Include directories:"
45
+ @incdirs.each { |d| puts " #{d}" }
46
+ puts "Header files:"
47
+ @headers.each { |h| puts " #{h}" }
48
+ puts "C Macros:"
49
+ @macros.each { |k,v| puts " #{k}=#{v}" }
50
+ puts "Library directories:"
51
+ @libdirs.each { |d| puts " #{d}" }
52
+ puts "Libraries:"
53
+ @libs.each { |l| puts " #{l}" }
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+
@@ -0,0 +1,300 @@
1
+ #
2
+ # autorake/definition.rb -- Definitions to produce a config
3
+ #
4
+
5
+ require "autorake/compile"
6
+ require "autorake/configure"
7
+
8
+ module Autorake
9
+
10
+ class Definitions
11
+
12
+ attr_reader :environment, :directories, :features
13
+ def parameters ; @args[ :par] ; end
14
+ def incpath ; @args[ :inc] ; end
15
+ def libpath ; @args[ :lib] ; end
16
+
17
+ def initialize
18
+ @environment = {}
19
+ @directories = Directories.new
20
+ @features = {}
21
+ @args = { :par => {}, :inc => {}, :lib => {}, }
22
+ @checks = []
23
+ end
24
+
25
+ def dump
26
+ c = perform
27
+ c.dump
28
+ end
29
+
30
+ def perform
31
+ Compiler.quiet = true
32
+ c = Configuration.new @environment, @directories
33
+ c.do_env
34
+ c.features.update @features
35
+ c.incdirs.push std_incdir
36
+ c.libdirs.push std_libdir
37
+ af = @features.keys.map { |k| AddFeature.new k }
38
+ am = @args[ :par].map { |k,v| AddMacro.new k, v }
39
+ ai = @args[ :inc].map { |k,v| AddIncdir.new k, v }
40
+ al = @args[ :lib].map { |k,v| AddLibdir.new k, v }
41
+ [ af, am, ai, al, @checks].each { |a| a.each { |k| k.perform c } }
42
+ c
43
+ end
44
+
45
+ protected
46
+
47
+ def std_incdir ; @directories.expand "INCLUDE" ; end
48
+ def std_libdir ; @directories.expand "LIB" ; end
49
+
50
+ def directory name, dir
51
+ @directories[ name]= dir
52
+ end
53
+
54
+ def feature name, enabled = nil
55
+ name = name.to_sym
56
+ @current and raise "Features may not be nested."
57
+ @current = name
58
+ @features[ name] = enabled
59
+ yield if block_given?
60
+ ensure
61
+ @current = nil
62
+ end
63
+ def enable name, &block
64
+ feature name, true, &block
65
+ end
66
+ def disable name, &block
67
+ feature name, false, &block
68
+ end
69
+
70
+ def with name, val ; argdef :par, name, val ; end
71
+
72
+ def incdir name, dir ; argdef :inc, name, dir ; end
73
+ def libdir name, dir ; argdef :lib, name, dir ; end
74
+
75
+ def extending_ruby
76
+ if RUBY_VERSION < "1.9" then
77
+ incdir :ruby, RbConfig::CONFIG[ "topdir"]
78
+ else
79
+ h = RbConfig::CONFIG[ "rubyhdrdir"]
80
+ incdir :ruby, h
81
+ incdir :ruby_arch, (File.join h, RbConfig::CONFIG[ "arch"])
82
+ #incdir :ruby_backward, (File.join h, "ruby/backward")
83
+ end
84
+ libdir :ruby, RbConfig::CONFIG[ "libdir"]
85
+ l = RbConfig::CONFIG[ "LIBRUBY"]
86
+ l.slice! /\Alib/
87
+ l.slice! /\.so(?:\..*)?\z/
88
+ have_library l
89
+ end
90
+
91
+ def have_header name
92
+ c = CheckHeader.new @current, name
93
+ @checks.push c
94
+ end
95
+
96
+ def have_macro name
97
+ c = CheckMacro.new @current, name
98
+ @checks.push c
99
+ end
100
+
101
+ def have_func name
102
+ c = CheckFunction.new @current, name
103
+ @checks.push c
104
+ end
105
+
106
+ def have_library name
107
+ c = CheckLibrary.new @current, name
108
+ @checks.push c
109
+ end
110
+
111
+ private
112
+
113
+ def argdef type, name, dir
114
+ return unless dir
115
+ dir.chomp!
116
+ return if dir.empty?
117
+ name = "#@current/#{name}" if @current
118
+ @args[ type][ name.to_sym] = dir
119
+ end
120
+
121
+ end
122
+
123
+
124
+ class Add
125
+ def initialize feature, name
126
+ @feature, @name = feature, name
127
+ end
128
+ def perform config
129
+ @config = config
130
+ check! and set!
131
+ ensure
132
+ @config = nil
133
+ end
134
+ private
135
+ def check!
136
+ not @feature or
137
+ @config.features[ @feature]
138
+ end
139
+ def set!
140
+ end
141
+ def name_upcase
142
+ r = @name.to_s.upcase
143
+ r.gsub! /[^A-Z_]/, "_"
144
+ r
145
+ end
146
+ end
147
+
148
+ class AddFeature < Add
149
+ def initialize feature
150
+ super feature, feature
151
+ end
152
+ def set!
153
+ @config.macros[ "FEATURE_#{name_upcase}"] = true
154
+ end
155
+ end
156
+
157
+ class AddKeyVal < Add
158
+ def initialize key, val
159
+ x, y = key.to_s.split "/"
160
+ if y then
161
+ x = x.to_sym
162
+ else
163
+ x, y = nil, x
164
+ end
165
+ super x, y.to_sym
166
+ @val = val
167
+ end
168
+ private
169
+ def expanded
170
+ @config.directories.expand @val
171
+ end
172
+ end
173
+ class AddMacro < AddKeyVal
174
+ def set!
175
+ @config.parameters[ @name] = @val
176
+ @config.macros[ "WITH_#{name_upcase}"] = @val
177
+ end
178
+ end
179
+ class AddIncdir < AddKeyVal
180
+ def set!
181
+ @config.incdirs.push expanded
182
+ end
183
+ end
184
+ class AddLibdir < AddKeyVal
185
+ def set!
186
+ @config.libdirs.push expanded
187
+ end
188
+ end
189
+
190
+ class Check < Add
191
+ private
192
+ def check!
193
+ super or return
194
+ print "Checking for #{self.class::TYPE} #@name ... "
195
+ res = TmpFiles.open build_source do |t|
196
+ compile t
197
+ end
198
+ print "yes"
199
+ true
200
+ rescue Compiler::Error
201
+ print "no"
202
+ false
203
+ ensure
204
+ puts
205
+ end
206
+ end
207
+
208
+ class CheckHeader < Check
209
+ TYPE = "header"
210
+ private
211
+ def build_source
212
+ <<-SRC
213
+ #include <#@name>
214
+ SRC
215
+ end
216
+ def compile t
217
+ c = CompilerPP.new @config.incdirs, @config.macros, "-w"
218
+ c.cc t.cpp, t.src
219
+ end
220
+ def set!
221
+ @config.macros[ "HAVE_HEADER_#{name_upcase}"] = true
222
+ @config.headers.push @name
223
+ end
224
+ end
225
+
226
+ class CheckWithHeaders < Check
227
+ private
228
+ def build_source
229
+ src = ""
230
+ @config.headers.each { |i|
231
+ src << <<-SRC
232
+ #include <#{i}>
233
+ SRC
234
+ }
235
+ src
236
+ end
237
+ end
238
+
239
+ class CheckMacro < CheckWithHeaders
240
+ TYPE = "macro"
241
+ private
242
+ def build_source
243
+ super << <<-SRC
244
+ #ifndef #@name
245
+ #error not defined
246
+ #endif
247
+ SRC
248
+ end
249
+ def compile t
250
+ c = CompilerPP.new @config.incdirs, @config.macros, "-w"
251
+ c.cc t.cpp, t.src
252
+ end
253
+ def check!
254
+ super or raise "Macro not defined: #@name."
255
+ end
256
+ end
257
+
258
+ class CheckFunction < CheckWithHeaders
259
+ TYPE = "function"
260
+ private
261
+ def build_source
262
+ super << <<-SRC
263
+ void dummy( void)
264
+ {
265
+ void (*f)( void) = (void (*)( void)) #@name;
266
+ }
267
+ SRC
268
+ end
269
+ def compile t
270
+ c = CompilerC.new @config.incdirs, @config.macros, "-w"
271
+ c.cc t.obj, t.src
272
+ end
273
+ def set!
274
+ @config.macros[ "HAVE_FUNC_#{name_upcase}"] = true
275
+ end
276
+ end
277
+
278
+ class CheckLibrary < Check
279
+ TYPE = "library"
280
+ def build_source
281
+ <<-SRC
282
+ int main( int argc, char *argv[]) { return 0; }
283
+ SRC
284
+ end
285
+ def compile t
286
+ c = CompilerC.new @config.incdirs, @config.macros, "-w"
287
+ c.cc t.obj, t.src
288
+ l = Linker.new @config.libdirs, [ @name], "-w"
289
+ l.cc t.bin, t.obj
290
+ end
291
+ def check!
292
+ super or raise "Library missing: #@name."
293
+ end
294
+ def set!
295
+ @config.libs.push @name
296
+ end
297
+ end
298
+
299
+ end
300
+
@@ -0,0 +1,57 @@
1
+ #
2
+ # autorake/directories.rb -- Directory shortcuts
3
+ #
4
+
5
+ module Autorake
6
+
7
+ class Directories < Hash
8
+
9
+ STD = {
10
+ :prefix => "/usr/local",
11
+ :eprefix => "PREFIX",
12
+ :cprefix => "EPREFIX",
13
+ :bin => "EPREFIX/bin",
14
+ :sbin => "EPREFIX/sbin",
15
+ :libexec => "EPREFIX/libexec",
16
+ :sysconf => "CPREFIX/etc",
17
+ :localstate => "/var",
18
+ :lib => "EPREFIX/lib",
19
+ :include => "PREFIX/include",
20
+ :data => "PREFIX/share",
21
+ :info => "DATA/info",
22
+ :locale => "DATA/locale",
23
+ :man => "DATA/man",
24
+ }
25
+
26
+ def initialize
27
+ super
28
+ update STD
29
+ end
30
+
31
+ def [] key
32
+ super key.to_sym
33
+ end
34
+
35
+ def []= key, value
36
+ super key.to_sym, value
37
+ end
38
+
39
+ def expanded key
40
+ expand self[ key]
41
+ end
42
+
43
+ def expand dir
44
+ case dir
45
+ when /\A[A-Z_]+/ then
46
+ (expand self[ $&.downcase]) + $'
47
+ when /\A:(\w+)/ then
48
+ (expand self[ $1]) + $'
49
+ else
50
+ dir
51
+ end
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+
@@ -0,0 +1,107 @@
1
+ #
2
+ # autorake/mkconfig.rb -- Make configuration
3
+ #
4
+
5
+ require "autorake/definition"
6
+ require "autorake/application"
7
+
8
+ module Autorake
9
+
10
+ class <<self
11
+ def configure &block
12
+ d = Definitions.new
13
+ d.instance_eval &block
14
+ p = MkConfig.new d
15
+ p.run
16
+ nil
17
+ end
18
+ end
19
+
20
+
21
+ class MkConfig < Application
22
+
23
+ attr_accessor :outfile
24
+ attr_bang :clean, :verbose
25
+
26
+ def initialize definition
27
+ @definition = definition
28
+ end
29
+
30
+ private
31
+
32
+ def define_options
33
+ add_option %w(o outfile), "specify output file",
34
+ Configuration::CONFIG_FILE, :outfile=
35
+ add_option %w(c clean), "delete config file resp. -o outfile",
36
+ nil, :clean!
37
+ add_option %w(d dump), "just dump the results", nil, :dump
38
+ add_option %w(v verbose), "lots of ubly debugging information",
39
+ nil, :verbose!
40
+ super
41
+ @definition.directories.each { |k,v|
42
+ add_option %W(dir-#{k}), "set directory #{k}", v, :set_dir, k
43
+ }
44
+ @definition.features.each { |k,v|
45
+ de, dd = "[default]", nil
46
+ de, dd = dd, de unless v
47
+ add_option %W(enable-#{k}), "enable #{k} #{de}", nil,
48
+ :set_feature, k, true
49
+ add_option %W(disable-#{k}), "disable #{k} #{dd}", nil,
50
+ :set_feature, k, false
51
+ }
52
+ @definition.parameters.each { |k,v|
53
+ add_option %W(with-#{k}), "define a parameter and C macro #{k}", v,
54
+ :set_parm, k
55
+ }
56
+ @definition.incpath.each { |k,v|
57
+ add_option %W(incdir-#{k}), "include directory #{k}", v, :set_incdir, k
58
+ }
59
+ @definition.libpath.each { |k,v|
60
+ add_option %W(libdir-#{k}), "library directory #{k}", v, :set_libdir, k
61
+ }
62
+ end
63
+
64
+ def set_dir name, val
65
+ @definition.directories[ name] = val
66
+ end
67
+
68
+ def set_feature name, val
69
+ @definition.features[ name.to_sym] = val
70
+ end
71
+
72
+ def set_parm name, val
73
+ @definition.parameters[ name.to_sym] = val
74
+ end
75
+
76
+ def set_incdir name, val
77
+ @definition.incpath[ name.to_sym] = val
78
+ end
79
+
80
+ def set_libdir name, val
81
+ @definition.libpath[ name.to_sym] = val
82
+ end
83
+
84
+ def dump
85
+ @definition.dump
86
+ raise Done
87
+ end
88
+
89
+ def environ name, val
90
+ @definition.environment[ name] = val
91
+ end
92
+
93
+ def execute
94
+ if @clean then
95
+ File.unlink @outfile if File.file? @outfile
96
+ else
97
+ cfg = @definition.perform
98
+ File.open @outfile, "w" do |f|
99
+ f.write cfg.to_yaml
100
+ end
101
+ end
102
+ end
103
+
104
+ end
105
+
106
+ end
107
+