Identifying Fortran compiler with CMake

Projects use a variety of methods to detect which compiler is being used to thereby set compiler options. Although this discussion focuses on Fortran, it is easily and equally applicable to other languages such as C and C++.

Robustly detect compiler in CMake CMAKE_Fortran_COMPILER_ID. This tells the compiler vendor (GNU, Intel, Clang, etc.) Don’t use CMAKE_Fortran_COMPILER because there are several compiler executables per vendor and this will not be robust over time. CMAKE_Fortran_COMPILER_VERSION allows compiler version comparisons.

Example:

if(CMAKE_Fortran_COMPILER_ID STREQUAL "IntelLLVM")
  add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-warn:declarations;-traceback>")
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
  # Gfortran
  add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-fimplicit-none>")
endif()