Secondary Expansion in the cmake language

I have a simple question: is there any concept of secondary expansion (such as !var in bash or GNU make secondary expansion) in the cmake language?

One usage example could be in the second step of the cmake tutorial. One could imagine such a line in the *.h.in file for handling the versioning:

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

That of course doesn’t work because the second ${PROJECT_NAME} is never expanded, but would be if I could specify a secondary expansion.

Thank you in advance! I’m relatively new to cmake, I maybe missed it in the doc.

1 Like

Secondary expansions are supported by CMake. What you can do is perform a second expansion like this:

message( "${${PROJECT_NAME}_VERSION_MINOR}" )

As you pointed out this does not work with the @VAR@ form being the outer replacement. I suspect this is an oversight.

Note that for configure_file this behaviour is not documented and might be changed in the future.

I suggest you to do the second expansion yourself:

configure_file( f.in f.tmp )
configure_file( ${CMAKE_CURRENT_BINARY_DIR}/f.tmp f.out )

Take care, though, as the order of the commands is important.

I’m sorry for my late answer!

Indeed I tested my (useless) test case, and the trivial secondary expansion is indeed supported. I was not expecting so much simplicity!

Thank you for your quick answer, and sorry for my (very!) delayed one.