Skip to content

Commit 5cac98b

Browse files
Improve CC_FOR_BUILD detection
This commits simply uses CC as CC_FOR_BUILD and the same for corresponding flags if we're not cross-compiling. This has a number of benefits in this common case: - It avoids strange cases where very old compilers are used (#768). - Flags are consistently set for CC and CC_FOR_BUILD. - ./configure is faster. - You get compiler x consistently if you set CC=x; we got this wrong in CI in the past. ./configure warns if a _FOR_BUILD variable is set but ignored because we're not cross-compiling. The change exposed that //-style comments are used in gen_context.c, which is also fixed by this commit. This commit also reorganizes code in configure.ac to have a cleaner separation of sections.
1 parent aeb33fb commit 5cac98b

File tree

2 files changed

+98
-62
lines changed

2 files changed

+98
-62
lines changed

configure.ac

+96-60
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ PKG_PROG_PKG_CONFIG
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

@@ -89,6 +88,10 @@ AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
8988
CFLAGS="$saved_CFLAGS"
9089
])
9190

91+
###
92+
### Parse config arguments
93+
###
94+
9295
AC_ARG_ENABLE(benchmark,
9396
AS_HELP_STRING([--enable-benchmark],[compile benchmark [default=yes]]),
9497
[use_benchmark=$enableval],
@@ -180,6 +183,10 @@ AC_ARG_WITH([valgrind], [AS_HELP_STRING([--with-valgrind=yes|no|auto],
180183
)],
181184
[req_valgrind=$withval], [req_valgrind=auto])
182185

186+
###
187+
### Handle config options
188+
###
189+
183190
if test x"$req_valgrind" = x"no"; then
184191
enable_valgrind=no
185192
else
@@ -203,61 +210,6 @@ else
203210
CFLAGS="-O2 $CFLAGS"
204211
fi
205212

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

266+
# This should come after all tests which modify CFLAGS and friends.
267+
if test x"$use_ecmult_static_precomputation" != x"no"; then
268+
if test x"$cross_compiling" = x"no"; then
269+
set_precomp=yes
270+
# If we're not cross-compiling, simply use the same compiler for building the static precompation code.
271+
CC_FOR_BUILD="$CC"
272+
CFLAGS_FOR_BUILD="$CFLAGS"
273+
CPPFLAGS_FOR_BUILD="$CPPFLAGS"
274+
LDFLAGS_FOR_BUILD="$LDFLAGS"
275+
if test x"${CC_FOR_BUILD+x}${CFLAGS_FOR_BUILD+x}${CPPFLAGS_FOR_BUILD+x}${LDFLAGS_FOR_BUILD+x}" != xxxxx; then
276+
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.])
277+
fi
278+
else
279+
AX_PROG_CC_FOR_BUILD
280+
281+
# Temporarily switch to an environment for the native compiler
282+
save_cross_compiling=$cross_compiling
283+
cross_compiling=no
284+
SAVE_CC="$CC"
285+
CC="$CC_FOR_BUILD"
286+
SAVE_CFLAGS="$CFLAGS"
287+
CFLAGS="$CFLAGS_FOR_BUILD"
288+
SAVE_CPPFLAGS="$CPPFLAGS"
289+
CPPFLAGS="$CPPFLAGS_FOR_BUILD"
290+
SAVE_LDFLAGS="$LDFLAGS"
291+
LDFLAGS="$LDFLAGS_FOR_BUILD"
292+
293+
warn_CFLAGS_FOR_BUILD="-Wall -Wextra -Wno-unused-function"
294+
saved_CFLAGS="$CFLAGS"
295+
CFLAGS="$warn_CFLAGS_FOR_BUILD $CFLAGS"
296+
AC_MSG_CHECKING([if native ${CC_FOR_BUILD} supports ${warn_CFLAGS_FOR_BUILD}])
297+
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
298+
[ AC_MSG_RESULT([yes]) ],
299+
[ AC_MSG_RESULT([no])
300+
CFLAGS="$saved_CFLAGS"
301+
])
302+
303+
AC_MSG_CHECKING([for working native compiler: ${CC_FOR_BUILD}])
304+
AC_RUN_IFELSE(
305+
[AC_LANG_PROGRAM([], [])],
306+
[working_native_cc=yes],
307+
[working_native_cc=no],[:])
308+
309+
# Restore the environment
310+
cross_compiling=$save_cross_compiling
311+
CC="$SAVE_CC"
312+
CFLAGS="$SAVE_CFLAGS"
313+
CPPFLAGS="$SAVE_CPPFLAGS"
314+
LDFLAGS="$SAVE_LDFLAGS"
315+
316+
if test x"$working_native_cc" = x"no"; then
317+
AC_MSG_RESULT([no])
318+
set_precomp=no
319+
m4_define([please_set_for_build], [Please set CC_FOR_BUILD, CFLAGS_FOR_BUILD, CPPFLAGS_FOR_BUILD, and/or LDFLAGS_FOR_BUILD.])
320+
if test x"$use_ecmult_static_precomputation" = x"yes"; then
321+
AC_MSG_ERROR([native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build])
322+
else
323+
AC_MSG_WARN([Disabling statically generated ecmult table because the native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build])
324+
fi
325+
else
326+
AC_MSG_RESULT([yes])
327+
set_precomp=yes
328+
fi
329+
fi
330+
331+
AC_SUBST(CC_FOR_BUILD)
332+
AC_SUBST(CFLAGS_FOR_BUILD)
333+
AC_SUBST(CPPFLAGS_FOR_BUILD)
334+
AC_SUBST(LDFLAGS_FOR_BUILD)
335+
else
336+
set_precomp=no
337+
fi
338+
314339
# select assembly optimization
315340
use_external_asm=no
316341

