Autoconf: check struct member type

259 Views Asked by At

I am new to autoconf so I would ask you how could I check if a struct member is declared with a particular type.

For example I should check if struct posix_acl.a_refcount is declared as refcount_t and not atomic_t.

I know AC functions as ac_fn_c_check_decl and ac_fn_c_check_member, but none that accomplish this task.

Thank you!

1

There are 1 best solutions below

0
AudioBubble On

Disclaimer: As there are no other answers at the time this answer is being written, this represents my best attempt to provide a solution, but you may need to adjust things to make it work for you. Caveat emptor.

You would need to use the AC_COMPILE_IFELSE macro with code that uses atomic_t, and if the compilation succeeds, then you're using atomic_t. As future-proofing, you might also add a test for refcount_t if the atomic_t test fails.

Example:

# _POSIX_ACL_REFCOUNT_T(type-to-check)
# ------------------------------------
# Checks whether the Linux kernel's `struct posix_acl'
# type uses `type-to-check' for its `a_refcount' member.
# Sets the shell variable `posix_acl_refcount_type' to
# `type-to-check' if that type is used, else the shell
# variable remains unset.
m4_define([_POSIX_ACL_REFCOUNT_T], [
 AC_REQUIRE([AC_PROG_CC])
 AC_MSG_CHECKING([whether struct posix_acl uses $1 for refcounts])
 AC_COMPILE_IFELSE(
  [AC_LANG_SOURCE(
   [#include <uapi/../linux/posix_acl.h>
    struct posix_acl acl;
    $1 v = acl.a_refcount;]
  )],
  [AC_MSG_RESULT([yes])
   AS_VAR_SET([posix_acl_refcount_type], [$1])],
  [AC_MSG_RESULT([no])
 )
])

_POSIX_ACL_REFCOUNT_T([atomic_t])
# If posix_acl_refcount_type isn't set, see if it works with refcount_t.
AS_VAR_SET_IF([posix_acl_refcount_type],
    [],
    [_POSIX_ACL_REFCOUNT_T([refcount_t])]
)
dnl
dnl Add future AS_VAR_SET_IF tests as shown above for the refcount type
dnl before the AS_VAR_SET_IF below, if necessary.
dnl
AS_VAR_SET_IF([posix_acl_refcount_type],
    [],
    [AC_MSG_FAILURE([struct posix_acl uses an unrecognized type for refcounts])]
)
AC_DEFINE([POSIX_ACL_REFCOUNT_T], [$posix_acl_refcount_type],
    [The type used for the a_refcount member of the Linux kernel's posix_acl struct.])

The tests assume that you already have a variable containing the kernel source directory, and the kernel source's include directory is specified in CPPFLAGS or CFLAGS prior to attempting the tests. You can add more tests at the position indicated, and if the resulting posix_acl_refcount_type shell variable is still not defined after all those tests, then the final AS_VAR_SET_IF invocation will invoke AC_MSG_FAILURE to stop configure with the specified error message.

Note that I used <uapi/../linux/posix_acl.h> to specifically target the kernel's linux/posix_acl.h header rather than the userspace API uapi/linux/posix_acl.h header installed in a system's include directory with the uapi/ stripped off, which may result in the compile tests above failing due to the missing struct posix_acl in the userspace API. This may not work the way I'd expect and may need modification.