@@ -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.
@@ -42,8 +38,8 @@ AM_PROG_AS
4238case $host_os in
4339  *darwin*)
4440     if  test x$cross_compiling != xyes; then
45-        AC_PATH_PROG ( [ BREW] brew ,) 
46-        if test x$BREW != x ; then
41+        AC_CHECK_PROG ( [ BREW]   brew ,  brew ) 
42+        if test x$BREW = xbrew ; then
4743         #  These Homebrew packages may be keg-only, meaning that they won't be found
4844         #  in expected paths because they may conflict with system files. Ask
4945         #  Homebrew where each one is located, then adjust paths accordingly.
@@ -58,10 +54,10 @@ case $host_os in
5854           VALGRIND_CPPFLAGS="-I$valgrind_prefix/include"
5955         fi
6056       else
61-          AC_PATH_PROG ( [ PORT] port ,) 
57+          AC_CHECK_PROG ( [ PORT]   port ,  port ) 
6258         #  If homebrew isn't installed and macports is, add the macports default paths
6359         #  as a last resort.
64-          if test x$PORT != x ; then
60+          if test x$PORT = xport ; then
6561           CPPFLAGS="$CPPFLAGS -isystem /opt/local/include"
6662           LDFLAGS="$LDFLAGS -L/opt/local/lib"
6763         fi
@@ -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]  
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
@@ -253,10 +255,14 @@ AM_CONDITIONAL([VALGRIND_ENABLED],[test "$enable_valgrind" = "yes"])
253255
254256if test x"$enable_coverage" = x"yes"; then
255257    AC_DEFINE ( COVERAGE , 1 , [ Define this symbol to compile out all VERIFY code] ) 
256-     CFLAGS ="-O0 --coverage $CFLAGS "
258+     SECP_CFLAGS ="-O0 --coverage $SECP_CFLAGS "
257259    LDFLAGS="--coverage $LDFLAGS"
258260else
259-     CFLAGS="-O2 $CFLAGS"
261+     #  Most likely the CFLAGS already contain -O2 because that is autoconf's default.
262+     #  We still add it here because passing it twice is not an issue, and handling
263+     #  this case would just add unnecessary complexity (see #896).
264+     SECP_CFLAGS="-O2 $SECP_CFLAGS"
265+     SECP_CFLAGS_FOR_BUILD="-O2 $SECP_CFLAGS_FOR_BUILD"
260266fi
261267
262268AC_MSG_CHECKING ( [ for __builtin_popcount] ) 
@@ -403,6 +409,9 @@ if test x"$enable_valgrind" = x"yes"; then
403409  SECP_INCLUDES="$SECP_INCLUDES $VALGRIND_CPPFLAGS"
404410fi
405411
412+ #  Add -Werror and similar flags passed from the outside (for testing, e.g., in CI)
413+ SECP_CFLAGS="$SECP_CFLAGS $WERROR_CFLAGS"
414+ 
406415#  Handle static precomputation (after everything which modifies CFLAGS and friends)
407416if test x"$use_ecmult_static_precomputation" != x"no"; then
408417  if test x"$cross_compiling" = x"no"; then
@@ -412,8 +421,9 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
412421    fi
413422    #  If we're not cross-compiling, simply use the same compiler for building the static precompation code.
414423    CC_FOR_BUILD="$CC"
415-     CFLAGS_FOR_BUILD="$CFLAGS"
416424    CPPFLAGS_FOR_BUILD="$CPPFLAGS"
425+     SECP_CFLAGS_FOR_BUILD="$SECP_CFLAGS"
426+     CFLAGS_FOR_BUILD="$CFLAGS"
417427    LDFLAGS_FOR_BUILD="$LDFLAGS"
418428  else
419429    AX_PROG_CC_FOR_BUILD 
@@ -423,42 +433,32 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
423433    cross_compiling=no
424434    SAVE_CC="$CC"
425435    CC="$CC_FOR_BUILD"
426-     SAVE_CFLAGS="$CFLAGS"
427-     CFLAGS="$CFLAGS_FOR_BUILD"
428436    SAVE_CPPFLAGS="$CPPFLAGS"
429437    CPPFLAGS="$CPPFLAGS_FOR_BUILD"
438+     SAVE_CFLAGS="$CFLAGS"
439+     CFLAGS="$CFLAGS_FOR_BUILD"
430440    SAVE_LDFLAGS="$LDFLAGS"
431441    LDFLAGS="$LDFLAGS_FOR_BUILD"
432442
433-     warn_CFLAGS_FOR_BUILD="-Wall -Wextra -Wno-unused-function"
434-     saved_CFLAGS="$CFLAGS"
435-     CFLAGS="$warn_CFLAGS_FOR_BUILD $CFLAGS"
436-     AC_MSG_CHECKING ( [ if native ${CC_FOR_BUILD} supports ${warn_CFLAGS_FOR_BUILD}] ) 
437-     AC_COMPILE_IFELSE ( [ AC_LANG_SOURCE ( [ [ char foo;] ] ) ] 
438-         [  AC_MSG_RESULT ( [ yes] )  ] 
439-         [  AC_MSG_RESULT ( [ no] ) 
440-           CFLAGS="$saved_CFLAGS" 
441-         ]  ) 
443+     SECP_TRY_APPEND_DEFAULT_CFLAGS(SECP_CFLAGS_FOR_BUILD)
442444
443445    AC_MSG_CHECKING ( [ for working native compiler: ${CC_FOR_BUILD}] ) 
444446    AC_RUN_IFELSE ( 
445447      [ AC_LANG_PROGRAM ( [ ] [ ] ) ] 
446448      [ working_native_cc=yes] 
447449      [ working_native_cc=no] [ :] ) 
448450
449-     CFLAGS_FOR_BUILD="$CFLAGS"
450- 
451451    #  Restore the environment
452452    cross_compiling=$save_cross_compiling
453453    CC="$SAVE_CC"
454-     CFLAGS="$SAVE_CFLAGS"
455454    CPPFLAGS="$SAVE_CPPFLAGS"
455+     CFLAGS="$SAVE_CFLAGS"
456456    LDFLAGS="$SAVE_LDFLAGS"
457457
458458    if test x"$working_native_cc" = x"no"; then
459459      AC_MSG_RESULT ( [ no] ) 
460460      set_precomp=no
461-       m4_define ( [ please_set_for_build] [ Please set CC_FOR_BUILD, CFLAGS_FOR_BUILD, CPPFLAGS_FOR_BUILD , and/or LDFLAGS_FOR_BUILD.] ) 
461+       m4_define ( [ please_set_for_build] [ Please set CC_FOR_BUILD, CPPFLAGS_FOR_BUILD, CFLAGS_FOR_BUILD , and/or LDFLAGS_FOR_BUILD.] ) 
462462      if test x"$use_ecmult_static_precomputation" = x"yes";  then
463463        AC_MSG_ERROR ( [ native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build] ) 
464464      else
@@ -471,8 +471,9 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
471471  fi
472472
473473  AC_SUBST ( CC_FOR_BUILD ) 
474-   AC_SUBST ( CFLAGS_FOR_BUILD ) 
475474  AC_SUBST ( CPPFLAGS_FOR_BUILD ) 
475+   AC_SUBST ( SECP_CFLAGS_FOR_BUILD ) 
476+   AC_SUBST ( CFLAGS_FOR_BUILD ) 
476477  AC_SUBST ( LDFLAGS_FOR_BUILD ) 
477478else
478479  set_precomp=no
@@ -626,6 +627,7 @@ AC_SUBST(SECP_INCLUDES)
626627AC_SUBST ( SECP_LIBS ) 
627628AC_SUBST ( SECP_TEST_LIBS ) 
628629AC_SUBST ( SECP_TEST_INCLUDES ) 
630+ AC_SUBST ( SECP_CFLAGS ) 
629631AM_CONDITIONAL([ ENABLE_COVERAGE] [ test x"$enable_coverage" = x"yes"] 
630632AM_CONDITIONAL([ USE_TESTS] [ test x"$use_tests" != x"no"] 
631633AM_CONDITIONAL([ USE_EXHAUSTIVE_TESTS] [ test x"$use_exhaustive_tests" != x"no"] 
679681echo
680682echo "  valgrind                = $enable_valgrind"
681683echo "  CC                      = $CC"
682- echo "  CFLAGS                  = $CFLAGS"
683684echo "  CPPFLAGS                = $CPPFLAGS"
685+ echo "  SECP_CFLAGS             = $SECP_CFLAGS"
686+ echo "  CFLAGS                  = $CFLAGS"
684687echo "  LDFLAGS                 = $LDFLAGS"
685688echo
686689if test x"$set_precomp" = x"yes"; then
687690echo "  CC_FOR_BUILD            = $CC_FOR_BUILD"
688- echo "  CFLAGS_FOR_BUILD        = $CFLAGS_FOR_BUILD"
689691echo "  CPPFLAGS_FOR_BUILD      = $CPPFLAGS_FOR_BUILD"
692+ echo "  SECP_CFLAGS_FOR_BUILD   = $SECP_CFLAGS_FOR_BUILD"
693+ echo "  CFLAGS_FOR_BUILD        = $CFLAGS_FOR_BUILD"
690694echo "  LDFLAGS_FOR_BUILD       = $LDFLAGS_FOR_BUILD"
691695fi
0 commit comments