3

I've seen different ways of setting / using Cache variables in Cmake. What is the standard?

What's the point of setting an empty string ("") to the cache variable?

Example

set(NVTX_ROOT_DIR "" CACHE PATH "Folder contains NVIDIA NVTX")
Chaitanya Bapat
  • 1,328
  • 2
  • 17
  • 37

1 Answers1

2

What's the point of setting an empty string ("") to the cache variable?

It is not just setting a value for a CACHE variable.

Command flow set(CACHE) normally performs two things:

  1. Declares the parameter (assigns description and possibly the type)
  2. Sets the parameter's default value. (That is, if the parameter is already set, its value isn't changed).

Empty value for CACHE variable could mean that a parameter denoted by this variable is not set. Depending on the project which uses it, this could be interpreted as:

  • Do not use functionality described by the parameter.
  • Emit an error telling the parameter should be set by a user.

In CMake code checking the parameter could be implemented with simple if(VAR):

if(NVTX_ROOT_DIR)
  # The parameter is set
else()
  # The parameter is not set
endif()

While if(NVTX_ROOT_DIR) is false even if the variable is not set, declaring the parameter is impossible without setting its value. So empty string as a default value is a logical choice for being able to use simple if(NVTX_ROOT_DIR) for check absence of the parameter's setting.

Tsyvarev
  • 45,732
  • 15
  • 64
  • 98
  • is there a place to understand cmake better (apart from the cmake official doc)? Thanks – Chaitanya Bapat Jan 23 '20 at 01:10
  • 1
    @ChaitanyaBapat You can start with the links in the CMake tag About section [here](https://stackoverflow.com/tags/cmake/info). I also think [this](https://preshing.com/20170511/how-to-build-a-cmake-based-project/#the-configure-and-generate-steps) article is helpful to get started. Specifically, for your question, you often see the empty quotes cache variables used in conjunction with CMake's `find_path` and `find_package` commands, as seen [here](https://github.com/CEED/GLVis/blob/ab82359a9b8adb548369a9514c183703140bd7b2/CMakeLists.txt#L16). – squareskittles Jan 23 '20 at 12:16
  • Since NVTX_ROOT_DIR is deprecated (according to https://cmake.org/cmake/help/latest/policy/CMP0074.html - *_ROOT is to be used in favor of *_ROOT_DIR), since it is deprecated does it make sense to set values to it? – Chaitanya Bapat Jan 24 '20 at 08:44