Skip to content

Commit 17d00b2

Browse files
real-or-randomFabcien
authored andcommitted
[SECP256K1] Autoconf improvements
Summary: ``` See individual commit messages. These are improvements in preparation of the switch to Cirrus CI. (Maybe I'll just open a PR on top of this one.) The first commit made the difference between successful build https://cirrus-ci.com/task/6740575057608704 and unsuccessful build https://cirrus-ci.com/task/4909571074424832. I've tested the second commit without cross-compilation and with cross-compilation for android (#621 (comment)) When working on the autoconf stuff, I noticed two things that I just want to write down here: At some point we should update build-aux/m4/ax_prog_cc_for_build.m4. This is outdated, and there have been a lot of fixes But the latest version is broken, so now is probably not the time. The latest autoconf 2.70 deprecates AC_PROG_CC_C89. It's not needed anymore because AC_PROG_CC cares about testing for version support. This makes autoconf 2.70 output a warning that we should probably just ignore. We don't want to force users onto 2.70... ``` Backport of [[bitcoin-core/secp256k1#862 | secp256k1#862]] Test Plan: ./autogen.sh ./configure make I didn't test valgrind from brew on OSX, I have no old enough OSX that support valgrind. Reviewers: #bitcoin_abc, deadalnix Reviewed By: #bitcoin_abc, deadalnix Differential Revision: https://reviews.bitcoinabc.org/D9382
1 parent 49a9bd8 commit 17d00b2

File tree

3 files changed

+144
-80
lines changed

3 files changed

+144
-80
lines changed

build-aux/m4/bitcoin_secp.m4

+8
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,11 @@ if test x"$has_gmp" != x"yes"; then
8787
LIBS="$LIBS_TEMP"
8888
fi
8989
])
90+
91+
AC_DEFUN([SECP_VALGRIND_CHECK],[
92+
if test x"$has_valgrind" != x"yes"; then
93+
CPPFLAGS_TEMP="$CPPFLAGS"
94+
CPPFLAGS="$VALGRIND_CPPFLAGS $CPPFLAGS"
95+
AC_CHECK_HEADER([valgrind/memcheck.h], [has_valgrind=yes; AC_DEFINE(HAVE_VALGRIND,1,[Define this symbol if valgrind is installed])])
96+
fi
97+
])

configure.ac

+134-78
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ AM_INIT_AUTOMAKE([foreign subdir-objects])
1414
: ${CFLAGS="-g"}
1515
LT_INIT
1616

17-
dnl make the compilation flags quiet unless V=1 is used
17+
# Make the compilation flags quiet unless V=1 is used.
1818
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
1919

2020
PKG_PROG_PKG_CONFIG
2121

2222
AC_PATH_TOOL(AR, ar)
2323
AC_PATH_TOOL(RANLIB, ranlib)
2424
AC_PATH_TOOL(STRIP, strip)
25-
AX_PROG_CC_FOR_BUILD
2625

2726
AM_PROG_CC_C_O
2827

@@ -37,12 +36,12 @@ case $host_os in
3736
if test x$cross_compiling != xyes; then
3837
AC_PATH_PROG([BREW],brew,)
3938
if test x$BREW != x; then
40-
dnl These Homebrew packages may be keg-only, meaning that they won't be found
41-
dnl in expected paths because they may conflict with system files. Ask
42-
dnl Homebrew where each one is located, then adjust paths accordingly.
43-
39+
# These Homebrew packages may be keg-only, meaning that they won't be found
40+
# in expected paths because they may conflict with system files. Ask
41+
# Homebrew where each one is located, then adjust paths accordingly.
4442
openssl_prefix=`$BREW --prefix openssl 2>/dev/null`
4543
gmp_prefix=`$BREW --prefix gmp 2>/dev/null`
44+
valgrind_prefix=`$BREW --prefix valgrind 2>/dev/null`
4645
if test x$openssl_prefix != x; then
4746
PKG_CONFIG_PATH="$openssl_prefix/lib/pkgconfig:$PKG_CONFIG_PATH"
4847
export PKG_CONFIG_PATH
@@ -52,10 +51,13 @@ case $host_os in
5251
GMP_CPPFLAGS="-I$gmp_prefix/include"
5352
GMP_LIBS="-L$gmp_prefix/lib"
5453
fi
54+
if test x$valgrind_prefix != x; then
55+
VALGRIND_CPPFLAGS="-I$valgrind_prefix/include"
56+
fi
5557
else
5658
AC_PATH_PROG([PORT],port,)
57-
dnl if homebrew isn't installed and macports is, add the macports default paths
58-
dnl as a last resort.
59+
# If homebrew isn't installed and macports is, add the macports default paths
60+
# as a last resort.
5961
if test x$PORT != x; then
6062
CPPFLAGS="$CPPFLAGS -isystem /opt/local/include"
6163
LDFLAGS="$LDFLAGS -L/opt/local/lib"
@@ -86,6 +88,10 @@ AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
8688
CFLAGS="$saved_CFLAGS"
8789
])
8890

