diff mbox

[PULL,3/3] configure: Fix -lm test, so that tools can be compiled on hosts that require -lm

Message ID 1404202956-30657-4-git-send-email-pbonzini@redhat.com
State New
Headers show

Commit Message

Paolo Bonzini July 1, 2014, 8:22 a.m. UTC
From: Alexey Kardashevskiy <aik@ozlabs.ru>

The existing test whether "-lm" needs to be included or not is
insufficient as it reports false negative on Fedora20/ppc64.
This happens because sin(0.0) is a constant value which compiler
can safely throw away and therefore there is no need to add "-lm".
As the result, qemu-nbd/qemu-io/qemu-img tools cannot compile.

This adds a global variable and uses it in the test to prevent
from optimization.

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
[Remove now useless -lm addition in Makefile.target. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 Makefile.target | 4 ----
 configure       | 2 +-
 2 files changed, 1 insertion(+), 5 deletions(-)

Comments

Peter Maydell July 1, 2014, 8:26 a.m. UTC | #1
On 1 July 2014 09:22, Paolo Bonzini <pbonzini@redhat.com> wrote:
> From: Alexey Kardashevskiy <aik@ozlabs.ru>
>
> The existing test whether "-lm" needs to be included or not is
> insufficient as it reports false negative on Fedora20/ppc64.
> This happens because sin(0.0) is a constant value which compiler
> can safely throw away and therefore there is no need to add "-lm".
> As the result, qemu-nbd/qemu-io/qemu-img tools cannot compile.
>
> This adds a global variable and uses it in the test to prevent
> from optimization.

> --- a/configure
> +++ b/configure
> @@ -3453,7 +3453,7 @@ fi
>  # Do we need libm
>  cat > $TMPC << EOF
>  #include <math.h>
> -int main(void) { return isnan(sin(0.0)); }
> +double x; int main(void) {return isnan(sin(x));}
>  EOF
>  if compile_prog "" "" ; then
>    :

This looks to me like we're leaving ourselves open for
a smarter compiler with linktime optimisation to complain
that x is used uninitialized.

thanks
-- PMM
Eric Blake July 1, 2014, 5:26 p.m. UTC | #2
On 07/01/2014 02:26 AM, Peter Maydell wrote:
> On 1 July 2014 09:22, Paolo Bonzini <pbonzini@redhat.com> wrote:
>> From: Alexey Kardashevskiy <aik@ozlabs.ru>
>>
>> The existing test whether "-lm" needs to be included or not is
>> insufficient as it reports false negative on Fedora20/ppc64.
>> This happens because sin(0.0) is a constant value which compiler
>> can safely throw away and therefore there is no need to add "-lm".
>> As the result, qemu-nbd/qemu-io/qemu-img tools cannot compile.
>>
>> This adds a global variable and uses it in the test to prevent
>> from optimization.
> 
>> --- a/configure
>> +++ b/configure
>> @@ -3453,7 +3453,7 @@ fi
>>  # Do we need libm
>>  cat > $TMPC << EOF
>>  #include <math.h>
>> -int main(void) { return isnan(sin(0.0)); }
>> +double x; int main(void) {return isnan(sin(x));}
>>  EOF
>>  if compile_prog "" "" ; then
>>    :
> 
> This looks to me like we're leaving ourselves open for
> a smarter compiler with linktime optimisation to complain
> that x is used uninitialized.

If that's your worry, what about:

int main(int argc, char **argv) { return isnan(sin(argc > 1)); }
Markus Armbruster July 2, 2014, 7:02 a.m. UTC | #3
Peter Maydell <peter.maydell@linaro.org> writes:

> On 1 July 2014 09:22, Paolo Bonzini <pbonzini@redhat.com> wrote:
>> From: Alexey Kardashevskiy <aik@ozlabs.ru>
>>
>> The existing test whether "-lm" needs to be included or not is
>> insufficient as it reports false negative on Fedora20/ppc64.
>> This happens because sin(0.0) is a constant value which compiler
>> can safely throw away and therefore there is no need to add "-lm".
>> As the result, qemu-nbd/qemu-io/qemu-img tools cannot compile.
>>
>> This adds a global variable and uses it in the test to prevent
>> from optimization.
>
>> --- a/configure
>> +++ b/configure
>> @@ -3453,7 +3453,7 @@ fi
>>  # Do we need libm
>>  cat > $TMPC << EOF
>>  #include <math.h>
>> -int main(void) { return isnan(sin(0.0)); }
>> +double x; int main(void) {return isnan(sin(x));}
>>  EOF
>>  if compile_prog "" "" ; then
>>    :
>
> This looks to me like we're leaving ourselves open for
> a smarter compiler with linktime optimisation to complain
> that x is used uninitialized.

x *is* initialized, to zero.  A sufficiently smart compiler(TM) could
figure out that x is still zero in main(), and constant fold the test
away.

Suggest to use argc.
Peter Maydell July 2, 2014, 9:40 a.m. UTC | #4
On 2 July 2014 08:02, Markus Armbruster <armbru@redhat.com> wrote:
>> This looks to me like we're leaving ourselves open for
>> a smarter compiler with linktime optimisation to complain
>> that x is used uninitialized.
>
> x *is* initialized, to zero.  A sufficiently smart compiler(TM) could
> figure out that x is still zero in main(), and constant fold the test
> away.

Yes, I was wrong there.

> Suggest to use argc.

That's what we did -- see commit f80ea986.

thanks
-- PMM
diff mbox

Patch

diff --git a/Makefile.target b/Makefile.target
index 6089d29..137d0b0 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -163,10 +163,6 @@  dummy := $(call unnest-vars,.., \
 all-obj-y += $(common-obj-y)
 all-obj-$(CONFIG_SOFTMMU) += $(block-obj-y)
 
-ifndef CONFIG_HAIKU
-LIBS+=-lm
-endif
-
 # build either PROG or PROGW
 $(QEMU_PROG_BUILD): $(all-obj-y) ../libqemuutil.a ../libqemustub.a
 	$(call LINK,$^)
diff --git a/configure b/configure
index 23ecb37..6dd44a9 100755
--- a/configure
+++ b/configure
@@ -3453,7 +3453,7 @@  fi
 # Do we need libm
 cat > $TMPC << EOF
 #include <math.h>
-int main(void) { return isnan(sin(0.0)); }
+double x; int main(void) {return isnan(sin(x));}
 EOF
 if compile_prog "" "" ; then
   :