I have a custom build command that needs to check if a certain file exists. I tried using
IF(EXISTS "/the/file")
...
ELSE()
...
ENDIF()
but that test is only evaluated one; when cmake is first run. I need it to perform the test every time a make is done. What's the method to check at make-time? Thanks.
From the CMake documentation, the file EXISTS check is only well-defined for full paths: True if the named file or directory exists. Behavior is well-defined only for full paths. 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.
Any non-integer version component or non-integer trailing part of a version component effectively truncates the string at that point. The if command was written very early in CMake's history, predating the $ {} variable evaluation syntax, and for convenience evaluates variables named by its arguments as shown in the above signatures.
If these sources are in the same directory as your current CMake file, you can use CMAKE_CURRENT_SOURCE_DIR: @МаркТюков Yes, relative paths do work in commands such as add_executable () and add_library ().
Variable Expansion ¶ The if command was written very early in CMake's history, predating the $ {} variable evaluation syntax, and for convenience evaluates variables named by its arguments as shown in the above signatures. Note that normal variable evaluation with $ {} applies before the if command even receives the arguments.
You can use add_custom_command
to invoke CMake itself in script mode by using the -P
command line option.
So your command would be something like:
set(FileToCheck "/the/file")
add_custom_command(TARGET MyExe
POST_BUILD
COMMAND ${CMAKE_COMMAND}
-DFileToCheck=${FileToCheck}
-P ${CMAKE_CURRENT_SOURCE_DIR}/check.cmake
COMMENT "Checking if ${FileToCheck} exists...")
and your script file "check.cmake" would be something like:
if(EXISTS ${FileToCheck})
message("${FileToCheck} exists.")
else()
message("${FileToCheck} doesn't exist.")
endif()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With