zig_example 0.4.0.pre → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/ext/mkmf.rb DELETED
@@ -1,2737 +0,0 @@
1
- # -*- coding: us-ascii -*-
2
- # frozen-string-literal: false
3
- # module to create Makefile for extension modules
4
- # modified to invoke zig build if build.zig exists
5
- # invoke like: ruby -r mkmf extconf.rb
6
-
7
- require 'rbconfig'
8
- require 'fileutils'
9
- require 'shellwords'
10
-
11
- # :stopdoc:
12
- class String
13
- # Wraps a string in escaped quotes if it contains whitespace.
14
- def quote
15
- /\s/ =~ self ? "\"#{self}\"" : "#{self}"
16
- end
17
-
18
- # Escape whitespaces for Makefile.
19
- def unspace
20
- gsub(/\s/, '\\\\\\&')
21
- end
22
-
23
- # Generates a string used as cpp macro name.
24
- def tr_cpp
25
- strip.upcase.tr_s("^A-Z0-9_*", "_").tr_s("*", "P")
26
- end
27
-
28
- def funcall_style
29
- /\)\z/ =~ self ? dup : "#{self}()"
30
- end
31
-
32
- def sans_arguments
33
- self[/\A[^()]+/]
34
- end
35
- end
36
-
37
- class Array
38
- # Wraps all strings in escaped quotes if they contain whitespace.
39
- def quote
40
- map {|s| s.quote}
41
- end
42
- end
43
- # :startdoc:
44
-
45
- ##
46
- # mkmf.rb is used by Ruby C extensions to generate a Makefile which will
47
- # correctly compile and link the C extension to Ruby and a third-party
48
- # library.
49
- module MakeMakefile
50
- #### defer until this module become global-state free.
51
- # def self.extended(obj)
52
- # obj.init_mkmf
53
- # super
54
- # end
55
- #
56
- # def initialize(*args, rbconfig: RbConfig, **rest)
57
- # init_mkmf(rbconfig::MAKEFILE_CONFIG, rbconfig::CONFIG)
58
- # super(*args, **rest)
59
- # end
60
-
61
- ##
62
- # The makefile configuration using the defaults from when Ruby was built.
63
-
64
- CONFIG = RbConfig::MAKEFILE_CONFIG
65
- ORIG_LIBPATH = ENV['LIB']
66
-
67
- ##
68
- # Extensions for files compiled with a C compiler
69
-
70
- C_EXT = %w[c m]
71
-
72
- ##
73
- # Extensions for files complied with a C++ compiler
74
-
75
- CXX_EXT = %w[cc mm cxx cpp]
76
- unless File.exist?(File.join(*File.split(__FILE__).tap {|d, b| b.swapcase}))
77
- CXX_EXT.concat(%w[C])
78
- end
79
-
80
- ##
81
- # Extensions for source files
82
-
83
- SRC_EXT = C_EXT + CXX_EXT
84
-
85
- ##
86
- # Extensions for header files
87
-
88
- HDR_EXT = %w[h hpp]
89
- $static = nil
90
- $config_h = '$(arch_hdrdir)/ruby/config.h'
91
- $default_static = $static
92
-
93
- unless defined? $configure_args
94
- $configure_args = {}
95
- args = CONFIG["configure_args"]
96
- if ENV["CONFIGURE_ARGS"]
97
- args << " " << ENV["CONFIGURE_ARGS"]
98
- end
99
- for arg in Shellwords::shellwords(args)
100
- arg, val = arg.split('=', 2)
101
- next unless arg
102
- arg.tr!('_', '-')
103
- if arg.sub!(/^(?!--)/, '--')
104
- val or next
105
- arg.downcase!
106
- end
107
- next if /^--(?:top|topsrc|src|cur)dir$/ =~ arg
108
- $configure_args[arg] = val || true
109
- end
110
- for arg in ARGV
111
- arg, val = arg.split('=', 2)
112
- next unless arg
113
- arg.tr!('_', '-')
114
- if arg.sub!(/^(?!--)/, '--')
115
- val or next
116
- arg.downcase!
117
- end
118
- $configure_args[arg] = val || true
119
- end
120
- end
121
-
122
- $libdir = CONFIG["libdir"]
123
- $rubylibdir = CONFIG["rubylibdir"]
124
- $archdir = CONFIG["archdir"]
125
- $sitedir = CONFIG["sitedir"]
126
- $sitelibdir = CONFIG["sitelibdir"]
127
- $sitearchdir = CONFIG["sitearchdir"]
128
- $vendordir = CONFIG["vendordir"]
129
- $vendorlibdir = CONFIG["vendorlibdir"]
130
- $vendorarchdir = CONFIG["vendorarchdir"]
131
-
132
- $mswin = /mswin/ =~ RUBY_PLATFORM
133
- $bccwin = /bccwin/ =~ RUBY_PLATFORM
134
- $mingw = /mingw/ =~ RUBY_PLATFORM
135
- $cygwin = /cygwin/ =~ RUBY_PLATFORM
136
- $netbsd = /netbsd/ =~ RUBY_PLATFORM
137
- $haiku = /haiku/ =~ RUBY_PLATFORM
138
- $solaris = /solaris/ =~ RUBY_PLATFORM
139
- $universal = /universal/ =~ RUBY_PLATFORM
140
- $dest_prefix_pattern = (File::PATH_SEPARATOR == ';' ? /\A([[:alpha:]]:)?/ : /\A/)
141
-
142
- # :stopdoc:
143
-
144
- def config_string(key, config = CONFIG)
145
- s = config[key] and !s.empty? and block_given? ? yield(s) : s
146
- end
147
- module_function :config_string
148
-
149
- def dir_re(dir)
150
- Regexp.new('\$(?:\('+dir+'\)|\{'+dir+'\})(?:\$(?:\(target_prefix\)|\{target_prefix\}))?')
151
- end
152
- module_function :dir_re
153
-
154
- def relative_from(path, base)
155
- dir = File.join(path, "")
156
- if File.expand_path(dir) == File.expand_path(dir, base)
157
- path
158
- else
159
- File.join(base, path)
160
- end
161
- end
162
-
163
- INSTALL_DIRS = [
164
- [dir_re('commondir'), "$(RUBYCOMMONDIR)"],
165
- [dir_re('sitedir'), "$(RUBYCOMMONDIR)"],
166
- [dir_re('vendordir'), "$(RUBYCOMMONDIR)"],
167
- [dir_re('rubylibdir'), "$(RUBYLIBDIR)"],
168
- [dir_re('archdir'), "$(RUBYARCHDIR)"],
169
- [dir_re('sitelibdir'), "$(RUBYLIBDIR)"],
170
- [dir_re('vendorlibdir'), "$(RUBYLIBDIR)"],
171
- [dir_re('sitearchdir'), "$(RUBYARCHDIR)"],
172
- [dir_re('vendorarchdir'), "$(RUBYARCHDIR)"],
173
- [dir_re('rubyhdrdir'), "$(RUBYHDRDIR)"],
174
- [dir_re('sitehdrdir'), "$(SITEHDRDIR)"],
175
- [dir_re('vendorhdrdir'), "$(VENDORHDRDIR)"],
176
- [dir_re('bindir'), "$(BINDIR)"],
177
- ]
178
-
179
- def install_dirs(target_prefix = nil)
180
- if $extout
181
- dirs = [
182
- ['BINDIR', '$(extout)/bin'],
183
- ['RUBYCOMMONDIR', '$(extout)/common'],
184
- ['RUBYLIBDIR', '$(RUBYCOMMONDIR)$(target_prefix)'],
185
- ['RUBYARCHDIR', '$(extout)/$(arch)$(target_prefix)'],
186
- ['HDRDIR', '$(extout)/include/ruby$(target_prefix)'],
187
- ['ARCHHDRDIR', '$(extout)/include/$(arch)/ruby$(target_prefix)'],
188
- ['extout', "#$extout"],
189
- ['extout_prefix', "#$extout_prefix"],
190
- ]
191
- elsif $extmk
192
- dirs = [
193
- ['BINDIR', '$(bindir)'],
194
- ['RUBYCOMMONDIR', '$(rubylibdir)'],
195
- ['RUBYLIBDIR', '$(rubylibdir)$(target_prefix)'],
196
- ['RUBYARCHDIR', '$(archdir)$(target_prefix)'],
197
- ['HDRDIR', '$(rubyhdrdir)/ruby$(target_prefix)'],
198
- ['ARCHHDRDIR', '$(rubyhdrdir)/$(arch)/ruby$(target_prefix)'],
199
- ]
200
- elsif $configure_args.has_key?('--vendor')
201
- dirs = [
202
- ['BINDIR', '$(bindir)'],
203
- ['RUBYCOMMONDIR', '$(vendordir)$(target_prefix)'],
204
- ['RUBYLIBDIR', '$(vendorlibdir)$(target_prefix)'],
205
- ['RUBYARCHDIR', '$(vendorarchdir)$(target_prefix)'],
206
- ['HDRDIR', '$(rubyhdrdir)/ruby$(target_prefix)'],
207
- ['ARCHHDRDIR', '$(rubyhdrdir)/$(arch)/ruby$(target_prefix)'],
208
- ]
209
- else
210
- dirs = [
211
- ['BINDIR', '$(bindir)'],
212
- ['RUBYCOMMONDIR', '$(sitedir)$(target_prefix)'],
213
- ['RUBYLIBDIR', '$(sitelibdir)$(target_prefix)'],
214
- ['RUBYARCHDIR', '$(sitearchdir)$(target_prefix)'],
215
- ['HDRDIR', '$(rubyhdrdir)/ruby$(target_prefix)'],
216
- ['ARCHHDRDIR', '$(rubyhdrdir)/$(arch)/ruby$(target_prefix)'],
217
- ]
218
- end
219
- dirs << ['target_prefix', (target_prefix ? "/#{target_prefix}" : "")]
220
- dirs
221
- end
222
-
223
- def map_dir(dir, map = nil)
224
- map ||= INSTALL_DIRS
225
- map.inject(dir) {|d, (orig, new)| d.gsub(orig, new)}
226
- end
227
-
228
- topdir = File.dirname(File.dirname(__FILE__))
229
- path = File.expand_path($0)
230
- until (dir = File.dirname(path)) == path
231
- if File.identical?(dir, topdir)
232
- $extmk = true if %r"\A(?:ext|enc|tool|test)\z" =~ File.basename(path)
233
- break
234
- end
235
- path = dir
236
- end
237
- # set to false
238
- $extmk = false
239
- if not $extmk and File.exist?(($hdrdir = RbConfig::CONFIG["rubyhdrdir"]) + "/ruby/ruby.h")
240
- $topdir = $hdrdir
241
- $top_srcdir = $hdrdir
242
- $arch_hdrdir = RbConfig::CONFIG["rubyarchhdrdir"]
243
- elsif File.exist?(($hdrdir = ($top_srcdir ||= topdir) + "/include") + "/ruby.h")
244
- $topdir ||= RbConfig::CONFIG["topdir"]
245
- $arch_hdrdir = "$(extout)/include/$(arch)"
246
- else
247
- abort "mkmf.rb can't find header files for ruby at #{$hdrdir}/ruby.h"
248
- end
249
-
250
- CONFTEST = "conftest".freeze
251
- CONFTEST_C = "#{CONFTEST}.c"
252
-
253
- OUTFLAG = CONFIG['OUTFLAG']
254
- COUTFLAG = CONFIG['COUTFLAG']
255
- CPPOUTFILE = config_string('CPPOUTFILE') {|str| str.sub(/\bconftest\b/, CONFTEST)}
256
-
257
- def rm_f(*files)
258
- opt = (Hash === files.last ? [files.pop] : [])
259
- FileUtils.rm_f(Dir[*files.flatten], *opt)
260
- end
261
- module_function :rm_f
262
-
263
- def rm_rf(*files)
264
- opt = (Hash === files.last ? [files.pop] : [])
265
- FileUtils.rm_rf(Dir[*files.flatten], *opt)
266
- end
267
- module_function :rm_rf
268
-
269
- # Returns time stamp of the +target+ file if it exists and is newer than or
270
- # equal to all of +times+.
271
- def modified?(target, times)
272
- (t = File.mtime(target)) rescue return nil
273
- Array === times or times = [times]
274
- t if times.all? {|n| n <= t}
275
- end
276
-
277
- def split_libs(*strs)
278
- strs.map {|s| s.split(/\s+(?=-|\z)/)}.flatten
279
- end
280
-
281
- def merge_libs(*libs)
282
- libs.inject([]) do |x, y|
283
- y = y.inject([]) {|ary, e| ary.last == e ? ary : ary << e}
284
- y.each_with_index do |v, yi|
285
- if xi = x.rindex(v)
286
- x[(xi+1)..-1] = merge_libs(y[(yi+1)..-1], x[(xi+1)..-1])
287
- x[xi, 0] = y[0...yi]
288
- break
289
- end
290
- end and x.concat(y)
291
- x
292
- end
293
- end
294
-
295
- # This is a custom logging module. It generates an mkmf.log file when you
296
- # run your extconf.rb script. This can be useful for debugging unexpected
297
- # failures.
298
- #
299
- # This module and its associated methods are meant for internal use only.
300
- #
301
- module Logging
302
- @log = nil
303
- @logfile = 'mkmf.log'
304
- @orgerr = $stderr.dup
305
- @orgout = $stdout.dup
306
- @postpone = 0
307
- @quiet = $extmk
308
-
309
- def self::log_open
310
- @log ||= File::open(@logfile, 'wb')
311
- @log.sync = true
312
- end
313
-
314
- def self::log_opened?
315
- @log and not @log.closed?
316
- end
317
-
318
- def self::open
319
- log_open
320
- $stderr.reopen(@log)
321
- $stdout.reopen(@log)
322
- yield
323
- ensure
324
- $stderr.reopen(@orgerr)
325
- $stdout.reopen(@orgout)
326
- end
327
-
328
- def self::message(*s)
329
- log_open
330
- @log.printf(*s)
331
- end
332
-
333
- def self::logfile file
334
- @logfile = file
335
- log_close
336
- end
337
-
338
- def self::log_close
339
- if @log and not @log.closed?
340
- @log.flush
341
- @log.close
342
- @log = nil
343
- end
344
- end
345
-
346
- def self::postpone
347
- tmplog = "mkmftmp#{@postpone += 1}.log"
348
- open do
349
- log, *save = @log, @logfile, @orgout, @orgerr
350
- @log, @logfile, @orgout, @orgerr = nil, tmplog, log, log
351
- begin
352
- log.print(open {yield @log})
353
- ensure
354
- @log.close if @log and not @log.closed?
355
- File::open(tmplog) {|t| FileUtils.copy_stream(t, log)} if File.exist?(tmplog)
356
- @log, @logfile, @orgout, @orgerr = log, *save
357
- @postpone -= 1
358
- MakeMakefile.rm_f tmplog
359
- end
360
- end
361
- end
362
-
363
- class << self
364
- attr_accessor :quiet
365
- end
366
- end
367
-
368
- def libpath_env
369
- # used only if native compiling
370
- if libpathenv = config_string("LIBPATHENV")
371
- pathenv = ENV[libpathenv]
372
- libpath = RbConfig.expand($DEFLIBPATH.join(File::PATH_SEPARATOR))
373
- {libpathenv => [libpath, pathenv].compact.join(File::PATH_SEPARATOR)}
374
- else
375
- {}
376
- end
377
- end
378
-
379
- def xsystem command, opts = nil
380
- varpat = /\$\((\w+)\)|\$\{(\w+)\}/
381
- if varpat =~ command
382
- vars = Hash.new {|h, k| h[k] = ENV[k]}
383
- command = command.dup
384
- nil while command.gsub!(varpat) {vars[$1||$2]}
385
- end
386
- Logging::open do
387
- puts command.quote
388
- if opts and opts[:werror]
389
- result = nil
390
- Logging.postpone do |log|
391
- output = IO.popen(libpath_env, command, &:read)
392
- result = ($?.success? and File.zero?(log.path))
393
- output
394
- end
395
- result
396
- else
397
- system(libpath_env, command)
398
- end
399
- end
400
- end
401
-
402
- def xpopen command, *mode, &block
403
- Logging::open do
404
- case mode[0]
405
- when nil, /^r/
406
- puts "#{command} |"
407
- else
408
- puts "| #{command}"
409
- end
410
- IO.popen(libpath_env, command, *mode, &block)
411
- end
412
- end
413
-
414
- def log_src(src, heading="checked program was")
415
- src = src.split(/^/)
416
- fmt = "%#{src.size.to_s.size}d: %s"
417
- Logging::message <<"EOM"
418
- #{heading}:
419
- /* begin */
420
- EOM
421
- src.each_with_index {|line, no| Logging::message fmt, no+1, line}
422
- Logging::message <<"EOM"
423
- /* end */
424
-
425
- EOM
426
- end
427
-
428
- def create_tmpsrc(src)
429
- src = "#{COMMON_HEADERS}\n#{src}"
430
- src = yield(src) if block_given?
431
- src.gsub!(/[ \t]+$/, '')
432
- src.gsub!(/\A\n+|^\n+$/, '')
433
- src.sub!(/[^\n]\z/, "\\&\n")
434
- count = 0
435
- begin
436
- open(CONFTEST_C, "wb") do |cfile|
437
- cfile.print src
438
- end
439
- rescue Errno::EACCES
440
- if (count += 1) < 5
441
- sleep 0.2
442
- retry
443
- end
444
- end
445
- src
446
- end
447
-
448
- def have_devel?
449
- unless defined? $have_devel
450
- $have_devel = true
451
- $have_devel = try_link(MAIN_DOES_NOTHING)
452
- end
453
- $have_devel
454
- end
455
-
456
- def try_do(src, command, *opts, &b)
457
- unless have_devel?
458
- raise <<MSG
459
- The compiler failed to generate an executable file.
460
- You have to install development tools first.
461
- MSG
462
- end
463
- begin
464
- src = create_tmpsrc(src, &b)
465
- xsystem(command, *opts)
466
- ensure
467
- log_src(src)
468
- MakeMakefile.rm_rf "#{CONFTEST}.dSYM"
469
- end
470
- end
471
-
472
- def link_command(ldflags, opt="", libpath=$DEFLIBPATH|$LIBPATH)
473
- librubyarg = $extmk ? $LIBRUBYARG_STATIC : "$(LIBRUBYARG)"
474
- conf = RbConfig::CONFIG.merge('hdrdir' => $hdrdir.quote,
475
- 'src' => "#{CONFTEST_C}",
476
- 'arch_hdrdir' => $arch_hdrdir.quote,
477
- 'top_srcdir' => $top_srcdir.quote,
478
- 'INCFLAGS' => "#$INCFLAGS",
479
- 'CPPFLAGS' => "#$CPPFLAGS",
480
- 'CFLAGS' => "#$CFLAGS",
481
- 'ARCH_FLAG' => "#$ARCH_FLAG",
482
- 'LDFLAGS' => "#$LDFLAGS #{ldflags}",
483
- 'LOCAL_LIBS' => "#$LOCAL_LIBS #$libs",
484
- 'LIBS' => "#{librubyarg} #{opt} #$LIBS")
485
- conf['LIBPATH'] = libpathflag(libpath.map {|s| RbConfig::expand(s.dup, conf)})
486
- RbConfig::expand(TRY_LINK.dup, conf)
487
- end
488
-
489
- def cc_command(opt="")
490
- conf = RbConfig::CONFIG.merge('hdrdir' => $hdrdir.quote, 'srcdir' => $srcdir.quote,
491
- 'arch_hdrdir' => $arch_hdrdir.quote,
492
- 'top_srcdir' => $top_srcdir.quote)
493
- RbConfig::expand("$(CC) #$INCFLAGS #$CPPFLAGS #$CFLAGS #$ARCH_FLAG #{opt} -c #{CONFTEST_C}",
494
- conf)
495
- end
496
-
497
- def cpp_command(outfile, opt="")
498
- conf = RbConfig::CONFIG.merge('hdrdir' => $hdrdir.quote, 'srcdir' => $srcdir.quote,
499
- 'arch_hdrdir' => $arch_hdrdir.quote,
500
- 'top_srcdir' => $top_srcdir.quote)
501
- if $universal and (arch_flag = conf['ARCH_FLAG']) and !arch_flag.empty?
502
- conf['ARCH_FLAG'] = arch_flag.gsub(/(?:\G|\s)-arch\s+\S+/, '')
503
- end
504
- RbConfig::expand("$(CPP) #$INCFLAGS #$CPPFLAGS #$CFLAGS #{opt} #{CONFTEST_C} #{outfile}",
505
- conf)
506
- end
507
-
508
- def libpathflag(libpath=$DEFLIBPATH|$LIBPATH)
509
- libpath.map{|x|
510
- case x
511
- when "$(topdir)", /\A\./
512
- LIBPATHFLAG
513
- else
514
- LIBPATHFLAG+RPATHFLAG
515
- end % x.quote
516
- }.join
517
- end
518
-
519
- def with_werror(opt, opts = nil)
520
- if opts
521
- if opts[:werror] and config_string("WERRORFLAG") {|flag| opt = opt ? "#{opt} #{flag}" : flag}
522
- (opts = opts.dup).delete(:werror)
523
- end
524
- yield(opt, opts)
525
- else
526
- yield(opt)
527
- end
528
- end
529
-
530
- def try_link0(src, opt="", *opts, &b) # :nodoc:
531
- cmd = link_command("", opt)
532
- if $universal
533
- require 'tmpdir'
534
- Dir.mktmpdir("mkmf_", oldtmpdir = ENV["TMPDIR"]) do |tmpdir|
535
- begin
536
- ENV["TMPDIR"] = tmpdir
537
- try_do(src, cmd, *opts, &b)
538
- ensure
539
- ENV["TMPDIR"] = oldtmpdir
540
- end
541
- end
542
- else
543
- try_do(src, cmd, *opts, &b)
544
- end and File.executable?(CONFTEST+$EXEEXT)
545
- end
546
-
547
- # Returns whether or not the +src+ can be compiled as a C source and linked
548
- # with its depending libraries successfully. +opt+ is passed to the linker
549
- # as options. Note that +$CFLAGS+ and +$LDFLAGS+ are also passed to the
550
- # linker.
551
- #
552
- # If a block given, it is called with the source before compilation. You can
553
- # modify the source in the block.
554
- #
555
- # [+src+] a String which contains a C source
556
- # [+opt+] a String which contains linker options
557
- def try_link(src, opt="", *opts, &b)
558
- try_link0(src, opt, *opts, &b)
559
- ensure
560
- MakeMakefile.rm_f "#{CONFTEST}*", "c0x32*"
561
- end
562
-
563
- # Returns whether or not the +src+ can be compiled as a C source. +opt+ is
564
- # passed to the C compiler as options. Note that +$CFLAGS+ is also passed to
565
- # the compiler.
566
- #
567
- # If a block given, it is called with the source before compilation. You can
568
- # modify the source in the block.
569
- #
570
- # [+src+] a String which contains a C source
571
- # [+opt+] a String which contains compiler options
572
- def try_compile(src, opt="", *opts, &b)
573
- with_werror(opt, *opts) {|_opt, *| try_do(src, cc_command(_opt), *opts, &b)} and
574
- File.file?("#{CONFTEST}.#{$OBJEXT}")
575
- ensure
576
- MakeMakefile.rm_f "#{CONFTEST}*"
577
- end
578
-
579
- # Returns whether or not the +src+ can be preprocessed with the C
580
- # preprocessor. +opt+ is passed to the preprocessor as options. Note that
581
- # +$CFLAGS+ is also passed to the preprocessor.
582
- #
583
- # If a block given, it is called with the source before preprocessing. You
584
- # can modify the source in the block.
585
- #
586
- # [+src+] a String which contains a C source
587
- # [+opt+] a String which contains preprocessor options
588
- def try_cpp(src, opt="", *opts, &b)
589
- try_do(src, cpp_command(CPPOUTFILE, opt), *opts, &b) and
590
- File.file?("#{CONFTEST}.i")
591
- ensure
592
- MakeMakefile.rm_f "#{CONFTEST}*"
593
- end
594
-
595
- alias_method :try_header, (config_string('try_header') || :try_cpp)
596
-
597
- def cpp_include(header)
598
- if header
599
- header = [header] unless header.kind_of? Array
600
- header.map {|h| String === h ? "#include <#{h}>\n" : h}.join
601
- else
602
- ""
603
- end
604
- end
605
-
606
- def with_cppflags(flags)
607
- cppflags = $CPPFLAGS
608
- $CPPFLAGS = flags
609
- ret = yield
610
- ensure
611
- $CPPFLAGS = cppflags unless ret
612
- end
613
-
614
- def try_cppflags(flags, opts = {})
615
- try_header(MAIN_DOES_NOTHING, flags, {:werror => true}.update(opts))
616
- end
617
-
618
- def append_cppflags(flags, *opts)
619
- Array(flags).each do |flag|
620
- if checking_for("whether #{flag} is accepted as CPPFLAGS") {
621
- try_cppflags(flag, *opts)
622
- }
623
- $CPPFLAGS << " " << flag
624
- end
625
- end
626
- end
627
-
628
- def with_cflags(flags)
629
- cflags = $CFLAGS
630
- $CFLAGS = flags
631
- ret = yield
632
- ensure
633
- $CFLAGS = cflags unless ret
634
- end
635
-
636
- def try_cflags(flags, opts = {})
637
- try_compile(MAIN_DOES_NOTHING, flags, {:werror => true}.update(opts))
638
- end
639
-
640
- def append_cflags(flags, *opts)
641
- Array(flags).each do |flag|
642
- if checking_for("whether #{flag} is accepted as CFLAGS") {
643
- try_cflags(flag, *opts)
644
- }
645
- $CFLAGS << " " << flag
646
- end
647
- end
648
- end
649
-
650
- def with_ldflags(flags)
651
- ldflags = $LDFLAGS
652
- $LDFLAGS = flags
653
- ret = yield
654
- ensure
655
- $LDFLAGS = ldflags unless ret
656
- end
657
-
658
- def try_ldflags(flags, opts = {})
659
- try_link(MAIN_DOES_NOTHING, flags, {:werror => true}.update(opts))
660
- end
661
-
662
- def append_ldflags(flags, *opts)
663
- Array(flags).each do |flag|
664
- if checking_for("whether #{flag} is accepted as LDFLAGS") {
665
- try_ldflags(flag, *opts)
666
- }
667
- $LDFLAGS << " " << flag
668
- end
669
- end
670
- end
671
-
672
- def try_static_assert(expr, headers = nil, opt = "", &b)
673
- headers = cpp_include(headers)
674
- try_compile(<<SRC, opt, &b)
675
- #{headers}
676
- /*top*/
677
- int conftest_const[(#{expr}) ? 1 : -1];
678
- SRC
679
- end
680
-
681
- def try_constant(const, headers = nil, opt = "", &b)
682
- includes = cpp_include(headers)
683
- neg = try_static_assert("#{const} < 0", headers, opt)
684
- if CROSS_COMPILING
685
- if neg
686
- const = "-(#{const})"
687
- elsif try_static_assert("#{const} > 0", headers, opt)
688
- # positive constant
689
- elsif try_static_assert("#{const} == 0", headers, opt)
690
- return 0
691
- else
692
- # not a constant
693
- return nil
694
- end
695
- upper = 1
696
- until try_static_assert("#{const} <= #{upper}", headers, opt)
697
- lower = upper
698
- upper <<= 1
699
- end
700
- return nil unless lower
701
- while upper > lower + 1
702
- mid = (upper + lower) / 2
703
- if try_static_assert("#{const} > #{mid}", headers, opt)
704
- lower = mid
705
- else
706
- upper = mid
707
- end
708
- end
709
- upper = -upper if neg
710
- return upper
711
- else
712
- src = %{#{includes}
713
- #include <stdio.h>
714
- /*top*/
715
- typedef#{neg ? '' : ' unsigned'}
716
- #ifdef PRI_LL_PREFIX
717
- #define PRI_CONFTEST_PREFIX PRI_LL_PREFIX
718
- LONG_LONG
719
- #else
720
- #define PRI_CONFTEST_PREFIX "l"
721
- long
722
- #endif
723
- conftest_type;
724
- conftest_type conftest_const = (conftest_type)(#{const});
725
- int main() {printf("%"PRI_CONFTEST_PREFIX"#{neg ? 'd' : 'u'}\\n", conftest_const); return 0;}
726
- }
727
- begin
728
- if try_link0(src, opt, &b)
729
- xpopen("./#{CONFTEST}") do |f|
730
- return Integer(f.gets)
731
- end
732
- end
733
- ensure
734
- MakeMakefile.rm_f "#{CONFTEST}*"
735
- end
736
- end
737
- nil
738
- end
739
-
740
- # You should use +have_func+ rather than +try_func+.
741
- #
742
- # [+func+] a String which contains a symbol name
743
- # [+libs+] a String which contains library names.
744
- # [+headers+] a String or an Array of strings which contains names of header
745
- # files.
746
- def try_func(func, libs, headers = nil, opt = "", &b)
747
- headers = cpp_include(headers)
748
- case func
749
- when /^&/
750
- decltype = proc {|x|"const volatile void *#{x}"}
751
- when /\)$/
752
- call = func
753
- else
754
- call = "#{func}()"
755
- decltype = proc {|x| "void ((*#{x})())"}
756
- end
757
- if opt and !opt.empty?
758
- [[:to_str], [:join, " "], [:to_s]].each do |meth, *args|
759
- if opt.respond_to?(meth)
760
- break opt = opt.send(meth, *args)
761
- end
762
- end
763
- opt = "#{opt} #{libs}"
764
- else
765
- opt = libs
766
- end
767
- decltype && try_link(<<"SRC", opt, &b) or
768
- #{headers}
769
- /*top*/
770
- extern int t(void);
771
- #{MAIN_DOES_NOTHING 't'}
772
- int t(void) { #{decltype["volatile p"]}; p = (#{decltype[]})#{func}; return !p; }
773
- SRC
774
- call && try_link(<<"SRC", opt, &b)
775
- #{headers}
776
- /*top*/
777
- extern int t(void);
778
- #{MAIN_DOES_NOTHING 't'}
779
- #{"extern void #{call};" if decltype}
780
- int t(void) { #{call}; return 0; }
781
- SRC
782
- end
783
-
784
- # You should use +have_var+ rather than +try_var+.
785
- def try_var(var, headers = nil, opt = "", &b)
786
- headers = cpp_include(headers)
787
- try_compile(<<"SRC", opt, &b)
788
- #{headers}
789
- /*top*/
790
- extern int t(void);
791
- #{MAIN_DOES_NOTHING 't'}
792
- int t(void) { const volatile void *volatile p; p = &(&#{var})[0]; return !p; }
793
- SRC
794
- end
795
-
796
- # Returns whether or not the +src+ can be preprocessed with the C
797
- # preprocessor and matches with +pat+.
798
- #
799
- # If a block given, it is called with the source before compilation. You can
800
- # modify the source in the block.
801
- #
802
- # [+pat+] a Regexp or a String
803
- # [+src+] a String which contains a C source
804
- # [+opt+] a String which contains preprocessor options
805
- #
806
- # NOTE: When pat is a Regexp the matching will be checked in process,
807
- # otherwise egrep(1) will be invoked to check it.
808
- def egrep_cpp(pat, src, opt = "", &b)
809
- src = create_tmpsrc(src, &b)
810
- xpopen(cpp_command('', opt)) do |f|
811
- if Regexp === pat
812
- puts(" ruby -ne 'print if #{pat.inspect}'")
813
- f.grep(pat) {|l|
814
- puts "#{f.lineno}: #{l}"
815
- return true
816
- }
817
- false
818
- else
819
- puts(" egrep '#{pat}'")
820
- begin
821
- stdin = $stdin.dup
822
- $stdin.reopen(f)
823
- system("egrep", pat)
824
- ensure
825
- $stdin.reopen(stdin)
826
- end
827
- end
828
- end
829
- ensure
830
- MakeMakefile.rm_f "#{CONFTEST}*"
831
- log_src(src)
832
- end
833
-
834
- # This is used internally by the have_macro? method.
835
- def macro_defined?(macro, src, opt = "", &b)
836
- src = src.sub(/[^\n]\z/, "\\&\n")
837
- try_compile(src + <<"SRC", opt, &b)
838
- /*top*/
839
- #ifndef #{macro}
840
- # error
841
- |:/ === #{macro} undefined === /:|
842
- #endif
843
- SRC
844
- end
845
-
846
- # Returns whether or not:
847
- # * the +src+ can be compiled as a C source,
848
- # * the result object can be linked with its depending libraries
849
- # successfully,
850
- # * the linked file can be invoked as an executable
851
- # * and the executable exits successfully
852
- #
853
- # +opt+ is passed to the linker as options. Note that +$CFLAGS+ and
854
- # +$LDFLAGS+ are also passed to the linker.
855
- #
856
- # If a block given, it is called with the source before compilation. You can
857
- # modify the source in the block.
858
- #
859
- # [+src+] a String which contains a C source
860
- # [+opt+] a String which contains linker options
861
- #
862
- # Returns true when the executable exits successfully, false when it fails,
863
- # or nil when preprocessing, compilation or link fails.
864
- def try_run(src, opt = "", &b)
865
- raise "cannot run test program while cross compiling" if CROSS_COMPILING
866
- if try_link0(src, opt, &b)
867
- xsystem("./#{CONFTEST}")
868
- else
869
- nil
870
- end
871
- ensure
872
- MakeMakefile.rm_f "#{CONFTEST}*"
873
- end
874
-
875
- def install_files(mfile, ifiles, map = nil, srcprefix = nil)
876
- ifiles or return
877
- ifiles.empty? and return
878
- srcprefix ||= "$(srcdir)/#{srcprefix}".chomp('/')
879
- RbConfig::expand(srcdir = srcprefix.dup)
880
- dirs = []
881
- path = Hash.new {|h, i| h[i] = dirs.push([i])[-1]}
882
- ifiles.each do |files, dir, prefix|
883
- dir = map_dir(dir, map)
884
- prefix &&= %r|\A#{Regexp.quote(prefix)}/?|
885
- if /\A\.\// =~ files
886
- # install files which are in current working directory.
887
- files = files[2..-1]
888
- len = nil
889
- else
890
- # install files which are under the $(srcdir).
891
- files = File.join(srcdir, files)
892
- len = srcdir.size
893
- end
894
- f = nil
895
- Dir.glob(files) do |fx|
896
- f = fx
897
- f[0..len] = "" if len
898
- case File.basename(f)
899
- when *$NONINSTALLFILES
900
- next
901
- end
902
- d = File.dirname(f)
903
- d.sub!(prefix, "") if prefix
904
- d = (d.empty? || d == ".") ? dir : File.join(dir, d)
905
- f = File.join(srcprefix, f) if len
906
- path[d] << f
907
- end
908
- unless len or f
909
- d = File.dirname(files)
910
- d.sub!(prefix, "") if prefix
911
- d = (d.empty? || d == ".") ? dir : File.join(dir, d)
912
- path[d] << files
913
- end
914
- end
915
- dirs
916
- end
917
-
918
- def install_rb(mfile, dest, srcdir = nil)
919
- install_files(mfile, [["lib/**/*.rb", dest, "lib"]], nil, srcdir)
920
- end
921
-
922
- def append_library(libs, lib) # :no-doc:
923
- format(LIBARG, lib) + " " + libs
924
- end
925
-
926
- def message(*s)
927
- unless Logging.quiet and not $VERBOSE
928
- printf(*s)
929
- $stdout.flush
930
- end
931
- end
932
-
933
- # This emits a string to stdout that allows users to see the results of the
934
- # various have* and find* methods as they are tested.
935
- #
936
- # Internal use only.
937
- #
938
- def checking_for(m, fmt = nil)
939
- f = caller[0][/in `([^<].*)'$/, 1] and f << ": " #` for vim #'
940
- m = "checking #{/\Acheck/ =~ f ? '' : 'for '}#{m}... "
941
- message "%s", m
942
- a = r = nil
943
- Logging::postpone do
944
- r = yield
945
- a = (fmt ? "#{fmt % r}" : r ? "yes" : "no") << "\n"
946
- "#{f}#{m}-------------------- #{a}\n"
947
- end
948
- message(a)
949
- Logging::message "--------------------\n\n"
950
- r
951
- end
952
-
953
- def checking_message(target, place = nil, opt = nil)
954
- [["in", place], ["with", opt]].inject("#{target}") do |msg, (pre, noun)|
955
- if noun
956
- [[:to_str], [:join, ","], [:to_s]].each do |meth, *args|
957
- if noun.respond_to?(meth)
958
- break noun = noun.send(meth, *args)
959
- end
960
- end
961
- msg << " #{pre} #{noun}" unless noun.empty?
962
- end
963
- msg
964
- end
965
- end
966
-
967
- # :startdoc:
968
-
969
- # Returns whether or not +macro+ is defined either in the common header
970
- # files or within any +headers+ you provide.
971
- #
972
- # Any options you pass to +opt+ are passed along to the compiler.
973
- #
974
- def have_macro(macro, headers = nil, opt = "", &b)
975
- checking_for checking_message(macro, headers, opt) do
976
- macro_defined?(macro, cpp_include(headers), opt, &b)
977
- end
978
- end
979
-
980
- # Returns whether or not the given entry point +func+ can be found within
981
- # +lib+. If +func+ is +nil+, the <code>main()</code> entry point is used by
982
- # default. If found, it adds the library to list of libraries to be used
983
- # when linking your extension.
984
- #
985
- # If +headers+ are provided, it will include those header files as the
986
- # header files it looks in when searching for +func+.
987
- #
988
- # The real name of the library to be linked can be altered by
989
- # <code>--with-FOOlib</code> configuration option.
990
- #
991
- def have_library(lib, func = nil, headers = nil, opt = "", &b)
992
- func = "main" if !func or func.empty?
993
- lib = with_config(lib+'lib', lib)
994
- checking_for checking_message(func.funcall_style, LIBARG%lib, opt) do
995
- if COMMON_LIBS.include?(lib)
996
- true
997
- else
998
- libs = append_library($libs, lib)
999
- if try_func(func, libs, headers, opt, &b)
1000
- $libs = libs
1001
- true
1002
- else
1003
- false
1004
- end
1005
- end
1006
- end
1007
- end
1008
-
1009
- # Returns whether or not the entry point +func+ can be found within the
1010
- # library +lib+ in one of the +paths+ specified, where +paths+ is an array
1011
- # of strings. If +func+ is +nil+ , then the <code>main()</code> function is
1012
- # used as the entry point.
1013
- #
1014
- # If +lib+ is found, then the path it was found on is added to the list of
1015
- # library paths searched and linked against.
1016
- #
1017
- def find_library(lib, func, *paths, &b)
1018
- func = "main" if !func or func.empty?
1019
- lib = with_config(lib+'lib', lib)
1020
- paths = paths.collect {|path| path.split(File::PATH_SEPARATOR)}.flatten
1021
- checking_for checking_message(func.funcall_style, LIBARG%lib) do
1022
- libpath = $LIBPATH
1023
- libs = append_library($libs, lib)
1024
- begin
1025
- until r = try_func(func, libs, &b) or paths.empty?
1026
- $LIBPATH = libpath | [paths.shift]
1027
- end
1028
- if r
1029
- $libs = libs
1030
- libpath = nil
1031
- end
1032
- ensure
1033
- $LIBPATH = libpath if libpath
1034
- end
1035
- r
1036
- end
1037
- end
1038
-
1039
- # Returns whether or not the function +func+ can be found in the common
1040
- # header files, or within any +headers+ that you provide. If found, a macro
1041
- # is passed as a preprocessor constant to the compiler using the function
1042
- # name, in uppercase, prepended with +HAVE_+.
1043
- #
1044
- # To check functions in an additional library, you need to check that
1045
- # library first using <code>have_library()</code>. The +func+ shall be
1046
- # either mere function name or function name with arguments.
1047
- #
1048
- # For example, if <code>have_func('foo')</code> returned +true+, then the
1049
- # +HAVE_FOO+ preprocessor macro would be passed to the compiler.
1050
- #
1051
- def have_func(func, headers = nil, opt = "", &b)
1052
- checking_for checking_message(func.funcall_style, headers, opt) do
1053
- if try_func(func, $libs, headers, opt, &b)
1054
- $defs << "-DHAVE_#{func.sans_arguments.tr_cpp}"
1055
- true
1056
- else
1057
- false
1058
- end
1059
- end
1060
- end
1061
-
1062
- # Returns whether or not the variable +var+ can be found in the common
1063
- # header files, or within any +headers+ that you provide. If found, a macro
1064
- # is passed as a preprocessor constant to the compiler using the variable
1065
- # name, in uppercase, prepended with +HAVE_+.
1066
- #
1067
- # To check variables in an additional library, you need to check that
1068
- # library first using <code>have_library()</code>.
1069
- #
1070
- # For example, if <code>have_var('foo')</code> returned true, then the
1071
- # +HAVE_FOO+ preprocessor macro would be passed to the compiler.
1072
- #
1073
- def have_var(var, headers = nil, opt = "", &b)
1074
- checking_for checking_message(var, headers, opt) do
1075
- if try_var(var, headers, opt, &b)
1076
- $defs.push(format("-DHAVE_%s", var.tr_cpp))
1077
- true
1078
- else
1079
- false
1080
- end
1081
- end
1082
- end
1083
-
1084
- # Returns whether or not the given +header+ file can be found on your system.
1085
- # If found, a macro is passed as a preprocessor constant to the compiler
1086
- # using the header file name, in uppercase, prepended with +HAVE_+.
1087
- #
1088
- # For example, if <code>have_header('foo.h')</code> returned true, then the
1089
- # +HAVE_FOO_H+ preprocessor macro would be passed to the compiler.
1090
- #
1091
- def have_header(header, preheaders = nil, opt = "", &b)
1092
- checking_for header do
1093
- if try_header(cpp_include(preheaders)+cpp_include(header), opt, &b)
1094
- $defs.push(format("-DHAVE_%s", header.tr_cpp))
1095
- true
1096
- else
1097
- false
1098
- end
1099
- end
1100
- end
1101
-
1102
- # Returns whether or not the given +framework+ can be found on your system.
1103
- # If found, a macro is passed as a preprocessor constant to the compiler
1104
- # using the framework name, in uppercase, prepended with +HAVE_FRAMEWORK_+.
1105
- #
1106
- # For example, if <code>have_framework('Ruby')</code> returned true, then
1107
- # the +HAVE_FRAMEWORK_RUBY+ preprocessor macro would be passed to the
1108
- # compiler.
1109
- #
1110
- # If +fw+ is a pair of the framework name and its header file name
1111
- # that header file is checked, instead of the normally used header
1112
- # file which is named same as the framework.
1113
- def have_framework(fw, &b)
1114
- if Array === fw
1115
- fw, header = *fw
1116
- else
1117
- header = "#{fw}.h"
1118
- end
1119
- checking_for fw do
1120
- src = cpp_include("#{fw}/#{header}") << "\n" "int main(void){return 0;}"
1121
- opt = " -framework #{fw}"
1122
- if try_link(src, opt, &b) or (objc = try_link(src, "-ObjC#{opt}", &b))
1123
- $defs.push(format("-DHAVE_FRAMEWORK_%s", fw.tr_cpp))
1124
- # TODO: non-worse way than this hack, to get rid of separating
1125
- # option and its argument.
1126
- $LDFLAGS << " -ObjC" if objc and /(\A|\s)-ObjC(\s|\z)/ !~ $LDFLAGS
1127
- $LIBS << opt
1128
- true
1129
- else
1130
- false
1131
- end
1132
- end
1133
- end
1134
-
1135
- # Instructs mkmf to search for the given +header+ in any of the +paths+
1136
- # provided, and returns whether or not it was found in those paths.
1137
- #
1138
- # If the header is found then the path it was found on is added to the list
1139
- # of included directories that are sent to the compiler (via the
1140
- # <code>-I</code> switch).
1141
- #
1142
- def find_header(header, *paths)
1143
- message = checking_message(header, paths)
1144
- header = cpp_include(header)
1145
- checking_for message do
1146
- if try_header(header)
1147
- true
1148
- else
1149
- found = false
1150
- paths.each do |dir|
1151
- opt = "-I#{dir}".quote
1152
- if try_header(header, opt)
1153
- $INCFLAGS << " " << opt
1154
- found = true
1155
- break
1156
- end
1157
- end
1158
- found
1159
- end
1160
- end
1161
- end
1162
-
1163
- # Returns whether or not the struct of type +type+ contains +member+. If
1164
- # it does not, or the struct type can't be found, then false is returned.
1165
- # You may optionally specify additional +headers+ in which to look for the
1166
- # struct (in addition to the common header files).
1167
- #
1168
- # If found, a macro is passed as a preprocessor constant to the compiler
1169
- # using the type name and the member name, in uppercase, prepended with
1170
- # +HAVE_+.
1171
- #
1172
- # For example, if <code>have_struct_member('struct foo', 'bar')</code>
1173
- # returned true, then the +HAVE_STRUCT_FOO_BAR+ preprocessor macro would be
1174
- # passed to the compiler.
1175
- #
1176
- # +HAVE_ST_BAR+ is also defined for backward compatibility.
1177
- #
1178
- def have_struct_member(type, member, headers = nil, opt = "", &b)
1179
- checking_for checking_message("#{type}.#{member}", headers) do
1180
- if try_compile(<<"SRC", opt, &b)
1181
- #{cpp_include(headers)}
1182
- /*top*/
1183
- int s = (char *)&((#{type}*)0)->#{member} - (char *)0;
1184
- #{MAIN_DOES_NOTHING}
1185
- SRC
1186
- $defs.push(format("-DHAVE_%s_%s", type.tr_cpp, member.tr_cpp))
1187
- $defs.push(format("-DHAVE_ST_%s", member.tr_cpp)) # backward compatibility
1188
- true
1189
- else
1190
- false
1191
- end
1192
- end
1193
- end
1194
-
1195
- # Returns whether or not the static type +type+ is defined.
1196
- #
1197
- # See also +have_type+
1198
- #
1199
- def try_type(type, headers = nil, opt = "", &b)
1200
- if try_compile(<<"SRC", opt, &b)
1201
- #{cpp_include(headers)}
1202
- /*top*/
1203
- typedef #{type} conftest_type;
1204
- int conftestval[sizeof(conftest_type)?1:-1];
1205
- SRC
1206
- $defs.push(format("-DHAVE_TYPE_%s", type.tr_cpp))
1207
- true
1208
- else
1209
- false
1210
- end
1211
- end
1212
-
1213
- # Returns whether or not the static type +type+ is defined. You may
1214
- # optionally pass additional +headers+ to check against in addition to the
1215
- # common header files.
1216
- #
1217
- # You may also pass additional flags to +opt+ which are then passed along to
1218
- # the compiler.
1219
- #
1220
- # If found, a macro is passed as a preprocessor constant to the compiler
1221
- # using the type name, in uppercase, prepended with +HAVE_TYPE_+.
1222
- #
1223
- # For example, if <code>have_type('foo')</code> returned true, then the
1224
- # +HAVE_TYPE_FOO+ preprocessor macro would be passed to the compiler.
1225
- #
1226
- def have_type(type, headers = nil, opt = "", &b)
1227
- checking_for checking_message(type, headers, opt) do
1228
- try_type(type, headers, opt, &b)
1229
- end
1230
- end
1231
-
1232
- # Returns where the static type +type+ is defined.
1233
- #
1234
- # You may also pass additional flags to +opt+ which are then passed along to
1235
- # the compiler.
1236
- #
1237
- # See also +have_type+.
1238
- #
1239
- def find_type(type, opt, *headers, &b)
1240
- opt ||= ""
1241
- fmt = "not found"
1242
- def fmt.%(x)
1243
- x ? x.respond_to?(:join) ? x.join(",") : x : self
1244
- end
1245
- checking_for checking_message(type, nil, opt), fmt do
1246
- headers.find do |h|
1247
- try_type(type, h, opt, &b)
1248
- end
1249
- end
1250
- end
1251
-
1252
- # Returns whether or not the constant +const+ is defined.
1253
- #
1254
- # See also +have_const+
1255
- #
1256
- def try_const(const, headers = nil, opt = "", &b)
1257
- const, type = *const
1258
- if try_compile(<<"SRC", opt, &b)
1259
- #{cpp_include(headers)}
1260
- /*top*/
1261
- typedef #{type || 'int'} conftest_type;
1262
- conftest_type conftestval = #{type ? '' : '(int)'}#{const};
1263
- SRC
1264
- $defs.push(format("-DHAVE_CONST_%s", const.tr_cpp))
1265
- true
1266
- else
1267
- false
1268
- end
1269
- end
1270
-
1271
- # Returns whether or not the constant +const+ is defined. You may
1272
- # optionally pass the +type+ of +const+ as <code>[const, type]</code>,
1273
- # such as:
1274
- #
1275
- # have_const(%w[PTHREAD_MUTEX_INITIALIZER pthread_mutex_t], "pthread.h")
1276
- #
1277
- # You may also pass additional +headers+ to check against in addition to the
1278
- # common header files, and additional flags to +opt+ which are then passed
1279
- # along to the compiler.
1280
- #
1281
- # If found, a macro is passed as a preprocessor constant to the compiler
1282
- # using the type name, in uppercase, prepended with +HAVE_CONST_+.
1283
- #
1284
- # For example, if <code>have_const('foo')</code> returned true, then the
1285
- # +HAVE_CONST_FOO+ preprocessor macro would be passed to the compiler.
1286
- #
1287
- def have_const(const, headers = nil, opt = "", &b)
1288
- checking_for checking_message([*const].compact.join(' '), headers, opt) do
1289
- try_const(const, headers, opt, &b)
1290
- end
1291
- end
1292
-
1293
- # :stopdoc:
1294
- STRING_OR_FAILED_FORMAT = "%s"
1295
- def STRING_OR_FAILED_FORMAT.%(x) # :nodoc:
1296
- x ? super : "failed"
1297
- end
1298
-
1299
- def typedef_expr(type, headers)
1300
- typename, member = type.split('.', 2)
1301
- prelude = cpp_include(headers).split(/$/)
1302
- prelude << "typedef #{typename} rbcv_typedef_;\n"
1303
- return "rbcv_typedef_", member, prelude
1304
- end
1305
-
1306
- def try_signedness(type, member, headers = nil, opts = nil)
1307
- raise ArgumentError, "don't know how to tell signedness of members" if member
1308
- if try_static_assert("(#{type})-1 < 0", headers, opts)
1309
- return -1
1310
- elsif try_static_assert("(#{type})-1 > 0", headers, opts)
1311
- return +1
1312
- end
1313
- end
1314
-
1315
- # :startdoc:
1316
-
1317
- # Returns the size of the given +type+. You may optionally specify
1318
- # additional +headers+ to search in for the +type+.
1319
- #
1320
- # If found, a macro is passed as a preprocessor constant to the compiler
1321
- # using the type name, in uppercase, prepended with +SIZEOF_+, followed by
1322
- # the type name, followed by <code>=X</code> where "X" is the actual size.
1323
- #
1324
- # For example, if <code>check_sizeof('mystruct')</code> returned 12, then
1325
- # the <code>SIZEOF_MYSTRUCT=12</code> preprocessor macro would be passed to
1326
- # the compiler.
1327
- #
1328
- def check_sizeof(type, headers = nil, opts = "", &b)
1329
- typedef, member, prelude = typedef_expr(type, headers)
1330
- prelude << "#{typedef} *rbcv_ptr_;\n"
1331
- prelude = [prelude]
1332
- expr = "sizeof((*rbcv_ptr_)#{"." << member if member})"
1333
- fmt = STRING_OR_FAILED_FORMAT
1334
- checking_for checking_message("size of #{type}", headers), fmt do
1335
- if size = try_constant(expr, prelude, opts, &b)
1336
- $defs.push(format("-DSIZEOF_%s=%s", type.tr_cpp, size))
1337
- size
1338
- end
1339
- end
1340
- end
1341
-
1342
- # Returns the signedness of the given +type+. You may optionally specify
1343
- # additional +headers+ to search in for the +type+.
1344
- #
1345
- # If the +type+ is found and is a numeric type, a macro is passed as a
1346
- # preprocessor constant to the compiler using the +type+ name, in uppercase,
1347
- # prepended with +SIGNEDNESS_OF_+, followed by the +type+ name, followed by
1348
- # <code>=X</code> where "X" is positive integer if the +type+ is unsigned
1349
- # and a negative integer if the +type+ is signed.
1350
- #
1351
- # For example, if +size_t+ is defined as unsigned, then
1352
- # <code>check_signedness('size_t')</code> would return +1 and the
1353
- # <code>SIGNEDNESS_OF_SIZE_T=+1</code> preprocessor macro would be passed to
1354
- # the compiler. The <code>SIGNEDNESS_OF_INT=-1</code> macro would be set
1355
- # for <code>check_signedness('int')</code>
1356
- #
1357
- def check_signedness(type, headers = nil, opts = nil, &b)
1358
- typedef, member, prelude = typedef_expr(type, headers)
1359
- signed = nil
1360
- checking_for("signedness of #{type}", STRING_OR_FAILED_FORMAT) do
1361
- signed = try_signedness(typedef, member, [prelude], opts, &b) or next nil
1362
- $defs.push("-DSIGNEDNESS_OF_%s=%+d" % [type.tr_cpp, signed])
1363
- signed < 0 ? "signed" : "unsigned"
1364
- end
1365
- signed
1366
- end
1367
-
1368
- # Returns the convertible integer type of the given +type+. You may
1369
- # optionally specify additional +headers+ to search in for the +type+.
1370
- # _convertible_ means actually the same type, or typedef'd from the same
1371
- # type.
1372
- #
1373
- # If the +type+ is a integer type and the _convertible_ type is found,
1374
- # the following macros are passed as preprocessor constants to the compiler
1375
- # using the +type+ name, in uppercase.
1376
- #
1377
- # * +TYPEOF_+, followed by the +type+ name, followed by <code>=X</code>
1378
- # where "X" is the found _convertible_ type name.
1379
- # * +TYP2NUM+ and +NUM2TYP+,
1380
- # where +TYP+ is the +type+ name in uppercase with replacing an +_t+
1381
- # suffix with "T", followed by <code>=X</code> where "X" is the macro name
1382
- # to convert +type+ to an Integer object, and vice versa.
1383
- #
1384
- # For example, if +foobar_t+ is defined as unsigned long, then
1385
- # <code>convertible_int("foobar_t")</code> would return "unsigned long", and
1386
- # define these macros:
1387
- #
1388
- # #define TYPEOF_FOOBAR_T unsigned long
1389
- # #define FOOBART2NUM ULONG2NUM
1390
- # #define NUM2FOOBART NUM2ULONG
1391
- #
1392
- def convertible_int(type, headers = nil, opts = nil, &b)
1393
- type, macname = *type
1394
- checking_for("convertible type of #{type}", STRING_OR_FAILED_FORMAT) do
1395
- if UNIVERSAL_INTS.include?(type)
1396
- type
1397
- else
1398
- typedef, member, prelude = typedef_expr(type, headers, &b)
1399
- if member
1400
- prelude << "static rbcv_typedef_ rbcv_var;"
1401
- compat = UNIVERSAL_INTS.find {|t|
1402
- try_static_assert("sizeof(rbcv_var.#{member}) == sizeof(#{t})", [prelude], opts, &b)
1403
- }
1404
- else
1405
- next unless signed = try_signedness(typedef, member, [prelude])
1406
- u = "unsigned " if signed > 0
1407
- prelude << "extern rbcv_typedef_ foo();"
1408
- compat = UNIVERSAL_INTS.find {|t|
1409
- try_compile([prelude, "extern #{u}#{t} foo();"].join("\n"), opts, :werror=>true, &b)
1410
- }
1411
- end
1412
- if compat
1413
- macname ||= type.sub(/_(?=t\z)/, '').tr_cpp
1414
- conv = (compat == "long long" ? "LL" : compat.upcase)
1415
- compat = "#{u}#{compat}"
1416
- typename = type.tr_cpp
1417
- $defs.push(format("-DSIZEOF_%s=SIZEOF_%s", typename, compat.tr_cpp))
1418
- $defs.push(format("-DTYPEOF_%s=%s", typename, compat.quote))
1419
- $defs.push(format("-DPRI_%s_PREFIX=PRI_%s_PREFIX", macname, conv))
1420
- conv = (u ? "U" : "") + conv
1421
- $defs.push(format("-D%s2NUM=%s2NUM", macname, conv))
1422
- $defs.push(format("-DNUM2%s=NUM2%s", macname, conv))
1423
- compat
1424
- end
1425
- end
1426
- end
1427
- end
1428
- # :stopdoc:
1429
-
1430
- # Used internally by the what_type? method to determine if +type+ is a scalar
1431
- # pointer.
1432
- def scalar_ptr_type?(type, member = nil, headers = nil, &b)
1433
- try_compile(<<"SRC", &b) # pointer
1434
- #{cpp_include(headers)}
1435
- /*top*/
1436
- volatile #{type} conftestval;
1437
- extern int t(void);
1438
- #{MAIN_DOES_NOTHING 't'}
1439
- int t(void) {return (int)(1-*(conftestval#{member ? ".#{member}" : ""}));}
1440
- SRC
1441
- end
1442
-
1443
- # Used internally by the what_type? method to determine if +type+ is a scalar
1444
- # pointer.
1445
- def scalar_type?(type, member = nil, headers = nil, &b)
1446
- try_compile(<<"SRC", &b) # pointer
1447
- #{cpp_include(headers)}
1448
- /*top*/
1449
- volatile #{type} conftestval;
1450
- extern int t(void);
1451
- #{MAIN_DOES_NOTHING 't'}
1452
- int t(void) {return (int)(1-(conftestval#{member ? ".#{member}" : ""}));}
1453
- SRC
1454
- end
1455
-
1456
- # Used internally by the what_type? method to check if the _typeof_ GCC
1457
- # extension is available.
1458
- def have_typeof?
1459
- return $typeof if defined?($typeof)
1460
- $typeof = %w[__typeof__ typeof].find do |t|
1461
- try_compile(<<SRC)
1462
- int rbcv_foo;
1463
- #{t}(rbcv_foo) rbcv_bar;
1464
- SRC
1465
- end
1466
- end
1467
-
1468
- def what_type?(type, member = nil, headers = nil, &b)
1469
- m = "#{type}"
1470
- var = val = "*rbcv_var_"
1471
- func = "rbcv_func_(void)"
1472
- if member
1473
- m << "." << member
1474
- else
1475
- type, member = type.split('.', 2)
1476
- end
1477
- if member
1478
- val = "(#{var}).#{member}"
1479
- end
1480
- prelude = [cpp_include(headers).split(/^/)]
1481
- prelude << ["typedef #{type} rbcv_typedef_;\n",
1482
- "extern rbcv_typedef_ *#{func};\n",
1483
- "rbcv_typedef_ #{var};\n",
1484
- ]
1485
- type = "rbcv_typedef_"
1486
- fmt = member && !(typeof = have_typeof?) ? "seems %s" : "%s"
1487
- if typeof
1488
- var = "*rbcv_member_"
1489
- func = "rbcv_mem_func_(void)"
1490
- member = nil
1491
- type = "rbcv_mem_typedef_"
1492
- prelude[-1] << "typedef #{typeof}(#{val}) #{type};\n"
1493
- prelude[-1] << "extern #{type} *#{func};\n"
1494
- prelude[-1] << "#{type} #{var};\n"
1495
- val = var
1496
- end
1497
- def fmt.%(x)
1498
- x ? super : "unknown"
1499
- end
1500
- checking_for checking_message(m, headers), fmt do
1501
- if scalar_ptr_type?(type, member, prelude, &b)
1502
- if try_static_assert("sizeof(*#{var}) == 1", prelude)
1503
- return "string"
1504
- end
1505
- ptr = "*"
1506
- elsif scalar_type?(type, member, prelude, &b)
1507
- unless member and !typeof or try_static_assert("(#{type})-1 < 0", prelude)
1508
- unsigned = "unsigned"
1509
- end
1510
- ptr = ""
1511
- else
1512
- next
1513
- end
1514
- type = UNIVERSAL_INTS.find do |t|
1515
- pre = prelude
1516
- unless member
1517
- pre += [["#{unsigned} #{t} #{ptr}#{var};\n",
1518
- "extern #{unsigned} #{t} #{ptr}*#{func};\n"]]
1519
- end
1520
- try_static_assert("sizeof(#{ptr}#{val}) == sizeof(#{unsigned} #{t})", pre)
1521
- end
1522
- type or next
1523
- [unsigned, type, ptr].join(" ").strip
1524
- end
1525
- end
1526
-
1527
- # This method is used internally by the find_executable method.
1528
- #
1529
- # Internal use only.
1530
- #
1531
- def find_executable0(bin, path = nil)
1532
- executable_file = proc do |name|
1533
- begin
1534
- stat = File.stat(name)
1535
- rescue SystemCallError
1536
- else
1537
- next name if stat.file? and stat.executable?
1538
- end
1539
- end
1540
-
1541
- exts = config_string('EXECUTABLE_EXTS') {|s| s.split} || config_string('EXEEXT') {|s| [s]}
1542
- if File.expand_path(bin) == bin
1543
- return bin if executable_file.call(bin)
1544
- if exts
1545
- exts.each {|ext| executable_file.call(file = bin + ext) and return file}
1546
- end
1547
- return nil
1548
- end
1549
- if path ||= ENV['PATH']
1550
- path = path.split(File::PATH_SEPARATOR)
1551
- else
1552
- path = %w[/usr/local/bin /usr/ucb /usr/bin /bin]
1553
- end
1554
- file = nil
1555
- path.each do |dir|
1556
- return file if executable_file.call(file = File.join(dir, bin))
1557
- if exts
1558
- exts.each {|ext| executable_file.call(ext = file + ext) and return ext}
1559
- end
1560
- end
1561
- nil
1562
- end
1563
-
1564
- # :startdoc:
1565
-
1566
- # Searches for the executable +bin+ on +path+. The default path is your
1567
- # +PATH+ environment variable. If that isn't defined, it will resort to
1568
- # searching /usr/local/bin, /usr/ucb, /usr/bin and /bin.
1569
- #
1570
- # If found, it will return the full path, including the executable name, of
1571
- # where it was found.
1572
- #
1573
- # Note that this method does not actually affect the generated Makefile.
1574
- #
1575
- def find_executable(bin, path = nil)
1576
- checking_for checking_message(bin, path) do
1577
- find_executable0(bin, path)
1578
- end
1579
- end
1580
-
1581
- # :stopdoc:
1582
-
1583
- def arg_config(config, default=nil, &block)
1584
- $arg_config << [config, default]
1585
- defaults = []
1586
- if default
1587
- defaults << default
1588
- elsif !block
1589
- defaults << nil
1590
- end
1591
- $configure_args.fetch(config.tr('_', '-'), *defaults, &block)
1592
- end
1593
-
1594
- # :startdoc:
1595
-
1596
- # Tests for the presence of a <tt>--with-</tt>_config_ or
1597
- # <tt>--without-</tt>_config_ option. Returns +true+ if the with option is
1598
- # given, +false+ if the without option is given, and the default value
1599
- # otherwise.
1600
- #
1601
- # This can be useful for adding custom definitions, such as debug
1602
- # information.
1603
- #
1604
- # Example:
1605
- #
1606
- # if with_config("debug")
1607
- # $defs.push("-DOSSL_DEBUG") unless $defs.include? "-DOSSL_DEBUG"
1608
- # end
1609
- #
1610
- def with_config(config, default=nil)
1611
- config = config.sub(/^--with[-_]/, '')
1612
- val = arg_config("--with-"+config) do
1613
- if arg_config("--without-"+config)
1614
- false
1615
- elsif block_given?
1616
- yield(config, default)
1617
- else
1618
- break default
1619
- end
1620
- end
1621
- case val
1622
- when "yes"
1623
- true
1624
- when "no"
1625
- false
1626
- else
1627
- val
1628
- end
1629
- end
1630
-
1631
- # Tests for the presence of an <tt>--enable-</tt>_config_ or
1632
- # <tt>--disable-</tt>_config_ option. Returns +true+ if the enable option is
1633
- # given, +false+ if the disable option is given, and the default value
1634
- # otherwise.
1635
- #
1636
- # This can be useful for adding custom definitions, such as debug
1637
- # information.
1638
- #
1639
- # Example:
1640
- #
1641
- # if enable_config("debug")
1642
- # $defs.push("-DOSSL_DEBUG") unless $defs.include? "-DOSSL_DEBUG"
1643
- # end
1644
- #
1645
- def enable_config(config, default=nil)
1646
- if arg_config("--enable-"+config)
1647
- true
1648
- elsif arg_config("--disable-"+config)
1649
- false
1650
- elsif block_given?
1651
- yield(config, default)
1652
- else
1653
- return default
1654
- end
1655
- end
1656
-
1657
- # Generates a header file consisting of the various macro definitions
1658
- # generated by other methods such as have_func and have_header. These are
1659
- # then wrapped in a custom <code>#ifndef</code> based on the +header+ file
1660
- # name, which defaults to "extconf.h".
1661
- #
1662
- # For example:
1663
- #
1664
- # # extconf.rb
1665
- # require 'mkmf'
1666
- # have_func('realpath')
1667
- # have_header('sys/utime.h')
1668
- # create_header
1669
- # create_makefile('foo')
1670
- #
1671
- # The above script would generate the following extconf.h file:
1672
- #
1673
- # #ifndef EXTCONF_H
1674
- # #define EXTCONF_H
1675
- # #define HAVE_REALPATH 1
1676
- # #define HAVE_SYS_UTIME_H 1
1677
- # #endif
1678
- #
1679
- # Given that the create_header method generates a file based on definitions
1680
- # set earlier in your extconf.rb file, you will probably want to make this
1681
- # one of the last methods you call in your script.
1682
- #
1683
- def create_header(header = "extconf.h")
1684
- message "creating %s\n", header
1685
- sym = header.tr_cpp
1686
- hdr = ["#ifndef #{sym}\n#define #{sym}\n"]
1687
- for line in $defs
1688
- case line
1689
- when /^-D([^=]+)(?:=(.*))?/
1690
- hdr << "#define #$1 #{$2 ? Shellwords.shellwords($2)[0].gsub(/(?=\t+)/, "\\\n") : 1}\n"
1691
- when /^-U(.*)/
1692
- hdr << "#undef #$1\n"
1693
- end
1694
- end
1695
- hdr << "#endif\n"
1696
- hdr = hdr.join("")
1697
- log_src(hdr, "#{header} is")
1698
- unless (IO.read(header) == hdr rescue false)
1699
- open(header, "wb") do |hfile|
1700
- hfile.write(hdr)
1701
- end
1702
- end
1703
- $extconf_h = header
1704
- end
1705
-
1706
- # call-seq:
1707
- # dir_config(target)
1708
- # dir_config(target, prefix)
1709
- # dir_config(target, idefault, ldefault)
1710
- #
1711
- # Sets a +target+ name that the user can then use to configure
1712
- # various "with" options with on the command line by using that
1713
- # name. For example, if the target is set to "foo", then the user
1714
- # could use the <code>--with-foo-dir=prefix</code>,
1715
- # <code>--with-foo-include=dir</code> and
1716
- # <code>--with-foo-lib=dir</code> command line options to tell where
1717
- # to search for header/library files.
1718
- #
1719
- # You may pass along additional parameters to specify default
1720
- # values. If one is given it is taken as default +prefix+, and if
1721
- # two are given they are taken as "include" and "lib" defaults in
1722
- # that order.
1723
- #
1724
- # In any case, the return value will be an array of determined
1725
- # "include" and "lib" directories, either of which can be nil if no
1726
- # corresponding command line option is given when no default value
1727
- # is specified.
1728
- #
1729
- # Note that dir_config only adds to the list of places to search for
1730
- # libraries and include files. It does not link the libraries into your
1731
- # application.
1732
- #
1733
- def dir_config(target, idefault=nil, ldefault=nil)
1734
- if dir = with_config(target + "-dir", (idefault unless ldefault))
1735
- defaults = Array === dir ? dir : dir.split(File::PATH_SEPARATOR)
1736
- idefault = ldefault = nil
1737
- end
1738
-
1739
- idir = with_config(target + "-include", idefault)
1740
- $arg_config.last[1] ||= "${#{target}-dir}/include"
1741
- ldir = with_config(target + "-lib", ldefault)
1742
- $arg_config.last[1] ||= "${#{target}-dir}/#{_libdir_basename}"
1743
-
1744
- idirs = idir ? Array === idir ? idir.dup : idir.split(File::PATH_SEPARATOR) : []
1745
- if defaults
1746
- idirs.concat(defaults.collect {|d| d + "/include"})
1747
- idir = ([idir] + idirs).compact.join(File::PATH_SEPARATOR)
1748
- end
1749
- unless idirs.empty?
1750
- idirs.collect! {|d| "-I" + d}
1751
- idirs -= Shellwords.shellwords($CPPFLAGS)
1752
- unless idirs.empty?
1753
- $CPPFLAGS = (idirs.quote << $CPPFLAGS).join(" ")
1754
- end
1755
- end
1756
-
1757
- ldirs = ldir ? Array === ldir ? ldir.dup : ldir.split(File::PATH_SEPARATOR) : []
1758
- if defaults
1759
- ldirs.concat(defaults.collect {|d| "#{d}/#{_libdir_basename}"})
1760
- ldir = ([ldir] + ldirs).compact.join(File::PATH_SEPARATOR)
1761
- end
1762
- $LIBPATH = ldirs | $LIBPATH
1763
-
1764
- [idir, ldir]
1765
- end
1766
-
1767
- # Returns compile/link information about an installed library in a
1768
- # tuple of <code>[cflags, ldflags, libs]</code>, by using the
1769
- # command found first in the following commands:
1770
- #
1771
- # 1. If <code>--with-{pkg}-config={command}</code> is given via
1772
- # command line option: <code>{command} {option}</code>
1773
- #
1774
- # 2. <code>{pkg}-config {option}</code>
1775
- #
1776
- # 3. <code>pkg-config {option} {pkg}</code>
1777
- #
1778
- # Where {option} is, for instance, <code>--cflags</code>.
1779
- #
1780
- # The values obtained are appended to +$CFLAGS+, +$LDFLAGS+ and
1781
- # +$libs+.
1782
- #
1783
- # If an <code>option</code> argument is given, the config command is
1784
- # invoked with the option and a stripped output string is returned
1785
- # without modifying any of the global values mentioned above.
1786
- def pkg_config(pkg, option=nil)
1787
- if pkgconfig = with_config("#{pkg}-config") and find_executable0(pkgconfig)
1788
- # iff package specific config command is given
1789
- elsif ($PKGCONFIG ||=
1790
- (pkgconfig = with_config("pkg-config", ("pkg-config" unless CROSS_COMPILING))) &&
1791
- find_executable0(pkgconfig) && pkgconfig) and
1792
- xsystem("#{$PKGCONFIG} --exists #{pkg}")
1793
- # default to pkg-config command
1794
- pkgconfig = $PKGCONFIG
1795
- get = proc {|opt|
1796
- opt = xpopen("#{$PKGCONFIG} --#{opt} #{pkg}", err:[:child, :out], &:read)
1797
- Logging.open {puts opt.each_line.map{|s|"=> #{s.inspect}"}}
1798
- opt.strip if $?.success?
1799
- }
1800
- elsif find_executable0(pkgconfig = "#{pkg}-config")
1801
- # default to package specific config command, as a last resort.
1802
- else
1803
- pkgconfig = nil
1804
- end
1805
- if pkgconfig
1806
- get ||= proc {|opt|
1807
- opt = xpopen("#{pkgconfig} --#{opt}", err:[:child, :out], &:read)
1808
- Logging.open {puts opt.each_line.map{|s|"=> #{s.inspect}"}}
1809
- opt.strip if $?.success?
1810
- }
1811
- end
1812
- orig_ldflags = $LDFLAGS
1813
- if get and option
1814
- get[option]
1815
- elsif get and try_ldflags(ldflags = get['libs'])
1816
- if incflags = get['cflags-only-I']
1817
- $INCFLAGS << " " << incflags
1818
- cflags = get['cflags-only-other']
1819
- else
1820
- cflags = get['cflags']
1821
- end
1822
- libs = get['libs-only-l']
1823
- if cflags
1824
- $CFLAGS += " " << cflags
1825
- $CXXFLAGS += " " << cflags
1826
- end
1827
- if libs
1828
- ldflags = (Shellwords.shellwords(ldflags) - Shellwords.shellwords(libs)).quote.join(" ")
1829
- else
1830
- libs, ldflags = Shellwords.shellwords(ldflags).partition {|s| s =~ /-l([^ ]+)/ }.map {|l|l.quote.join(" ")}
1831
- end
1832
- $libs += " " << libs
1833
-
1834
- $LDFLAGS = [orig_ldflags, ldflags].join(' ')
1835
- Logging::message "package configuration for %s\n", pkg
1836
- Logging::message "cflags: %s\nldflags: %s\nlibs: %s\n\n",
1837
- cflags, ldflags, libs
1838
- [cflags, ldflags, libs]
1839
- else
1840
- Logging::message "package configuration for %s is not found\n", pkg
1841
- nil
1842
- end
1843
- end
1844
-
1845
- # :stopdoc:
1846
-
1847
- def with_destdir(dir)
1848
- dir = dir.sub($dest_prefix_pattern, '')
1849
- /\A\$[\(\{]/ =~ dir ? dir : "$(DESTDIR)"+dir
1850
- end
1851
-
1852
- # Converts forward slashes to backslashes. Aimed at MS Windows.
1853
- #
1854
- # Internal use only.
1855
- #
1856
- def winsep(s)
1857
- s.tr('/', '\\')
1858
- end
1859
-
1860
- # Converts native path to format acceptable in Makefile
1861
- #
1862
- # Internal use only.
1863
- #
1864
- if !CROSS_COMPILING
1865
- case CONFIG['build_os']
1866
- when 'mingw32'
1867
- def mkintpath(path)
1868
- # mingw uses make from msys and it needs special care
1869
- # converts from C:\some\path to /C/some/path
1870
- path = path.dup
1871
- path.tr!('\\', '/')
1872
- path.sub!(/\A([A-Za-z]):(?=\/)/, '/\1')
1873
- path
1874
- end
1875
- when 'cygwin'
1876
- if CONFIG['target_os'] != 'cygwin'
1877
- def mkintpath(path)
1878
- IO.popen(["cygpath", "-u", path], &:read).chomp
1879
- end
1880
- end
1881
- end
1882
- end
1883
- unless method_defined?(:mkintpath)
1884
- def mkintpath(path)
1885
- path
1886
- end
1887
- end
1888
-
1889
- def configuration(srcdir)
1890
- mk = []
1891
- vpath = $VPATH.dup
1892
- CONFIG["hdrdir"] ||= $hdrdir
1893
- mk << %{
1894
- SHELL = /bin/sh
1895
-
1896
- # V=0 quiet, V=1 verbose. other values don't work.
1897
- V = 0
1898
- Q1 = $(V:1=)
1899
- Q = $(Q1:0=@)
1900
- ECHO1 = $(V:1=@#{CONFIG['NULLCMD']})
1901
- ECHO = $(ECHO1:0=@echo)
1902
- NULLCMD = #{CONFIG['NULLCMD']}
1903
-
1904
- #### Start of system configuration section. ####
1905
- #{"top_srcdir = " + $top_srcdir.sub(%r"\A#{Regexp.quote($topdir)}/", "$(topdir)/") if $extmk}
1906
- srcdir = #{srcdir.gsub(/\$\((srcdir)\)|\$\{(srcdir)\}/) {mkintpath(CONFIG[$1||$2]).unspace}}
1907
- topdir = #{mkintpath(topdir = $extmk ? CONFIG["topdir"] : $topdir).unspace}
1908
- hdrdir = #{(hdrdir = CONFIG["hdrdir"]) == topdir ? "$(topdir)" : mkintpath(hdrdir).unspace}
1909
- arch_hdrdir = #{$arch_hdrdir.quote}
1910
- PATH_SEPARATOR = #{CONFIG['PATH_SEPARATOR']}
1911
- VPATH = #{vpath.join(CONFIG['PATH_SEPARATOR'])}
1912
- }
1913
- if $extmk
1914
- mk << "RUBYLIB =\n""RUBYOPT = -\n"
1915
- end
1916
- prefix = mkintpath(CONFIG["prefix"])
1917
- if destdir = prefix[$dest_prefix_pattern, 1]
1918
- mk << "\nDESTDIR = #{destdir}\n"
1919
- prefix = prefix[destdir.size..-1]
1920
- end
1921
- mk << "prefix = #{with_destdir(prefix).unspace}\n"
1922
- CONFIG.each do |key, var|
1923
- mk << "#{key} = #{with_destdir(mkintpath(var)).unspace}\n" if /.prefix$/ =~ key
1924
- end
1925
- CONFIG.each do |key, var|
1926
- next if /^abs_/ =~ key
1927
- next if /^(?:src|top(?:_src)?|build|hdr)dir$/ =~ key
1928
- next unless /dir$/ =~ key
1929
- mk << "#{key} = #{with_destdir(var)}\n"
1930
- end
1931
- if !$extmk and !$configure_args.has_key?('--ruby') and
1932
- sep = config_string('BUILD_FILE_SEPARATOR')
1933
- sep = ":/=#{sep}"
1934
- else
1935
- sep = ""
1936
- end
1937
- possible_command = (proc {|s| s if /top_srcdir/ !~ s} unless $extmk)
1938
- extconf_h = $extconf_h ? "-DRUBY_EXTCONF_H=\\\"$(RUBY_EXTCONF_H)\\\" " : $defs.join(" ") << " "
1939
- headers = %w[
1940
- $(hdrdir)/ruby.h
1941
- $(hdrdir)/ruby/ruby.h
1942
- $(hdrdir)/ruby/defines.h
1943
- $(hdrdir)/ruby/missing.h
1944
- $(hdrdir)/ruby/intern.h
1945
- $(hdrdir)/ruby/st.h
1946
- $(hdrdir)/ruby/subst.h
1947
- ]
1948
- if RULE_SUBST
1949
- headers.each {|h| h.sub!(/.*/, &RULE_SUBST.method(:%))}
1950
- end
1951
- headers << $config_h
1952
- headers << '$(RUBY_EXTCONF_H)' if $extconf_h
1953
- mk << %{
1954
-
1955
- CC = #{CONFIG['CC']}
1956
- CXX = #{CONFIG['CXX']}
1957
- LIBRUBY = #{CONFIG['LIBRUBY']}
1958
- LIBRUBY_A = #{CONFIG['LIBRUBY_A']}
1959
- LIBRUBYARG_SHARED = #$LIBRUBYARG_SHARED
1960
- LIBRUBYARG_STATIC = #$LIBRUBYARG_STATIC
1961
- empty =
1962
- OUTFLAG = #{OUTFLAG}$(empty)
1963
- COUTFLAG = #{COUTFLAG}$(empty)
1964
-
1965
- RUBY_EXTCONF_H = #{$extconf_h}
1966
- cflags = #{CONFIG['cflags']}
1967
- cxxflags = #{CONFIG['cxxflags']}
1968
- optflags = #{CONFIG['optflags']}
1969
- debugflags = #{CONFIG['debugflags']}
1970
- warnflags = #{$warnflags}
1971
- CCDLFLAGS = #{$static ? '' : CONFIG['CCDLFLAGS']}
1972
- CFLAGS = $(CCDLFLAGS) #$CFLAGS $(ARCH_FLAG)
1973
- INCFLAGS = -I. #$INCFLAGS
1974
- DEFS = #{CONFIG['DEFS']}
1975
- CPPFLAGS = #{extconf_h}#{$CPPFLAGS}
1976
- CXXFLAGS = $(CCDLFLAGS) #$CXXFLAGS $(ARCH_FLAG)
1977
- ldflags = #{$LDFLAGS}
1978
- dldflags = #{$DLDFLAGS} #{CONFIG['EXTDLDFLAGS']}
1979
- ARCH_FLAG = #{$ARCH_FLAG}
1980
- DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG)
1981
- LDSHARED = #{CONFIG['LDSHARED']}
1982
- LDSHAREDXX = #{config_string('LDSHAREDXX') || '$(LDSHARED)'}
1983
- AR = #{CONFIG['AR']}
1984
- EXEEXT = #{CONFIG['EXEEXT']}
1985
-
1986
- }
1987
- CONFIG.each do |key, val|
1988
- mk << "#{key} = #{val}\n" if /^RUBY.*NAME/ =~ key
1989
- end
1990
- mk << %{
1991
- arch = #{CONFIG['arch']}
1992
- sitearch = #{CONFIG['sitearch']}
1993
- ruby_version = #{RbConfig::CONFIG['ruby_version']}
1994
- ruby = #{$ruby.sub(%r[\A#{Regexp.quote(RbConfig::CONFIG['bindir'])}(?=/|\z)]) {'$(bindir)'}}
1995
- RUBY = $(ruby#{sep})
1996
- ruby_headers = #{headers.join(' ')}
1997
-
1998
- RM = #{config_string('RM', &possible_command) || '$(RUBY) -run -e rm -- -f'}
1999
- RM_RF = #{'$(RUBY) -run -e rm -- -rf'}
2000
- RMDIRS = #{config_string('RMDIRS', &possible_command) || '$(RUBY) -run -e rmdir -- -p'}
2001
- MAKEDIRS = #{config_string('MAKEDIRS', &possible_command) || '@$(RUBY) -run -e mkdir -- -p'}
2002
- INSTALL = #{config_string('INSTALL', &possible_command) || '@$(RUBY) -run -e install -- -vp'}
2003
- INSTALL_PROG = #{config_string('INSTALL_PROG') || '$(INSTALL) -m 0755'}
2004
- INSTALL_DATA = #{config_string('INSTALL_DATA') || '$(INSTALL) -m 0644'}
2005
- COPY = #{config_string('CP', &possible_command) || '@$(RUBY) -run -e cp -- -v'}
2006
- TOUCH = exit >
2007
-
2008
- #### End of system configuration section. ####
2009
-
2010
- preload = #{defined?($preload) && $preload ? $preload.join(' ') : ''}
2011
- }
2012
- if $nmake == ?b
2013
- mk.each do |x|
2014
- x.gsub!(/^(MAKEDIRS|INSTALL_(?:PROG|DATA))+\s*=.*\n/) do
2015
- "!ifndef " + $1 + "\n" +
2016
- $& +
2017
- "!endif\n"
2018
- end
2019
- end
2020
- end
2021
- mk
2022
- end
2023
-
2024
- def timestamp_file(name, target_prefix = nil)
2025
- if target_prefix
2026
- pat = []
2027
- install_dirs.each do |n, d|
2028
- pat << n if /\$\(target_prefix\)\z/ =~ d
2029
- end
2030
- name = name.gsub(/\$\((#{pat.join("|")})\)/) {$&+target_prefix}
2031
- end
2032
- name = name.gsub(/(\$[({]|[})])|(\/+)|[^-.\w]+/) {$1 ? "" : $2 ? ".-." : "_"}
2033
- "$(TIMESTAMP_DIR)/.#{name}.time"
2034
- end
2035
- # :startdoc:
2036
-
2037
- # creates a stub Makefile.
2038
- #
2039
- def dummy_makefile(srcdir)
2040
- configuration(srcdir) << <<RULES << CLEANINGS
2041
- CLEANFILES = #{$cleanfiles.join(' ')}
2042
- DISTCLEANFILES = #{$distcleanfiles.join(' ')}
2043
-
2044
- all install static install-so install-rb: Makefile
2045
- .PHONY: all install static install-so install-rb
2046
- .PHONY: clean clean-so clean-static clean-rb
2047
-
2048
- RULES
2049
- end
2050
-
2051
- def each_compile_rules # :nodoc:
2052
- vpath_splat = /\$\(\*VPATH\*\)/
2053
- COMPILE_RULES.each do |rule|
2054
- if vpath_splat =~ rule
2055
- $VPATH.each do |path|
2056
- yield rule.sub(vpath_splat) {path}
2057
- end
2058
- else
2059
- yield rule
2060
- end
2061
- end
2062
- end
2063
-
2064
- # Processes the data contents of the "depend" file. Each line of this file
2065
- # is expected to be a file name.
2066
- #
2067
- # Returns the output of findings, in Makefile format.
2068
- #
2069
- def depend_rules(depend)
2070
- suffixes = []
2071
- depout = []
2072
- cont = implicit = nil
2073
- impconv = proc do
2074
- each_compile_rules {|rule| depout << (rule % implicit[0]) << implicit[1]}
2075
- implicit = nil
2076
- end
2077
- ruleconv = proc do |line|
2078
- if implicit
2079
- if /\A\t/ =~ line
2080
- implicit[1] << line
2081
- next
2082
- else
2083
- impconv[]
2084
- end
2085
- end
2086
- if m = /\A\.(\w+)\.(\w+)(?:\s*:)/.match(line)
2087
- suffixes << m[1] << m[2]
2088
- implicit = [[m[1], m[2]], [m.post_match]]
2089
- next
2090
- elsif RULE_SUBST and /\A(?!\s*\w+\s*=)[$\w][^#]*:/ =~ line
2091
- line.sub!(/\s*\#.*$/, '')
2092
- comment = $&
2093
- line.gsub!(%r"(\s)(?!\.)([^$(){}+=:\s\\,]+)(?=\s|\z)") {$1 + RULE_SUBST % $2}
2094
- line = line.chomp + comment + "\n" if comment
2095
- end
2096
- depout << line
2097
- end
2098
- depend.each_line do |line|
2099
- line.gsub!(/\.o\b/, ".#{$OBJEXT}")
2100
- line.gsub!(/\{\$\(VPATH\)\}/, "") unless $nmake
2101
- line.gsub!(/\$\((?:hdr|top)dir\)\/config.h/, $config_h)
2102
- line.gsub!(%r"\$\(hdrdir\)/(?!ruby(?![^:;/\s]))(?=[-\w]+\.h)", '\&ruby/')
2103
- if $nmake && /\A\s*\$\(RM|COPY\)/ =~ line
2104
- line.gsub!(%r"[-\w\./]{2,}"){$&.tr("/", "\\")}
2105
- line.gsub!(/(\$\((?!RM|COPY)[^:)]+)(?=\))/, '\1:/=\\')
2106
- end
2107
- if /(?:^|[^\\])(?:\\\\)*\\$/ =~ line
2108
- (cont ||= []) << line
2109
- next
2110
- elsif cont
2111
- line = (cont << line).join
2112
- cont = nil
2113
- end
2114
- ruleconv.call(line)
2115
- end
2116
- if cont
2117
- ruleconv.call(cont.join)
2118
- elsif implicit
2119
- impconv.call
2120
- end
2121
- unless suffixes.empty?
2122
- depout.unshift(".SUFFIXES: ." + suffixes.uniq.join(" .") + "\n\n")
2123
- end
2124
- depout.unshift("$(OBJS): $(RUBY_EXTCONF_H)\n\n") if $extconf_h
2125
- depout.flatten!
2126
- depout
2127
- end
2128
-
2129
- # Generates the Makefile for your extension, passing along any options and
2130
- # preprocessor constants that you may have generated through other methods.
2131
- #
2132
- # The +target+ name should correspond the name of the global function name
2133
- # defined within your C extension, minus the +Init_+. For example, if your
2134
- # C extension is defined as +Init_foo+, then your target would simply be
2135
- # "foo".
2136
- #
2137
- # If any "/" characters are present in the target name, only the last name
2138
- # is interpreted as the target name, and the rest are considered toplevel
2139
- # directory names, and the generated Makefile will be altered accordingly to
2140
- # follow that directory structure.
2141
- #
2142
- # For example, if you pass "test/foo" as a target name, your extension will
2143
- # be installed under the "test" directory. This means that in order to
2144
- # load the file within a Ruby program later, that directory structure will
2145
- # have to be followed, e.g. <code>require 'test/foo'</code>.
2146
- #
2147
- # The +srcprefix+ should be used when your source files are not in the same
2148
- # directory as your build script. This will not only eliminate the need for
2149
- # you to manually copy the source files into the same directory as your
2150
- # build script, but it also sets the proper +target_prefix+ in the generated
2151
- # Makefile.
2152
- #
2153
- # Setting the +target_prefix+ will, in turn, install the generated binary in
2154
- # a directory under your <code>RbConfig::CONFIG['sitearchdir']</code> that
2155
- # mimics your local filesystem when you run <code>make install</code>.
2156
- #
2157
- # For example, given the following file tree:
2158
- #
2159
- # ext/
2160
- # extconf.rb
2161
- # test/
2162
- # foo.c
2163
- #
2164
- # And given the following code:
2165
- #
2166
- # create_makefile('test/foo', 'test')
2167
- #
2168
- # That will set the +target_prefix+ in the generated Makefile to "test".
2169
- # That, in turn, will create the following file tree when installed via the
2170
- # <code>make install</code> command:
2171
- #
2172
- # /path/to/ruby/sitearchdir/test/foo.so
2173
- #
2174
- # It is recommended that you use this approach to generate your makefiles,
2175
- # instead of copying files around manually, because some third party
2176
- # libraries may depend on the +target_prefix+ being set properly.
2177
- #
2178
- # The +srcprefix+ argument can be used to override the default source
2179
- # directory, i.e. the current directory. It is included as part of the
2180
- # +VPATH+ and added to the list of +INCFLAGS+.
2181
- #
2182
- def create_makefile(target, srcprefix = nil)
2183
- $target = target
2184
- libpath = $DEFLIBPATH|$LIBPATH
2185
- message "creating Makefile\n"
2186
- MakeMakefile.rm_f "#{CONFTEST}*"
2187
- if CONFIG["DLEXT"] == $OBJEXT
2188
- for lib in libs = $libs.split
2189
- lib.sub!(/-l(.*)/, %%"lib\\1.#{$LIBEXT}"%)
2190
- end
2191
- $defs.push(format("-DEXTLIB='%s'", libs.join(",")))
2192
- end
2193
-
2194
- if target.include?('/')
2195
- target_prefix, target = File.split(target)
2196
- target_prefix[0,0] = '/'
2197
- else
2198
- target_prefix = ""
2199
- end
2200
-
2201
- srcprefix ||= "$(srcdir)/#{srcprefix}".chomp('/')
2202
- RbConfig.expand(srcdir = srcprefix.dup)
2203
-
2204
- ext = ".#{$OBJEXT}"
2205
- orig_srcs = Dir[File.join(srcdir, "*.{#{SRC_EXT.join(%q{,})}}")]
2206
- if not $objs
2207
- srcs = $srcs || orig_srcs
2208
- objs = srcs.inject(Hash.new {[]}) {|h, f| h[File.basename(f, ".*") << ext] <<= f; h}
2209
- $objs = objs.keys
2210
- unless objs.delete_if {|b, f| f.size == 1}.empty?
2211
- dups = objs.sort.map {|b, f|
2212
- "#{b[/.*\./]}{#{f.collect {|n| n[/([^.]+)\z/]}.join(',')}}"
2213
- }
2214
- abort "source files duplication - #{dups.join(", ")}"
2215
- end
2216
- else
2217
- $objs.collect! {|o| File.basename(o, ".*") << ext} unless $OBJEXT == "o"
2218
- srcs = $srcs || $objs.collect {|o| o.chomp(ext) << ".c"}
2219
- end
2220
- $srcs = srcs
2221
-
2222
- hdrs = Dir[File.join(srcdir, "*.{#{HDR_EXT.join(%q{,})}}")]
2223
-
2224
- if File.exist?('build.zig')
2225
- $objs = ['build.zig']
2226
- else
2227
- target = nil if $objs.empty?
2228
- end
2229
-
2230
- if target and EXPORT_PREFIX
2231
- if File.exist?(File.join(srcdir, target + '.def'))
2232
- deffile = "$(srcdir)/$(TARGET).def"
2233
- unless EXPORT_PREFIX.empty?
2234
- makedef = %{$(RUBY) -pe "$$_.sub!(/^(?=\\w)/,'#{EXPORT_PREFIX}') unless 1../^EXPORTS$/i" #{deffile}}
2235
- end
2236
- else
2237
- makedef = %{(echo EXPORTS && echo $(TARGET_ENTRY))}
2238
- end
2239
- if makedef
2240
- $cleanfiles << '$(DEFFILE)'
2241
- origdef = deffile
2242
- deffile = "$(TARGET)-$(arch).def"
2243
- end
2244
- end
2245
- origdef ||= ''
2246
-
2247
- if $extout and $INSTALLFILES
2248
- $cleanfiles.concat($INSTALLFILES.collect {|files, dir|File.join(dir, files.sub(/\A\.\//, ''))})
2249
- $distcleandirs.concat($INSTALLFILES.collect {|files, dir| dir})
2250
- end
2251
-
2252
- if $extmk and $static
2253
- $defs << "-DRUBY_EXPORT=1"
2254
- end
2255
-
2256
- if $extmk and not $extconf_h
2257
- create_header
2258
- end
2259
-
2260
- libpath = libpathflag(libpath)
2261
-
2262
- dllib = target ? "$(TARGET).#{CONFIG['DLEXT']}" : ""
2263
- staticlib = target ? "$(TARGET).#$LIBEXT" : ""
2264
- mfile = open("Makefile", "wb")
2265
- conf = configuration(srcprefix)
2266
- conf = yield(conf) if block_given?
2267
- mfile.puts(conf)
2268
- mfile.print "
2269
- libpath = #{($DEFLIBPATH|$LIBPATH).join(" ")}
2270
- LIBPATH = #{libpath}
2271
- DEFFILE = #{deffile}
2272
-
2273
- CLEANFILES = #{$cleanfiles.join(' ')}
2274
- DISTCLEANFILES = #{$distcleanfiles.join(' ')}
2275
- DISTCLEANDIRS = #{$distcleandirs.join(' ')}
2276
-
2277
- extout = #{$extout && $extout.quote}
2278
- extout_prefix = #{$extout_prefix}
2279
- target_prefix = #{target_prefix}
2280
- LOCAL_LIBS = #{$LOCAL_LIBS}
2281
- LIBS = #{$LIBRUBYARG} #{$libs} #{$LIBS}
2282
- ORIG_SRCS = #{orig_srcs.collect(&File.method(:basename)).join(' ')}
2283
- SRCS = $(ORIG_SRCS) #{(srcs - orig_srcs).collect(&File.method(:basename)).join(' ')}
2284
- OBJS = #{$objs.join(" ")}
2285
- HDRS = #{hdrs.map{|h| '$(srcdir)/' + File.basename(h)}.join(' ')}
2286
- TARGET = #{target}
2287
- TARGET_NAME = #{target && target[/\A\w+/]}
2288
- TARGET_ENTRY = #{EXPORT_PREFIX || ''}Init_$(TARGET_NAME)
2289
- DLLIB = #{dllib}
2290
- EXTSTATIC = #{$static || ""}
2291
- STATIC_LIB = #{staticlib unless $static.nil?}
2292
- #{!$extout && defined?($installed_list) ? "INSTALLED_LIST = #{$installed_list}\n" : ""}
2293
- TIMESTAMP_DIR = #{$extout ? '$(extout)/.timestamp' : '.'}
2294
- " #"
2295
- # TODO: fixme
2296
- install_dirs.each {|d| mfile.print("%-14s= %s\n" % d) if /^[[:upper:]]/ =~ d[0]}
2297
- n = ($extout ? '$(RUBYARCHDIR)/' : '') + '$(TARGET)'
2298
- mfile.print "
2299
- TARGET_SO = #{($extout ? '$(RUBYARCHDIR)/' : '')}$(DLLIB)
2300
- CLEANLIBS = #{n}.#{CONFIG['DLEXT']} #{config_string('cleanlibs') {|t| t.gsub(/\$\*/) {n}}}
2301
- CLEANOBJS = *.#{$OBJEXT} #{config_string('cleanobjs') {|t| t.gsub(/\$\*/, "$(TARGET)#{deffile ? '-$(arch)': ''}")} if target} *.bak
2302
-
2303
- all: #{$extout ? "install" : target ? "$(DLLIB)" : "Makefile"}
2304
- static: #{$extmk && !$static ? "all" : "$(STATIC_LIB)#{!$extmk ? " install-rb" : ""}"}
2305
- .PHONY: all install static install-so install-rb
2306
- .PHONY: clean clean-so clean-static clean-rb
2307
- "
2308
- mfile.print CLEANINGS
2309
- fsep = config_string('BUILD_FILE_SEPARATOR') {|s| s unless s == "/"}
2310
- if fsep
2311
- sep = ":/=#{fsep}"
2312
- fseprepl = proc {|s|
2313
- s = s.gsub("/", fsep)
2314
- s = s.gsub(/(\$\(\w+)(\))/) {$1+sep+$2}
2315
- s.gsub(/(\$\{\w+)(\})/) {$1+sep+$2}
2316
- }
2317
- rsep = ":#{fsep}=/"
2318
- else
2319
- fseprepl = proc {|s| s}
2320
- sep = ""
2321
- rsep = ""
2322
- end
2323
- dirs = []
2324
- mfile.print "install: install-so install-rb\n\n"
2325
- sodir = (dir = "$(RUBYARCHDIR)").dup
2326
- mfile.print("install-so: ")
2327
- if target
2328
- f = "$(DLLIB)"
2329
- dest = "#{dir}/#{f}"
2330
- stamp = timestamp_file(dir, target_prefix)
2331
- if $extout
2332
- mfile.puts dest
2333
- mfile.print "clean-so::\n"
2334
- mfile.print "\t-$(Q)$(RM) #{fseprepl[dest]} #{fseprepl[stamp]}\n"
2335
- mfile.print "\t-$(Q)$(RMDIRS) #{fseprepl[dir]}#{$ignore_error}\n"
2336
- else
2337
- mfile.print "#{f} #{stamp}\n"
2338
- mfile.print "\t$(INSTALL_PROG) #{fseprepl[f]} #{dir}\n"
2339
- if defined?($installed_list)
2340
- mfile.print "\t@echo #{dir}/#{File.basename(f)}>>$(INSTALLED_LIST)\n"
2341
- end
2342
- end
2343
- mfile.print "clean-static::\n"
2344
- mfile.print "\t-$(Q)$(RM) $(STATIC_LIB)\n"
2345
- else
2346
- mfile.puts "Makefile"
2347
- end
2348
- mfile.print("install-rb: pre-install-rb install-rb-default\n")
2349
- mfile.print("install-rb-default: pre-install-rb-default\n")
2350
- mfile.print("pre-install-rb: Makefile\n")
2351
- mfile.print("pre-install-rb-default: Makefile\n")
2352
- for sfx, i in [["-default", [["lib/**/*.rb", "$(RUBYLIBDIR)", "lib"]]], ["", $INSTALLFILES]]
2353
- files = install_files(mfile, i, nil, srcprefix) or next
2354
- for dir, *files in files
2355
- unless dirs.include?(dir)
2356
- dirs << dir
2357
- mfile.print "pre-install-rb#{sfx}: #{timestamp_file(dir, target_prefix)}\n"
2358
- end
2359
- for f in files
2360
- dest = "#{dir}/#{File.basename(f)}"
2361
- mfile.print("install-rb#{sfx}: #{dest}\n")
2362
- mfile.print("#{dest}: #{f} #{timestamp_file(dir, target_prefix)}\n")
2363
- mfile.print("\t$(Q) $(#{$extout ? 'COPY' : 'INSTALL_DATA'}) #{f} $(@D)\n")
2364
- if defined?($installed_list) and !$extout
2365
- mfile.print("\t@echo #{dest}>>$(INSTALLED_LIST)\n")
2366
- end
2367
- if $extout
2368
- mfile.print("clean-rb#{sfx}::\n")
2369
- mfile.print("\t-$(Q)$(RM) #{fseprepl[dest]}\n")
2370
- end
2371
- end
2372
- end
2373
- mfile.print "pre-install-rb#{sfx}:\n"
2374
- if files.empty?
2375
- mfile.print("\t@$(NULLCMD)\n")
2376
- else
2377
- mfile.print("\t$(ECHO) installing#{sfx.sub(/^-/, " ")} #{target} libraries\n")
2378
- end
2379
- if $extout
2380
- dirs.uniq!
2381
- unless dirs.empty?
2382
- mfile.print("clean-rb#{sfx}::\n")
2383
- for dir in dirs.sort_by {|d| -d.count('/')}
2384
- stamp = timestamp_file(dir, target_prefix)
2385
- mfile.print("\t-$(Q)$(RM) #{fseprepl[stamp]}\n")
2386
- mfile.print("\t-$(Q)$(RMDIRS) #{fseprepl[dir]}#{$ignore_error}\n")
2387
- end
2388
- end
2389
- end
2390
- end
2391
- dirs.unshift(sodir) if target and !dirs.include?(sodir)
2392
- dirs.each do |d|
2393
- t = timestamp_file(d, target_prefix)
2394
- mfile.print "#{t}:\n\t$(Q) $(MAKEDIRS) $(@D) #{d}\n\t$(Q) $(TOUCH) $@\n"
2395
- end
2396
-
2397
- mfile.print <<-SITEINSTALL
2398
-
2399
- site-install: site-install-so site-install-rb
2400
- site-install-so: install-so
2401
- site-install-rb: install-rb
2402
-
2403
- SITEINSTALL
2404
-
2405
- return unless target
2406
-
2407
- mfile.puts SRC_EXT.collect {|e| ".path.#{e} = $(VPATH)"} if $nmake == ?b
2408
- mfile.print ".SUFFIXES: .#{(SRC_EXT + [$OBJEXT, $ASMEXT]).compact.join(' .')}\n"
2409
- mfile.print "\n"
2410
-
2411
- compile_command = "\n\t$(ECHO) compiling $(<#{rsep})\n\t$(Q) %s\n\n"
2412
- command = compile_command % COMPILE_CXX
2413
- asm_command = compile_command.sub(/compiling/, 'translating') % ASSEMBLE_CXX
2414
- CXX_EXT.each do |e|
2415
- each_compile_rules do |rule|
2416
- mfile.printf(rule, e, $OBJEXT)
2417
- mfile.print(command)
2418
- mfile.printf(rule, e, $ASMEXT)
2419
- mfile.print(asm_command)
2420
- end
2421
- end
2422
- command = compile_command % COMPILE_C
2423
- asm_command = compile_command.sub(/compiling/, 'translating') % ASSEMBLE_C
2424
- C_EXT.each do |e|
2425
- each_compile_rules do |rule|
2426
- mfile.printf(rule, e, $OBJEXT)
2427
- mfile.print(command)
2428
- mfile.printf(rule, e, $ASMEXT)
2429
- mfile.print(asm_command)
2430
- end
2431
- end
2432
-
2433
- mfile.print "$(RUBYARCHDIR)/" if $extout
2434
- mfile.print "$(DLLIB): "
2435
- mfile.print "$(DEFFILE) " if makedef
2436
- mfile.print "$(OBJS) Makefile"
2437
- mfile.print " #{timestamp_file('$(RUBYARCHDIR)', target_prefix)}" if $extout
2438
- mfile.print "\n"
2439
- if File.exist?('build.zig')
2440
- mfile.print("\tenv -u DESTDIR PKG_CONFIG_PATH='$(libdir)/pkgconfig' RUBY_PC='#{File.basename(CONFIG['ruby_pc'], '.pc')}' zig build -Doptimize=ReleaseSafe --prefix-lib-dir $(shell pwd) test install\n")
2441
- if CONFIG['target_vendor'] == 'apple' && CONFIG['DLEXT'] = 'bundle'
2442
- # zig builds a .dylib but ruby wants to load a .bundle
2443
- mfile.print("\tmv -v $(TARGET).dylib $(TARGET).bundle\n")
2444
- end
2445
- else
2446
- mfile.print "\t$(ECHO) linking shared-object #{target_prefix.sub(/\A\/(.*)/, '\1/')}$(DLLIB)\n"
2447
- mfile.print "\t-$(Q)$(RM) $(@#{sep})\n"
2448
- link_so = LINK_SO.gsub(/^/, "\t$(Q) ")
2449
- if srcs.any?(&%r"\.(?:#{CXX_EXT.join('|')})\z".method(:===))
2450
- link_so = link_so.sub(/\bLDSHARED\b/, '\&XX')
2451
- end
2452
- mfile.print link_so, "\n\n"
2453
- unless $static.nil?
2454
- mfile.print "$(STATIC_LIB): $(OBJS)\n\t-$(Q)$(RM) $(@#{sep})\n\t"
2455
- mfile.print "$(ECHO) linking static-library $(@#{rsep})\n\t$(Q) "
2456
- mfile.print "$(AR) #{config_string('ARFLAGS') || 'cru '}$@ $(OBJS)"
2457
- config_string('RANLIB') do |ranlib|
2458
- mfile.print "\n\t-$(Q)#{ranlib} $(@) 2> /dev/null || true"
2459
- end
2460
- end
2461
- end
2462
- mfile.print "\n\n"
2463
- if makedef
2464
- mfile.print "$(DEFFILE): #{origdef}\n"
2465
- mfile.print "\t$(ECHO) generating $(@#{rsep})\n"
2466
- mfile.print "\t$(Q) #{makedef} > $@\n\n"
2467
- end
2468
-
2469
- depend = File.join(srcdir, "depend")
2470
- if File.exist?(depend)
2471
- mfile.print("###\n", *depend_rules(File.read(depend)))
2472
- else
2473
- mfile.print "$(OBJS): $(HDRS) $(ruby_headers)\n"
2474
- end
2475
-
2476
- $makefile_created = true
2477
- ensure
2478
- mfile.close if mfile
2479
- end
2480
-
2481
- # :stopdoc:
2482
-
2483
- def init_mkmf(config = CONFIG, rbconfig = RbConfig::CONFIG)
2484
- $makefile_created = false
2485
- $arg_config = []
2486
- $enable_shared = config['ENABLE_SHARED'] == 'yes'
2487
- $defs = []
2488
- $extconf_h = nil
2489
- if $warnflags = CONFIG['warnflags'] and CONFIG['GCC'] == 'yes'
2490
- # turn warnings into errors only for bundled extensions.
2491
- config['warnflags'] = $warnflags.gsub(/(\A|\s)-Werror[-=]/, '\1-W')
2492
- if /icc\z/ =~ config['CC']
2493
- config['warnflags'].gsub!(/(\A|\s)-W(?:division-by-zero|deprecated-declarations)/, '\1')
2494
- end
2495
- RbConfig.expand(rbconfig['warnflags'] = config['warnflags'].dup)
2496
- config.each do |key, val|
2497
- RbConfig.expand(rbconfig[key] = val.dup) if /warnflags/ =~ val
2498
- end
2499
- $warnflags = config['warnflags'] unless $extmk
2500
- end
2501
- $CFLAGS = with_config("cflags", arg_config("CFLAGS", config["CFLAGS"])).dup
2502
- $CXXFLAGS = (with_config("cxxflags", arg_config("CXXFLAGS", config["CXXFLAGS"]))||'').dup
2503
- $ARCH_FLAG = with_config("arch_flag", arg_config("ARCH_FLAG", config["ARCH_FLAG"])).dup
2504
- $CPPFLAGS = with_config("cppflags", arg_config("CPPFLAGS", config["CPPFLAGS"])).dup
2505
- $LDFLAGS = with_config("ldflags", arg_config("LDFLAGS", config["LDFLAGS"])).dup
2506
- $INCFLAGS = "-I$(arch_hdrdir)"
2507
- $INCFLAGS << " -I$(hdrdir)/ruby/backward" unless $extmk
2508
- $INCFLAGS << " -I$(hdrdir) -I$(srcdir)"
2509
- $DLDFLAGS = with_config("dldflags", arg_config("DLDFLAGS", config["DLDFLAGS"])).dup
2510
- $LIBEXT = config['LIBEXT'].dup
2511
- $OBJEXT = config["OBJEXT"].dup
2512
- $EXEEXT = config["EXEEXT"].dup
2513
- $ASMEXT = config_string('ASMEXT', &:dup) || 'S'
2514
- $LIBS = "#{config['LIBS']} #{config['DLDLIBS']}"
2515
- $LIBRUBYARG = ""
2516
- $LIBRUBYARG_STATIC = config['LIBRUBYARG_STATIC']
2517
- $LIBRUBYARG_SHARED = config['LIBRUBYARG_SHARED']
2518
- $DEFLIBPATH = [$extmk ? "$(topdir)" : "$(#{config["libdirname"] || "libdir"})"]
2519
- $DEFLIBPATH.unshift(".")
2520
- $LIBPATH = []
2521
- $INSTALLFILES = []
2522
- $NONINSTALLFILES = [/~\z/, /\A#.*#\z/, /\A\.#/, /\.bak\z/i, /\.orig\z/, /\.rej\z/, /\.l[ao]\z/, /\.o\z/]
2523
- $VPATH = %w[$(srcdir) $(arch_hdrdir)/ruby $(hdrdir)/ruby]
2524
-
2525
- $objs = nil
2526
- $srcs = nil
2527
- $libs = ""
2528
- if $enable_shared or RbConfig.expand(config["LIBRUBY"].dup) != RbConfig.expand(config["LIBRUBY_A"].dup)
2529
- $LIBRUBYARG = config['LIBRUBYARG']
2530
- end
2531
-
2532
- $LOCAL_LIBS = ""
2533
-
2534
- $cleanfiles = config_string('CLEANFILES') {|s| Shellwords.shellwords(s)} || []
2535
- $cleanfiles << "mkmf.log"
2536
- $distcleanfiles = config_string('DISTCLEANFILES') {|s| Shellwords.shellwords(s)} || []
2537
- $distcleandirs = config_string('DISTCLEANDIRS') {|s| Shellwords.shellwords(s)} || []
2538
-
2539
- $extout ||= nil
2540
- $extout_prefix ||= nil
2541
-
2542
- $arg_config.clear
2543
- dir_config("opt")
2544
- end
2545
-
2546
- FailedMessage = <<MESSAGE
2547
- Could not create Makefile due to some reason, probably lack of necessary
2548
- libraries and/or headers. Check the mkmf.log file for more details. You may
2549
- need configuration options.
2550
-
2551
- Provided configuration options:
2552
- MESSAGE
2553
-
2554
- # Returns whether or not the Makefile was successfully generated. If not,
2555
- # the script will abort with an error message.
2556
- #
2557
- # Internal use only.
2558
- #
2559
- def mkmf_failed(path)
2560
- unless $makefile_created or File.exist?("Makefile")
2561
- opts = $arg_config.collect {|t, n| "\t#{t}#{n ? "=#{n}" : ""}\n"}
2562
- abort "*** #{path} failed ***\n" + FailedMessage + opts.join
2563
- end
2564
- end
2565
-
2566
- private
2567
-
2568
- def _libdir_basename
2569
- @libdir_basename ||= config_string("libdir") {|name| name[/\A\$\(exec_prefix\)\/(.*)/, 1]} || "lib"
2570
- end
2571
-
2572
- def MAIN_DOES_NOTHING(*refs)
2573
- src = MAIN_DOES_NOTHING
2574
- unless refs.empty?
2575
- src = src.sub(/\{/) do
2576
- $& +
2577
- "\n if (argc > 1000000) {\n" +
2578
- refs.map {|n|" printf(\"%p\", &#{n});\n"}.join("") +
2579
- " }\n"
2580
- end
2581
- end
2582
- src
2583
- end
2584
-
2585
- extend self
2586
- init_mkmf
2587
-
2588
- $make = with_config("make-prog", ENV["MAKE"] || "make")
2589
- make, = Shellwords.shellwords($make)
2590
- $nmake = nil
2591
- case
2592
- when $mswin
2593
- $nmake = ?m if /nmake/i =~ make
2594
- end
2595
- $ignore_error = $nmake ? '' : ' 2> /dev/null || true'
2596
-
2597
- RbConfig::CONFIG["srcdir"] = CONFIG["srcdir"] =
2598
- $srcdir = arg_config("--srcdir", File.dirname($0))
2599
- $configure_args["--topsrcdir"] ||= $srcdir
2600
- if $curdir = arg_config("--curdir")
2601
- RbConfig.expand(curdir = $curdir.dup)
2602
- else
2603
- curdir = $curdir = "."
2604
- end
2605
- unless File.expand_path(RbConfig::CONFIG["topdir"]) == File.expand_path(curdir)
2606
- CONFIG["topdir"] = $curdir
2607
- RbConfig::CONFIG["topdir"] = curdir
2608
- end
2609
- $configure_args["--topdir"] ||= $curdir
2610
- $ruby = arg_config("--ruby", File.join(RbConfig::CONFIG["bindir"], CONFIG["ruby_install_name"]))
2611
-
2612
- RbConfig.expand(CONFIG["RUBY_SO_NAME"])
2613
-
2614
- # :startdoc:
2615
-
2616
- split = Shellwords.method(:shellwords).to_proc
2617
-
2618
- EXPORT_PREFIX = config_string('EXPORT_PREFIX') {|s| s.strip}
2619
-
2620
- hdr = ['#include "ruby.h"' "\n"]
2621
- config_string('COMMON_MACROS') do |s|
2622
- Shellwords.shellwords(s).each do |w|
2623
- w, v = w.split(/=/, 2)
2624
- hdr << "#ifndef #{w}"
2625
- hdr << "#define #{[w, v].compact.join(" ")}"
2626
- hdr << "#endif /* #{w} */"
2627
- end
2628
- end
2629
- config_string('COMMON_HEADERS') do |s|
2630
- Shellwords.shellwords(s).each {|w| hdr << "#include <#{w}>"}
2631
- end
2632
-
2633
- ##
2634
- # Common headers for Ruby C extensions
2635
-
2636
- COMMON_HEADERS = hdr.join("\n")
2637
-
2638
- ##
2639
- # Common libraries for Ruby C extensions
2640
-
2641
- COMMON_LIBS = config_string('COMMON_LIBS', &split) || []
2642
-
2643
- ##
2644
- # make compile rules
2645
-
2646
- COMPILE_RULES = config_string('COMPILE_RULES', &split) || %w[.%s.%s:]
2647
- RULE_SUBST = config_string('RULE_SUBST')
2648
-
2649
- ##
2650
- # Command which will compile C files in the generated Makefile
2651
-
2652
- COMPILE_C = config_string('COMPILE_C') || '$(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<'
2653
-
2654
- ##
2655
- # Command which will compile C++ files in the generated Makefile
2656
-
2657
- COMPILE_CXX = config_string('COMPILE_CXX') || '$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<'
2658
-
2659
- ##
2660
- # Command which will translate C files to assembler sources in the generated Makefile
2661
-
2662
- ASSEMBLE_C = config_string('ASSEMBLE_C') || COMPILE_C.sub(/(?<=\s)-c(?=\s)/, '-S')
2663
-
2664
- ##
2665
- # Command which will translate C++ files to assembler sources in the generated Makefile
2666
-
2667
- ASSEMBLE_CXX = config_string('ASSEMBLE_CXX') || COMPILE_CXX.sub(/(?<=\s)-c(?=\s)/, '-S')
2668
-
2669
- ##
2670
- # Command which will compile a program in order to test linking a library
2671
-
2672
- TRY_LINK = config_string('TRY_LINK') ||
2673
- "$(CC) #{OUTFLAG}#{CONFTEST}#{$EXEEXT} $(INCFLAGS) $(CPPFLAGS) " \
2674
- "$(CFLAGS) $(src) $(LIBPATH) $(LDFLAGS) $(ARCH_FLAG) $(LOCAL_LIBS) $(LIBS)"
2675
-
2676
- ##
2677
- # Command which will link a shared library
2678
-
2679
- LINK_SO = (config_string('LINK_SO') || "").sub(/^$/) do
2680
- if CONFIG["DLEXT"] == $OBJEXT
2681
- "ld $(DLDFLAGS) -r -o $@ $(OBJS)\n"
2682
- else
2683
- "$(LDSHARED) #{OUTFLAG}$@ $(OBJS) " \
2684
- "$(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)"
2685
- end
2686
- end
2687
-
2688
- ##
2689
- # Argument which will add a library path to the linker
2690
-
2691
- LIBPATHFLAG = config_string('LIBPATHFLAG') || ' -L%s'
2692
- RPATHFLAG = config_string('RPATHFLAG') || ''
2693
-
2694
- ##
2695
- # Argument which will add a library to the linker
2696
-
2697
- LIBARG = config_string('LIBARG') || '-l%s'
2698
-
2699
- ##
2700
- # A C main function which does no work
2701
-
2702
- MAIN_DOES_NOTHING = config_string('MAIN_DOES_NOTHING') || "int main(int argc, char **argv)\n{\n return 0;\n}"
2703
- UNIVERSAL_INTS = config_string('UNIVERSAL_INTS') {|s| Shellwords.shellwords(s)} ||
2704
- %w[int short long long\ long]
2705
-
2706
- sep = config_string('BUILD_FILE_SEPARATOR') {|s| ":/=#{s}" if s != "/"} || ""
2707
-
2708
- ##
2709
- # Makefile rules that will clean the extension build directory
2710
-
2711
- CLEANINGS = "
2712
- clean-static::
2713
- clean-rb-default::
2714
- clean-rb::
2715
- clean-so::
2716
- clean: clean-so clean-static clean-rb-default clean-rb
2717
- \t\t-$(Q)$(RM) $(CLEANLIBS#{sep}) $(CLEANOBJS#{sep}) $(CLEANFILES#{sep}) .*.time
2718
- \t\t-$(Q)$(RM_RF) zig-cache zig-out
2719
-
2720
- distclean-rb-default::
2721
- distclean-rb::
2722
- distclean-so::
2723
- distclean-static::
2724
- distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb
2725
- \t\t-$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) #{CONFTEST}.* mkmf.log
2726
- \t\t-$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES#{sep})
2727
- \t\t-$(Q)$(RMDIRS) $(DISTCLEANDIRS#{sep})#{$ignore_error}
2728
-
2729
- realclean: distclean
2730
- "
2731
- end
2732
-
2733
- include MakeMakefile
2734
-
2735
- if not $extmk and /\A(extconf|makefile).rb\z/ =~ File.basename($0)
2736
- END {mkmf_failed($0)}
2737
- end