nextep: Create data directory only if necessary.
[utils.git] / sztest
1 #!/bin/bash
2
3 usage() {
4     echo "usage: sztest [-hab] [-H SYS-HEADER] [-L LOCAL-HEADER] TYPE [CCFLAGS...]"
5 }
6
7 mode=size
8 sheaders=
9 lheaders=
10 while [ "${1:0:1}" = "-" ]; do
11     opt="$1"
12     shift
13     case "$opt" in
14         "-h")
15             usage
16             exit 0
17             ;;
18         "-a")
19             mode=align
20             ;;
21         "-b")
22             mode=both
23             ;;
24         "-H")
25             sheaders="$sheaders $1"
26             shift
27             ;;
28         "-L")
29             lheaders="$lheaders $1"
30             shift
31             ;;
32     esac
33 done
34 if [ $# -lt 1 ]; then
35     usage >&2
36     exit 1
37 fi
38 type="$1"
39 shift
40 file="$(mktemp /tmp/sztestXXXXXX)"
41 cat >"$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>
47 EOF
48 for header in $sheaders; do
49     echo "#include <$header>" >>"$file.c"
50 done
51 for header in $lheaders; do
52     echo "#include \"$header\"" >>"$file.c"
53 done
54 cat >>"$file.c" <<EOF
55 int main(int argc, char **argv)
56 {
57 EOF
58 case "$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         ;;
68 esac
69 cat >>"$file.c" <<EOF
70     return(0);
71 }
72 EOF
73
74 if ! gcc "$@" -iquote. -g -Wall -o "$file" "$file.c"; then
75     rm -f "$file" "$file.c"
76     exit 1
77 fi
78 "$file"
79 rm "$file" "$file.c"