Adding quotes in a custom command

Hi,

in my project I need to read content from a file VERSION ( a simple string line without spaces or newlines ), adding quotes (") at begin and end of file, and write it in a new file VERSION_Q. E.g, if VERSION is 3.0, VERSION_Q will be “3.0”

I have inserted the line in the add_custom_command block:

COMMAND ${CMAKE_COMMAND} -E cat ${VERSION_FILE} > ${VERSION_Q_FILE}

but this line simply copies VERSION file to VERSION_Q. How to add quotes here?

You probably can achieve that with a one-liner too, but I would do it like this:

file(READ "${VERSION_FILE}" VERSION_CONTENT)
# the file might (should) contain a empty line in the end of it
string(STRIP "${VERSION_CONTENT}" VERSION_CONTENT)
# perform some other checks like a RegEx for the version format
# ...
file(WRITE "${VERSION_Q_FILE}" "\"${VERSION_CONTENT}\"\n")

The first two lines can be simplified more:

file(STRINGS "${VERSION_FILE}" VERSION_CONTENT LIMIT_COUNT 1 NO_HEX_CONVERSION)

The command even has some REGEX option.

MODERATOR NOTE: Please open a separate discussion thread for new questions rather than changing the focus of an existing thread.

Thank you, @retif and @jtxa . Both proposals are valid, I have selected @retif one since I prefer more simpler commands.

I would like ask a new question now: how I can introduce dependencies?

In my project, a source file depends upon the VERSION_Q_FILE, that depends in turn upon the VERSION_FILE. I have tryed to write it using add_dependencies command, but I cannot formulate them correctly.

I apologize: I am removing it and opening a new topic.