Skip to content

Commit 0725626

Browse files
build: Use own variable SECP_CFLAGS instead of touching user CFLAGS
Fixes one of the items in bitcoin-core#923, namely the warnings of the form '_putenv' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] This also cleans up the way we add CFLAGS, in particular flags enabling warnings. Now we perform some more fine-grained checking for flag support, which is not strictly necessary but the changes also help to document autoconf.ac.
1 parent 1758a92 commit 0725626

File tree

3 files changed

+64
-43
lines changed

3 files changed

+64
-43
lines changed

Makefile.am

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
ACLOCAL_AMFLAGS = -I build-aux/m4
22

3+
# AM_CFLAGS will be automatically prepended to CFLAGS by Automake when compiling some foo
4+
# which does not have an explicit foo_CFLAGS variable set.
5+
AM_CFLAGS = $(SECP_CFLAGS)
6+
37
lib_LTLIBRARIES = libsecp256k1.la
48
include_HEADERS = include/secp256k1.h
59
include_HEADERS += include/secp256k1_preallocated.h
@@ -129,10 +133,10 @@ CPPFLAGS_FOR_BUILD +=-I$(top_srcdir) -I$(builddir)/src
129133
gen_context_OBJECTS = gen_context.o
130134
gen_context_BIN = gen_context$(BUILD_EXEEXT)
131135
gen_%.o: src/gen_%.c src/libsecp256k1-config.h
132-
$(CC_FOR_BUILD) $(DEFS) $(CPPFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD) -c $< -o $@
136+
$(CC_FOR_BUILD) $(DEFS) $(CPPFLAGS_FOR_BUILD) $(SECP_CFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD) -c $< -o $@
133137

134138
$(gen_context_BIN): $(gen_context_OBJECTS)
135-
$(CC_FOR_BUILD) $(CFLAGS_FOR_BUILD) $(LDFLAGS_FOR_BUILD) $^ -o $@
139+
$(CC_FOR_BUILD) $(SECP_CFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD) $(LDFLAGS_FOR_BUILD) $^ -o $@
136140

137141
$(libsecp256k1_la_OBJECTS): src/ecmult_static_context.h
138142
$(tests_OBJECTS): src/ecmult_static_context.h

build-aux/m4/bitcoin_secp.m4

+16
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,19 @@ if test x"$has_valgrind" != x"yes"; then
8282
AC_CHECK_HEADER([valgrind/memcheck.h], [has_valgrind=yes; AC_DEFINE(HAVE_VALGRIND,1,[Define this symbol if valgrind is installed])])
8383
fi
8484
])
85+
86+
dnl SECP_TRY_APPEND_CFLAGS(flags, VAR)
87+
dnl Append flags to VAR if CC accepts them.
88+
AC_DEFUN([SECP_TRY_APPEND_CFLAGS], [
89+
AC_MSG_CHECKING([if ${CC} supports $1])
90+
SECP_TRY_APPEND_CFLAGS_saved_CFLAGS="$CFLAGS"
91+
CFLAGS="$1 $CFLAGS"
92+
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])], [flag_works=yes], [flag_works=no])
93+
AC_MSG_RESULT($flag_works)
94+
CFLAGS="$SECP_TRY_APPEND_CFLAGS_saved_CFLAGS"
95+
if test x"$flag_works" = x"yes"; then
96+
$2="$$2 $1"
97+
fi
98+
unset flag_works
99+
AC_SUBST($2)
100+
])

configure.ac

+42-41
Original file line numberDiff line numberDiff line change
@@ -70,35 +70,41 @@ case $host_os in
7070
;;
7171
esac
7272

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-
])
73+
# Try if some desirable compiler flags are supported and append them to SECP_CFLAGS.
74+
#
75+
# These are our own flags, so we append them to our own SECP_CFLAGS variable (instead of CFLAGS) as
76+
# recommended in the automake manual (Section "Flag Variables Ordering"). CFLAGS belongs to the user
77+
# and we are not supposed to touch it. In the Makefile, we will need to ensure that SECP_CFLAGS
78+
# is prepended to CFLAGS when invoking the compiler so that the user always has the last word (flag).
79+
#
80+
# Another advantage of not touching CFLAGS is that the contents of CFLAGS will be picked up by
81+
# libtool for compiling helper executables. For example, when compiling for Windows, libtool will
82+
# generate entire wrapper executables (instead of simple wrapper scripts as on Unix) to ensure
83+
# proper operation of uninstalled programs linked by libtool against the uninstalled shared library.
84+
# These executables are compiled from C source file for which our flags may not be appropriate,
85+
# e.g., -std=c89 flag has lead to undesirable warnings in the past.
86+
#
87+
# TODO We still touch the CFLAGS for --coverage and -O0/-O2.
88+
# TODO We should analogously not touch CPPFLAGS and LDFLAGS but currently there are no issues.
89+
AC_DEFUN([SECP_TRY_APPEND_DEFAULT_CFLAGS], [
90+
# Try to append -Werror=unknown-warning-option to CFLAGS temporarily. Otherwise clang will
91+
# not error out if it gets unknown warning flags and the checks here will always succeed
92+
# no matter if clang knows the flag or not.
93+
SECP_TRY_APPEND_DEFAULT_CFLAGS_saved_CFLAGS="$CFLAGS"
94+
SECP_TRY_APPEND_CFLAGS([-Werror=unknown-warning-option], CFLAGS)
95+
96+
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.
97+
SECP_TRY_APPEND_CFLAGS([-Wno-overlength-strings], $1) # GCC >= 4.2, -Woverlength-strings is implied by -pedantic.
98+
SECP_TRY_APPEND_CFLAGS([-Wall], $1) # GCC >= 2.95 and probably many other compilers
99+
SECP_TRY_APPEND_CFLAGS([-Wno-unused-function], $1) # GCC >= 3.0, -Wunused-function is implied by -Wall.
100+
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.
101+
SECP_TRY_APPEND_CFLAGS([-Wcast-align], $1) # GCC >= 2.95
102+
SECP_TRY_APPEND_CFLAGS([-Wconditional-uninitialized], $1) # Clang >= 3.0 only
103+
SECP_TRY_APPEND_CFLAGS([-fvisibility=hidden], $1) # GCC >= 4.0
104+
105+
CFLAGS="$SECP_TRY_APPEND_DEFAULT_CFLAGS_saved_CFLAGS"
106+
])
107+
SECP_TRY_APPEND_DEFAULT_CFLAGS(SECP_CFLAGS)
102108

