[libvirt] [PATCH] build: allow building with newer glibc-headers and -O0

glibc 2.15 (on Fedora 17) coupled with explicit disabling of optimization during development dies a painful death: In file included from /usr/include/limits.h:27:0, from /usr/lib/gcc/x86_64-redhat-linux/4.7.0/include/limits.h:169, from /usr/lib/gcc/x86_64-redhat-linux/4.7.0/include/syslimits.h:7, from /usr/lib/gcc/x86_64-redhat-linux/4.7.0/include/limits.h:34, from util/bitmap.c:26: /usr/include/features.h:314:4: error: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Werror=cpp] cc1: all warnings being treated as errors Work around this by only conditionally defining _FORTIFY_SOURCE, in the case where glibc can actually use it. The trick is using AH_VERBATIM instead of AC_DEFINE. * m4/virt-compile-warnings.m4 (LIBVIRT_COMPILE_WARNINGS): Squelch _FORTIFY_SOURCE when needed to avoid glibc #warnings. --- Pushing under the build-breaker rule. m4/virt-compile-warnings.m4 | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/m4/virt-compile-warnings.m4 b/m4/virt-compile-warnings.m4 index 5527bff..a91d69f 100644 --- a/m4/virt-compile-warnings.m4 +++ b/m4/virt-compile-warnings.m4 @@ -102,8 +102,12 @@ AC_DEFUN([LIBVIRT_COMPILE_WARNINGS],[ # Silence certain warnings in gnulib, and use improved glibc headers AC_DEFINE([lint], [1], [Define to 1 if the compiler is checking for lint.]) - AC_DEFINE([_FORTIFY_SOURCE], [2], - [enable compile-time and run-time bounds-checking, and some warnings]) + AH_VERBATIM([FORTIFY_SOURCE], + [/* Enable compile-time and run-time bounds-checking, and some warnings. */ + #if __OPTIMIZE__ + # define _FORTIFY_SOURCE 2 + #endif + ]) # Extra special flags dnl -fstack-protector stuff passes gl_WARN_ADD with gcc -- 1.7.10.2

