xref: /freebsd/crypto/openssh/m4/openssh.m4 (revision cb7aa33ac6cd46a5434798e50363136e64f3ae98)
1dnl OpenSSH-specific autoconf macros
2dnl
3
4dnl OSSH_CHECK_CFLAG_COMPILE(check_flag[, define_flag])
5dnl Check that $CC accepts a flag 'check_flag'. If it is supported append
6dnl 'define_flag' to $CFLAGS. If 'define_flag' is not specified, then append
7dnl 'check_flag'.
8AC_DEFUN([OSSH_CHECK_CFLAG_COMPILE], [{
9	AC_MSG_CHECKING([if $CC supports compile flag $1])
10	saved_CFLAGS="$CFLAGS"
11	CFLAGS="$CFLAGS $WERROR $1"
12	_define_flag="$2"
13	test "x$_define_flag" = "x" && _define_flag="$1"
14	AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
15#include <stdlib.h>
16#include <stdio.h>
17/* Trivial function to help test for -fzero-call-used-regs */
18void f(int n) {}
19int main(int argc, char **argv) {
20	(void)argv;
21	/* Some math to catch -ftrapv problems in the toolchain */
22	int i = 123 * argc, j = 456 + argc, k = 789 - argc;
23	float l = i * 2.1;
24	double m = l / 0.5;
25	long long int n = argc * 12345LL, o = 12345LL * (long long int)argc;
26	f(0);
27	printf("%d %d %d %f %f %lld %lld\n", i, j, k, l, m, n, o);
28	/*
29	 * Test fallthrough behaviour.  clang 10's -Wimplicit-fallthrough does
30	 * not understand comments and we don't use the "fallthrough" attribute
31	 * that it's looking for.
32	 */
33	switch(i){
34	case 0: j += i;
35		/* FALLTHROUGH */
36	default: j += k;
37	}
38	exit(0);
39}
40	]])],
41		[
42if $ac_cv_path_EGREP -i "unrecognized option|warning.*ignored" conftest.err >/dev/null
43then
44		AC_MSG_RESULT([no])
45		CFLAGS="$saved_CFLAGS"
46else
47		AC_MSG_RESULT([yes])
48		 CFLAGS="$saved_CFLAGS $_define_flag"
49fi],
50		[ AC_MSG_RESULT([no])
51		  CFLAGS="$saved_CFLAGS" ]
52	)
53}])
54
55dnl OSSH_CHECK_CFLAG_LINK(check_flag[, define_flag])
56dnl Check that $CC accepts a flag 'check_flag'. If it is supported append
57dnl 'define_flag' to $CFLAGS. If 'define_flag' is not specified, then append
58dnl 'check_flag'.
59AC_DEFUN([OSSH_CHECK_CFLAG_LINK], [{
60	AC_MSG_CHECKING([if $CC supports compile flag $1 and linking succeeds])
61	saved_CFLAGS="$CFLAGS"
62	CFLAGS="$CFLAGS $WERROR $1"
63	_define_flag="$2"
64	test "x$_define_flag" = "x" && _define_flag="$1"
65	AC_LINK_IFELSE([AC_LANG_SOURCE([[
66#include <stdlib.h>
67#include <stdio.h>
68int main(int argc, char **argv) {
69	(void)argv;
70	/* Some math to catch -ftrapv problems in the toolchain */
71	int i = 123 * argc, j = 456 + argc, k = 789 - argc;
72	float l = i * 2.1;
73	double m = l / 0.5;
74	long long int n = argc * 12345LL, o = 12345LL * (long long int)argc;
75	long long int p = n * o;
76	printf("%d %d %d %f %f %lld %lld %lld\n", i, j, k, l, m, n, o, p);
77	exit(0);
78}
79	]])],
80		[
81if $ac_cv_path_EGREP -i "unrecognized option|warning.*ignored" conftest.err >/dev/null
82then
83		AC_MSG_RESULT([no])
84		CFLAGS="$saved_CFLAGS"
85else
86		AC_MSG_RESULT([yes])
87		 CFLAGS="$saved_CFLAGS $_define_flag"
88fi],
89		[ AC_MSG_RESULT([no])
90		  CFLAGS="$saved_CFLAGS" ]
91	)
92}])
93
94dnl OSSH_CHECK_LDFLAG_LINK(check_flag[, define_flag])
95dnl Check that $LD accepts a flag 'check_flag'. If it is supported append
96dnl 'define_flag' to $LDFLAGS. If 'define_flag' is not specified, then append
97dnl 'check_flag'.
98AC_DEFUN([OSSH_CHECK_LDFLAG_LINK], [{
99	AC_MSG_CHECKING([if $LD supports link flag $1])
100	saved_LDFLAGS="$LDFLAGS"
101	LDFLAGS="$LDFLAGS $WERROR $1"
102	_define_flag="$2"
103	test "x$_define_flag" = "x" && _define_flag="$1"
104	AC_LINK_IFELSE([AC_LANG_SOURCE([[
105#include <stdlib.h>
106#include <stdio.h>
107int main(int argc, char **argv) {
108	(void)argv;
109	/* Some math to catch -ftrapv problems in the toolchain */
110	int i = 123 * argc, j = 456 + argc, k = 789 - argc;
111	float l = i * 2.1;
112	double m = l / 0.5;
113	long long int n = argc * 12345LL, o = 12345LL * (long long int)argc;
114	long long p = n * o;
115	printf("%d %d %d %f %f %lld %lld %lld\n", i, j, k, l, m, n, o, p);
116	exit(0);
117}
118		]])],
119		[
120if $ac_cv_path_EGREP -i "unrecognized option|warning.*ignored" conftest.err >/dev/null
121then
122		  AC_MSG_RESULT([no])
123		  LDFLAGS="$saved_LDFLAGS"
124else
125		  AC_MSG_RESULT([yes])
126		  LDFLAGS="$saved_LDFLAGS $_define_flag"
127fi		],
128		[ AC_MSG_RESULT([no])
129		  LDFLAGS="$saved_LDFLAGS" ]
130	)
131}])
132
133dnl OSSH_CHECK_HEADER_FOR_FIELD(field, header, symbol)
134dnl Does AC_EGREP_HEADER on 'header' for the string 'field'
135dnl If found, set 'symbol' to be defined. Cache the result.
136dnl TODO: This is not foolproof, better to compile and read from there
137AC_DEFUN([OSSH_CHECK_HEADER_FOR_FIELD], [
138# look for field '$1' in header '$2'
139	dnl This strips characters illegal to m4 from the header filename
140	ossh_safe=`echo "$2" | sed 'y%./+-%__p_%'`
141	dnl
142	ossh_varname="ossh_cv_$ossh_safe""_has_"$1
143	AC_MSG_CHECKING(for $1 field in $2)
144	AC_CACHE_VAL($ossh_varname, [
145		AC_EGREP_HEADER($1, $2, [ dnl
146			eval "$ossh_varname=yes" dnl
147		], [ dnl
148			eval "$ossh_varname=no" dnl
149		]) dnl
150	])
151	ossh_result=`eval 'echo $'"$ossh_varname"`
152	if test -n "`echo $ossh_varname`"; then
153		AC_MSG_RESULT($ossh_result)
154		if test "x$ossh_result" = "xyes"; then
155			AC_DEFINE($3, 1, [Define if you have $1 in $2])
156		fi
157	else
158		AC_MSG_RESULT(no)
159	fi
160])
161
162dnl Check for socklen_t: historically on BSD it is an int, and in
163dnl POSIX 1g it is a type of its own, but some platforms use different
164dnl types for the argument to getsockopt, getpeername, etc.  So we
165dnl have to test to find something that will work.
166AC_DEFUN([TYPE_SOCKLEN_T],
167[
168   AC_CHECK_TYPE([socklen_t], ,[
169      AC_MSG_CHECKING([for socklen_t equivalent])
170      AC_CACHE_VAL([curl_cv_socklen_t_equiv],
171      [
172	 # Systems have either "struct sockaddr *" or
173	 # "void *" as the second argument to getpeername
174	 curl_cv_socklen_t_equiv=
175	 for arg2 in "struct sockaddr" void; do
176	    for t in int size_t unsigned long "unsigned long"; do
177	       AC_COMPILE_IFELSE([
178		  AC_LANG_PROGRAM([[
179		    #include <sys/types.h>
180		    #include <sys/socket.h>
181		    int getpeername (int, $arg2 *, $t *);
182		  ]], [[
183		    $t len;
184		    getpeername(0,0,&len);
185		  ]])
186	       ],[
187		  curl_cv_socklen_t_equiv="$t"
188		  break
189	       ])
190	    done
191	 done
192
193	 if test "x$curl_cv_socklen_t_equiv" = x; then
194	    AC_MSG_ERROR([Cannot find a type to use in place of socklen_t])
195	 fi
196      ])
197      AC_MSG_RESULT($curl_cv_socklen_t_equiv)
198      AC_DEFINE_UNQUOTED(socklen_t, $curl_cv_socklen_t_equiv,
199			[type to use in place of socklen_t if not defined])],
200      [#include <sys/types.h>
201#include <sys/socket.h>])
202])
203
204