[CMake] Texinfo file configuration.
Nicolas Desprès
nicolas.despres at gmail.com
Fri Mar 28 10:02:26 EDT 2008
Hi list,
I have a peace of code that I would like to share with you. When
trying to configure a texinfo file such as:
@set myemail ${MYEMAIL}
using CONFIGURE_FILE, I faced a character escaping proble. The
variable MYEMAIL is set to foo at bar.com but in texinfo, since @ is the
command prefix I have to escape the one in the email address like
this: foo@@bar.com. The ESCAPE_QUOTES option of CONFIGURE_FILE doesn't
help for this. May be, we need a way to specify a custom list of
character to escape to CONFIGURE_FILE.
Thus, I wrote this function that do the job by collecting all the
variables included in the input file, then escapes the @ and finally
ask to CONFIGURE_FILE to configure the file.
# - Configure texinfo file
# This function is very similar to configure_file except that it takes care to
# escape @ characters. Prefers to use ${var} expansion form instead of @var@
if(NOT COMMAND CONFIGURE_TEXINFO)
function(CONFIGURE_TEXINFO input output)
# Collect all variables ${var} in the file.
file(READ ${input} inbuf)
# We write [{] instead of { to avoid pattern like ${..} that cmake tries to
# expand.
string(REGEX MATCHALL "\\$[{]([^}]+)}" varlist ${inbuf})
# Removes $, { and } from the variables list.
set(tmpvarlist "")
foreach(i ${varlist})
string(REGEX REPLACE "[$}{]" "" tmp ${i})
list(APPEND tmpvarlist ${tmp})
endforeach(i)
set(varlist ${tmpvarlist})
set(tmpvarlist)
# Save and escape variables.
foreach(i ${varlist})
set(__configure_textinfo_copy__${i} ${${i}})
string(REGEX REPLACE "@([^@])" "@@\\1" tmp "${${i}}")
set(${i} ${tmp})
endforeach(i)
# Configure the file.
configure_file(${input} ${output} ESCAPE_QUOTES)
# Restore variables and destroy backup.
foreach(i ${varlist})
set(${i} ${__configure_textinfo_copy__${i}})
set(__configure_textinfo_copy__${i})
endforeach(i)
message(STATUS "Configure '${input}' to '${output}'")
endfunction(CONFIGURE_TEXINFO)
endif(NOT COMMAND CONFIGURE_TEXINFO)
Best regards,
--
Nicolas Desprès
More information about the CMake
mailing list