Ted Middleton Ted Middleton - 2 months ago 7
C++ Question

cmake formatted string output

I'm using cmake (3.1) and I'm trying to convert a version string that's been passed to a find_package() function into a specially formatted version string that another find_package() function makes use of. In particular, I'm trying to convert something that looks like


into something that looks like


(If you haven't guessed yet, this is for FindBoost.cmake which needs Boost_VERSION in the form "105700").

I'm just realizing that I have no idea how to do this in cmake; there doesn't seem to be a command/macro expansion/function to do formatted output with fixed field widths and leading zeros? Like
in printf. Or is there?

Update: I bungled the version number. Updated to reflect this.


Pessimistic answer
No, there is no such functionality for producing formatted output in CMake.

Optimisitic Answer
Roll your own.

I don't know the exact logic for padding the version string with leading and trailing dots, however, the string command offers all the subcommands you probably will need (plus the one or the other if block). E.g., use REPLACE to get rid of the dots:

string(REPLACE "." "" "${version}" version_wo_dots)