I have been updating my build tools to optionally use autotools (autoconfig/automake/libtool etc.).
As part of this change I have written a couple of M4 macros. This not being something I have done before. Any input is appreciated on style or if things can be done better.
A macro for detecting what version of C++ is available and defining the correct flags:
AC_DEFUN(
[AX_FUNC_THOR_LANG_FLAG],
[
minLangFeature=$1
AS_IF([test "$2" = ""], [maxLangFeature=17], [maxLangFeature=$2])
AS_IF([test $minLangFeature -gt $maxLangFeature], AC_MSG_ERROR([Invalid Language Value],[1]))
AC_LANG(C++)
CXXMaxLanguage=03
CXXExpLanguage=03
AX_CHECK_COMPILE_FLAG([-std=c++11], [AC_SUBST([CXXMaxLanguage],11) AC_SUBST([StdFlag11],[-std=c++11])])
AX_CHECK_COMPILE_FLAG([-std=c++14], [AC_SUBST([CXXMaxLanguage],14) AC_SUBST([StdFlag14],[-std=c++14])])
AX_CHECK_COMPILE_FLAG([-std=c++17], [AC_SUBST([CXXMaxLanguage],17) AC_SUBST([StdFlag17],[-std=c++17])])
AX_CHECK_COMPILE_FLAG([-std=c++1x], [AC_SUBST([CXXExpLanguage],11) AC_SUBST([ExpFlag11],[-std=c++1x])])
AX_CHECK_COMPILE_FLAG([-std=c++1y], [AC_SUBST([CXXExpLanguage],14) AC_SUBST([ExpFlag14],[-std=c++1y])])
AX_CHECK_COMPILE_FLAG([-std=c++1z], [AC_SUBST([CXXExpLanguage],17) AC_SUBST([ExpFlag17],[-std=c++1z])])
#CXX_STD_FLAG
#CXXSTDVER
AS_IF(
[test $minLangFeature -le $CXXMaxLanguage],
[
AS_IF(
[test $maxLangFeature -le $CXXMaxLanguage],
[AC_SUBST([CXXSTDVER], [$maxLangFeature])],
[AC_SUBST([CXXSTDVER], [$CXXMaxLanguage])]
)
AC_SUBST([CXX_STD_FLAG], [$(eval echo "\${StdFlag${CXXSTDVER}}")])
],
[AS_IF(
[test $minLangFeature -le $CXXExpLanguage],
[
AS_IF(
[test $maxLangFeature -le $CXXExpLanguage],
[AC_SUBST([CXXSTDVER], [$maxLangFeature])],
[AC_SUBST([CXXSTDVER], [$CXXExpLanguage])]
)
AC_SUBST([CXX_STD_FLAG], [$(eval echo "\${ExpFlag${CXXSTDVER}}")])
],
[AC_MSG_ERROR([
Error: Need C++${minLangFeature} but the compiler only supports ${CXXMaxLanguage} (Experimental ${CXXExpLanguage})
], [1])]
)]
)
]
)
Usage:
AX_FUNC_THOR_LANG_FLAG(<min acceptable C++> [, <max acceptable C++>])
If you don't specify a max then 17
is used as the default. It works out what flags are available to modify the behavior of the C++ compiler to a different version of the standard and then defines the two macros:
CXXSTDVER /* Will hold 03 11 14 or 17 */
CXX_STD_FLAG /* Will hold the flag needed be the C++ compiler */
It considers -std=c++1[147]
as standard version and will use these if they satisfy the minimum requirements. It considers -std=c++1[xyz]
as experimental versions and will only use these if the minimum requirements cannot be achieved by using the standard version flags.