Factor out compiler test program into a macro.

ok djm@
This commit is contained in:
Darren Tucker 2023-11-22 21:18:07 +11:00
parent de304c7631
commit ee0d305828
No known key found for this signature in database
1 changed files with 24 additions and 49 deletions

View File

@ -1,30 +1,26 @@
dnl OpenSSH-specific autoconf macros dnl OpenSSH-specific autoconf macros
dnl dnl
dnl OSSH_CHECK_CFLAG_COMPILE(check_flag[, define_flag]) dnl The test program that is used to try to trigger various compiler
dnl Check that $CC accepts a flag 'check_flag'. If it is supported append dnl behaviours.
dnl 'define_flag' to $CFLAGS. If 'define_flag' is not specified, then append AC_DEFUN([OSSH_COMPILER_FLAG_TEST_PROGRAM],
dnl 'check_flag'. [AC_LANG_SOURCE([[
AC_DEFUN([OSSH_CHECK_CFLAG_COMPILE], [{
AC_MSG_CHECKING([if $CC supports compile flag $1])
saved_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS $WERROR $1"
_define_flag="$2"
test "x$_define_flag" = "x" && _define_flag="$1"
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
/* Trivial function to help test for -fzero-call-used-regs */ /* Trivial function to help test for -fzero-call-used-regs */
int f(int n) {return rand() % n;} int f(int n) {return rand() % n;}
int main(int argc, char **argv) { int main(int argc, char **argv) {
(void)argv; (void)argv;
char b[256];
/* Some math to catch -ftrapv problems in the toolchain */ /* Some math to catch -ftrapv problems in the toolchain */
int i = 123 * argc, j = 456 + argc, k = 789 - argc; int i = 123 * argc, j = 456 + argc, k = 789 - argc;
float l = i * 2.1; float l = i * 2.1;
double m = l / 0.5; double m = l / 0.5;
long long int n = argc * 12345LL, o = 12345LL * (long long int)argc; long long int n = argc * 12345LL, o = 12345LL * (long long int)argc;
f(0); f(0);
printf("%d %d %d %f %f %lld %lld\n", i, j, k, l, m, n, o); snprintf(b, sizeof b, "%d %d %d %f %f %lld %lld\n", i,j,k,l,m,n,o);
write(1, b, 0);
/* /*
* Test fallthrough behaviour. clang 10's -Wimplicit-fallthrough does * Test fallthrough behaviour. clang 10's -Wimplicit-fallthrough does
* not understand comments and we don't use the "fallthrough" attribute * not understand comments and we don't use the "fallthrough" attribute
@ -37,7 +33,20 @@ int main(int argc, char **argv) {
} }
exit(0); exit(0);
} }
]])], ]])]
)
dnl OSSH_CHECK_CFLAG_COMPILE(check_flag[, define_flag])
dnl Check that $CC accepts a flag 'check_flag'. If it is supported append
dnl 'define_flag' to $CFLAGS. If 'define_flag' is not specified, then append
dnl 'check_flag'.
AC_DEFUN([OSSH_CHECK_CFLAG_COMPILE], [{
AC_MSG_CHECKING([if $CC supports compile flag $1])
saved_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS $WERROR $1"
_define_flag="$2"
test "x$_define_flag" = "x" && _define_flag="$1"
AC_COMPILE_IFELSE([OSSH_COMPILER_FLAG_TEST_PROGRAM],
[ [
if $ac_cv_path_EGREP -i "unrecognized option|warning.*ignored" conftest.err >/dev/null if $ac_cv_path_EGREP -i "unrecognized option|warning.*ignored" conftest.err >/dev/null
then then
@ -62,24 +71,7 @@ AC_DEFUN([OSSH_CHECK_CFLAG_LINK], [{
CFLAGS="$CFLAGS $WERROR $1" CFLAGS="$CFLAGS $WERROR $1"
_define_flag="$2" _define_flag="$2"
test "x$_define_flag" = "x" && _define_flag="$1" test "x$_define_flag" = "x" && _define_flag="$1"
AC_LINK_IFELSE([AC_LANG_SOURCE([[ AC_LINK_IFELSE([OSSH_COMPILER_FLAG_TEST_PROGRAM],
#include <stdlib.h>
#include <stdio.h>
/* Trivial function to help test for -fzero-call-used-regs */
int f(int n) {return rand() % n;}
int main(int argc, char **argv) {
(void)argv;
/* Some math to catch -ftrapv problems in the toolchain */
int i = 123 * argc, j = 456 + argc, k = 789 - argc;
float l = i * 2.1;
double m = l / 0.5;
long long int n = argc * 12345LL, o = 12345LL * (long long int)argc;
long long int p = n * o;
f(0);
printf("%d %d %d %f %f %lld %lld %lld\n", i, j, k, l, m, n, o, p);
exit(0);
}
]])],
[ [
if $ac_cv_path_EGREP -i "unrecognized option|warning.*ignored" conftest.err >/dev/null if $ac_cv_path_EGREP -i "unrecognized option|warning.*ignored" conftest.err >/dev/null
then then
@ -104,24 +96,7 @@ AC_DEFUN([OSSH_CHECK_LDFLAG_LINK], [{
LDFLAGS="$LDFLAGS $WERROR $1" LDFLAGS="$LDFLAGS $WERROR $1"
_define_flag="$2" _define_flag="$2"
test "x$_define_flag" = "x" && _define_flag="$1" test "x$_define_flag" = "x" && _define_flag="$1"
AC_LINK_IFELSE([AC_LANG_SOURCE([[ AC_LINK_IFELSE([OSSH_COMPILER_FLAG_TEST_PROGRAM],
#include <stdlib.h>
#include <stdio.h>
/* Trivial function to help test for -fzero-call-used-regs */
int f(int n) {return rand() % n;}
int main(int argc, char **argv) {
(void)argv;
/* Some math to catch -ftrapv problems in the toolchain */
int i = 123 * argc, j = 456 + argc, k = 789 - argc;
float l = i * 2.1;
double m = l / 0.5;
long long int n = argc * 12345LL, o = 12345LL * (long long int)argc;
long long p = n * o;
f(0);
printf("%d %d %d %f %f %lld %lld %lld\n", i, j, k, l, m, n, o, p);
exit(0);
}
]])],
[ [
if $ac_cv_path_EGREP -i "unrecognized option|warning.*ignored" conftest.err >/dev/null if $ac_cv_path_EGREP -i "unrecognized option|warning.*ignored" conftest.err >/dev/null
then then