test: Fix a mem leak reported by Coverity
[tor.git] / acinclude.m4
blobe6389ea1d4e70a71926b0652e22c6c0f10591869
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-2019, 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: try to link too if this is nonempty.
47 dnl 3: what to do on success compiling
48 dnl 4: what to do on failure compiling
49 AC_DEFUN([TOR_TRY_COMPILE_WITH_CFLAGS], [
50   AS_VAR_PUSHDEF([VAR],[tor_cv_cflags_$1])
51   AC_CACHE_CHECK([whether the compiler accepts $1], VAR, [
52     tor_saved_CFLAGS="$CFLAGS"
53     CFLAGS="$CFLAGS -pedantic -Werror $1"
54     AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
55                    [AS_VAR_SET(VAR,yes)],
56                    [AS_VAR_SET(VAR,no)])
57     if test x$2 != x; then
58       AS_VAR_PUSHDEF([can_link],[tor_can_link_$1])
59       AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
60                   [AS_VAR_SET(can_link,yes)],
61                   [AS_VAR_SET(can_link,no)])
62       AS_VAR_POPDEF([can_link])
63     fi
64     CFLAGS="$tor_saved_CFLAGS"
65   ])
66   if test x$VAR = xyes; then
67      $3
68   else
69      $4
70   fi
71   AS_VAR_POPDEF([VAR])
74 dnl 1:flags
75 dnl 2:also try to link (yes: non-empty string)
76 dnl   will set yes or no in $tor_can_link_$1 (as modified by AS_VAR_PUSHDEF)
77 AC_DEFUN([TOR_CHECK_CFLAGS], [
78   TOR_TRY_COMPILE_WITH_CFLAGS($1, $2, CFLAGS="$CFLAGS $1", true)
81 dnl 1:flags
82 dnl 2:extra ldflags
83 dnl 3:extra libraries
84 AC_DEFUN([TOR_CHECK_LDFLAGS], [
85   AS_VAR_PUSHDEF([VAR],[tor_cv_ldflags_$1])
86   AC_CACHE_CHECK([whether the linker accepts $1], VAR, [
87     tor_saved_CFLAGS="$CFLAGS"
88     tor_saved_LDFLAGS="$LDFLAGS"
89     tor_saved_LIBS="$LIBS"
90     CFLAGS="$CFLAGS -pedantic -Werror"
91     LDFLAGS="$LDFLAGS $2 $1"
92     LIBS="$LIBS $3"
93     AC_RUN_IFELSE([AC_LANG_PROGRAM([#include <stdio.h>], [fputs("", stdout)])],
94                   [AS_VAR_SET(VAR,yes)],
95                   [AS_VAR_SET(VAR,no)],
96                   [AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
97                                    [AS_VAR_SET(VAR,yes)],
98                                    [AS_VAR_SET(VAR,no)])])
99     CFLAGS="$tor_saved_CFLAGS"
100     LDFLAGS="$tor_saved_LDFLAGS"
101     LIBS="$tor_saved_LIBS"
102   ])
103   if test x$VAR = xyes; then
104     LDFLAGS="$LDFLAGS $1"
105   fi
106   AS_VAR_POPDEF([VAR])
109 dnl 1:libname
110 AC_DEFUN([TOR_WARN_MISSING_LIB], [
111 h=""
112 if test x$2 = xdevpkg; then
113   h=" headers for"
115 if test -f /etc/debian_version && test x"$tor_$1_$2_debian" != x; then
116   AC_MSG_WARN([On Debian, you can install$h $1 using "apt-get install $tor_$1_$2_debian"])
117   if test x"$tor_$1_$2_debian" != x"$tor_$1_devpkg_debian"; then 
118     AC_MSG_WARN([   You will probably need $tor_$1_devpkg_debian too.])
119   fi 
121 if test -f /etc/fedora-release && test x"$tor_$1_$2_redhat" != x; then
122   AC_MSG_WARN([On Fedora, you can install$h $1 using "dnf install $tor_$1_$2_redhat"])
123   if test x"$tor_$1_$2_redhat" != x"$tor_$1_devpkg_redhat"; then 
124     AC_MSG_WARN([   You will probably need to install $tor_$1_devpkg_redhat too.])
125   fi 
126 else
127   if test -f /etc/redhat-release && test x"$tor_$1_$2_redhat" != x; then
128     AC_MSG_WARN([On most Redhat-based systems, you can get$h $1 by installing the $tor_$1_$2_redhat RPM package])
129     if test x"$tor_$1_$2_redhat" != x"$tor_$1_devpkg_redhat"; then 
130       AC_MSG_WARN([   You will probably need to install $tor_$1_devpkg_redhat too.])
131     fi 
132   fi
136 dnl Look for a library, and its associated includes, and how to link
137 dnl against it.
139 dnl TOR_SEARCH_LIBRARY(1:libname, 2:IGNORED, 3:linkargs, 4:headers,
140 dnl                    5:prototype,
141 dnl                    6:code, 7:IGNORED, 8:searchextra)
143 dnl Special variables:
144 dnl   ALT_{libname}_WITHVAL -- another possible value for --with-$1-dir.
145 dnl       Used to support renaming --with-ssl-dir to --with-openssl-dir
147 AC_DEFUN([TOR_SEARCH_LIBRARY], [
148 try$1dir=""
149 AC_ARG_WITH($1-dir,
150   AS_HELP_STRING(--with-$1-dir=PATH, [specify path to $1 installation]),
151   [
152      if test x$withval != xno ; then
153         try$1dir="$withval"
154      fi
155   ])
156 if test "x$try$1dir" = x && test "x$ALT_$1_WITHVAL" != x ; then
157   try$1dir="$ALT_$1_WITHVAL"
160 tor_saved_LIBS="$LIBS"
161 tor_saved_LDFLAGS="$LDFLAGS"
162 tor_saved_CPPFLAGS="$CPPFLAGS"
163 AC_CACHE_CHECK([for $1 directory], tor_cv_library_$1_dir, [
164   tor_$1_dir_found=no
165   tor_$1_any_linkable=no
167   for tor_trydir in "$try$1dir" "(system)" "$prefix" /usr/local /usr/pkg $8; do
168     LDFLAGS="$tor_saved_LDFLAGS"
169     LIBS="$3 $tor_saved_LIBS"
170     CPPFLAGS="$tor_saved_CPPFLAGS"
172     if test -z "$tor_trydir" ; then
173       continue;
174     fi
176     # Skip the directory if it isn't there.
177     if test ! -d "$tor_trydir" && test "$tor_trydir" != "(system)"; then
178       continue;
179     fi
181     # If this isn't blank, try adding the directory (or appropriate
182     # include/libs subdirectories) to the command line.
183     if test "$tor_trydir" != "(system)"; then
184       TOR_EXTEND_CODEPATH($tor_trydir)
185     fi
187     # Can we link against (but not necessarily run, or find the headers for)
188     # the binary?
189     AC_LINK_IFELSE([AC_LANG_PROGRAM([$5], [$6])],
190                    [linkable=yes], [linkable=no])
192     if test "$linkable" = yes; then
193       tor_$1_any_linkable=yes
194       # Okay, we can link against it.  Can we find the headers?
195       AC_COMPILE_IFELSE([AC_LANG_PROGRAM([$4], [$6])],
196                         [buildable=yes], [buildable=no])
197       if test "$buildable" = yes; then
198          tor_cv_library_$1_dir=$tor_trydir
199          tor_$1_dir_found=yes
200          break
201       fi
202     fi
203   done
205   if test "$tor_$1_dir_found" = no; then
206     if test "$tor_$1_any_linkable" = no ; then
207       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])
208       TOR_WARN_MISSING_LIB($1, pkg)
209       AC_MSG_ERROR([Missing libraries; unable to proceed.])
210     else
211       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.])
212       TOR_WARN_MISSING_LIB($1, devpkg)
213       AC_MSG_ERROR([Missing headers; unable to proceed.])
214     fi
215   fi
217   LDFLAGS="$tor_saved_LDFLAGS"
218   LIBS="$tor_saved_LIBS"
219   CPPFLAGS="$tor_saved_CPPFLAGS"
220 ]) dnl end cache check
222 LIBS="$3 $LIBS"
223 if test "$tor_cv_library_$1_dir" != "(system)"; then
224    TOR_EXTEND_CODEPATH($tor_cv_library_$1_dir)
227 TOR_DEFINE_CODEPATH($tor_cv_library_$1_dir, $1)
229 if test "$cross_compiling" != yes; then
230   AC_CACHE_CHECK([whether we need extra options to link $1],
231                  tor_cv_library_$1_linker_option, [
232    orig_LDFLAGS="$LDFLAGS"
233    runs=no
234    linked_with=nothing
235    if test -d "$tor_cv_library_$1_dir/lib"; then
236      tor_trydir="$tor_cv_library_$1_dir/lib"
237    else
238      tor_trydir="$tor_cv_library_$1_dir"
239    fi
240    for tor_tryextra in "(none)" "-Wl,-R$tor_trydir" "-R$tor_trydir" \
241                        "-Wl,-rpath,$tor_trydir" ; do
242      if test "$tor_tryextra" = "(none)"; then
243        LDFLAGS="$orig_LDFLAGS"
244      else
245        LDFLAGS="$tor_tryextra $orig_LDFLAGS"
246      fi
247      AC_RUN_IFELSE([AC_LANG_PROGRAM([$5], [$6])],
248                    [runnable=yes], [runnable=no],
249                    [AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
250                                    [runnable=yes],
251                                    [runnable=no])])
252      if test "$runnable" = yes; then
253         tor_cv_library_$1_linker_option=$tor_tryextra
254         break
255      fi
256    done
258    if test "$runnable" = no; then
259      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}])
260    fi
261    LDFLAGS="$orig_LDFLAGS"
262   ]) dnl end cache check check for extra options.
264   if test "$tor_cv_library_$1_linker_option" != "(none)" ; then
265     TOR_LDFLAGS_$1="$TOR_LDFLAGS_$1 $tor_cv_library_$1_linker_option"
266   fi
267 fi # cross-compile
269 LIBS="$tor_saved_LIBS"
270 LDFLAGS="$tor_saved_LDFLAGS"
271 CPPFLAGS="$tor_saved_CPPFLAGS"
273 ]) dnl end defun
275 dnl Check whether the prototype for a function is present or missing.
276 dnl Apple has a nasty habit of putting functions in their libraries (so that
277 dnl AC_CHECK_FUNCS passes) but not actually declaring them in the headers.
279 dnl TOR_CHECK_PROTYPE(1:functionname, 2:macroname, 2: includes)
280 AC_DEFUN([TOR_CHECK_PROTOTYPE], [
281  AC_CACHE_CHECK([for declaration of $1], tor_cv_$1_declared, [
282    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([$3],[void *ptr= $1 ;])],
283                      tor_cv_$1_declared=yes,tor_cv_$1_declared=no)])
284 if test x$tor_cv_$1_declared != xno ; then
285   AC_DEFINE($2, 1,
286        [Defined if the prototype for $1 seems to be present.])