Discussion:
Check whether AC_PROG_X has failed
Shahbaz Youssefi
2014-11-28 10:51:46 UTC
Permalink
Hi all,

First off, I'm not in the mailing list, so please CC replies to myself.

Second, I am rather new to autoconf.

I noticed a problem with the output of AC_PROG_CXX which I guess (but not
yet sure) extends to others such as AC_PROG_CC, AC_PROG_CPP etc.

The problem is that if you call AC_PROG_CXX, even if there are no C++
compilers, *CXX is set to g++ anyway* and the macro doesn't provide a means
to understand if it actually succeeded or not. This is a problem because if
you want to enable a feature only if a C++ compiler exists, you need to be
able to well, check if there is a C++ compiler. This is the relevant Stack
Overflow question: http://stackoverflow.com/q/27111000/912144

Now I'm by no means an expert with m4, but I think I could hack away at
this. Before sending a patch however, I wanted to make sure if the idea is
acceptable.

My idea is to add a variable that simply says whether the test was
successful or not. For example, ac_cv_prog_cc_g, ac_cv_prog_cc_c_o,
ac_cv_prog_cc_c99 etc are used for various feature tests, so why not have
these additional variables:

- ac_cv_prog_cc set to yes if AC_PROG_CC succeeded (and therefore CC is
valid)
- ac_cv_prog_cpp set to yes if AC_PROG_CPP succeeded (and therefore CPP is
valid)
- ac_cv_prog_cxx set to yes if AC_PROG_CXX succeeded (and therefore CXX is
valid)

You get the idea!

This addition would be completely backward-compatible since it doesn't
remove anything and uses only reserved variable names.

That is unless those variables are actually used and I don't know about it,
then of course another name could be picked.

Please let me know what you think,
Shahbaz
Nick Bowler
2014-11-28 15:34:50 UTC
Permalink
Hello,
Post by Shahbaz Youssefi
The problem is that if you call AC_PROG_CXX, even if there are no C++
compilers, *CXX is set to g++ anyway* and the macro doesn't provide a means
to understand if it actually succeeded or not. This is a problem because if
you want to enable a feature only if a C++ compiler exists, you need to be
able to well, check if there is a C++ compiler. This is the relevant Stack
Overflow question: http://stackoverflow.com/q/27111000/912144
Now I'm by no means an expert with m4, but I think I could hack away at
this. Before sending a patch however, I wanted to make sure if the idea is
acceptable.
To contrast: AC_PROG_CC behaviour is to fail hard if no C compiler is
found (or if certain tests of it fail).

You may find that defining what "succeeded" means for AC_PROG_CXX
will be hard to do. But you can always do something like this in
configure.ac (totally untested):

AC_PROG_CXX

AC_LANG([C++])
AC_CACHE_CHECK([whether the C++ compiler works], [my_cv_cxx_works],
[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <iostream>],
[std::cout << "Hello, World" << std::endl;])],
[my_cv_cxx_works=yes], [my_cv_cxx_works=no])])

