Title: How to change a CACHE variable in cmake file run by -P mode
Tags: caching;cmake
Question: I have a cache variable created with
```set(firmware_version ${FirmwareVersion} CACHE STRING &quot;firmware&quot;)
```
and passed this variable to a cmake file with
```COMMAND  ${CMAKE_COMMAND} -Dfirmware_version:STRING=${firmware_version} -P &quot;C:/Projekt/Lapp/PDM/Versioning.cmake&quot;
```
Now I am changing this variable in ```Versioning.cmake``` with a variable
```set(firmware_version ${FirmwareVersion} CACHE STRING &quot;firmware&quot; FORCE)
```
But I cannot see the changes made to the variable visible to ```CMakeLists.txt``` file.
I am forcing the reconfigure of the cmake with
```add_custom_target(versioning ALL
DEPENDS version_tag
)

add_custom_command(
    OUTPUT version_tag
    PRE_BUILD
    COMMAND  ${CMAKE_COMMAND} -Dfirmware_version:STRING=${firmware_version} -P &quot;C:/Projekt/Lapp/PDM/Versioning.cmake&quot;
    )
```

Comment: Putting an absolute path like `C:/Projekt/Lapp/PDM/Versioning.cmake` is a terrible idea. You should instead make it relative to the current source directory like `${CMAKE_CURRENT_SOURCE_DIR}/PDM/Versioning.cmake` (or whatever the actual path is).

Comment: You mentioned "Now I am changing this variable in Versioning.cmake with a variable".  The type of `add_custom_command()` being used will only run during pre build, this means if the target is not out of date the command won't run again.  You may want to use a custom command that `DEPENDS` on `Versioning.cmake`.

Comment: CMake variables are accessible only during the **configuration** stage, because only this stage is processed by CMake. **Build** stage is controlled by make,nmake or other **build utility**. That is, you cannot change CMake variables when you project is built. The only way for change CMake variables is to re-configure the project.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:02/28/21', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<caching><cmake>', 'Title': 'How to change a CACHE variable in cmake file run by -P mode', 'ViewCount': 84, 'id': 66408621, 'user_AboutMe': None, 'user_AccountId': 13283192.0, 'user_DisplayName': 'ankish', 'user_Reputation': 1.0, 'user_Views': 3.0, 'user_id': 9590097.0}}