91+
###
92+
### Define config arguments
93+
###
94+
8995
AC_ARG_ENABLE(benchmark,
9096
AS_HELP_STRING([--enable-benchmark],[compile benchmark [default=yes]]),
9197
[use_benchmark=$enableval],
@@ -156,8 +162,8 @@ AC_ARG_ENABLE(external_default_callbacks,
156162
[use_external_default_callbacks=$enableval],
157163
[use_external_default_callbacks=no])
158164

159-
dnl Test-only override of the (autodetected by the C code) "widemul" setting.
160-
dnl Legal values are int64 (for [u]int64_t), int128 (for [unsigned] __int128), and auto (the default).
165+
# Test-only override of the (autodetected by the C code) "widemul" setting.
166+
# Legal values are int64 (for [u]int64_t), int128 (for [unsigned] __int128), and auto (the default).
161167
AC_ARG_WITH([test-override-wide-multiply], [] ,[set_widemul=$withval], [set_widemul=auto])
162168

163169
AC_ARG_ENABLE(jni,
@@ -192,15 +198,22 @@ AC_ARG_WITH([valgrind], [AS_HELP_STRING([--with-valgrind=yes|no|auto],
192198
)],
193199
[req_valgrind=$withval], [req_valgrind=auto])
194200

201+
###
202+
### Handle config options (except for modules)
203+
###
204+
195205
if test x"$req_valgrind" = x"no"; then
196206
enable_valgrind=no
197207
else
198-
AC_CHECK_HEADER([valgrind/memcheck.h], [enable_valgrind=yes], [
208+
SECP_VALGRIND_CHECK
209+
if test x"$has_valgrind" != x"yes"; then
199210
if test x"$req_valgrind" = x"yes"; then
200211
AC_MSG_ERROR([Valgrind support explicitly requested but valgrind/memcheck.h header not available])
201212
fi
202213
enable_valgrind=no
203-
], [])
214+
else
215+
enable_valgrind=yes
216+
fi
204217
fi
205218
AM_CONDITIONAL([VALGRIND_ENABLED],[test "$enable_valgrind" = "yes"])
206219

@@ -212,61 +225,6 @@ else
212225
CFLAGS="-O2 $CFLAGS"
213226
fi
214227

215-
if test x"$use_ecmult_static_precomputation" != x"no"; then
216-
# Temporarily switch to an environment for the native compiler
217-
save_cross_compiling=$cross_compiling
218-
cross_compiling=no
219-
SAVE_CC="$CC"
220-
CC="$CC_FOR_BUILD"
221-
SAVE_CFLAGS="$CFLAGS"
222-
CFLAGS="$CFLAGS_FOR_BUILD"
223-
SAVE_CPPFLAGS="$CPPFLAGS"
224-
CPPFLAGS="$CPPFLAGS_FOR_BUILD"
225-
SAVE_LDFLAGS="$LDFLAGS"
226-
LDFLAGS="$LDFLAGS_FOR_BUILD"
227-
228-
warn_CFLAGS_FOR_BUILD="-Wall -Wextra -Wno-unused-function"
229-
saved_CFLAGS="$CFLAGS"
230-
CFLAGS="$warn_CFLAGS_FOR_BUILD $CFLAGS"
231-
AC_MSG_CHECKING([if native ${CC_FOR_BUILD} supports ${warn_CFLAGS_FOR_BUILD}])
232-
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
233-
[ AC_MSG_RESULT([yes]) ],
234-
[ AC_MSG_RESULT([no])
235-
CFLAGS="$saved_CFLAGS"
236-
])
237-
238-
AC_MSG_CHECKING([for working native compiler: ${CC_FOR_BUILD}])
239-
AC_RUN_IFELSE(
240-
[AC_LANG_PROGRAM([], [])],
241-
[working_native_cc=yes],
242-
[working_native_cc=no],[:])
243-
244-
CFLAGS_FOR_BUILD="$CFLAGS"
245-
246-
# Restore the environment
247-
cross_compiling=$save_cross_compiling
248-
CC="$SAVE_CC"
249-
CFLAGS="$SAVE_CFLAGS"
250-
CPPFLAGS="$SAVE_CPPFLAGS"
251-
LDFLAGS="$SAVE_LDFLAGS"
252-
253-
if test x"$working_native_cc" = x"no"; then
254-
AC_MSG_RESULT([no])
255-
set_precomp=no
256-
m4_define([please_set_for_build], [Please set CC_FOR_BUILD, CFLAGS_FOR_BUILD, CPPFLAGS_FOR_BUILD, and/or LDFLAGS_FOR_BUILD.])
257-
if test x"$use_ecmult_static_precomputation" = x"yes"; then
258-
AC_MSG_ERROR([native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build])
259-
else
260-
AC_MSG_WARN([Disabling statically generated ecmult table because the native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build])
261-
fi
262-
else
263-
AC_MSG_RESULT([yes])
264-
set_precomp=yes
265-
fi
266-
else
267-
set_precomp=no
268-
fi
269-
270228
if test x"$req_asm" = x"auto"; then
271229
SECP_64BIT_ASM_CHECK
272230
if test x"$has_64bit_asm" = x"yes"; then
@@ -320,7 +278,7 @@ else
320278
esac
321279
fi
322280

323-
# select assembly optimization
281+
# Select assembly optimization
324282
use_external_asm=no
325283

326284
case $set_asm in
@@ -337,7 +295,12 @@ no)
337295
;;
338296
esac
339297

340-
# select wide multiplication implementation
298+
if test x"$use_external_asm" = x"yes"; then
299+
AC_DEFINE(USE_EXTERNAL_ASM, 1, [Define this symbol if an external (non-inline) assembly implementation is used])
300+
fi
301+
302+
303+
# Select wide multiplication implementation
341304
case $set_widemul in
342305
int128)
343306
AC_DEFINE(USE_FORCE_WIDEMUL_INT128, 1, [Define this symbol to force the use of the (unsigned) __int128 based wide multiplication implementation])
@@ -352,7 +315,7 @@ auto)
352315
;;
353316
esac
354317

355-
# select bignum implementation
318+
# Select bignum implementation
356319
case $set_bignum in
357320
gmp)
358321
AC_DEFINE(HAVE_LIBGMP, 1, [Define this symbol if libgmp is installed])
@@ -370,7 +333,7 @@ no)
370333
;;
371334
esac
372335

373-
#set ecmult window size
336+
# Set ecmult window size
374337
if test x"$req_ecmult_window" = x"auto"; then
375338
set_ecmult_window=15
376339
else
@@ -392,7 +355,7 @@ case $set_ecmult_window in
392355
;;
393356
esac
394357

395-
#set ecmult gen precision
358+
# Set ecmult gen precision
396359
if test x"$req_ecmult_gen_precision" = x"auto"; then
397360
set_ecmult_gen_precision=4
398361
else
@@ -462,10 +425,93 @@ if test x"$set_bignum" = x"gmp"; then
462425
SECP_INCLUDES="$SECP_INCLUDES $GMP_CPPFLAGS"
463426
fi
464427

