file(TIMESTAMP) doesn't treat relative paths as located in CMAKE_CURRENT_SOURCE_DIR
Description
If you give other file()
commands (like file(STRINGS)
) a relative path, they treat the path as relative to CMAKE_CURRENT_SOURCE_DIR
. However, file(TIMESTAMP)
seems to only search the current directory, which is an issue when using out-of-source builds.
Steps To Reproduce
- Create a file named
VERSION
next toCMakeLists.txt
. Put random data in it. - Add to
CMakeLists.txt
:file(STRINGS VERSION VERSION) message(STATUS "VERSION: ${VERSION}") file(TIMESTAMP VERSION VERSION_DATE "%Y-%m-%d" UTC) message(STATUS "VERSION_DATE: ${VERSION_DATE}")
- Notice that when using an out-of-source build,
VERSION_DATE
is blank. - Now replace the previous script with:
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" VERSION) message(STATUS "VERSION: ${VERSION}") file(TIMESTAMP "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" VERSION_DATE "%Y-%m-%d" UTC) message(STATUS "VERSION_DATE: ${VERSION_DATE}")
- Notice that the out-of-source build now works perfectly.
Suggested Fix
if (!cmsys::SystemTools::FileIsFullPath(fileName)) {
fileName =
cmStrCat(status.GetMakefile().GetCurrentSourceDirectory(), '/', args[1]);
}
(Code taken from https://gitlab.kitware.com/cmake/cmake/-/blob/9e94510475d02b42f2c33e9c242de51770851105/Source/cmFileCommand.cxx#L2839.)