Skip to content

Commit 3c15130

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 47802a4 commit 3c15130

File tree

2 files changed

+74
-50
lines changed

2 files changed

+74
-50
lines changed

configure.ac

+72-48
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

@@ -394,56 +393,75 @@ fi
394393

395394
# Handle static precomputation (after everything which modifies CFLAGS and friends)
396395
if test x"$use_ecmult_static_precomputation" != x"no"; then
397-
# Temporarily switch to an environment for the native compiler
398-
save_cross_compiling=$cross_compiling
399-
cross_compiling=no
400-
SAVE_CC="$CC"
401-
CC="$CC_FOR_BUILD"
402-
SAVE_CFLAGS="$CFLAGS"
403-
CFLAGS="$CFLAGS_FOR_BUILD"
404-
SAVE_CPPFLAGS="$CPPFLAGS"
405-
CPPFLAGS="$CPPFLAGS_FOR_BUILD"
406-
SAVE_LDFLAGS="$LDFLAGS"
407-
LDFLAGS="$LDFLAGS_FOR_BUILD"
408-
409-
warn_CFLAGS_FOR_BUILD="-Wall -Wextra -Wno-unused-function"
410-
saved_CFLAGS="$CFLAGS"
411-
CFLAGS="$warn_CFLAGS_FOR_BUILD $CFLAGS"
412-
AC_MSG_CHECKING([if native ${CC_FOR_BUILD} supports ${warn_CFLAGS_FOR_BUILD}])
413-
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
414-
[ AC_MSG_RESULT([yes]) ],
415-
[ AC_MSG_RESULT([no])
416-
CFLAGS="$saved_CFLAGS"
417-
])
418-
419-
AC_MSG_CHECKING([for working native compiler: ${CC_FOR_BUILD}])
420-
AC_RUN_IFELSE(
421-
[AC_LANG_PROGRAM([], [])],
422-
[working_native_cc=yes],
423-
[working_native_cc=no],[:])
424-
425-
CFLAGS_FOR_BUILD="$CFLAGS"
426-
427-
# Restore the environment
428-
cross_compiling=$save_cross_compiling
429-
CC="$SAVE_CC"
430-
CFLAGS="$SAVE_CFLAGS"
431-
CPPFLAGS="$SAVE_CPPFLAGS"
432-
LDFLAGS="$SAVE_LDFLAGS"
433-
434-
if test x"$working_native_cc" = x"no"; then
435-
AC_MSG_RESULT([no])
436-
set_precomp=no
437-
m4_define([please_set_for_build], [Please set CC_FOR_BUILD, CFLAGS_FOR_BUILD, CPPFLAGS_FOR_BUILD, and/or LDFLAGS_FOR_BUILD.])
438-
if test x"$use_ecmult_static_precomputation" = x"yes"; then
439-
AC_MSG_ERROR([native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build])
440-
else
441-
AC_MSG_WARN([Disabling statically generated ecmult table because the native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build])
396+
if test x"$cross_compiling" = x"no"; then
397+
set_precomp=yes
398+
if test x"${CC_FOR_BUILD+x}${CFLAGS_FOR_BUILD+x}${CPPFLAGS_FOR_BUILD+x}${LDFLAGS_FOR_BUILD+x}" != x; then
399+
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.])
442400
fi
401+
# If we're not cross-compiling, simply use the same compiler for building the static precompation code.
402+
CC_FOR_BUILD="$CC"
403+
CFLAGS_FOR_BUILD="$CFLAGS"
404+
CPPFLAGS_FOR_BUILD="$CPPFLAGS"
405+
LDFLAGS_FOR_BUILD="$LDFLAGS"
443406
else
444-
AC_MSG_RESULT([yes])
445-
set_precomp=yes
407+
AX_PROG_CC_FOR_BUILD
408+
409+
# Temporarily switch to an environment for the native compiler
410+
save_cross_compiling=$cross_compiling
411+
cross_compiling=no
412+
SAVE_CC="$CC"
413+
CC="$CC_FOR_BUILD"
414+
SAVE_CFLAGS="$CFLAGS"
415+
CFLAGS="$CFLAGS_FOR_BUILD"
416+
SAVE_CPPFLAGS="$CPPFLAGS"
417+
CPPFLAGS="$CPPFLAGS_FOR_BUILD"
418+
SAVE_LDFLAGS="$LDFLAGS"
419+
LDFLAGS="$LDFLAGS_FOR_BUILD"
420+
421+
warn_CFLAGS_FOR_BUILD="-Wall -Wextra -Wno-unused-function"
422+
saved_CFLAGS="$CFLAGS"
423+
CFLAGS="$warn_CFLAGS_FOR_BUILD $CFLAGS"
424+
AC_MSG_CHECKING([if native ${CC_FOR_BUILD} supports ${warn_CFLAGS_FOR_BUILD}])
425+
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
426+
[ AC_MSG_RESULT([yes]) ],
427+
[ AC_MSG_RESULT([no])
428+
CFLAGS="$saved_CFLAGS"
429+
])
430+
431+
AC_MSG_CHECKING([for working native compiler: ${CC_FOR_BUILD}])
432+
AC_RUN_IFELSE(
433+
[AC_LANG_PROGRAM([], [])],
434+
[working_native_cc=yes],
435+
[working_native_cc=no],[:])
436+
437+
CFLAGS_FOR_BUILD="$CFLAGS"
438+
439+
# Restore the environment
440+
cross_compiling=$save_cross_compiling
441+
CC="$SAVE_CC"
442+
CFLAGS="$SAVE_CFLAGS"
443+
CPPFLAGS="$SAVE_CPPFLAGS"
444+
LDFLAGS="$SAVE_LDFLAGS"
445+
446+
if test x"$working_native_cc" = x"no"; then
447+
AC_MSG_RESULT([no])
448+
set_precomp=no
449+
m4_define([please_set_for_build], [Please set CC_FOR_BUILD, CFLAGS_FOR_BUILD, CPPFLAGS_FOR_BUILD, and/or LDFLAGS_FOR_BUILD.])
450+
if test x"$use_ecmult_static_precomputation" = x"yes"; then
451+
AC_MSG_ERROR([native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build])
452+
else
453+
AC_MSG_WARN([Disabling statically generated ecmult table because the native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build])
454+
fi
455+
else
456+
AC_MSG_RESULT([yes])
457+
set_precomp=yes
458+
fi
446459
fi
460+
461+
AC_SUBST(CC_FOR_BUILD)
462+
AC_SUBST(CFLAGS_FOR_BUILD)
463+
AC_SUBST(CPPFLAGS_FOR_BUILD)
464+
AC_SUBST(LDFLAGS_FOR_BUILD)
447465
else
448466
set_precomp=no
449467
fi
@@ -559,3 +577,9 @@ echo " CFLAGS = $CFLAGS"
559577
echo " CPPFLAGS = $CPPFLAGS"
560578
echo " LDFLAGS = $LDFLAGS"
561579
echo
580+
if test x"$set_precomp" = x"yes"; then
581+
echo " CC_FOR_BUILD = $CC_FOR_BUILD"
582+
echo " CFLAGS_FOR_BUILD = $CFLAGS_FOR_BUILD"
583+
echo " CPPFLAGS_FOR_BUILD = $CPPFLAGS_FOR_BUILD"
584+
echo " LDFLAGS_FOR_BUILD = $LDFLAGS_FOR_BUILD"
585+
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)