Skip to content

configure_file does not replace outer variable values referenced in the format @VAR@

configure_file substitutes variable values referenced as @VAR@ or ${VAR} in the input file content. Tests have shown that this is not always the case. Consider the following examples:

#define @PROJECT_NAME@_VERSION_MAJOR @@PROJECT_NAME@_VERSION_MAJOR@
#define @PROJECT_NAME@_VERSION_MAJOR @${PROJECT_NAME}_VERSION_MAJOR@
#define @PROJECT_NAME@_VERSION_MAJOR ${${PROJECT_NAME}_VERSION_MAJOR}
#define @PROJECT_NAME@_VERSION_MAJOR ${@PROJECT_NAME@_VERSION_MAJOR}

#define @PROJECT_NAME@_VERSION_@LEVEL@ ${${PROJECT_NAME}_VERSION_${LEVEL}}
#define @PROJECT_NAME@_VERSION_@LEVEL@ ${${PROJECT_NAME}_VERSION_@LEVEL@}
#define @PROJECT_NAME@_VERSION_@LEVEL@ @${PROJECT_NAME}_VERSION_@LEVEL@@

In the results shown below we can observe that both @VAR@ as well as ${VAR} formats are replaced correctly. We can also see that inner variables are replaced before outer variables using both variants.

An unexpected exception to this rule is when @VAR@ format is used for the outer variable replacement. In this case the outer variable is not being replaced. All inner variable replacements are still performed regardless.

See the result for the examples above:

#define MyProj_VERSION_MAJOR @MyProj_VERSION_MAJOR@
#define MyProj_VERSION_MAJOR @MyProj_VERSION_MAJOR@
#define MyProj_VERSION_MAJOR 5
#define MyProj_VERSION_MAJOR 5

#define MyProj_VERSION_PATCH 0
#define MyProj_VERSION_PATCH 0
#define MyProj_VERSION_PATCH @MyProj_VERSION_PATCH@

the observation applies to CMake 3.18.2.

Edited by Arcturus
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information