In my CMake script I want to see if I have a file on my system, and if it is there do something with it, otherwise do something with a default file. Here is the code:
find_file( ${project_name}_${customer}_config ${ROOT}/configuration/${customer}/configuration.${project_name}.xml ) if(NOT ${${project_name}_${customer}_config} STREQUAL ${project_name}_${customer}_config-NOTFOUND ) configure_file(${ROOT}/configuration/${customer}/configuration.${project_name}.xml ${CMAKE_CURRENT_BINARY_DIR}/conf/configuration.xml) else() configure_file(${FAPP_ROOT}/configuration/Default/configuration.${project_name}.xml ${CMAKE_CURRENT_BINARY_DIR}/conf/configuration.xml) endif()
But it seems, this is not working.
What is the proper way of checking if a file exists in CMake?
Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.
CMakeLists. txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both). When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory.
A CMAKE file is a CMake language source file used by CMake, a collection of tools designed to build, test, and package software. The file may contain a script or module written in the CMake language. CMake Language source files are organized into "Directories" (CMakeLists. txt), "Scripts" (script.
The proper way to check if a file exists, if you already know the full path name to the file is simply:
if(EXISTS "${ROOT}/configuration/${customer}/configuration.${project_name}.xml") ... else() ... endif()
You should be able to just use
if(NOT ${project_name}_${customer}_config)
From the docs:
if(<constant>)
True if the constant is 1, ON, YES, TRUE, Y, or a non-zero number. False if the constant is 0, OFF, NO, FALSE, N, IGNORE, "", or ends in the suffix '-NOTFOUND'.
However, if the file is found using find_file
, the value is cached, and subsequent runs of CMake will not try to find it again. To force a recheck on every run, before the find_file
call do:
unset(${project_name}_${customer}_config CACHE)
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