The code for this recipe is available at https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-06/recipe-01, including a Fortran/C example. The recipe is valid with CMake version 3.10 (and higher), and has been tested on GNU/Linux, macOS, and Windows with MSYS Makefiles.
The most straightforward code generation happens at configure time. For example, CMake can detect the operating system and available libraries; based on that information, we can tailor what sources are built, to offer maximum performance to the end user of our library or program. In this and some of the following recipes, we will illustrate how to generate a simple source file that defines a function to report the build system configuration.