Search code examples
autotoolsautoconf

How do I convert architecture-based checks to autoconf?


I'm working on trying to convert a project to autoconf which has a hand-written configure script. In this script, there are several occasions where variables are set based on CPU architecture, such as in the example provided at the bottom, which are later subtituted into the makefile.

The old script currently runs config.guess to get the platform, and derives the architecture from that, using these two pieces of information to influence the logic.

Is there a simpler way that you are "supposed" to do this in autoconf?

Example:

# Auto-vectorization
VECFLAGS=
if $VECTORIZE
then
    case "x$ARCH" in
    xx86_64) VECFLAGS="-msse2 -mfpmath=sse";;
    x*86)    VECFLAGS="-msse -mfpmath=sse";;
    xppc)    VECFLAGS=-maltivec ;;
    esac

    case "x$CC_VER" in
    x4.*) VECFLAGS="$FASTMATH -ftree-vectorize $VECFLAGS"
          if [ $VECTORIZE_LEVEL != 0 ]
          then
              VECFLAGS="$VECFLAGS -ftree-vectorizer-verbose=$VECTORIZE_LEVEL"
          fi
          echo "     Auto-vectorization, verbosity level $VECTORIZE_LEVEL"
          ;;
    *) echo "     Not GCC 4+, won't try auto-vectorization"
    esac
fi
CFLAGSEXTRA="$CFLAGSEXTRA $VECFLAGS"

Solution

  • Autoconf has a few standard macros that select compiler flags for specific purposes, but for the most part, if your software requires special compilation flags that vary by CPU or by OS then those details are specific to your software and its build system. I don't know what, exactly, Autoconf could provide to help you with those.

    It does, however, have some help for determining the details of the OS and architecture on which to base such decisions. Specifically, the AC_CANONICAL_HOST macro will cause the host_cpu and host_os variables to be defined appropriately within configure, where "appropriately" means based either on config.guess or on a --build or --host argument specified explicitly to configure.

    You may also find that some of the third-party macros in the Autoconf Archive provide access to additional details that the build system wants to take into account.

    Additionally, I don't think it's what you mean, but Autoconf does have some macros that can help you build portable shell case and if statements (AS_CASE, AS_IF), such as you present in the example code in the question. I tend to use them, but I am sympathetic to those who find them ugly / unwieldy / etc..