[CMake] CMAKE way to get leaf of file path

Russell L. Carter rcarter at esturion.net
Mon Nov 8 14:49:56 EST 2010


On 11/07/2010 01:49 AM, Michael Hertling wrote:
> On 11/07/2010 02:07 AM, Russell L. Carter wrote:
>>
>> Hi there,
>> Happy cmake user here.  I want to retrieve the leaf name of
>> the directory property "PARENT_DIRECTORY".  This is really
>> a more general cmake question:  how do I most efficiently
>> manipulate path components of the absolute pathnames that
>> cmake uses and returns for many variables?
>>
>> In the current case, suppose I have
>>
>> "/home/me/project/src/module/help"
>>
>> how do I, in the best CMAKE WAY, extract the leaf, "help"?
>>
>> string(REGEXP) or something like that?
>>
>> I would love to be able to run a bash script on the full
>> path and return just the leaf...  can I do that?
> 
> You might try STRING(REGEX REPLACE "^.*/([^/]*)\$" "\\1" LEAF ${PATH}),
> but also read about FILE(TO_CMAKE_PATH ...), FILE(TO_NATIVE_PATH ...)
> and GET_FILENAME_COMPONENT().

That STRING(REGEX ...) got me closer to what I want, but I eventually
settled on this approach (suggested by an email from several years
ago:

string(REPLACE "/" ";" p2list "${path}")
list(REVERSE p2list)
list(GET p2list 0 first)
list(GET p2list 1 second)
message(STATUS "first: ${first} second: ${second}")

Now I can automatically generate unique names for tests by location in
the hierarchy.  Awesome.

Thanks,
Russell


> 
> Regards,
> 
> Michael
> 
> PS: Please don't start a new thread by replying to another one.
> _______________________________________________
> 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


-- 
Russell L. Carter
Esturion, LLC
2285 Sandia Drive
Prescott, Arizona 86301

rcarter at esturion.net
928 308-4154


More information about the CMake mailing list