Rewrite FPTOOLS_CHECK_HTYPE to be cross-compilation-friendly
[ghc-base.git] / aclocal.m4
1 # FP_COMPUTE_INT(EXPRESSION, VARIABLE, INCLUDES, IF-FAILS)
2 # --------------------------------------------------------
3 # Assign VARIABLE the value of the compile-time EXPRESSION using INCLUDES for
4 # compilation. Execute IF-FAILS when unable to determine the value. Works for
5 # cross-compilation, too.
6 #
7 # Implementation note: We are lazy and use an internal autoconf macro, but it
8 # is supported in autoconf versions 2.50 up to the actual 2.57, so there is
9 # little risk.
10 AC_DEFUN([FP_COMPUTE_INT],
11 [_AC_COMPUTE_INT([$1], [$2], [$3], [$4])[]dnl
12 ])# FP_COMPUTE_INT
13
14
15 # FP_CHECK_CONST(EXPRESSION, [INCLUDES = DEFAULT-INCLUDES], [VALUE-IF-FAIL = -1])
16 # -------------------------------------------------------------------------------
17 # Defines CONST_EXPRESSION to the value of the compile-time EXPRESSION, using
18 # INCLUDES. If the value cannot be determined, use VALUE-IF-FAIL.
19 AC_DEFUN([FP_CHECK_CONST],
20 [AS_VAR_PUSHDEF([fp_Cache], [fp_cv_const_$1])[]dnl
21 AC_CACHE_CHECK([value of $1], fp_Cache,
22 [FP_COMPUTE_INT([$1], fp_check_const_result, [AC_INCLUDES_DEFAULT([$2])],
23                 [fp_check_const_result=m4_default([$3], ['-1'])])
24 AS_VAR_SET(fp_Cache, [$fp_check_const_result])])[]dnl
25 AC_DEFINE_UNQUOTED(AS_TR_CPP([CONST_$1]), AS_VAR_GET(fp_Cache), [The value of $1.])[]dnl
26 AS_VAR_POPDEF([fp_Cache])[]dnl
27 ])# FP_CHECK_CONST
28
29
30 # FP_CHECK_CONSTS_TEMPLATE(EXPRESSION...)
31 # ---------------------------------------
32 # autoheader helper for FP_CHECK_CONSTS
33 m4_define([FP_CHECK_CONSTS_TEMPLATE],
34 [AC_FOREACH([fp_Const], [$1],
35   [AH_TEMPLATE(AS_TR_CPP(CONST_[]fp_Const),
36                [The value of ]fp_Const[.])])[]dnl
37 ])# FP_CHECK_CONSTS_TEMPLATE
38
39
40 # FP_CHECK_CONSTS(EXPRESSION..., [INCLUDES = DEFAULT-INCLUDES], [VALUE-IF-FAIL = -1])
41 # -----------------------------------------------------------------------------------
42 # List version of FP_CHECK_CONST
43 AC_DEFUN([FP_CHECK_CONSTS],
44 [FP_CHECK_CONSTS_TEMPLATE([$1])dnl
45 for fp_const_name in $1
46 do
47 FP_CHECK_CONST([$fp_const_name], [$2], [$3])
48 done
49 ])# FP_CHECK_CONSTS
50
51
52 dnl FPTOOLS_HTYPE_INCLUDES
53 AC_DEFUN([FPTOOLS_HTYPE_INCLUDES],
54 [
55 #include <stdio.h>
56 #include <stddef.h>
57
58 #if HAVE_SYS_TYPES_H
59 # include <sys/types.h>
60 #endif
61
62 #if HAVE_UNISTD_H
63 # include <unistd.h>
64 #endif
65
66 #if HAVE_SYS_STAT_H
67 # include <sys/stat.h>
68 #endif
69
70 #if HAVE_FCNTL_H
71 # include <fcntl.h>
72 #endif
73
74 #if HAVE_SIGNAL_H
75 # include <signal.h>
76 #endif
77
78 #if HAVE_TIME_H
79 # include <time.h>
80 #endif
81
82 #if HAVE_TERMIOS_H
83 # include <termios.h>
84 #endif
85
86 #if HAVE_STRING_H
87 # include <string.h>
88 #endif
89
90 #if HAVE_CTYPE_H
91 # include <ctype.h>
92 #endif
93
94 #if HAVE_INTTYPES_H
95 # include <inttypes.h>
96 #else
97 # if HAVE_STDINT_H
98 #  include <stdint.h>
99 # endif
100 #endif
101
102 #if HAVE_SYS_RESOURCE_H
103 # include <sys/resource.h>
104 #endif
105
106 #include <stdlib.h>
107 ])
108
109
110 dnl ** Map an arithmetic C type to a Haskell type.
111 dnl    Based on autconf's AC_CHECK_SIZEOF.
112
113 dnl FPTOOLS_CHECK_HTYPE(TYPE [, DEFAULT_VALUE ])
114 AC_DEFUN([FPTOOLS_CHECK_HTYPE],[
115     changequote(<<, >>)
116     dnl The name to #define.
117     define(<<AC_TYPE_NAME>>, translit(htype_$1, [a-z *], [A-Z_P]))
118     dnl The cache variable names.
119     define(<<AC_CV_NAME>>, translit(fptools_cv_htype_$1, [ *], [_p]))
120     define(<<AC_CV_NAME_supported>>, translit(fptools_cv_htype_sup_$1, [ *], [_p]))
121     changequote([, ])
122
123     AC_MSG_CHECKING(Haskell type for $1)
124     AC_CACHE_VAL(AC_CV_NAME,[
125         AC_CV_NAME_supported=yes
126         AC_COMPUTE_INT([HTYPE_IS_INTEGRAL],
127                        [(($1)((int)(($1)1.4))) == (($1)1.4)],
128                        [FPTOOLS_HTYPE_INCLUDES],[AC_CV_NAME_supported=no])
129         if test "$AC_CV_NAME_supported" = "yes"
130         then
131             if test "$HTYPE_IS_INTEGRAL" -eq 0
132             then
133                 AC_COMPUTE_INT([HTYPE_IS_FLOAT],[sizeof($1) == sizeof(float)],
134                                [FPTOOLS_HTYPE_INCLUDES],
135                                [AC_CV_NAME_supported=no])
136                 AC_COMPUTE_INT([HTYPE_IS_DOUBLE],[sizeof($1) == sizeof(double)],
137                                [FPTOOLS_HTYPE_INCLUDES],
138                                [AC_CV_NAME_supported=no])
139                 AC_COMPUTE_INT([HTYPE_IS_LDOUBLE],[sizeof($1) == sizeof(long double)],
140                                [FPTOOLS_HTYPE_INCLUDES],
141                                [AC_CV_NAME_supported=no])
142                 if test "$HTYPE_IS_FLOAT" -eq 1
143                 then
144                     AC_CV_NAME=Float
145                 elif test "$HTYPE_IS_DOUBLE" -eq 1
146                 then
147                     AC_CV_NAME=Double
148                 elif test "$HTYPE_IS_LDOUBLE" -eq 1
149                 then
150                     AC_CV_NAME=LDouble
151                 else
152                     AC_CV_NAME_supported=no
153                 fi
154             else
155                 AC_COMPUTE_INT([HTYPE_IS_SIGNED],[(($1)(-1)) < (($1)0)],
156                                [FPTOOLS_HTYPE_INCLUDES],
157                                [AC_CV_NAME_supported=no])
158                 AC_COMPUTE_INT([HTYPE_SIZE],[sizeof($1) * 8],
159                                [FPTOOLS_HTYPE_INCLUDES],
160                                [AC_CV_NAME_supported=no])
161                 if test "$HTYPE_IS_SIGNED" -eq 0
162                 then
163                     AC_CV_NAME="Word$HTYPE_SIZE"
164                 else
165                     AC_CV_NAME="Int$HTYPE_SIZE"
166                 fi
167             fi
168         fi
169         ])
170     if test "$AC_CV_NAME_supported" = yes; then
171         AC_MSG_RESULT($AC_CV_NAME)
172         AC_DEFINE_UNQUOTED(AC_TYPE_NAME, $AC_CV_NAME,
173                            [Define to Haskell type for $1])
174     else
175         AC_CV_NAME=NotReallyAType
176         AC_MSG_RESULT([not supported])
177     fi
178     undefine([AC_TYPE_NAME])dnl
179     undefine([AC_CV_NAME])dnl
180     undefine([AC_CV_NAME_supported])dnl
181 ])
182
183
184 # FP_SEARCH_LIBS_PROTO(WHAT, PROTOTYPE, FUNCTION, SEARCH-LIBS,
185 #                [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND],
186 #                [OTHER-LIBRARIES])
187 # --------------------------------------------------------
188 # Search for a library defining FUNC, if it's not already available.
189 # This is a copy of the AC_SEARCH_LIBS definition, but extended to take
190 # the name of the thing we are looking for as its first argument, and
191 # prototype text as its second argument. It also calls AC_LANG_PROGRAM
192 # instead of AC_LANG_CALL
193 AC_DEFUN([FP_SEARCH_LIBS_PROTO],
194 [AS_VAR_PUSHDEF([ac_Search], [ac_cv_search_$1])dnl
195 AC_CACHE_CHECK([for library containing $1], [ac_Search],
196 [ac_func_search_save_LIBS=$LIBS
197 AC_LANG_CONFTEST([AC_LANG_PROGRAM([$2], [$3])])
198 for ac_lib in '' $4; do
199   if test -z "$ac_lib"; then
200     ac_res="none required"
201   else
202     ac_res=-l$ac_lib
203     LIBS="-l$ac_lib $7 $ac_func_search_save_LIBS"
204   fi
205   AC_LINK_IFELSE([], [AS_VAR_SET([ac_Search], [$ac_res])])
206   AS_VAR_SET_IF([ac_Search], [break])
207 done
208 AS_VAR_SET_IF([ac_Search], , [AS_VAR_SET([ac_Search], [no])])
209 rm conftest.$ac_ext
210 LIBS=$ac_func_search_save_LIBS])
211 ac_res=AS_VAR_GET([ac_Search])
212 AS_IF([test "$ac_res" != no],
213   [test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
214   $5],
215       [$6])dnl
216 AS_VAR_POPDEF([ac_Search])dnl
217 ])