xref: /freebsd/sys/contrib/libsodium/configure.ac (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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 -- As a side effect, this disables CPU-specific implementations on non-Windows platforms. Only for use with targets such as WebAssembly and NativeClient.])],
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([-mindirect-branch=thunk],
203  [CFLAGS="$CFLAGS -mindirect-branch=thunk"],
204  [
205    AX_CHECK_COMPILE_FLAG([-mretpoline], [CFLAGS="$CFLAGS -mretpoline"])
206  ])
207
208AX_CHECK_COMPILE_FLAG([-fvisibility=hidden],
209  [CFLAGS="$CFLAGS -fvisibility=hidden"])
210
211AS_CASE([$host_os], [cygwin*|mingw*|msys|pw32*|cegcc*], [ ], [
212  AX_CHECK_COMPILE_FLAG([-fPIC], [CFLAGS="$CFLAGS -fPIC"])
213])
214
215AS_IF([test "$enable_pie" != "no"],[
216  AX_CHECK_COMPILE_FLAG([-fPIE], [
217    AX_CHECK_LINK_FLAG([-pie], [
218      [CFLAGS="$CFLAGS -fPIE"
219       LDFLAGS="$LDFLAGS -pie"]
220    ])
221  ])
222])
223
224AX_CHECK_COMPILE_FLAG([-fno-strict-aliasing], [CFLAGS="$CFLAGS -fno-strict-aliasing"])
225AX_CHECK_COMPILE_FLAG([-fno-strict-overflow], [CFLAGS="$CFLAGS -fno-strict-overflow"], [
226  AX_CHECK_COMPILE_FLAG([-fwrapv], [CFLAGS="$CFLAGS -fwrapv"])
227])
228
229AS_IF([test "$GCC" = "yes" ], [
230  AS_CASE([$host_cpu],
231    [i?86|amd64|x86_64], [
232      AC_COMPILE_IFELSE(
233        [AC_LANG_SOURCE([
234#if !defined(__clang__) && defined(__GNUC__) && ((__GNUC__ << 8) | __GNUC_MINOR__) < 0x403
235# error old gcc
236#endif
237int main(void) { return 0; }
238         ])],,[
239          AX_CHECK_COMPILE_FLAG([-flax-vector-conversions], [CFLAGS="$CFLAGS -flax-vector-conversions"])
240        ])
241      ]
242    )
243  ])
244
245LIBTOOL_OLD_FLAGS="$LIBTOOL_EXTRA_FLAGS"
246LIBTOOL_EXTRA_FLAGS="$LIBTOOL_EXTRA_FLAGS -version-info $SODIUM_LIBRARY_VERSION"
247AC_ARG_ENABLE(soname-versions,
248  [AC_HELP_STRING([--enable-soname-versions], [enable soname versions (must be disabled for Android) (default: enabled)])],
249    [
250        AS_IF([test "x$enableval" = "xno"], [
251          LIBTOOL_EXTRA_FLAGS="$LIBTOOL_OLD_FLAGS -avoid-version"
252        ])
253    ]
254)
255
256AS_CASE([$host_os],
257  [cygwin*|mingw*|msys|pw32*|cegcc*], [
258    AX_CHECK_LINK_FLAG([-Wl,--dynamicbase], [LDFLAGS="$LDFLAGS -Wl,--dynamicbase"])
259    AX_CHECK_LINK_FLAG([-Wl,--high-entropy-va], [LDFLAGS="$LDFLAGS -Wl,--high-entropy-va"])
260    AX_CHECK_LINK_FLAG([-Wl,--nxcompat], [LDFLAGS="$LDFLAGS -Wl,--nxcompat"])
261  ])
262
263AS_CASE([$host_os],
264  [cygwin*|mingw*|msys|pw32*|cegcc*], [
265    AX_CHECK_COMPILE_FLAG([-fno-asynchronous-unwind-tables], [
266      [CFLAGS="$CFLAGS -fno-asynchronous-unwind-tables"]
267    ])
268])
269
270AS_IF([test "x$enable_ssp" != "xno"],[
271
272AS_CASE([$host_os],
273  [cygwin*|mingw*|msys|pw32*|cegcc*|haiku], [ ],
274  [*], [
275    AX_CHECK_COMPILE_FLAG([-fstack-protector], [
276      AX_CHECK_LINK_FLAG([-fstack-protector],
277        [CFLAGS="$CFLAGS -fstack-protector"]
278      )
279    ])
280  ])
281])
282
283AC_ARG_VAR([CWFLAGS], [define to compilation flags for generating extra warnings])
284
285AX_CHECK_COMPILE_FLAG([$CFLAGS -Wall], [CWFLAGS="$CFLAGS -Wall"])
286AX_CHECK_COMPILE_FLAG([$CFLAGS -Wextra], [CWFLAGS="$CFLAGS -Wextra"])
287
288AC_MSG_CHECKING(for clang)
289AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[
290#ifndef __clang__
291#error Not clang
292#endif
293]])],
294  [AC_MSG_RESULT(yes)
295   AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wno-unknown-warning-option],
296     [CWFLAGS="$CWFLAGS -Wno-unknown-warning-option"])
297  ],
298  [AC_MSG_RESULT(no)
299])
300
301AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wbad-function-cast], [CWFLAGS="$CWFLAGS -Wbad-function-cast"])
302AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wcast-qual], [CWFLAGS="$CWFLAGS -Wcast-qual"])
303AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wdiv-by-zero], [CWFLAGS="$CWFLAGS -Wdiv-by-zero"])
304AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wduplicated-branches], [CWFLAGS="$CWFLAGS -Wduplicated-branches"])
305AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wduplicated-cond], [CWFLAGS="$CWFLAGS -Wduplicated-cond"])
306AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wfloat-equal], [CWFLAGS="$CWFLAGS -Wfloat-equal"])
307AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wformat=2], [CWFLAGS="$CWFLAGS -Wformat=2"])
308AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wlogical-op], [CWFLAGS="$CWFLAGS -Wlogical-op"])
309AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wmaybe-uninitialized], [CWFLAGS="$CWFLAGS -Wmaybe-uninitialized"])
310AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wmisleading-indentation], [CWFLAGS="$CWFLAGS -Wmisleading-indentation"])
311AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wmissing-declarations], [CWFLAGS="$CWFLAGS -Wmissing-declarations"])
312AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wmissing-prototypes], [CWFLAGS="$CWFLAGS -Wmissing-prototypes"])
313AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wnested-externs], [CWFLAGS="$CWFLAGS -Wnested-externs"])
314AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wno-type-limits], [CWFLAGS="$CWFLAGS -Wno-type-limits"])
315AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wno-unknown-pragmas], [CWFLAGS="$CWFLAGS -Wno-unknown-pragmas"])
316AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wnormalized=id], [CWFLAGS="$CWFLAGS -Wnormalized=id"])
317AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wnull-dereference], [CWFLAGS="$CWFLAGS -Wnull-dereference"])
318AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wold-style-declaration], [CWFLAGS="$CWFLAGS -Wold-style-declaration"])
319AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wpointer-arith], [CWFLAGS="$CWFLAGS -Wpointer-arith"])
320AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wredundant-decls], [CWFLAGS="$CWFLAGS -Wredundant-decls"])
321AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wrestrict], [CWFLAGS="$CWFLAGS -Wrestrict"])
322AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wshorten-64-to-32], [CWFLAGS="$CWFLAGS -Wshorten-64-to-32"])
323AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wsometimes-uninitialized], [CWFLAGS="$CWFLAGS -Wsometimes-uninitialized"])
324AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wstrict-prototypes], [CWFLAGS="$CWFLAGS -Wstrict-prototypes"])
325AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wswitch-enum], [CWFLAGS="$CWFLAGS -Wswitch-enum"])
326AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wvariable-decl], [CWFLAGS="$CWFLAGS -Wvariable-decl"])
327AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wwrite-strings], [CWFLAGS="$CWFLAGS -Wwrite-strings"])
328
329AX_CHECK_LINK_FLAG([-Wl,-z,relro], [LDFLAGS="$LDFLAGS -Wl,-z,relro"])
330AX_CHECK_LINK_FLAG([-Wl,-z,now], [LDFLAGS="$LDFLAGS -Wl,-z,now"])
331AX_CHECK_LINK_FLAG([-Wl,-z,noexecstack], [LDFLAGS="$LDFLAGS -Wl,-z,noexecstack"])
332
333AC_MSG_CHECKING(for a broken clang + AVX512 combination)
334AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[
335#if !(defined(__AVX512F__) && defined(__clang__) && __clang_major__ < 4)
336#error Not a broken clang + AVX512 combination
337#endif
338]])],
339  [AC_MSG_RESULT(yes - disabling AVX512 optimizations)
340   AX_CHECK_COMPILE_FLAG([$CFLAGS -mno-avx512f],
341     [CFLAGS="$CFLAGS -mno-avx512f"])
342  ],
343  [AC_MSG_RESULT(no)
344])
345
346AX_CHECK_CATCHABLE_SEGV
347AX_CHECK_CATCHABLE_ABRT
348
349AS_IF([test "x$with_threads" = "xyes"], [
350  AX_TLS([AC_MSG_RESULT(thread local storage is supported)],
351         [AC_MSG_RESULT(thread local storage is not supported)]) ])
352
353LT_INIT
354AC_SUBST(LIBTOOL_DEPS)
355
356AC_ARG_VAR([AR], [path to the ar utility])
357AC_CHECK_TOOL([AR], [ar], [ar])
358
359dnl Checks for headers
360
361AS_IF([test "x$EMSCRIPTEN" = "x" -a "$host_os" != "pnacl"], [
362
363  oldcflags="$CFLAGS"
364  AX_CHECK_COMPILE_FLAG([-mmmx], [CFLAGS="$CFLAGS -mmmx"])
365  AC_MSG_CHECKING(for MMX instructions set)
366  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
367#pragma GCC target("mmx")
368#include <mmintrin.h>
369]], [[ __m64 x = _mm_setzero_si64(); ]])],
370    [AC_MSG_RESULT(yes)
371     AC_DEFINE([HAVE_MMINTRIN_H], [1], [mmx is available])
372     AX_CHECK_COMPILE_FLAG([-mmmx], [CFLAGS_MMX="-mmmx"])],
373    [AC_MSG_RESULT(no)])
374  CFLAGS="$oldcflags"
375
376  oldcflags="$CFLAGS"
377  AX_CHECK_COMPILE_FLAG([-msse2], [CFLAGS="$CFLAGS -msse2"])
378  AC_MSG_CHECKING(for SSE2 instructions set)
379  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
380#pragma GCC target("sse2")
381#ifndef __SSE2__
382# define __SSE2__
383#endif
384#include <emmintrin.h>
385]], [[ __m128d x = _mm_setzero_pd();
386       __m128i z = _mm_srli_epi64(_mm_setzero_si128(), 26); ]])],
387    [AC_MSG_RESULT(yes)
388     AC_DEFINE([HAVE_EMMINTRIN_H], [1], [sse2 is available])
389     AX_CHECK_COMPILE_FLAG([-msse2], [CFLAGS_SSE2="-msse2"])],
390    [AC_MSG_RESULT(no)])
391  CFLAGS="$oldcflags"
392
393  oldcflags="$CFLAGS"
394  AX_CHECK_COMPILE_FLAG([-msse3], [CFLAGS="$CFLAGS -msse3"])
395  AC_MSG_CHECKING(for SSE3 instructions set)
396  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
397#pragma GCC target("sse3")
398#include <pmmintrin.h>
399]], [[ __m128 x = _mm_addsub_ps(_mm_cvtpd_ps(_mm_setzero_pd()),
400                                _mm_cvtpd_ps(_mm_setzero_pd())); ]])],
401    [AC_MSG_RESULT(yes)
402     AC_DEFINE([HAVE_PMMINTRIN_H], [1], [sse3 is available])
403     AX_CHECK_COMPILE_FLAG([-msse3], [CFLAGS_SSE3="-msse3"])],
404    [AC_MSG_RESULT(no)])
405  CFLAGS="$oldcflags"
406
407  oldcflags="$CFLAGS"
408  AX_CHECK_COMPILE_FLAG([-mssse3], [CFLAGS="$CFLAGS -mssse3"])
409  AC_MSG_CHECKING(for SSSE3 instructions set)
410  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
411#pragma GCC target("ssse3")
412#include <tmmintrin.h>
413]], [[ __m64 x = _mm_abs_pi32(_m_from_int(0)); ]])],
414    [AC_MSG_RESULT(yes)
415     AC_DEFINE([HAVE_TMMINTRIN_H], [1], [ssse3 is available])
416     AX_CHECK_COMPILE_FLAG([-mssse3], [CFLAGS_SSSE3="-mssse3"])],
417    [AC_MSG_RESULT(no)])
418  CFLAGS="$oldcflags"
419
420  oldcflags="$CFLAGS"
421  AX_CHECK_COMPILE_FLAG([-msse4.1], [CFLAGS="$CFLAGS -msse4.1"])
422  AC_MSG_CHECKING(for SSE4.1 instructions set)
423  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
424#pragma GCC target("sse4.1")
425#include <smmintrin.h>
426]], [[ __m128i x = _mm_minpos_epu16(_mm_setzero_si128()); ]])],
427    [AC_MSG_RESULT(yes)
428     AC_DEFINE([HAVE_SMMINTRIN_H], [1], [sse4.1 is available])
429     AX_CHECK_COMPILE_FLAG([-msse4.1], [CFLAGS_SSE41="-msse4.1"])],
430    [AC_MSG_RESULT(no)])
431  CFLAGS="$oldcflags"
432
433  oldcflags="$CFLAGS"
434  AX_CHECK_COMPILE_FLAG([-mavx], [CFLAGS="$CFLAGS -mavx"])
435  AC_MSG_CHECKING(for AVX instructions set)
436  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
437#ifdef __native_client__
438# error NativeClient detected - Avoiding AVX opcodes
439#endif
440#pragma GCC target("avx")
441#include <immintrin.h>
442]], [[ _mm256_zeroall(); ]])],
443    [AC_MSG_RESULT(yes)
444     AC_DEFINE([HAVE_AVXINTRIN_H], [1], [AVX is available])
445     AX_CHECK_COMPILE_FLAG([-mavx], [CFLAGS_AVX="-mavx"])],
446    [AC_MSG_RESULT(no)])
447  CFLAGS="$oldcflags"
448
449  oldcflags="$CFLAGS"
450  AX_CHECK_COMPILE_FLAG([-mavx2], [CFLAGS="$CFLAGS -mavx2"])
451  AC_MSG_CHECKING(for AVX2 instructions set)
452  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
453#ifdef __native_client__
454# error NativeClient detected - Avoiding AVX2 opcodes
455#endif
456#pragma GCC target("avx2")
457#include <immintrin.h>
458]], [[
459__m256 x = _mm256_set1_ps(3.14);
460__m256 y = _mm256_permutevar8x32_ps(x, _mm256_set1_epi32(42));
461return _mm256_movemask_ps(_mm256_cmp_ps(x, y, _CMP_NEQ_OQ));
462]])],
463    [AC_MSG_RESULT(yes)
464     AC_DEFINE([HAVE_AVX2INTRIN_H], [1], [AVX2 is available])
465     AX_CHECK_COMPILE_FLAG([-mavx2], [CFLAGS_AVX2="-mavx2"])
466     AC_MSG_CHECKING(if _mm256_broadcastsi128_si256 is correctly defined)
467     AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
468#ifdef __native_client__
469# error NativeClient detected - Avoiding AVX2 opcodes
470#endif
471#pragma GCC target("avx2")
472#include <immintrin.h>
473     ]], [[ __m256i y = _mm256_broadcastsi128_si256(_mm_setzero_si128()); ]])],
474       [AC_MSG_RESULT(yes)],
475       [AC_MSG_RESULT(no)
476        AC_DEFINE([_mm256_broadcastsi128_si256], [_mm_broadcastsi128_si256],
477                  [Define to the local name of _mm256_broadcastsi128_si256])])
478     ],
479    [AC_MSG_RESULT(no)])
480  CFLAGS="$oldcflags"
481
482  oldcflags="$CFLAGS"
483  AX_CHECK_COMPILE_FLAG([-mavx512f], [CFLAGS="$CFLAGS -mavx512f"])
484  AC_MSG_CHECKING(for AVX512F instructions set)
485  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
486#ifdef __native_client__
487# error NativeClient detected - Avoiding AVX512F opcodes
488#endif
489#pragma GCC target("avx512f")
490#include <immintrin.h>
491]], [[
492__m512i x = _mm512_setzero_epi32();
493__m512i y = _mm512_permutexvar_epi64(_mm512_setr_epi64(0, 1, 4, 5, 2, 3, 6, 7), x);
494]])],
495    [AC_MSG_RESULT(yes)
496     AC_DEFINE([HAVE_AVX512FINTRIN_H], [1], [AVX512F is available])
497     AX_CHECK_COMPILE_FLAG([-mavx512f], [CFLAGS_AVX512F="-mavx512f"])],
498    [AC_MSG_RESULT(no)])
499  CFLAGS="$oldcflags"
500
501  oldcflags="$CFLAGS"
502  AX_CHECK_COMPILE_FLAG([-maes], [CFLAGS="$CFLAGS -maes"])
503  AX_CHECK_COMPILE_FLAG([-mpclmul], [CFLAGS="$CFLAGS -mpclmul"])
504  AC_MSG_CHECKING(for AESNI instructions set and PCLMULQDQ)
505  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
506#ifdef __native_client__
507# error NativeClient detected - Avoiding AESNI opcodes
508#endif
509#pragma GCC target("aes")
510#pragma GCC target("pclmul")
511#include <wmmintrin.h>
512]], [[ __m128i x = _mm_aesimc_si128(_mm_setzero_si128());
513       __m128i y = _mm_clmulepi64_si128(_mm_setzero_si128(), _mm_setzero_si128(), 0);]])],
514    [AC_MSG_RESULT(yes)
515     AC_DEFINE([HAVE_WMMINTRIN_H], [1], [aesni is available])
516     AX_CHECK_COMPILE_FLAG([-maes], [CFLAGS_AESNI="-maes"])
517     AX_CHECK_COMPILE_FLAG([-mpclmul], [CFLAGS_PCLMUL="-mpclmul"])
518     ],
519    [AC_MSG_RESULT(no)])
520  CFLAGS="$oldcflags"
521
522  oldcflags="$CFLAGS"
523  AX_CHECK_COMPILE_FLAG([-mrdrnd], [CFLAGS="$CFLAGS -mrdrnd"])
524  AC_MSG_CHECKING(for RDRAND)
525  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
526#ifdef __native_client__
527# error NativeClient detected - Avoiding RDRAND opcodes
528#endif
529#pragma GCC target("rdrnd")
530#include <immintrin.h>
531]], [[ unsigned long long x; _rdrand64_step(&x); ]])],
532    [AC_MSG_RESULT(yes)
533     AC_DEFINE([HAVE_RDRAND], [1], [rdrand is available])
534     AX_CHECK_COMPILE_FLAG([-mrdrnd], [CFLAGS_RDRAND="-mrdrnd"])
535     ],
536    [AC_MSG_RESULT(no)])
537  CFLAGS="$oldcflags"
538
539])
540
541AC_SUBST(CFLAGS_MMX)
542AC_SUBST(CFLAGS_SSE2)
543AC_SUBST(CFLAGS_SSE3)
544AC_SUBST(CFLAGS_SSSE3)
545AC_SUBST(CFLAGS_SSE41)
546AC_SUBST(CFLAGS_AVX)
547AC_SUBST(CFLAGS_AVX2)
548AC_SUBST(CFLAGS_AVX512F)
549AC_SUBST(CFLAGS_AESNI)
550AC_SUBST(CFLAGS_PCLMUL)
551AC_SUBST(CFLAGS_RDRAND)
552
553AC_CHECK_HEADERS([sys/mman.h intrin.h])
554
555AC_MSG_CHECKING([if _xgetbv() is available])
556AC_LINK_IFELSE(
557  [AC_LANG_PROGRAM([[ #include <intrin.h> ]], [[ (void) _xgetbv(0) ]])],
558  [AC_MSG_RESULT(yes)
559   AC_DEFINE([HAVE__XGETBV], [1], [_xgetbv() is available])],
560  [AC_MSG_RESULT(no)])
561
562dnl Checks for typedefs, structures, and compiler characteristics.
563
564AC_C_INLINE
565AS_CASE([$host_cpu],
566  [i?86|amd64|x86_64],
567    [ac_cv_c_bigendian=no]
568)
569AC_C_BIGENDIAN(
570  AC_DEFINE(NATIVE_BIG_ENDIAN, 1, [machine is bigendian]),
571  AC_DEFINE(NATIVE_LITTLE_ENDIAN, 1, [machine is littleendian]),
572  AC_MSG_ERROR([unknown endianness]),
573  AC_MSG_ERROR([universal endianness is not supported - compile separately and use lipo(1)])
574)
575
576AC_MSG_CHECKING(whether __STDC_LIMIT_MACROS is required)
577AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
578#include <limits.h>
579#include <stdint.h>
580]], [[
581(void) SIZE_MAX;
582(void) UINT64_MAX;
583]])],
584  [AC_MSG_RESULT(no)],
585  [AC_MSG_RESULT(yes)
586   CPPFLAGS="$CPPFLAGS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS"
587])
588
589AC_MSG_CHECKING(whether we can use inline asm code)
590AC_LINK_IFELSE([AC_LANG_PROGRAM([[
591]], [[
592int a = 42;
593int *pnt = &a;
594__asm__ __volatile__ ("" : : "r"(pnt) : "memory");
595]])],
596  [AC_MSG_RESULT(yes)
597   AC_DEFINE([HAVE_INLINE_ASM], [1], [inline asm code can be used])]
598  [AC_MSG_RESULT(no)]
599)
600
601HAVE_AMD64_ASM_V=0
602AS_IF([test "$enable_asm" != "no"],[
603  AC_MSG_CHECKING(whether we can use x86_64 asm code)
604  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
605  ]], [[
606#if defined(__amd64) || defined(__amd64__) || defined(__x86_64__)
607# if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || defined(_WIN32) || defined(_WIN64)
608#  error Windows x86_64 calling conventions are not supported yet
609# endif
610/* neat */
611#else
612# error !x86_64
613#endif
614unsigned char i = 0, o = 0, t;
615__asm__ __volatile__ ("pxor %%xmm12, %%xmm6 \n"
616                      "movb (%[i]), %[t] \n"
617                      "addb %[t], (%[o]) \n"
618                      : [t] "=&r"(t)
619                      : [o] "D"(&o), [i] "S"(&i)
620                      : "memory", "flags", "cc");
621]])],
622  [AC_MSG_RESULT(yes)
623   AC_DEFINE([HAVE_AMD64_ASM], [1], [x86_64 asm code can be used])
624   HAVE_AMD64_ASM_V=1],
625  [AC_MSG_RESULT(no)])
626])
627AM_CONDITIONAL([HAVE_AMD64_ASM], [test $HAVE_AMD64_ASM_V = 1])
628AC_SUBST(HAVE_AMD64_ASM_V)
629
630HAVE_AVX_ASM_V=0
631AS_IF([test "$enable_asm" != "no"],[
632  AC_MSG_CHECKING(whether we can assemble AVX opcodes)
633  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
634  ]], [[
635#if defined(__amd64) || defined(__amd64__) || defined(__x86_64__)
636# if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || defined(_WIN32) || defined(_WIN64)
637#  error Windows x86_64 calling conventions are not supported yet
638# endif
639/* neat */
640#else
641# error !x86_64
642#endif
643__asm__ __volatile__ ("vpunpcklqdq %xmm0,%xmm13,%xmm0");
644]])],
645  [AC_MSG_RESULT(yes)
646   AC_DEFINE([HAVE_AVX_ASM], [1], [AVX opcodes are supported])
647   HAVE_AVX_ASM_V=1],
648  [AC_MSG_RESULT(no)])
649])
650AM_CONDITIONAL([HAVE_AVX_ASM], [test $HAVE_AVX_ASM_V = 1])
651AC_SUBST(HAVE_AVX_ASM_V)
652
653AC_MSG_CHECKING(for 128-bit arithmetic)
654HAVE_TI_MODE_V=0
655AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
656#if !defined(__clang__) && !defined(__GNUC__) && !defined(__SIZEOF_INT128__)
657# error mode(TI) is a gcc extension, and __int128 is not available
658#endif
659#if defined(__clang__) && !defined(__x86_64__) && !defined(__aarch64__)
660# error clang does not properly handle the 128-bit type on 32-bit systems
661#endif
662#ifndef NATIVE_LITTLE_ENDIAN
663# error libsodium currently expects a little endian CPU for the 128-bit type
664#endif
665#ifdef __EMSCRIPTEN__
666# error emscripten currently doesn't support some operations on integers larger than 64 bits
667#endif
668#include <stddef.h>
669#include <stdint.h>
670#if defined(__SIZEOF_INT128__)
671typedef unsigned __int128 uint128_t;
672#else
673typedef unsigned uint128_t __attribute__((mode(TI)));
674#endif
675void fcontract(uint128_t *t) {
676  *t += 0x8000000000000 - 1;
677  *t *= *t;
678  *t >>= 84;
679}
680]], [[
681(void) fcontract;
682]])],
683[AC_MSG_RESULT(yes)
684 AC_DEFINE([HAVE_TI_MODE], [1], [gcc TI mode is available])
685 HAVE_TI_MODE_V=1],
686[AC_MSG_RESULT(no)])
687AM_CONDITIONAL([HAVE_TI_MODE], [test $HAVE_TI_MODE_V = 1])
688AC_SUBST(HAVE_TI_MODE_V)
689
690HAVE_CPUID_V=0
691AS_IF([test "$enable_asm" != "no" -o "$host_alias" = "x86_64-nacl"],[
692  AC_MSG_CHECKING(for cpuid instruction)
693  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ ]], [[
694unsigned int cpu_info[4];
695__asm__ __volatile__ ("xchgl %%ebx, %k1; cpuid; xchgl %%ebx, %k1" :
696                      "=a" (cpu_info[0]), "=&r" (cpu_info[1]),
697                      "=c" (cpu_info[2]), "=d" (cpu_info[3]) :
698                      "0" (0U), "2" (0U));
699  ]])],
700  [AC_MSG_RESULT(yes)
701   AC_DEFINE([HAVE_CPUID], [1], [cpuid instruction is available])
702   HAVE_CPUID_V=1],
703  [AC_MSG_RESULT(no)])
704  ])
705AC_SUBST(HAVE_CPUID_V)
706
707asm_hide_symbol="unsupported"
708AS_IF([test "$enable_asm" != "no" -o "$host_os" = "nacl"],[
709  AC_MSG_CHECKING(if the .private_extern asm directive is supported)
710  AC_LINK_IFELSE([AC_LANG_PROGRAM([[ ]], [[
711__asm__ __volatile__ (".private_extern dummy_symbol \n"
712                      ".private_extern _dummy_symbol \n"
713                      ".globl dummy_symbol \n"
714                      ".globl _dummy_symbol \n"
715                      "dummy_symbol: \n"
716                      "_dummy_symbol: \n"
717                      "    nop \n"
718);
719  ]])],
720  [AC_MSG_RESULT(yes)
721   asm_hide_symbol=".private_extern"],
722  [AC_MSG_RESULT(no)])
723
724  AC_MSG_CHECKING(if the .hidden asm directive is supported)
725  AC_LINK_IFELSE([AC_LANG_PROGRAM([[ ]], [[
726__asm__ __volatile__ (".hidden dummy_symbol \n"
727                      ".hidden _dummy_symbol \n"
728                      ".globl dummy_symbol \n"
729                      ".globl _dummy_symbol \n"
730                      "dummy_symbol: \n"
731                      "_dummy_symbol: \n"
732                      "    nop \n"
733);
734  ]])],
735  [AC_MSG_RESULT(yes)
736   AS_IF([test "$asm_hide_symbol" = "unsupported"],
737          [asm_hide_symbol=".hidden"],
738          [AC_MSG_NOTICE([unable to reliably tag symbols as private])
739           asm_hide_symbol="unsupported"])
740  ],
741  [AC_MSG_RESULT(no)])
742
743  AS_IF([test "$asm_hide_symbol" != "unsupported"],[
744    AC_DEFINE_UNQUOTED([ASM_HIDE_SYMBOL], [$asm_hide_symbol], [directive to hide symbols])
745  ])
746])
747
748AC_MSG_CHECKING(if weak symbols are supported)
749AC_LINK_IFELSE([AC_LANG_PROGRAM([[
750#if !defined(__ELF__) && !defined(__APPLE_CC__)
751# error Support for weak symbols may not be available
752#endif
753__attribute__((weak)) void __dummy(void *x) { }
754void f(void *x) { __dummy(x); }
755]], [[ ]]
756)],
757[AC_MSG_RESULT(yes)
758 AC_DEFINE([HAVE_WEAK_SYMBOLS], [1], [weak symbols are supported])],
759[AC_MSG_RESULT(no)])
760
761AC_MSG_CHECKING(if data alignment is required)
762aligned_access_required=yes
763AS_CASE([$host_cpu],
764  [i?86|amd64|x86_64|powerpc*|s390*],
765    [aligned_access_required=no],
766  [arm*],
767    [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
768#ifndef __ARM_FEATURE_UNALIGNED
769# error data alignment is required
770#endif
771      ]], [[]])], [aligned_access_required=no], [])]
772)
773AS_IF([test "x$aligned_access_required" = "xyes"],
774  [AC_MSG_RESULT(yes)],
775  [AC_MSG_RESULT(no)
776   AC_DEFINE([CPU_UNALIGNED_ACCESS], [1], [unaligned memory access is supported])])
777
778AC_MSG_CHECKING(if atomic operations are supported)
779AC_LINK_IFELSE([AC_LANG_PROGRAM([[ ]], [[
780static volatile int _sodium_lock;
781__sync_lock_test_and_set(&_sodium_lock, 1);
782__sync_lock_release(&_sodium_lock);
783]]
784)],
785[AC_MSG_RESULT(yes)
786 AC_DEFINE([HAVE_ATOMIC_OPS], [1], [atomic operations are supported])],
787[AC_MSG_RESULT(no)])
788
789dnl Checks for functions and headers
790
791AC_FUNC_ALLOCA
792AS_IF([test "x$EMSCRIPTEN" = "x"],[
793  AC_CHECK_FUNCS([arc4random arc4random_buf])
794  AC_CHECK_FUNCS([mmap mlock madvise mprotect memset_s explicit_bzero nanosleep])
795])
796AC_CHECK_FUNCS([posix_memalign getpid])
797
798AC_SUBST([LIBTOOL_EXTRA_FLAGS])
799
800TEST_LDFLAGS=''
801AS_IF([test "x$EMSCRIPTEN" != "x"],[
802  EXEEXT=.js
803  TEST_LDFLAGS='--memory-init-file 0 --pre-js pre.js.inc -s RESERVED_FUNCTION_POINTERS=8'
804])
805AC_SUBST(TEST_LDFLAGS)
806AM_CONDITIONAL([EMSCRIPTEN], [test "x$EMSCRIPTEN" != "x"])
807
808AM_CONDITIONAL([NATIVECLIENT], [test "x$NATIVECLIENT" != "x"])
809
810AC_DEFINE([CONFIGURED], [1], [the build system was properly configured])
811
812dnl Libtool.
813
814LT_INIT([dlopen])
815AC_LIBTOOL_WIN32_DLL
816gl_LD_OUTPUT_DEF
817
818dnl Output.
819
820AH_VERBATIM([NDEBUG], [/* Always evaluate assert() calls */
821#ifdef NDEBUG
822#/**/undef/**/ NDEBUG
823#endif])
824
825AS_IF([test "x$ENABLE_CWFLAGS" = "xyes"], [
826  CFLAGS="$CFLAGS $CWFLAGS"
827])
828
829AC_CONFIG_FILES([Makefile
830                 builds/Makefile
831                 contrib/Makefile
832                 dist-build/Makefile
833                 libsodium.pc
834                 libsodium-uninstalled.pc
835                 msvc-scripts/Makefile
836                 src/Makefile
837                 src/libsodium/Makefile
838                 src/libsodium/include/Makefile
839                 src/libsodium/include/sodium/version.h
840                 test/default/Makefile
841                 test/Makefile
842                 ])
843AC_OUTPUT
844