Hey, On Mon, Jun 04, 2012 at 12:08:20PM -0600, Eric Blake wrote:
glibc 2.15 (on Fedora 17) coupled with explicit disabling of optimization during development dies a painful death:
In file included from /usr/include/limits.h:27:0, from /usr/lib/gcc/x86_64-redhat-linux/4.7.0/include/limits.h:169, from /usr/lib/gcc/x86_64-redhat-linux/4.7.0/include/syslimits.h:7, from /usr/lib/gcc/x86_64-redhat-linux/4.7.0/include/limits.h:34, from util/bitmap.c:26: /usr/include/features.h:314:4: error: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Werror=cpp] cc1: all warnings being treated as errors
Work around this by only conditionally defining _FORTIFY_SOURCE, in the case where glibc can actually use it. The trick is using AH_VERBATIM instead of AC_DEFINE.
* m4/virt-compile-warnings.m4 (LIBVIRT_COMPILE_WARNINGS): Squelch _FORTIFY_SOURCE when needed to avoid glibc #warnings. ---
Pushing under the build-breaker rule.
m4/virt-compile-warnings.m4 | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/m4/virt-compile-warnings.m4 b/m4/virt-compile-warnings.m4 index 5527bff..a91d69f 100644 --- a/m4/virt-compile-warnings.m4 +++ b/m4/virt-compile-warnings.m4 @@ -102,8 +102,12 @@ AC_DEFUN([LIBVIRT_COMPILE_WARNINGS],[ # Silence certain warnings in gnulib, and use improved glibc headers AC_DEFINE([lint], [1], [Define to 1 if the compiler is checking for lint.]) - AC_DEFINE([_FORTIFY_SOURCE], [2], - [enable compile-time and run-time bounds-checking, and some warnings]) + AH_VERBATIM([FORTIFY_SOURCE], + [/* Enable compile-time and run-time bounds-checking, and some warnings. */ + #if __OPTIMIZE__ + # define _FORTIFY_SOURCE 2 + #endif + ])
This is not causing an actual issue while compiling libvirt, but __OPTIMIZE__ is not defined when using -O0, which causes a warning with -Wundef I've tested this with $ gcc --version gcc (GCC) 4.7.0 20120507 (Red Hat 4.7.0-5) $ gcc -c -Wundef -O0 test.c #if __OPTIMIZE__ #warning if __OPTIMIZE__ #endif #ifdef __OPTIMIZE__ #warning ifdef __OPTIMIZE #endif Using a #ifdef would be more correct: diff --git a/m4/virt-compile-warnings.m4 b/m4/virt-compile-warnings.m4 index a91d69f..0ea7276 100644 --- a/m4/virt-compile-warnings.m4 +++ b/m4/virt-compile-warnings.m4 @@ -104,7 +104,7 @@ AC_DEFUN([LIBVIRT_COMPILE_WARNINGS],[ [Define to 1 if the compiler is checking for lint.]) AH_VERBATIM([FORTIFY_SOURCE], [/* Enable compile-time and run-time bounds-checking, and some warnings. */ - #if __OPTIMIZE__ + #ifdef __OPTIMIZE__ # define _FORTIFY_SOURCE 2 #endif ]) [ (or doing #ifdef __OPTIMIZE__ #if __OPTIMIZE .... if __OPTIMIZE__ was set to 0 with -O0 with some gcc/libc versions) Christophe

On 06/07/2012 05:24 AM, Christophe Fergeau wrote: Hello, and thanks for reviewing this.
This is not causing an actual issue while compiling libvirt, but __OPTIMIZE__ is not defined when using -O0, which causes a warning with -Wundef
Libvirt explicitly refuses to use -Wundef (see m4/virt-compile-warnings.m4), since C99 guarantees that you can sanely use an undefined macro as 0 in preprocessor evaluation.
Using a #ifdef would be more correct:
Only if you care about -Wundef, which we don't. But I agree that if other projects are copying this trick to work around glibc's braindead choice of making this warning verbose, then those projects may have to consider this fact. -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On Thu, Jun 07, 2012 at 06:25:40AM -0600, Eric Blake wrote:
On 06/07/2012 05:24 AM, Christophe Fergeau wrote:
Using a #ifdef would be more correct:
Only if you care about -Wundef, which we don't. But I agree that if other projects are copying this trick to work around glibc's braindead choice of making this warning verbose, then those projects may have to consider this fact.
Yes, I've tried reusing this change in other projects (libosinfo) which also use -Wundef which gave me this warning. I felt it was better to use the same 'code' in libvirt and these projects, hence this email ;) But I agree it's not needed in libvirt, I'm fine if thing are kept this way. Christophe

On 06/07/2012 06:45 AM, Christophe Fergeau wrote:
On Thu, Jun 07, 2012 at 06:25:40AM -0600, Eric Blake wrote:
On 06/07/2012 05:24 AM, Christophe Fergeau wrote:
Using a #ifdef would be more correct:
Only if you care about -Wundef, which we don't. But I agree that if other projects are copying this trick to work around glibc's braindead choice of making this warning verbose, then those projects may have to consider this fact.
Yes, I've tried reusing this change in other projects (libosinfo) which also use -Wundef which gave me this warning. I felt it was better to use the same 'code' in libvirt and these projects, hence this email ;) But I agree it's not needed in libvirt, I'm fine if thing are kept this way.
If the argument is that we want to make the autoconf snippet easier to copy elsewhere, then I don't mind applying a patch that uses: #if defined __OPTIMIZE__ && __OPTIMIZE__ as the gate in the AH_VERBATIM block of code. -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On Thu, Jun 07, 2012 at 07:24:30AM -0600, Eric Blake wrote:
If the argument is that we want to make the autoconf snippet easier to copy elsewhere, then I don't mind applying a patch that uses:
#if defined __OPTIMIZE__ && __OPTIMIZE__
as the gate in the AH_VERBATIM block of code.
That would be great to reuse it in other projects! Thanks! Christophe

While libvirt intentionally avoids -Wundef (after all, C99 guarantees sane semantics of treating undefined macros as 0), the glibc insanity of #warning on _FORTIFY_SOURCE coupled with what some people feel is the black magic of autoconf means that other projects are likely to copy our snippet verbatim. We can be nicer to other projects by making it easier to integrate into projects that use -Wundef. Suggested by Christophe Fergeau. * m4/virt-compile-warnings.m4 (LIBVIRT_COMPILE_WARNINGS): Be nice to other projects using -Wundef. --- Pushing under the trivial rule. m4/virt-compile-warnings.m4 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/m4/virt-compile-warnings.m4 b/m4/virt-compile-warnings.m4 index a91d69f..1817047 100644 --- a/m4/virt-compile-warnings.m4 +++ b/m4/virt-compile-warnings.m4 @@ -103,8 +103,9 @@ AC_DEFUN([LIBVIRT_COMPILE_WARNINGS],[ AC_DEFINE([lint], [1], [Define to 1 if the compiler is checking for lint.]) AH_VERBATIM([FORTIFY_SOURCE], - [/* Enable compile-time and run-time bounds-checking, and some warnings. */ - #if __OPTIMIZE__ + [/* Enable compile-time and run-time bounds-checking, and some warnings, + without upsetting newer glibc. */ + #if defined __OPTIMIZE__ && __OPTIMIZE__ # define _FORTIFY_SOURCE 2 #endif ]) -- 1.7.10.2

Thanks! On Thu, Jun 07, 2012 at 10:54:10AM -0600, Eric Blake wrote:
While libvirt intentionally avoids -Wundef (after all, C99 guarantees sane semantics of treating undefined macros as 0), the glibc insanity of #warning on _FORTIFY_SOURCE coupled with what some people feel is the black magic of autoconf means that other projects are likely to copy our snippet verbatim. We can be nicer to other projects by making it easier to integrate into projects that use -Wundef.
Suggested by Christophe Fergeau.
* m4/virt-compile-warnings.m4 (LIBVIRT_COMPILE_WARNINGS): Be nice to other projects using -Wundef. ---
Pushing under the trivial rule.
m4/virt-compile-warnings.m4 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/m4/virt-compile-warnings.m4 b/m4/virt-compile-warnings.m4 index a91d69f..1817047 100644 --- a/m4/virt-compile-warnings.m4 +++ b/m4/virt-compile-warnings.m4 @@ -103,8 +103,9 @@ AC_DEFUN([LIBVIRT_COMPILE_WARNINGS],[ AC_DEFINE([lint], [1], [Define to 1 if the compiler is checking for lint.]) AH_VERBATIM([FORTIFY_SOURCE], - [/* Enable compile-time and run-time bounds-checking, and some warnings. */ - #if __OPTIMIZE__ + [/* Enable compile-time and run-time bounds-checking, and some warnings, + without upsetting newer glibc. */ + #if defined __OPTIMIZE__ && __OPTIMIZE__ # define _FORTIFY_SOURCE 2 #endif ]) -- 1.7.10.2
participants (2)
-
Christophe Fergeau
-
Eric Blake