2

I am trying to use autoconf to check the version of gfortran used to compile my code. How to get the version number safely in a portable way?

The easiest way would be to simply use gfortran -dumpversion, but I read that there is some problems with this approach (on my machine for example, it does not return the patchlevel number). The option -dumpfullversion I found in the documentation does not seem to be implemented (I get a fatal error).

Instead, I am trying to use gfortran --version or gfortran -v. My first attempt is the following:

GFORTRAN_VERSION=`gfortran -v 2>&1 | $AWK 'END { print }' | $AWK '{ print $3 }'`

where $AWK contains the awk-type program found by AC_PROG_AWK. It works perfectly on all the linux machines I have tested. However, it does not look reasonable to assume that gfortran -v will always return the version number in the third position of the last line of its output. For instance, will this approach work on MacOS or Windows?

Any advice would be very welcome, thank you very much in advance!

remek
  • 933
  • 1
  • 10
  • 20

3 Answers3

2

You can write a short program that will use the preprocessor definitions as follows

program p
  implicit none

  write(*,*) __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
end program p

see What are the gcc predefined macros for the compiler's version number?

You might customize the formatting if you like.

Community
  • 1
  • 1
Pierre de Buyl
  • 7,074
  • 2
  • 16
  • 22
  • This is a good suggestion, but unfortunately I cannot include such a short a program in the package I am developing. I could use the trick with `gfortran -E -dM - < /dev/null` explained on the page you suggested, but how portable would that solution be? (is there a `/dev/null` file on all systems? probably not) – remek Feb 12 '17 at 10:49
  • 2
    `echo "__GNUC__.__GNUC_MINOR__.__GNUC_PATCHLEVEL__" | gfortran -E -cpp - 2>/dev/null | tail -n 1 | sed -e 's/ //g'` outputs 5.3.1 with gfortran 5.3.1 . Thats implies to have some basic unix tools (tail and sed here) and that some sort of piping is supported. I don't know how portable that is, what are the dependencies of the software? – Pierre de Buyl Feb 12 '17 at 15:43
  • 1
    We can also get each value as `ver=$( echo "__GNUC__" | gfortran -E -P - )` etc... – roygvib Feb 12 '17 at 15:53
  • Combining your previous answers, `ver=$( echo "__GNUC__.__GNUC_MINOR__.__GNUC_PATCHLEVEL__" | gfortran -E -P - | sed 's/ //g' )` looks like a promising solution, assuming that sed is available (`AC_PROG_SED` can help) and piping is supported... Thanks! – remek Feb 12 '17 at 17:04
0

I don't understand why you suspect that your current implementation won't work for gfortran on other platforms. This page even refers the reader to the GCC documentation for "non-Fortran-specific" options (e.g. -v). With documentation like that, I'd expect it to be extremely similar to gcc.

You should be able to set your autoconf tests (e.g. AC_LANG or AC_LANG_PUSH) and use regular autoconf macros (e.g. AC_COMPILE_IFELSE). But running a test program for output has a downside -- it breaks cross-compilation in the general case.

You may find it instructive to look at what they are doing in this macro which is a C/C++ implementation (for many compilers) of what you are trying to do in Fortran (for just gfortran).

ldav1s
  • 15,885
  • 2
  • 53
  • 56
0

Recent versions of gfortran support the Fortran 2008 standard library routine COMPILER_VERSION(). Before calling this routine your program must have a USE ISO_FORTRAN_ENV statement.

JPaget
  • 969
  • 10
  • 13