1# frozen_string_literal: false
2$extmk = true
3require 'rbconfig'
4RbConfig.fire_update!("top_srcdir", File.expand_path("../..", __dir__))
5File.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
10end
11
12require 'test/unit'
13require 'mkmf'
14require 'tmpdir'
15
16$extout = '$(topdir)/'+RbConfig::CONFIG["EXTOUT"]
17RbConfig::CONFIG['topdir'] = CONFIG['topdir'] = File.expand_path(CONFIG['topdir'])
18RbConfig::CONFIG["extout"] = CONFIG["extout"] = $extout
19$INCFLAGS << " -I."
20$extout_prefix = "$(extout)$(target_prefix)/"
21
22class TestMkmf < Test::Unit::TestCase
23end
24
25module TestMkmf::Base
26  MKMFLOG = proc {File.read("mkmf.log") rescue ""}
27
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
68
69  attr_reader :stdout
70
71  def mkmflog(msg)
72    proc {MKMFLOG[] << msg}
73  end
74
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
109
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
129
130  def mkmf(*args, &block)
131    @stdout.clear
132    stdout, @stdout.origin, $stdout = @stdout.origin, $stdout, @stdout
133    @mkmfobj.instance_eval(*args, &block)
134  ensure
135    $stdout, @stdout.origin = @stdout.origin, stdout
136  end
137
138  def config_value(name)
139    create_tmpsrc("---config-value=#{name}")
140    xpopen(cpp_command('')) do |f|
141      f.grep(/^---config-value=(.*)/) {return $1}
142    end
143    nil
144  end
145end
146
147class TestMkmf
148  include TestMkmf::Base
149
150  def assert_separately(args, src, *rest)
151    super(args + ["-r#{__FILE__}"], "extend TestMkmf::Base; setup\nEND{teardown}\n#{src}", *rest)
152  end
153end
154