rework config.m4
[m6w6/ext-psi] / config.psi_type.m4
1 psi_type_pair() { # (type, size)
2 local psi_type_name=`tr -cd A-Za-z <<<$1`
3 local psi_type_lower=`tr A-Z a-z <<<$psi_type_name`
4 case $psi_type_lower in
5 int*|uint*)
6 local psi_type_upper=`tr a-z A-Z <<<$psi_type_name`
7 local psi_type_bits=`expr $2 \* 8`
8 echo "PSI_T_${psi_type_upper}${psi_type_bits}, \"${psi_type_lower}${psi_type_bits}_t\""
9 ;;
10 struct*)
11 echo "PSI_T_STRUCT, \"$2\""
12 ;;
13 void)
14 echo "PSI_T_VOID, \"void\""
15 ;;
16 *)
17 echo "PSI_T_NAME, \"$psi_type_name\""
18 ;;
19 esac
20 }
21
22 PSI_TYPES=""
23 dnl PSI_TYPE(type name, basic type, includes)
24 AC_DEFUN(PSI_TYPE, [
25 ifdef(AS_TR_CPP(AC_TYPE_$1), AS_TR_CPP(AC_TYPE_$1))
26 AC_CHECK_SIZEOF($1, [], PSI_INCLUDES)
27 psi_basic_type=$2
28 case $psi_basic_type in
29 int)
30 AX_CHECK_SIGN($1, :, [
31 psi_basic_type=uint
32 ], $psi_header)
33 ;;
34 sint)
35 psi_basic_type=int
36 ;;
37 esac
38 if test "$2" && test "$ac_cv_sizeof_[]$1" -gt 0; then
39 AS_TR_SH(psi_basic_type_$1)=$psi_basic_type
40 PSI_TYPES="{`psi_type_pair $psi_basic_type $ac_cv_sizeof_[]$1`, \""$1"\"}, $PSI_TYPES"
41 fi
42 ])
43
44 PSI_CONSTS=""
45 # add_str_const(name, value)
46 add_str_const() {
47 PSI_CONSTS="{PSI_T_STRING, \"string\", \"psi\\\\$1\", $2, PSI_T_QUOTED_STRING}, $PSI_CONSTS"
48 }
49 # add_int_const(name, value)
50 add_int_const() {
51 PSI_CONSTS="{PSI_T_INT, \"int\", \"psi\\\\$1\", \"$2\", PSI_T_NUMBER}, $PSI_CONSTS"
52 }
53 dnl PSI_CONST(const name, type)
54 AC_DEFUN(PSI_CONST, [
55 AC_CACHE_CHECK(value of $1, psi_cv_const_$1, [
56 psi_const_val=
57 case $2 in
58 str*)
59 if test "$cross_compiling" = "yes"
60 then
61 AC_TRY_CPP(PSI_INCLUDES $1, psi_const_val=`eval "$ac_try|tail -n1"`, psi_const_val=)
62 else
63 PSI_COMPUTE_STR(psi_const_val, $1, PSI_INCLUDES)
64 fi
65 ;;
66 int)
67 AC_COMPUTE_INT(psi_const_val, $1, PSI_INCLUDES)
68 ;;
69 esac
70 psi_cv_const_$1=$psi_const_val
71 ])
72 if test "$psi_cv_const_$1"
73 then
74 case $2 in
75 str*)
76 add_str_const "$1" "$psi_cv_const_$1"
77 ;;
78 int)
79 add_int_const "$1" "$psi_cv_const_$1"
80 ;;
81 esac
82 fi
83 ])
84
85 PSI_DECLS=
86 # add_decl(ret type, symbol, args)
87 add_decl() {
88 local size
89 eval size=\$ac_cv_sizeof_$1
90 PSI_DECLS="{`psi_type_pair \"$1\" \"$size\"`, \"$2\"}, $PSI_DECLS"
91 }
92 PSI_REDIR=
93 # add_redir(symbol, macro=symbol)
94 add_redir() {
95 local sym=$2
96 test -z "$sym" && sym=$1
97 PSI_REDIR="{\"$1\", (void (*)(void)) $sym}, $PSI_REDIR"
98 }
99 # arg_names(typed args)
100 arg_names() {
101 AS_ECHO_N([$1]) \
102 | tr -cd '()' \
103 | $AWK -F, '{for (i=1;i<NF;++i) print $i ","; print $NF}' \
104 | $AWK '{print $NF}' \
105 | xargs
106 }
107 PSI_MACROS=
108 # gen_macro(ret type, symbol, args)
109 gen_macro() {
110 local dargs="$3"
111 test -z "$3" && dargs="()"
112 AS_ECHO_N(["static $1 psi_macro_$2$dargs {"])
113 if test "$1" != "void"; then
114 AS_ECHO_N(["return "])
115 fi
116 arg_names "$3"
117 AS_ECHO_N(["$2`arg_names \"$3\"`;}"])
118 }
119 # add_macro(ret type, symbol, args)
120 add_macro() {
121 PSI_MACROS="`gen_macro \"$1\" \"$2\" \"$3\"` $PSI_MACROS"
122 add_redir "$2" "psi_macro_$2"
123 }
124
125 dnl PSI_MACRO(macro, decl args, action-if-true)
126 AC_DEFUN(PSI_MACRO, [
127 AC_CHECK_DECL($1$2, $3, [], PSI_INCLUDES)
128 ])
129
130 dnl PSI_FUNC(fn, action-if-yes, action-if-no)
131 AC_DEFUN(PSI_FUNC, [
132 AC_REQUIRE([AC_PROG_NM])
133 AC_REQUIRE([AC_PROG_AWK])
134 psi_symbol=$1
135 AC_CACHE_CHECK(for $1, psi_cv_fn_$1, [
136 psi_symbol_redirect=
137 AC_TRY_LINK(PSI_INCLUDES, [
138 void (*fn)(void) = (void (*)(void)) $psi_symbol;
139 ], [
140 psi_symbol_redirect=`$NM -g conftest$ac_exeext | $AWK -F" *|@" '/ U .*$1.*/ {print$[]3; exit}'`
141 ])
142 psi_cv_fn_$1=$psi_symbol_redirect
143 ])
144 case "$psi_cv_fn_$1" in
145 "") $3 ;;
146 "$psi_symbol"|"_$psi_symbol") $2 ;;
147 *) add_redir "$psi_symbol"; $2 ;;
148 esac
149 ])
150
151 dnl PSI_EXTVAR(var, type)
152 AC_DEFUN(PSI_EXTVAR, [
153 AC_CHECK_DECL($1, [
154 add_macro "$2" "$1"
155 ], [], PSI_INCLUDES)
156 ])
157
158
159 dnl PSI_DECL(ret type, func, args)
160 AC_DEFUN(PSI_DECL, [
161 PSI_FUNC($2, [
162 add_decl "$1" "$2" "$3"
163 ], [
164 PSI_MACRO($2, $3, [
165 add_macro "$1" "$2" "$3"
166 add_decl "$1" "$2" "$3"
167 ])
168 ])
169 ])
170
171 AC_DEFUN(PSI_STRUCT_MEMBER, [
172 psi_member_name=
173 psi_member_type=
174 m4_map_args_w([$1], [
175 psi_member_type="$psi_member_type $psi_member_name"
176 psi_member_name=], [
177 ])
178 AC_MSG_RESULT([member=$psi_member_name - $psi_member_type])
179 ])
180
181 AC_DEFUN(PSI_STRUCT, [
182 AC_CHECK_SIZEOF($1, [], PSI_INCLUDES)
183 m4_map_args([PSI_STRUCT_MEMBER], $2)
184 exit
185 ])
186
187 AC_DEFUN(PSI_CHECK_STD_TYPES, [
188 AC_CHECK_HEADERS(stdint.h)
189
190 AC_TYPE_INT8_T
191 AC_CHECK_SIZEOF(int8_t)
192 AC_CHECK_ALIGNOF(int8_t)
193 AC_TYPE_UINT8_T
194 AC_CHECK_SIZEOF(uint8_t)
195 AC_CHECK_ALIGNOF(uint8_t)
196 AC_TYPE_INT16_T
197 AC_CHECK_SIZEOF(int16_t)
198 AC_CHECK_ALIGNOF(int16_t)
199 AC_TYPE_UINT16_T
200 AC_CHECK_SIZEOF(uint16_t)
201 AC_CHECK_ALIGNOF(uint16_t)
202 AC_TYPE_INT32_T
203 AC_CHECK_SIZEOF(int32_t)
204 AC_CHECK_ALIGNOF(int32_t)
205 AC_TYPE_UINT32_T
206 AC_CHECK_SIZEOF(uint32_t)
207 AC_CHECK_ALIGNOF(uint32_t)
208 AC_TYPE_INT64_T
209 AC_CHECK_SIZEOF(int64_t)
210 AC_CHECK_ALIGNOF(int64_t)
211 AC_TYPE_UINT64_T
212 AC_CHECK_SIZEOF(uint64_t)
213 AC_CHECK_ALIGNOF(uint64_t)
214
215 PSI_TYPE(float)
216 AC_CHECK_ALIGNOF(float)
217 PSI_TYPE(double)
218 AC_CHECK_ALIGNOF(double)
219 PSI_TYPE(void *)
220 AC_CHECK_ALIGNOF(void *)
221
222 PSI_TYPE(char, int)
223 PSI_TYPE(short, int)
224 PSI_TYPE(int, int)
225 PSI_TYPE(long, int)
226 ])