summaryrefslogtreecommitdiff
path: root/win32/mkexports.rb
blob: 90c4d7f9144bbea938eaa9e722de4244629ebba1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!./miniruby -s

$name = $library = $description = nil

module RbConfig
  autoload :CONFIG, "rbconfig"
end

class Exports
  @subclass = []
  def self.inherited(klass)
    @subclass << [/#{klass.name.sub(/.*::/, '').downcase}/i, klass]
  end

  def self.create(*args, &block)
    platform = RUBY_PLATFORM
    pat, klass = @subclass.find {|p, k| p =~ platform}
    unless klass
      raise ArgumentError, "unsupported platform: #{platform}"
    end
    klass.new(*args, &block)
  end

  def self.extract(objs, *rest)
    create(objs).exports(*rest)
  end

  def self.output(output = $output, &block)
    if output
      open(output, 'w', &block)
    else
      yield STDOUT
    end
  end

  def initialize(objs)
    syms = {}
    winapis = {}
    each_export(objs) do |internal, export|
      syms[internal] = export
      winapis[$1] = internal if /^_?(rb_w32_\w+)(?:@\d+)?$/ =~ internal
    end
    win32h = File.join(File.dirname(File.dirname(__FILE__)), "include/ruby/win32.h")
    IO.foreach(win32h) do |line|
      if /^#define (\w+)\((.*?)\)\s+(?:\(void\))?(rb_w32_\w+)\((.*?)\)\s*$/ =~ line and
          $2.delete(" ") == $4.delete(" ")
        export, internal = $1, $3
        if syms[internal] or internal = winapis[internal]
          syms[forwarding(internal, export)] = internal
        end
      end
    end
    @syms = syms
  end

  def exports(name = $name, library = $library, description = $description)
    exports = []
    if name
      exports << "Name " + name
    elsif library
      exports << "Library " + library
    end
    exports << "Description " + description.dump if description
    exports << "EXPORTS" << symbols()
    exports
  end

  private
  def forwarding(internal, export)
    internal.sub(/^[^@]+/, "\\1#{export}")
  end

  def each_export(objs)
  end

  def symbols()
    @syms.sort.collect {|k, v| v ? "#{k}=#{v}" : k}
  end
end

class Exports::Mswin < Exports
  def each_export(objs)
    noprefix = ($arch ||= nil and /^(sh|i\d86)/ !~ $arch)
    objs = objs.collect {|s| s.tr('/', '\\')}
    filetype = nil
    IO.popen(%w"dumpbin -symbols -exports" + objs) do |f|
      f.each do |l|
        if (filetype = l[/^File Type: (.+)/, 1])..(/^\f/ =~ l)
          case filetype
          when /OBJECT/, /LIBRARY/
            next if /^[[:xdigit:]]+ 0+ UNDEF / =~ l
            next unless l.sub!(/.*\sExternal\s+\|\s+/, '')
            if noprefix or l.sub!(/^_/, '')
              next if /@.*@/ =~ l || /@[[:xdigit:]]{16}$/ =~ l
              l.sub!(/^/, '_') if /@\d+$/ =~ l
            elsif !l.sub!(/^(\S+) \([^@?\`\']*\)$/, '\1')
              next
            end
          when /DLL/
            next unless l.sub!(/^\s*\d+\s+[[:xdigit:]]+\s+[[:xdigit:]]+\s+/, '')
          else
            next
          end
          yield l.strip
        end
      end
    end
    yield "strcasecmp", "msvcrt.stricmp"
    yield "strncasecmp", "msvcrt.strnicmp"
  end
end

class Exports::Mingw < Exports
  def self.nm
    @@nm ||= RbConfig::CONFIG["NM"]
  end

  def each_export(objs)
    IO.popen([self.class.nm, "--extern", "--defined", *objs]) do |f|
      f.each {|l| yield $1 if / [[:upper:]] _(.*)$/ =~ l}
    end
    yield "strcasecmp", "_stricmp"
    yield "strncasecmp", "_strnicmp"
  end

  def symbols()
    @syms.select {|k, v| v}.sort.collect {|k, v| "#{k}=#{v}"}
  end
end

END {
  exports = Exports.extract(ARGV)
  Exports.output {|f| f.puts(*exports)}
}