CMake provides multiple ways of querying the environment with CMAKE_ variables, ENV variables, and special commands. For example, collected information can be used to support cross-platform scripts. These mechanisms allow us to avoid using platform-specific shell commands that may not be easily portable or differ in naming across environments.

For performance-critical applications, it will be useful to know all the features of the destination platform (for example, instruction sets, CPU core count, and more). This information can then be passed to the compiled binaries so that they can be tuned to perfection. Let's see what information is available in CMake natively.

Discovering the operating system

There are many occasions when it is useful to know what the target operating system is. Even as mundane a thing as a filesystem differs greatly between Windows and Unix in things such as case sensitivity, file path structures, the presence of extensions, privileges, and so on. Most commands present on one system won't be available on another, or they could be named differently (even if it's by a single letter – for example, the ifconfig and ipconfig commands).

If we ever need to support multiple target operating systems with a single CMake script, we just check the CMAKE_SYSTEM_NAME variable so that we can act accordingly. Here's a simple example:

Get hands-on with 1200+ tech skills courses.