1AC_PREREQ([2.60])
2AC_INIT([libsecp256k1],[0.1])
3AC_CONFIG_AUX_DIR([build-aux])
4AC_CONFIG_MACRO_DIR([build-aux/m4])
5AC_CANONICAL_HOST
6AH_TOP([#ifndef LIBSECP256K1_CONFIG_H])
7AH_TOP([#define LIBSECP256K1_CONFIG_H])
8AH_BOTTOM([#endif /*LIBSECP256K1_CONFIG_H*/])
9AM_INIT_AUTOMAKE([foreign subdir-objects])
10LT_INIT
11
12dnl make the compilation flags quiet unless V=1 is used
13m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
14
15PKG_PROG_PKG_CONFIG
16
17AC_PATH_TOOL(AR, ar)
18AC_PATH_TOOL(RANLIB, ranlib)
19AC_PATH_TOOL(STRIP, strip)
20AX_PROG_CC_FOR_BUILD
21
22if test "x$CFLAGS" = "x"; then
23  CFLAGS="-g"
24fi
25
26AM_PROG_CC_C_O
27
28AC_PROG_CC_C89
29if test x"$ac_cv_prog_cc_c89" = x"no"; then
30  AC_MSG_ERROR([c89 compiler support required])
31fi
32AM_PROG_AS
33
34case $host_os in
35  *darwin*)
36     if  test x$cross_compiling != xyes; then
37       AC_PATH_PROG([BREW],brew,)
38       if test x$BREW != x; then
39         dnl These Homebrew packages may be keg-only, meaning that they won't be found
40         dnl in expected paths because they may conflict with system files. Ask
41         dnl Homebrew where each one is located, then adjust paths accordingly.
42
43         openssl_prefix=`$BREW --prefix openssl 2>/dev/null`
44         gmp_prefix=`$BREW --prefix gmp 2>/dev/null`
45         if test x$openssl_prefix != x; then
46           PKG_CONFIG_PATH="$openssl_prefix/lib/pkgconfig:$PKG_CONFIG_PATH"
47           export PKG_CONFIG_PATH
48         fi
49         if test x$gmp_prefix != x; then
50           GMP_CPPFLAGS="-I$gmp_prefix/include"
51           GMP_LIBS="-L$gmp_prefix/lib"
52         fi
53       else
54         AC_PATH_PROG([PORT],port,)
55         dnl if homebrew isn't installed and macports is, add the macports default paths
56         dnl as a last resort.
57         if test x$PORT != x; then
58           CPPFLAGS="$CPPFLAGS -isystem /opt/local/include"
59           LDFLAGS="$LDFLAGS -L/opt/local/lib"
60         fi
61       fi
62     fi
63   ;;
64esac
65
66CFLAGS="$CFLAGS -W"
67
68warn_CFLAGS="-std=c89 -pedantic -Wall -Wextra -Wcast-align -Wnested-externs -Wshadow -Wstrict-prototypes -Wno-unused-function -Wno-long-long -Wno-overlength-strings"
69saved_CFLAGS="$CFLAGS"
70CFLAGS="$CFLAGS $warn_CFLAGS"
71AC_MSG_CHECKING([if ${CC} supports ${warn_CFLAGS}])
72AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
73    [ AC_MSG_RESULT([yes]) ],
74    [ AC_MSG_RESULT([no])
75      CFLAGS="$saved_CFLAGS"
76    ])
77
78saved_CFLAGS="$CFLAGS"
79CFLAGS="$CFLAGS -fvisibility=hidden"
80AC_MSG_CHECKING([if ${CC} supports -fvisibility=hidden])
81AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
82    [ AC_MSG_RESULT([yes]) ],
83    [ AC_MSG_RESULT([no])
84      CFLAGS="$saved_CFLAGS"
85    ])
86
87AC_ARG_ENABLE(benchmark,
88    AS_HELP_STRING([--enable-benchmark],[compile benchmark (default is no)]),
89    [use_benchmark=$enableval],
90    [use_benchmark=no])
91
92AC_ARG_ENABLE(coverage,
93    AS_HELP_STRING([--enable-coverage],[enable compiler flags to support kcov coverage analysis]),
94    [enable_coverage=$enableval],
95    [enable_coverage=no])
96
97AC_ARG_ENABLE(tests,
98    AS_HELP_STRING([--enable-tests],[compile tests (default is yes)]),
99    [use_tests=$enableval],
100    [use_tests=yes])
101
102AC_ARG_ENABLE(openssl_tests,
103    AS_HELP_STRING([--enable-openssl-tests],[enable OpenSSL tests, if OpenSSL is available (default is auto)]),
104    [enable_openssl_tests=$enableval],
105    [enable_openssl_tests=auto])
106
107AC_ARG_ENABLE(experimental,
108    AS_HELP_STRING([--enable-experimental],[allow experimental configure options (default is no)]),
109    [use_experimental=$enableval],
110    [use_experimental=no])
111
112AC_ARG_ENABLE(exhaustive_tests,
113    AS_HELP_STRING([--enable-exhaustive-tests],[compile exhaustive tests (default is yes)]),
114    [use_exhaustive_tests=$enableval],
115    [use_exhaustive_tests=yes])
116
117AC_ARG_ENABLE(endomorphism,
118    AS_HELP_STRING([--enable-endomorphism],[enable endomorphism (default is no)]),
119    [use_endomorphism=$enableval],
120    [use_endomorphism=no])
121
122AC_ARG_ENABLE(ecmult_static_precomputation,
123    AS_HELP_STRING([--enable-ecmult-static-precomputation],[enable precomputed ecmult table for signing (default is yes)]),
124    [use_ecmult_static_precomputation=$enableval],
125    [use_ecmult_static_precomputation=auto])
126
127AC_ARG_ENABLE(module_ecdh,
128    AS_HELP_STRING([--enable-module-ecdh],[enable ECDH shared secret computation (experimental)]),
129    [enable_module_ecdh=$enableval],
130    [enable_module_ecdh=no])
131
132AC_ARG_ENABLE(module_recovery,
133    AS_HELP_STRING([--enable-module-recovery],[enable ECDSA pubkey recovery module (default is no)]),
134    [enable_module_recovery=$enableval],
135    [enable_module_recovery=no])
136
137AC_ARG_ENABLE(jni,
138    AS_HELP_STRING([--enable-jni],[enable libsecp256k1_jni (default is auto)]),
139    [use_jni=$enableval],
140    [use_jni=auto])
141
142AC_ARG_WITH([field], [AS_HELP_STRING([--with-field=64bit|32bit|auto],
143[Specify Field Implementation. Default is auto])],[req_field=$withval], [req_field=auto])
144
145AC_ARG_WITH([bignum], [AS_HELP_STRING([--with-bignum=gmp|no|auto],
146[Specify Bignum Implementation. Default is auto])],[req_bignum=$withval], [req_bignum=auto])
147
148AC_ARG_WITH([scalar], [AS_HELP_STRING([--with-scalar=64bit|32bit|auto],
149[Specify scalar implementation. Default is auto])],[req_scalar=$withval], [req_scalar=auto])
150
151AC_ARG_WITH([asm], [AS_HELP_STRING([--with-asm=x86_64|arm|no|auto]
152[Specify assembly optimizations to use. Default is auto (experimental: arm)])],[req_asm=$withval], [req_asm=auto])
153
154AC_CHECK_TYPES([__int128])
155
156AC_MSG_CHECKING([for __builtin_expect])
157AC_COMPILE_IFELSE([AC_LANG_SOURCE([[void myfunc() {__builtin_expect(0,0);}]])],
158    [ AC_MSG_RESULT([yes]);AC_DEFINE(HAVE_BUILTIN_EXPECT,1,[Define this symbol if __builtin_expect is available]) ],
159    [ AC_MSG_RESULT([no])
160    ])
161
162if test x"$enable_coverage" = x"yes"; then
163    AC_DEFINE(COVERAGE, 1, [Define this symbol to compile out all VERIFY code])
164    CFLAGS="$CFLAGS -O0 --coverage"
165    LDFLAGS="--coverage"
166else
167    CFLAGS="$CFLAGS -O3"
168fi
169
170if test x"$use_ecmult_static_precomputation" != x"no"; then
171  save_cross_compiling=$cross_compiling
172  cross_compiling=no
173  TEMP_CC="$CC"
174  CC="$CC_FOR_BUILD"
175  AC_MSG_CHECKING([native compiler: ${CC_FOR_BUILD}])
176  AC_RUN_IFELSE(
177    [AC_LANG_PROGRAM([], [return 0])],
178    [working_native_cc=yes],
179    [working_native_cc=no],[dnl])
180  CC="$TEMP_CC"
181  cross_compiling=$save_cross_compiling
182
183  if test x"$working_native_cc" = x"no"; then
184    set_precomp=no
185    if test x"$use_ecmult_static_precomputation" = x"yes";  then
186      AC_MSG_ERROR([${CC_FOR_BUILD} does not produce working binaries. Please set CC_FOR_BUILD])
187    else
188      AC_MSG_RESULT([${CC_FOR_BUILD} does not produce working binaries. Please set CC_FOR_BUILD])
189    fi
190  else
191    AC_MSG_RESULT([ok])
192    set_precomp=yes
193  fi
194else
195  set_precomp=no
196fi
197
198if test x"$req_asm" = x"auto"; then
199  SECP_64BIT_ASM_CHECK
200  if test x"$has_64bit_asm" = x"yes"; then
201    set_asm=x86_64
202  fi
203  if test x"$set_asm" = x; then
204    set_asm=no
205  fi
206else
207  set_asm=$req_asm
208  case $set_asm in
209  x86_64)
210    SECP_64BIT_ASM_CHECK
211    if test x"$has_64bit_asm" != x"yes"; then
212      AC_MSG_ERROR([x86_64 assembly optimization requested but not available])
213    fi
214    ;;
215  arm)
216    ;;
217  no)
218    ;;
219  *)
220    AC_MSG_ERROR([invalid assembly optimization selection])
221    ;;
222  esac
223fi
224
225if test x"$req_field" = x"auto"; then
226  if test x"set_asm" = x"x86_64"; then
227    set_field=64bit
228  fi
229  if test x"$set_field" = x; then
230    SECP_INT128_CHECK
231    if test x"$has_int128" = x"yes"; then
232      set_field=64bit
233    fi
234  fi
235  if test x"$set_field" = x; then
236    set_field=32bit
237  fi
238else
239  set_field=$req_field
240  case $set_field in
241  64bit)
242    if test x"$set_asm" != x"x86_64"; then
243      SECP_INT128_CHECK
244      if test x"$has_int128" != x"yes"; then
245        AC_MSG_ERROR([64bit field explicitly requested but neither __int128 support or x86_64 assembly available])
246      fi
247    fi
248    ;;
249  32bit)
250    ;;
251  *)
252    AC_MSG_ERROR([invalid field implementation selection])
253    ;;
254  esac
255fi
256
257if test x"$req_scalar" = x"auto"; then
258  SECP_INT128_CHECK
259  if test x"$has_int128" = x"yes"; then
260    set_scalar=64bit
261  fi
262  if test x"$set_scalar" = x; then
263    set_scalar=32bit
264  fi
265else
266  set_scalar=$req_scalar
267  case $set_scalar in
268  64bit)
269    SECP_INT128_CHECK
270    if test x"$has_int128" != x"yes"; then
271      AC_MSG_ERROR([64bit scalar explicitly requested but __int128 support not available])
272    fi
273    ;;
274  32bit)
275    ;;
276  *)
277    AC_MSG_ERROR([invalid scalar implementation selected])
278    ;;
279  esac
280fi
281
282if test x"$req_bignum" = x"auto"; then
283  SECP_GMP_CHECK
284  if test x"$has_gmp" = x"yes"; then
285    set_bignum=gmp
286  fi
287
288  if test x"$set_bignum" = x; then
289    set_bignum=no
290  fi
291else
292  set_bignum=$req_bignum
293  case $set_bignum in
294  gmp)
295    SECP_GMP_CHECK
296    if test x"$has_gmp" != x"yes"; then
297      AC_MSG_ERROR([gmp bignum explicitly requested but libgmp not available])
298    fi
299    ;;
300  no)
301    ;;
302  *)
303    AC_MSG_ERROR([invalid bignum implementation selection])
304    ;;
305  esac
306fi
307
308# select assembly optimization
309use_external_asm=no
310
311case $set_asm in
312x86_64)
313  AC_DEFINE(USE_ASM_X86_64, 1, [Define this symbol to enable x86_64 assembly optimizations])
314  ;;
315arm)
316  use_external_asm=yes
317  ;;
318no)
319  ;;
320*)
321  AC_MSG_ERROR([invalid assembly optimizations])
322  ;;
323esac
324
325# select field implementation
326case $set_field in
32764bit)
328  AC_DEFINE(USE_FIELD_5X52, 1, [Define this symbol to use the FIELD_5X52 implementation])
329  ;;
33032bit)
331  AC_DEFINE(USE_FIELD_10X26, 1, [Define this symbol to use the FIELD_10X26 implementation])
332  ;;
333*)
334  AC_MSG_ERROR([invalid field implementation])
335  ;;
336esac
337
338# select bignum implementation
339case $set_bignum in
340gmp)
341  AC_DEFINE(HAVE_LIBGMP, 1, [Define this symbol if libgmp is installed])
342  AC_DEFINE(USE_NUM_GMP, 1, [Define this symbol to use the gmp implementation for num])
343  AC_DEFINE(USE_FIELD_INV_NUM, 1, [Define this symbol to use the num-based field inverse implementation])
344  AC_DEFINE(USE_SCALAR_INV_NUM, 1, [Define this symbol to use the num-based scalar inverse implementation])
345  ;;
346no)
347  AC_DEFINE(USE_NUM_NONE, 1, [Define this symbol to use no num implementation])
348  AC_DEFINE(USE_FIELD_INV_BUILTIN, 1, [Define this symbol to use the native field inverse implementation])
349  AC_DEFINE(USE_SCALAR_INV_BUILTIN, 1, [Define this symbol to use the native scalar inverse implementation])
350  ;;
351*)
352  AC_MSG_ERROR([invalid bignum implementation])
353  ;;
354esac
355
356#select scalar implementation
357case $set_scalar in
35864bit)
359  AC_DEFINE(USE_SCALAR_4X64, 1, [Define this symbol to use the 4x64 scalar implementation])
360  ;;
36132bit)
362  AC_DEFINE(USE_SCALAR_8X32, 1, [Define this symbol to use the 8x32 scalar implementation])
363  ;;
364*)
365  AC_MSG_ERROR([invalid scalar implementation])
366  ;;
367esac
368
369if test x"$use_tests" = x"yes"; then
370  SECP_OPENSSL_CHECK
371  if test x"$has_openssl_ec" = x"yes"; then
372    if test x"$enable_openssl_tests" != x"no"; then
373      AC_DEFINE(ENABLE_OPENSSL_TESTS, 1, [Define this symbol if OpenSSL EC functions are available])
374      SECP_TEST_INCLUDES="$SSL_CFLAGS $CRYPTO_CFLAGS"
375      SECP_TEST_LIBS="$CRYPTO_LIBS"
376
377      case $host in
378      *mingw*)
379        SECP_TEST_LIBS="$SECP_TEST_LIBS -lgdi32"
380        ;;
381      esac
382    fi
383  else
384    if test x"$enable_openssl_tests" = x"yes"; then
385      AC_MSG_ERROR([OpenSSL tests requested but OpenSSL with EC support is not available])
386    fi
387  fi
388else
389  if test x"$enable_openssl_tests" = x"yes"; then
390    AC_MSG_ERROR([OpenSSL tests requested but tests are not enabled])
391  fi
392fi
393
394if test x"$use_jni" != x"no"; then
395  AX_JNI_INCLUDE_DIR
396  have_jni_dependencies=yes
397  if test x"$enable_module_ecdh" = x"no"; then
398    have_jni_dependencies=no
399  fi
400  if test "x$JNI_INCLUDE_DIRS" = "x"; then
401    have_jni_dependencies=no
402  fi
403  if test "x$have_jni_dependencies" = "xno"; then
404    if test x"$use_jni" = x"yes"; then
405      AC_MSG_ERROR([jni support explicitly requested but headers/dependencies were not found. Enable ECDH and try again.])
406    fi
407    AC_MSG_WARN([jni headers/dependencies not found. jni support disabled])
408    use_jni=no
409  else
410    use_jni=yes
411    for JNI_INCLUDE_DIR in $JNI_INCLUDE_DIRS; do
412      JNI_INCLUDES="$JNI_INCLUDES -I$JNI_INCLUDE_DIR"
413    done
414  fi
415fi
416
417if test x"$set_bignum" = x"gmp"; then
418  SECP_LIBS="$SECP_LIBS $GMP_LIBS"
419  SECP_INCLUDES="$SECP_INCLUDES $GMP_CPPFLAGS"
420fi
421
422if test x"$use_endomorphism" = x"yes"; then
423  AC_DEFINE(USE_ENDOMORPHISM, 1, [Define this symbol to use endomorphism optimization])
424fi
425
426if test x"$set_precomp" = x"yes"; then
427  AC_DEFINE(USE_ECMULT_STATIC_PRECOMPUTATION, 1, [Define this symbol to use a statically generated ecmult table])
428fi
429
430if test x"$enable_module_ecdh" = x"yes"; then
431  AC_DEFINE(ENABLE_MODULE_ECDH, 1, [Define this symbol to enable the ECDH module])
432fi
433
434if test x"$enable_module_recovery" = x"yes"; then
435  AC_DEFINE(ENABLE_MODULE_RECOVERY, 1, [Define this symbol to enable the ECDSA pubkey recovery module])
436fi
437
438AC_C_BIGENDIAN()
439
440if test x"$use_external_asm" = x"yes"; then
441  AC_DEFINE(USE_EXTERNAL_ASM, 1, [Define this symbol if an external (non-inline) assembly implementation is used])
442fi
443
444AC_MSG_NOTICE([Using static precomputation: $set_precomp])
445AC_MSG_NOTICE([Using assembly optimizations: $set_asm])
446AC_MSG_NOTICE([Using field implementation: $set_field])
447AC_MSG_NOTICE([Using bignum implementation: $set_bignum])
448AC_MSG_NOTICE([Using scalar implementation: $set_scalar])
449AC_MSG_NOTICE([Using endomorphism optimizations: $use_endomorphism])
450AC_MSG_NOTICE([Building for coverage analysis: $enable_coverage])
451AC_MSG_NOTICE([Building ECDH module: $enable_module_ecdh])
452AC_MSG_NOTICE([Building ECDSA pubkey recovery module: $enable_module_recovery])
453AC_MSG_NOTICE([Using jni: $use_jni])
454
455if test x"$enable_experimental" = x"yes"; then
456  AC_MSG_NOTICE([******])
457  AC_MSG_NOTICE([WARNING: experimental build])
458  AC_MSG_NOTICE([Experimental features do not have stable APIs or properties, and may not be safe for production use.])
459  AC_MSG_NOTICE([Building ECDH module: $enable_module_ecdh])
460  AC_MSG_NOTICE([******])
461else
462  if test x"$enable_module_ecdh" = x"yes"; then
463    AC_MSG_ERROR([ECDH module is experimental. Use --enable-experimental to allow.])
464  fi
465  if test x"$set_asm" = x"arm"; then
466    AC_MSG_ERROR([ARM assembly optimization is experimental. Use --enable-experimental to allow.])
467  fi
468fi
469
470AC_CONFIG_HEADERS([src/libsecp256k1-config.h])
471AC_CONFIG_FILES([Makefile libsecp256k1.pc])
472AC_SUBST(JNI_INCLUDES)
473AC_SUBST(SECP_INCLUDES)
474AC_SUBST(SECP_LIBS)
475AC_SUBST(SECP_TEST_LIBS)
476AC_SUBST(SECP_TEST_INCLUDES)
477AM_CONDITIONAL([ENABLE_COVERAGE], [test x"$enable_coverage" = x"yes"])
478AM_CONDITIONAL([USE_TESTS], [test x"$use_tests" != x"no"])
479AM_CONDITIONAL([USE_EXHAUSTIVE_TESTS], [test x"$use_exhaustive_tests" != x"no"])
480AM_CONDITIONAL([USE_BENCHMARK], [test x"$use_benchmark" = x"yes"])
481AM_CONDITIONAL([USE_ECMULT_STATIC_PRECOMPUTATION], [test x"$set_precomp" = x"yes"])
482AM_CONDITIONAL([ENABLE_MODULE_ECDH], [test x"$enable_module_ecdh" = x"yes"])
483AM_CONDITIONAL([ENABLE_MODULE_RECOVERY], [test x"$enable_module_recovery" = x"yes"])
484AM_CONDITIONAL([USE_JNI], [test x"$use_jni" == x"yes"])
485AM_CONDITIONAL([USE_EXTERNAL_ASM], [test x"$use_external_asm" = x"yes"])
486AM_CONDITIONAL([USE_ASM_ARM], [test x"$set_asm" = x"arm"])
487
488dnl make sure nothing new is exported so that we don't break the cache
489PKGCONFIG_PATH_TEMP="$PKG_CONFIG_PATH"
490unset PKG_CONFIG_PATH
491PKG_CONFIG_PATH="$PKGCONFIG_PATH_TEMP"
492
493AC_OUTPUT
494