CMake, how to set different value in a configure_file in Build vs Install

Solution for CMake, how to set different value in a configure_file in Build vs Install
is Given Below:

I have a simple CMake project with CTest and CPack. It uses the Lua C API to load and execute an script file called script.lua.
This script will be in different location when built vs when installed/packed, it’s location would be:

[build]  : ${CMAKE_CURRENT_SOURCE_DIR}/src/scripts
[install]: ../scripts (relative to app which is in bin directory)

What I’m trying to achieve here is to have install step regenerate configure_file then rebuild using new configure_file and only then proceed to do the normal install step and of course revert the configure_file back to it’s original state afterwards.

Any help regarding this issue is appreciated.

My understanding is that CMake’s configure_file command has its full effect during the execution of the cmake program. It has no representation in generated makefiles, or whatever other build system components cmake generates. Thus, if you want to configure a file differently for installation than for pre-installation testing,

  1. You would need to perform completely separate builds (starting with executing cmake) for the two cases, and

  2. You would need to use some attribute of the cmake command line or execution environment to convey the wanted information, such as using a -D option to define a CMake variable on the command line.

I advise you not to pursue this route. Aside from being overcomplicated, it’s also poor form to install a different build of the software than is tested.

You have a variety of alternatives that could serve better. Among those are

  • Give the program itself the ability to accept a custom location for the Lua script. That is, make it recognize a command-line argument or environment variable that serves this purpose. Make use of that during pre-installation testing.

  • If indeed the program is using a relative path to locate the script at runtime, then just (have CMake) put a copy of the script at the appropriate location in the build tree, so that the program will find it normally during testing.