103109
###
104110
### Define config arguments
@@ -360,8 +366,9 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
360366
fi
361367
# If we're not cross-compiling, simply use the same compiler for building the static precompation code.
362368
CC_FOR_BUILD="$CC"
363-
CFLAGS_FOR_BUILD="$CFLAGS"
369+
SECP_CFLAGS_FOR_BUILD="$SECP_CFLAGS"
364370
CPPFLAGS_FOR_BUILD="$CPPFLAGS"
371+
CFLAGS_FOR_BUILD="$CFLAGS"
365372
LDFLAGS_FOR_BUILD="$LDFLAGS"
366373
else
367374
AX_PROG_CC_FOR_BUILD
@@ -378,24 +385,14 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
378385
SAVE_LDFLAGS="$LDFLAGS"
379386
LDFLAGS="$LDFLAGS_FOR_BUILD"
380387

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-
])
388+
SECP_TRY_APPEND_DEFAULT_CFLAGS(SECP_CFLAGS_FOR_BUILD)
390389

391390
AC_MSG_CHECKING([for working native compiler: ${CC_FOR_BUILD}])
392391
AC_RUN_IFELSE(
393392
[AC_LANG_PROGRAM([], [])],
394393
[working_native_cc=yes],
395394
[working_native_cc=no],[:])
396395

397-
CFLAGS_FOR_BUILD="$CFLAGS"
398-
399396
# Restore the environment
400397
cross_compiling=$save_cross_compiling
401398
CC="$SAVE_CC"
@@ -419,6 +416,7 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
419416
fi
420417

421418
AC_SUBST(CC_FOR_BUILD)
419+
AC_SUBST(SECP_CFLAGS_FOR_BUILD)
422420
AC_SUBST(CFLAGS_FOR_BUILD)
423421
AC_SUBST(CPPFLAGS_FOR_BUILD)
424422
AC_SUBST(LDFLAGS_FOR_BUILD)
@@ -490,6 +488,7 @@ AC_SUBST(SECP_INCLUDES)
490488
AC_SUBST(SECP_LIBS)
491489
AC_SUBST(SECP_TEST_LIBS)
492490
AC_SUBST(SECP_TEST_INCLUDES)
491+
AC_SUBST(SECP_CFLAGS)
493492
AM_CONDITIONAL([ENABLE_COVERAGE], [test x"$enable_coverage" = x"yes"])
494493
AM_CONDITIONAL([USE_TESTS], [test x"$use_tests" != x"no"])
495494
AM_CONDITIONAL([USE_EXHAUSTIVE_TESTS], [test x"$use_exhaustive_tests" != x"no"])
@@ -532,12 +531,14 @@ fi
532531
echo
533532
echo " valgrind = $enable_valgrind"
534533
echo " CC = $CC"
534+
echo " SECP_CFLAGS = $SECP_CFLAGS"
535535
echo " CFLAGS = $CFLAGS"
536536
echo " CPPFLAGS = $CPPFLAGS"
537537
echo " LDFLAGS = $LDFLAGS"
538538
echo
539539
if test x"$set_precomp" = x"yes"; then
540540
echo " CC_FOR_BUILD = $CC_FOR_BUILD"
541+
echo " SECP_CFLAGS_FOR_BUILD = $SECP_CFLAGS_FOR_BUILD"
541542
echo " CFLAGS_FOR_BUILD = $CFLAGS_FOR_BUILD"
542543
echo " CPPFLAGS_FOR_BUILD = $CPPFLAGS_FOR_BUILD"
543544
echo " LDFLAGS_FOR_BUILD = $LDFLAGS_FOR_BUILD"

0 commit comments

Comments
 (0)