428+
if test x"$enable_valgrind" = x"yes"; then
429+
SECP_INCLUDES="$SECP_INCLUDES $VALGRIND_CPPFLAGS"
430+
fi
431+
432+
# Handle static precomputation (after everything which modifies CFLAGS and friends)
433+
if test x"$use_ecmult_static_precomputation" != x"no"; then
434+
if test x"$cross_compiling" = x"no"; then
435+
set_precomp=yes
436+
if test x"${CC_FOR_BUILD+x}${CFLAGS_FOR_BUILD+x}${CPPFLAGS_FOR_BUILD+x}${LDFLAGS_FOR_BUILD+x}" != x; then
437+
AC_MSG_WARN([CC_FOR_BUILD, CFLAGS_FOR_BUILD, CPPFLAGS_FOR_BUILD, and/or LDFLAGS_FOR_BUILD is set but ignored because we are not cross-compiling.])
438+
fi
439+
# If we're not cross-compiling, simply use the same compiler for building the static precompation code.
440+
CC_FOR_BUILD="$CC"
441+
CFLAGS_FOR_BUILD="$CFLAGS"
442+
CPPFLAGS_FOR_BUILD="$CPPFLAGS"
443+
LDFLAGS_FOR_BUILD="$LDFLAGS"
444+
else
445+
AX_PROG_CC_FOR_BUILD
446+
447+
# Temporarily switch to an environment for the native compiler
448+
save_cross_compiling=$cross_compiling
449+
cross_compiling=no
450+
SAVE_CC="$CC"
451+
CC="$CC_FOR_BUILD"
452+
SAVE_CFLAGS="$CFLAGS"
453+
CFLAGS="$CFLAGS_FOR_BUILD"
454+
SAVE_CPPFLAGS="$CPPFLAGS"
455+
CPPFLAGS="$CPPFLAGS_FOR_BUILD"
456+
SAVE_LDFLAGS="$LDFLAGS"
457+
LDFLAGS="$LDFLAGS_FOR_BUILD"
458+
459+
warn_CFLAGS_FOR_BUILD="-Wall -Wextra -Wno-unused-function"
460+
saved_CFLAGS="$CFLAGS"
461+
CFLAGS="$warn_CFLAGS_FOR_BUILD $CFLAGS"
462+
AC_MSG_CHECKING([if native ${CC_FOR_BUILD} supports ${warn_CFLAGS_FOR_BUILD}])
463+
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
464+
[ AC_MSG_RESULT([yes]) ],
465+
[ AC_MSG_RESULT([no])
466+
CFLAGS="$saved_CFLAGS"
467+
])
468+
469+
AC_MSG_CHECKING([for working native compiler: ${CC_FOR_BUILD}])
470+
AC_RUN_IFELSE(
471+
[AC_LANG_PROGRAM([], [])],
472+
[working_native_cc=yes],
473+
[working_native_cc=no],[:])
474+
475+
CFLAGS_FOR_BUILD="$CFLAGS"
476+
477+
# Restore the environment
478+
cross_compiling=$save_cross_compiling
479+
CC="$SAVE_CC"
480+
CFLAGS="$SAVE_CFLAGS"
481+
CPPFLAGS="$SAVE_CPPFLAGS"
482+
LDFLAGS="$SAVE_LDFLAGS"
483+
484+
if test x"$working_native_cc" = x"no"; then
485+
AC_MSG_RESULT([no])
486+
set_precomp=no
487+
m4_define([please_set_for_build], [Please set CC_FOR_BUILD, CFLAGS_FOR_BUILD, CPPFLAGS_FOR_BUILD, and/or LDFLAGS_FOR_BUILD.])
488+
if test x"$use_ecmult_static_precomputation" = x"yes"; then
489+
AC_MSG_ERROR([native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build])
490+
else
491+
AC_MSG_WARN([Disabling statically generated ecmult table because the native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build])
492+
fi
493+
else
494+
AC_MSG_RESULT([yes])
495+
set_precomp=yes
496+
fi
497+
fi
498+
499+
AC_SUBST(CC_FOR_BUILD)
500+
AC_SUBST(CFLAGS_FOR_BUILD)
501+
AC_SUBST(CPPFLAGS_FOR_BUILD)
502+
AC_SUBST(LDFLAGS_FOR_BUILD)
503+
else
504+
set_precomp=no
505+
fi
506+
465507
if test x"$set_precomp" = x"yes"; then
466508
AC_DEFINE(USE_ECMULT_STATIC_PRECOMPUTATION, 1, [Define this symbol to use a statically generated ecmult table])
467509
fi
468510

