summaryrefslogtreecommitdiff
path: root/macros/offset-tool.sh.in
blob: c28b50f0225f2e310fd2e435161cb586b3865ae3 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/sh
#
# offset-tool.sh (C)2000 Peter Williams
#
# This tool is used to pregenerate structure offset
# data for use with Balsa's configuration, because
# it's really constant data although GCC, at least,
# doesn't think so.
#
# To achieve that effect, I have writen this here
# absurdly overcomplicated tool, autoconf macro,
# configure.in code, and Makefile.am :-) There
# might be a simpler way to do this, and I guess
# I'll trade in this cool code for better code,
# but I can't think of a simpler way right now :-)

srcdir="@srcdir@"
top_srcdir="@top_srcdir@"
real_top_srcdir="@BALSA_OFFSET_SRCPATH@"
offset_tool_home="@BALSA_OFFSET_TOOL_HOME@"
top_dots="@BALSA_OFFSET_TOOL_DOTS@"
tool_defs="@BALSA_OFFSET_TOOL_DEFSFILE@"
CPP="@CPP@"
SHELL="@SHELL@"
CC="@CC@"
DEFS="@DEFS@"
CFLAGS="@CFLAGS@"
mid_file=offset-tool.c
no_clean=no

case "${top_srcdir}" in
/*)
    test -f ../../config.status && top_builddir="../.."
    test -f ../config.status && top_builddir=".."
    test -f config.status && top_builddir="."
    ;;
#..)
#    top_builddir=".."
#    real_top_srcdir=".."
#    ;;
#.)
#    top_builddir="."
#    real_top_srcdir"."
#    ;;
*)
    test -f ../../${real_top_srcdir}/configure && top_builddir="../.."
    test -f ../${real_top_srcdir}/configure && top_builddir=".."
    test -f ./${real_top_srcdir}/configure && top_builddir="."
    ;;
esac

case "${real_top_srcdir}" in
/*)
    header_dots="${real_top_srcdir}"
    ;;
*)
    header_dots="${top_dots}${real_top_srcdir}"
    ;;
esac

# Backup top srcdir to be what we want it to be

tmp="${top_srcdir}"
top_srcdir="${header_dots}"

echo top builddir: ${top_builddir}

if test -r "${top_builddir}/${tool_defs}" ; then
    . "${top_builddir}/${tool_defs}"
fi

# Restore the backup

top_srcdir="${tmp}"

usage()
{
    echo "Usage: offset-tool.sh (--help|--create|--makerules|--headername) [--fragname=<fragment name>] [--noclean] <filenames...>"
    echo "Look at $(top_srcdir)/${offset_tool_home}/offset-tool.sh.in for more information"
    exit 1
}

if test -z "$1" ; then
    usage
fi

mode=`echo $1 |sed -e 's,^--,,'`

case $mode in
help)
    usage
    ;;
create)
    ;;
makerules)
    ;;
headername)
    ;;
*)
    echo "Unknown mode $mode"
    usage
    ;;
esac

shift

fragname=

while test $# -gt 0 ; do
    case "${1}" in
	--fragname=*)
	    fragname=`echo ${1} |sed -e 's,.*=\(.*\),\1,'`; shift ;;
	--fragname)
	    shift; fragname="${1}" ; shift ;;
	--noclean)
	    no_clean=yes ; shift ;;
	--)
	    shift; break ;;
	-*)
	    usage ;;
	*)
	    break ;;
    esac
done

test x"$fragname" = x && fragname=Makefragment.offset-tool
infiles=$*

test x"$mode" = xmakerules || echo "Scanning these files for config offsets: $infiles"

header=`cat $infiles |grep "OFFSET HEADER" |sed -e 's,# *include *\"\(.*\)\".*,\1,' |sed -e '1!d'`
if test x"$header" = x ; then
    echo "Couldn't determine the configuration header." 1>&2
    exit 2
fi

if test x"$mode" = xcreate ; then
    echo Removing old header ... 
    echo "/* Nothing */" >${header}

    echo Getting needed defines ...
    defines=`${CPP} ${CFLAGS} $infiles |grep "BALSA_OFFSET_" |sed -e 's|^.*BALSA_OFFSET_\([a-zA-Z0-9_]*\)_ELEM_\([a-zA-Z0-9_]*\).*$|\1--\2|'`
    # Helpers will be in comments, don't chomp them.
    helpers=`cat $infiles |grep "OFFSET DEF" |sed -e 's|^.*OFFSET DEF *\[\[\(.*\)\]\].*$|\1|'`
    
    echo Generating ${mid_file} ...
    cat <<EOF >${mid_file}
