Added silc.m4. Added SILC_SYSTEM_IS_CPU and SILC_CPU_FLAG
[silc.git] / silc.m4
1 #
2 #  silc.m4
3 #
4 #  Author: Pekka Riikonen <priikone@silcnet.org>
5 #
6 #  Copyright (C) 2007 Pekka Riikonen
7 #
8 #  This program is free software; you can redistribute it and/or modify
9 #  it under the terms of the GNU General Public License as published by
10 #  the Free Software Foundation; version 2 of the License.
11 #
12 #  This program is distributed in the hope that it will be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #  GNU General Public License for more details.
16 #
17
18 # Function to check if system has SMP kernel.
19 # Usage: SILC_SYSTEM_IS_SMP([ACTION-IF-FOUND] [, ACTION-IF-NOT-FOUND])
20 AC_DEFUN([SILC_SYSTEM_IS_SMP],
21 [
22   AC_MSG_CHECKING(whether system has SMP kernel)
23
24   case "$target" in
25     *-*-linux*)
26       # Take data from Linux /proc
27       if test -f /proc/stat; then
28         cpucount=`grep "^cpu" /proc/stat -c 2> /dev/null`
29         if test $cpucount -gt 1; then
30           AC_DEFINE([SILC_SMP], [], [SILC_SMP])
31           AC_MSG_RESULT(yes)
32           ifelse([$1], , :, [$1])
33         else
34           AC_MSG_RESULT(no)
35           ifelse([$2], , :, [$2])
36         fi
37       else
38         AC_MSG_RESULT(no)
39         ifelse([$2], , :, [$2])
40       fi
41       ;;
42
43     *-*-*bsd*)
44       # BSDs can have SMP info in sysctl 'kern.smp.cpus' variable
45       cpucount=`/sbin/sysctl kern.smp.cpus 2> /dev/null | \
46          cut -d'=' -f2 | cut -d' ' -f2`
47       if test $cpucount -gt 1; then
48         AC_DEFINE([SILC_SMP], [], [SILC_SMP])
49         AC_MSG_RESULT(yes)
50         ifelse([$1], , :, [$1])
51       else
52         AC_MSG_RESULT(no)
53         ifelse([$2], , :, [$2])
54       fi
55       ;;
56
57     *)
58       AC_MSG_RESULT(no)
59       ifelse([$2], , :, [$2])
60       ;;
61   esac
62 ])
63
64 # Function to check for CPU feature flags.
65 # Usage: SILC_CPU_FLAG(flag [, ACTION-IF-FOUND] [, ACTION-IF-NOT-FOUND])
66 AC_DEFUN([SILC_CPU_FLAG],
67 [
68   AC_MSG_CHECKING(whether CPU supports $1)
69
70   case "$target" in
71     *-*-linux*)
72       # Take data from Linux /proc
73       if test -f /proc/cpuinfo; then
74         cpuflags=`grep "^flags.*$1 " /proc/cpuinfo 2> /dev/null`
75         if test $? != 0; then
76           AC_MSG_RESULT(no)
77           ifelse([$3], , :, [$3])
78         else
79           AC_MSG_RESULT(yes)
80           ifelse([$2], , :, [$2])
81         fi
82       else
83         AC_MSG_RESULT(no)
84         ifelse([$3], , :, [$3])
85       fi
86       ;;
87
88     *-*-*bsd*)
89       # BSDs have some flags in sysctl 'machdep' variable
90       cpuflags=`/sbin/sysctl machdep 2> /dev/null | grep "\.$1.*.1"`
91       if test $? != 0; then
92         AC_MSG_RESULT(no)
93         ifelse([$3], , :, [$3])
94       else
95         AC_MSG_RESULT(yes)
96         ifelse([$2], , :, [$2])
97       fi
98       ;;
99
100     *)
101       AC_MSG_RESULT(no)
102       ifelse([$3], , :, [$3])
103       ;;
104   esac
105 ])
106
107 # Function to check if compiler flag works
108 # Usage: SILC_ADD_CFLAGS(FLAGS, [ACTION-IF-FAILED])
109 AC_DEFUN([SILC_ADD_CFLAGS],
110 [ tmp_CFLAGS="$CFLAGS"
111   CFLAGS="$CFLAGS $1"
112   AC_MSG_CHECKING(whether $CC accepts $1 flag)
113   AC_TRY_LINK([], [], [AC_MSG_RESULT(yes)], [AC_MSG_RESULT(no)
114                                        CFLAGS="$tmp_CFLAGS"
115                                        $2])
116   unset tmp_CFLAGS
117 ])
118
119 # Function to check if compiler flag works, destination specifiable
120 # Usage: SILC_ADD_CC_FLAGS(VAR, FLAGS, [ACTION-IF-FAILED])
121 AC_DEFUN([SILC_ADD_CC_FLAGS],
122 [ tmp_CFLAGS="$1_CFLAGS"
123   $1_CFLAGS="${$1_CFLAGS} $2"
124   AC_MSG_CHECKING(whether $CC accepts $2 flag)
125   AC_TRY_LINK([], [], [AC_MSG_RESULT(yes)], [AC_MSG_RESULT(no)
126                                        $1_CFLAGS="$tmp_CFLAGS"
127                                        $3])
128   unset tmp_CFLAGS
129 ])