[cmake-developers] Limitations of IS_ABSOLUTE

Eric Noulard eric.noulard at gmail.com
Fri Dec 21 03:43:10 EST 2012


2012/12/21 Alan W. Irwin <irwin at beluga.phys.uvic.ca>:
> My use case is I want to quickly test whether a data file that is external
> to the source tree exists or not.  So I naively tried to use if(IS_ABSOLUTE
> fullpathname) for this purpose, but all that tests at the current time is
> whether fullpathname starts with a slash or not.  It does not test
> whether fullpathname actually exists.
>
> To demonstrate this limitation use the following test script
>
> set(file /home/irwinx)
> if(IS_ABSOLUTE ${file})
>   message(STATUS "${file} is absolute")
> else(IS_ABSOLUTE ${file})
>   message(STATUS "${file} is not absolute")
> endif(IS_ABSOLUTE ${file})
>
> and run
>
> cmake -p test.cmake
>
> The result is
>
> -- /home/irwinx is absolute
>
> even though that absolute pathname does not exist.
>
> I suspect this behaviour is by design and not a bug, but in that case
> please change the documentation to say that IS_ABSOLUTE (unlike
> IS_SYMLINK and IS_DIRECTORY) just checks for a leading "/" regardless
> of whether the PATH exists or not so that users know the actual limits
> to the usefulness of IS_ABSOLUTE.
>
> I have brought this up because I think it is important to document the
> limitations of IS_ABSOLUTE when the existence of IS_SYMLINK and
> IS_DIRECTORY might lead the user to think there might be an existence
> test with IS_ABSOLUTE.

The doc may be clarified but my point of view is that
IS_SYMLINK and IS_DIRECTORY semantically requires the file to exists
while IS_ABSOLUTE does not.

Perhaps you should simply use:
if(IS_ABSOLUTE  ${file} AND EXISTS ${file})

>  Obviously for my use case it does look like a
> reasonable alternative is to use file(GLOB...) followed by a check of
> the result variable to see if it is empty.  But implementation of an
> additional IF subcommand called IS_FILE which just checks for the
> existence of the specified file would do this in fewer lines and does

Precisely the purpose of
if(EXISTS ${file})

I think.

> seem to be a logical companion subcommand to IS_SYMLINK and IS_DIRECTORY.

Check this:
set(afile /home/irwinx)
set(anexistingpath /tmp/)

foreach (file ${anexistingpath} ${afile})
if(IS_ABSOLUTE ${file})
  message(STATUS "${file} is absolute")
else(IS_ABSOLUTE ${file})
  message(STATUS "${file} is not absolute")
endif(IS_ABSOLUTE ${file})

if(IS_ABSOLUTE ${file} AND EXISTS ${file})
  message(STATUS "${file} is an existing absolute path")
else()
  message(STATUS "${file} is not absolute or does not exists")
endif()
endforeach()

which gives:
$ cmake -P test-abs.cmake
-- /tmp/ is absolute
-- /tmp/ is an existing absolute path
-- /home/irwinx is absolute
-- /home/irwinx is not absolute or does not exists


-- 
Erk
Le gouvernement représentatif n'est pas la démocratie --
http://www.le-message.org



More information about the cmake-developers mailing list