Search code examples
cautotoolsautoconf

Check value of C #define with autoconf


I know I can use AC_CHECK_DECL / AC_CHECK_DECLS to check whether a set of headers provides a declaration for a specified identifier, but how can I check not only whether a macro is declared, but also whether its expansion meets my criteria? Specifically, I would like to check whether numa.h contains a macro definition equivalent to this ...

#define LIBNUMA_API_VERSION 2

... including the specific value "2".

UPDATE: <numa.h> header contains a definition such as #define LIBNUMA_API_VERSION 2 to declare it's version. C code that uses this header typically uses it like so:

#if defined(LIBNUMA_API_VERSION) && LIBNUMA_API_VERSION >= 2
....
#endif

I want to determine NUMA header version with autoconf, and define a macro that succinctly conveys whether NUMA version 2 is provided. i.e.:

if test "$have_numa_v2" = "yes" ; then
    AC_DEFINE(NUMA_VERSION_2, 1, [Determine whether NUMA v2 available)
fi

That could be used like so:

#ifdef NUMA_VERSION_2
....
#endif

Is it possible? I'm having trouble determining how I could set the value of variable have_numa_v2 variable in my Autoconf file.


Solution

  • You can use AC_COMPILE_IFELSE or AC_RUN_IFELSE with a suitably-structured test program to determine whether the macro is defined to a specific value you designate. For example, supposing that the current language is C:

    have_numa_v2=no
    AC_RUN_IFELSE([AC_LANG_PROGRAM([
    #include <numa.h>
    ],[
    #if LIBNUMA_API_VERSION != 2
    exit(1);
    #endif
    ])], [have_numa_v2=yes])
    

    That constructs a program whose return value depends on whether LIBNUMA_API_VERSION is defined as a macro, and if so, whether it expands to 2. If Autoconf cannot compile it (because, say, it cannot find numa.h) or if it exits with a status other than 0 then nothing else happens ($have_numa_v2 retains its assigned value of "no"). Otherwise, the assignment in the second argument is performed, and $have_numa_v2 ends up with the value "yes".

    For what it's worth, the source of the particular test program produced and used by that macro contains some Autoconf-standard macro definitions, plus this:

    #include <numa.h>
    
    int
    main ()
    {
    
    #if LIBNUMA_API_VERSION != 2
    exit(1);
    #endif
    
      ;
      return 0;
    }
    

    The version using AC_COMPILE_IFELSE would be similar, but built around using an #error preprocessor directive to make compilation fail if the macro is not defined to the specified value. That variation might be a better choice if you anticipate any possibility that your program will be cross-compiled for a foreign architecture.