forked from libav/c99-to-c89
-
Notifications
You must be signed in to change notification settings - Fork 2
/
makedef
executable file
·158 lines (126 loc) · 3.57 KB
/
makedef
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env perl
# Copyright (c) 2012, Derek Buitenhuis
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use strict;
use warnings;
use File::Temp;
# Handle Arguments
my ($vscript, @objects) = @ARGV;
unless (defined($vscript) && @objects) {
print("Usage: makedef <version_script> <objects>\n");
exit(0);
}
unless (-e $vscript) {
print("Version script does not exist\n");
exit(0);
}
foreach my $object (@objects) {
unless (-e $object) {
print("Object does not exist: $object\n");
exit(0);
}
}
# Create a lib temporarily to dump symbols from.
# It's just much easier to do it this way.
my $lib = mktemp("library.XXXXXXXX").".lib";
my $objectstring = join(" ", @objects);
`lib -out:$lib $objectstring`;
if ($?) {
print("Could not create temporary library.\n");
exit(1);
}
# Determine if we're building for x86 or x86_64 and
# set the symbol prefix accordingly.
my $prefix;
my @machinedump = `dumpbin -headers $lib`;
foreach(@machinedump) {
chomp;
next if !(s/^\s+.+\s+machine\s+\((...)\).*/$1/);
if (/x86/) {
$prefix = "_";
} elsif (/x64/) {
$prefix = "";
} elsif (/ARM/) {
$prefix = "";
} else {
print("Unknown machine type.\n");
exit(1);
}
}
open(my $file, "<", $vscript) || die("Cannot open $vscript");
my $started = 0;
my @regex;
while(<$file>) {
chomp;
# We only care about global symbols
if (s/^\s+global://) {
$started = 1;
} elsif (/^\s+local:/) {
$started = 0;
}
next if !$started;
# Handle multiple symbols on one line
my @delim = split(/;/);
foreach my $exp (@delim) {
# Remove leading and trailing whitespace
$exp =~ s/^\s*|\s*$//;
# Convert asterisks into its PCRE-friendly equivalent
$exp =~ s/\*/.+/;
push(@regex, $exp);
}
}
close($file);
my @dump = `dumpbin -linkermember:1 $lib`;
if ($?) {
print("Execution of dumpbin failed.\n");
exit(1);
}
# Delete the temp lib, as we no longer need it.
if (!unlink($lib)) {
print("Could not delete temporary library.\n");
exit(1);
}
my @syms;
$started = 0;
foreach(@dump) {
# Where to start and stop parsing
if (/public symbols/) {
$started = 1;
} elsif (/^\s+Summary/) {
$started = 0;
}
next if !$started;
# Only process lines with symbols
next if !(s/^\s+[0-9A-F]+\s+$prefix//);
# Perl's chomp only handles whatever is in $/, so in an effort
# to be portable, strip trailing newlines manually.
s/\r?\n$//;
# It doesn't matter if we push duplicates here, since we make
# the array unique later on.
foreach my $exp (@regex) {
if (/^$exp/) {
push(@syms, $_);
}
}
}
close($file);
my $last = "none";
@syms = sort(@syms);
print("EXPORTS\n");
foreach my $sym (@syms) {
# Ignore any duplicate entries
next if ($last eq $sym);
print(" $sym\n");
$last = $sym;
}