How to modify a cmake variable based on whether "install" has been specified on command line

I am using header file generated by configure_file() to specify a path to a json file which is parsed by my c++ application. In my CMakeLists.txt file I set a CMake variable equal to the path I want which is used in the the * file.

I would like to modify a CMake variable (the path) base on whether I’m just building (i.e. command line “make”), in which variable would point to somewhere in the build directory or building and installing (i.e. command line “make install”) in which case I’d set my variable to usr/local…

What can I use in a conditional to modify my CMake variable based on whether “install” was specified at the command line? I can’t find this.

Conditionally specifying the location of a non-compiled file based on whether the target is being installed seems like a common use case, is there a more standard way of doing this? generator expressions are related, but seem to be used specifically for specifying target properties.

You cannot do this; the install script may also be run manually via cmake -P cmake_install.cmake (and is, in fact, all that the install target really does). Instead, I would consider other potential solutions:

  • make the build tree look like the install tree and always use relative paths from some known anchor (using platform APIs to ask “where does the binary containing this function live?”)
  • bake the install prefix into the binary too and check if some “landmark” of the build tree exists; if not, assume it is an install tree and use that logic