Since we configure and build the code on a host environment (in this case, GNU/Linux or macOS) that is different than the target environment (Windows), we need to provide CMake with information about the target environment which we have encoded in the toolchain.cmake file (https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling).
First and foremost, we provide the name of the target operating system:
set(CMAKE_SYSTEM_NAME Windows)
Then, we specify the compiler(s), for instance:
set(CMAKE_C_COMPILER i686-w64-mingw32-gcc)
set(CMAKE_CXX_COMPILER i686-w64-mingw32-g++)
set(CMAKE_Fortran_COMPILER i686-w64-mingw32-gfortran)
In this simple example, we did not have to detect any libraries or header files but, if we had to, we would specify the root path using the following:
set(CMAKE_FIND_ROOT_PATH /path/to/target/environment)
The target environment can, for instance, be the one provided by an MXE installation.
Finally, we adjust the default behavior of the find commands. We instruct CMake to search headers and libraries in the target environment:
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
And to search programs in the host environment:
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)