Help: Document behavior of if(EXISTS,IS_DIRECTORY) for empty paths

Document and add explicit tests for empty string input:
`if(EXISTS "")` and `if(IS_DIRECTORY "")` are always false.
This avoids need for users to do extra checks due to
CMake non-short-circuit logic as below:

    if("${p}")
      if(EXISTS "${p}")
This commit is contained in:
scivision
2023-01-23 10:47:54 -05:00
committed by Brad King
parent adac7f3ca4
commit a1dc38a567
4 changed files with 13 additions and 2 deletions

View File

@@ -165,6 +165,8 @@ File Operations
Resolves symbolic links, i.e. if the named file or directory is a Resolves symbolic links, i.e. if the named file or directory is a
symbolic link, returns true if the target of the symbolic link exists. symbolic link, returns true if the target of the symbolic link exists.
False if the given path is an empty string.
``if(file1 IS_NEWER_THAN file2)`` ``if(file1 IS_NEWER_THAN file2)``
True if ``file1`` is newer than ``file2`` or if one of the two files doesn't True if ``file1`` is newer than ``file2`` or if one of the two files doesn't
exist. Behavior is well-defined only for full paths. If the file exist. Behavior is well-defined only for full paths. If the file
@@ -173,10 +175,12 @@ File Operations
of a tie. This includes the case of passing the same file name for of a tie. This includes the case of passing the same file name for
both file1 and file2. both file1 and file2.
``if(IS_DIRECTORY path-to-directory)`` ``if(IS_DIRECTORY path)``
True if the given name is a directory. Behavior is well-defined only True if ``path`` is a directory. Behavior is well-defined only
for full paths. for full paths.
False if the given path is an empty string.
``if(IS_SYMLINK file-name)`` ``if(IS_SYMLINK file-name)``
True if the given name is a symbolic link. Behavior is well-defined True if the given name is a symbolic link. Behavior is well-defined
only for full paths. only for full paths.

View File

@@ -8,3 +8,6 @@ if(IS_DIRECTORY "${d}/")
else() else()
message(STATUS "Directory path with length ${dl} correctly does not exist.") message(STATUS "Directory path with length ${dl} correctly does not exist.")
endif() endif()
if(IS_DIRECTORY "")
message(FATAL_ERROR "IS_DIRECTORY \"\" should not exist")
endif()

View File

@@ -1,6 +1,7 @@
include(RunCMake) include(RunCMake)
run_cmake(InvalidArgument1) run_cmake(InvalidArgument1)
run_cmake(exists)
run_cmake(IsDirectory) run_cmake(IsDirectory)
run_cmake(IsDirectoryLong) run_cmake(IsDirectoryLong)
run_cmake(duplicate-deep-else) run_cmake(duplicate-deep-else)

View File

@@ -0,0 +1,3 @@
if(EXISTS "")
message(FATAL_ERROR "EXISTS \"\" should not exist")
endif()