511+
###
512+
### Handle module options
513+
###
514+
469515
if test x"$enable_module_ecdh" = x"yes"; then
470516
AC_DEFINE(ENABLE_MODULE_ECDH, 1, [Define this symbol to enable the ECDH module])
471517
fi
@@ -493,14 +539,14 @@ if test x"$enable_module_extrakeys" = x"yes"; then
493539
AC_DEFINE(ENABLE_MODULE_EXTRAKEYS, 1, [Define this symbol to enable the extrakeys module])
494540
fi
495541

496-
if test x"$use_external_asm" = x"yes"; then
497-
AC_DEFINE(USE_EXTERNAL_ASM, 1, [Define this symbol if an external (non-inline) assembly implementation is used])
498-
fi
499-
500542
if test x"$use_external_default_callbacks" = x"yes"; then
501543
AC_DEFINE(USE_EXTERNAL_DEFAULT_CALLBACKS, 1, [Define this symbol if an external implementation of the default callbacks is used])
502544
fi
503545

546+
###
547+
### Check for --enable-experimental if necessary
548+
###
549+
504550
if test x"$enable_experimental" = x"yes"; then
505551
AC_MSG_NOTICE([******])
506552
AC_MSG_NOTICE([WARNING: experimental build])
@@ -523,6 +569,10 @@ else
523569
fi
524570
fi
525571

572+
###
573+
### Generate output
574+
###
575+
526576
AC_CONFIG_HEADERS([src/libsecp256k1-config.h])
527577
AC_CONFIG_FILES([Makefile libsecp256k1.pc])
528578
AC_SUBST(JNI_INCLUDES)
@@ -545,7 +595,7 @@ AM_CONDITIONAL([USE_JNI], [test x"$use_jni" = x"yes"])
545595
AM_CONDITIONAL([USE_EXTERNAL_ASM], [test x"$use_external_asm" = x"yes"])
546596
AM_CONDITIONAL([USE_ASM_ARM], [test x"$set_asm" = x"arm"])
547597

548-
dnl make sure nothing new is exported so that we don't break the cache
598+
# Make sure nothing new is exported so that we don't break the cache.
549599
PKGCONFIG_PATH_TEMP="$PKG_CONFIG_PATH"
550600
unset PKG_CONFIG_PATH
551601
PKG_CONFIG_PATH="$PKGCONFIG_PATH_TEMP"
@@ -572,7 +622,7 @@ echo " asm = $set_asm"
572622
echo " bignum = $set_bignum"
573623
echo " ecmult window size = $set_ecmult_window"
574624
echo " ecmult gen prec. bits = $set_ecmult_gen_precision"
575-
dnl Hide test-only options unless they're used.
625+
# Hide test-only options unless they're used.
576626
if test x"$set_widemul" != xauto; then
577627
echo " wide multiplication = $set_widemul"
578628
fi
@@ -583,3 +633,9 @@ echo " CFLAGS = $CFLAGS"
583633
echo " CPPFLAGS = $CPPFLAGS"
584634
echo " LDFLAGS = $LDFLAGS"
585635
echo
636+
if test x"$set_precomp" = x"yes"; then
637+
echo " CC_FOR_BUILD = $CC_FOR_BUILD"
638+
echo " CFLAGS_FOR_BUILD = $CFLAGS_FOR_BUILD"
639+
echo " CPPFLAGS_FOR_BUILD = $CPPFLAGS_FOR_BUILD"
640+
echo " LDFLAGS_FOR_BUILD = $LDFLAGS_FOR_BUILD"
641+
fi

src/gen_context.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
55
***********************************************************************/
66

7-
/* Autotools creates libsecp256k1-config.h, of which ECMULT_GEN_PREC_BITS is needed. */
8-
/* ifndef guard so downstream users can define their own if they do not use autotools. */
7+
/* Autotools creates libsecp256k1-config.h, of which ECMULT_GEN_PREC_BITS is needed.
8+
ifndef guard so downstream users can define their own if they do not use autotools. */
99
#if !defined(ECMULT_GEN_PREC_BITS)
1010
#include "libsecp256k1-config.h"
1111
#endif

0 commit comments

Comments
 (0)