In this recipe, we will start out with an example in C++, and later we will demonstrate a Fortran example and attempt to refactor and simplify the CMake code.
Let us consider the following hello-world.cpp source code:
#include <cstdlib>#include <iostream>#include <string>std::string say_hello() {#ifdef IS_INTEL_CXX_COMPILER // only compiled when Intel compiler is selected // such compiler will not compile the other branches return std::string("Hello Intel compiler!");#elif IS_GNU_CXX_COMPILER // only compiled when GNU compiler is selected // such compiler will not compile the other branches return std::string("Hello GNU compiler!");#elif IS_PGI_CXX_COMPILER // etc. return std::string("Hello PGI compiler!");#elif IS_XL_CXX_COMPILER ...