Retrieve compiler info at run time

Dear community,
I am currently working on a project (that should go public soonish, fingers crossed) and I would like to provide compiler’ info at runtime. For this, I would like to identify the compiler from the outcome of compiler_version(). Problem is that I only have access to gfortran, ifort and lfortran. I also found the result of nvfortran on the fortran-lang website. I am confident that all together we can cover most compilers referenced in fpm:

enum, bind(C)
    enumerator :: &
        id_unknown, &
        id_gcc, &
        id_f95, &
        id_caf, &
        id_intel_classic_nix, &
        id_intel_classic_mac, &
        id_intel_classic_windows, &
        id_intel_llvm_nix, &
        id_intel_llvm_windows, &
        id_intel_llvm_unknown, &
        id_pgi, &
        id_nvhpc, &
        id_nag, &
        id_flang, &
        id_flang_new, &
        id_f18, &
        id_ibmxl, &
        id_cray, &
        id_lahey, &
        id_lfortran
end enum

So if you would be so kind to post in this thread the outcome of this program using different compilers, I would really appreciate.

program main
use, intrinsic :: iso_fortran_env, only : compiler_version
implicit none
   print '(A)', &
      compiler_version()
end program

Thanks in advance

I will keep the table up to date

compiler id status outcome macro
gfortran id_gcc GCC version 14.1.0 __GFORTRAN__
f95 id_f95
caf id_caf GCC version 12.3.0
ifort id_intel_classic_nix Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.12.0 Build 20240211_000000 __INTEL_COMPILER
ifort id_intel_classic_mac __INTEL_COMPILER
ifort id_intel_classic_windows (x64) Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.3.0 Build 20210609_000000 __INTEL_COMPILER
ia32 id_intel_classic_windows (x86) Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on IA-32, Version 2021.12.0 Build 20240222_000000 __INTEL_COMPILER
ifx id_intel_llvm_nix Intel(R) Fortran Compiler for applications running on Intel(R) 64, Version 2024.1.2 Build 20240508 __INTEL_COMPILER and __INTEL_LLVM_COMPILER
ifx id_intel_llvm_windows Intel(R) Fortran Compiler for applications running on Intel(R) 64, Version 2024.1.0 Build 20240308 __INTEL_COMPILER{{{ and __INTEL_LLVM_COMPILER```
pgfortran id_pgi __PGI
nvfortran id_nvhpc nvfortran 21.5-0 LLVM __NVCOMPILER (and __PGI for some versions)
nagfor id_nag NAG Fortran Compiler Release 7.2(Shin-Urayasu) Build 7205 NAGFOR
flang id_flang __PGLLVM__
flang id_flang_new flang version 19.0.0 __flang__
f18 id_f18
xlf90 id_ibmxl __IBMC__
crayftn id_cray _CRAYFTN
lf95 id_lahey
lfortran id_lfortran __LFORTRAN__
2 Likes
NAG Fortran Compiler Release 7.2(Shin-Urayasu) Build 7205
1 Like

id_intel_classic_windows (x86)
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on IA-32, Version 2021.12.0 Build 20240222_000000

id_intel_llvm_nix
Intel(R) Fortran Compiler for applications running on Intel(R) 64, Version 2024.1.2 Build 20240508

id_intel_llvm_windows
Intel(R) Fortran Compiler for applications running on Intel(R) 64, Version 2024.1.0 Build 20240308

1 Like

I forgot to do id_intel_classic_nix:
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.12.0 Build 20240211_000000

1 Like

caf is a just a wrapper around gfortran, so it returns the same result as gfortran

GCC version 12.3.0

FWIW, caf --version does return something distinctive:

OpenCoarrays Coarray Fortran Compiler Wrapper (caf version 2.10.2)
1 Like

I think we haven’t implemented that in LFortarn yet. What would you all like us to print?
One idea is:

LFortran version 0.36.0

Or do you want us to also print various configuration options (which backends are enabled, platform, etc.)?

Indeed, I can confirm that compiler_version() is not available on LFortran neither is compiler_options().
For my use case what you propose would be perfectly fine.Since the backend is set with command arguments it would probably be fine to get the info from compiler_options().

We have compiler options to select the backend, optimizations, Fortran features, etc. But then we also have cmake options that you select when you build LFortran itself (such as if LLVM backend is enabled, etc., but you would get an error if you tried to use it and it wasn’t enabled, so it would never get to compiling/executing compiler_options()). And finally there is a platform, like Linux/x86_64, or Apple/arm, or Windows/x86/arm or WebAssembly (at https://dev.lfortran.org/), etc. So it seems compiler options go to compiler_options(). Where would the platform information go, if anywhere? It seems none of the other compilers are printing the platform information.

I opened up an issue to implement these two functions: Implement compiler_version() and compiler_options() intrinsics · Issue #4248 · lfortran/lfortran · GitHub.

1 Like

I see your point now. And looking at the other compilers it seems that the information about the platform is absent. With intel a would usually retrieve similar info with preprocess or macros like _WIN32 or _APPLE_. Maybe that could also be an option for LFortran?

1 Like

AMD flang appears to be different, and although my version is 4.2.0 this program makes me think its compiler_version is 7 years out of date.

program testcompiler !f2008 or later
  use iso_fortran_env, only: compiler_version, compiler_options
  implicit none
  print "(A)", 'compiler version = '//compiler_version()
  print "(A)", 'compiler options = '//compiler_options()
end program testcompiler
lf) john:~/Test$ ~/AMD/aocc-compiler-4.2.0/bin/flang  testcompiler.f90 && ./a.out
compiler version = flang Flang - 1.5 2017-05-01
compiler options = long in
2 Likes

It looks like I will need a combination of compiler_version and default preprocessor macros to identify the compiler. I added a column in the table with macros specific to a given compiler.
@certik, I could not find any predifined macro for lfotran. Are there any? something like __LFORTRAN?

1 Like

Almost: you have to use __LFORTRAN__. We predefine the following macros: lfortran/src/lfortran/parser/preprocessor.re at 2cfed3f5c2236c20be656ade25b6da839cc8c08f · lfortran/lfortran · GitHub. Don’t forget to enable preprocessing using --cpp.

1 Like

For LLVM Flang (flang-new) I get:
flang version 19.0.0 (https://github.com/llvm/llvm-project.git 527e7328607ea0a55855e53a59c5030a7d07a554)

The version matches clang built from the same source.

The predefined macro is __flang__

1 Like

NVIDIA HPC compilers are freely available. They support common Linux distributions for all major processor architectures. You can use them on a range of systems including Raspberry Pi (assuming you run 64b Linux).

If you want you test LLVM Fortran (flang-new), I have a script that makes it easy to install on Linux. MacOS support in my script is at least partially broken right now and I don’t have time to fix it.

Thanks, I appreciate.
I should have mentioned that I am working on Windows though :sweat_smile:

If the standard was more specific about the format of the information supplied it would be easier to consume, If the compiler version string returned was recommended to be in a NAMELIST group format and like X11 (see xrdb -symbols) required VERSION=NN, REVISION=NN,VENDOR=“name”, RELEASE=YYYYMMDD like X11 or some standard subset of the metadata recommended for program versions would have been nice. Now with most compilers supporting user-defined types that might be nice as well. There would be maintenance issues but perhaps a stdlib procedure based on the information you have gathered could be used to create something more like what the compiler_version procedure was probably originally conceived as being able to provide.

Looks like a missed opportunity indeed. And I agree that parsing the outcome of compiler_version would probably require some maintenance if compiler vendors change the format every now and then.
I’ll try to come up with something and if you find it useful and suitable for stdlib, I’ll be more than happy to contribute.

Compilers Explorer supports Fortran but not NVHPC Fortran yet. It’s probably easier to contribute NVHPC Fortran support to Compiler Explorer than to switch to Linux :laughing:

1 Like

Wondering what is missing to get nvfortran available in CE? nvc++ is already available, I saw there is a github issue with a discussion about nvfortran Add pgroup compiler · Issue #891 · compiler-explorer/compiler-explorer · GitHub :thinking: It would be a nice addition indeed!!

3 Likes

I finally found the time to work on this and released a piece of code to retrieve compiler info at run time. The code is part of the library benchmark.f, and can be found in the file Compiler.f90.
The parser is very primitive at the moment but it does the job done.
Thanks for your help.

1 Like