summaryrefslogtreecommitdiff
path: root/mkc_check_sizeof
blob: 3980b23bf4a1d61de30482c9a879e24f33c68e40 (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
#!/bin/sh

############################################################
# Copyright (c) 2009-2010 by Aleksey Cheusov
#
# See COPYRIGHT file in the distribution.
############################################################

set -e

LC_ALL=C
export LC_ALL

##################################################
# options
usage (){
    cat <<EOF
mkc_check_sizeof detects sizeof(type)
by compiling a test program.
mkc_check_sizeof doesn't run a generated executable
and therefore is ready for cross-compiling.

Usage: mkc_check_sizeof type [headers...]

Examples:
   mkc_check_sizeof 'void*'
   mkc_check_sizeof long-long
   mkc_check_sizeof size_t stdlib.h
EOF
}

if test $# -eq 0; then
    usage
    exit 1
fi
if test "_$1" = '_-h' -o "_$1" = '_--help'; then
    usage
    exit 0
fi

##################################################
# initializing

type=`echo $1 | tr ' -' '  '`
pathpart=sizeof_`echo $type | tr '* ' 'P~'`
shift

. mkc_check_common.sh

##################################################
# test

try_it (){
    # succeedes if size is bad
    # $1 - test size
    # $2.. - #includes
    sz=$1
    shift

    for f in $MKC_COMMON_HEADERS "$@"; do
	echo "#include <$f>"
    done > "$tmpc"
    cat >> "$tmpc" <<EOF
int main ()
{
   switch (0){
      case sizeof ($type): break;
      case $sz:            break;
   }
   return 0;
}
EOF

    if $CC -c -o "${tmpo}" $CPPFLAGS $CFLAGS "${tmpc}" 2>"${tmperr}"; then
    	return 0
    else
    	return 1
    fi
}

check_itself (){
    if try_it 2147483647 "$@"
    then
	for sz in 4 8 2 1 16 12    3 5 6 7 9 10 11 13 14 15; do
	    if try_it $sz "$@"
	    then
		:
	    else
		echo $sz
		return
	    fi
	done
    fi
    echo failed
}

check_and_cache "checking for sizeof ${type}" "$cache" "$@"

##################################################
# clean-ups

cleanup

##################################################
# finishing

printme "$ret\n" 1>&2
echo $ret