Skip to content
Advertisement

How to print value of C macro in configure

I have configure.in script. I want to invoke configure ( after autoheader && autoconf ) and see value of PAGE_SHIFT macro from system header page_types.h. Something like this:

checking PAGE_SHIFT... 12

I’ve tried those:

1.

AC_CHECK_DECL([PAGE_SHIFT],[HAVE_PAGE_SHIFT=$PAGE_SHIFT],[],
[#include <linux/module.h>])
AC_MSG_NOTICE([${HAVE_PAGE_SHIFT}])

Result: macro PAGE_SHIFT was detected, but haven’t been printed

checking whether PAGE_SHIFT is declared... yes
configure:

2.

HAVE_PAGE_SHIFT="Unknown"
AC_MSG_CHECKING([PAGE_SHIFT])
AC_COMPILE_IFELSE(
[
AC_LANG_PROGRAM([
#include <linux/module.h>
#include <stdio.h>
],
[
printf("%dn", PAGE_SHIFT);
])],
[HAVE_PAGE_SHIFT="$PAGE_SHIFT"], [])
AC_MSG_RESULT([${HAVE_PAGE_SHIFT}])

Result: doesn’t work

checking PAGE_SHIFT... 

3.

AC_MSG_CHECKING(['PAGE_SHIFT' value])
AC_RUN_IFELSE(
[
#include <linux/module.h>
#include <stdio.h>
int
main ()
{

    printf("%dn", PAGE_SHIFT);
    return 0;
}
]
)

Result: works, but can’t be used with cross-compile

checking 'PAGE_SHIFT' value... 12
  1. method was suggested by Brett Hale

Autoconf will generate something like this:

#include <linux/module.h>
static long int longval () { return PAGE_SHIFT; }
static unsigned long int ulongval () { return PAGE_SHIFT; }
#include <stdio.h>
#include <stdlib.h>

Result: doesn’t work with kernel headers

In file included from /usr/include/stdlib.h:314:0,
             from conftest.c:125:
/usr/include/x86_64-linux-gnu/sys/types.h:44:18: error: conflicting types for 'loff_t'
typedef __loff_t loff_t;
              ^
In file included from /lib/modules/4.2.0-18-generic/build/arch/x86/include/asm/page_types.h:5:0,
             from [...],
             from conftest.c:121:
/lib/modules/4.2.0-18-generic/build/include/linux/types.h:45:26: note: previous declaration of 'loff_t' was here
typedef __kernel_loff_t  loff_t;

Autoconf version: autoconf (GNU Autoconf) 2.69

Advertisement

Answer

I would recommend using the AC_COMPUTE_INT macro. e.g.,

AC_MSG_CHECKING(['PAGE_SHIFT' value])

AC_COMPUTE_INT([PAGE_SHIFT_VAL], [PAGE_SHIFT], [[#include <linux/module.h>]],
  AC_MSG_FAILURE([linux/module.h: could not determine 'PAGE_SHIFT' value]))

AC_MSG_RESULT([$PAGE_SHIFT_VAL])

Alternatively, you can replace the FAILURE macro with PAGE_SHIFT_VAL=0 and test that value for (0) if the error is recoverable.

Note that there’s nothing magical about the variable name PAGE_SHIFT_VAL, in this context; you can use another name. You still need the AC_SUBST if you want this value substituted in generated files, like config.h, or those listed in AC_CONFIG_FILES.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement