Spell occurred right
[tor.git] / acinclude.m4
blob8782a3eeaa6fa5f03ea11e77051b1e5f877b6df5
1 dnl Helper macros for Tor configure.ac
2 dnl Copyright (c) 2001-2004, Roger Dingledine
3 dnl Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson
4 dnl Copyright (c) 2007-2008, Roger Dingledine, Nick Mathewson
5 dnl Copyright (c) 2007-2015, The Tor Project, Inc.
6 dnl See LICENSE for licensing information
8 AC_DEFUN([TOR_EXTEND_CODEPATH],
10   if test -d "$1/lib"; then
11     LDFLAGS="-L$1/lib $LDFLAGS"
12   else
13     LDFLAGS="-L$1 $LDFLAGS"
14   fi
15   if test -d "$1/include"; then
16     CPPFLAGS="-I$1/include $CPPFLAGS"
17   else
18     CPPFLAGS="-I$1 $CPPFLAGS"
19   fi
22 AC_DEFUN([TOR_DEFINE_CODEPATH],
24   if test x$1 = "x(system)"; then
25     TOR_LDFLAGS_$2=""
26     TOR_CPPFLAGS_$2=""
27   else
28    if test -d "$1/lib"; then
29      TOR_LDFLAGS_$2="-L$1/lib"
30      TOR_LIBDIR_$2="$1/lib"
31    else
32      TOR_LDFLAGS_$2="-L$1"
33      TOR_LIBDIR_$2="$1"
34    fi
35    if test -d "$1/include"; then
36      TOR_CPPFLAGS_$2="-I$1/include"
37    else
38      TOR_CPPFLAGS_$2="-I$1"
39    fi
40   fi
41   AC_SUBST(TOR_CPPFLAGS_$2)
42   AC_SUBST(TOR_LDFLAGS_$2)
45 dnl 1:flags
46 dnl 2:also try to link (yes: non-empty string)
47 dnl   will set yes or no in $tor_can_link_$1 (as modified by AS_VAR_PUSHDEF)
48 AC_DEFUN([TOR_CHECK_CFLAGS], [
49   AS_VAR_PUSHDEF([VAR],[tor_cv_cflags_$1])
50   AC_CACHE_CHECK([whether the compiler accepts $1], VAR, [
51     tor_saved_CFLAGS="$CFLAGS"
52     CFLAGS="$CFLAGS -pedantic -Werror $1"
53     AC_TRY_COMPILE([], [return 0;],
54                    [AS_VAR_SET(VAR,yes)],
55                    [AS_VAR_SET(VAR,no)])
56     if test x$2 != x; then
57       AS_VAR_PUSHDEF([can_link],[tor_can_link_$1])
58       AC_TRY_LINK([], [return 0;],
59                   [AS_VAR_SET(can_link,yes)],
60                   [AS_VAR_SET(can_link,no)])
61       AS_VAR_POPDEF([can_link])
62     fi
63     CFLAGS="$tor_saved_CFLAGS"
64   ])
65   if test x$VAR = xyes; then
66     CFLAGS="$CFLAGS $1"
67   fi
68   AS_VAR_POPDEF([VAR])
71 dnl 1:flags
72 dnl 2:extra ldflags
73 dnl 3:extra libraries
74 AC_DEFUN([TOR_CHECK_LDFLAGS], [
75   AS_VAR_PUSHDEF([VAR],[tor_cv_ldflags_$1])
76   AC_CACHE_CHECK([whether the linker accepts $1], VAR, [
77     tor_saved_CFLAGS="$CFLAGS"
78     tor_saved_LDFLAGS="$LDFLAGS"
79     tor_saved_LIBS="$LIBS"
80     CFLAGS="$CFLAGS -pedantic -Werror"
81     LDFLAGS="$LDFLAGS $2 $1"
82     LIBS="$LIBS $3"
83     AC_RUN_IFELSE([AC_LANG_PROGRAM([#include <stdio.h>], [fputs("", stdout)])],
84                   [AS_VAR_SET(VAR,yes)],
85                   [AS_VAR_SET(VAR,no)],
86                   [AC_TRY_LINK([], [return 0;],
87                                    [AS_VAR_SET(VAR,yes)],
88                                    [AS_VAR_SET(VAR,no)])])
89     CFLAGS="$tor_saved_CFLAGS"
90     LDFLAGS="$tor_saved_LDFLAGS"
91     LIBS="$tor_saved_LIBS"
92   ])
93   if test x$VAR = xyes; then
94     LDFLAGS="$LDFLAGS $1"
95   fi
96   AS_VAR_POPDEF([VAR])
99 dnl 1:libname
100 AC_DEFUN([TOR_WARN_MISSING_LIB], [
101 h=""
102 if test x$2 = xdevpkg; then
103   h=" headers for"
105 if test -f /etc/debian_version && test x"$tor_$1_$2_debian" != x; then
106   AC_WARN([On Debian, you can install$h $1 using "apt-get install $tor_$1_$2_debian"])
107   if test x"$tor_$1_$2_debian" != x"$tor_$1_devpkg_debian"; then 
108     AC_WARN([   You will probably need $tor_$1_devpkg_debian too.])
109   fi 
111 if test -f /etc/fedora-release && test x"$tor_$1_$2_redhat" != x; then
112   AC_WARN([On Fedora Core, you can install$h $1 using "yum install $tor_$1_$2_redhat"])
113   if test x"$tor_$1_$2_redhat" != x"$tor_$1_devpkg_redhat"; then 
114     AC_WARN([   You will probably need to install $tor_$1_devpkg_redhat too.])
115   fi 
116 else
117   if test -f /etc/redhat-release && test x"$tor_$1_$2_redhat" != x; then
118     AC_WARN([On most Redhat-based systems, you can get$h $1 by installing the $tor_$1_$2_redhat RPM package])
119     if test x"$tor_$1_$2_redhat" != x"$tor_$1_devpkg_redhat"; then 
120       AC_WARN([   You will probably need to install $tor_$1_devpkg_redhat too.])
121     fi 
122   fi
126 dnl Look for a library, and its associated includes, and how to link
127 dnl against it.
129 dnl TOR_SEARCH_LIBRARY(1:libname, 2:IGNORED, 3:linkargs, 4:headers,
130 dnl                    5:prototype,
131 dnl                    6:code, 7:IGNORED, 8:searchextra)
133 dnl Special variables:
134 dnl   ALT_{libname}_WITHVAL -- another possible value for --with-$1-dir.
135 dnl       Used to support renaming --with-ssl-dir to --with-openssl-dir
137 AC_DEFUN([TOR_SEARCH_LIBRARY], [
138 try$1dir=""
139 AC_ARG_WITH($1-dir,
140   [  --with-$1-dir=PATH    Specify path to $1 installation ],
141   [
142      if test x$withval != xno ; then
143         try$1dir="$withval"
144      fi
145   ])
146 if test "x$try$1dir" = x && test "x$ALT_$1_WITHVAL" != x ; then
147   try$1dir="$ALT_$1_WITHVAL"
150 tor_saved_LIBS="$LIBS"
151 tor_saved_LDFLAGS="$LDFLAGS"
152 tor_saved_CPPFLAGS="$CPPFLAGS"
153 AC_CACHE_CHECK([for $1 directory], tor_cv_library_$1_dir, [
154   tor_$1_dir_found=no
155   tor_$1_any_linkable=no
157   for tor_trydir in "$try$1dir" "(system)" "$prefix" /usr/local /usr/pkg $8; do
158     LDFLAGS="$tor_saved_LDFLAGS"
159     LIBS="$tor_saved_LIBS $3"
160     CPPFLAGS="$tor_saved_CPPFLAGS"
162     if test -z "$tor_trydir" ; then
163       continue;
164     fi
166     # Skip the directory if it isn't there.
167     if test ! -d "$tor_trydir" && test "$tor_trydir" != "(system)"; then
168       continue;
169     fi
171     # If this isn't blank, try adding the directory (or appropriate
172     # include/libs subdirectories) to the command line.
173     if test "$tor_trydir" != "(system)"; then
174       TOR_EXTEND_CODEPATH($tor_trydir)
175     fi
177     # Can we link against (but not necessarily run, or find the headers for)
178     # the binary?
179     AC_LINK_IFELSE([AC_LANG_PROGRAM([$5], [$6])],
180                    [linkable=yes], [linkable=no])
182     if test "$linkable" = yes; then
183       tor_$1_any_linkable=yes
184       # Okay, we can link against it.  Can we find the headers?
185       AC_COMPILE_IFELSE([AC_LANG_PROGRAM([$4], [$6])],
186                         [buildable=yes], [buildable=no])
187       if test "$buildable" = yes; then
188          tor_cv_library_$1_dir=$tor_trydir
189          tor_$1_dir_found=yes
190          break
191       fi
192     fi
193   done
195   if test "$tor_$1_dir_found" = no; then
196     if test "$tor_$1_any_linkable" = no ; then
197       AC_MSG_WARN([Could not find a linkable $1.  If you have it installed somewhere unusual, you can specify an explicit path using --with-$1-dir])
198       TOR_WARN_MISSING_LIB($1, pkg)
199       AC_MSG_ERROR([Missing libraries; unable to proceed.])
200     else
201       AC_MSG_WARN([We found the libraries for $1, but we could not find the C header files.  You may need to install a devel package.])
202       TOR_WARN_MISSING_LIB($1, devpkg)
203       AC_MSG_ERROR([Missing headers; unable to proceed.])
204     fi
205   fi
207   LDFLAGS="$tor_saved_LDFLAGS"
208   LIBS="$tor_saved_LIBS"
209   CPPFLAGS="$tor_saved_CPPFLAGS"
210 ]) dnl end cache check
212 LIBS="$LIBS $3"
213 if test "$tor_cv_library_$1_dir" != "(system)"; then
214    TOR_EXTEND_CODEPATH($tor_cv_library_$1_dir)
217 TOR_DEFINE_CODEPATH($tor_cv_library_$1_dir, $1)
219 if test "$cross_compiling" != yes; then
220   AC_CACHE_CHECK([whether we need extra options to link $1],
221                  tor_cv_library_$1_linker_option, [
222    orig_LDFLAGS="$LDFLAGS"
223    runs=no
224    linked_with=nothing
225    if test -d "$tor_cv_library_$1_dir/lib"; then
226      tor_trydir="$tor_cv_library_$1_dir/lib"
227    else
228      tor_trydir="$tor_cv_library_$1_dir"
229    fi
230    for tor_tryextra in "(none)" "-Wl,-R$tor_trydir" "-R$tor_trydir" \
231                        "-Wl,-rpath,$tor_trydir" ; do
232      if test "$tor_tryextra" = "(none)"; then
233        LDFLAGS="$orig_LDFLAGS"
234      else
235        LDFLAGS="$tor_tryextra $orig_LDFLAGS"
236      fi
237      AC_RUN_IFELSE([AC_LANG_PROGRAM([$5], [$6])],
238                    [runnable=yes], [runnable=no])
239      if test "$runnable" = yes; then
240         tor_cv_library_$1_linker_option=$tor_tryextra
241         break
242      fi
243    done
245    if test "$runnable" = no; then
246      AC_MSG_ERROR([Found linkable $1 in $tor_cv_library_$1_dir, but it does not seem to run, even with -R. Maybe specify another using --with-$1-dir}])
247    fi
248    LDFLAGS="$orig_LDFLAGS"
249   ]) dnl end cache check check for extra options.
251   if test "$tor_cv_library_$1_linker_option" != "(none)" ; then
252     TOR_LDFLAGS_$1="$TOR_LDFLAGS_$1 $tor_cv_library_$1_linker_option"
253   fi
254 fi # cross-compile
256 LIBS="$tor_saved_LIBS"
257 LDFLAGS="$tor_saved_LDFLAGS"
258 CPPFLAGS="$tor_saved_CPPFLAGS"
260 ]) dnl end defun
262 dnl Check whether the prototype for a function is present or missing.
263 dnl Apple has a nasty habit of putting functions in their libraries (so that
264 dnl AC_CHECK_FUNCS passes) but not actually declaring them in the headers.
266 dnl TOR_CHECK_PROTYPE(1:functionname, 2:macroname, 2: includes)
267 AC_DEFUN([TOR_CHECK_PROTOTYPE], [
268  AC_CACHE_CHECK([for declaration of $1], tor_cv_$1_declared, [
269    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([$3],[void *ptr= $1 ;])],
270                      tor_cv_$1_declared=yes,tor_cv_$1_declared=no)])
271 if test x$tor_cv_$1_declared != xno ; then
272   AC_DEFINE($2, 1,
273        [Defined if the prototype for $1 seems to be present.])