1dnl Copyright (c) 2001-2004, Roger Dingledine
2dnl Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson
3dnl Copyright (c) 2007-2019, The Tor Project, Inc.
4dnl See LICENSE for licensing information
5
6AC_PREREQ([2.63])
7AC_INIT([tor],[0.4.6.8])
8AC_CONFIG_SRCDIR([src/app/main/tor_main.c])
9AC_CONFIG_MACRO_DIR([m4])
10
11configure_flags="$*"
12AC_DEFINE_UNQUOTED([CONFIG_FLAGS], ["$configure_flags"], [Flags passed to configure])
13
14
15# DO NOT EDIT THIS DEFINITION BY HAND UNLESS YOU KNOW WHAT YOU'RE DOING.
16#
17# The update_versions.py script updates this definition when the
18# version number changes.  Tor uses it to make sure that it
19# only shuts down for missing "required protocols" when those protocols
20# are listed as required by a consensus after this date.
21AC_DEFINE(APPROX_RELEASE_DATE, ["2021-10-26"], # for 0.4.6.8
22          [Approximate date when this software was released. (Updated when the version changes.)])
23
24# "foreign" means we don't follow GNU package layout standards
25# "1.11" means we require automake version 1.11 or newer
26# "subdir-objects" means put .o files in the same directory as the .c files
27AM_INIT_AUTOMAKE([foreign 1.11 subdir-objects -Wall -Werror])
28
29tor_ac_n_warnings=0
30tor_incr_n_warnings() {
31  tor_ac_n_warnings=`expr $tor_ac_n_warnings + 1`
32}
33
34m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
35AC_CONFIG_HEADERS([orconfig.h])
36
37AC_USE_SYSTEM_EXTENSIONS
38AC_CANONICAL_HOST
39
40PKG_PROG_PKG_CONFIG
41if test "x$PKG_CONFIG" = "x" ; then
42    pkg_config_user_action="install pkg-config, and check the PKG_CONFIG_PATH environment variable"
43    AC_MSG_NOTICE([Some libraries need pkg-config, including systemd, nss, lzma, zstd, and custom mallocs.])
44    AC_MSG_NOTICE([To use those libraries, $pkg_config_user_action.])
45else
46    pkg_config_user_action="check the PKG_CONFIG_PATH environment variable"
47fi
48
49if test "x$PKG_CONFIG_PATH" = "x" && test "x$prefix" != "xNONE" && test "$host" != "$build"; then
50   export PKG_CONFIG_PATH=$prefix/lib/pkgconfig
51   AC_MSG_NOTICE([set PKG_CONFIG_PATH=$PKG_CONFIG_PATH to support cross-compiling])
52fi
53
54AC_ARG_ENABLE(openbsd-malloc,
55   AS_HELP_STRING(--enable-openbsd-malloc, [use malloc code from OpenBSD.  Linux only. Deprecated: see --with-malloc]))
56AC_ARG_ENABLE(static-openssl,
57   AS_HELP_STRING(--enable-static-openssl, [link against a static openssl library. Requires --with-openssl-dir]))
58AC_ARG_ENABLE(static-libevent,
59   AS_HELP_STRING(--enable-static-libevent, [link against a static libevent library. Requires --with-libevent-dir]))
60AC_ARG_ENABLE(static-zlib,
61   AS_HELP_STRING(--enable-static-zlib, [link against a static zlib library. Requires --with-zlib-dir]))
62AC_ARG_ENABLE(static-tor,
63   AS_HELP_STRING(--enable-static-tor, [create an entirely static Tor binary. Requires --with-openssl-dir and --with-libevent-dir and --with-zlib-dir]))
64AC_ARG_ENABLE(unittests,
65   AS_HELP_STRING(--disable-unittests, [don't build unit tests for Tor. Risky!]))
66AC_ARG_ENABLE(coverage,
67   AS_HELP_STRING(--enable-coverage, [enable coverage support in the unit-test build]))
68AC_ARG_ENABLE(asserts-in-tests,
69   AS_HELP_STRING(--disable-asserts-in-tests, [disable tor_assert() calls in the unit tests, for branch coverage]))
70AC_ARG_ENABLE(system-torrc,
71   AS_HELP_STRING(--disable-system-torrc, [don't look for a system-wide torrc file]))
72AC_ARG_ENABLE(libfuzzer,
73   AS_HELP_STRING(--enable-libfuzzer, [build extra fuzzers based on 'libfuzzer']))
74AC_ARG_ENABLE(oss-fuzz,
75   AS_HELP_STRING(--enable-oss-fuzz, [build extra fuzzers based on 'oss-fuzz' environment]))
76AC_ARG_ENABLE(memory-sentinels,
77   AS_HELP_STRING(--disable-memory-sentinels, [disable code that tries to prevent some kinds of memory access bugs. For fuzzing only.]))
78AC_ARG_ENABLE(rust,
79   AS_HELP_STRING(--enable-rust, [enable rust integration]))
80AC_ARG_ENABLE(cargo-online-mode,
81   AS_HELP_STRING(--enable-cargo-online-mode, [Allow cargo to make network requests to fetch crates. For builds with rust only.]))
82AC_ARG_ENABLE(restart-debugging,
83   AS_HELP_STRING(--enable-restart-debugging, [Build Tor with support for debugging in-process restart. Developers only.]))
84AC_ARG_ENABLE(zstd-advanced-apis,
85   AS_HELP_STRING(--disable-zstd-advanced-apis, [Build without support for zstd's "static-only" APIs.]))
86AC_ARG_ENABLE(nss,
87   AS_HELP_STRING(--enable-nss, [Use Mozilla's NSS TLS library. (EXPERIMENTAL)]))
88AC_ARG_ENABLE(pic,
89   AS_HELP_STRING(--enable-pic, [Build Tor's binaries as position-independent code, suitable to link as a library.]))
90
91AC_ARG_ENABLE(missing-doc-warnings,
92   AS_HELP_STRING(--enable-missing-doc-warnings, [Tell doxygen to warn about missing documentation. Makes doxygen warnings nonfatal.]))
93
94if test "$enable_missing_doc_warnings" = "yes"; then
95   DOXYGEN_FATAL_WARNINGS=NO
96   DOXYGEN_WARN_ON_MISSING=YES
97elif test "$enable_fatal_warnings" = "yes"; then
98   # Fatal warnings from doxygen are nice, but not if we're warning about
99   # missing documentation.
100   DOXYGEN_FATAL_WARNINGS=YES
101   DOXYGEN_WARN_ON_MISSING=NO
102else
103   DOXYGEN_FATAL_WARNINGS=NO
104   DOXYGEN_WARN_ON_MISSING=NO
105fi
106AC_SUBST(DOXYGEN_FATAL_WARNINGS)
107AC_SUBST(DOXYGEN_WARN_ON_MISSING)
108
109if test "x$enable_coverage" != "xyes" -a "x$enable_asserts_in_tests" = "xno" ; then
110    AC_MSG_ERROR([Can't disable assertions outside of coverage build])
111fi
112
113AM_CONDITIONAL(UNITTESTS_ENABLED, test "x$enable_unittests" != "xno")
114AM_CONDITIONAL(COVERAGE_ENABLED, test "x$enable_coverage" = "xyes")
115AM_CONDITIONAL(DISABLE_ASSERTS_IN_UNIT_TESTS, test "x$enable_asserts_in_tests" = "xno")
116AM_CONDITIONAL(LIBFUZZER_ENABLED, test "x$enable_libfuzzer" = "xyes")
117AM_CONDITIONAL(OSS_FUZZ_ENABLED, test "x$enable_oss_fuzz" = "xyes")
118AM_CONDITIONAL(USE_RUST, test "x$enable_rust" = "xyes")
119AM_CONDITIONAL(USE_NSS, test "x$enable_nss" = "xyes")
120AM_CONDITIONAL(USE_OPENSSL, test "x$enable_nss" != "xyes")
121
122if test "x$enable_nss" = "xyes"; then
123  AC_DEFINE(ENABLE_NSS, 1,
124  	    [Defined if we're building with NSS.])
125else
126  AC_DEFINE(ENABLE_OPENSSL, 1,
127            [Defined if we're building with OpenSSL or LibreSSL])
128fi
129
130if test "$enable_static_tor" = "yes"; then
131  enable_static_libevent="yes";
132  enable_static_openssl="yes";
133  enable_static_zlib="yes";
134  TOR_STATIC_LDFLAGS="-static"
135fi
136AC_SUBST(TOR_STATIC_LDFLAGS)
137
138if test "$enable_system_torrc" = "no"; then
139  AC_DEFINE(DISABLE_SYSTEM_TORRC, 1,
140            [Defined if we're not going to look for a torrc in SYSCONF])
141fi
142
143if test "$enable_memory_sentinels" = "no"; then
144  AC_DEFINE(DISABLE_MEMORY_SENTINELS, 1,
145           [Defined if we're turning off memory safety code to look for bugs])
146fi
147
148AC_ARG_ENABLE(manpage,
149              AS_HELP_STRING(--disable-manpage, [Disable manpage generation.]))
150
151AC_ARG_ENABLE(html-manual,
152              AS_HELP_STRING(--disable-html-manual, [Disable HTML documentation.]))
153
154AC_ARG_ENABLE(asciidoc,
155     AS_HELP_STRING(--disable-asciidoc, [don't use asciidoc (disables building of manpages)]),
156     [case "${enableval}" in
157        "yes") asciidoc=true ;;
158        "no")  asciidoc=false ;;
159        *) AC_MSG_ERROR(bad value for --disable-asciidoc) ;;
160      esac], [asciidoc=true])
161
162# systemd notify support
163AC_ARG_ENABLE(systemd,
164      AS_HELP_STRING(--enable-systemd, [enable systemd notification support]),
165      [case "${enableval}" in
166        "yes") systemd=true ;;
167        "no")  systemd=false ;;
168        * ) AC_MSG_ERROR(bad value for --enable-systemd) ;;
169      esac], [systemd=auto])
170
171if test "$enable_restart_debugging" = "yes"; then
172  AC_DEFINE(ENABLE_RESTART_DEBUGGING, 1,
173            [Defined if we're building with support for in-process restart debugging.])
174fi
175
176if test "$enable_zstd_advanced_apis" != "no"; then
177   AC_DEFINE(ENABLE_ZSTD_ADVANCED_APIS, 1,
178             [Defined if we're going to try to use zstd's "static-only" APIs.])
179fi
180
181# systemd support
182if test "x$enable_systemd" = "xno"; then
183    have_systemd=no;
184else
185    PKG_CHECK_MODULES(SYSTEMD,
186        [libsystemd-daemon],
187        have_systemd=yes,
188        have_systemd=no)
189    if test "x$have_systemd" = "xno"; then
190        AC_MSG_NOTICE([Okay, checking for systemd a different way...])
191        PKG_CHECK_MODULES(SYSTEMD,
192            [libsystemd],
193            have_systemd=yes,
194            have_systemd=no)
195    fi
196fi
197
198if test "x$have_systemd" = "xyes"; then
199    AC_DEFINE(HAVE_SYSTEMD,1,[Have systemd])
200    TOR_SYSTEMD_CFLAGS="${SYSTEMD_CFLAGS}"
201    TOR_SYSTEMD_LIBS="${SYSTEMD_LIBS}"
202    PKG_CHECK_MODULES(LIBSYSTEMD209, [libsystemd >= 209],
203         [AC_DEFINE(HAVE_SYSTEMD_209,1,[Have systemd v209 or greater])], [])
204fi
205AC_SUBST(TOR_SYSTEMD_CFLAGS)
206AC_SUBST(TOR_SYSTEMD_LIBS)
207
208if test "x$enable_systemd" = "xyes" -a "x$have_systemd" != "xyes" ; then
209    AC_MSG_ERROR([Explicitly requested systemd support, but systemd not found, $pkg_config_user_action, or set SYSTEMD_CFLAGS and SYSTEMD_LIBS.])
210fi
211
212case "$host" in
213   *-*-solaris* )
214     AC_DEFINE(_REENTRANT, 1, [Define on some platforms to activate x_r() functions in time.h])
215     ;;
216esac
217
218AC_ARG_ENABLE(gcc-warnings,
219     AS_HELP_STRING(--enable-gcc-warnings, [deprecated alias for enable-fatal-warnings]))
220AC_ARG_ENABLE(fatal-warnings,
221     AS_HELP_STRING(--enable-fatal-warnings, [tell the compiler to treat all warnings as errors.]))
222AC_ARG_ENABLE(gcc-warnings-advisory,
223     AS_HELP_STRING(--disable-gcc-warnings-advisory, [disable the regular verbose warnings]))
224
225dnl Others suggest '/gs /safeseh /nxcompat /dynamicbase' for non-gcc on Windows
226AC_ARG_ENABLE(gcc-hardening,
227    AS_HELP_STRING(--disable-gcc-hardening, [disable compiler security checks]))
228
229dnl Deprecated --enable-expensive-hardening but keep it for now for backward compat.
230AC_ARG_ENABLE(expensive-hardening,
231    AS_HELP_STRING(--enable-expensive-hardening, [enable more fragile and expensive compiler hardening; makes Tor slower]))
232AC_ARG_ENABLE(fragile-hardening,
233    AS_HELP_STRING(--enable-fragile-hardening, [enable more fragile and expensive compiler hardening; makes Tor slower]))
234if test "x$enable_expensive_hardening" = "xyes" || test "x$enable_fragile_hardening" = "xyes"; then
235  fragile_hardening="yes"
236  AC_DEFINE(DEBUG_SMARTLIST, 1, [Enable smartlist debugging])
237fi
238
239AC_ARG_ENABLE(all-bugs-are-fatal,
240   AS_HELP_STRING(--enable-all-bugs-are-fatal, [force all soft asserts in Tor codebase (tor_assert_nonfatal(), BUG(), etc.) to act as hard asserts (tor_assert() and equivalents); makes Tor fragile; only recommended for dev builds]))
241
242if test "x$enable_all_bugs_are_fatal" = "xyes"; then
243  AC_DEFINE(ALL_BUGS_ARE_FATAL, 1, [All assert failures are fatal])
244fi
245
246dnl Linker hardening options
247dnl Currently these options are ELF specific - you can't use this with MacOSX
248AC_ARG_ENABLE(linker-hardening,
249    AS_HELP_STRING(--disable-linker-hardening, [disable linker security fixups]))
250
251AC_ARG_ENABLE(local-appdata,
252   AS_HELP_STRING(--enable-local-appdata, [default to host local application data paths on Windows]))
253if test "$enable_local_appdata" = "yes"; then
254  AC_DEFINE(ENABLE_LOCAL_APPDATA, 1,
255            [Defined if we default to host local appdata paths on Windows])
256fi
257
258AC_ARG_ENABLE(tool-name-check,
259     AS_HELP_STRING(--disable-tool-name-check, [check for sanely named toolchain when cross-compiling]))
260
261AC_ARG_ENABLE(seccomp,
262     AS_HELP_STRING(--disable-seccomp, [do not attempt to use libseccomp]))
263
264AC_ARG_ENABLE(libscrypt,
265     AS_HELP_STRING(--disable-libscrypt, [do not attempt to use libscrypt]))
266
267dnl --- Tracing Options. ---
268
269TOR_TRACE_LIBS=
270
271dnl LTTng instrumentation option.
272AC_ARG_ENABLE(tracing-instrumentation-lttng,
273              AS_HELP_STRING([--enable-tracing-instrumentation-lttng],
274                             [build with LTTng-UST instrumentation]))
275AM_CONDITIONAL([USE_TRACING_INSTRUMENTATION_LTTNG],
276               [test "x$enable_tracing_instrumentation_lttng" = "xyes"])
277
278if test "x$enable_tracing_instrumentation_lttng" = "xyes"; then
279  AC_CHECK_HEADERS([lttng/tracepoint.h], [],
280                   [AC_MSG_ERROR([LTTng instrumentation headers not found.
281                                  On Debian, apt install liblttng-ust-dev"])], [])
282  AC_DEFINE([USE_TRACING_INSTRUMENTATION_LTTNG], [1], [Using LTTng instrumentation])
283  TOR_TRACE_LIBS="-llttng-ust -ldl"
284  have_tracing=1
285fi
286
287dnl USDT instrumentation option.
288AC_ARG_ENABLE(tracing-instrumentation-usdt,
289              AS_HELP_STRING([--enable-tracing-instrumentation-usdt],
290                             [build with tracing USDT instrumentation]))
291AM_CONDITIONAL([USE_TRACING_INSTRUMENTATION_USDT],
292               [test "x$enable_tracing_instrumentation_usdt" = "xyes"])
293
294if test "x$enable_tracing_instrumentation_usdt" = "xyes"; then
295  AC_CHECK_HEADERS([sys/sdt.h], [],
296                   [AC_MSG_ERROR([USDT instrumentation requires sys/sdt.h header.
297                                  On Debian, apt install systemtap-sdt-dev])], [])
298  AC_MSG_CHECKING([STAP_PROBEV()])
299  AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
300    #define SDT_USE_VARIADIC
301    #include <sys/sdt.h>
302    void test(void)
303    {
304      STAP_PROBEV(p, n, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
305    }
306  ]])], [
307    AC_MSG_RESULT([yes])
308    dnl LTTng generates USDT probes if the UST library was built with
309    dnl --with-sdt. There is unfortunately no way to check that so we always
310    dnl build the USDT probes even though LTTng instrumentation was requested.
311    AC_DEFINE([USE_TRACING_INSTRUMENTATION_USDT], [1], [Using USDT instrumentation])
312    have_tracing=1
313  ], [
314    AC_MSG_RESULT([no])
315    AC_MSG_ERROR([USDT tracing support requires STAP_PROBEV()])
316  ])
317fi
318
319dnl Tracepoints event to debug logs.
320AC_ARG_ENABLE(tracing-instrumentation-log-debug,
321     AS_HELP_STRING([--enable-tracing-instrumentation-log-debug],
322                    [build with tracing event to debug log]),
323     AC_DEFINE([USE_TRACING_INSTRUMENTATION_LOG_DEBUG], [1],
324               [Tracepoints to log debug]), [])
325AM_CONDITIONAL([USE_TRACING_INSTRUMENTATION_LOG_DEBUG],
326               [test "x$enable_tracing_instrumentation_log_debug" = "xyes"])
327if test "x$enable_tracing_instrumentation_log_debug" = "xyes"; then
328  have_tracing=1
329fi
330
331dnl Define that tracing is supported if any instrumentation is used.
332AM_COND_IF([USE_TRACING_INSTRUMENTATION_LOG_DEBUG],
333           AC_DEFINE([HAVE_TRACING], [1], [Compiled with tracing support]))
334AM_COND_IF([USE_TRACING_INSTRUMENTATION_USDT],
335           AC_DEFINE([HAVE_TRACING], [1], [Compiled with tracing support]))
336AM_COND_IF([USE_TRACING_INSTRUMENTATION_LTTNG],
337           AC_DEFINE([HAVE_TRACING], [1], [Compiled with tracing support]))
338AM_CONDITIONAL([USE_TRACING], [test "x$have_tracing" = x1 ])
339
340dnl Finally, define the trace libs.
341AC_SUBST([TOR_TRACE_LIBS])
342
343dnl -- End Tracing Options. --
344
345dnl Enable Android only features.
346AC_ARG_ENABLE(android,
347     AS_HELP_STRING(--enable-android, [build with Android features enabled]))
348AM_CONDITIONAL([USE_ANDROID], [test "x$enable_android" = "xyes"])
349
350if test "x$enable_android" = "xyes"; then
351  AC_DEFINE([USE_ANDROID], [1], [Compile with Android specific features enabled])
352
353fi
354
355dnl ---
356dnl Tor modules options. These options are namespaced with --disable-module-XXX
357dnl ---
358
359dnl All our modules.
360m4_define(MODULES, relay dirauth dircache)
361
362# Some modules are only disabled through another option. For those, we don't
363# want to print the help in the summary at the end of the configure. Any entry
364# in the following set will not print the "--disable-module-NAME" command in
365# the summary.
366m4_set_add_all([MODULES_WITH_NO_OPTIONS], [dircache])
367
368dnl Relay module.
369AC_ARG_ENABLE([module-relay],
370              AS_HELP_STRING([--disable-module-relay],
371                             [Build tor without the Relay modules: tor can not run as a relay, bridge, or authority. Implies --disable-module-dirauth]))
372AM_CONDITIONAL(BUILD_MODULE_RELAY, [test "x$enable_module_relay" != "xno"])
373AM_COND_IF(BUILD_MODULE_RELAY,
374           AC_DEFINE([HAVE_MODULE_RELAY], [1],
375                     [Compile with Relay feature support]))
376
377dnl Dircache module.  (This cannot be enabled or disabled independently of
378dnl the relay module.  It is not listed by --list-modules for this reason.)
379AM_CONDITIONAL(BUILD_MODULE_DIRCACHE,
380               [test "x$enable_module_relay" != "xno"])
381AM_COND_IF(BUILD_MODULE_DIRCACHE,
382           AC_DEFINE([HAVE_MODULE_DIRCACHE], [1],
383                     [Compile with directory cache support]))
384
385dnl Directory Authority module.
386AC_ARG_ENABLE([module-dirauth],
387              AS_HELP_STRING([--disable-module-dirauth],
388                             [Build tor without the Directory Authority module: tor can not run as a directory authority or bridge authority]))
389AM_CONDITIONAL(BUILD_MODULE_DIRAUTH,[test "x$enable_module_dirauth" != "xno" && test "x$enable_module_relay" != "xno"])
390AM_COND_IF(BUILD_MODULE_DIRAUTH,
391           AC_DEFINE([HAVE_MODULE_DIRAUTH], [1],
392                     [Compile with Directory Authority feature support]))
393
394dnl Helper variables.
395TOR_MODULES_ALL_ENABLED=
396AC_DEFUN([ADD_MODULE], [
397    MODULE=m4_toupper($1)
398    TOR_MODULES_ALL_ENABLED="${TOR_MODULES_ALL_ENABLED} -DHAVE_MODULE_${MODULE}=1"
399])
400m4_foreach_w([module], MODULES, [ADD_MODULE([module])])
401AC_SUBST(TOR_MODULES_ALL_ENABLED)
402
403dnl check for the correct "ar" when cross-compiling.
404dnl   (AM_PROG_AR was new in automake 1.11.2, which we do not yet require,
405dnl    so kludge up a replacement for the case where it isn't there yet.)
406m4_ifdef([AM_PROG_AR],
407         [AM_PROG_AR],
408         [AN_MAKEVAR([AR], [AC_PROG_AR])
409          AN_PROGRAM([ar], [AC_PROG_AR])
410          AC_DEFUN([AC_PROG_AR], [AC_CHECK_TOOL([AR], [ar], [:])])
411          AC_PROG_AR])
412
413dnl Check whether the above macro has settled for a simply named tool even
414dnl though we're cross compiling. We must do this before running AC_PROG_CC,
415dnl because that will find any cc on the system, not only the cross-compiler,
416dnl and then verify that a binary built with this compiler runs on the
417dnl build system. It will then come to the false conclusion that we're not
418dnl cross-compiling.
419if test "x$enable_tool_name_check" != "xno"; then
420    if test "x$ac_tool_warned" = "xyes"; then
421        AC_MSG_ERROR([We are cross compiling but could not find a properly named toolchain. Do you have your cross-compiling toolchain in PATH? (You can --disable-tool-name-check to ignore this.)])
422	elif test "x$ac_ct_AR" != "x" -a "x$cross_compiling" = "xmaybe"; then
423		AC_MSG_ERROR([We think we are cross compiling but could not find a properly named toolchain. Do you have your cross-compiling toolchain in PATH? (You can --disable-tool-name-check to ignore this.)])
424	fi
425fi
426
427AC_PROG_CC
428AC_PROG_CPP
429AC_PROG_MAKE_SET
430AC_PROG_RANLIB
431AC_PROG_SED
432
433AX_COMPILER_VERSION
434AX_COMPILER_VENDOR
435
436AC_DEFINE_UNQUOTED([COMPILER_VERSION],["$ax_cv_c_compiler_version"], [Compiler version])
437AC_DEFINE_UNQUOTED([COMPILER_VENDOR],["$ax_cv_c_compiler_vendor"], [Compiler vendor])
438
439AS_CASE($ax_cv_c_compiler_vendor,
440        [gnu], AC_DEFINE([COMPILER],["GCC"],[Compiler name]),
441        AC_DEFINE([COMPILER],[],[Compiler name])
442)
443
444AC_ARG_VAR([PERL], [path to Perl binary])
445AC_CHECK_PROGS([PERL], [perl])
446AM_CONDITIONAL(USE_PERL, [test "x$ac_cv_prog_PERL" != "x"])
447
448dnl check for asciidoc and a2x
449AC_PATH_PROG([ASCIIDOC], [asciidoc], none)
450AC_PATH_PROGS([A2X], [a2x a2x.py], none)
451
452AM_CONDITIONAL(USE_ASCIIDOC, test "x$asciidoc" = "xtrue")
453AM_CONDITIONAL(BUILD_MANPAGE, [test "x$enable_manpage" != "xno"])
454AM_CONDITIONAL(BUILD_HTML_DOCS, [test "x$enable_html_manual" != "xno"])
455
456AM_PROG_CC_C_O
457
458dnl Before autoconf 2.70, AC_PROG_CC_C99 is supposedly necessary for some
459dnl compilers if you wan't C99 support. Starting with 2.70, it is obsolete and
460dnl forbidden.
461m4_version_prereq([2.70], [:], [AC_PROG_CC_C99])
462
463AC_CACHE_CHECK([for Python 3], [tor_cv_PYTHON],
464   [AC_PATH_PROGS_FEATURE_CHECK([PYTHON], [ \
465	python3 \
466	python3.8 python3.7 python3.6 python3.5 python3.4 \
467	python ],
468   [["$ac_path_PYTHON" -c 'import sys; sys.exit(sys.version_info[0]<3)' && tor_cv_PYTHON="$ac_path_PYTHON" ac_path_PYTHON_found=:]] )])
469AC_SUBST([PYTHON], [$tor_cv_PYTHON])
470
471PYTHON="$tor_cv_PYTHON"
472
473if test "x$PYTHON" = "x"; then
474  tor_incr_n_warnings
475  AC_MSG_WARN([Python 3 unavailable; some tests will not be run.])
476fi
477
478AM_CONDITIONAL(USEPYTHON, [test "x$PYTHON" != "x"])
479
480dnl List all external rust crates we depend on here. Include the version
481rust_crates=" \
482    digest-0.7.2 \
483    libc-0.2.39 \
484"
485AC_SUBST(rust_crates)
486
487ifdef([AC_C_FLEXIBLE_ARRAY_MEMBER], [
488AC_C_FLEXIBLE_ARRAY_MEMBER
489], [
490 dnl Maybe we've got an old autoconf...
491 AC_CACHE_CHECK([for flexible array members],
492     tor_cv_c_flexarray,
493     [AC_COMPILE_IFELSE(
494       AC_LANG_PROGRAM([
495 struct abc { int a; char b[]; };
496], [
497 struct abc *def = malloc(sizeof(struct abc)+sizeof(char));
498 def->b[0] = 33;
499]),
500  [tor_cv_c_flexarray=yes],
501  [tor_cv_c_flexarray=no])])
502 if test "$tor_cv_flexarray" = "yes"; then
503   AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], [], [Define to nothing if C supports flexible array members, and to 1 if it does not.])
504 else
505   AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], [1], [Define to nothing if C supports flexible array members, and to 1 if it does not.])
506 fi
507])
508
509AC_CACHE_CHECK([for working C99 mid-block declaration syntax],
510      tor_cv_c_c99_decl,
511      [AC_COMPILE_IFELSE(
512         [AC_LANG_PROGRAM([], [int x; x = 3; int y; y = 4 + x;])],
513	 [tor_cv_c_c99_decl=yes],
514	 [tor_cv_c_c99_decl=no] )])
515if test "$tor_cv_c_c99_decl" != "yes"; then
516  AC_MSG_ERROR([Your compiler doesn't support c99 mid-block declarations. This is required as of Tor 0.2.6.x])
517fi
518
519AC_CACHE_CHECK([for working C99 designated initializers],
520      tor_cv_c_c99_designated_init,
521      [AC_COMPILE_IFELSE(
522         [AC_LANG_PROGRAM([struct s { int a; int b; };],
523  	       [[ struct s ss = { .b = 5, .a = 6 }; ]])],
524	 [tor_cv_c_c99_designated_init=yes],
525	 [tor_cv_c_c99_designated_init=no] )])
526
527if test "$tor_cv_c_c99_designated_init" != "yes"; then
528  AC_MSG_ERROR([Your compiler doesn't support c99 designated initializers. This is required as of Tor 0.2.6.x])
529fi
530
531saved_CFLAGS="$CFLAGS"
532CFLAGS="$CFLAGS -Werror"
533AC_CACHE_CHECK([for __attribute__((fallthrough))],
534      tor_cv_c_attr_fallthrough,
535      [AC_COMPILE_IFELSE(
536         [AC_LANG_PROGRAM([extern int x; void fn(void) ;],
537	       [[ switch (x) { case 1: fn(); __attribute__((fallthrough));
538                               case 2: fn(); break; } ]])],
539	 [tor_cv_c_attr_fallthrough=yes],
540	 [tor_cv_c_attr_fallthrough=no] )])
541CFLAGS="$saved_CFLAGS"
542
543if test "$tor_cv_c_attr_fallthrough" = "yes"; then
544  AC_DEFINE(HAVE_ATTR_FALLTHROUGH, [1], [defined if we have the fallthrough attribute.])
545fi
546
547TORUSER=_tor
548AC_ARG_WITH(tor-user,
549        AS_HELP_STRING(--with-tor-user=NAME, [specify username for tor daemon]),
550        [
551           TORUSER=$withval
552        ]
553)
554AC_SUBST(TORUSER)
555
556TORGROUP=_tor
557AC_ARG_WITH(tor-group,
558        AS_HELP_STRING(--with-tor-group=NAME, [specify group name for tor daemon]),
559        [
560           TORGROUP=$withval
561        ]
562)
563AC_SUBST(TORGROUP)
564
565
566dnl If _WIN32 is defined and non-zero, we are building for win32
567AC_MSG_CHECKING([for win32])
568AC_RUN_IFELSE([AC_LANG_SOURCE([
569int main(int c, char **v) {
570#ifdef _WIN32
571#if _WIN32
572  return 0;
573#else
574  return 1;
575#endif
576#else
577  return 2;
578#endif
579}])],
580bwin32=true; AC_MSG_RESULT([yes]),
581bwin32=false; AC_MSG_RESULT([no]),
582bwin32=cross; AC_MSG_RESULT([cross])
583)
584
585if test "$bwin32" = "cross"; then
586AC_MSG_CHECKING([for win32 (cross)])
587AC_COMPILE_IFELSE([AC_LANG_SOURCE([
588#ifdef _WIN32
589int main(int c, char **v) {return 0;}
590#else
591#error
592int main(int c, char **v) {return x(y);}
593#endif
594])],
595bwin32=true; AC_MSG_RESULT([yes]),
596bwin32=false; AC_MSG_RESULT([no]))
597fi
598
599AH_BOTTOM([
600#ifdef _WIN32
601/* Defined to access windows functions and definitions for >=WinVista */
602# ifndef WINVER
603#  define WINVER 0x0600
604# endif
605
606/* Defined to access _other_ windows functions and definitions for >=WinVista */
607# ifndef _WIN32_WINNT
608#  define _WIN32_WINNT 0x0600
609# endif
610
611/* Defined to avoid including some windows headers as part of Windows.h */
612# ifndef WIN32_LEAN_AND_MEAN
613#  define WIN32_LEAN_AND_MEAN 1
614# endif
615#endif
616])
617
618AM_CONDITIONAL(WIN32, test "x$bwin32" = "xtrue")
619AM_CONDITIONAL(BUILD_NT_SERVICES, test "x$bwin32" = "xtrue")
620AM_CONDITIONAL(BUILD_LIBTORRUNNER, test "x$bwin32" != "xtrue")
621
622dnl Enable C99 when compiling with MIPSpro
623AC_MSG_CHECKING([for MIPSpro compiler])
624AC_COMPILE_IFELSE([AC_LANG_PROGRAM(, [
625#if (defined(__sgi) && defined(_COMPILER_VERSION))
626#error
627  return x(y);
628#endif
629])],
630bmipspro=false; AC_MSG_RESULT(no),
631bmipspro=true; AC_MSG_RESULT(yes))
632
633if test "$bmipspro" = "true"; then
634  CFLAGS="$CFLAGS -c99"
635fi
636
637AC_C_BIGENDIAN
638
639AC_ARG_VAR([TOR_RUST_TARGET], [Rust target, must be specified when cross-compiling (HOST != BUILD). example: i686-pc-windows-gnu])
640
641if test "x$enable_rust" = "xyes"; then
642  AC_ARG_VAR([RUSTC], [path to the rustc binary])
643  AC_CHECK_PROG([RUSTC], [rustc], [rustc],[no])
644  if test "x$RUSTC" = "xno"; then
645    AC_MSG_ERROR([rustc unavailable but rust integration requested.])
646  fi
647
648  AC_ARG_VAR([CARGO], [path to the cargo binary])
649  AC_CHECK_PROG([CARGO], [cargo], [cargo],[no])
650  if test "x$CARGO" = "xno"; then
651    AC_MSG_ERROR([cargo unavailable but rust integration requested.])
652  fi
653
654  AC_DEFINE([HAVE_RUST], 1, [have Rust])
655  if test "x$enable_fatal_warnings" = "xyes"; then
656    RUST_WARN=
657  else
658    RUST_WARN=#
659  fi
660  if test "x$enable_cargo_online_mode" = "xyes"; then
661    CARGO_ONLINE=
662    RUST_DL=#
663  else
664    CARGO_ONLINE=--frozen
665    RUST_DL=
666
667    dnl When we're not allowed to touch the network, we need crate dependencies
668    dnl locally available.
669    AC_MSG_CHECKING([rust crate dependencies])
670    AC_ARG_VAR([TOR_RUST_DEPENDENCIES], [path to directory with local crate mirror])
671    if test "x$TOR_RUST_DEPENDENCIES" = "x"; then
672      TOR_RUST_DEPENDENCIES="${srcdir}/src/ext/rust/crates"
673    fi
674    dnl Check whether the path exists before we try to cd into it.
675    if test ! -d "$TOR_RUST_DEPENDENCIES"; then
676      AC_MSG_ERROR([Rust dependency directory $TOR_RUST_DEPENDENCIES does not exist. Specify a dependency directory using the TOR_RUST_DEPENDENCIES variable or allow cargo to fetch crates using --enable-cargo-online-mode.])
677      ERRORED=1
678    fi
679    dnl Make the path absolute, since we'll be using it from within a
680    dnl subdirectory.
681    TOR_RUST_DEPENDENCIES=$(cd "$TOR_RUST_DEPENDENCIES" ; pwd)
682
683    for dep in $rust_crates; do
684      if test ! -d "$TOR_RUST_DEPENDENCIES"/"$dep"; then
685        AC_MSG_ERROR([Failure to find rust dependency $TOR_RUST_DEPENDENCIES/$dep. Specify a dependency directory using the TOR_RUST_DEPENDENCIES variable or allow cargo to fetch crates using --enable-cargo-online-mode.])
686        ERRORED=1
687      fi
688    done
689    if test "x$ERRORED" = "x"; then
690      AC_MSG_RESULT([yes])
691    fi
692  fi
693
694  dnl For now both MSVC and MinGW rust libraries will output static libs with
695  dnl the MSVC naming convention.
696  if test "$bwin32" = "true"; then
697    tor_rust_static_name=tor_rust.lib
698  else
699    tor_rust_static_name=libtor_rust.a
700  fi
701
702  AC_CANONICAL_BUILD
703
704  if test -n "$TOR_RUST_TARGET"; then
705    if test "$host" = "$build"; then
706      AC_MSG_ERROR([HOST = BUILD is invalid if TOR_RUST_TARGET is specified, see configure --help for more information.])
707    fi
708    RUST_TARGET_PROP="target = '$TOR_RUST_TARGET'"
709    TOR_RUST_LIB_PATH="src/rust/target/$TOR_RUST_TARGET/release/$tor_rust_static_name"
710  else
711    if test "$host" != "$build"; then
712      AC_MSG_ERROR([TOR_RUST_TARGET must be specified when cross-compiling with Rust enabled.])
713    fi
714    RUST_TARGET_PROP=
715    TOR_RUST_LIB_PATH="src/rust/target/release/$tor_rust_static_name"
716  fi
717
718  AC_SUBST(RUST_TARGET_PROP)
719  AC_SUBST(TOR_RUST_LIB_PATH)
720  AC_SUBST(CARGO_ONLINE)
721  AC_SUBST(RUST_WARN)
722  AC_SUBST(RUST_DL)
723
724  dnl Let's check the rustc version, too
725  AC_MSG_CHECKING([rust version])
726  RUSTC_VERSION=`$RUSTC --version`
727  RUSTC_VERSION_MAJOR=`$RUSTC --version | cut -d ' ' -f 2 | cut -d '.' -f 1`
728  RUSTC_VERSION_MINOR=`$RUSTC --version | cut -d ' ' -f 2 | cut -d '.' -f 2`
729  if test "x$RUSTC_VERSION_MAJOR" = "x" -o "x$RUSTC_VERSION_MINOR" = "x"; then
730    AC_MSG_ERROR([rustc version couldn't be identified])
731  fi
732  if test "$RUSTC_VERSION_MAJOR" -lt 2 -a "$RUSTC_VERSION_MINOR" -lt 31; then
733    AC_MSG_ERROR([rustc must be at least version 1.31.0])
734  fi
735  AC_MSG_RESULT([$RUSTC_VERSION])
736fi
737
738AC_SEARCH_LIBS(socket, [socket network])
739AC_SEARCH_LIBS(gethostbyname, [nsl])
740AC_SEARCH_LIBS(dlopen, [dl])
741AC_SEARCH_LIBS(inet_aton, [resolv])
742AC_SEARCH_LIBS(backtrace, [execinfo])
743saved_LIBS="$LIBS"
744AC_SEARCH_LIBS([clock_gettime], [rt])
745if test "$LIBS" != "$saved_LIBS"; then
746   # Looks like we need -lrt for clock_gettime().
747   have_rt=yes
748fi
749
750if test "$bwin32" = "false"; then
751  AC_SEARCH_LIBS(pthread_create, [pthread])
752  AC_SEARCH_LIBS(pthread_detach, [pthread])
753fi
754
755AM_CONDITIONAL(THREADS_WIN32, test "$bwin32" = "true")
756AM_CONDITIONAL(THREADS_PTHREADS, test "$bwin32" = "false")
757
758AC_CHECK_FUNCS(
759	_NSGetEnviron \
760	RtlSecureZeroMemory \
761	SecureZeroMemory \
762	accept4 \
763	backtrace \
764	backtrace_symbols_fd \
765	eventfd \
766	explicit_bzero \
767	timingsafe_memcmp \
768	flock \
769	fsync \
770	ftime \
771	get_current_dir_name \
772	getaddrinfo \
773	getdelim \
774	getifaddrs \
775	getline \
776	getrlimit \
777	gettimeofday \
778	gmtime_r \
779	gnu_get_libc_version \
780	inet_aton \
781	ioctl \
782	issetugid \
783	llround \
784	localtime_r \
785	lround \
786	madvise \
787	memmem \
788	memset_s \
789	minherit \
790	mmap \
791	pipe \
792	pipe2 \
793	prctl \
794	readpassphrase \
795	rint \
796	sigaction \
797	socketpair \
798	statvfs \
799	strncasecmp \
800	strcasecmp \
801	strlcat \
802	strlcpy \
803	strnlen \
804	strptime \
805	strtok_r \
806	strtoull \
807	sysconf \
808	sysctl \
809        timegm \
810	truncate \
811	uname \
812	usleep \
813	vasprintf \
814	_vscprintf
815)
816
817# Apple messed up when they added some functions: they
818# forgot to decorate them with appropriate AVAILABLE_MAC_OS_VERSION
819# checks.
820
821# We should only probe for these functions if we are sure that we
822# are not targeting OS X 10.9 or earlier.
823AC_MSG_CHECKING([for a pre-Yosemite OS X build target])
824AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
825#ifdef __APPLE__
826#  include <AvailabilityMacros.h>
827#  ifndef MAC_OS_X_VERSION_10_10
828#    define MAC_OS_X_VERSION_10_10 101000
829#  endif
830#  if defined(MAC_OS_X_VERSION_MIN_REQUIRED)
831#    if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_10
832#      error "Running on Mac OS X 10.9 or earlier"
833#    endif
834#  endif
835#endif
836]], [[]])],
837   [on_macos_pre_10_10=no ; AC_MSG_RESULT([no])],
838   [on_macos_pre_10_10=yes; AC_MSG_RESULT([yes])])
839
840if test "$on_macos_pre_10_10" = "no"; then
841  AC_CHECK_FUNCS(
842        mach_approximate_time \
843  )
844fi
845
846# We should only probe for these functions if we are sure that we
847# are not targeting OSX 10.11 or earlier.
848AC_MSG_CHECKING([for a pre-Sierra OSX build target])
849AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
850#ifdef __APPLE__
851#  include <AvailabilityMacros.h>
852#  ifndef MAC_OS_X_VERSION_10_12
853#    define MAC_OS_X_VERSION_10_12 101200
854#  endif
855#  if defined(MAC_OS_X_VERSION_MIN_REQUIRED)
856#    if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_12
857#      error "Running on Mac OSX 10.11 or earlier"
858#    endif
859#  endif
860#endif
861]], [[]])],
862   [on_macos_pre_10_12=no ; AC_MSG_RESULT([no])],
863   [on_macos_pre_10_12=yes; AC_MSG_RESULT([yes])])
864
865if test "$on_macos_pre_10_12" = "no"; then
866  AC_CHECK_FUNCS(
867        clock_gettime \
868        getentropy \
869  )
870fi
871
872if test "$bwin32" != "true"; then
873  AC_CHECK_HEADERS(pthread.h)
874  AC_CHECK_FUNCS(pthread_create)
875  AC_CHECK_FUNCS(pthread_condattr_setclock)
876fi
877
878if test "$bwin32" = "true"; then
879  AC_CHECK_DECLS([SecureZeroMemory, _getwch], , , [
880#include <windows.h>
881#include <conio.h>
882#include <wchar.h>
883                 ])
884fi
885
886AM_CONDITIONAL(BUILD_READPASSPHRASE_C,
887  test "x$ac_cv_func_readpassphrase" = "xno" && test "$bwin32" = "false")
888
889AC_CHECK_FUNCS(glob)
890
891AC_MSG_CHECKING([whether free(NULL) works])
892AC_RUN_IFELSE([AC_LANG_PROGRAM([
893  #include <stdlib.h>
894], [
895char *p = NULL;
896free(p);
897])],
898[free_null_ok=true; AC_MSG_RESULT(yes)],
899[free_null_ok=false; AC_MSG_RESULT(no)],
900[free_null_ok=cross; AC_MSG_RESULT(cross)])
901
902if test "$free_null_ok" = "false"; then
903   AC_MSG_ERROR([Your libc implementation doesn't allow free(NULL), as required by C99.])
904fi
905
906dnl ------------------------------------------------------
907dnl Where do you live, libevent?  And how do we call you?
908
909if test "$bwin32" = "true"; then
910  TOR_LIB_WS32=-lws2_32
911  TOR_LIB_IPHLPAPI=-liphlpapi
912  TOR_LIB_SHLWAPI=-lshlwapi
913  # Some of the cargo-cults recommend -lwsock32 as well, but I don't
914  # think it's actually necessary.
915  TOR_LIB_GDI=-lgdi32
916  TOR_LIB_USERENV=-luserenv
917  TOR_LIB_BCRYPT=-lbcrypt
918  TOR_LIB_CRYPT32=-lcrypt32
919else
920  TOR_LIB_WS32=
921  TOR_LIB_GDI=
922  TOR_LIB_USERENV=
923fi
924AC_SUBST(TOR_LIB_WS32)
925AC_SUBST(TOR_LIB_GDI)
926AC_SUBST(TOR_LIB_IPHLPAPI)
927AC_SUBST(TOR_LIB_BCRYPT)
928AC_SUBST(TOR_LIB_CRYPT32)
929AC_SUBST(TOR_LIB_SHLWAPI)
930AC_SUBST(TOR_LIB_USERENV)
931
932tor_libevent_pkg_redhat="libevent"
933tor_libevent_pkg_debian="libevent-dev"
934tor_libevent_devpkg_redhat="libevent-devel"
935tor_libevent_devpkg_debian="libevent-dev"
936
937dnl On Gnu/Linux or any place we require it, we'll add librt to the Libevent
938dnl linking for static builds.
939STATIC_LIBEVENT_FLAGS=""
940if test "$enable_static_libevent" = "yes"; then
941    if test "$have_rt" = "yes"; then
942      STATIC_LIBEVENT_FLAGS=" -lrt "
943    fi
944fi
945
946TOR_SEARCH_LIBRARY(libevent, $trylibeventdir, [-levent $STATIC_LIBEVENT_FLAGS $TOR_LIB_IPHLPAPI $TOR_LIB_BCRYPT $TOR_LIB_WS32], [
947#ifdef _WIN32
948#include <winsock2.h>
949#endif
950#include <sys/time.h>
951#include <sys/types.h>
952#include <event2/event.h>], [
953#ifdef _WIN32
954#include <winsock2.h>
955#endif
956struct event_base;
957struct event_base *event_base_new(void);
958void event_base_free(struct event_base *);],
959    [
960#ifdef _WIN32
961{WSADATA d; WSAStartup(0x101,&d); }
962#endif
963event_base_free(event_base_new());
964], [--with-libevent-dir], [/opt/libevent])
965
966dnl Determine the incantation needed to link libevent.
967save_LIBS="$LIBS"
968save_LDFLAGS="$LDFLAGS"
969save_CPPFLAGS="$CPPFLAGS"
970
971LIBS="$STATIC_LIBEVENT_FLAGS $TOR_LIB_WS32 $save_LIBS"
972LDFLAGS="$TOR_LDFLAGS_libevent $LDFLAGS"
973CPPFLAGS="$TOR_CPPFLAGS_libevent $CPPFLAGS"
974
975AC_CHECK_HEADERS(event2/event.h event2/dns.h event2/bufferevent_ssl.h)
976
977if test "$enable_static_libevent" = "yes"; then
978   if test "$tor_cv_library_libevent_dir" = "(system)"; then
979     AC_MSG_ERROR("You must specify an explicit --with-libevent-dir=x option when using --enable-static-libevent")
980   else
981     TOR_LIBEVENT_LIBS="$TOR_LIBDIR_libevent/libevent.a $STATIC_LIBEVENT_FLAGS"
982   fi
983else
984     if test "x$ac_cv_header_event2_event_h" = "xyes"; then
985       AC_SEARCH_LIBS(event_new, [event event_core], , AC_MSG_ERROR("libevent2 is installed but linking it failed while searching for event_new"))
986       AC_SEARCH_LIBS(evdns_base_new, [event event_extra], , AC_MSG_ERROR("libevent2 is installed but linking it failed while searching for evdns_base_new"))
987
988       if test "$ac_cv_search_event_new" != "none required"; then
989         TOR_LIBEVENT_LIBS="$ac_cv_search_event_new"
990       fi
991       if test "$ac_cv_search_evdns_base_new" != "none required"; then
992         TOR_LIBEVENT_LIBS="$ac_cv_search_evdns_base_new $TOR_LIBEVENT_LIBS"
993       fi
994     else
995       AC_MSG_ERROR("libevent2 is required but the headers could not be found")
996     fi
997fi
998
999dnl Now check for particular libevent functions.
1000AC_CHECK_FUNCS([evutil_secure_rng_set_urandom_device_file \
1001                evutil_secure_rng_add_bytes \
1002                evdns_base_get_nameserver_addr \
1003
1004])
1005
1006LIBS="$save_LIBS"
1007LDFLAGS="$save_LDFLAGS"
1008CPPFLAGS="$save_CPPFLAGS"
1009
1010dnl Check that libevent is at least at version 2.0.10, the first stable
1011dnl release of its series
1012CPPFLAGS="$CPPFLAGS $TOR_CPPFLAGS_libevent"
1013AC_MSG_CHECKING([whether Libevent is new enough])
1014AC_COMPILE_IFELSE([AC_LANG_SOURCE([
1015#include <event2/event.h>
1016#if !defined(LIBEVENT_VERSION_NUMBER) || LIBEVENT_VERSION_NUMBER < 0x02000a00
1017#error
1018int x = y(zz);
1019#else
1020int x = 1;
1021#endif
1022])], [ AC_MSG_RESULT([yes]) ],
1023   [ AC_MSG_RESULT([no])
1024     AC_MSG_ERROR([Libevent is not new enough.  We require 2.0.10-stable or later]) ] )
1025
1026LIBS="$save_LIBS"
1027LDFLAGS="$save_LDFLAGS"
1028CPPFLAGS="$save_CPPFLAGS"
1029
1030AC_SUBST(TOR_LIBEVENT_LIBS)
1031
1032dnl ------------------------------------------------------
1033dnl Where do you live, libm?
1034
1035dnl On some platforms (Haiku/BeOS) the math library is
1036dnl part of libroot. In which case don't link against lm
1037TOR_LIB_MATH=""
1038save_LIBS="$LIBS"
1039AC_SEARCH_LIBS(pow, [m], , AC_MSG_ERROR([Could not find pow in libm or libc.]))
1040if test "$ac_cv_search_pow" != "none required"; then
1041    TOR_LIB_MATH="$ac_cv_search_pow"
1042fi
1043LIBS="$save_LIBS"
1044AC_SUBST(TOR_LIB_MATH)
1045
1046dnl ------------------------------------------------------
1047dnl Hello, NSS.  You're new around here.
1048if test "x$enable_nss" = "xyes"; then
1049  PKG_CHECK_MODULES(NSS,
1050     [nss],
1051     [have_nss=yes],
1052     [have_nss=no; AC_MSG_ERROR([You asked for NSS but I can't find it, $pkg_config_user_action, or set NSS_CFLAGS and NSS_LIBS.])])
1053  AC_SUBST(NSS_CFLAGS)
1054  AC_SUBST(NSS_LIBS)
1055fi
1056
1057dnl ------------------------------------------------------
1058dnl Where do you live, openssl?  And how do we call you?
1059
1060if test "x$enable_nss" != "xyes"; then
1061
1062tor_openssl_pkg_redhat="openssl"
1063tor_openssl_pkg_debian="libssl-dev"
1064tor_openssl_devpkg_redhat="openssl-devel"
1065tor_openssl_devpkg_debian="libssl-dev"
1066
1067ALT_openssl_WITHVAL=""
1068AC_ARG_WITH(ssl-dir,
1069  AS_HELP_STRING(--with-ssl-dir=PATH, [obsolete alias for --with-openssl-dir]),
1070  [
1071      if test "x$withval" != "xno" && test "x$withval" != "x"; then
1072         ALT_openssl_WITHVAL="$withval"
1073      fi
1074  ])
1075
1076AC_MSG_NOTICE([Now, we'll look for OpenSSL >= 1.0.1])
1077TOR_SEARCH_LIBRARY(openssl, $tryssldir, [-lssl -lcrypto $TOR_LIB_GDI $TOR_LIB_WS32 $TOR_LIB_CRYPT32],
1078    [#include <openssl/ssl.h>
1079     char *getenv(const char *);],
1080    [struct ssl_cipher_st;
1081     unsigned SSL_CIPHER_get_id(const struct ssl_cipher_st *);
1082     char *getenv(const char *);],
1083    dnl This funny-looking test program calls getenv, so that the compiler
1084    dnl will neither make code that call SSL_CIPHER_get_id(NULL) [producing
1085    dnl a crash], nor optimize out the call to SSL_CIPHER_get_id().
1086    dnl We look for SSL_cipher_get_id() because it is present in
1087    dnl OpenSSL >=1.0.1, because it is not deprecated, and because Tor
1088    dnl depends on it.
1089    [if (getenv("THIS_SHOULDNT_BE_SET_X201803")) SSL_CIPHER_get_id((void *)0);], [],
1090    [/usr/local/opt/openssl /usr/local/openssl /usr/lib/openssl /usr/local/ssl /usr/lib/ssl /usr/local /opt/openssl])
1091
1092if test "$enable_static_openssl" = "yes"; then
1093   if test "$tor_cv_library_openssl_dir" = "(system)"; then
1094     AC_MSG_ERROR("You must specify an explicit --with-openssl-dir=x option when using --enable-static-openssl")
1095   else
1096     TOR_OPENSSL_LIBS="$TOR_LIBDIR_openssl/libssl.a $TOR_LIBDIR_openssl/libcrypto.a $TOR_LIB_WS32 $TOR_LIB_CRYPT32 $TOR_LIB_BCRYPT"
1097   fi
1098else
1099     TOR_OPENSSL_LIBS="-lssl -lcrypto"
1100fi
1101AC_SUBST(TOR_OPENSSL_LIBS)
1102
1103dnl Now validate openssl, and check for particular openssl functions.
1104save_LIBS="$LIBS"
1105save_LDFLAGS="$LDFLAGS"
1106save_CPPFLAGS="$CPPFLAGS"
1107LIBS="$TOR_OPENSSL_LIBS $LIBS"
1108LDFLAGS="$TOR_LDFLAGS_openssl $LDFLAGS"
1109CPPFLAGS="$TOR_CPPFLAGS_openssl $CPPFLAGS"
1110
1111dnl Tor currently uses a number of APIs that are deprecated in OpenSSL 3.0.0
1112dnl and later.  We want to migrate away from them, but that will be a lot of
1113dnl work. (See ticket tor#40166.)  For now, we disable the deprecation
1114dnl warnings.
1115
1116AC_MSG_CHECKING([for OpenSSL >= 3.0.0])
1117AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
1118#include <openssl/opensslv.h>
1119#if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER <= 0x30000000L
1120#error "you_have_version_3"
1121#endif
1122   ]], [[]])],
1123   [ AC_MSG_RESULT([no]) ],
1124   [ AC_MSG_RESULT([yes]);
1125     AC_DEFINE(OPENSSL_SUPPRESS_DEPRECATED, 1, [disable openssl deprecated-function warnings]) ])
1126
1127AC_MSG_CHECKING([for OpenSSL < 1.0.1])
1128AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
1129#include <openssl/opensslv.h>
1130#if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x1000100fL
1131#error "too old"
1132#endif
1133   ]], [[]])],
1134   [ AC_MSG_RESULT([no]) ],
1135   [ AC_MSG_ERROR([OpenSSL is too old. We require 1.0.1 or later. You can specify a path to a newer one with --with-openssl-dir.]) ])
1136
1137AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
1138#include <openssl/opensslv.h>
1139#include <openssl/evp.h>
1140#if defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_ECDH) || defined(OPENSSL_NO_ECDSA)
1141#error "no ECC"
1142#endif
1143#if !defined(NID_X9_62_prime256v1) || !defined(NID_secp224r1)
1144#error "curves unavailable"
1145#endif
1146   ]], [[]])],
1147   [ : ],
1148   [ AC_MSG_ERROR([OpenSSL is built without full ECC support, including curves P256 and P224. You can specify a path to one with ECC support with --with-openssl-dir.]) ])
1149
1150dnl Let's see if we have a version mismatch between includes and libs.
1151AC_MSG_CHECKING([for significant mismatch between openssl headers and libraries])
1152ac_retval=foo
1153AC_RUN_IFELSE([AC_LANG_SOURCE([AC_LANG_PROGRAM([[
1154 #include <openssl/opensslv.h>
1155 #include <openssl/crypto.h>
1156]], [[
1157  /* Include major, minor, and fix, but not patch or status. */
1158  unsigned long mask = 0xfffff000;
1159  unsigned long linking = OpenSSL_version_num() & mask;
1160  unsigned long running = OPENSSL_VERSION_NUMBER & mask;
1161  return !(linking==running);
1162]])])], [openssl_ver_mismatch=no], [
1163   # This is a kludge to figure out whether compilation failed, or whether
1164   # running the program failed.
1165   if test "$ac_retval" = "1"; then
1166      openssl_ver_mismatch=inconclusive
1167   else
1168      openssl_ver_mismatch=yes
1169   fi], [openssl_ver_mismatch=cross])
1170AC_MSG_RESULT([$openssl_ver_mismatch])
1171
1172AC_CHECK_MEMBERS([struct ssl_method_st.get_cipher_by_char], , ,
1173[#include <openssl/ssl.h>
1174])
1175
1176dnl OpenSSL functions which we might not have.  In theory, we could just
1177dnl check the openssl version number, but in practice that gets pretty
1178dnl confusing with LibreSSL, OpenSSL, and various distributions' patches
1179dnl to them.
1180AC_CHECK_FUNCS([ \
1181		ERR_load_KDF_strings \
1182		EVP_PBE_scrypt \
1183		SSL_CIPHER_find \
1184		SSL_CTX_set1_groups_list \
1185		SSL_CTX_set_security_level \
1186		SSL_SESSION_get_master_key \
1187		SSL_get_client_ciphers \
1188		SSL_get_client_random \
1189		SSL_get_server_random \
1190		TLS_method \
1191	       ])
1192
1193dnl Check if OpenSSL structures are opaque
1194AC_CHECK_MEMBERS([SSL.state], , ,
1195[#include <openssl/ssl.h>
1196])
1197
1198AC_CHECK_SIZEOF(SHA_CTX, , [AC_INCLUDES_DEFAULT()
1199#include <openssl/sha.h>
1200])
1201
1202fi # enable_nss
1203
1204dnl We will someday make KECCAK_TINY optional, but for now we still need
1205dnl it for SHAKE, since OpenSSL's SHAKE can't be squeezed more than
1206dnl once.  See comment in the definition of crypto_xof_t.
1207
1208dnl AM_CONDITIONAL(BUILD_KECCAK_TINY,
1209dnl   test "x$ac_cv_func_EVP_sha3_256" != "xyes")
1210
1211AM_CONDITIONAL(BUILD_KECCAK_TINY, true)
1212
1213dnl ======================================================================
1214dnl Can we use KIST?
1215
1216dnl Define the set of checks for KIST scheduler support.
1217AC_DEFUN([CHECK_KIST_SUPPORT],[
1218  dnl KIST needs struct tcp_info and for certain members to exist.
1219  AC_CHECK_MEMBERS(
1220    [struct tcp_info.tcpi_unacked, struct tcp_info.tcpi_snd_mss],
1221    , ,[[#include <netinet/tcp.h>]])
1222  dnl KIST needs SIOCOUTQNSD to exist for an ioctl call.
1223  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [
1224                     #include <linux/sockios.h>
1225                     #ifndef SIOCOUTQNSD
1226                     #error
1227                     #endif
1228                     ])], have_siocoutqnsd=yes, have_siocoutqnsd=no)
1229  if test "x$have_siocoutqnsd" = "xyes"; then
1230    if test "x$ac_cv_member_struct_tcp_info_tcpi_unacked" = "xyes"; then
1231      if test "x$ac_cv_member_struct_tcp_info_tcpi_snd_mss" = "xyes"; then
1232        have_kist_support=yes
1233      fi
1234    fi
1235  fi
1236])
1237dnl Now, trigger the check.
1238CHECK_KIST_SUPPORT
1239AS_IF([test "x$have_kist_support" = "xyes"],
1240      [AC_DEFINE(HAVE_KIST_SUPPORT, 1, [Defined if KIST scheduler is supported
1241                                        on this system])],
1242      [AC_MSG_NOTICE([KIST scheduler can't be used. Missing support.])])
1243
1244LIBS="$save_LIBS"
1245LDFLAGS="$save_LDFLAGS"
1246CPPFLAGS="$save_CPPFLAGS"
1247
1248dnl ------------------------------------------------------
1249dnl Where do you live, zlib?  And how do we call you?
1250
1251tor_zlib_pkg_redhat="zlib"
1252tor_zlib_pkg_debian="zlib1g"
1253tor_zlib_devpkg_redhat="zlib-devel"
1254tor_zlib_devpkg_debian="zlib1g-dev"
1255
1256TOR_SEARCH_LIBRARY(zlib, $tryzlibdir, [-lz],
1257    [#include <zlib.h>],
1258    [const char * zlibVersion(void);],
1259    [zlibVersion();], [--with-zlib-dir],
1260    [/opt/zlib])
1261
1262if test "$enable_static_zlib" = "yes"; then
1263   if test "$tor_cv_library_zlib_dir" = "(system)"; then
1264     AC_MSG_ERROR("You must specify an explicit --with-zlib-dir=x option when
1265 using --enable-static-zlib")
1266   else
1267     TOR_ZLIB_LIBS="$TOR_LIBDIR_zlib/libz.a"
1268   fi
1269else
1270     TOR_ZLIB_LIBS="-lz"
1271fi
1272AC_SUBST(TOR_ZLIB_LIBS)
1273
1274dnl ------------------------------------------------------
1275dnl Where we do we find lzma?
1276
1277AC_ARG_ENABLE(lzma,
1278      AS_HELP_STRING(--enable-lzma, [enable support for the LZMA compression scheme.]),
1279      [case "${enableval}" in
1280        "yes") ;;
1281        "no")  ;;
1282        * ) AC_MSG_ERROR(bad value for --enable-lzma) ;;
1283      esac], [enable_lzma=auto])
1284
1285if test "x$enable_lzma" = "xno"; then
1286    have_lzma=no;
1287else
1288    PKG_CHECK_MODULES([LZMA],
1289                      [liblzma],
1290                      have_lzma=yes,
1291                      have_lzma=no)
1292
1293    if test "x$have_lzma" = "xno" ; then
1294        tor_incr_n_warnings
1295        AC_MSG_WARN([Unable to find liblzma, $pkg_config_user_action, or set LZMA_CFLAGS and LZMA_LIBS.])
1296    fi
1297fi
1298
1299if test "x$have_lzma" = "xyes"; then
1300    AC_DEFINE(HAVE_LZMA,1,[Have LZMA])
1301    TOR_LZMA_CFLAGS="${LZMA_CFLAGS}"
1302    TOR_LZMA_LIBS="${LZMA_LIBS}"
1303fi
1304AC_SUBST(TOR_LZMA_CFLAGS)
1305AC_SUBST(TOR_LZMA_LIBS)
1306
1307dnl ------------------------------------------------------
1308dnl Where we do we find zstd?
1309
1310AC_ARG_ENABLE(zstd,
1311      AS_HELP_STRING(--enable-zstd, [enable support for the Zstandard compression scheme.]),
1312      [case "${enableval}" in
1313        "yes") ;;
1314        "no")  ;;
1315        * ) AC_MSG_ERROR(bad value for --enable-zstd) ;;
1316      esac], [enable_zstd=auto])
1317
1318if test "x$enable_zstd" = "xno"; then
1319    have_zstd=no;
1320else
1321    PKG_CHECK_MODULES([ZSTD],
1322                      [libzstd >= 1.1],
1323                      have_zstd=yes,
1324                      have_zstd=no)
1325
1326    if test "x$have_zstd" = "xno" ; then
1327        tor_incr_n_warnings
1328        AC_MSG_WARN([Unable to find libzstd, $pkg_config_user_action, or set ZSTD_CFLAGS and ZSTD_LIBS.])
1329    fi
1330fi
1331
1332if test "x$have_zstd" = "xyes"; then
1333    AC_DEFINE(HAVE_ZSTD,1,[Have Zstd])
1334    TOR_ZSTD_CFLAGS="${ZSTD_CFLAGS}"
1335    TOR_ZSTD_LIBS="${ZSTD_LIBS}"
1336
1337    dnl now check for zstd functions
1338    save_LIBS="$LIBS"
1339    save_CFLAGS="$CFLAGS"
1340    LIBS="$LIBS $ZSTD_LIBS"
1341    CFLAGS="$CFLAGS $ZSTD_CFLAGS"
1342    AC_CHECK_FUNCS(ZSTD_estimateCStreamSize \
1343                   ZSTD_estimateDCtxSize)
1344    LIBS="$save_LIBS"
1345    CFLAGS="$save_CFLAGS"
1346fi
1347AC_SUBST(TOR_ZSTD_CFLAGS)
1348AC_SUBST(TOR_ZSTD_LIBS)
1349
1350dnl ----------------------------------------------------------------------
1351dnl Check if libcap is available for capabilities.
1352
1353tor_cap_pkg_debian="libcap2"
1354tor_cap_pkg_redhat="libcap"
1355tor_cap_devpkg_debian="libcap-dev"
1356tor_cap_devpkg_redhat="libcap-devel"
1357
1358AC_CHECK_LIB([cap], [cap_init], [],
1359  AC_MSG_NOTICE([Libcap was not found. Capabilities will not be usable.])
1360)
1361AC_CHECK_FUNCS(cap_set_proc)
1362
1363dnl ---------------------------------------------------------------------
1364dnl Now that we know about our major libraries, we can check for compiler
1365dnl and linker hardening options.  We need to do this with the libraries known,
1366dnl since sometimes the linker will like an option but not be willing to
1367dnl use it with a build of a library.
1368
1369all_ldflags_for_check="$TOR_LDFLAGS_zlib $TOR_LDFLAGS_openssl $TOR_LDFLAGS_libevent"
1370all_libs_for_check="$TOR_ZLIB_LIBS $TOR_LIB_MATH $TOR_LIBEVENT_LIBS $TOR_OPENSSL_LIBS $TOR_SYSTEMD_LIBS $TOR_LIB_WS32 $TOR_LIB_GDI $TOR_LIB_USERENV $TOR_CAP_LIBS"
1371
1372CFLAGS_FTRAPV=
1373CFLAGS_FWRAPV=
1374CFLAGS_ASAN=
1375CFLAGS_UBSAN=
1376
1377
1378AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [
1379#if !defined(__clang__)
1380#error
1381#endif])], have_clang=yes, have_clang=no)
1382
1383if test "x$enable_pic" = "xyes"; then
1384    TOR_CHECK_CFLAGS(-fPIC)
1385fi
1386
1387if test "x$enable_gcc_hardening" != "xno"; then
1388    CFLAGS="$CFLAGS -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2"
1389    if test "x$have_clang" = "xyes"; then
1390        TOR_CHECK_CFLAGS(-Qunused-arguments)
1391    fi
1392    TOR_CHECK_CFLAGS(-fstack-protector-all, also_link)
1393    AS_VAR_PUSHDEF([can_compile], [tor_cv_cflags_-fstack-protector-all])
1394    AS_VAR_PUSHDEF([can_link], [tor_can_link_-fstack-protector-all])
1395m4_ifdef([AS_VAR_IF],[
1396    AS_VAR_IF(can_compile, [yes],
1397        AS_VAR_IF(can_link, [yes],
1398                  [],
1399                  AC_MSG_ERROR([We tried to build with stack protection; it looks like your compiler supports it but your libc does not provide it. Are you missing libssp? (You can --disable-gcc-hardening to ignore this error.)]))
1400        )])
1401    AS_VAR_POPDEF([can_link])
1402    AS_VAR_POPDEF([can_compile])
1403    TOR_CHECK_CFLAGS(-Wstack-protector)
1404    TOR_CHECK_CFLAGS(--param ssp-buffer-size=1)
1405    if test "$bwin32" = "false" && test "$enable_libfuzzer" != "yes" && test "$enable_oss_fuzz" != "yes"; then
1406       if test "$enable_pic" != "yes"; then
1407           # If we have already enabled -fPIC, then we don't also need to
1408           # compile with -fPIE...
1409           TOR_CHECK_CFLAGS(-fPIE)
1410       fi
1411       # ... but we want to link our executables with -pie in any case, since
1412       # they're executables, not a library.
1413       TOR_CHECK_LDFLAGS(-pie, "$all_ldflags_for_check", "$all_libs_for_check")
1414    fi
1415    TOR_TRY_COMPILE_WITH_CFLAGS(-fwrapv, also_link, CFLAGS_FWRAPV="-fwrapv", true)
1416
1417   AC_MSG_CHECKING([whether we can run hardened binaries])
1418   AC_RUN_IFELSE([AC_LANG_PROGRAM([], [return 0;])],
1419        [AC_MSG_RESULT([yes])],
1420        [AC_MSG_RESULT([no])
1421         AC_MSG_ERROR([dnl
1422 We can link with compiler hardening options, but we can't run with them.
1423 That's a bad sign! If you must, you can pass --disable-gcc-hardening to
1424 configure, but it would be better to figure out what the underlying problem
1425 is.])],
1426        [AC_MSG_RESULT([cross])])
1427fi
1428
1429if test "$fragile_hardening" = "yes"; then
1430    TOR_TRY_COMPILE_WITH_CFLAGS(-ftrapv, also_link, CFLAGS_FTRAPV="-ftrapv", true)
1431   if test "$tor_cv_cflags__ftrapv" = "yes" && test "$tor_can_link__ftrapv" != "yes"; then
1432      tor_incr_n_warnings
1433      AC_MSG_WARN([The compiler supports -ftrapv, but for some reason I was not able to link with -ftrapv. Are you missing run-time support? Run-time hardening will not work as well as it should.])
1434   fi
1435
1436   if test "$tor_cv_cflags__ftrapv" != "yes"; then
1437     AC_MSG_ERROR([You requested fragile hardening, but the compiler does not seem to support -ftrapv.])
1438   fi
1439
1440   TOR_TRY_COMPILE_WITH_CFLAGS([-fsanitize=address], also_link, CFLAGS_ASAN="-fsanitize=address", true)
1441    if test "$tor_cv_cflags__fsanitize_address" = "yes" && test "$tor_can_link__fsanitize_address" != "yes"; then
1442      AC_MSG_ERROR([The compiler supports -fsanitize=address, but for some reason I was not able to link when using it. Are you missing run-time support? With GCC you need libubsan.*, and with Clang you need libclang_rt.ubsan*])
1443    fi
1444
1445   TOR_TRY_COMPILE_WITH_CFLAGS([-fsanitize=undefined], also_link, CFLAGS_UBSAN="-fsanitize=undefined", true)
1446    if test "$tor_cv_cflags__fsanitize_address" = "yes" && test "$tor_can_link__fsanitize_address" != "yes"; then
1447      AC_MSG_ERROR([The compiler supports -fsanitize=undefined, but for some reason I was not able to link when using it. Are you missing run-time support? With GCC you need libasan.*, and with Clang you need libclang_rt.ubsan*])
1448    fi
1449
1450   TOR_TRY_COMPILE_WITH_CFLAGS([-fno-sanitize=float-divide-by-zero], also_link, CFLAGS_UBSAN="-fno-sanitize=float-divide-by-zero", true)
1451    if test "$tor_cv_cflags__fno_sanitize_float_divide_by_zero" = "yes" && test "$tor_can_link__fno_sanitize_float_divide_by_zero" != "yes"; then
1452      AC_MSG_ERROR([The compiler supports -fno-sanitize=float-divide-by-zero, but for some reason I was not able to link when using it. Are you missing run-time support? With GCC you need libasan.*, and with Clang you need libclang_rt.ubsan*])
1453    fi
1454
1455TOR_CHECK_CFLAGS([-fno-omit-frame-pointer])
1456fi
1457
1458dnl Find the correct libraries to add in order to use the sanitizers.
1459dnl
1460dnl When building Rust, Cargo will run the linker with the -nodefaultlibs
1461dnl option, which will prevent the compiler from linking the sanitizer
1462dnl libraries it needs.  We need to specify them manually.
1463dnl
1464dnl What's more, we need to specify them in a linker script rather than
1465dnl from build.rs: these options aren't allowed in the cargo:rustc-flags
1466dnl variable.
1467RUST_LINKER_OPTIONS=""
1468if test "x$have_clang" = "xyes"; then
1469	if test "x$CFLAGS_ASAN" != "x"; then
1470		RUST_LINKER_OPTIONS="$RUST_LINKER_OPTIONS -Clink-arg=$CFLAGS_ASAN -Cdefault-linker-libraries"
1471	fi
1472	if test "x$CFLAGS_UBSAN" != "x"; then
1473        	RUST_LINKER_OPTIONS="$RUST_LINKER_OPTIONS -Clink-arg=$CFLAGS_UBSAN -Cdefault-linker-libraries"
1474	fi
1475else
1476	if test "x$CFLAGS_ASAN" != "x"; then
1477		RUST_LINKER_OPTIONS="$RUST_LINKER_OPTIONS -Clink-arg=-fsanitize=address -Cdefault-linker-libraries"
1478	fi
1479	if test "x$CFLAGS_UBSAN" != "x"; then
1480        	RUST_LINKER_OPTIONS="$RUST_LINKER_OPTIONS -Clink-arg=-fsanitize=undefined -Cdefault-linker-libraries"
1481	fi
1482fi
1483AC_SUBST(RUST_LINKER_OPTIONS)
1484
1485CFLAGS_BUGTRAP="$CFLAGS_FTRAPV $CFLAGS_ASAN $CFLAGS_UBSAN"
1486CFLAGS_CONSTTIME="$CFLAGS_FWRAPV"
1487
1488mulodi_fixes_ftrapv=no
1489if test "$have_clang" = "yes"; then
1490  saved_CFLAGS="$CFLAGS"
1491  CFLAGS="$CFLAGS $CFLAGS_FTRAPV"
1492  AC_MSG_CHECKING([whether clang -ftrapv can link a 64-bit int multiply])
1493  AC_LINK_IFELSE([
1494      AC_LANG_SOURCE([[
1495          #include <stdint.h>
1496          #include <stdlib.h>
1497	  int main(int argc, char **argv)
1498	  {
1499            int64_t x = ((int64_t)atoi(argv[1])) * (int64_t)atoi(argv[2])
1500	                * (int64_t)atoi(argv[3]);
1501	    return x == 9;
1502	  } ]])],
1503	  [ftrapv_can_link=yes; AC_MSG_RESULT([yes])],
1504	  [ftrapv_can_link=no; AC_MSG_RESULT([no])])
1505  if test "$ftrapv_can_link" = "no"; then
1506    AC_MSG_CHECKING([whether defining __mulodi4 fixes that])
1507    AC_LINK_IFELSE([
1508      AC_LANG_SOURCE([[
1509          #include <stdint.h>
1510          #include <stdlib.h>
1511	  int64_t __mulodi4(int64_t a, int64_t b, int *overflow) {
1512             *overflow=0;
1513	     return a;
1514          }
1515	  int main(int argc, char **argv)
1516	  {
1517            int64_t x = ((int64_t)atoi(argv[1])) * (int64_t)atoi(argv[2])
1518	                * (int64_t)atoi(argv[3]);
1519	    return x == 9;
1520	  } ]])],
1521	  [mulodi_fixes_ftrapv=yes; AC_MSG_RESULT([yes])],
1522	  [mulodi_fixes_ftrapv=no; AC_MSG_RESULT([no])])
1523  fi
1524  CFLAGS="$saved_CFLAGS"
1525fi
1526
1527AM_CONDITIONAL(ADD_MULODI4, test "$mulodi_fixes_ftrapv" = "yes")
1528
1529dnl These cflags add bunches of branches, and we haven't been able to
1530dnl persuade ourselves that they're suitable for code that needs to be
1531dnl constant time.
1532AC_SUBST(CFLAGS_BUGTRAP)
1533dnl These cflags are variant ones suitable for code that needs to be
1534dnl constant-time.
1535AC_SUBST(CFLAGS_CONSTTIME)
1536
1537if test "x$enable_linker_hardening" != "xno"; then
1538    TOR_CHECK_LDFLAGS(-z relro -z now, "$all_ldflags_for_check", "$all_libs_for_check")
1539fi
1540
1541# For backtrace support
1542TOR_CHECK_LDFLAGS(-rdynamic)
1543
1544dnl ------------------------------------------------------
1545dnl Now see if we have a -fomit-frame-pointer compiler option.
1546
1547saved_CFLAGS="$CFLAGS"
1548TOR_CHECK_CFLAGS(-fomit-frame-pointer)
1549F_OMIT_FRAME_POINTER=''
1550if test "$saved_CFLAGS" != "$CFLAGS"; then
1551  if test "$fragile_hardening" = "yes"; then
1552    F_OMIT_FRAME_POINTER='-fomit-frame-pointer'
1553  fi
1554fi
1555CFLAGS="$saved_CFLAGS"
1556AC_SUBST(F_OMIT_FRAME_POINTER)
1557
1558dnl ------------------------------------------------------
1559dnl If we are adding -fomit-frame-pointer (or if the compiler's doing it
1560dnl for us, as GCC 4.6 and later do at many optimization levels), then
1561dnl we should try to add -fasynchronous-unwind-tables so that our backtrace
1562dnl code will work.
1563TOR_CHECK_CFLAGS(-fasynchronous-unwind-tables)
1564
1565dnl ============================================================
1566dnl Check for libseccomp
1567
1568if test "x$enable_seccomp" != "xno"; then
1569  AC_CHECK_HEADERS([seccomp.h])
1570  AC_SEARCH_LIBS(seccomp_init, [seccomp])
1571fi
1572
1573dnl ============================================================
1574dnl Check for libscrypt
1575
1576if test "x$enable_libscrypt" != "xno"; then
1577  AC_CHECK_HEADERS([libscrypt.h])
1578  AC_SEARCH_LIBS(libscrypt_scrypt, [scrypt])
1579  AC_CHECK_FUNCS([libscrypt_scrypt])
1580fi
1581
1582dnl ============================================================
1583dnl We need an implementation of curve25519.
1584
1585dnl set these defaults.
1586build_curve25519_donna=no
1587build_curve25519_donna_c64=no
1588use_curve25519_donna=no
1589use_curve25519_nacl=no
1590CURVE25519_LIBS=
1591
1592dnl The best choice is using curve25519-donna-c64, but that requires
1593dnl that we
1594AC_CACHE_CHECK([whether we can use curve25519-donna-c64],
1595  tor_cv_can_use_curve25519_donna_c64,
1596  [AC_RUN_IFELSE(
1597    [AC_LANG_PROGRAM([dnl
1598      #include <stdint.h>
1599      typedef unsigned uint128_t __attribute__((mode(TI)));
1600  int func(uint64_t a, uint64_t b) {
1601           uint128_t c = ((uint128_t)a) * b;
1602           int ok = ((uint64_t)(c>>96)) == 522859 &&
1603             (((uint64_t)(c>>64))&0xffffffffL) == 3604448702L &&
1604                 (((uint64_t)(c>>32))&0xffffffffL) == 2351960064L &&
1605                 (((uint64_t)(c))&0xffffffffL) == 0;
1606           return ok;
1607      }
1608  ], [dnl
1609    int ok = func( ((uint64_t)2000000000) * 1000000000,
1610                   ((uint64_t)1234567890) << 24);
1611        return !ok;
1612      ])],
1613  [tor_cv_can_use_curve25519_donna_c64=yes],
1614      [tor_cv_can_use_curve25519_donna_c64=no],
1615  [AC_LINK_IFELSE(
1616        [AC_LANG_PROGRAM([dnl
1617      #include <stdint.h>
1618      typedef unsigned uint128_t __attribute__((mode(TI)));
1619  int func(uint64_t a, uint64_t b) {
1620           uint128_t c = ((uint128_t)a) * b;
1621           int ok = ((uint64_t)(c>>96)) == 522859 &&
1622             (((uint64_t)(c>>64))&0xffffffffL) == 3604448702L &&
1623                 (((uint64_t)(c>>32))&0xffffffffL) == 2351960064L &&
1624                 (((uint64_t)(c))&0xffffffffL) == 0;
1625           return ok;
1626      }
1627  ], [dnl
1628    int ok = func( ((uint64_t)2000000000) * 1000000000,
1629    	         ((uint64_t)1234567890) << 24);
1630        return !ok;
1631      ])],
1632          [tor_cv_can_use_curve25519_donna_c64=cross],
1633      [tor_cv_can_use_curve25519_donna_c64=no])])])
1634
1635AC_CHECK_HEADERS([crypto_scalarmult_curve25519.h \
1636                  nacl/crypto_scalarmult_curve25519.h])
1637
1638AC_CACHE_CHECK([for nacl compiled with a fast curve25519 implementation],
1639  tor_cv_can_use_curve25519_nacl,
1640  [tor_saved_LIBS="$LIBS"
1641   LIBS="$LIBS -lnacl"
1642   AC_LINK_IFELSE(
1643     [AC_LANG_PROGRAM([dnl
1644       #ifdef HAVE_CRYPTO_SCALARMULT_CURVE25519_H
1645       #include <crypto_scalarmult_curve25519.h>
1646   #elif defined(HAVE_NACL_CRYPTO_SCALARMULT_CURVE25519_H)
1647   #include <nacl/crypto_scalarmult_curve25519.h>
1648   #endif
1649       #ifdef crypto_scalarmult_curve25519_ref_BYTES
1650   #error Hey, this is the reference implementation! That's not fast.
1651   #endif
1652     ], [
1653   unsigned char *a, *b, *c; crypto_scalarmult_curve25519(a,b,c);
1654     ])], [tor_cv_can_use_curve25519_nacl=yes],
1655     [tor_cv_can_use_curve25519_nacl=no])
1656   LIBS="$tor_saved_LIBS" ])
1657
1658 dnl Okay, now we need to figure out which one to actually use. Fall back
1659 dnl to curve25519-donna.c
1660
1661 if test "x$tor_cv_can_use_curve25519_donna_c64" != "xno"; then
1662   build_curve25519_donna_c64=yes
1663   use_curve25519_donna=yes
1664 elif test "x$tor_cv_can_use_curve25519_nacl" = "xyes"; then
1665   use_curve25519_nacl=yes
1666   CURVE25519_LIBS=-lnacl
1667 else
1668   build_curve25519_donna=yes
1669   use_curve25519_donna=yes
1670 fi
1671
1672if test "x$use_curve25519_donna" = "xyes"; then
1673  AC_DEFINE(USE_CURVE25519_DONNA, 1,
1674            [Defined if we should use an internal curve25519_donna{,_c64} implementation])
1675fi
1676if test "x$use_curve25519_nacl" = "xyes"; then
1677  AC_DEFINE(USE_CURVE25519_NACL, 1,
1678            [Defined if we should use a curve25519 from nacl])
1679fi
1680AM_CONDITIONAL(BUILD_CURVE25519_DONNA,
1681  test "x$build_curve25519_donna" = "xyes")
1682AM_CONDITIONAL(BUILD_CURVE25519_DONNA_C64,
1683  test "x$build_curve25519_donna_c64" = "xyes")
1684AC_SUBST(CURVE25519_LIBS)
1685
1686dnl Make sure to enable support for large off_t if available.
1687AC_SYS_LARGEFILE
1688
1689AC_CHECK_HEADERS([errno.h \
1690		  fcntl.h \
1691		  signal.h \
1692		  string.h \
1693		  sys/capability.h \
1694		  sys/fcntl.h \
1695		  sys/stat.h \
1696		  sys/time.h \
1697		  sys/types.h \
1698		  time.h \
1699		  unistd.h \
1700		  arpa/inet.h \
1701		  crt_externs.h \
1702		  execinfo.h \
1703		  gnu/libc-version.h \
1704		  grp.h \
1705		  ifaddrs.h \
1706		  inttypes.h \
1707		  limits.h \
1708		  linux/types.h \
1709		  mach/vm_inherit.h \
1710		  machine/limits.h \
1711		  malloc.h \
1712		  netdb.h \
1713		  netinet/in.h \
1714		  netinet/in6.h \
1715		  pwd.h \
1716		  readpassphrase.h \
1717		  stdatomic.h \
1718		  sys/eventfd.h \
1719		  sys/file.h \
1720		  sys/ioctl.h \
1721		  sys/limits.h \
1722		  sys/mman.h \
1723		  sys/param.h \
1724		  sys/prctl.h \
1725		  sys/random.h \
1726		  sys/resource.h \
1727		  sys/select.h \
1728		  sys/socket.h \
1729		  sys/statvfs.h \
1730		  sys/syscall.h \
1731		  sys/sysctl.h \
1732		  sys/time.h \
1733		  sys/types.h \
1734		  sys/un.h \
1735		  sys/utime.h \
1736		  sys/wait.h \
1737		  syslog.h \
1738		  utime.h \
1739		  glob.h])
1740
1741AC_CHECK_HEADERS(sys/param.h)
1742
1743AC_CHECK_HEADERS(net/if.h, net_if_found=1, net_if_found=0,
1744[#ifdef HAVE_SYS_TYPES_H
1745#include <sys/types.h>
1746#endif
1747#ifdef HAVE_SYS_SOCKET_H
1748#include <sys/socket.h>
1749#endif])
1750AC_CHECK_HEADERS(net/pfvar.h, net_pfvar_found=1, net_pfvar_found=0,
1751[#ifdef HAVE_SYS_TYPES_H
1752#include <sys/types.h>
1753#endif
1754#ifdef HAVE_SYS_SOCKET_H
1755#include <sys/socket.h>
1756#endif
1757#ifdef HAVE_NET_IF_H
1758#include <net/if.h>
1759#endif
1760#ifdef HAVE_NETINET_IN_H
1761#include <netinet/in.h>
1762#endif])
1763
1764AC_CHECK_HEADERS(linux/if.h,[],[],
1765[
1766#ifdef HAVE_SYS_SOCKET_H
1767#include <sys/socket.h>
1768#endif
1769])
1770
1771AC_CHECK_HEADERS(linux/netfilter_ipv4.h,
1772        linux_netfilter_ipv4=1, linux_netfilter_ipv4=0,
1773[#ifdef HAVE_SYS_TYPES_H
1774#include <sys/types.h>
1775#endif
1776#ifdef HAVE_SYS_SOCKET_H
1777#include <sys/socket.h>
1778#endif
1779#ifdef HAVE_LIMITS_H
1780#include <limits.h>
1781#endif
1782#ifdef HAVE_LINUX_TYPES_H
1783#include <linux/types.h>
1784#endif
1785#ifdef HAVE_NETINET_IN6_H
1786#include <netinet/in6.h>
1787#endif
1788#ifdef HAVE_NETINET_IN_H
1789#include <netinet/in.h>
1790#endif])
1791
1792AC_CHECK_HEADERS(linux/netfilter_ipv6/ip6_tables.h,
1793        linux_netfilter_ipv6_ip6_tables=1, linux_netfilter_ipv6_ip6_tables=0,
1794[#ifdef HAVE_SYS_TYPES_H
1795#include <sys/types.h>
1796#endif
1797#ifdef HAVE_SYS_SOCKET_H
1798#include <sys/socket.h>
1799#endif
1800#ifdef HAVE_LIMITS_H
1801#include <limits.h>
1802#endif
1803#ifdef HAVE_LINUX_TYPES_H
1804#include <linux/types.h>
1805#endif
1806#ifdef HAVE_NETINET_IN6_H
1807#include <netinet/in6.h>
1808#endif
1809#ifdef HAVE_NETINET_IN_H
1810#include <netinet/in.h>
1811#endif
1812#ifdef HAVE_LINUX_IF_H
1813#include <linux/if.h>
1814#endif])
1815
1816transparent_ok=0
1817if test "x$net_if_found" = "x1" && test "x$net_pfvar_found" = "x1"; then
1818  transparent_ok=1
1819fi
1820if test "x$linux_netfilter_ipv4" = "x1"; then
1821  transparent_ok=1
1822fi
1823if test "x$linux_netfilter_ipv6_ip6_tables" = "x1"; then
1824  transparent_ok=1
1825fi
1826if test "x$transparent_ok" = "x1"; then
1827  AC_DEFINE(USE_TRANSPARENT, 1, "Define to enable transparent proxy support")
1828else
1829  AC_MSG_NOTICE([Transparent proxy support enabled, but missing headers.])
1830fi
1831
1832AC_CHECK_MEMBERS([struct timeval.tv_sec], , ,
1833[#ifdef HAVE_SYS_TYPES_H
1834#include <sys/types.h>
1835#endif
1836#ifdef HAVE_SYS_TIME_H
1837#include <sys/time.h>
1838#endif])
1839
1840AC_CHECK_SIZEOF(char)
1841AC_CHECK_SIZEOF(short)
1842AC_CHECK_SIZEOF(int)
1843AC_CHECK_SIZEOF(unsigned int)
1844AC_CHECK_SIZEOF(long)
1845AC_CHECK_SIZEOF(long long)
1846AC_CHECK_SIZEOF(__int64)
1847AC_CHECK_SIZEOF(void *)
1848AC_CHECK_SIZEOF(time_t)
1849AC_CHECK_SIZEOF(size_t)
1850AC_CHECK_SIZEOF(pid_t)
1851
1852AC_CHECK_TYPES([uint, u_char, ssize_t])
1853
1854AC_PC_FROM_UCONTEXT([:])
1855
1856dnl used to include sockaddr_storage, but everybody has that.
1857AC_CHECK_TYPES([struct in6_addr, struct sockaddr_in6, sa_family_t], , ,
1858[#ifdef HAVE_SYS_TYPES_H
1859#include <sys/types.h>
1860#endif
1861#ifdef HAVE_NETINET_IN_H
1862#include <netinet/in.h>
1863#endif
1864#ifdef HAVE_NETINET_IN6_H
1865#include <netinet/in6.h>
1866#endif
1867#ifdef HAVE_SYS_SOCKET_H
1868#include <sys/socket.h>
1869#endif
1870#ifdef _WIN32
1871#define _WIN32_WINNT 0x0501
1872#define WIN32_LEAN_AND_MEAN
1873#include <winsock2.h>
1874#include <ws2tcpip.h>
1875#endif
1876])
1877AC_CHECK_MEMBERS([struct in6_addr.s6_addr32, struct in6_addr.s6_addr16, struct sockaddr_in.sin_len, struct sockaddr_in6.sin6_len], , ,
1878[#ifdef HAVE_SYS_TYPES_H
1879#include <sys/types.h>
1880#endif
1881#ifdef HAVE_NETINET_IN_H
1882#include <netinet/in.h>
1883#endif
1884#ifdef HAVE_NETINET_IN6_H
1885#include <netinet/in6.h>
1886#endif
1887#ifdef HAVE_SYS_SOCKET_H
1888#include <sys/socket.h>
1889#endif
1890#ifdef _WIN32
1891#define _WIN32_WINNT 0x0501
1892#define WIN32_LEAN_AND_MEAN
1893#include <winsock2.h>
1894#include <ws2tcpip.h>
1895#endif
1896])
1897
1898AC_CHECK_TYPES([rlim_t], , ,
1899[#ifdef HAVE_SYS_TYPES_H
1900#include <sys/types.h>
1901#endif
1902#ifdef HAVE_SYS_TIME_H
1903#include <sys/time.h>
1904#endif
1905#ifdef HAVE_SYS_RESOURCE_H
1906#include <sys/resource.h>
1907#endif
1908])
1909
1910AX_CHECK_SIGN([time_t],
1911       [ : ],
1912       [ : ], [
1913#ifdef HAVE_SYS_TYPES_H
1914#include <sys/types.h>
1915#endif
1916#ifdef HAVE_SYS_TIME_H
1917#include <sys/time.h>
1918#endif
1919#ifdef HAVE_TIME_H
1920#include <time.h>
1921#endif
1922])
1923
1924if test "$ax_cv_decl_time_t_signed" = "no"; then
1925  AC_MSG_ERROR([You have an unsigned time_t; Tor does not support that. Please tell the Tor developers about your interesting platform.])
1926fi
1927
1928AX_CHECK_SIGN([size_t],
1929       [ tor_cv_size_t_signed=yes ],
1930       [ tor_cv_size_t_signed=no ], [
1931#ifdef HAVE_SYS_TYPES_H
1932#include <sys/types.h>
1933#endif
1934])
1935
1936if test "$ax_cv_decl_size_t_signed" = "yes"; then
1937  AC_MSG_ERROR([You have a signed size_t; that's grossly nonconformant.])
1938fi
1939
1940AX_CHECK_SIGN([enum always],
1941       [ AC_DEFINE(ENUM_VALS_ARE_SIGNED, 1, [Define if enum is always signed]) ],
1942       [ : ], [
1943 enum always { AAA, BBB, CCC };
1944])
1945
1946AC_CHECK_SIZEOF(socklen_t, , [AC_INCLUDES_DEFAULT()
1947#ifdef HAVE_SYS_SOCKET_H
1948#include <sys/socket.h>
1949#endif
1950])
1951
1952# We want to make sure that we _don't_ have a cell_t defined, like IRIX does.
1953
1954AC_CHECK_SIZEOF(cell_t)
1955
1956# Let's see if stdatomic works. (There are some debian clangs that screw it
1957# up; see Tor bug #26779 and debian bug 903709.)
1958AC_CACHE_CHECK([whether C11 stdatomic.h actually works],
1959               tor_cv_stdatomic_works,
1960[AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
1961#include <stdatomic.h>
1962struct x { atomic_size_t y; };
1963void try_atomic_init(struct x *xx)
1964{
1965  atomic_init(&xx->y, 99);
1966  atomic_fetch_add(&xx->y, 1);
1967}
1968]])], [tor_cv_stdatomic_works=yes], [tor_cv_stdatomic_works=no])])
1969
1970if test "$tor_cv_stdatomic_works" = "yes"; then
1971   AC_DEFINE(STDATOMIC_WORKS, 1, [Set to 1 if we can compile a simple stdatomic example.])
1972elif test "$ac_cv_header_stdatomic_h" = "yes"; then
1973   tor_incr_n_warnings
1974   AC_MSG_WARN([Your compiler provides the stdatomic.h header, but it doesn't seem to work.  I'll pretend it isn't there. If you are using Clang on Debian, maybe this is because of https://bugs.debian.org/903709 ])
1975fi
1976
1977# Now make sure that NULL can be represented as zero bytes.
1978AC_CACHE_CHECK([whether memset(0) sets pointers to NULL], tor_cv_null_is_zero,
1979[AC_RUN_IFELSE([AC_LANG_SOURCE(
1980[[#include <stdlib.h>
1981#include <string.h>
1982#include <stdio.h>
1983#ifdef HAVE_STDDEF_H
1984#include <stddef.h>
1985#endif
1986int main () { char *p1,*p2; p1=NULL; memset(&p2,0,sizeof(p2));
1987return memcmp(&p1,&p2,sizeof(char*))?1:0; }]])],
1988       [tor_cv_null_is_zero=yes],
1989       [tor_cv_null_is_zero=no],
1990       [tor_cv_null_is_zero=cross])])
1991
1992if test "$tor_cv_null_is_zero" = "cross"; then
1993  # Cross-compiling; let's hope that the target isn't raving mad.
1994  AC_MSG_NOTICE([Cross-compiling: we'll assume that NULL is represented as a sequence of 0-valued bytes.])
1995fi
1996
1997if test "$tor_cv_null_is_zero" != "no"; then
1998  AC_DEFINE([NULL_REP_IS_ZERO_BYTES], 1,
1999            [Define to 1 iff memset(0) sets pointers to NULL])
2000fi
2001
2002AC_CACHE_CHECK([whether memset(0) sets doubles to 0.0], tor_cv_dbl0_is_zero,
2003[AC_RUN_IFELSE([AC_LANG_SOURCE(
2004[[#include <stdlib.h>
2005#include <string.h>
2006#include <stdio.h>
2007#ifdef HAVE_STDDEF_H
2008#include <stddef.h>
2009#endif
2010int main () { double d1,d2; d1=0; memset(&d2,0,sizeof(d2));
2011return memcmp(&d1,&d2,sizeof(d1))?1:0; }]])],
2012       [tor_cv_dbl0_is_zero=yes],
2013       [tor_cv_dbl0_is_zero=no],
2014       [tor_cv_dbl0_is_zero=cross])])
2015
2016if test "$tor_cv_dbl0_is_zero" = "cross"; then
2017  # Cross-compiling; let's hope that the target isn't raving mad.
2018  AC_MSG_NOTICE([Cross-compiling: we'll assume that 0.0 can be represented as a sequence of 0-valued bytes.])
2019fi
2020
2021if test "$tor_cv_dbl0_is_zero" != "no"; then
2022  AC_DEFINE([DOUBLE_0_REP_IS_ZERO_BYTES], 1,
2023            [Define to 1 iff memset(0) sets doubles to 0.0])
2024fi
2025
2026# And what happens when we malloc zero?
2027AC_CACHE_CHECK([whether we can malloc(0) safely.], tor_cv_malloc_zero_works,
2028[AC_RUN_IFELSE([AC_LANG_SOURCE(
2029[[#include <stdlib.h>
2030#include <string.h>
2031#include <stdio.h>
2032#ifdef HAVE_STDDEF_H
2033#include <stddef.h>
2034#endif
2035int main () { return malloc(0)?0:1; }]])],
2036       [tor_cv_malloc_zero_works=yes],
2037       [tor_cv_malloc_zero_works=no],
2038       [tor_cv_malloc_zero_works=cross])])
2039
2040if test "$tor_cv_malloc_zero_works" = "cross"; then
2041  # Cross-compiling; let's hope that the target isn't raving mad.
2042  AC_MSG_NOTICE([Cross-compiling: we'll assume that we need to check malloc() arguments for 0.])
2043fi
2044
2045if test "$tor_cv_malloc_zero_works" = "yes"; then
2046  AC_DEFINE([MALLOC_ZERO_WORKS], 1,
2047            [Define to 1 iff malloc(0) returns a pointer])
2048fi
2049
2050# whether we seem to be in a 2s-complement world.
2051AC_CACHE_CHECK([whether we are using 2s-complement arithmetic], tor_cv_twos_complement,
2052[AC_RUN_IFELSE([AC_LANG_SOURCE(
2053[[int main () { int problem = ((-99) != (~99)+1);
2054return problem ? 1 : 0; }]])],
2055       [tor_cv_twos_complement=yes],
2056       [tor_cv_twos_complement=no],
2057       [tor_cv_twos_complement=cross])])
2058
2059if test "$tor_cv_twos_complement" = "cross"; then
2060  # Cross-compiling; let's hope that the target isn't raving mad.
2061  AC_MSG_NOTICE([Cross-compiling: we'll assume that negative integers are represented with two's complement.])
2062fi
2063
2064if test "$tor_cv_twos_complement" != "no"; then
2065  AC_DEFINE([USING_TWOS_COMPLEMENT], 1,
2066            [Define to 1 iff we represent negative integers with
2067             two's complement])
2068fi
2069
2070# What does shifting a negative value do?
2071AC_CACHE_CHECK([whether right-shift on negative values does sign-extension], tor_cv_sign_extend,
2072[AC_RUN_IFELSE([AC_LANG_SOURCE(
2073[[int main () { int okay = (-60 >> 8) == -1; return okay ? 0 : 1; }]])],
2074       [tor_cv_sign_extend=yes],
2075       [tor_cv_sign_extend=no],
2076       [tor_cv_sign_extend=cross])])
2077
2078if test "$tor_cv_sign_extend" = "cross"; then
2079  # Cross-compiling; let's hope that the target isn't raving mad.
2080  AC_MSG_NOTICE([Cross-compiling: we'll assume that right-shifting negative integers causes sign-extension])
2081fi
2082
2083if test "$tor_cv_sign_extend" != "no"; then
2084  AC_DEFINE([RSHIFT_DOES_SIGN_EXTEND], 1,
2085            [Define to 1 iff right-shifting a negative value performs sign-extension])
2086fi
2087
2088# Is uint8_t the same type as unsigned char?
2089AC_CACHE_CHECK([whether uint8_t is the same type as unsigned char], tor_cv_uint8_uchar,
2090[AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
2091#include <stdint.h>
2092extern uint8_t c;
2093unsigned char c;]])],
2094       [tor_cv_uint8_uchar=yes],
2095       [tor_cv_uint8_uchar=no],
2096       [tor_cv_uint8_uchar=cross])])
2097
2098if test "$tor_cv_uint8_uchar" = "cross"; then
2099  AC_MSG_NOTICE([Cross-compiling: we'll assume that uint8_t is the same type as unsigned char])
2100fi
2101
2102if test "$tor_cv_uint8_uchar" = "no"; then
2103  AC_MSG_ERROR([We assume that uint8_t is the same type as unsigned char, but your compiler disagrees.])
2104fi
2105
2106AC_ARG_WITH(tcmalloc,
2107AS_HELP_STRING(--with-tcmalloc, [use tcmalloc memory allocation library. Deprecated; see --with-malloc]))
2108
2109default_malloc=system
2110
2111if test "x$enable_openbsd_malloc" = "xyes" ; then
2112  AC_MSG_NOTICE([The --enable-openbsd-malloc argument is deprecated; use --with-malloc=openbsd instead.])
2113  default_malloc=openbsd
2114fi
2115
2116if test "x$with_tcmalloc" = "xyes"; then
2117  AC_MSG_NOTICE([The --with-tcmalloc argument is deprecated; use --with-malloc=tcmalloc instead.])
2118  default_malloc=tcmalloc
2119fi
2120
2121AC_ARG_WITH(malloc,
2122   AS_HELP_STRING([--with-malloc=[system,jemalloc,tcmalloc,openbsd]],
2123                  [select special malloc implementation [system]]),
2124   [ malloc="$with_malloc" ], [ malloc="$default_malloc" ])
2125
2126AS_CASE([$malloc],
2127  [tcmalloc], [
2128      PKG_CHECK_MODULES([TCMALLOC],
2129			[libtcmalloc],
2130			have_tcmalloc=yes,
2131			have_tcmalloc=no)
2132
2133      if test "x$have_tcmalloc" = "xno" ; then
2134          AC_MSG_ERROR([Unable to find tcmalloc requested by --with-malloc, $pkg_config_user_action, or set TCMALLOC_CFLAGS and TCMALLOC_LIBS.])
2135      fi
2136
2137      CFLAGS="$CFLAGS $TCMALLOC_CFLAGS"
2138      LIBS="$TCMALLOC_LIBS $LIBS"
2139  ],
2140
2141  [jemalloc], [
2142      PKG_CHECK_MODULES([JEMALLOC],
2143		        [jemalloc],
2144			have_jemalloc=yes,
2145			have_jemalloc=no)
2146
2147      if test "x$have_jemalloc" = "xno" ; then
2148          AC_MSG_ERROR([Unable to find jemalloc requested by --with-malloc, $pkg_config_user_action, or set JEMALLOC_CFLAGS and JEMALLOC_LIBS.])
2149      fi
2150
2151      CFLAGS="$CFLAGS $JEMALLOC_CFLAGS"
2152      LIBS="$JEMALLOC_LIBS $LIBS"
2153      using_custom_malloc=yes
2154  ],
2155
2156  [openbsd], [
2157    tor_incr_n_warnings
2158    AC_MSG_WARN([The openbsd malloc port is deprecated in Tor 0.3.5 and will be removed in a future version.])
2159    enable_openbsd_malloc=yes
2160  ],
2161
2162  [system], [
2163     # handle this later, including the jemalloc fallback
2164  ],
2165
2166  [AC_MSG_ERROR([--with-malloc=`$with_malloc' not supported, see --help])
2167])
2168
2169AM_CONDITIONAL(USE_OPENBSD_MALLOC, test "x$enable_openbsd_malloc" = "xyes")
2170
2171if test "$malloc" != "system"; then
2172  # Tell the C compiler not to use the system allocator functions.
2173  TOR_CHECK_CFLAGS([-fno-builtin-malloc -fno-builtin-realloc -fno-builtin-calloc -fno-builtin-free])
2174fi
2175if test "$using_custom_malloc" = "yes"; then
2176  # Tell the C compiler not to use the system allocator functions.
2177  TOR_CHECK_CFLAGS([-fno-builtin-malloc -fno-builtin-realloc -fno-builtin-calloc -fno-builtin-free])
2178fi
2179
2180# By default, we're going to assume we don't have mlockall()
2181# bionic and other platforms have various broken mlockall subsystems.
2182# Some systems don't have a working mlockall, some aren't linkable,
2183# and some have it but don't declare it.
2184AC_CHECK_FUNCS(mlockall)
2185AC_CHECK_DECLS([mlockall], , , [
2186#ifdef HAVE_SYS_MMAN_H
2187#include <sys/mman.h>
2188#endif])
2189
2190# Allow user to specify an alternate syslog facility
2191AC_ARG_WITH(syslog-facility,
2192AS_HELP_STRING(--with-syslog-facility=LOG, [syslog facility to use (default=LOG_DAEMON)]),
2193syslog_facility="$withval", syslog_facility="LOG_DAEMON")
2194AC_DEFINE_UNQUOTED(LOGFACILITY,$syslog_facility,[name of the syslog facility])
2195AC_SUBST(LOGFACILITY)
2196
2197# Check if we have getresuid and getresgid
2198AC_CHECK_FUNCS(getresuid getresgid)
2199
2200# Check for gethostbyname_r in all its glorious incompatible versions.
2201#   (This logic is based on that in Python's configure.in)
2202AH_TEMPLATE(HAVE_GETHOSTBYNAME_R,
2203  [Define this if you have any gethostbyname_r()])
2204
2205AC_CHECK_FUNC(gethostbyname_r, [
2206  AC_MSG_CHECKING([how many arguments gethostbyname_r() wants])
2207  OLD_CFLAGS=$CFLAGS
2208  CFLAGS="$CFLAGS $MY_CPPFLAGS $MY_THREAD_CPPFLAGS $MY_CFLAGS"
2209  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
2210#include <netdb.h>
2211  ]], [[
2212    char *cp1, *cp2;
2213    struct hostent *h1, *h2;
2214    int i1, i2;
2215    (void)gethostbyname_r(cp1,h1,cp2,i1,&h2,&i2);
2216  ]])],[
2217    AC_DEFINE(HAVE_GETHOSTBYNAME_R)
2218    AC_DEFINE(HAVE_GETHOSTBYNAME_R_6_ARG, 1,
2219     [Define this if gethostbyname_r takes 6 arguments])
2220    AC_MSG_RESULT(6)
2221  ], [
2222    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
2223#include <netdb.h>
2224    ]], [[
2225      char *cp1, *cp2;
2226      struct hostent *h1;
2227      int i1, i2;
2228      (void)gethostbyname_r(cp1,h1,cp2,i1,&i2);
2229    ]])], [
2230      AC_DEFINE(HAVE_GETHOSTBYNAME_R)
2231      AC_DEFINE(HAVE_GETHOSTBYNAME_R_5_ARG, 1,
2232        [Define this if gethostbyname_r takes 5 arguments])
2233      AC_MSG_RESULT(5)
2234   ], [
2235      AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
2236#include <netdb.h>
2237     ]], [[
2238       char *cp1;
2239       struct hostent *h1;
2240       struct hostent_data hd;
2241       (void) gethostbyname_r(cp1,h1,&hd);
2242     ]])], [
2243       AC_DEFINE(HAVE_GETHOSTBYNAME_R)
2244       AC_DEFINE(HAVE_GETHOSTBYNAME_R_3_ARG, 1,
2245         [Define this if gethostbyname_r takes 3 arguments])
2246       AC_MSG_RESULT(3)
2247     ], [
2248       AC_MSG_RESULT(0)
2249     ])
2250  ])
2251 ])
2252 CFLAGS=$OLD_CFLAGS
2253])
2254
2255AC_CACHE_CHECK([whether the C compiler supports __func__],
2256  tor_cv_have_func_macro,
2257  AC_COMPILE_IFELSE([AC_LANG_SOURCE([
2258#include <stdio.h>
2259int main(int c, char **v) { puts(__func__); }])],
2260  tor_cv_have_func_macro=yes,
2261  tor_cv_have_func_macro=no))
2262
2263AC_CACHE_CHECK([whether the C compiler supports __FUNC__],
2264  tor_cv_have_FUNC_macro,
2265  AC_COMPILE_IFELSE([AC_LANG_SOURCE([
2266#include <stdio.h>
2267int main(int c, char **v) { puts(__FUNC__); }])],
2268  tor_cv_have_FUNC_macro=yes,
2269  tor_cv_have_FUNC_macro=no))
2270
2271AC_CACHE_CHECK([whether the C compiler supports __FUNCTION__],
2272  tor_cv_have_FUNCTION_macro,
2273  AC_COMPILE_IFELSE([AC_LANG_SOURCE([
2274#include <stdio.h>
2275int main(int c, char **v) { puts(__FUNCTION__); }])],
2276  tor_cv_have_FUNCTION_macro=yes,
2277  tor_cv_have_FUNCTION_macro=no))
2278
2279AC_CACHE_CHECK([whether we have extern char **environ already declared],
2280  tor_cv_have_environ_declared,
2281  AC_COMPILE_IFELSE([AC_LANG_SOURCE([
2282#ifdef HAVE_UNISTD_H
2283#include <unistd.h>
2284#endif
2285#include <stdlib.h>
2286int main(int c, char **v) { char **t = environ; }])],
2287  tor_cv_have_environ_declared=yes,
2288  tor_cv_have_environ_declared=no))
2289
2290if test "$tor_cv_have_func_macro" = "yes"; then
2291  AC_DEFINE(HAVE_MACRO__func__, 1, [Defined if the compiler supports __func__])
2292fi
2293
2294if test "$tor_cv_have_FUNC_macro" = "yes"; then
2295  AC_DEFINE(HAVE_MACRO__FUNC__, 1, [Defined if the compiler supports __FUNC__])
2296fi
2297
2298if test "$tor_cv_have_FUNCTION_macro" = "yes"; then
2299  AC_DEFINE(HAVE_MACRO__FUNCTION__, 1,
2300           [Defined if the compiler supports __FUNCTION__])
2301fi
2302
2303if test "$tor_cv_have_environ_declared" = "yes"; then
2304  AC_DEFINE(HAVE_EXTERN_ENVIRON_DECLARED, 1,
2305           [Defined if we have extern char **environ already declared])
2306fi
2307
2308# $prefix stores the value of the --prefix command line option, or
2309# NONE if the option wasn't set.  In the case that it wasn't set, make
2310# it be the default, so that we can use it to expand directories now.
2311if test "x$prefix" = "xNONE"; then
2312  prefix=$ac_default_prefix
2313fi
2314
2315# and similarly for $exec_prefix
2316if test "x$exec_prefix" = "xNONE"; then
2317  exec_prefix=$prefix
2318fi
2319
2320if test "x$BUILDDIR" = "x"; then
2321  BUILDDIR=`pwd`
2322fi
2323AC_SUBST(BUILDDIR)
2324AH_TEMPLATE([BUILDDIR],[tor's build directory])
2325AC_DEFINE_UNQUOTED(BUILDDIR,"$BUILDDIR")
2326
2327if test "x$SRCDIR" = "x"; then
2328  SRCDIR=$(cd "$srcdir"; pwd)
2329fi
2330AH_TEMPLATE([SRCDIR],[tor's sourcedir directory])
2331AC_DEFINE_UNQUOTED(SRCDIR,"$SRCDIR")
2332
2333if test "x$CONFDIR" = "x"; then
2334  CONFDIR=`eval echo $sysconfdir/tor`
2335fi
2336AC_SUBST(CONFDIR)
2337AH_TEMPLATE([CONFDIR],[tor's configuration directory])
2338AC_DEFINE_UNQUOTED(CONFDIR,"$CONFDIR")
2339
2340BINDIR=`eval echo $bindir`
2341AC_SUBST(BINDIR)
2342LOCALSTATEDIR=`eval echo $localstatedir`
2343AC_SUBST(LOCALSTATEDIR)
2344
2345if test "$bwin32" = "true"; then
2346  # Test if the linker supports the --nxcompat and --dynamicbase options
2347  # for Windows
2348  save_LDFLAGS="$LDFLAGS"
2349  LDFLAGS="-Wl,--nxcompat -Wl,--dynamicbase"
2350  AC_MSG_CHECKING([whether the linker supports DllCharacteristics])
2351  AC_LINK_IFELSE([AC_LANG_PROGRAM([])],
2352    [AC_MSG_RESULT([yes])]
2353    [save_LDFLAGS="$save_LDFLAGS $LDFLAGS"],
2354    [AC_MSG_RESULT([no])]
2355  )
2356  LDFLAGS="$save_LDFLAGS"
2357fi
2358
2359# Set CFLAGS _after_ all the above checks, since our warnings are stricter
2360# than autoconf's macros like.
2361if test "$GCC" = "yes"; then
2362  # Disable GCC's strict aliasing checks.  They are an hours-to-debug
2363  # accident waiting to happen.
2364  CFLAGS="$CFLAGS -Wall -fno-strict-aliasing"
2365else
2366  # Override optimization level for non-gcc compilers
2367  CFLAGS="$CFLAGS -O"
2368  enable_gcc_warnings=no
2369  enable_gcc_warnings_advisory=no
2370fi
2371
2372# Warnings implies advisory-warnings and -Werror.
2373if test "$enable_gcc_warnings" = "yes"; then
2374  enable_gcc_warnings_advisory=yes
2375  enable_fatal_warnings=yes
2376fi
2377
2378# OS X Lion started deprecating the system openssl. Let's just disable
2379# all deprecation warnings on OS X. Also, to potentially make the binary
2380# a little smaller, let's enable dead_strip.
2381case "$host_os" in
2382
2383 darwin*)
2384    CFLAGS="$CFLAGS -Wno-deprecated-declarations"
2385    LDFLAGS="$LDFLAGS -dead_strip" ;;
2386esac
2387
2388TOR_WARNING_FLAGS=""
2389
2390# Add some more warnings which we use in development but not in the
2391# released versions.  (Some relevant gcc versions can't handle these.)
2392#
2393# Note that we have to do this near the end  of the autoconf process, or
2394# else we may run into problems when these warnings hit on the testing C
2395# programs that autoconf wants to build.
2396if test "x$enable_gcc_warnings_advisory" != "xno"; then
2397
2398  case "$host" in
2399    *-*-openbsd* | *-*-bitrig*)
2400      # Some OpenBSD versions (like 4.8) have -Wsystem-headers by default.
2401      # That's fine, except that the headers don't pass -Wredundant-decls.
2402      # Therefore, let's disable -Wsystem-headers when we're building
2403      # with maximal warnings on OpenBSD.
2404      CFLAGS="$CFLAGS -Wno-system-headers" ;;
2405  esac
2406
2407  CFLAGS_NOWARNINGS="$CFLAGS"
2408
2409  # GCC4.3 users once report trouble with -Wstrict-overflow=5.  GCC5 users
2410  # have it work better.
2411  # CFLAGS="$CFLAGS -Wstrict-overflow=1"
2412
2413  # This warning was added in gcc 4.3, but it appears to generate
2414  # spurious warnings in gcc 4.4.  I don't know if it works in 4.5.
2415  #CFLAGS="$CFLAGS -Wlogical-op"
2416
2417  m4_foreach_w([warning_flag], [
2418     -Waddress
2419     -Waddress-of-array-temporary
2420     -Waddress-of-temporary
2421     -Wambiguous-macro
2422     -Wanonymous-pack-parens
2423     -Warc
2424     -Warc-abi
2425     -Warc-bridge-casts-disallowed-in-nonarc
2426     -Warc-maybe-repeated-use-of-weak
2427     -Warc-performSelector-leaks
2428     -Warc-repeated-use-of-weak
2429     -Warray-bounds
2430     -Warray-bounds-pointer-arithmetic
2431     -Wasm
2432     -Wasm-operand-widths
2433     -Watomic-properties
2434     -Watomic-property-with-user-defined-accessor
2435     -Wauto-import
2436     -Wauto-storage-class
2437     -Wauto-var-id
2438     -Wavailability
2439     -Wbackslash-newline-escape
2440     -Wbad-array-new-length
2441     -Wbind-to-temporary-copy
2442     -Wbitfield-constant-conversion
2443     -Wbool-conversion
2444     -Wbool-conversions
2445     -Wbuiltin-requires-header
2446     -Wchar-align
2447     -Wcompare-distinct-pointer-types
2448     -Wcomplex-component-init
2449     -Wconditional-type-mismatch
2450     -Wconfig-macros
2451     -Wconstant-conversion
2452     -Wconstant-logical-operand
2453     -Wconstexpr-not-const
2454     -Wcustom-atomic-properties
2455     -Wdangling-field
2456     -Wdangling-initializer-list
2457     -Wdate-time
2458     -Wdelegating-ctor-cycles
2459     -Wdeprecated-implementations
2460     -Wdeprecated-register
2461     -Wdirect-ivar-access
2462     -Wdiscard-qual
2463     -Wdistributed-object-modifiers
2464     -Wdivision-by-zero
2465     -Wdollar-in-identifier-extension
2466     -Wdouble-promotion
2467     -Wduplicate-decl-specifier
2468     -Wduplicate-enum
2469     -Wduplicate-method-arg
2470     -Wduplicate-method-match
2471     -Wduplicated-cond
2472     -Wdynamic-class-memaccess
2473     -Wembedded-directive
2474     -Wempty-translation-unit
2475     -Wenum-conversion
2476     -Wexit-time-destructors
2477     -Wexplicit-ownership-type
2478     -Wextern-initializer
2479     -Wextra
2480     -Wextra-semi
2481     -Wextra-tokens
2482     -Wflexible-array-extensions
2483     -Wfloat-conversion
2484     -Wformat-non-iso
2485     -Wfour-char-constants
2486     -Wgcc-compat
2487     -Wglobal-constructors
2488     -Wgnu-array-member-paren-init
2489     -Wgnu-designator
2490     -Wgnu-static-float-init
2491     -Wheader-guard
2492     -Wheader-hygiene
2493     -Widiomatic-parentheses
2494     -Wignored-attributes
2495     -Wimplicit-atomic-properties
2496     -Wimplicit-conversion-floating-point-to-bool
2497     -Wimplicit-exception-spec-mismatch
2498     -Wimplicit-fallthrough
2499     -Wimplicit-fallthrough-per-function
2500     -Wimplicit-retain-self
2501     -Wimport-preprocessor-directive-pedantic
2502     -Wincompatible-library-redeclaration
2503     -Wincompatible-pointer-types-discards-qualifiers
2504     -Wincomplete-implementation
2505     -Wincomplete-module
2506     -Wincomplete-umbrella
2507     -Winit-self
2508     -Wint-conversions
2509     -Wint-to-void-pointer-cast
2510     -Winteger-overflow
2511     -Winvalid-constexpr
2512     -Winvalid-iboutlet
2513     -Winvalid-noreturn
2514     -Winvalid-pp-token
2515     -Winvalid-source-encoding
2516     -Winvalid-token-paste
2517     -Wknr-promoted-parameter
2518     -Wlarge-by-value-copy
2519     -Wliteral-conversion
2520     -Wliteral-range
2521     -Wlocal-type-template-args
2522     -Wlogical-op
2523     -Wloop-analysis
2524     -Wmain-return-type
2525     -Wmalformed-warning-check
2526     -Wmethod-signatures
2527     -Wmicrosoft
2528     -Wmicrosoft-exists
2529     -Wmismatched-parameter-types
2530     -Wmismatched-return-types
2531     -Wmissing-field-initializers
2532     -Wmissing-format-attribute
2533     -Wmissing-noreturn
2534     -Wmissing-selector-name
2535     -Wmissing-sysroot
2536     -Wmissing-variable-declarations
2537     -Wmodule-conflict
2538     -Wnested-anon-types
2539     -Wnewline-eof
2540     -Wnon-literal-null-conversion
2541     -Wnon-pod-varargs
2542     -Wnonportable-cfstrings
2543     -Wnormalized=nfkc
2544     -Wnull-arithmetic
2545     -Wnull-character
2546     -Wnull-conversion
2547     -Wnull-dereference
2548     -Wout-of-line-declaration
2549     -Wover-aligned
2550     -Woverlength-strings
2551     -Woverride-init
2552     -Woverriding-method-mismatch
2553     -Wpointer-type-mismatch
2554     -Wpredefined-identifier-outside-function
2555     -Wprotocol-property-synthesis-ambiguity
2556     -Wreadonly-iboutlet-property
2557     -Wreadonly-setter-attrs
2558     -Wreceiver-expr
2559     -Wreceiver-forward-class
2560     -Wreceiver-is-weak
2561     -Wreinterpret-base-class
2562     -Wrequires-super-attribute
2563     -Wreserved-user-defined-literal
2564     -Wreturn-stack-address
2565     -Wsection
2566     -Wselector-type-mismatch
2567     -Wsentinel
2568     -Wserialized-diagnostics
2569     -Wshadow
2570     -Wshift-count-negative
2571     -Wshift-count-overflow
2572     -Wshift-negative-value
2573     -Wshift-overflow=2
2574     -Wshift-sign-overflow
2575     -Wshorten-64-to-32
2576     -Wsizeof-array-argument
2577     -Wsource-uses-openmp
2578     -Wstatic-float-init
2579     -Wstatic-in-inline
2580     -Wstatic-local-in-inline
2581     -Wstrict-overflow=1
2582     -Wstring-compare
2583     -Wstring-conversion
2584     -Wstrlcpy-strlcat-size
2585     -Wstrncat-size
2586     -Wsuggest-attribute=format
2587     -Wsuggest-attribute=noreturn
2588     -Wsuper-class-method-mismatch
2589     -Wswitch-bool
2590     -Wsync-nand
2591     -Wtautological-constant-out-of-range-compare
2592     -Wtentative-definition-incomplete-type
2593     -Wtrampolines
2594     -Wtype-safety
2595     -Wtypedef-redefinition
2596     -Wtypename-missing
2597     -Wundefined-inline
2598     -Wundefined-internal
2599     -Wundefined-reinterpret-cast
2600     -Wunicode
2601     -Wunicode-whitespace
2602     -Wunknown-warning-option
2603     -Wunnamed-type-template-args
2604     -Wunneeded-member-function
2605     -Wunsequenced
2606     -Wunsupported-visibility
2607     -Wunused-but-set-parameter
2608     -Wunused-but-set-variable
2609     -Wunused-command-line-argument
2610     -Wunused-const-variable=2
2611     -Wunused-exception-parameter
2612     -Wunused-local-typedefs
2613     -Wunused-member-function
2614     -Wunused-sanitize-argument
2615     -Wunused-volatile-lvalue
2616     -Wuser-defined-literals
2617     -Wvariadic-macros
2618     -Wvector-conversion
2619     -Wvector-conversions
2620     -Wvexing-parse
2621     -Wvisibility
2622     -Wvla-extension
2623     -Wzero-length-array
2624  ], [ TOR_TRY_COMPILE_WITH_CFLAGS(warning_flag, [],
2625              [TOR_WARNING_FLAGS="$TOR_WARNING_FLAGS warning_flag" CFLAGS="$CFLAGS warning_flag"], true)
2626     ])
2627
2628dnl    We should re-enable this in some later version.  Clang doesn't
2629dnl    mind, but it causes trouble with GCC.
2630dnl     -Wstrict-overflow=2
2631
2632dnl    These seem to require annotations that we don't currently use,
2633dnl    and they give false positives in our pthreads wrappers. (Clang 4)
2634dnl     -Wthread-safety
2635dnl     -Wthread-safety-analysis
2636dnl     -Wthread-safety-attributes
2637dnl     -Wthread-safety-beta
2638dnl     -Wthread-safety-precise
2639
2640  W_FLAGS="$W_FLAGS -W -Wfloat-equal -Wundef -Wpointer-arith"
2641  W_FLAGS="$W_FLAGS -Wstrict-prototypes -Wmissing-prototypes -Wwrite-strings"
2642  W_FLAGS="$W_FLAGS -Wredundant-decls -Wchar-subscripts -Wcomment -Wformat=2"
2643  W_FLAGS="$W_FLAGS -Wwrite-strings"
2644  W_FLAGS="$W_FLAGS -Wnested-externs -Wbad-function-cast -Wswitch-enum"
2645  W_FLAGS="$W_FLAGS -Waggregate-return -Wpacked -Wunused"
2646  W_FLAGS="$W_FLAGS -Wunused-parameter "
2647  # These interfere with building main() { return 0; }, which autoconf
2648  # likes to use as its default program.
2649  W_FLAGS="$W_FLAGS -Wold-style-definition -Wmissing-declarations"
2650
2651  TOR_WARNING_FLAGS="$TOR_WARNING_FLAGS $W_FLAGS"
2652  CFLAGS="$CFLAGS $W_FLAGS"
2653
2654  if test "$tor_cv_cflags__Wnull_dereference" = "yes"; then
2655    AC_DEFINE([HAVE_CFLAG_WNULL_DEREFERENCE], 1, [True if we have -Wnull-dereference])
2656  fi
2657  if test "$tor_cv_cflags__Woverlength_strings" = "yes"; then
2658    AC_DEFINE([HAVE_CFLAG_WOVERLENGTH_STRINGS], 1, [True if we have -Woverlength-strings])
2659  fi
2660  if test "$tor_cv_cflags__warn_unused_const_variable_2" = "yes"; then
2661    AC_DEFINE([HAVE_CFLAG_WUNUSED_CONST_VARIABLE], 1, [True if we have -Wunused-const-variable])
2662  fi
2663
2664  CFLAGS="$CFLAGS_NOWARNINGS"
2665
2666  if test "x$enable_fatal_warnings" = "xyes"; then
2667    # I'd like to use TOR_CHECK_CFLAGS here, but I can't, since the
2668    # default autoconf programs are full of errors.
2669    CFLAGS="$CFLAGS -Werror"
2670  fi
2671
2672fi
2673
2674AC_SUBST(TOR_WARNING_FLAGS)
2675
2676echo "$TOR_WARNING_FLAGS">warning_flags
2677
2678TOR_TRY_COMPILE_WITH_CFLAGS([@warning_flags], [],
2679                            CFLAGS="$CFLAGS @warning_flags",
2680                            CFLAGS="$CFLAGS $TOR_WARNING_FLAGS")
2681
2682if test "$enable_coverage" = "yes" && test "$have_clang" = "no"; then
2683   case "$host_os" in
2684    darwin*)
2685      tor_incr_n_warnings
2686      AC_MSG_WARN([Tried to enable coverage on OSX without using the clang compiler. This might not work! If coverage fails, use CC=clang when configuring with --enable-coverage.])
2687   esac
2688fi
2689
2690CPPFLAGS="$CPPFLAGS $TOR_CPPFLAGS_libevent $TOR_CPPFLAGS_openssl $TOR_CPPFLAGS_zlib"
2691
2692AC_CONFIG_FILES([
2693	Doxyfile
2694	Makefile
2695	config.rust
2696	contrib/operator-tools/tor.logrotate
2697	src/config/torrc.sample
2698	src/config/torrc.minimal
2699	src/rust/.cargo/config
2700	scripts/maint/checkOptionDocs.pl
2701        warning_flags
2702])
2703
2704if test "x$asciidoc" = "xtrue" && test "$ASCIIDOC" = "none"; then
2705  regular_mans="doc/man/tor doc/man/tor-gencert doc/man/tor-resolve doc/man/torify"
2706  for file in $regular_mans ; do
2707    if ! [[ -f "$srcdir/$file.1.in" ]] || ! [[ -f "$srcdir/$file.html.in" ]] ; then
2708      echo "==================================";
2709      echo;
2710      echo "Building Tor has failed since manpages cannot be built.";
2711      echo;
2712      echo "You need asciidoc installed to be able to build the manpages.";
2713      echo "To build without manpages, use the --disable-asciidoc argument";
2714      echo "when calling configure.";
2715      echo;
2716      echo "==================================";
2717      exit 1;
2718    fi
2719  done
2720fi
2721
2722if test "$fragile_hardening" = "yes"; then
2723  tor_incr_n_warnings
2724  AC_MSG_WARN([
2725
2726============
2727Warning!  Building Tor with --enable-fragile-hardening (also known as
2728--enable-expensive-hardening) makes some kinds of attacks harder, but makes
2729other kinds of attacks easier. A Tor instance build with this option will be
2730somewhat less vulnerable to remote code execution, arithmetic overflow, or
2731out-of-bounds read/writes... but at the cost of becoming more vulnerable to
2732denial of service attacks. For more information, see
2733https://gitlab.torproject.org/tpo/core/team/-/wikis/TorFragileHardening
2734============
2735  ])
2736fi
2737
2738AC_OUTPUT
2739
2740if test "$openssl_ver_mismatch" = "yes"; then
2741   tor_incr_n_warnings
2742   AC_MSG_WARN([
2743============
2744Warning! The version OpenSSL headers we get from compiling with
2745    "${TOR_CPPFLAGS_OPENSSL:-(no extra options)}"
2746do not match version of the OpenSSL library we get when linking with
2747    "$TOR_LDFLAGS_OPENSSL $TOR_OPENSSL_LIBS".
2748This might cause compilation to fail. Try using --with-openssl-dir to specify
2749the exact OpenSSL path you want.
2750============
2751])
2752fi
2753
2754#
2755# Mini-report on what will be built.
2756#
2757
2758PPRINT_INIT
2759PPRINT_SET_INDENT(1)
2760PPRINT_SET_TS(65)
2761
2762AS_ECHO
2763AS_ECHO("Tor Version: ${PPRINT_COLOR_BLDBLU}Tor $PACKAGE_VERSION $PPRINT_COLOR_RST")
2764AS_ECHO
2765
2766PPRINT_SUBTITLE([Build Features])
2767
2768PPRINT_PROP_STRING([Compiler], [$CC])
2769PPRINT_PROP_STRING([Host OS], [$host_os])
2770AS_ECHO
2771
2772test "x$enable_fatal_warnings" = "xyes" && value=1 || value=0
2773PPRINT_PROP_BOOL([Warnings are fatal (--enable-fatal-warnings)], $value)
2774
2775test "x$enable_rust" = "xyes" && value=1 || value=0
2776PPRINT_PROP_BOOL([Rust support (--enable-rust)], $value)
2777
2778test "x$enable_cargo_online_mode" = "xyes" && value=1 || value=0
2779PPRINT_PROP_BOOL([Cargo Online Fetch (--enable-cargo-online-mode)], $value)
2780
2781test "x$enable_android" = "xyes" && value=1 || value=0
2782PPRINT_PROP_BOOL([Android support (--enable-android)], $value)
2783
2784AS_ECHO
2785PPRINT_SUBTITLE([Static Build])
2786
2787test "x$enable_static_tor" = "xyes" && value=1 || value=0
2788PPRINT_PROP_BOOL([tor (--enable-static-tor)], $value)
2789
2790if test "x$enable_static_libevent" = "xyes"; then
2791  PPRINT_PROP_STRING([libevent], [$TOR_LIBDIR_libevent])
2792else
2793  PPRINT_PROP_BOOL([libevent (--enable-static-libevent)], $value)
2794fi
2795
2796if test "x$enable_static_openssl" = "xyes"; then
2797  PPRINT_PROP_STRING([libssl], [$TOR_LIBDIR_openssl])
2798else
2799  PPRINT_PROP_BOOL([libssl (--enable-static-openssl)], $value)
2800fi
2801
2802if test "x$enable_static_zlib" = "xyes"; then
2803  PPRINT_PROP_STRING([zlib1g], [$TOR_LIBDIR_zlib])
2804else
2805  PPRINT_PROP_BOOL([zlib1g (--enable-static-zlib)], $value)
2806fi
2807
2808AS_ECHO
2809PPRINT_SUBTITLE([Optional Libraries])
2810
2811test "x$enable_nss" = "xyes" && value=1 || value=0
2812PPRINT_PROP_BOOL([libnss (--enable-nss)], $value)
2813
2814test "x$enable_seccomp" != "xno" && value=1 || value=0
2815PPRINT_PROP_BOOL([libseccomp (--disable-seccomp)], $value)
2816
2817test "x$enable_libscrypt" != "xno" && value=1 || value=0
2818PPRINT_PROP_BOOL([libscrypt (--disable-libscrypt)], $value)
2819
2820test "x$enable_systemd" = "xyes" && value=1 || value=0
2821PPRINT_PROP_BOOL([Systemd support (--enable-systemd)], $value)
2822
2823test "x$have_lzma" = "xyes" && value=1 || value=0
2824PPRINT_PROP_BOOL([liblzma (--enable-lzma)], $value)
2825
2826test "x$have_zstd" = "xyes" && value=1 || value=0
2827PPRINT_PROP_BOOL([libzstd (--enable-zstd)], $value)
2828
2829AS_ECHO
2830PPRINT_SUBTITLE([Hardening])
2831
2832test "x$enable_gcc_hardening" != "xno" && value=1 || value=0
2833PPRINT_PROP_BOOL([Compiler Hardening (--disable-gcc-hardening)], $value)
2834
2835test "x$enable_linker_hardening" != "xno" && value=1 || value=0
2836PPRINT_PROP_BOOL([Linker Hardening (--disable-linker-hardening)], $value)
2837
2838test "x$fragile_hardening" = "xyes" && value=1 || value=0
2839PPRINT_PROP_BOOL([Fragile Hardening (--enable-fragile-hardening, dev only)], $value)
2840
2841AS_ECHO
2842PPRINT_SUBTITLE([Modules])
2843
2844m4_foreach_w([mname], MODULES,
2845  [
2846    AM_COND_IF(m4_join([], [BUILD_MODULE_], m4_toupper([]mname[])), value=1, value=0)
2847    m4_set_contains([MODULES_WITH_NO_OPTIONS], mname,
2848                    PPRINT_PROP_BOOL([mname], $value),
2849                    PPRINT_PROP_BOOL([mname (--disable-module-mname)], $value))
2850  ]
2851)
2852
2853AS_ECHO
2854PPRINT_SUBTITLE([Documentation])
2855
2856test "x$enable_asciidoc" != "xno" && value=1 || value=0
2857PPRINT_PROP_BOOL([AsciiDoc (--disable-asciidoc)], $value)
2858
2859test "x$enable_manpage" != "xno" && value=1 || value=0
2860PPRINT_PROP_BOOL([Man Pages (--disable-manpage)], $value)
2861
2862test "x$enable_html_manual" != "xno" && value=1 || value=0
2863PPRINT_PROP_BOOL([HTML Manual (--disable-html-manual)], $value)
2864
2865AS_ECHO
2866PPRINT_SUBTITLE([Tests])
2867
2868test "x$enable_unittests" != "xno" && value=1 || value=0
2869PPRINT_PROP_BOOL([Unit tests (--disable-unittests)], $value)
2870
2871test "x$enable_asserts_in_tests" = "xno" && value=1 || value=0
2872PPRINT_PROP_BOOL([assert()s enabled (--enable-asserts-in-tests, dev only)], $value)
2873
2874test "x$enable_coverage" = "xyes" && value=1 || value=0
2875PPRINT_PROP_BOOL([Code Coverage (--enable-coverage)], $value)
2876
2877test "x$enable_libfuzzer" = "xyes" && value=1 || value=0
2878PPRINT_PROP_BOOL([libFuzzer support (--enable-libfuzzer)], $value)
2879
2880test "x$enable_oss_fuzz" = "xyes" && value=1 || value=0
2881PPRINT_PROP_BOOL([OSS-Fuzz support (--enable-oss-fuzz)], $value)
2882
2883AS_ECHO
2884PPRINT_SUBTITLE([Tracing (--enable-tracing-instrumentation-<type>)])
2885
2886test "x$enable_tracing_instrumentation_log_debug" = "xyes" && value=1 || value=0
2887PPRINT_PROP_BOOL([Tracepoints to log_debug() (log-debug)], $value)
2888
2889test "x$enable_tracing_instrumentation_usdt" = "xyes" && value=1 || value=0
2890PPRINT_PROP_BOOL([USDT Instrumentation (usdt)], $value)
2891
2892test "x$enable_tracing_instrumentation_lttng" = "xyes" && value=1 || value=0
2893PPRINT_PROP_BOOL([LTTng Instrumentation (lttng)], $value)
2894
2895AS_ECHO
2896PPRINT_SUBTITLE([Install Directories])
2897
2898report_mandir="`eval eval echo $mandir`"
2899PPRINT_PROP_STRING([Binaries], [$BINDIR])
2900PPRINT_PROP_STRING([Configuration], [$CONFDIR])
2901PPRINT_PROP_STRING([Man Pages], [$report_mandir])
2902
2903AS_ECHO
2904AS_ECHO(["Configure Line: ./configure $configure_flags"])
2905
2906if test "$tor_ac_n_warnings" != "0"; then
2907  AS_ECHO
2908  PPRINT_WARN([
2909Encountered $tor_ac_n_warnings warning(s). See messages above for more info.
2910  ])
2911fi
2912