[CMake] reading in a file and splitting by line

David Cole david.cole at kitware.com
Wed May 23 10:15:13 EDT 2007


This "works" with just cmake:

  FILE(READ "${file}" contents)

  # Convert file contents into a CMake list (where each element in the list
  # is one line of the file)
  #
  STRING(REGEX REPLACE ";" "\\\\;" contents "${contents}")
  STRING(REGEX REPLACE "\n" ";" contents "${contents}")

The reason I say "works" (in quotes) is that there are two caveats wherein
it does not work:

(1) It puts each line of the (presumed to be text) file into an element of a
cmake list including empty lines as unique elements... However, if you are
familiar with cmake lists at all, you'll instantly say to yourself -- "ah
ha, so blank lines will be skipped in a FOREACH loop." True : caveat #1 :
blank lines are undetectable in a cmake FOREACH structure. But the non-blank
lines are usually the important ones... so skipping the blank ones is
probably ok... depends on your task, I suppose.

(2) It does not handle trailing backslashes at the end of a line
"correctly." It will end up with consecutive lines with trailing backslashes
all put together as one line in the cmake list. caveat #2 : you'll need to
do more work if you want to detect trailing backslashes and get lines
including those stuffed into a cmake list properly

Other than that, it's quite useful and works with CMake 2.4.6 (probably
earlier versions, too, but most of my experience using this technique has
been with 2.4.6.)

HTH,
David



On 5/23/07, Pau Garcia i Quiles <pgquiles at elpauer.org> wrote:
>
> Quoting Philip Lowman <philip at yhbt.com>:
>
> > I'm trying to figure out how to use FILE(READ) coupled with some other
> > CMake call to split a text file into multiple lines that ultimately end
> > up in a CMake List.
> >
> > The goal is to get all of the lines into a CMake list so I can iterate
> > through it with FOREACH() and process each line separately.
> >
> > I was wondering if anyone has any ideas on how to do this?
>
> I don't know how to achieve that using only CMake.
>
> I tried to do the same because I wanted to have a "make uninstall"
> target in my projects and I had to resort to the shell. AFAIK you can
> group the platforms CMake supports in two groups: Windows (where you
> have to script cmd.exe) and all the others (which have a
> bsh-compatible shell).
>
> This is the code I am using in my "make uninstall", you can probably
> use it as a starting point:
>
>
> #
> # Create a "make uninstall" target
> #
> # Prototype:
> #    GENERATE_UNINSTALL_TARGET()
> # Parameters:
> #    (none)
>
> # Unix version works with any SUS-compliant operating system, as it
> needs only Bourne Shell features
> # Win32 version works with any Windows which supports extended cmd.exe
> syntax (Windows NT 4.0 and newer, maybe Windows NT 3.x too).
>
> MACRO(GENERATE_UNINSTALL_TARGET)
> ADD_TO_DISTCLEAN( ${PROJECT_BINARY_DIR}/uninstall.dir )
> IF(WIN32)
>   ADD_CUSTOM_TARGET(uninstall
>         \"FOR /F \"tokens=1* delims= \" %%f IN
> \(${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt"}\)\" DO \(
>                  IF EXIST %%f \(
>                         del /q /f %%f"
>                 \) ELSE \(
>                 echo Problem when removing %%f - Probable causes: File
> already
> removed or not enough permissions
>                 \)
>                 \) VERBATIM
>         )
> ELSE(WIN32)
>      # Unix
>      ADD_CUSTOM_TARGET(uninstall cat
> "${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt" | while read f \;
> do if [ -e \"\$\${f}\" ]; then rm \"\$\${f}\" \; else echo \"Problem
> when removing \"\$\${f}\" - Probable causes: File already removed or
> not enough permissions\" \; fi\; done COMMENT Uninstalling... )
> ENDIF(WIN32)
> ENDMACRO(GENERATE_UNINSTALL_TARGET)
>
> --
> Pau Garcia i Quiles
> http://www.elpauer.org
> (Due to the amount of work, I usually need 10 days to answer)
>
>
>
> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/cmake/attachments/20070523/e4eddf9a/attachment.html


More information about the CMake mailing list