Search code examples
filecmakefile-exists

CMake check that a local file exists


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?


Solution

  • 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()