aboutsummaryrefslogtreecommitdiffstats
path: root/compat/windows/makedef
blob: fd7959a745fff3e30df3e5f7444f4ff3c4b0e859 (plain) (blame)
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
#!/bin/sh

# Copyright (c) 2013, 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.

# mktemp isn't POSIX, so supply an implementation
mktemp() {
    echo "${2%%XXX*}.${HOSTNAME}.${UID}.$$"
}

if [ $# -lt 2 ]; then
    echo "Usage: makedef <version_script> <objects>" >&2
    exit 0
fi

vscript=$1
shift

if [ ! -f "$vscript" ]; then
    echo "Version script does not exist" >&2
    exit 1
fi

for object in "$@"; do
    if [ ! -f "$object" ]; then
        echo "Object does not exist: ${object}" >&2
        exit 1
    fi
done

# Create a lib temporarily to dump symbols from.
# It's just much easier to do it this way
libname=$(mktemp -u "library").lib

trap 'rm -f -- $libname' EXIT

if [ -n "$AR" ]; then
    $AR rcs ${libname} $@ >/dev/null
else
    lib -out:${libname} $@ >/dev/null
fi
if [ $? != 0 ]; then
    echo "Could not create temporary library." >&2
    exit 1
fi

IFS='
'

# Determine if we're building for x86 or x86_64 and
# set the symbol prefix accordingly.
prefix=""
if [ -n "$NM" ]; then
    case $ARCH in
    *86)
        prefix="_"
        ;;
    *)
        ;;
    esac
else
    arch=$(dumpbin -headers ${libname} |
           tr '\t' ' ' |
           grep '^ \+.\+machine \+(.\+)' |
           head -1 |
           sed -e 's/^ \{1,\}.\{1,\} \{1,\}machine \{1,\}(\(...\)).*/\1/')

    if [ "${arch}" = "x86" ]; then
        prefix="_"
    else
        if [ "${arch}" != "ARM" ] && [ "${arch}" != "x64" ]; then
            echo "Unknown machine type." >&2
            exit 1
        fi
    fi
fi

started=0
regex="none"

for line in $(cat ${vscript} | tr '\t' ' '); do
    # We only care about global symbols
    echo "${line}" | grep -q '^ \+global:'
    if [ $? = 0 ]; then
        started=1
        line=$(echo "${line}" | sed -e 's/^ \{1,\}global: *//')
    else
        echo "${line}" | grep -q '^ \+local:'
        if [ $? = 0 ]; then
            started=0
        fi
    fi

    if [ ${started} = 0 ]; then
        continue
    fi

    # Handle multiple symbols on one line
    IFS=';'

    # Work around stupid expansion to filenames
    line=$(echo "${line}" | sed -e 's/\*/.\\+/g')
    for exp in ${line}; do
        # Remove leading and trailing whitespace
        exp=$(echo "${exp}" | sed -e 's/^ *//' -e 's/ *$//')

        if [ "${regex}" = "none" ]; then
            regex="${exp}"
        else
            regex="${regex};${exp}"
        fi
    done

    IFS='
'
done

if [ -n "$NM" ]; then
    # Use eval, since NM="nm -g"
    dump=$(eval "$NM --defined-only -g ${libname}" |
              grep -v : |
              grep -v ^$ |
              cut -d' ' -f3 |
              sed -e "s/^${prefix}//")
else
    dump=$(dumpbin -linkermember:1 ${libname} |
              sed -e '/public symbols/,$!d' -e '/^ \{1,\}Summary/,$d' -e "s/ \{1,\}${prefix}/ /" -e 's/ \{1,\}/ /g' |
              tail -n +2 |
              cut -d' ' -f3)
fi

rm ${libname}

IFS=';'
list=""
for exp in ${regex}; do
    list="${list}"'
'$(echo "${dump}" |
          grep "^${exp}" |
          sed -e 's/^/    /')
done

echo "EXPORTS"
echo "${list}" | sort | uniq | tail -n +2