Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if string is a valid directory (if it is a drive letter)

Tags:

cmake

I've stumbled upon an oddity when dealing with cmake.

When I try to check whether a string is a valid directory or not I am using IS_DIRECTORY however if the string I am testing a drive I get a false.

Repro code:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.3)
IF( IS_DIRECTORY "C:/") # "C:/Windows" works
    MESSAGE(STATUS "Is a dir")
ELSE()
    MESSAGE(STATUS "Is not a dir")
ENDIF()

Which always yield false for C:/ and true for C:/Windows

Is there a way to check whether C:/ is a valid 'directory' without having the pain to create a file on this location and check whether this succeeds and then delete it?

The context in which the dir check is performed is to obtain a read only location and creating a file might entirely fail or require elevated rights (which are not desired in this situation)

like image 968
Samuel Avatar asked Sep 30 '25 09:09

Samuel


1 Answers

Thanks to @Fraser I tracked this behaviour being present in CMake version 2.8.11.2 but not in 2.8.12.2. However AFAIK there is no changelog entry in http://www.cmake.org/files/v2.8/CMakeChangeLog-2.8.12.1 mentioning any fixes to IS_DIRECTORY

I recommend using the min version to ensure consistent behaviour then

 CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12.2)
like image 74
Samuel Avatar answered Oct 02 '25 04:10

Samuel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!