@@ -8,10 +8,6 @@ AH_TOP([#define LIBSECP256K1_CONFIG_H])
8
8
AH_BOTTOM ( [ #endif /*LIBSECP256K1_CONFIG_H*/] )
9
9
AM_INIT_AUTOMAKE ( [ foreign subdir-objects] )
10
10
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"}
15
11
LT_INIT
16
12
17
13
# Make the compilation flags quiet unless V=1 is used.
@@ -70,35 +66,41 @@ case $host_os in
70
66
;;
71
67
esac
72
68
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)
102
104
103
105
# ##
104
106
# ## Define config arguments
@@ -213,10 +215,14 @@ AM_CONDITIONAL([VALGRIND_ENABLED],[test "$enable_valgrind" = "yes"])
213
215
214
216
if test x"$enable_coverage" = x"yes"; then
215
217
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 "
217
219
LDFLAGS="--coverage $LDFLAGS"
218
220
else
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"
220
226
fi
221
227
222
228
if test x"$req_asm" = x"auto"; then
@@ -351,6 +357,9 @@ if test x"$enable_valgrind" = x"yes"; then
351
357
SECP_INCLUDES="$SECP_INCLUDES $VALGRIND_CPPFLAGS"
352
358
fi
353
359
360
+ # Add -Werror and similar flags passed from the outside (for testing, e.g., in CI)
361
+ SECP_CFLAGS="$SECP_CFLAGS $WERROR_CFLAGS"
362
+
354
363
# Handle static precomputation (after everything which modifies CFLAGS and friends)
355
364
if test x"$use_ecmult_static_precomputation" != x"no"; then
356
365
if test x"$cross_compiling" = x"no"; then
@@ -360,8 +369,9 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
360
369
fi
361
370
# If we're not cross-compiling, simply use the same compiler for building the static precompation code.
362
371
CC_FOR_BUILD="$CC"
363
- CFLAGS_FOR_BUILD="$CFLAGS"
364
372
CPPFLAGS_FOR_BUILD="$CPPFLAGS"
373
+ SECP_CFLAGS_FOR_BUILD="$SECP_CFLAGS"
374
+ CFLAGS_FOR_BUILD="$CFLAGS"
365
375
LDFLAGS_FOR_BUILD="$LDFLAGS"
366
376
else
367
377
AX_PROG_CC_FOR_BUILD
@@ -371,42 +381,32 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
371
381
cross_compiling=no
372
382
SAVE_CC="$CC"
373
383
CC="$CC_FOR_BUILD"
374
- SAVE_CFLAGS="$CFLAGS"
375
- CFLAGS="$CFLAGS_FOR_BUILD"
376
384
SAVE_CPPFLAGS="$CPPFLAGS"
377
385
CPPFLAGS="$CPPFLAGS_FOR_BUILD"
386
+ SAVE_CFLAGS="$CFLAGS"
387
+ CFLAGS="$CFLAGS_FOR_BUILD"
378
388
SAVE_LDFLAGS="$LDFLAGS"
379
389
LDFLAGS="$LDFLAGS_FOR_BUILD"
380
390
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)
390
392
391
393
AC_MSG_CHECKING ( [ for working native compiler: ${CC_FOR_BUILD}] )
392
394
AC_RUN_IFELSE (
393
395
[ AC_LANG_PROGRAM ( [ ] , [ ] ) ] ,
394
396
[ working_native_cc=yes] ,
395
397
[ working_native_cc=no] ,[ :] )
396
398
397
- CFLAGS_FOR_BUILD="$CFLAGS"
398
-
399
399
# Restore the environment
400
400
cross_compiling=$save_cross_compiling
401
401
CC="$SAVE_CC"
402
- CFLAGS="$SAVE_CFLAGS"
403
402
CPPFLAGS="$SAVE_CPPFLAGS"
403
+ CFLAGS="$SAVE_CFLAGS"
404
404
LDFLAGS="$SAVE_LDFLAGS"
405
405
406
406
if test x"$working_native_cc" = x"no"; then
407
407
AC_MSG_RESULT ( [ no] )
408
408
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.] )
410
410
if test x"$use_ecmult_static_precomputation" = x"yes"; then
411
411
AC_MSG_ERROR ( [ native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build] )
412
412
else
@@ -419,8 +419,9 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
419
419
fi
420
420
421
421
AC_SUBST ( CC_FOR_BUILD )
422
- AC_SUBST ( CFLAGS_FOR_BUILD )
423
422
AC_SUBST ( CPPFLAGS_FOR_BUILD )
423
+ AC_SUBST ( SECP_CFLAGS_FOR_BUILD )
424
+ AC_SUBST ( CFLAGS_FOR_BUILD )
424
425
AC_SUBST ( LDFLAGS_FOR_BUILD )
425
426
else
426
427
set_precomp=no
@@ -490,6 +491,7 @@ AC_SUBST(SECP_INCLUDES)
490
491
AC_SUBST ( SECP_LIBS )
491
492
AC_SUBST ( SECP_TEST_LIBS )
492
493
AC_SUBST ( SECP_TEST_INCLUDES )
494
+ AC_SUBST ( SECP_CFLAGS )
493
495
AM_CONDITIONAL([ ENABLE_COVERAGE] , [ test x"$enable_coverage" = x"yes"] )
494
496
AM_CONDITIONAL([ USE_TESTS] , [ test x"$use_tests" != x"no"] )
495
497
AM_CONDITIONAL([ USE_EXHAUSTIVE_TESTS] , [ test x"$use_exhaustive_tests" != x"no"] )
532
534
echo
533
535
echo " valgrind = $enable_valgrind"
534
536
echo " CC = $CC"
535
- echo " CFLAGS = $CFLAGS"
536
537
echo " CPPFLAGS = $CPPFLAGS"
538
+ echo " SECP_CFLAGS = $SECP_CFLAGS"
539
+ echo " CFLAGS = $CFLAGS"
537
540
echo " LDFLAGS = $LDFLAGS"
538
541
echo
539
542
if test x"$set_precomp" = x"yes"; then
540
543
echo " CC_FOR_BUILD = $CC_FOR_BUILD"
541
- echo " CFLAGS_FOR_BUILD = $CFLAGS_FOR_BUILD"
542
544
echo " CPPFLAGS_FOR_BUILD = $CPPFLAGS_FOR_BUILD"
545
+ echo " SECP_CFLAGS_FOR_BUILD = $SECP_CFLAGS_FOR_BUILD"
546
+ echo " CFLAGS_FOR_BUILD = $CFLAGS_FOR_BUILD"
543
547
echo " LDFLAGS_FOR_BUILD = $LDFLAGS_FOR_BUILD"
544
548
fi
0 commit comments