[CMake] Problems with REGEX REPLACE

Wim Dumon wim at emweb.be
Tue Mar 17 07:09:36 EDT 2009


Hi,

With some off-list help from Pau, and by introducing a few
work-arounds, I settled with this solution:

In CMakeLists.txt:
MACRO (FILE_TO_STRING infile outfile var)
ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${outfile}
                   COMMAND ${CMAKE_COMMAND}
                   ARGS
-Dinfile:FILEPATH=${CMAKE_CURRENT_SOURCE_DIR}/web/skeleton/${infile}

-Doutfile:FILEPATH=${CMAKE_CURRENT_BINARY_DIR}/${outfile}
                        -Dvar:STRING=${var}
                        -P ${CMAKE_CURRENT_SOURCE_DIR}/filetostring.cmake
                   MAIN_DEPENDENCY
                        ${CMAKE_CURRENT_SOURCE_DIR}/web/skeleton/${infile}
)
ENDMACRO (FILE_TO_STRING)

with filetostring.cmake:
FILE(READ ${infile} f0)
STRING( REGEX REPLACE "\\\\" "\\\\\\\\" f1 "${f0}" )
STRING( REGEX REPLACE "\"" "\\\\\"" f2 "${f1}" )
STRING( REGEX REPLACE "\r?\n" "\\\\r\\\\n\"\n  \"" f3 "${f2}" )
SET( f4 "// This is automatically generated code -- do not edit!\n//
Generated from ${file} \n\nnamespace skeletons {\n\n  const char *
${var} =\n  \"${f3}\";\n}\n" )
FILE(WRITE ${outfile} "${f4}")

Nice thing is that we now have dependencies working like before, and
that there's no need to build a program that causes trouble in
cross-compilation setups.

Best regards,
Wim.

2009/3/16 Pau Garcia i Quiles <pgquiles at elpauer.org>:
> Hi Wim,
>
> Try this:
>
> FOREACH(file a.js b.js)
>  FILE(READ ${file} f0 )
>  SET( f1 "char* myFile = \r\n${f0}" )
>  STRING( REGEX REPLACE "\"" "\\\\\"" f2 "${f1}" )
>  STRING( REGEX REPLACE "\r?\n" "\"\r\n\"" f3 "${f2}" )
>  STRING( REGEX REPLACE "\r?\n.$" ";" f4 "${f3}" )
>  STRING( REGEX REPLACE " = ." " = " f5 "${f4}" )
> FILE(WRITE ${file}.cc "${f3}")
> ENDFOREACH(file)
>
> Regular expressions with CMake are tricky :-)
>
> The problem with \" is you needed to use \\\\\" instead
>
> The problem with ^ and $ not matching beginning-of-line and
> end-of-line for multiline cases is bug 5380 ( see
> http://public.kitware.com/Bug/view.php?id=5380 ) and the work-around
> is replacing CRLF (see this thread:
> http://www.mail-archive.com/cmake@cmake.org/msg07252.html )
>
> On Sat, Mar 14, 2009 at 4:31 PM, Wim Dumon <wim at emweb.be> wrote:
>> Hi,
>>
>> I have some files which contain lines like this:
>> int a() {
>>  print "foo";
>>  do "bar";
>> }
>> Which I want to convert into a C-style string, so I want to start with
>> some transformations that would turn this file into something like:
>> char *myFile =
>> "int a() {"
>> "  print \"foo\";"
>> "  do \"bar\";"
>> "}";
>>
>> Currently we're using a small C++ program that does this, but for
>> cross-compilation simplicity I was considering to replace this with a
>> CMake script. I managed to write this transformation in sed, but not
>> in cmake, and after a short look at the cmake code
>> (cmStringCommand.cxx), I gave up. Is what I want possible with cmake?
>>
>> The errors produced are similar to:
>>   string sub-command REGEX, mode REPLACE: Unknown escape "\""in
>>   replace-expression.
>>
>>   string sub-command REGEX, mode REPLACE regex "^" matched an empty string.
>>
>> Best regards,
>> Wim.
>>
>> cmake_minimum_required(VERSION 2.6)
>>
>> # The equivalent of: sed s/\"/\\\\\"/g | sed s/^/\"/ | sed s/$/\"/
>> FOREACH(file a.js b.js)
>>  FILE(READ ${file} f0)
>>  # Does not work: \" is not valid in replacement string
>>  STRING( REGEX REPLACE "\"" "\\\"" f1 "${f0}")
>>  # Does not work: ^ matches an empty string and is for obscure reasons invalid
>>  # Additionally, \" is not valid in replacement string
>>  STRING( REGEX REPLACE "^" "\"" f2 "${f1}")
>>  # Does not work: $ matches an empty string and is for obscure reasons invalid
>>  # Additionally, \" is not valid in replacement string
>>  STRING( REGEX REPLACE "$" "\"" f3 "${f2}")
>>  # Alternative: does not work: \" is not a valid escape in replacement string
>>  #STRING( REGEX REPLACE "\n" "\"\n\"" f2 "${f1}")
>>  FILE(WRITE ${file}.cc "${f3}")
>> ENDFOREACH(file)
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.cmake.org/mailman/listinfo/cmake
>>
>
>
>
> --
> Pau Garcia i Quiles
> http://www.elpauer.org
> (Due to my workload, I may need 10 days to answer)
>


More information about the CMake mailing list