No way retrieve the value of the --prefix argument at install-time?

I have a project that, if a CMAKE_INSTALL_PREFIX is provided at configure/generation-time, provides that prefix to the project code to help out with certain search paths and file storage logic.

I wanted to provide a warning to the developer if they configured by passing a prefix, but then they try to call cmake --install . --prefix <prefix> with a different prefix than they configured with.

I assumed that I could do this with something like the following:

if(NOT CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  set(CONFIGURE_PREFIX ${CMAKE_INSTALL_PREFIX})
endif()
install(CODE "
  if(NOT \"${CMAKE_INSTALL_PREFIX}\" STREQUAL \"${CONFIGURE_PREFIX}\")
    message(AUTHOR_WARNING \" Install prefix differs from configured install prefix! \")
  endif()
")

But the value of CMAKE_INSTALL_PREFIX is still the same as CONFIGURE_PREFIX regardless of the argument passed with --prefix.

This might be a bit of an edge case, because who would pass an install prefix different than the one they configured with, but isn’t it a bit odd that we can’t access the contents of a provided --prefix argument at install time?

This is actually an extremely common case! CPack does this when it produces packages, for example. It uses an install step with a prefix override to install to a temporary staging area, then it creates the final package from that staging area’s contents.

I would recommend rethinking your approach. The CMAKE_INSTALL_PREFIX set at configure time should be thought of as just setting the default install location. The user is free to install to anywhere they want, and you should expect that many will override the default.

1 Like