AS_IF([test x"$my_cv_cxx_works" != x"yes"],
[AC_MSG_ERROR([sorry, can't help you])])

Perhaps AC_PROG_CXX would be improved by simply performing a test like
the above at the end.

Regards,
--
Nick Bowler, Elliptic Technologies (http://www.elliptictech.com/)
Shahbaz Youssefi
2014-11-28 15:45:35 UTC
Permalink
I see, so AC_PROG_CC cannot "optionally fail"?

No matter. Particularly regarding AC_PROG_CXX, I am aware that you can add
a test then to check if the compiler works, but that is essentially a
duplicate of what AC_PROG_CXX was supposed to do, no?

As a side note, why does AC_PROG_CXX set the CXX variable to g++ even if it
knows for sure that g++ doesn't exist? This is according to the
documentation (
https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/C_002b_002b-Compiler.html
)
If none of those checks succeed, then as a last resort set CXX to g++
What does AC_PROG_CXX hope to achieve by setting CXX to something that it
knows doesn't work?
Hello,
Post by Shahbaz Youssefi
The problem is that if you call AC_PROG_CXX, even if there are no C++
compilers, *CXX is set to g++ anyway* and the macro doesn't provide a
means
Post by Shahbaz Youssefi
to understand if it actually succeeded or not. This is a problem because
if
Post by Shahbaz Youssefi
you want to enable a feature only if a C++ compiler exists, you need to
be
Post by Shahbaz Youssefi
able to well, check if there is a C++ compiler. This is the relevant
Stack
Post by Shahbaz Youssefi
Overflow question: http://stackoverflow.com/q/27111000/912144
Now I'm by no means an expert with m4, but I think I could hack away at
this. Before sending a patch however, I wanted to make sure if the idea
is
Post by Shahbaz Youssefi
acceptable.
To contrast: AC_PROG_CC behaviour is to fail hard if no C compiler is
found (or if certain tests of it fail).
You may find that defining what "succeeded" means for AC_PROG_CXX
will be hard to do. But you can always do something like this in
AC_PROG_CXX
AC_LANG([C++])
AC_CACHE_CHECK([whether the C++ compiler works], [my_cv_cxx_works],
[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <iostream>],
[std::cout << "Hello, World" << std::endl;])],
[my_cv_cxx_works=yes], [my_cv_cxx_works=no])])
AS_IF([test x"$my_cv_cxx_works" != x"yes"],
[AC_MSG_ERROR([sorry, can't help you])])
Perhaps AC_PROG_CXX would be improved by simply performing a test like
the above at the end.
Regards,
--
Nick Bowler, Elliptic Technologies (http://www.elliptictech.com/)
Aggelos Kolaitis
2014-11-28 16:29:55 UTC
Permalink
I run into that issue a while ago. That's what I came up with, it works perfecty

# Search for required tools
AC_PROG_CC
AC_PROG_CXX

# Check if g++ works
AC_MSG_CHECKING(if $CXX works)
cxx_compiler_works=no
AC_LANG_PUSH(C++)
AC_TRY_COMPILE([#include <iostream>],[ ], cxx_compiler_works=yes, CXX="" )
AC_LANG_POP
AC_MSG_RESULT($cxx_compiler_works)
Post by Shahbaz Youssefi
Hi all,
First off, I'm not in the mailing list, so please CC replies to myself.
Second, I am rather new to autoconf.
I noticed a problem with the output of AC_PROG_CXX which I guess (but not
yet sure) extends to others such as AC_PROG_CC, AC_PROG_CPP etc.
The problem is that if you call AC_PROG_CXX, even if there are no C++
compilers, *CXX is set to g++ anyway* and the macro doesn't provide a means
to understand if it actually succeeded or not. This is a problem because if
you want to enable a feature only if a C++ compiler exists, you need to be
able to well, check if there is a C++ compiler. This is the relevant Stack
Overflow question: http://stackoverflow.com/q/27111000/912144
Now I'm by no means an expert with m4, but I think I could hack away at
this. Before sending a patch however, I wanted to make sure if the idea is
acceptable.
My idea is to add a variable that simply says whether the test was
successful or not. For example, ac_cv_prog_cc_g, ac_cv_prog_cc_c_o,
ac_cv_prog_cc_c99 etc are used for various feature tests, so why not have
- ac_cv_prog_cc set to yes if AC_PROG_CC succeeded (and therefore CC is
valid)
- ac_cv_prog_cpp set to yes if AC_PROG_CPP succeeded (and therefore CPP is
valid)
- ac_cv_prog_cxx set to yes if AC_PROG_CXX succeeded (and therefore CXX is
valid)
You get the idea!
This addition would be completely backward-compatible since it doesn't
remove anything and uses only reserved variable names.
That is unless those variables are actually used and I don't know about it,
then of course another name could be picked.
Please let me know what you think,
Shahbaz
_______________________________________________
Autoconf mailing list
https://lists.gnu.org/mailman/listinfo/autoconf
Loading...