Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 347
Ferenc Deak Avatar asked Oct 25 '12 11:10

Ferenc Deak


People also ask

What does Add_subdirectory do 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.

What is CMakeLists txt?

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.

What is a .CMake file?

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.


2 Answers

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() 
like image 146
DLRdave Avatar answered Oct 05 '22 22:10

DLRdave


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) 
like image 42
Fraser Avatar answered Oct 05 '22 23:10

Fraser