/* This file generated by offset-tool.sh. Do not edit! */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef STDC_HEADERS
#include <stdio.h>
#else
extern void printf( char *, ... );
#endif

EOF

    echo >>${mid_file}
    echo "$helpers" >>${mid_file}
    echo >>${mid_file}

    cat <<EOF >>${mid_file}

#ifndef offsetof
#define offsetof( type, member ) ( (int) ( (char *) ( &( ( (type *) 0 )->member ) ) ) )
#endif

int main( int argc, char **argv )
{
    printf( "/* This file generated by ${mid_file}, generated by offset-tool.sh. Do not edit!*/\n" );
    printf( "/* Scanned from files: $infiles */\n\n" );

    printf( "#ifndef _OFFSET_TOOL_HEADER_FILE\n" );
    printf( "#define _OFFSET_TOOL_HEADER_FILE\n\n" );

EOF

    for ping in $defines ; do
	type=`echo $ping |sed -e 's|--.*$||'`
	member=`echo $ping |sed -e 's|^.*--||'`

	cat <<EOF >>${mid_file}
    printf( "#define BALSA_OFFSET_${type}_ELEM_${member} %d\n", offsetof( $type, $member ) );
EOF
    done

    cat <<EOF >>${mid_file}

    printf( "\n\n#endif\n" );
    return 0;
}
EOF

    mid_exe=`echo ${mid_file} |sed -e 's,\.c,,'`
    if test x`echo ${mid_exe} |grep '^/'` = x ; then
	mid_exe="./${mid_exe}"
    fi

    echo "Building ${mid_exe} ..."
    ${SHELL} libtool --mode=link ${CC} -o ${mid_exe} ${DEFS} ${CFLAGS} ${mid_file} || {
	echo "Error compiling ${mid_file}"
	exit 2
    }

    echo "Running ${mid_exe} >${header}"
    ${mid_exe} >${header} || {
	echo "Error running ${mid_exe} made from ${mid_file}"
	exit 3
    }

    if test x"$no_clean" = xyes ; then
	:
    else
	echo "Cleaning up..."
	rm -f ${mid_file} ${mid_exe}
    fi

    res=$?
    echo "Done creating ${header}"
    exit $res
fi

if test x"$mode" = xmakerules ; then
    # FIXME bleah
    tab=`echo -e '\t'`
    
    cat <<EOF
${header}: \$(top_builddir)/${offset_tool_home}/offset-tool.sh ${infiles}
${tab}\$(SHELL) \$(top_builddir)/${offset_tool_home}/offset-tool.sh --create ${infiles}

${fragname}: \$(top_builddir)/${offset_tool_home}/offset-tool.sh
${tab}\$(SHELL) \$(top_builddir)/${offset_tool_home}/offset-tool.sh --makerules --fragname ${fragname} ${infiles} >${fragname}

\$(top_builddir)/${offset_tool_home}/offset-tool.sh: \$(top_builddir)/config.status \$(top_srcdir)/${offset_tool_home}/offset-tool.sh.in
${tab}(cd \$(top_builddir) && CONFIG_FILES="${offset_tool_home}/offset-tool.sh" CONFIG_HEADERS= \$(SHELL) ./config.status)
${tab}chmod +x \$(top_builddir)/${offset_tool_home}/offset-tool.sh

offset-clean:
${tab}rm -f ${header}

offset-realclean:
${tab}rm -f \$(top_builddir)/${offset_tool_home}/offset-tool.sh ${fragname}
EOF
    exit 0
fi

if x"$mode" = xheadername; then
    echo ${header}
    exit 0
fi

echo "Unhandled mode: $mode" 1>&2
exit 1