Defining the FORCE keyword in CMake

The FORCE keyword is used to force overwrite a cached variable or target property. Knowing how to use CMake’s FORCE keyword efficiently is a useful skill for developers. It allows more control over build configurations and ensures that important variables and properties are set correctly regardless of earlier settings. By mastering this keyword, developers may help achieve more reliable and maintainable build processes by avoiding common issues and unexpected behavior in complex projects.

Cached variable

When a variable is cached in CMake, its value is preserved across multiple CMake runs. However, if we want to change the value of a cache variable, we can use the set() command with the CACHE option and the FORCE keyword. For example:

set(MY_VARIABLE "new value" CACHE STRING "Description" FORCE)
Setting a cached variable with FORCE keyword

In the code above, the FORCE keyword ensures that the MY_VARIABLE is forcefully set to “new value” regardless of its previous value.

Target property

Likewise, when setting target properties with the set_target_properties() command, the FORCE keyword can be used to force overwriting of existing properties. For example:

set_target_properties(my_target PROPERTIES PROPERTY_NAME "new value" FORCE)
Setting target properties with FORCE keyword

Here, the FORCE keyword ensures that the PROPERTY_NAME of my_target is forcefully set to "new value," overriding any previous value.

Note: Using FORCE keyword should be done cautiously, as it can bypass normal dependency checks and may lead to unexpected behavior. It's generally recommended to use FORCE keyword sparingly and only when necessary.

Try it yourself

Let’s say we have a CMake project with a cached variable named MY_VARIABLE, and we want to allow the user to change its value through a command-line option. However, we also want to provide a default value and ensure that the user-specified value is used even if the cached variable has been set previously.

Here’s an example keyword.cmake file that demonstrates the usage of FORCE keyword.

Click the “Run” button and execute cmake -P keyword.cmake command in the terminal.

cmake_minimum_required(VERSION 3.25.1)

set(MY_VARIABLE "default value" CACHE STRING "Description")
message("MY_VARIABLE: ${MY_VARIABLE}")

set(MY_VARIABLE "without FORCE" CACHE STRING "Description")
message("Change MY_VARIABLE without FORCE: ${MY_VARIABLE}")

set(MY_VARIABLE "with FORCE" CACHE STRING "Description" FORCE)
message("Change MY_VARIABLE with FORCE: ${MY_VARIABLE}")
Playground for the FORCE keyword

Code explanation

The provided code snippet entails the following actions:

  • Line 1: In the CMake script establishes the minimum required version as 3.25.1.

  • Lines 3–4: We introduce a cached variable called MY_VARIABLE with the default value of “default value” and a descriptive text. The initial value of MY_VARIABLE is then displayed.

  • Lines 6–7: The MY_VARIABLE is altered without utilizing the FORCE keyword, and the updated value is printed.

  • Lines 9–10: The MY_VARIABLE undergoes modification using the FORCE keyword, which overrides any previous value. The final value of MY_VARIABLE is then exhibited.

Conclusion

By forcing the overwrite of cached variables and target attributes, the FORCE keyword in CMake gives developers exact control over build configurations and ensures consistent settings between runs. To prevent unexpected dependencies and guarantee consistent project behaviour, its use should be done with caution. Through the judicious use of the FORCE keyword, developers can improve the maintainability and dependability of their CMake projects, hence increasing workflow efficiency in intricate development environments.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved