Hi Bruno,

Here is my interpretation of your work.  Alexandre, what do you think
about it?

This implementation requires CVS Autoconf.  Install it as
`configure.in' and CVS autoconf it to give it a try.


# ======================================== aclang.m4

# AC_LANG_BOOLEAN(PROLOGUE, EXPRESSION)
# -------------------------------------
# Produce a program that compiles with success iff the boolean EXPRESSION
# evaluates to true at compile time.
AC_DEFUN([AC_LANG_BOOLEAN],
[_AC_LANG_DISPATCH([$0], _AC_LANG, $@)])

define([AC_LANG_BOOLEAN(C)],
[AC_LANG_PROGRAM([$1], [int _array_ @<:@1 - 2 * !($2)@:>@])])

define([AC_LANG_BOOLEAN(C++)],
defn([AC_LANG_BOOLEAN(C)]))


# AC_LANG_COMPUTE(PROLOGUE, EXPRESSION)
# -------------------------------------
# Produce a program that saves the runtime evaluation of the integer
# EXPRESSION into `conftestdate'.
AC_DEFUN([AC_LANG_COMPUTE],
[_AC_LANG_DISPATCH([$0], _AC_LANG, $@)])

# We need `stdio.h' to open a `FILE', so the prologue defaults to the
# inclusion of `stdio.h'.
define([AC_LANG_COMPUTE(C)],
[AC_LANG_PROGRAM([m4_default([$1], [@%:@include "stdio.h"])],
[FILE *f = fopen ("conftestval", "w");
if (!f)
  exit (1);
fprintf (f, "%d\n", ($2));])])

define([AC_LANG_COMPUTE(C++)],
defn([AC_LANG_COMPUTE(C)]))



# ======================================== acgeneral.m4

# _AC_COMPUTE_INT_COMPILE(EXPRESSION, VARIABLE, [INCLUDES])
# ---------------------------------------------------------
# Compute the integer EXPRESSION and store the result in the VARIABLE.
# Works OK if cross compiling.
define([_AC_COMPUTE_INT_COMPILE],
[# Depending upon the size, compute the lo and hi bounds.
AC_COMPILE_IFELSE([AC_LANG_BOOLEAN([$3], [($1) >= 0])],
 [ac_lo=0 ac_try=0
  while true; do
    AC_COMPILE_IFELSE([AC_LANG_BOOLEAN([$3], [($1) <= $ac_try])],
                    [ac_hi=$ac_try; break],
                    [ac_lo=`expr $ac_try + 1` ac_try=`expr 2 '*' $ac_try + 1`])
  done],
 [ac_hi=-1 ac_try=-1
  while true; do
    AC_COMPILE_IFELSE([AC_LANG_BOOLEAN([$3], [($1) >= $ac_try])],
                      [ac_lo=$ac_try; break],
                      [ac_hi=`expr $ac_try - 1` ac_try=`expr 2 '*' $ac_try`])
  done])
# Binary search between lo and hi bounds.
while test "x$ac_lo" != "x$ac_hi"; do
  ac_try=`expr '(' $ac_hi - $ac_lo ')' / 2 + $ac_lo`
  AC_COMPILE_IFELSE([AC_LANG_BOOLEAN([$3], [($1) <= $ac_try])],
                     [ac_hi=$ac_try], [ac_lo=`expr $ac_try + 1`])
done
$2=$ac_lo[]dnl
])# _AC_COMPUTE_INT_COMPILE


# _AC_COMPUTE_INT_RUN(EXPRESSION, VARIABLE, [INCLUDES], [IF-FAILS])
# -----------------------------------------------------------------
# Store the evaluation of the integer EXPRESSION in VARIABLE.
define([_AC_COMPUTE_INT_RUN],
[AC_RUN_IFELSE([AC_LANG_COMPUTE([$3], [$1])],
               [$2=`cat conftestval`], [$4])])


# _AC_COMPUTE_INT(EXPRESSION, VARIABLE, INCLUDES, IF-FAILS)
# ---------------------------------------------------------
define([_AC_COMPUTE_INT],
[if test "$cross_compiling" = yes; then
  _AC_COMPUTE_INT_COMPILE([$1], [$2], [$3])
else
 _AC_COMPUTE_INT_RUN([$1], [$2], [$3], [$4])
fi[]dnl
])


# AC_CHECK_SIZEOF(TYPE, [CROSS-SIZE], [INCLUDES])
# -----------------------------------------------
# This macro will probably be obsoleted by the macros of Kaveh.  In
# addition `CHECK' is not a proper name (is not boolean).
AC_DEFUN([AC_CHECK_SIZEOF],
[AC_VAR_PUSHDEF([ac_Sizeof], [ac_cv_sizeof_$1])dnl
AC_CACHE_CHECK([size of $1], ac_Sizeof,
[_AC_COMPUTE_INT([sizeof($1)], ac_Sizeof, [AC_INCLUDES_DEFAULT([$3])])])
AC_DEFINE_UNQUOTED(AC_TR_CPP(sizeof_$1),
                   AC_VAR_GET(ac_Sizeof),
                   [The number of bytes in a `]$1['.])
AC_VAR_POPDEF([ac_Sizeof])dnl
])

# ======================================================================
AC_INIT
AC_PROG_CC
AC_PROG_CPP

# Not cross
_AC_COMPUTE_INT(5 * 3, foo)
echo "5 * 3 = $foo"
_AC_COMPUTE_INT(~17, bar)
echo "~17 = $bar"
AC_CHECK_SIZEOF(short)

# Cross.
cross_compiling=yes
_AC_COMPUTE_INT(5 * 3, foo)
echo "5 * 3 = $foo"
_AC_COMPUTE_INT(~17, bar)
echo "~17 = $bar"
AC_CHECK_SIZEOF(int)


Reply via email to