• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2
3# Copyright (c) 2013, Derek Buitenhuis
4#
5# Permission to use, copy, modify, and/or distribute this software for any
6# purpose with or without fee is hereby granted, provided that the above
7# copyright notice and this permission notice appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17# mktemp isn't POSIX, so supply an implementation
18mktemp() {
19    echo "${2%%XXX*}.${HOSTNAME}.${UID}.$$"
20}
21
22if [ $# -lt 2 ]; then
23    echo "Usage: makedef <version_script> <objects>" >&2
24    exit 0
25fi
26
27vscript=$1
28shift
29
30if [ ! -f "$vscript" ]; then
31    echo "Version script does not exist" >&2
32    exit 1
33fi
34
35for object in "$@"; do
36    if [ ! -f "$object" ]; then
37        echo "Object does not exist: ${object}" >&2
38        exit 1
39    fi
40done
41
42# Create a lib temporarily to dump symbols from.
43# It's just much easier to do it this way
44libname=$(mktemp -u "library").lib
45
46trap 'rm -f -- $libname' EXIT
47
48if [ -n "$AR" ]; then
49    $AR rcs ${libname} $@ >/dev/null
50else
51    lib.exe -out:${libname} $@ >/dev/null
52fi
53if [ $? != 0 ]; then
54    echo "Could not create temporary library." >&2
55    exit 1
56fi
57
58IFS='
59'
60
61prefix="$EXTERN_PREFIX"
62
63started=0
64regex="none"
65
66for line in $(cat ${vscript} | tr '\t' ' '); do
67    # We only care about global symbols
68    echo "${line}" | grep -q '^ \+global:'
69    if [ $? = 0 ]; then
70        started=1
71        line=$(echo "${line}" | sed -e 's/^ \{1,\}global: *//')
72    else
73        echo "${line}" | grep -q '^ \+local:'
74        if [ $? = 0 ]; then
75            started=0
76        fi
77    fi
78
79    if [ ${started} = 0 ]; then
80        continue
81    fi
82
83    # Handle multiple symbols on one line
84    IFS=';'
85
86    # Work around stupid expansion to filenames
87    line=$(echo "${line}" | sed -e 's/\*/.\\+/g')
88    for exp in ${line}; do
89        # Remove leading and trailing whitespace
90        exp=$(echo "${exp}" | sed -e 's/^ *//' -e 's/ *$//')
91
92        if [ "${regex}" = "none" ]; then
93            regex="${exp}"
94        else
95            regex="${regex};${exp}"
96        fi
97    done
98
99    IFS='
100'
101done
102
103if [ -n "$NM" ]; then
104    # Use eval, since NM="nm -g"
105    dump=$(eval "$NM --defined-only -g ${libname}" |
106              grep -v : |
107              grep -v ^$ |
108              cut -d' ' -f3 |
109              sed -e "s/^${prefix}//")
110else
111    dump=$(dumpbin.exe -linkermember:1 ${libname} |
112              sed -e '/public symbols/,$!d' -e '/^ \{1,\}Summary/,$d' -e "s/ \{1,\}${prefix}/ /" -e 's/ \{1,\}/ /g' |
113              tail -n +2 |
114              cut -d' ' -f3)
115fi
116
117rm ${libname}
118
119IFS=';'
120list=""
121for exp in ${regex}; do
122    list="${list}"'
123'$(echo "${dump}" |
124          grep "^${exp}" |
125          sed -e 's/^/    /')
126done
127
128echo "EXPORTS"
129echo "${list}" | sort | uniq | tail -n +2
130