planime: Fixed proper quoting of basename.
[utils.git] / sztest
... / ...
CommitLineData
1#!/bin/bash
2
3usage() {
4 echo "usage: sztest [-hab] [-H SYS-HEADER] [-L LOCAL-HEADER] TYPE [CCFLAGS...]"
5}
6
7mode=size
8sheaders=
9lheaders=
10while [ "${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
33done
34if [ $# -lt 1 ]; then
35 usage >&2
36 exit 1
37fi
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
48for header in $sheaders; do
49 echo "#include <$header>" >>"$file.c"
50done
51for header in $lheaders; do
52 echo "#include \"$header\"" >>"$file.c"
53done
54cat >>"$file.c" <<EOF
55int main(int argc, char **argv)
56{
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
70 return(0);
71}
72EOF
73
74if ! gcc "$@" -iquote. -g -Wall -o "$file" "$file.c"; then
75 rm -f "$file" "$file.c"
76 exit 1
77fi
78"$file"
79rm "$file" "$file.c"