* 2022-01-18 [ci skip]
[ruby-80x24.org.git] / test / mkmf / base.rb
blob4f67478a5641426f0313dd68a48f4c655546e99f
1 # frozen_string_literal: false
2 $extmk = true
3 require 'rbconfig'
4 RbConfig.fire_update!("top_srcdir", File.expand_path("../..", __dir__))
5 File.foreach(RbConfig::CONFIG["topdir"]+"/Makefile") do |line|
6   if /^CC_WRAPPER\s*=\s*/ =~ line
7     RbConfig.fire_update!('CC_WRAPPER', $'.strip)
8     break
9   end
10 end
12 require 'test/unit'
13 require 'mkmf'
14 require 'tmpdir'
16 $extout = '$(topdir)/'+RbConfig::CONFIG["EXTOUT"]
17 RbConfig::CONFIG['topdir'] = CONFIG['topdir'] = File.expand_path(CONFIG['topdir'])
18 RbConfig::CONFIG["extout"] = CONFIG["extout"] = $extout
19 $INCFLAGS << " -I."
20 $extout_prefix = "$(extout)$(target_prefix)/"
22 class TestMkmf < Test::Unit::TestCase
23 end
25 module TestMkmf::Base
26   MKMFLOG = proc {File.read("mkmf.log") rescue ""}
28   class Capture
29     attr_accessor :origin
30     def initialize
31       @buffer = ""
32       @filter = nil
33       @out = true
34       @origin = nil
35     end
36     def clear
37       @buffer.clear
38     end
39     def flush
40       STDOUT.print @filter ? @filter.call(@buffer) : @buffer
41       clear
42     end
43     def reopen(io)
44       case io
45       when Capture
46         initialize_copy(io)
47       when File
48         @out = false
49         @origin.reopen(io) if @origin
50       when IO
51         @out = true
52         @origin.reopen(io) if @origin
53       else
54         @out = false
55       end
56     end
57     def filter(&block)
58       @filter = block
59     end
60     def write(*s)
61       if @out
62         @buffer.concat(*s)
63       elsif @origin
64         @origin.write(*s)
65       end
66     end
67   end
69   attr_reader :stdout
71   def mkmflog(msg)
72     proc {MKMFLOG[] << msg}
73   end
75   def setup
76     @rbconfig = rbconfig0 = RbConfig::CONFIG
77     @mkconfig = mkconfig0 = RbConfig::MAKEFILE_CONFIG
78     rbconfig = {
79       "hdrdir" => $hdrdir,
80       "srcdir" => $srcdir,
81       "topdir" => $topdir,
82     }
83     mkconfig = {
84       "hdrdir" => "$(top_srcdir)/include",
85       "srcdir" => "$(top_srcdir)",
86       "topdir" => $topdir,
87     }
88     rbconfig0.each_pair {|key, val| rbconfig[key] ||= val.dup}
89     mkconfig0.each_pair {|key, val| mkconfig[key] ||= val.dup}
90     RbConfig.module_eval {
91       remove_const(:CONFIG)
92       const_set(:CONFIG, rbconfig)
93       remove_const(:MAKEFILE_CONFIG)
94       const_set(:MAKEFILE_CONFIG, mkconfig)
95     }
96     MakeMakefile.class_eval {
97       remove_const(:CONFIG)
98       const_set(:CONFIG, mkconfig)
99     }
100     @tmpdir = Dir.mktmpdir
101     @curdir = Dir.pwd
102     @mkmfobj = Object.new
103     @stdout = Capture.new
104     Dir.chdir(@tmpdir)
105     @quiet, Logging.quiet = Logging.quiet, true
106     init_mkmf
107     $INCFLAGS[0, 0] = "-I. "
108   end
110   def teardown
111     rbconfig0 = @rbconfig
112     mkconfig0 = @mkconfig
113     RbConfig.module_eval {
114       remove_const(:CONFIG)
115       const_set(:CONFIG, rbconfig0)
116       remove_const(:MAKEFILE_CONFIG)
117       const_set(:MAKEFILE_CONFIG, mkconfig0)
118     }
119     MakeMakefile.class_eval {
120       remove_const(:CONFIG)
121       const_set(:CONFIG, mkconfig0)
122     }
123     Logging.quiet = @quiet
124     Logging.log_close
125     FileUtils.rm_f("mkmf.log")
126     Dir.chdir(@curdir)
127     FileUtils.rm_rf(@tmpdir)
128   end
130   def mkmf(*args, &block)
131     @stdout.clear
132     stdout, @stdout.origin, $stdout = @stdout.origin, $stdout, @stdout
133     verbose, $VERBOSE = $VERBOSE, false
134     @mkmfobj.instance_eval(*args, &block)
135   ensure
136     $VERBOSE = verbose
137     $stdout, @stdout.origin = @stdout.origin, stdout
138   end
140   def config_value(name)
141     create_tmpsrc("---config-value=#{name}")
142     xpopen(cpp_command('')) do |f|
143       f.grep(/^---config-value=(.*)/) {return $1}
144     end
145     nil
146   end
149 class TestMkmf
150   include TestMkmf::Base
152   def assert_separately(args, src, *rest, **options)
153     super(args + ["-r#{__FILE__}"], "extend TestMkmf::Base; setup\nEND{teardown}\n#{src}", *rest, **options)
154   end