1AC_PREREQ([2.65])
2AC_INIT([libsodium],[1.0.18],
3  [https://github.com/jedisct1/libsodium/issues],
4  [libsodium],
5  [https://github.com/jedisct1/libsodium])
6AC_CONFIG_AUX_DIR([build-aux])
7AC_CONFIG_MACRO_DIR([m4])
8AC_CONFIG_SRCDIR([src/libsodium/sodium/version.c])
9AC_CANONICAL_HOST
10AM_INIT_AUTOMAKE([1.11 dist-bzip2 tar-ustar foreign subdir-objects])
11m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
12AM_MAINTAINER_MODE
13AM_DEP_TRACK
14
15AC_SUBST(VERSION)
16
17SODIUM_LIBRARY_VERSION_MAJOR=10
18SODIUM_LIBRARY_VERSION_MINOR=3
19DLL_VERSION=24
20SODIUM_LIBRARY_VERSION=26:0:3
21#                       | | |
22#                +------+ | +---+
23#                |        |     |
24#             current:revision:age
25#                |        |     |
26#                |        |     +- increment if interfaces have been added
27#                |        |        set to zero if interfaces have been removed
28#                |        |        or changed
29#                |        +- increment if source code has changed
30#                |           set to zero if current is incremented
31#                +- increment if interfaces have been added, removed or changed
32AC_SUBST(SODIUM_LIBRARY_VERSION_MAJOR)
33AC_SUBST(SODIUM_LIBRARY_VERSION_MINOR)
34AC_SUBST(SODIUM_LIBRARY_VERSION)
35AC_SUBST(DLL_VERSION)
36
37AC_LANG_ASSERT(C)
38LX_CFLAGS=${CFLAGS-NONE}
39PKGCONFIG_LIBS_PRIVATE=""
40
41dnl Path check
42
43AS_IF([pwd | fgrep ' ' > /dev/null 2>&1],
44  [AC_MSG_ERROR([The build directory contains whitespaces - This can cause tests/installation to fail due to limitations of some libtool versions])]
45)
46
47AC_PROG_CC_C99
48AM_PROG_AS
49AC_USE_SYSTEM_EXTENSIONS
50
51dnl Switches
52
53AC_ARG_ENABLE(ssp,
54[AS_HELP_STRING(--disable-ssp,Do not compile with -fstack-protector)],
55[
56  AS_IF([test "x$enableval" = "xno"], [
57    enable_ssp="no"
58  ], [
59    enable_ssp="yes"
60  ])
61],
62[
63  enable_ssp="yes"
64])
65
66AC_ARG_ENABLE(asm,
67[AS_HELP_STRING(--disable-asm,[Do not compile assembly code -- As a side effect, this disables CPU-specific implementations on non-Windows platforms. Only for use with targets such as WebAssembly.])],
68[
69  AS_IF([test "x$enableval" = "xno"], [
70    enable_asm="no"
71  ], [
72    enable_asm="yes"
73  ])
74],
75[
76  enable_asm="yes"
77])
78
79AS_IF([test "x$EMSCRIPTEN" != "x"], [
80  AX_CHECK_COMPILE_FLAG([-s ASSERTIONS=0], [
81    enable_asm="no"
82    AC_MSG_WARN([compiling to JavaScript - asm implementations disabled])
83  ], [
84    AC_MSG_WARN([EMSCRIPTEN environment variable defined, but emcc doesn't appear to be used - Assuming compilation to native code])
85    CFLAGS="$CFLAGS -U__EMSCRIPTEN__"
86    unset EMSCRIPTEN
87  ])
88])
89
90AC_ARG_ENABLE(pie,
91[AS_HELP_STRING(--disable-pie,Do not produce position independent executables)],
92 enable_pie=$enableval, enable_pie="maybe")
93
94AS_CASE([$host_os], [mingw*|cygwin*|msys|eabi*], [enable_pie="no"])
95
96AC_ARG_ENABLE(blocking-random,
97[AS_HELP_STRING(--enable-blocking-random,Enable this switch only if /dev/urandom is totally broken on the target platform)],
98[
99  AS_IF([test "x$enableval" = "xyes"], [
100    AC_DEFINE([USE_BLOCKING_RANDOM], [1], [/dev/urandom is insecure on the target platform])
101  ])
102])
103
104AC_ARG_ENABLE(minimal,
105[AS_HELP_STRING(--enable-minimal,
106  [Only compile the minimum set of functions required for the high-level API])],
107[
108  AS_IF([test "x$enableval" = "xyes"], [
109    enable_minimal="yes"
110    SODIUM_LIBRARY_MINIMAL_DEF="#define SODIUM_LIBRARY_MINIMAL 1"
111    AC_DEFINE([MINIMAL], [1], [Define for a minimal build, without deprecated functions and functions that high-level APIs depend on])
112  ], [
113    enable_minimal="no"
114  ])
115],
116[
117  enable_minimal="no"
118])
119AM_CONDITIONAL([MINIMAL], [test x$enable_minimal = xyes])
120AC_SUBST(SODIUM_LIBRARY_MINIMAL_DEF)
121
122AC_ARG_WITH(pthreads, AC_HELP_STRING([--with-pthreads],
123 [use pthreads library, or --without-pthreads to disable threading support.]),
124 [ ], [withval="yes"])
125
126AS_IF([test "x$withval" = "xyes"], [
127  AX_PTHREAD([
128    AC_LINK_IFELSE([AC_LANG_PROGRAM([[
129        #include <pthread.h>
130      ]], [[
131        pthread_mutex_t mutex;
132
133        pthread_mutex_lock(&mutex);
134        pthread_mutex_unlock(&mutex)
135      ]]
136    )], [
137        AC_DEFINE([HAVE_PTHREAD], [1], [Define if you have POSIX threads libraries and header files])
138        with_threads="yes"
139        LIBS="$PTHREAD_LIBS $LIBS"
140        PKGCONFIG_LIBS_PRIVATE="$PTHREAD_LIBS $PTHREAD_CFLAGS $PKGCONFIG_LIBS_PRIVATE"
141        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
142        CC="$PTHREAD_CC"
143      ])
144    ],
145    [ AC_MSG_NOTICE(pthread mutexes are not available) ]
146  )
147], [with_threads="no"])
148
149AC_ARG_WITH(safecode,
150[AS_HELP_STRING(--with-safecode,For maintainers only - please do not use)],
151[AS_IF([test "x$withval" = "xyes"], [
152    AC_ARG_VAR([SAFECODE_HOME], [set to the safecode base directory])
153    : ${SAFECODE_HOME:=/opt/safecode}
154    LDFLAGS="$LDFLAGS -L${SAFECODE_HOME}/lib"
155    LIBS="$LIBS -lsc_dbg_rt -lpoolalloc_bitmap -lstdc++"
156    CFLAGS="$CFLAGS -fmemsafety"
157  ])
158])
159
160AC_ARG_WITH(ctgrind,
161[AS_HELP_STRING(--with-ctgrind,For maintainers only - please do not use)],
162[AS_IF([test "x$withval" = "xyes"], [
163    AC_CHECK_LIB(ctgrind, ct_poison)
164  ])
165])
166
167AC_ARG_ENABLE(retpoline,
168[AS_HELP_STRING(--enable-retpoline,Use return trampolines for indirect calls)],
169[AS_IF([test "x$enableval" = "xyes"], [
170  AX_CHECK_COMPILE_FLAG([-mindirect-branch=thunk-inline],
171    [CFLAGS="$CFLAGS -mindirect-branch=thunk-inline"],
172    [AX_CHECK_COMPILE_FLAG([-mretpoline], [CFLAGS="$CFLAGS -mretpoline"])]
173  )
174  AX_CHECK_COMPILE_FLAG([-mindirect-branch-register])
175  ])
176])
177
178ENABLE_CWFLAGS=no
179AC_ARG_ENABLE(debug,
180[AS_HELP_STRING(--enable-debug,For maintainers only - please do not use)],
181[
182  AS_IF([test "x$enableval" = "xyes"], [
183    AS_IF([test "x$LX_CFLAGS" = "xNONE"], [
184      nxflags=""
185      for flag in `echo $CFLAGS`; do
186        AS_CASE([$flag],
187          [-O*], [ ],
188          [-g*], [ ],
189          [*], [AS_VAR_APPEND([nxflags], [" $flag"])])
190      done
191      CFLAGS="$nxflags -O -g3"
192    ])
193    ENABLE_CWFLAGS=yes
194    CPPFLAGS="$CPPFLAGS -DDEBUG=1 -U_FORTIFY_SOURCE"
195  ])
196])
197
198AC_ARG_ENABLE(opt,
199[AS_HELP_STRING(--enable-opt,Optimize for the native CPU - The resulting library will be faster but not portable)],
200[
201  AS_IF([test "x$enableval" = "xyes"], [
202    AX_CHECK_COMPILE_FLAG([-Ofast], [CFLAGS="$CFLAGS -Ofast"])
203    AX_CHECK_COMPILE_FLAG([-ftree-vectorize], [CFLAGS="$CFLAGS -ftree-vectorize"])
204    AX_CHECK_COMPILE_FLAG([-ftree-slp-vectorize], [CFLAGS="$CFLAGS -ftree-slp-vectorize"])
205    AX_CHECK_COMPILE_FLAG([-fomit-frame-pointer], [CFLAGS="$CFLAGS -fomit-frame-pointer"])
206    AX_CHECK_COMPILE_FLAG([-march=native], [CFLAGS="$CFLAGS -march=native"])
207  ])
208])
209
210AC_SUBST(MAINT)
211AC_SUBST(PKGCONFIG_LIBS_PRIVATE)
212
213AX_VALGRIND_CHECK
214
215dnl Checks
216
217AC_C_VARARRAYS
218
219AC_CHECK_DEFINE([__wasi__], [WASI="yes"], [])
220
221AS_CASE([$host_os], [linux-gnu], [AX_ADD_FORTIFY_SOURCE], [ ])
222
223AX_CHECK_COMPILE_FLAG([-fvisibility=hidden],
224  [CFLAGS="$CFLAGS -fvisibility=hidden"])
225
226AS_CASE([$host_os], [cygwin*|mingw*|msys|pw32*|cegcc*|eabi*], [ ], [
227  AX_CHECK_COMPILE_FLAG([-fPIC], [CFLAGS="$CFLAGS -fPIC"])
228])
229
230AS_IF([test "$enable_pie" != "no"],[
231  AX_CHECK_COMPILE_FLAG([-fPIE], [
232    AX_CHECK_LINK_FLAG([-pie], [
233      [CFLAGS="$CFLAGS -fPIE"
234       LDFLAGS="$LDFLAGS -pie"]
235    ])
236  ])
237])
238
239AX_CHECK_COMPILE_FLAG([-fno-strict-aliasing], [CFLAGS="$CFLAGS -fno-strict-aliasing"])
240AX_CHECK_COMPILE_FLAG([-fno-strict-overflow], [CFLAGS="$CFLAGS -fno-strict-overflow"], [
241  AX_CHECK_COMPILE_FLAG([-fwrapv], [CFLAGS="$CFLAGS -fwrapv"])
242])
243
244AS_IF([test "$GCC" = "yes" ], [
245  AS_CASE([$host_cpu],
246    [i?86|amd64|x86_64], [
247      AC_COMPILE_IFELSE(
248        [AC_LANG_SOURCE([
249#if !defined(__clang__) && defined(__GNUC__) && ((__GNUC__ << 8) | __GNUC_MINOR__) < 0x403
250# error old gcc
251#endif
252int main(void) { return 0; }
253         ])],,[
254          AX_CHECK_COMPILE_FLAG([-flax-vector-conversions], [CFLAGS="$CFLAGS -flax-vector-conversions"])
255        ])
256      ]
257    )
258  ])
259
260LIBTOOL_OLD_FLAGS="$LIBTOOL_EXTRA_FLAGS"
261LIBTOOL_EXTRA_FLAGS="$LIBTOOL_EXTRA_FLAGS -version-info $SODIUM_LIBRARY_VERSION"
262AC_ARG_ENABLE(soname-versions,
263  [AC_HELP_STRING([--enable-soname-versions], [enable soname versions (must be disabled for Android) (default: enabled)])],
264    [
265        AS_IF([test "x$enableval" = "xno"], [
266          LIBTOOL_EXTRA_FLAGS="$LIBTOOL_OLD_FLAGS -avoid-version"
267        ])
268    ]
269)
270
271AS_CASE([$host_os],
272  [cygwin*|mingw*|msys|pw32*|cegcc*], [
273    AX_CHECK_LINK_FLAG([-Wl,--dynamicbase], [LDFLAGS="$LDFLAGS -Wl,--dynamicbase"])
274    AX_CHECK_LINK_FLAG([-Wl,--high-entropy-va], [LDFLAGS="$LDFLAGS -Wl,--high-entropy-va"])
275    AX_CHECK_LINK_FLAG([-Wl,--nxcompat], [LDFLAGS="$LDFLAGS -Wl,--nxcompat"])
276  ])
277
278AS_CASE([$host_os],
279  [cygwin*|mingw*|msys|pw32*|cegcc*|eabi*], [
280    AX_CHECK_COMPILE_FLAG([-fno-asynchronous-unwind-tables], [
281      [CFLAGS="$CFLAGS -fno-asynchronous-unwind-tables"]
282    ])
283])
284
285AC_MSG_CHECKING(for a broken Xcode version)
286AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[
287#if !defined(__APPLE_CC__) || __APPLE_CC__ != 6000
288#error Not Apple
289#endif
290#if !defined(__clang_major__) || __clang_major__ != 11
291#error Not Xcode 11
292#endif
293]])],
294  [AC_MSG_RESULT(yes)
295   AC_MSG_WARN([Using unsupported Xcode version])
296   AX_CHECK_COMPILE_FLAG([$CFLAGS -fno-stack-check],
297     [CFLAGS="$CFLAGS -fno-stack-check"])
298  ],
299  [AC_MSG_RESULT(no)
300])
301
302AS_IF([test "x$enable_ssp" != "xno"],[
303
304AS_CASE([$host_os],
305  [cygwin*|mingw*|msys|pw32*|cegcc*|haiku|none|eabi*], [ ],
306  [*], [
307    AX_CHECK_COMPILE_FLAG([-fstack-protector], [
308      AX_CHECK_LINK_FLAG([-fstack-protector],
309        [CFLAGS="$CFLAGS -fstack-protector"]
310      )
311    ])
312  ])
313])
314
315AC_ARG_VAR([CWFLAGS], [define to compilation flags for generating extra warnings])
316
317AX_CHECK_COMPILE_FLAG([$CFLAGS -Wall], [CWFLAGS="$CFLAGS -Wall"])
318AX_CHECK_COMPILE_FLAG([$CFLAGS -Wextra], [CWFLAGS="$CFLAGS -Wextra"])
319
320AC_MSG_CHECKING(for clang)
321AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[
322#ifndef __clang__
323#error Not clang
324#endif
325]])],
326  [AC_MSG_RESULT(yes)
327   AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wno-unknown-warning-option],
328     [CWFLAGS="$CWFLAGS -Wno-unknown-warning-option"])
329  ],
330  [AC_MSG_RESULT(no)
331])
332
333AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wbad-function-cast], [CWFLAGS="$CWFLAGS -Wbad-function-cast"])
334AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wcast-qual], [CWFLAGS="$CWFLAGS -Wcast-qual"])
335AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wdiv-by-zero], [CWFLAGS="$CWFLAGS -Wdiv-by-zero"])
336AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wduplicated-branches], [CWFLAGS="$CWFLAGS -Wduplicated-branches"])
337AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wduplicated-cond], [CWFLAGS="$CWFLAGS -Wduplicated-cond"])
338AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wfloat-equal], [CWFLAGS="$CWFLAGS -Wfloat-equal"])
339AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wformat=2], [CWFLAGS="$CWFLAGS -Wformat=2"])
340AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wlogical-op], [CWFLAGS="$CWFLAGS -Wlogical-op"])
341AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wmaybe-uninitialized], [CWFLAGS="$CWFLAGS -Wmaybe-uninitialized"])
342AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wmisleading-indentation], [CWFLAGS="$CWFLAGS -Wmisleading-indentation"])
343AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wmissing-declarations], [CWFLAGS="$CWFLAGS -Wmissing-declarations"])
344AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wmissing-prototypes], [CWFLAGS="$CWFLAGS -Wmissing-prototypes"])
345AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wnested-externs], [CWFLAGS="$CWFLAGS -Wnested-externs"])
346AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wno-type-limits], [CWFLAGS="$CWFLAGS -Wno-type-limits"])
347AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wno-unknown-pragmas], [CWFLAGS="$CWFLAGS -Wno-unknown-pragmas"])
348AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wnormalized=id], [CWFLAGS="$CWFLAGS -Wnormalized=id"])
349AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wnull-dereference], [CWFLAGS="$CWFLAGS -Wnull-dereference"])
350AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wold-style-declaration], [CWFLAGS="$CWFLAGS -Wold-style-declaration"])
351AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wpointer-arith], [CWFLAGS="$CWFLAGS -Wpointer-arith"])
352AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wredundant-decls], [CWFLAGS="$CWFLAGS -Wredundant-decls"])
353AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wrestrict], [CWFLAGS="$CWFLAGS -Wrestrict"])
354AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wshorten-64-to-32], [CWFLAGS="$CWFLAGS -Wshorten-64-to-32"])
355AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wsometimes-uninitialized], [CWFLAGS="$CWFLAGS -Wsometimes-uninitialized"])
356AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wstrict-prototypes], [CWFLAGS="$CWFLAGS -Wstrict-prototypes"])
357AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wswitch-enum], [CWFLAGS="$CWFLAGS -Wswitch-enum"])
358AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wvariable-decl], [CWFLAGS="$CWFLAGS -Wvariable-decl"])
359AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wwrite-strings], [CWFLAGS="$CWFLAGS -Wwrite-strings"])
360
361AS_IF([test "x$EMSCRIPTEN" = "x"], [
362  AX_CHECK_LINK_FLAG([-Wl,-z,relro], [LDFLAGS="$LDFLAGS -Wl,-z,relro"])
363  AX_CHECK_LINK_FLAG([-Wl,-z,now], [LDFLAGS="$LDFLAGS -Wl,-z,now"])
364  AX_CHECK_LINK_FLAG([-Wl,-z,noexecstack], [LDFLAGS="$LDFLAGS -Wl,-z,noexecstack"])
365])
366
367AX_CHECK_CATCHABLE_SEGV
368AX_CHECK_CATCHABLE_ABRT
369
370AS_IF([test "x$with_threads" = "xyes"], [
371  AX_TLS([AC_MSG_RESULT(thread local storage is supported)
372          AX_CHECK_COMPILE_FLAG([-ftls-model=local-dynamic],
373            [CFLAGS="$CFLAGS -ftls-model=local-dynamic"])],
374         [AC_MSG_RESULT(thread local storage is not supported)]) ])
375
376LT_INIT
377AC_SUBST(LIBTOOL_DEPS)
378
379AC_ARG_VAR([AR], [path to the ar utility])
380AC_CHECK_TOOL([AR], [ar], [ar])
381
382dnl Checks for headers
383
384AS_IF([test "x$EMSCRIPTEN" = "x"], [
385
386  oldcflags="$CFLAGS"
387  AX_CHECK_COMPILE_FLAG([-mmmx], [CFLAGS="$CFLAGS -mmmx"])
388  AC_MSG_CHECKING(for MMX instructions set)
389  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
390#pragma GCC target("mmx")
391#include <mmintrin.h>
392]], [[ __m64 x = _mm_setzero_si64(); ]])],
393    [AC_MSG_RESULT(yes)
394     AC_DEFINE([HAVE_MMINTRIN_H], [1], [mmx is available])
395     AX_CHECK_COMPILE_FLAG([-mmmx], [CFLAGS_MMX="-mmmx"])],
396    [AC_MSG_RESULT(no)])
397  CFLAGS="$oldcflags"
398
399  oldcflags="$CFLAGS"
400  AX_CHECK_COMPILE_FLAG([-msse2], [CFLAGS="$CFLAGS -msse2"])
401  AC_MSG_CHECKING(for SSE2 instructions set)
402  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
403#pragma GCC target("sse2")
404#ifndef __SSE2__
405# define __SSE2__
406#endif
407#include <emmintrin.h>
408]], [[ __m128d x = _mm_setzero_pd();
409       __m128i z = _mm_srli_epi64(_mm_setzero_si128(), 26); ]])],
410    [AC_MSG_RESULT(yes)
411     AC_DEFINE([HAVE_EMMINTRIN_H], [1], [sse2 is available])
412     AX_CHECK_COMPILE_FLAG([-msse2], [CFLAGS_SSE2="-msse2"])],
413    [AC_MSG_RESULT(no)])
414  CFLAGS="$oldcflags"
415
416  oldcflags="$CFLAGS"
417  AX_CHECK_COMPILE_FLAG([-msse3], [CFLAGS="$CFLAGS -msse3"])
418  AC_MSG_CHECKING(for SSE3 instructions set)
419  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
420#pragma GCC target("sse3")
421#include <pmmintrin.h>
422]], [[ __m128 x = _mm_addsub_ps(_mm_cvtpd_ps(_mm_setzero_pd()),
423                                _mm_cvtpd_ps(_mm_setzero_pd())); ]])],
424    [AC_MSG_RESULT(yes)
425     AC_DEFINE([HAVE_PMMINTRIN_H], [1], [sse3 is available])
426     AX_CHECK_COMPILE_FLAG([-msse3], [CFLAGS_SSE3="-msse3"])],
427    [AC_MSG_RESULT(no)])
428  CFLAGS="$oldcflags"
429
430  oldcflags="$CFLAGS"
431  AX_CHECK_COMPILE_FLAG([-mssse3], [CFLAGS="$CFLAGS -mssse3"])
432  AC_MSG_CHECKING(for SSSE3 instructions set)
433  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
434#pragma GCC target("ssse3")
435#include <tmmintrin.h>
436]], [[ __m64 x = _mm_abs_pi32(_m_from_int(0)); ]])],
437    [AC_MSG_RESULT(yes)
438     AC_DEFINE([HAVE_TMMINTRIN_H], [1], [ssse3 is available])
439     AX_CHECK_COMPILE_FLAG([-mssse3], [CFLAGS_SSSE3="-mssse3"])],
440    [AC_MSG_RESULT(no)])
441  CFLAGS="$oldcflags"
442
443  oldcflags="$CFLAGS"
444  AX_CHECK_COMPILE_FLAG([-msse4.1], [CFLAGS="$CFLAGS -msse4.1"])
445  AC_MSG_CHECKING(for SSE4.1 instructions set)
446  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
447#pragma GCC target("sse4.1")
448#include <smmintrin.h>
449]], [[ __m128i x = _mm_minpos_epu16(_mm_setzero_si128()); ]])],
450    [AC_MSG_RESULT(yes)
451     AC_DEFINE([HAVE_SMMINTRIN_H], [1], [sse4.1 is available])
452     AX_CHECK_COMPILE_FLAG([-msse4.1], [CFLAGS_SSE41="-msse4.1"])],
453    [AC_MSG_RESULT(no)])
454  CFLAGS="$oldcflags"
455
456  oldcflags="$CFLAGS"
457  AX_CHECK_COMPILE_FLAG([-mavx], [CFLAGS="$CFLAGS -mavx"])
458  AC_MSG_CHECKING(for AVX instructions set)
459  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
460#pragma GCC target("avx")
461#include <immintrin.h>
462]], [[ _mm256_zeroall(); ]])],
463    [AC_MSG_RESULT(yes)
464     AC_DEFINE([HAVE_AVXINTRIN_H], [1], [AVX is available])
465     AX_CHECK_COMPILE_FLAG([-mavx], [CFLAGS_AVX="-mavx"])],
466    [AC_MSG_RESULT(no)])
467  CFLAGS="$oldcflags"
468
469  oldcflags="$CFLAGS"
470  AX_CHECK_COMPILE_FLAG([-mavx2], [CFLAGS="$CFLAGS -mavx2"])
471  AC_MSG_CHECKING(for AVX2 instructions set)
472  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
473#pragma GCC target("avx2")
474#include <immintrin.h>
475]], [[
476__m256 x = _mm256_set1_ps(3.14);
477__m256 y = _mm256_permutevar8x32_ps(x, _mm256_set1_epi32(42));
478return _mm256_movemask_ps(_mm256_cmp_ps(x, y, _CMP_NEQ_OQ));
479]])],
480    [AC_MSG_RESULT(yes)
481     AC_DEFINE([HAVE_AVX2INTRIN_H], [1], [AVX2 is available])
482     AX_CHECK_COMPILE_FLAG([-mavx2], [CFLAGS_AVX2="-mavx2"])
483     AC_MSG_CHECKING(if _mm256_broadcastsi128_si256 is correctly defined)
484     AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
485#pragma GCC target("avx2")
486#include <immintrin.h>
487     ]], [[ __m256i y = _mm256_broadcastsi128_si256(_mm_setzero_si128()); ]])],
488       [AC_MSG_RESULT(yes)],
489       [AC_MSG_RESULT(no)
490        AC_DEFINE([_mm256_broadcastsi128_si256], [_mm_broadcastsi128_si256],
491                  [Define to the local name of _mm256_broadcastsi128_si256])])
492     ],
493    [AC_MSG_RESULT(no)])
494  CFLAGS="$oldcflags"
495
496  oldcflags="$CFLAGS"
497  AX_CHECK_COMPILE_FLAG([-mavx512f], [CFLAGS="$CFLAGS -mavx512f"])
498  AC_MSG_CHECKING(for AVX512F instructions set)
499  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
500#pragma GCC target("avx512f")
501#include <immintrin.h>
502]], [[
503
504#ifndef __AVX512F__
505# error No AVX512 support
506#elif defined(__clang__)
507# if __clang_major__ < 4
508#  error Compiler AVX512 support may be broken
509# endif
510#elif defined(__GNUC__)
511# if __GNUC__ < 6
512#  error Compiler AVX512 support may be broken
513# endif
514#endif
515
516__m512i x = _mm512_setzero_epi32();
517__m512i y = _mm512_permutexvar_epi64(_mm512_setr_epi64(0, 1, 4, 5, 2, 3, 6, 7), x);
518]])],
519    [AC_MSG_RESULT(yes)
520     AC_DEFINE([HAVE_AVX512FINTRIN_H], [1], [AVX512F is available])
521     AX_CHECK_COMPILE_FLAG([-mavx512f], [CFLAGS_AVX512F="-mavx512f"])],
522    [AC_MSG_RESULT(no)
523     AX_CHECK_COMPILE_FLAG([$CFLAGS -mno-avx512f],
524       [CFLAGS="$CFLAGS -mno-avx512f"])
525    ])
526  CFLAGS="$oldcflags"
527
528  oldcflags="$CFLAGS"
529  AX_CHECK_COMPILE_FLAG([-maes], [CFLAGS="$CFLAGS -maes"])
530  AX_CHECK_COMPILE_FLAG([-mpclmul], [CFLAGS="$CFLAGS -mpclmul"])
531  AC_MSG_CHECKING(for AESNI instructions set and PCLMULQDQ)
532  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
533#pragma GCC target("aes")
534#pragma GCC target("pclmul")
535#include <wmmintrin.h>
536]], [[ __m128i x = _mm_aesimc_si128(_mm_setzero_si128());
537       __m128i y = _mm_clmulepi64_si128(_mm_setzero_si128(), _mm_setzero_si128(), 0);]])],
538    [AC_MSG_RESULT(yes)
539     AC_DEFINE([HAVE_WMMINTRIN_H], [1], [aesni is available])
540     AX_CHECK_COMPILE_FLAG([-maes], [CFLAGS_AESNI="-maes"])
541     AX_CHECK_COMPILE_FLAG([-mpclmul], [CFLAGS_PCLMUL="-mpclmul"])
542     ],
543    [AC_MSG_RESULT(no)])
544  CFLAGS="$oldcflags"
545
546  oldcflags="$CFLAGS"
547  AX_CHECK_COMPILE_FLAG([-mrdrnd], [CFLAGS="$CFLAGS -mrdrnd"])
548  AC_MSG_CHECKING(for RDRAND)
549  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
550#pragma GCC target("rdrnd")
551#include <immintrin.h>
552]], [[ unsigned long long x; _rdrand64_step(&x); ]])],
553    [AC_MSG_RESULT(yes)
554     AC_DEFINE([HAVE_RDRAND], [1], [rdrand is available])
555     AX_CHECK_COMPILE_FLAG([-mrdrnd], [CFLAGS_RDRAND="-mrdrnd"])
556     ],
557    [AC_MSG_RESULT(no)])
558  CFLAGS="$oldcflags"
559
560])
561
562AC_SUBST(CFLAGS_MMX)
563AC_SUBST(CFLAGS_SSE2)
564AC_SUBST(CFLAGS_SSE3)
565AC_SUBST(CFLAGS_SSSE3)
566AC_SUBST(CFLAGS_SSE41)
567AC_SUBST(CFLAGS_AVX)
568AC_SUBST(CFLAGS_AVX2)
569AC_SUBST(CFLAGS_AVX512F)
570AC_SUBST(CFLAGS_AESNI)
571AC_SUBST(CFLAGS_PCLMUL)
572AC_SUBST(CFLAGS_RDRAND)
573
574AC_CHECK_HEADERS([sys/mman.h sys/random.h intrin.h])
575
576AC_MSG_CHECKING([if _xgetbv() is available])
577AC_LINK_IFELSE(
578  [AC_LANG_PROGRAM([[ #include <intrin.h> ]], [[ (void) _xgetbv(0) ]])],
579  [AC_MSG_RESULT(yes)
580   AC_DEFINE([HAVE__XGETBV], [1], [_xgetbv() is available])],
581  [AC_MSG_RESULT(no)])
582
583dnl Checks for typedefs, structures, and compiler characteristics.
584
585AC_C_INLINE
586AS_CASE([$host_cpu],
587  [i?86|amd64|x86_64],
588    [ac_cv_c_bigendian=no]
589)
590AC_C_BIGENDIAN(
591  AC_DEFINE(NATIVE_BIG_ENDIAN, 1, [machine is bigendian]),
592  AC_DEFINE(NATIVE_LITTLE_ENDIAN, 1, [machine is littleendian]),
593  AC_MSG_ERROR([unknown endianness]),
594  AC_MSG_ERROR([universal endianness is not supported - compile separately and use lipo(1)])
595)
596
597AC_MSG_CHECKING(whether __STDC_LIMIT_MACROS is required)
598AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
599#include <limits.h>
600#include <stdint.h>
601]], [[
602(void) SIZE_MAX;
603(void) UINT64_MAX;
604]])],
605  [AC_MSG_RESULT(no)],
606  [AC_MSG_RESULT(yes)
607   CPPFLAGS="$CPPFLAGS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS"
608])
609
610AC_MSG_CHECKING(whether we can use inline asm code)
611AC_LINK_IFELSE([AC_LANG_PROGRAM([[
612]], [[
613int a = 42;
614int *pnt = &a;
615__asm__ __volatile__ ("" : : "r"(pnt) : "memory");
616]])],
617  [AC_MSG_RESULT(yes)
618   AC_DEFINE([HAVE_INLINE_ASM], [1], [inline asm code can be used])]
619  [AC_MSG_RESULT(no)]
620)
621
622HAVE_AMD64_ASM_V=0
623AS_IF([test "$enable_asm" != "no"],[
624  AC_MSG_CHECKING(whether we can use x86_64 asm code)
625  AC_LINK_IFELSE([AC_LANG_PROGRAM([[
626  ]], [[
627#if defined(__amd64) || defined(__amd64__) || defined(__x86_64__)
628# if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || defined(_WIN32) || defined(_WIN64)
629#  error Windows x86_64 calling conventions are not supported yet
630# endif
631/* neat */
632#else
633# error !x86_64
634#endif
635unsigned char i = 0, o = 0, t;
636__asm__ __volatile__ ("pxor %%xmm12, %%xmm6 \n"
637                      "movb (%[i]), %[t] \n"
638                      "addb %[t], (%[o]) \n"
639                      : [t] "=&r"(t)
640                      : [o] "D"(&o), [i] "S"(&i)
641                      : "memory", "flags", "cc");
642]])],
643  [AC_MSG_RESULT(yes)
644   AC_DEFINE([HAVE_AMD64_ASM], [1], [x86_64 asm code can be used])
645   HAVE_AMD64_ASM_V=1],
646  [AC_MSG_RESULT(no)])
647])
648AM_CONDITIONAL([HAVE_AMD64_ASM], [test $HAVE_AMD64_ASM_V = 1])
649AC_SUBST(HAVE_AMD64_ASM_V)
650
651HAVE_AVX_ASM_V=0
652AS_IF([test "$enable_asm" != "no"],[
653  AC_MSG_CHECKING(whether we can assemble AVX opcodes)
654  AC_LINK_IFELSE([AC_LANG_PROGRAM([[
655  ]], [[
656#if defined(__amd64) || defined(__amd64__) || defined(__x86_64__)
657# if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || defined(_WIN32) || defined(_WIN64)
658#  error Windows x86_64 calling conventions are not supported yet
659# endif
660/* neat */
661#else
662# error !x86_64
663#endif
664__asm__ __volatile__ ("vpunpcklqdq %xmm0,%xmm13,%xmm0");
665]])],
666  [AC_MSG_RESULT(yes)
667   AC_DEFINE([HAVE_AVX_ASM], [1], [AVX opcodes are supported])
668   HAVE_AVX_ASM_V=1],
669  [AC_MSG_RESULT(no)])
670])
671AM_CONDITIONAL([HAVE_AVX_ASM], [test $HAVE_AVX_ASM_V = 1])
672AC_SUBST(HAVE_AVX_ASM_V)
673
674AC_MSG_CHECKING(for 128-bit arithmetic)
675HAVE_TI_MODE_V=0
676AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
677#if !defined(__clang__) && !defined(__GNUC__) && !defined(__SIZEOF_INT128__)
678# error mode(TI) is a gcc extension, and __int128 is not available
679#endif
680#if defined(__clang__) && !defined(__x86_64__) && !defined(__aarch64__)
681# error clang does not properly handle the 128-bit type on 32-bit systems
682#endif
683#ifndef NATIVE_LITTLE_ENDIAN
684# error libsodium currently expects a little endian CPU for the 128-bit type
685#endif
686#ifdef __EMSCRIPTEN__
687# error emscripten currently doesn't support some operations on integers larger than 64 bits
688#endif
689#include <stddef.h>
690#include <stdint.h>
691#if defined(__SIZEOF_INT128__)
692typedef unsigned __int128 uint128_t;
693#else
694typedef unsigned uint128_t __attribute__((mode(TI)));
695#endif
696void fcontract(uint128_t *t) {
697  *t += 0x8000000000000 - 1;
698  *t *= *t;
699  *t >>= 84;
700}
701]], [[
702(void) fcontract;
703]])],
704[AC_MSG_RESULT(yes)
705 AC_DEFINE([HAVE_TI_MODE], [1], [gcc TI mode is available])
706 HAVE_TI_MODE_V=1],
707[AC_MSG_RESULT(no)])
708AM_CONDITIONAL([HAVE_TI_MODE], [test $HAVE_TI_MODE_V = 1])
709AC_SUBST(HAVE_TI_MODE_V)
710
711HAVE_CPUID_V=0
712AS_IF([test "$enable_asm" != "no"],[
713  AC_MSG_CHECKING(for cpuid instruction)
714  AC_LINK_IFELSE([AC_LANG_PROGRAM([[ ]], [[
715unsigned int cpu_info[4];
716__asm__ __volatile__ ("xchgl %%ebx, %k1; cpuid; xchgl %%ebx, %k1" :
717                      "=a" (cpu_info[0]), "=&r" (cpu_info[1]),
718                      "=c" (cpu_info[2]), "=d" (cpu_info[3]) :
719                      "0" (0U), "2" (0U));
720  ]])],
721  [AC_MSG_RESULT(yes)
722   AC_DEFINE([HAVE_CPUID], [1], [cpuid instruction is available])
723   HAVE_CPUID_V=1],
724  [AC_MSG_RESULT(no)])
725  ])
726AC_SUBST(HAVE_CPUID_V)
727
728asm_hide_symbol="unsupported"
729AS_IF([test "$enable_asm" != "no"],[
730  AC_MSG_CHECKING(if the .private_extern asm directive is supported)
731  AC_LINK_IFELSE([AC_LANG_PROGRAM([[ ]], [[
732__asm__ __volatile__ (".private_extern dummy_symbol \n"
733                      ".private_extern _dummy_symbol \n"
734                      ".globl dummy_symbol \n"
735                      ".globl _dummy_symbol \n"
736                      "dummy_symbol: \n"
737                      "_dummy_symbol: \n"
738                      "    nop \n"
739);
740  ]])],
741  [AC_MSG_RESULT(yes)
742   asm_hide_symbol=".private_extern"],
743  [AC_MSG_RESULT(no)])
744
745  AC_MSG_CHECKING(if the .hidden asm directive is supported)
746  AC_LINK_IFELSE([AC_LANG_PROGRAM([[ ]], [[
747__asm__ __volatile__ (".hidden dummy_symbol \n"
748                      ".hidden _dummy_symbol \n"
749                      ".globl dummy_symbol \n"
750                      ".globl _dummy_symbol \n"
751                      "dummy_symbol: \n"
752                      "_dummy_symbol: \n"
753                      "    nop \n"
754);
755  ]])],
756  [AC_MSG_RESULT(yes)
757   AS_IF([test "$asm_hide_symbol" = "unsupported"],
758          [asm_hide_symbol=".hidden"],
759          [AC_MSG_NOTICE([unable to reliably tag symbols as private])
760           asm_hide_symbol="unsupported"])
761  ],
762  [AC_MSG_RESULT(no)])
763
764  AS_IF([test "$asm_hide_symbol" != "unsupported"],[
765    AC_DEFINE_UNQUOTED([ASM_HIDE_SYMBOL], [$asm_hide_symbol], [directive to hide symbols])
766  ])
767])
768
769AC_MSG_CHECKING(if weak symbols are supported)
770AC_LINK_IFELSE([AC_LANG_PROGRAM([[
771#if !defined(__ELF__) && !defined(__APPLE_CC__)
772# error Support for weak symbols may not be available
773#endif
774__attribute__((weak)) void __dummy(void *x) { }
775void f(void *x) { __dummy(x); }
776]], [[ ]]
777)],
778[AC_MSG_RESULT(yes)
779 AC_DEFINE([HAVE_WEAK_SYMBOLS], [1], [weak symbols are supported])],
780[AC_MSG_RESULT(no)])
781
782AC_MSG_CHECKING(if data alignment is required)
783aligned_access_required=yes
784AS_CASE([$host_cpu],
785  [i?86|amd64|x86_64|powerpc*|s390*],
786    [aligned_access_required=no],
787  [arm*],
788    [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
789#ifndef __ARM_FEATURE_UNALIGNED
790# error data alignment is required
791#endif
792      ]], [[]])], [aligned_access_required=no], [])]
793)
794AS_IF([test "x$aligned_access_required" = "xyes"],
795  [AC_MSG_RESULT(yes)],
796  [AC_MSG_RESULT(no)
797   AC_DEFINE([CPU_UNALIGNED_ACCESS], [1], [unaligned memory access is supported])])
798
799AC_MSG_CHECKING(if atomic operations are supported)
800AC_LINK_IFELSE([AC_LANG_PROGRAM([[ ]], [[
801static volatile int _sodium_lock;
802__sync_lock_test_and_set(&_sodium_lock, 1);
803__sync_lock_release(&_sodium_lock);
804]]
805)],
806[AC_MSG_RESULT(yes)
807 AC_DEFINE([HAVE_ATOMIC_OPS], [1], [atomic operations are supported])],
808[AC_MSG_RESULT(no)])
809
810dnl Checks for functions and headers
811
812AC_FUNC_ALLOCA
813AS_IF([test "x$EMSCRIPTEN" = "x"],[
814  AC_CHECK_FUNCS([arc4random arc4random_buf])
815  AC_CHECK_FUNCS([mmap mlock madvise mprotect])
816
817  AC_MSG_CHECKING(for getrandom with a standard API)
818  AC_LINK_IFELSE([AC_LANG_PROGRAM([[
819#include <stdlib.h>
820#ifdef HAVE_UNISTD_H
821# include <unistd.h>
822#endif
823#ifdef HAVE_SYS_RANDOM_H
824# include <sys/random.h>
825#endif
826#ifdef __SANITIZE_ADDRESS__
827# error A recent libasan version on an old system may intercept nonexistent functions
828#endif
829]], [[
830unsigned char buf;
831(void) getrandom((void *) &buf, 1U, 0U);
832  ]])],
833  [AC_MSG_RESULT(yes)
834   AC_CHECK_FUNCS([getrandom])],
835  [AC_MSG_RESULT(no)
836  ])
837
838  AC_MSG_CHECKING(for getentropy with a standard API)
839  AC_LINK_IFELSE([AC_LANG_PROGRAM([[
840#include <stdlib.h>
841#ifdef HAVE_UNISTD_H
842# include <unistd.h>
843#endif
844#ifdef HAVE_SYS_RANDOM_H
845# include <sys/random.h>
846#endif
847#ifdef __SANITIZE_ADDRESS__
848# error A recent libasan version on an old system may intercept nonexistent functions
849#endif
850]], [[
851unsigned char buf;
852
853if (&getentropy != NULL) {
854  (void) getentropy((void *) &buf, 1U);
855}
856  ]])],
857  [AC_MSG_RESULT(yes)
858   AC_CHECK_FUNCS([getentropy])],
859  [AC_MSG_RESULT(no)
860  ])
861])
862AC_CHECK_FUNCS([posix_memalign getpid nanosleep])
863AC_CHECK_FUNCS([memset_s explicit_bzero explicit_memset])
864
865AC_SUBST([LIBTOOL_EXTRA_FLAGS])
866
867TEST_LDFLAGS=''
868AS_IF([test "x$EMSCRIPTEN" != "x"],[
869  EXEEXT=.js
870  TEST_LDFLAGS='--memory-init-file 0 --pre-js pre.js.inc -s RESERVED_FUNCTION_POINTERS=8'
871])
872AC_SUBST(TEST_LDFLAGS)
873AM_CONDITIONAL([EMSCRIPTEN], [test "x$EMSCRIPTEN" != "x"])
874AM_CONDITIONAL([WASI], [test "x$WASI" != "x"])
875
876AC_DEFINE([CONFIGURED], [1], [the build system was properly configured])
877
878dnl Libtool.
879
880LT_INIT([dlopen])
881AC_LIBTOOL_WIN32_DLL
882gl_LD_OUTPUT_DEF
883
884dnl Output.
885
886AH_VERBATIM([NDEBUG], [/* Always evaluate assert() calls */
887#ifdef NDEBUG
888#/**/undef/**/ NDEBUG
889#endif])
890
891AS_IF([test "x$ENABLE_CWFLAGS" = "xyes"], [
892  CFLAGS="$CFLAGS $CWFLAGS"
893])
894
895AC_CONFIG_FILES([Makefile
896                 builds/Makefile
897                 contrib/Makefile
898                 dist-build/Makefile
899                 libsodium.pc
900                 libsodium-uninstalled.pc
901                 msvc-scripts/Makefile
902                 src/Makefile
903                 src/libsodium/Makefile
904                 src/libsodium/include/Makefile
905                 src/libsodium/include/sodium/version.h
906                 test/default/Makefile
907                 test/Makefile
908                 ])
909AC_OUTPUT
910