[CMake] Splitting directory and file.

Pablo Zurita pzurita at gmail.com
Wed Mar 2 23:00:50 EST 2011


Thanks for the help guys. With that I was able to write the following
function that allows me to deal with my source as I want.

	# create_source_group(relativeSourcePath sourceGroupName files)
	#
	# Creates a source group with the specified name relative to the relative path
	# specified.
	#
	# Parameters:
	#	- sourceGroupName: Name of the source group to create.
	#	- relativeSourcePath: Relative path to the files.
	#	- sourceFiles: Files to add to the source group.
	#
	# For example if you have the following directory structure:
	#
	#	- ExampleApplication
	#		- include
	#			- Main.h
	#				- Window
	#					Window.h
	#		- source
	#			- Main.cpp
	#				- Window
	#					Window.cpp
	#
	# You can get your list of files and call create_source_group the following way
	#
	#	file(GLOB_RECURSE my_source_files ${CMAKE_CURRENT_SOURCE_DIR}/source/*)
	#	create_source_group("Source Files"
"${CMAKE_CURRENT_SOURCE_DIR}/source" ${my_source_files})
	#	file(GLOB_RECURSE my_header_files ${CMAKE_CURRENT_SOURCE_DIR}/include/*)
	#	create_source_group("Header Files"
"${CMAKE_CURRENT_SOURCE_DIR}/include" ${my_header_files})
	#	add_executable(ExampleApplication ${my_source_files} ${my_header_files})
	#
	# Then the generated solution would look like this
	#
	#	- ExampleApplication (project)
	#		- Header Files
	#			- Main.h
	#				- Window
	#					Window.h
	#		- Source Files
	#			- Main.cpp
	#				- Window
	#					Window.cpp
	#
	function(create_source_group sourceGroupName relativeSourcePath sourceFiles)
		FOREACH(currentSourceFile ${ARGN})
			FILE(RELATIVE_PATH folder ${relativeSourcePath} ${currentSourceFile})
			get_filename_component(filename ${folder} NAME)
			string(REPLACE ${filename} "" folder ${folder})
			if(NOT folder STREQUAL "")
				string(REGEX REPLACE "/+$" "" folderlast ${folder})
				string(REPLACE "/" "\\" folderlast ${folderlast})
				SOURCE_GROUP("${sourceGroupName}\\${folderlast}" FILES ${currentSourceFile})
			endif(NOT folder STREQUAL "")
		ENDFOREACH(currentSourceFile ${ARGN})
	endfunction(create_source_group)


Thanks,
Pablo Zurita.

On Wed, Mar 2, 2011 at 12:17 AM, Pablo Zurita <pzurita at gmail.com> wrote:
> Hello everybody,
>
> I'm just getting started and I need some help dealing with strings in
> CMake. Basically what I have a bunch of strings such as the ones
> below:
>
> "Main.cpp"
> "Window/Example/Example.cpp"
> "Window/Window.cpp"
>
> What I need to do is create two strings for each, one is going to be
> the directory, so the output in order for the list above is:
>
> ""
> "Window/Example"
> "Window"
>
> And the other string I need is the file itself. So the output for that would be
>
> "Main.cpp"
> "Example.cpp"
> "Window.cpp"
>
> Since I'm processing each one by one it doesn't matter if there are collisions.
>
> As you can see all I need to is find the first forward slash on the
> string reading from right to left, and then put what's on the left of
> that string on one variable, and what is to the right in another.
>
> Does anybody know how I can do that?
>
> Thanks,
> Pablo Zurita.
>


More information about the CMake mailing list