autoconf: do AC_RUN_IFELSE in сase of native compilation, otherwise AC_COMPILE_IFELSE

Viewed 173

There is modified configrue.ac from rsync:

if test x"$host_cpu" = x"x86_64"; then
    if test x"$host_cpu" = x"$build_cpu"; then
        AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include <stdio.h>
#include <immintrin.h>
/* some long C++ code here */
]], [[if (test_ssse3(42) != 42 || test_sse2(42) != 42 || test_avx2(42) != 42) exit(1);]])],[CXX_OK=yes],[CXX_OK=no])
    else
        AC_COMPILE_IFELSE(AC_LANG_PROGRAM([[#include <stdio.h>
#include <immintrin.h>
/* the same C++ code again */
]]),[CXX_OK=yes],[CXX_OK=no])
    fi
fi

How could this be improved? Is there a way to avoid duplicating C++ code?

1 Answers

Is there a way to avoid duplicating C++ code?

Sure. Autoconf is, after all, fundamentally a macro language. Instead of duplicating code, you can define and (re)use a macro that expands to that code. Either AC_DEFUN() or m4_define() will serve, but the former is a bit overkill for what I think you want. So,

m4_define([_EDO1_LONG_CODE], [[
// long C++ code ...
]])

You'll want to take a bit of care with quoting when you use it. You are using double-quoting to ensure that the program source is not subject to macro expansion, but where you actually want macro expansion, you'll need to reduce the quotation level by one, something like this:

if test x"$host_cpu" = x"x86_64"; then
    if test x"$host_cpu" = x"$build_cpu"; then
        AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include <stdio.h>
#include <immintrin.h>
]_EDO1_LONG_CODE], [[if (test_ssse3(42) != 42 || test_sse2(42) != 42 || test_avx2(42) != 42) exit(1);]])],[CXX_OK=yes],[CXX_OK=no])
    else
        AC_COMPILE_IFELSE(AC_LANG_PROGRAM([[#include <stdio.h>
#include <immintrin.h>
]_EDO1_LONG_CODE]),[CXX_OK=yes],[CXX_OK=no])
    fi
fi
Related