Skip to content

Commit b16f3ec

Browse files
author
Lawrence Nahum
committed
from real-or-random #584
bitcoin-core/secp256k1#584 configure: Use CFLAGS_FOR_BUILD when checking native compiler This fixes a bug where configure would fail or disable static ecmult tables because it wrongly checks the native compiler using the target CFLAGS (instead of the native CFLAGS_FOR_BUILD). Moreover, this commit adds tests to figure out whether the native compiler supports the warning flags passed during the build, and it contains a few minor improvements to the code that checks the native compiler.
1 parent f8e68f6 commit b16f3ec

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

src/secp256k1/Makefile.am

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,14 @@ endif
152152

153153
if USE_ECMULT_STATIC_PRECOMPUTATION
154154
CPPFLAGS_FOR_BUILD +=-I$(top_srcdir)
155-
CFLAGS_FOR_BUILD += -Wall -Wextra -Wno-unused-function
156155

157156
gen_context_OBJECTS = gen_context.o
158157
gen_context_BIN = gen_context$(BUILD_EXEEXT)
159158
gen_%.o: src/gen_%.c
160159
$(CC_FOR_BUILD) $(CPPFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD) -c $< -o $@
161160

162161
$(gen_context_BIN): $(gen_context_OBJECTS)
163-
$(CC_FOR_BUILD) $^ -o $@
162+
$(CC_FOR_BUILD) $(CFLAGS_FOR_BUILD) $(LDFLAGS_FOR_BUILD) $^ -o $@
164163

165164
$(libsecp256k1_la_OBJECTS): src/ecmult_static_context.h
166165
$(tests_OBJECTS): src/ecmult_static_context.h

src/secp256k1/configure.ac

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,27 +219,54 @@ AC_COMPILE_IFELSE([AC_LANG_SOURCE([[void myfunc() {__builtin_popcount(0);}]])],
219219
])
220220

221221
if test x"$use_ecmult_static_precomputation" != x"no"; then
222+
# Temporarily switch to an environment for the native compiler
222223
save_cross_compiling=$cross_compiling
223224
cross_compiling=no
224-
TEMP_CC="$CC"
225+
SAVE_CC="$CC"
225226
CC="$CC_FOR_BUILD"
226-
AC_MSG_CHECKING([native compiler: ${CC_FOR_BUILD}])
227+
SAVE_CFLAGS="$CFLAGS"
228+
CFLAGS="$CFLAGS_FOR_BUILD"
229+
SAVE_CPPFLAGS="$CPPFLAGS"
230+
CPPFLAGS="$CPPFLAGS_FOR_BUILD"
231+
SAVE_LDFLAGS="$LDFLAGS"
232+
LDFLAGS="$LDFLAGS_FOR_BUILD"
233+
234+
warn_CFLAGS_FOR_BUILD="-Wall -Wextra -Wno-unused-function"
235+
saved_CFLAGS="$CFLAGS"
236+
CFLAGS="$CFLAGS $warn_CFLAGS_FOR_BUILD"
237+
AC_MSG_CHECKING([if native ${CC_FOR_BUILD} supports ${warn_CFLAGS_FOR_BUILD}])
238+
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
239+
[ AC_MSG_RESULT([yes]) ],
240+
[ AC_MSG_RESULT([no])
241+
CFLAGS="$saved_CFLAGS"
242+
])
243+
244+
AC_MSG_CHECKING([for working native compiler: ${CC_FOR_BUILD}])
227245
AC_RUN_IFELSE(
228-
[AC_LANG_PROGRAM([], [return 0])],
246+
[AC_LANG_PROGRAM([], [])],
229247
[working_native_cc=yes],
230248
[working_native_cc=no],[dnl])
231-
CC="$TEMP_CC"
249+
250+
CFLAGS_FOR_BUILD="$CFLAGS"
251+
252+
# Restore the environment
232253
cross_compiling=$save_cross_compiling
254+
CC="$SAVE_CC"
255+
CFLAGS="$SAVE_CFLAGS"
256+
CPPFLAGS="$SAVE_CPPFLAGS"
257+
LDFLAGS="$SAVE_LDFLAGS"
233258

234259
if test x"$working_native_cc" = x"no"; then
260+
AC_MSG_RESULT([no])
235261
set_precomp=no
262+
m4_define([please_set_for_build], [Please set CC_FOR_BUILD, CFLAGS_FOR_BUILD, CPPFLAGS_FOR_BUILD, and/or LDFLAGS_FOR_BUILD.])
236263
if test x"$use_ecmult_static_precomputation" = x"yes"; then
237-
AC_MSG_ERROR([${CC_FOR_BUILD} does not produce working binaries. Please set CC_FOR_BUILD])
264+
AC_MSG_ERROR([native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build])
238265
else
239-
AC_MSG_RESULT([${CC_FOR_BUILD} does not produce working binaries. Please set CC_FOR_BUILD])
266+
AC_MSG_WARN([Disabling statically generated ecmult table because the native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build])
240267
fi
241268
else
242-
AC_MSG_RESULT([ok])
269+
AC_MSG_RESULT([yes])
243270
set_precomp=yes
244271
fi
245272
else

0 commit comments

Comments
 (0)