[CMake] reading in a file and splitting by line

Pau Garcia i Quiles pgquiles at elpauer.org
Wed May 23 10:11:37 EDT 2007


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)





More information about the CMake mailing list