Added alignment checking to sztest.
[utils.git] / sztest
CommitLineData
1c2fdcc2
FT
1#!/bin/sh
2
8f1c8d49 3usage() {
2dcf5486 4 echo "usage: sztest [-hab] [-H SYS-HEADER] [-L LOCAL-HEADER] TYPE [CCFLAGS...]"
8f1c8d49 5}
1c2fdcc2 6
2dcf5486 7mode=size
c5f010d4
FT
8sheaders=
9lheaders=
1c2fdcc2
FT
10while [ "${1:0:1}" = "-" ]; do
11 opt="$1"
12 shift
13 case "$opt" in
8f1c8d49
FT
14 "-h")
15 usage
16 exit 0
17 ;;
2dcf5486
FT
18 "-a")
19 mode=align
20 ;;
21 "-b")
22 mode=both
23 ;;
1c2fdcc2 24 "-H")
c5f010d4
FT
25 sheaders="$sheaders $1"
26 shift
27 ;;
28 "-L")
29 lheaders="$lheaders $1"
1c2fdcc2
FT
30 shift
31 ;;
32 esac
33done
8f1c8d49
FT
34if [ $# -lt 1 ]; then
35 usage >&2
36 exit 1
37fi
1c2fdcc2
FT
38type="$1"
39shift
40file="$(mktemp /tmp/sztestXXXXXX)"
41cat >"$file.c" <<EOF
42#include <stdio.h>
43#include <stdint.h>
44#include <sys/types.h>
45#include <unistd.h>
46#include <sys/stat.h>
47EOF
c5f010d4 48for header in $sheaders; do
1c2fdcc2
FT
49 echo "#include <$header>" >>"$file.c"
50done
c5f010d4
FT
51for header in $lheaders; do
52 echo "#include \"$header\"" >>"$file.c"
53done
1c2fdcc2
FT
54cat >>"$file.c" <<EOF
55int main(int argc, char **argv)
56{
2dcf5486
FT
57EOF
58case "$mode" in
59 size)
60 echo " printf(\"%zi\\n\", sizeof($type));" >>"$file.c"
61 ;;
62 align)
63 echo " printf(\"%zi\\n\", __alignof__($type));" >>"$file.c"
64 ;;
65 both)
66 echo " printf(\"%zi %zi\\n\", sizeof($type), __alignof__($type));" >>"$file.c"
67 ;;
68esac
69cat >>"$file.c" <<EOF
1c2fdcc2
FT
70 return(0);
71}
72EOF
73
c5f010d4 74if ! gcc "$@" -iquote. -g -Wall -o "$file" "$file.c"; then
1c2fdcc2
FT
75 rm -f "$file" "$file.c"
76 exit 1
77fi
78"$file"
79rm "$file" "$file.c"