@@ -328,6 +353,11 @@ no)
328353
;;
329354
esac
330355

356+
if test x"$use_external_asm" = x"yes"; then
357+
AC_DEFINE(USE_EXTERNAL_ASM, 1, [Define this symbol if an external (non-inline) assembly implementation is used])
358+
fi
359+
360+
331361
# select wide multiplication implementation
332362
case $set_widemul in
333363
int128)
@@ -457,10 +487,6 @@ if test x"$enable_module_extrakeys" = x"yes"; then
457487
AC_DEFINE(ENABLE_MODULE_EXTRAKEYS, 1, [Define this symbol to enable the extrakeys module])
458488
fi
459489

460-
if test x"$use_external_asm" = x"yes"; then
461-
AC_DEFINE(USE_EXTERNAL_ASM, 1, [Define this symbol if an external (non-inline) assembly implementation is used])
462-
fi
463-
464490
if test x"$use_external_default_callbacks" = x"yes"; then
465491
AC_DEFINE(USE_EXTERNAL_DEFAULT_CALLBACKS, 1, [Define this symbol if an external implementation of the default callbacks is used])
466492
fi
@@ -484,6 +510,10 @@ else
484510
fi
485511
fi
486512

513+
###
514+
### Generate output
515+
###
516+
487517
AC_CONFIG_HEADERS([src/libsecp256k1-config.h])
488518
AC_CONFIG_FILES([Makefile libsecp256k1.pc])
489519
AC_SUBST(SECP_INCLUDES)
@@ -536,4 +566,10 @@ echo " CC = $CC"
536566
echo " CFLAGS = $CFLAGS"
537567
echo " CPPFLAGS = $CPPFLAGS"
538568
echo " LDFLAGS = $LDFLAGS"
569+
if test x"$cross_compiling" = x"yes"; then
570+
echo " CC_FOR_BUILD = $CC_FOR_BUILD"
571+
echo " CFLAGS_FOR_BUILD = $CFLAGS_FOR_BUILD"
572+
echo " CPPFLAGS_FOR_BUILD = $CPPFLAGS_FOR_BUILD"
573+
echo " LDFLAGS_FOR_BUILD = $LDFLAGS_FOR_BUILD"
574+
fi
539575
echo

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)