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