Identifying Fortran compiler with CMake
Projects use a variety of methods to detect which compiler is being used to thereby set compilation 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. To get the compiler version, CMAKE_Fortran_COMPILER_VERSION allows version comparisons.
Example:
if(CMAKE_Fortran_COMPILER_ID MATCHES "^Intel")
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-check;-warn;-traceback>")
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
# Gfortran
add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-Wall;-Werror=array-bounds;-Wextra;-Wpedantic>")
endif()