@@ -8,10 +8,6 @@ AH_TOP([#define LIBSECP256K1_CONFIG_H])
88AH_BOTTOM ( [ #endif /*LIBSECP256K1_CONFIG_H*/] )
99AM_INIT_AUTOMAKE ( [ foreign subdir-objects] )
1010
11- # Set -g if CFLAGS are not already set, which matches the default autoconf
12- # behavior (see PROG_CC in the Autoconf manual) with the exception that we don't
13- # set -O2 here because we set it in any case (see further down).
14- : ${CFLAGS="-g"}
1511LT_INIT
1612
1713# Make the compilation flags quiet unless V=1 is used.
@@ -70,35 +66,41 @@ case $host_os in
7066 ;;
7167esac
7268
73- CFLAGS="-W $CFLAGS"
74-
75- warn_CFLAGS="-std=c89 -pedantic -Wall -Wextra -Wcast-align -Wnested-externs -Wshadow -Wstrict-prototypes -Wundef -Wno-unused-function -Wno-long-long -Wno-overlength-strings"
76- saved_CFLAGS="$CFLAGS"
77- CFLAGS="$warn_CFLAGS $CFLAGS"
78- AC_MSG_CHECKING ( [ if ${CC} supports ${warn_CFLAGS}] )
79- AC_COMPILE_IFELSE ( [ AC_LANG_SOURCE ( [ [ char foo;] ] ) ] ,
80- [ AC_MSG_RESULT ( [ yes] ) ] ,
81- [ AC_MSG_RESULT ( [ no] )
82- CFLAGS="$saved_CFLAGS"
83- ] )
84-
85- saved_CFLAGS="$CFLAGS"
86- CFLAGS="-Wconditional-uninitialized $CFLAGS"
87- AC_MSG_CHECKING ( [ if ${CC} supports -Wconditional-uninitialized] )
88- AC_COMPILE_IFELSE ( [ AC_LANG_SOURCE ( [ [ char foo;] ] ) ] ,
89- [ AC_MSG_RESULT ( [ yes] ) ] ,
90- [ AC_MSG_RESULT ( [ no] )
91- CFLAGS="$saved_CFLAGS"
92- ] )
93-
94- saved_CFLAGS="$CFLAGS"
95- CFLAGS="-fvisibility=hidden $CFLAGS"
96- AC_MSG_CHECKING ( [ if ${CC} supports -fvisibility=hidden] )
97- AC_COMPILE_IFELSE ( [ AC_LANG_SOURCE ( [ [ char foo;] ] ) ] ,
98- [ AC_MSG_RESULT ( [ yes] ) ] ,
99- [ AC_MSG_RESULT ( [ no] )
100- CFLAGS="$saved_CFLAGS"
101- ] )
69+ # Try if some desirable compiler flags are supported and append them to SECP_CFLAGS.
70+ #
71+ # These are our own flags, so we append them to our own SECP_CFLAGS variable (instead of CFLAGS) as
72+ # recommended in the automake manual (Section "Flag Variables Ordering"). CFLAGS belongs to the user
73+ # and we are not supposed to touch it. In the Makefile, we will need to ensure that SECP_CFLAGS
74+ # is prepended to CFLAGS when invoking the compiler so that the user always has the last word (flag).
75+ #
76+ # Another advantage of not touching CFLAGS is that the contents of CFLAGS will be picked up by
77+ # libtool for compiling helper executables. For example, when compiling for Windows, libtool will
78+ # generate entire wrapper executables (instead of simple wrapper scripts as on Unix) to ensure
79+ # proper operation of uninstalled programs linked by libtool against the uninstalled shared library.
80+ # These executables are compiled from C source file for which our flags may not be appropriate,
81+ # e.g., -std=c89 flag has lead to undesirable warnings in the past.
82+ #
83+ # TODO We should analogously not touch CPPFLAGS and LDFLAGS but currently there are no issues.
84+ AC_DEFUN ( [ SECP_TRY_APPEND_DEFAULT_CFLAGS] , [
85+ # Try to append -Werror=unknown-warning-option to CFLAGS temporarily. Otherwise clang will
86+ # not error out if it gets unknown warning flags and the checks here will always succeed
87+ # no matter if clang knows the flag or not.
88+ SECP_TRY_APPEND_DEFAULT_CFLAGS_saved_CFLAGS="$CFLAGS"
89+ SECP_TRY_APPEND_CFLAGS([ -Werror=unknown-warning-option] , CFLAGS)
90+
91+ SECP_TRY_APPEND_CFLAGS([ -std=c89 -pedantic -Wno-long-long -Wnested-externs -Wshadow -Wstrict-prototypes -Wundef] , $1 ) # GCC >= 3.0, -Wlong-long is implied by -pedantic.
92+ SECP_TRY_APPEND_CFLAGS([ -Wno-overlength-strings] , $1 ) # GCC >= 4.2, -Woverlength-strings is implied by -pedantic.
93+ SECP_TRY_APPEND_CFLAGS([ -Wall] , $1 ) # GCC >= 2.95 and probably many other compilers
94+ SECP_TRY_APPEND_CFLAGS([ -Wno-unused-function] , $1 ) # GCC >= 3.0, -Wunused-function is implied by -Wall.
95+ SECP_TRY_APPEND_CFLAGS([ -Wextra] , $1 ) # GCC >= 3.4, this is the newer name of -W, which we don't use because older GCCs will warn about unused functions.
96+ SECP_TRY_APPEND_CFLAGS([ -Wcast-align] , $1 ) # GCC >= 2.95
97+ SECP_TRY_APPEND_CFLAGS([ -Wcast-align=strict] , $1 ) # GCC >= 8.0
98+ SECP_TRY_APPEND_CFLAGS([ -Wconditional-uninitialized] , $1 ) # Clang >= 3.0 only
99+ SECP_TRY_APPEND_CFLAGS([ -fvisibility=hidden] , $1 ) # GCC >= 4.0
100+
101+ CFLAGS="$SECP_TRY_APPEND_DEFAULT_CFLAGS_saved_CFLAGS"
102+ ] )
103+ SECP_TRY_APPEND_DEFAULT_CFLAGS(SECP_CFLAGS)
102104
103105# ##
104106# ## Define config arguments
@@ -213,10 +215,14 @@ AM_CONDITIONAL([VALGRIND_ENABLED],[test "$enable_valgrind" = "yes"])
213215
214216if test x"$enable_coverage" = x"yes"; then
215217 AC_DEFINE ( COVERAGE , 1 , [ Define this symbol to compile out all VERIFY code] )
216- CFLAGS ="-O0 --coverage $CFLAGS "
218+ SECP_CFLAGS ="-O0 --coverage $SECP_CFLAGS "
217219 LDFLAGS="--coverage $LDFLAGS"
218220else
219- CFLAGS="-O2 $CFLAGS"
221+ # Most likely the CFLAGS already contain -O2 because that is autoconf's default.
222+ # We still add it here because passing it twice is not an issue, and handling
223+ # this case would just add unnecessary complexity (see #896).
224+ SECP_CFLAGS="-O2 $SECP_CFLAGS"
225+ SECP_CFLAGS_FOR_BUILD="-O2 $SECP_CFLAGS_FOR_BUILD"
220226fi
221227
222228if test x"$req_asm" = x"auto"; then
@@ -351,6 +357,9 @@ if test x"$enable_valgrind" = x"yes"; then
351357 SECP_INCLUDES="$SECP_INCLUDES $VALGRIND_CPPFLAGS"
352358fi
353359
360+ # Add -Werror and similar flags passed from the outside (for testing, e.g., in CI)
361+ SECP_CFLAGS="$SECP_CFLAGS $WERROR_CFLAGS"
362+
354363# Handle static precomputation (after everything which modifies CFLAGS and friends)
355364if test x"$use_ecmult_static_precomputation" != x"no"; then
356365 if test x"$cross_compiling" = x"no"; then
@@ -360,8 +369,9 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
360369 fi
361370 # If we're not cross-compiling, simply use the same compiler for building the static precompation code.
362371 CC_FOR_BUILD="$CC"
363- CFLAGS_FOR_BUILD="$CFLAGS"
364372 CPPFLAGS_FOR_BUILD="$CPPFLAGS"
373+ SECP_CFLAGS_FOR_BUILD="$SECP_CFLAGS"
374+ CFLAGS_FOR_BUILD="$CFLAGS"
365375 LDFLAGS_FOR_BUILD="$LDFLAGS"
366376 else
367377 AX_PROG_CC_FOR_BUILD
@@ -371,42 +381,32 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
371381 cross_compiling=no
372382 SAVE_CC="$CC"
373383 CC="$CC_FOR_BUILD"
374- SAVE_CFLAGS="$CFLAGS"
375- CFLAGS="$CFLAGS_FOR_BUILD"
376384 SAVE_CPPFLAGS="$CPPFLAGS"
377385 CPPFLAGS="$CPPFLAGS_FOR_BUILD"
386+ SAVE_CFLAGS="$CFLAGS"
387+ CFLAGS="$CFLAGS_FOR_BUILD"
378388 SAVE_LDFLAGS="$LDFLAGS"
379389 LDFLAGS="$LDFLAGS_FOR_BUILD"
380390
381- warn_CFLAGS_FOR_BUILD="-Wall -Wextra -Wno-unused-function"
382- saved_CFLAGS="$CFLAGS"
383- CFLAGS="$warn_CFLAGS_FOR_BUILD $CFLAGS"
384- AC_MSG_CHECKING ( [ if native ${CC_FOR_BUILD} supports ${warn_CFLAGS_FOR_BUILD}] )
385- AC_COMPILE_IFELSE ( [ AC_LANG_SOURCE ( [ [ char foo;] ] ) ] ,
386- [ AC_MSG_RESULT ( [ yes] ) ] ,
387- [ AC_MSG_RESULT ( [ no] )
388- CFLAGS="$saved_CFLAGS"
389- ] )
391+ SECP_TRY_APPEND_DEFAULT_CFLAGS(SECP_CFLAGS_FOR_BUILD)
390392
391393 AC_MSG_CHECKING ( [ for working native compiler: ${CC_FOR_BUILD}] )
392394 AC_RUN_IFELSE (
393395 [ AC_LANG_PROGRAM ( [ ] , [ ] ) ] ,
394396 [ working_native_cc=yes] ,
395397 [ working_native_cc=no] ,[ :] )
396398
397- CFLAGS_FOR_BUILD="$CFLAGS"
398-
399399 # Restore the environment
400400 cross_compiling=$save_cross_compiling
401401 CC="$SAVE_CC"
402- CFLAGS="$SAVE_CFLAGS"
403402 CPPFLAGS="$SAVE_CPPFLAGS"
403+ CFLAGS="$SAVE_CFLAGS"
404404 LDFLAGS="$SAVE_LDFLAGS"
405405
406406 if test x"$working_native_cc" = x"no"; then
407407 AC_MSG_RESULT ( [ no] )
408408 set_precomp=no
409- m4_define ( [ please_set_for_build] , [ Please set CC_FOR_BUILD, CFLAGS_FOR_BUILD, CPPFLAGS_FOR_BUILD , and/or LDFLAGS_FOR_BUILD.] )
409+ m4_define ( [ please_set_for_build] , [ Please set CC_FOR_BUILD, CPPFLAGS_FOR_BUILD, CFLAGS_FOR_BUILD , and/or LDFLAGS_FOR_BUILD.] )
410410 if test x"$use_ecmult_static_precomputation" = x"yes"; then
411411 AC_MSG_ERROR ( [ native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build] )
412412 else
@@ -419,8 +419,9 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
419419 fi
420420
421421 AC_SUBST ( CC_FOR_BUILD )
422- AC_SUBST ( CFLAGS_FOR_BUILD )
423422 AC_SUBST ( CPPFLAGS_FOR_BUILD )
423+ AC_SUBST ( SECP_CFLAGS_FOR_BUILD )
424+ AC_SUBST ( CFLAGS_FOR_BUILD )
424425 AC_SUBST ( LDFLAGS_FOR_BUILD )
425426else
426427 set_precomp=no
@@ -490,6 +491,7 @@ AC_SUBST(SECP_INCLUDES)
490491AC_SUBST ( SECP_LIBS )
491492AC_SUBST ( SECP_TEST_LIBS )
492493AC_SUBST ( SECP_TEST_INCLUDES )
494+ AC_SUBST ( SECP_CFLAGS )
493495AM_CONDITIONAL([ ENABLE_COVERAGE] , [ test x"$enable_coverage" = x"yes"] )
494496AM_CONDITIONAL([ USE_TESTS] , [ test x"$use_tests" != x"no"] )
495497AM_CONDITIONAL([ USE_EXHAUSTIVE_TESTS] , [ test x"$use_exhaustive_tests" != x"no"] )
532534echo
533535echo " valgrind = $enable_valgrind"
534536echo " CC = $CC"
535- echo " CFLAGS = $CFLAGS"
536537echo " CPPFLAGS = $CPPFLAGS"
538+ echo " SECP_CFLAGS = $SECP_CFLAGS"
539+ echo " CFLAGS = $CFLAGS"
537540echo " LDFLAGS = $LDFLAGS"
538541echo
539542if test x"$set_precomp" = x"yes"; then
540543echo " CC_FOR_BUILD = $CC_FOR_BUILD"
541- echo " CFLAGS_FOR_BUILD = $CFLAGS_FOR_BUILD"
542544echo " CPPFLAGS_FOR_BUILD = $CPPFLAGS_FOR_BUILD"
545+ echo " SECP_CFLAGS_FOR_BUILD = $SECP_CFLAGS_FOR_BUILD"
546+ echo " CFLAGS_FOR_BUILD = $CFLAGS_FOR_BUILD"
543547echo " LDFLAGS_FOR_BUILD = $LDFLAGS_FOR_BUILD"
544548fi
0 commit comments